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