linked_list.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef LINKEDLIST_H
  17. #define LINKEDLIST_H
  18. #include "types.h"
  19. enum direction{FORWARD,BACKWARD};
  20. template<class TYPE> class LinkedListIterator;
  21. template<class TYPE>
  22. class ListElement
  23. {
  24. private:
  25. TYPE data;
  26. ListElement<TYPE>* next;
  27. ListElement<TYPE>* prev;
  28. public:
  29. ListElement ();
  30. ListElement (const TYPE&);
  31. ListElement (const ListElement<TYPE>&);
  32. ~ListElement ();
  33. ListElement<TYPE>& operator= (const ListElement<TYPE>&);
  34. ListElement<TYPE>* GetLast ()
  35. {
  36. ListElement<TYPE>* tmp = this;
  37. while (tmp->GetNext()) {
  38. tmp = tmp->GetNext();
  39. }
  40. return tmp;
  41. }
  42. ListElement<TYPE>* GetNext () const { return next ; }
  43. ListElement<TYPE>* GetPrev () const { return prev ; }
  44. inline TYPE& GetData () { return data ; }
  45. inline const TYPE& GetData () const { return data ; }
  46. void SetData ( const TYPE& d ) { data = d ; } // Quagmire - this may look like a mem leak, but dont change it, this behavior is expected where it's called
  47. void SetLastNext ( ListElement<TYPE>* p )
  48. {
  49. GetLast()->SetNext(p);
  50. }
  51. void SetNext (ListElement<TYPE>* n) { next = n ; }
  52. void SetPrev (ListElement<TYPE>* p) { prev = p ; }
  53. void ReplaceData(const TYPE&);
  54. };
  55. template<class TYPE>
  56. class LinkedList
  57. {
  58. private:
  59. int32 count;
  60. ListElement<TYPE>* first;
  61. bool list_destructor_invoked;
  62. public:
  63. LinkedList();
  64. ~LinkedList();
  65. bool dont_delete;
  66. LinkedList<TYPE>& operator= (const LinkedList<TYPE>&);
  67. void Append (const TYPE&);
  68. void Insert (const TYPE&);
  69. TYPE Pop();
  70. TYPE PeekTop();
  71. void Clear();
  72. void LCount() { count--; }
  73. void ResetCount() { count=0; }
  74. int32 Count() { return count; }
  75. friend class LinkedListIterator<TYPE>;
  76. };
  77. template<class TYPE>
  78. class LinkedListIterator
  79. {
  80. private:
  81. LinkedList<TYPE>& list;
  82. ListElement<TYPE>* current_element;
  83. direction dir;
  84. public:
  85. LinkedListIterator(LinkedList<TYPE>& l,direction d = FORWARD) : list(l), dir(d) {};
  86. void Advance();
  87. const TYPE& GetData();
  88. bool IsFirst()
  89. {
  90. if (current_element->GetPrev() == 0)
  91. return true;
  92. else
  93. return false;
  94. }
  95. bool IsLast()
  96. {
  97. if (current_element->GetNext() == 0)
  98. return true;
  99. else
  100. return false;
  101. }
  102. bool MoreElements();
  103. void MoveFirst();
  104. void MoveLast();
  105. void RemoveCurrent(bool DeleteData = true);
  106. void Replace(const TYPE& new_data);
  107. void Reset();
  108. void SetDir(direction);
  109. };
  110. template<class TYPE>
  111. void LinkedListIterator<TYPE>::Advance()
  112. {
  113. if (current_element == 0)
  114. {
  115. return;
  116. }
  117. if (dir == FORWARD)
  118. {
  119. current_element = current_element->GetNext();
  120. }
  121. else
  122. {
  123. current_element = current_element->GetPrev();
  124. }
  125. if (list.list_destructor_invoked)
  126. {
  127. while(current_element && current_element->GetData() == 0)
  128. {
  129. // if (current_element == 0)
  130. // {
  131. // return;
  132. // }
  133. if (dir == FORWARD)
  134. {
  135. current_element = current_element->GetNext();
  136. }
  137. else
  138. {
  139. current_element = current_element->GetPrev();
  140. }
  141. }
  142. }
  143. }
  144. template<class TYPE>
  145. bool LinkedListIterator<TYPE>::MoreElements()
  146. {
  147. if (current_element == 0)
  148. return false;
  149. return true;
  150. }
  151. template<class TYPE>
  152. const TYPE& LinkedListIterator<TYPE>::GetData()
  153. {
  154. return current_element->GetData();
  155. }
  156. template<class TYPE>
  157. void LinkedListIterator<TYPE>::MoveFirst()
  158. {
  159. ListElement<TYPE>* prev = current_element->GetPrev();
  160. ListElement<TYPE>* next = current_element->GetNext();
  161. if (prev == 0)
  162. {
  163. return;
  164. }
  165. // if (prev != 0)
  166. // {
  167. prev->SetNext(next);
  168. // }
  169. if (next != 0)
  170. {
  171. next->SetPrev(prev);
  172. }
  173. current_element->SetPrev(0);
  174. current_element->SetNext(list.first);
  175. list.first->SetPrev(current_element);
  176. list.first = current_element;
  177. }
  178. template<class TYPE>
  179. void LinkedListIterator<TYPE>::MoveLast()
  180. {
  181. ListElement<TYPE>* prev = current_element->GetPrev();
  182. ListElement<TYPE>* next = current_element->GetNext();
  183. if (next == 0)
  184. {
  185. return;
  186. }
  187. if (prev != 0)
  188. {
  189. prev->SetNext(next);
  190. }
  191. else
  192. {
  193. list.first = next;
  194. }
  195. // if (next != 0)
  196. // {
  197. next->SetPrev(prev);
  198. // }
  199. current_element->SetNext(0);
  200. current_element->SetPrev(next->GetLast());
  201. next->GetLast()->SetNext(current_element);
  202. }
  203. template<class TYPE>
  204. void LinkedListIterator<TYPE>::RemoveCurrent(bool DeleteData)
  205. {
  206. ListElement<TYPE>* save;
  207. if (list.first == current_element)
  208. {
  209. list.first = current_element->GetNext();
  210. }
  211. if (current_element->GetPrev() != 0)
  212. {
  213. current_element->GetPrev()->SetNext(current_element->GetNext());
  214. }
  215. if (current_element->GetNext() != 0)
  216. {
  217. current_element->GetNext()->SetPrev(current_element->GetPrev());
  218. }
  219. if (dir == FORWARD)
  220. {
  221. save = current_element->GetNext();
  222. }
  223. else
  224. {
  225. save = current_element->GetPrev();
  226. }
  227. current_element->SetNext(0);
  228. current_element->SetPrev(0);
  229. if (!DeleteData)
  230. current_element->SetData(0);
  231. safe_delete(current_element);
  232. current_element = save;
  233. list.LCount();
  234. }
  235. template<class TYPE>
  236. void LinkedListIterator<TYPE>::Replace(const TYPE& new_data)
  237. {
  238. current_element->ReplaceData(new_data);
  239. }
  240. template<class TYPE>
  241. void LinkedListIterator<TYPE>::Reset()
  242. {
  243. if (!(&list))
  244. {
  245. current_element=0;
  246. return;
  247. }
  248. if (dir == FORWARD)
  249. {
  250. current_element = list.first;
  251. }
  252. else
  253. {
  254. if (list.first == 0)
  255. {
  256. current_element = 0;
  257. }
  258. else
  259. {
  260. current_element = list.first->GetLast();
  261. }
  262. }
  263. if (list.list_destructor_invoked)
  264. {
  265. while(current_element && current_element->GetData() == 0)
  266. {
  267. // if (current_element == 0)
  268. // {
  269. // return;
  270. // }
  271. if (dir == FORWARD)
  272. {
  273. current_element = current_element->GetNext();
  274. }
  275. else
  276. {
  277. current_element = current_element->GetPrev();
  278. }
  279. }
  280. }
  281. }
  282. template<class TYPE>
  283. void LinkedListIterator<TYPE>::SetDir(direction d)
  284. {
  285. dir = d;
  286. }
  287. template<class TYPE>
  288. ListElement<TYPE>::ListElement(const TYPE& d)
  289. {
  290. data = d;
  291. next = 0;
  292. prev = 0;
  293. }
  294. template<class TYPE>
  295. ListElement<TYPE>::~ListElement()
  296. {
  297. // cout << "ListElement<TYPE>::~ListElement()" << endl;
  298. if (data != 0)
  299. safe_delete(data);
  300. data = 0;
  301. if (next != 0)
  302. {
  303. safe_delete(next);
  304. next = 0;
  305. }
  306. }
  307. template<class TYPE>
  308. void ListElement<TYPE>::ReplaceData(const TYPE& new_data)
  309. {
  310. if (data != 0)
  311. safe_delete(data);
  312. data = new_data;
  313. }
  314. template<class TYPE>
  315. LinkedList<TYPE>::LinkedList()
  316. {
  317. list_destructor_invoked = false;
  318. first = 0;
  319. count = 0;
  320. dont_delete = false;
  321. }
  322. template<class TYPE>
  323. LinkedList<TYPE>::~LinkedList()
  324. {
  325. list_destructor_invoked = true;
  326. if(!dont_delete)
  327. Clear();
  328. }
  329. template<class TYPE>
  330. void LinkedList<TYPE>::Clear() {
  331. while (first) {
  332. ListElement<TYPE>* tmp = first;
  333. first = tmp->GetNext();
  334. tmp->SetNext(0);
  335. safe_delete(tmp);
  336. }
  337. ResetCount();
  338. }
  339. template<class TYPE>
  340. void LinkedList<TYPE>::Append(const TYPE& data)
  341. {
  342. ListElement<TYPE>* new_element = new ListElement<TYPE>(data);
  343. if (first == 0)
  344. {
  345. first = new_element;
  346. }
  347. else
  348. {
  349. new_element->SetPrev(first->GetLast());
  350. first->SetLastNext(new_element);
  351. }
  352. count++;
  353. }
  354. template<class TYPE>
  355. void LinkedList<TYPE>::Insert(const TYPE& data)
  356. {
  357. ListElement<TYPE>* new_element = new ListElement<TYPE>(data);
  358. new_element->SetNext(first);
  359. if (first != 0)
  360. {
  361. first->SetPrev(new_element);
  362. }
  363. first = new_element;
  364. count++;
  365. }
  366. template<class TYPE>
  367. TYPE LinkedList<TYPE>::Pop() {
  368. TYPE ret = 0;
  369. if (first) {
  370. ListElement<TYPE>* tmpdel = first;
  371. first = tmpdel->GetNext();
  372. if (first)
  373. first->SetPrev(0);
  374. ret = tmpdel->GetData();
  375. tmpdel->SetData(0);
  376. tmpdel->SetNext(0);
  377. safe_delete(tmpdel);
  378. count--;
  379. }
  380. return ret;
  381. }
  382. template<class TYPE>
  383. TYPE LinkedList<TYPE>::PeekTop() {
  384. if (first)
  385. return first->GetData();
  386. return 0;
  387. }
  388. #endif