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