Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Robot2016_2-0 by
Map/nVector.h@46:be4eebf40568, 2016-04-25 (annotated)
- Committer:
- IceTeam
- Date:
- Mon Apr 25 12:42:32 2016 +0000
- Revision:
- 46:be4eebf40568
- Child:
- 54:be4ea8da9057
Petit commit de batard;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IceTeam | 46:be4eebf40568 | 1 | #ifndef VECTOR_H |
IceTeam | 46:be4eebf40568 | 2 | #define VECTOR_H |
IceTeam | 46:be4eebf40568 | 3 | |
IceTeam | 46:be4eebf40568 | 4 | template<class T> |
IceTeam | 46:be4eebf40568 | 5 | class elem { |
IceTeam | 46:be4eebf40568 | 6 | public: |
IceTeam | 46:be4eebf40568 | 7 | elem () { ; } |
IceTeam | 46:be4eebf40568 | 8 | elem (T nval) : val (nval) { next = NULL; prev = NULL; } |
IceTeam | 46:be4eebf40568 | 9 | elem (T nval, elem* nprev) : val (nval) { next = NULL; prev = nprev; } |
IceTeam | 46:be4eebf40568 | 10 | |
IceTeam | 46:be4eebf40568 | 11 | elem<T> * gNext () { return next; } |
IceTeam | 46:be4eebf40568 | 12 | elem<T> * gprev () { return prev; } |
IceTeam | 46:be4eebf40568 | 13 | T& getVal () { return val; } |
IceTeam | 46:be4eebf40568 | 14 | |
IceTeam | 46:be4eebf40568 | 15 | void sNext (elem<T> * nnext) { next = nnext; } |
IceTeam | 46:be4eebf40568 | 16 | void sPrev (elem<T> * nprev) { prev = nprev; } |
IceTeam | 46:be4eebf40568 | 17 | |
IceTeam | 46:be4eebf40568 | 18 | private: |
IceTeam | 46:be4eebf40568 | 19 | T val; |
IceTeam | 46:be4eebf40568 | 20 | elem<T> * next; |
IceTeam | 46:be4eebf40568 | 21 | elem<T> * prev; |
IceTeam | 46:be4eebf40568 | 22 | }; |
IceTeam | 46:be4eebf40568 | 23 | |
IceTeam | 46:be4eebf40568 | 24 | template<class T> |
IceTeam | 46:be4eebf40568 | 25 | class nVector { |
IceTeam | 46:be4eebf40568 | 26 | public: |
IceTeam | 46:be4eebf40568 | 27 | nVector () { |
IceTeam | 46:be4eebf40568 | 28 | first = NULL; |
IceTeam | 46:be4eebf40568 | 29 | curr = NULL; |
IceTeam | 46:be4eebf40568 | 30 | init = false; |
IceTeam | 46:be4eebf40568 | 31 | VectorSize = 0; |
IceTeam | 46:be4eebf40568 | 32 | } |
IceTeam | 46:be4eebf40568 | 33 | |
IceTeam | 46:be4eebf40568 | 34 | void push_back (T val) { |
IceTeam | 46:be4eebf40568 | 35 | if (init) { |
IceTeam | 46:be4eebf40568 | 36 | setCurrLast (); |
IceTeam | 46:be4eebf40568 | 37 | elem<T> * n = new elem<T> (val, curr); |
IceTeam | 46:be4eebf40568 | 38 | curr->sNext (n); |
IceTeam | 46:be4eebf40568 | 39 | n->sPrev (curr); |
IceTeam | 46:be4eebf40568 | 40 | VectorSize++; |
IceTeam | 46:be4eebf40568 | 41 | } |
IceTeam | 46:be4eebf40568 | 42 | else { |
IceTeam | 46:be4eebf40568 | 43 | init = true; |
IceTeam | 46:be4eebf40568 | 44 | first = new elem<T> (val); |
IceTeam | 46:be4eebf40568 | 45 | VectorSize = 1; |
IceTeam | 46:be4eebf40568 | 46 | } |
IceTeam | 46:be4eebf40568 | 47 | } |
IceTeam | 46:be4eebf40568 | 48 | |
IceTeam | 46:be4eebf40568 | 49 | void push_first (T val) { |
IceTeam | 46:be4eebf40568 | 50 | if (init) { |
IceTeam | 46:be4eebf40568 | 51 | curr = first; |
IceTeam | 46:be4eebf40568 | 52 | elem<T> * n = new elem<T> (val); |
IceTeam | 46:be4eebf40568 | 53 | curr->sPrev (n); |
IceTeam | 46:be4eebf40568 | 54 | n->sNext (curr); |
IceTeam | 46:be4eebf40568 | 55 | first = n; |
IceTeam | 46:be4eebf40568 | 56 | VectorSize++; |
IceTeam | 46:be4eebf40568 | 57 | } |
IceTeam | 46:be4eebf40568 | 58 | else { |
IceTeam | 46:be4eebf40568 | 59 | init = true; |
IceTeam | 46:be4eebf40568 | 60 | first = new elem<T> (val); |
IceTeam | 46:be4eebf40568 | 61 | VectorSize = 1; |
IceTeam | 46:be4eebf40568 | 62 | } |
IceTeam | 46:be4eebf40568 | 63 | } |
IceTeam | 46:be4eebf40568 | 64 | |
IceTeam | 46:be4eebf40568 | 65 | bool erase (int index) { |
IceTeam | 46:be4eebf40568 | 66 | bool worked = true; |
IceTeam | 46:be4eebf40568 | 67 | |
IceTeam | 46:be4eebf40568 | 68 | if (init) { |
IceTeam | 46:be4eebf40568 | 69 | if (index > 0 && index < VectorSize) { |
IceTeam | 46:be4eebf40568 | 70 | if (setCurrIndex (index)) { |
IceTeam | 46:be4eebf40568 | 71 | elem<T>* p = curr->gprev (); |
IceTeam | 46:be4eebf40568 | 72 | if (p != NULL) |
IceTeam | 46:be4eebf40568 | 73 | p->sNext (curr->gNext ()); |
IceTeam | 46:be4eebf40568 | 74 | |
IceTeam | 46:be4eebf40568 | 75 | elem<T>* n = curr->gNext (); |
IceTeam | 46:be4eebf40568 | 76 | if (n != NULL) |
IceTeam | 46:be4eebf40568 | 77 | n->sPrev (p); |
IceTeam | 46:be4eebf40568 | 78 | |
IceTeam | 46:be4eebf40568 | 79 | delete curr; |
IceTeam | 46:be4eebf40568 | 80 | |
IceTeam | 46:be4eebf40568 | 81 | VectorSize--; |
IceTeam | 46:be4eebf40568 | 82 | if (VectorSize <= 0) { |
IceTeam | 46:be4eebf40568 | 83 | init = false; |
IceTeam | 46:be4eebf40568 | 84 | } |
IceTeam | 46:be4eebf40568 | 85 | } |
IceTeam | 46:be4eebf40568 | 86 | } |
IceTeam | 46:be4eebf40568 | 87 | else if (index == 0 && index < VectorSize) { |
IceTeam | 46:be4eebf40568 | 88 | elem<T> * n = first->gNext (); |
IceTeam | 46:be4eebf40568 | 89 | |
IceTeam | 46:be4eebf40568 | 90 | if (n != NULL) { |
IceTeam | 46:be4eebf40568 | 91 | n->sPrev (NULL); |
IceTeam | 46:be4eebf40568 | 92 | delete first; |
IceTeam | 46:be4eebf40568 | 93 | first = n; |
IceTeam | 46:be4eebf40568 | 94 | VectorSize--; |
IceTeam | 46:be4eebf40568 | 95 | } |
IceTeam | 46:be4eebf40568 | 96 | else { |
IceTeam | 46:be4eebf40568 | 97 | init = false; |
IceTeam | 46:be4eebf40568 | 98 | delete first; |
IceTeam | 46:be4eebf40568 | 99 | VectorSize = 0; |
IceTeam | 46:be4eebf40568 | 100 | } |
IceTeam | 46:be4eebf40568 | 101 | } |
IceTeam | 46:be4eebf40568 | 102 | else { |
IceTeam | 46:be4eebf40568 | 103 | worked = false; |
IceTeam | 46:be4eebf40568 | 104 | } |
IceTeam | 46:be4eebf40568 | 105 | } |
IceTeam | 46:be4eebf40568 | 106 | else { |
IceTeam | 46:be4eebf40568 | 107 | worked = false; |
IceTeam | 46:be4eebf40568 | 108 | } |
IceTeam | 46:be4eebf40568 | 109 | return worked; |
IceTeam | 46:be4eebf40568 | 110 | } |
IceTeam | 46:be4eebf40568 | 111 | |
IceTeam | 46:be4eebf40568 | 112 | void concatenate (nVector<T>& t) { |
IceTeam | 46:be4eebf40568 | 113 | for (int i = 0; i < t.size (); ++i) |
IceTeam | 46:be4eebf40568 | 114 | push_back (t[i]); |
IceTeam | 46:be4eebf40568 | 115 | } |
IceTeam | 46:be4eebf40568 | 116 | |
IceTeam | 46:be4eebf40568 | 117 | int size () { return VectorSize; } |
IceTeam | 46:be4eebf40568 | 118 | bool empty () { return !init; } |
IceTeam | 46:be4eebf40568 | 119 | |
IceTeam | 46:be4eebf40568 | 120 | T& operator[](int ind) { |
IceTeam | 46:be4eebf40568 | 121 | setCurrIndex (ind); |
IceTeam | 46:be4eebf40568 | 122 | return curr->getVal (); |
IceTeam | 46:be4eebf40568 | 123 | } |
IceTeam | 46:be4eebf40568 | 124 | |
IceTeam | 46:be4eebf40568 | 125 | void show () { |
IceTeam | 46:be4eebf40568 | 126 | for (int i = 0; i < VectorSize; ++i) { |
IceTeam | 46:be4eebf40568 | 127 | setCurrIndex (i); |
IceTeam | 46:be4eebf40568 | 128 | std::cout << "(" << curr << ") Element " << i + 1 << " pere : " << curr->gprev() << ", fils " << curr->gNext () << std::endl; |
IceTeam | 46:be4eebf40568 | 129 | } |
IceTeam | 46:be4eebf40568 | 130 | } |
IceTeam | 46:be4eebf40568 | 131 | |
IceTeam | 46:be4eebf40568 | 132 | void clear () { |
IceTeam | 46:be4eebf40568 | 133 | while (VectorSize != 0) { |
IceTeam | 46:be4eebf40568 | 134 | erase (0); |
IceTeam | 46:be4eebf40568 | 135 | } |
IceTeam | 46:be4eebf40568 | 136 | } |
IceTeam | 46:be4eebf40568 | 137 | private: |
IceTeam | 46:be4eebf40568 | 138 | void setCurrLast () { |
IceTeam | 46:be4eebf40568 | 139 | if (init) { |
IceTeam | 46:be4eebf40568 | 140 | curr = first; |
IceTeam | 46:be4eebf40568 | 141 | while (curr->gNext () != NULL) { |
IceTeam | 46:be4eebf40568 | 142 | curr = curr->gNext (); |
IceTeam | 46:be4eebf40568 | 143 | } |
IceTeam | 46:be4eebf40568 | 144 | } |
IceTeam | 46:be4eebf40568 | 145 | } |
IceTeam | 46:be4eebf40568 | 146 | |
IceTeam | 46:be4eebf40568 | 147 | bool setCurrIndex (int index) { |
IceTeam | 46:be4eebf40568 | 148 | curr = first; |
IceTeam | 46:be4eebf40568 | 149 | bool worked = true; |
IceTeam | 46:be4eebf40568 | 150 | |
IceTeam | 46:be4eebf40568 | 151 | for (int i = 0; i < index; ++i) { |
IceTeam | 46:be4eebf40568 | 152 | if (curr->gNext () != NULL) |
IceTeam | 46:be4eebf40568 | 153 | curr = curr->gNext (); |
IceTeam | 46:be4eebf40568 | 154 | else { |
IceTeam | 46:be4eebf40568 | 155 | worked = false; |
IceTeam | 46:be4eebf40568 | 156 | break; |
IceTeam | 46:be4eebf40568 | 157 | } |
IceTeam | 46:be4eebf40568 | 158 | } |
IceTeam | 46:be4eebf40568 | 159 | return worked; |
IceTeam | 46:be4eebf40568 | 160 | } |
IceTeam | 46:be4eebf40568 | 161 | |
IceTeam | 46:be4eebf40568 | 162 | elem<T>* first; |
IceTeam | 46:be4eebf40568 | 163 | elem<T>* curr; |
IceTeam | 46:be4eebf40568 | 164 | |
IceTeam | 46:be4eebf40568 | 165 | bool init; |
IceTeam | 46:be4eebf40568 | 166 | int VectorSize; |
IceTeam | 46:be4eebf40568 | 167 | }; |
IceTeam | 46:be4eebf40568 | 168 | #endif |