isllist.hh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. //{{{ Intrusive singly linked list
  9. //
  10. // Notes:
  11. // The templatized class MUST have member:
  12. // T* next;
  13. //
  14. #ifndef ISLLIST_HPP
  15. #define ISLLIST_HPP
  16. #include "arch.hh" // for definition of i4_bool
  17. template <class T>
  18. class i4_isl_list
  19. {
  20. protected:
  21. typedef T* link;
  22. link list;
  23. public:
  24. T *first() { return list; }
  25. i4_isl_list() : list(0) {}
  26. class iterator
  27. {
  28. friend class i4_isl_list<T>;
  29. protected:
  30. link node;
  31. public:
  32. iterator() : node(0) {}
  33. iterator(const iterator &p) : node(p.node) {}
  34. iterator(link p) : node(p) {}
  35. int operator==(const iterator &p) const { return (node == p.node); }
  36. int operator!=(const iterator &p) const { return (node != p.node); }
  37. T& operator*() const { return *node; }
  38. T* operator->() const { return node; }
  39. iterator& operator++()
  40. {
  41. node = node->next;
  42. return *this;
  43. }
  44. iterator& operator++(int)
  45. {
  46. node = node->next;
  47. return *this;
  48. }
  49. };
  50. iterator end() const { return 0; }
  51. iterator begin() const { return list; }
  52. i4_bool empty() const { return begin() == end(); }
  53. iterator insert_after(iterator _pos, T &item)
  54. {
  55. item.next = (*_pos.node).next;
  56. _pos.node->next = &item;
  57. return &item;
  58. }
  59. void erase_after(iterator _pos)
  60. {
  61. _pos.node->next = _pos.node->next->next;
  62. }
  63. void insert_end(T &item)
  64. {
  65. item.next=0;
  66. if (!list)
  67. list=&item;
  68. else
  69. {
  70. link last=list;
  71. while (last->next)
  72. last=last->next;
  73. last->next=&item;
  74. }
  75. }
  76. void insert(T& item) { item.next = list; list = &item; }
  77. void erase() { list = list->next; }
  78. void destroy_all()
  79. {
  80. link p;
  81. while (list)
  82. {
  83. p = list;
  84. erase();
  85. delete p;
  86. }
  87. }
  88. iterator find(T *item)
  89. {
  90. iterator p=begin();
  91. for (;p!=end();++p)
  92. if (p.node==item)
  93. return p;
  94. return end();
  95. }
  96. void swap(i4_isl_list &other)
  97. {
  98. link other_list=other.list;
  99. other.list=list;
  100. list=other_list;
  101. }
  102. T *find_and_unlink(T *item)
  103. {
  104. iterator p=begin(), last=end();
  105. for (;p!=end(); ++p)
  106. if (p.node==item)
  107. {
  108. if (last!=end())
  109. erase_after(last);
  110. else
  111. erase();
  112. return item;
  113. }
  114. else last=p;
  115. return 0;
  116. }
  117. };
  118. #endif