tlist.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #ifndef _TList_H_
  2. #define _TList_H_
  3. //////////////////////////////////////////////////////////////////////////////
  4. //
  5. // List Template Generic Implementation
  6. //
  7. //////////////////////////////////////////////////////////////////////////////
  8. class TListImpl : public IObject {
  9. public:
  10. //
  11. // Types
  12. //
  13. class ListNodeImpl : public IObjectSingle {
  14. public:
  15. ListNodeImpl* m_pprev;
  16. TRef<ListNodeImpl> m_pnext;
  17. ListNodeImpl(ListNodeImpl* pprev, ListNodeImpl* pnext) :
  18. m_pprev(pprev),
  19. m_pnext(pnext)
  20. {}
  21. virtual ~ListNodeImpl();
  22. };
  23. class IteratorImpl : public IObject {
  24. protected:
  25. TListImpl& m_list;
  26. TRef<ListNodeImpl> m_pnode;
  27. void RemoveImpl();
  28. public:
  29. IteratorImpl(TListImpl& list) :
  30. m_list(list),
  31. m_pnode(list.m_pfirst)
  32. {}
  33. bool End()
  34. {
  35. return (m_pnode == NULL);
  36. }
  37. bool First();
  38. bool Last();
  39. bool Next();
  40. bool Prev();
  41. bool IsFirst();
  42. bool IsLast();
  43. };
  44. protected:
  45. friend class IteratorImpl;
  46. //
  47. // Data Members
  48. //
  49. TRef<ListNodeImpl> m_pfirst;
  50. TRef<ListNodeImpl> m_plast;
  51. int m_count;
  52. TListImpl() :
  53. m_pfirst(NULL),
  54. m_plast(NULL),
  55. m_count(0)
  56. {}
  57. //
  58. // Methods
  59. //
  60. void PushFrontImpl(ListNodeImpl* pnew);
  61. void PushEndImpl(ListNodeImpl* pnew);
  62. void InsertBeforeImpl(ListNodeImpl* pnode, ListNodeImpl* pnew);
  63. void InsertAfterImpl(ListNodeImpl* pnode, ListNodeImpl* pnew);
  64. void RemoveNode(ListNodeImpl* premove);
  65. void PopFrontImpl();
  66. void PopEndImpl();
  67. void SetEmptyImpl();
  68. ListNodeImpl* Get(int index) const;
  69. public:
  70. int GetCount()
  71. {
  72. return m_count;
  73. }
  74. bool IsEmpty()
  75. {
  76. return (m_count == 0);
  77. }
  78. bool operator==(const TListImpl&);
  79. };
  80. //////////////////////////////////////////////////////////////////////////////
  81. //
  82. // List Template
  83. //
  84. //////////////////////////////////////////////////////////////////////////////
  85. template<
  86. class TValue,
  87. class EqualsFunctionType = DefaultEquals,
  88. class CompareFunctionType = DefaultNoCompare,
  89. class SinkFunctionType = NullFunc
  90. >
  91. class TList : public TListImpl {
  92. private:
  93. class ListNode : public TListImpl::ListNodeImpl {
  94. public:
  95. TValue m_value;
  96. ListNode(ListNodeImpl* pprev, ListNodeImpl* pnext) :
  97. TListImpl::ListNodeImpl(pprev, pnext)
  98. {}
  99. ListNode(const TValue& value, ListNodeImpl* pprev, ListNodeImpl* pnext) :
  100. TListImpl::ListNodeImpl(pprev, pnext),
  101. m_value(value)
  102. {}
  103. ListNode* GetNext()
  104. {
  105. return (ListNode*)(ListNodeImpl*)m_pnext;
  106. }
  107. ListNode* GetPrev()
  108. {
  109. return (ListNode*)(ListNodeImpl*)m_pprev;
  110. }
  111. };
  112. ListNode* FindInternal(const TValue& value)
  113. {
  114. ListNode* pfind = GetFirst();
  115. while (pfind) {
  116. if (m_fnEquals(((const TValue&)pfind->m_value), value)) {
  117. return pfind;
  118. }
  119. pfind = pfind->GetNext();
  120. }
  121. return NULL;
  122. }
  123. ListNode* GetFirst()
  124. {
  125. return (ListNode*)(ListNodeImpl*)m_pfirst;
  126. }
  127. ListNode* GetLast()
  128. {
  129. return (ListNode*)(ListNodeImpl*)m_plast;
  130. }
  131. SinkFunctionType m_fnSink;
  132. EqualsFunctionType m_fnEquals;
  133. CompareFunctionType m_fnCompare;
  134. public:
  135. class Iterator : public TListImpl::IteratorImpl {
  136. public:
  137. Iterator(TList& list) :
  138. TListImpl::IteratorImpl(list)
  139. {}
  140. TValue& Value() { return ((ListNode*)(ListNodeImpl*)m_pnode)->m_value; }
  141. void Remove()
  142. {
  143. RemoveImpl();
  144. ((TList<TValue, EqualsFunctionType, CompareFunctionType, SinkFunctionType>&)m_list).GetSink()();
  145. }
  146. void InsertBefore(const TValue& value)
  147. {
  148. m_list.InsertBeforeImpl(m_pnode, new ListNodeImpl(value, NULL, NULL));
  149. ((TList<TValue, EqualsFunctionType, CompareFunctionType, SinkFunctionType>&)m_list).GetSink()();
  150. }
  151. void InsertAfter(TValue& value)
  152. {
  153. m_list.InsertAfterImpl(m_pnode, new ListNodeImpl(value, NULL, NULL));
  154. ((TList<TValue, EqualsFunctionType, CompareFunctionType, SinkFunctionType>&)m_list).GetSink()();
  155. }
  156. };
  157. TList()
  158. {
  159. }
  160. TList(EqualsFunctionType fnEquals, CompareFunctionType fnCompare)
  161. : m_fnEquals(fnEquals), m_fnCompare(fnCompare)
  162. {
  163. }
  164. TList(TList& list)
  165. {
  166. Iterator iter(list);
  167. while (!iter.End()) {
  168. PushEnd(iter.Value());
  169. iter.Next();
  170. }
  171. }
  172. void PushFront()
  173. {
  174. PushFrontImpl(new ListNode(NULL, GetFirst()));
  175. m_fnSink();
  176. }
  177. void PushFront(const TValue& value)
  178. {
  179. PushFrontImpl(new ListNode(value, NULL, GetFirst()));
  180. m_fnSink();
  181. }
  182. void PushEnd()
  183. {
  184. PushEndImpl(new ListNode(GetLast(), NULL));
  185. m_fnSink();
  186. }
  187. void PushEnd(const TValue& value)
  188. {
  189. PushEndImpl(new ListNode(value, GetLast(), NULL));
  190. m_fnSink();
  191. }
  192. //
  193. // The compare function should return true if the first value
  194. // should be after the second value
  195. //
  196. void InsertSorted(const TValue& value)
  197. {
  198. ListNode* pnode = GetLast();
  199. while (pnode != NULL) {
  200. if (!m_fnCompare(((const TValue&)pnode->m_value), value)) {
  201. InsertAfterImpl(pnode, new ListNode(value, NULL, NULL));
  202. m_fnSink();
  203. return;
  204. }
  205. pnode = pnode->GetPrev();
  206. }
  207. PushFront(value);
  208. m_fnSink();
  209. }
  210. void InsertBefore(const TValue& valueReference, const TValue& valueNew)
  211. {
  212. InsertBeforeImpl(FindInternal(valueReference), new ListNode(valueNew, NULL, NULL));
  213. m_fnSink();
  214. }
  215. void InsertAfter(const TValue& valueReference, const TValue& valueNew)
  216. {
  217. InsertAfterImpl(FindInternal(valueReference), new ListNode(valueNew, NULL, NULL));
  218. m_fnSink();
  219. }
  220. TValue PopFront()
  221. {
  222. TValue value = GetFirst()->m_value;
  223. PopFrontImpl();
  224. m_fnSink();
  225. return value;
  226. }
  227. TValue PopEnd()
  228. {
  229. TValue value = GetLast()->m_value;
  230. PopEndImpl();
  231. m_fnSink();
  232. return value;
  233. }
  234. TValue& GetFront()
  235. {
  236. return GetFirst()->m_value;
  237. }
  238. TValue& GetEnd()
  239. {
  240. return GetLast()->m_value;
  241. }
  242. bool Find(const TValue& value)
  243. {
  244. return FindInternal(value) != NULL;
  245. }
  246. bool Remove(const TValue& value)
  247. {
  248. ListNode* premove = FindInternal(value);
  249. if (premove) {
  250. RemoveNode(premove);
  251. m_fnSink();
  252. return true;
  253. }
  254. return false;
  255. }
  256. bool Replace(const TValue& value, const TValue& valueNew)
  257. {
  258. ListNode* pfind = FindInternal(value);
  259. if (pfind) {
  260. pfind->m_value = valueNew;
  261. }
  262. m_fnSink();
  263. return false;
  264. }
  265. TValue& operator[](int index) const
  266. {
  267. return ((ListNode*)Get(index))->m_value;
  268. }
  269. void SetEmpty()
  270. {
  271. SetEmptyImpl();
  272. m_fnSink();
  273. }
  274. SinkFunctionType& GetSink()
  275. {
  276. return m_fnSink;
  277. };
  278. };
  279. //////////////////////////////////////////////////////////////////////////////
  280. //
  281. // A List as an object
  282. //
  283. //////////////////////////////////////////////////////////////////////////////
  284. template<class TValue>
  285. class TListObject : public TList<TValue> {
  286. public:
  287. };
  288. //////////////////////////////////////////////////////////////////////////////
  289. //
  290. // List Template
  291. //
  292. //////////////////////////////////////////////////////////////////////////////
  293. template<
  294. class TValue
  295. >
  296. class TPointerListObject : public IObject {
  297. private:
  298. TList<TRef<TValue> > m_list;
  299. VSNET_TNFIX TList<TRef<TValue> >::Iterator m_iter;
  300. public:
  301. TPointerListObject() :
  302. m_list(),
  303. m_iter(m_list)
  304. {
  305. }
  306. void PushFront(TValue* pvalue) { m_list.PushFront(pvalue); }
  307. void PushEnd(TValue* pvalue) { m_list.PushEnd(pvalue); }
  308. void Remove(TValue* pvalue) { m_list.Remove(pvalue); }
  309. int GetCount() { return m_list.GetCount(); }
  310. bool IsFirst() { return m_iter.IsFirst(); }
  311. bool IsLast() { return m_iter.IsLast(); }
  312. TValue* GetFirst()
  313. {
  314. if (m_iter.First()) {
  315. return m_iter.Value();
  316. }
  317. return NULL;
  318. }
  319. TValue* GetLast()
  320. {
  321. if (m_iter.Last()) {
  322. return m_iter.Value();
  323. }
  324. return NULL;
  325. }
  326. TValue* GetNext()
  327. {
  328. if (m_iter.Next()) {
  329. return m_iter.Value();
  330. }
  331. return NULL;
  332. }
  333. TValue* GetPrevious()
  334. {
  335. if (m_iter.Prev()) {
  336. return m_iter.Value();
  337. }
  338. return NULL;
  339. }
  340. TValue* GetCurrent()
  341. {
  342. if (!m_iter.End()) {
  343. return m_iter.Value();
  344. } else {
  345. return NULL;
  346. }
  347. }
  348. TValue* GetNth(int index)
  349. {
  350. GetFirst();
  351. while (index != 0) {
  352. GetNext();
  353. index--;
  354. }
  355. return GetCurrent();
  356. }
  357. TValue* RemoveCurrent()
  358. {
  359. if (!m_iter.End()) {
  360. m_iter.Remove();
  361. if (!m_iter.End()) {
  362. return m_iter.Value();
  363. }
  364. }
  365. return NULL;
  366. }
  367. };
  368. #endif