biglist.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #ifndef __BIGLIST_H
  2. #define __BIGLIST_H
  3. // A biglist is a linked list of fifty item arrays.
  4. // If you add more than fifty items, another fifty item node is added.
  5. // If you delete an item, the used property is set to false.
  6. // When you iterate through the list using biglist_iterator, it
  7. // skips the unused items.
  8. // Concurrency problems are unlikely with this design.
  9. #include "idisposable.h"
  10. template <class T>
  11. class biglist_item
  12. {
  13. public:
  14. T item;
  15. bool used;
  16. };
  17. template <class T>
  18. class biglist
  19. {
  20. public:
  21. static const int BIGLIST_SIZE = 50;
  22. biglist_item<T> items[BIGLIST_SIZE];
  23. biglist *next;
  24. biglist_item<T> *operator[](int index)
  25. {
  26. biglist *loop;
  27. if (index < 0) {
  28. return nullptr;
  29. }
  30. loop = this;
  31. while (index >= BIGLIST_SIZE) {
  32. loop = loop->next;
  33. index -= BIGLIST_SIZE;
  34. if (loop == nullptr) {
  35. return nullptr;
  36. }
  37. }
  38. return &loop->items[index];
  39. }
  40. biglist()
  41. {
  42. int loop;
  43. next = nullptr;
  44. for(loop=0;loop<BIGLIST_SIZE;loop++) {
  45. items[loop].used = false;
  46. }
  47. }
  48. void deleteallitems() { // This should only be called if T inherits from idisposable.
  49. biglist *loop1;
  50. int loop2;
  51. T temp;
  52. bool should_delete = true;
  53. /* This safety check requires C++17. But my compiler is too old for it.
  54. if ((!std::is_base_of<idisposable, T>) && (!std::is_same<idisposable, T>)) {
  55. should_delete = false;
  56. } */
  57. for(loop1=this;loop1!=nullptr;loop1=loop1->next) {
  58. for(loop2=0;loop2<BIGLIST_SIZE;loop2++) {
  59. if (loop1->items[loop2].used) {
  60. temp = loop1->items[loop2].item;
  61. loop1->items[loop2].used = false;
  62. if (should_delete) {
  63. loop1->items[loop2].item = nullptr;
  64. delete (idisposable *)temp;
  65. }
  66. }
  67. }
  68. }
  69. clear(true);
  70. }
  71. ~biglist()
  72. {
  73. biglist *loop1 = next;
  74. biglist *loop2;
  75. while (loop1 != nullptr) {
  76. loop2 = loop1;
  77. loop1 = loop1->next;
  78. loop2->next = nullptr;
  79. delete loop2;
  80. }
  81. next = nullptr;
  82. }
  83. void clear(bool shorten) // If shorten is true, remove extra list items > BIGLIST_SIZE.
  84. {
  85. biglist *loop1;
  86. int loop2;
  87. if (shorten) {
  88. for(loop2=0;loop2<BIGLIST_SIZE;loop2++) {
  89. items[loop2].used = false;
  90. }
  91. while (next != nullptr) {
  92. loop1 = next;
  93. next = next->next;
  94. loop1->next = nullptr;
  95. delete loop1;
  96. }
  97. } else {
  98. for(loop1 = this;loop1 != nullptr; loop1 = loop1->next) {
  99. for(loop2=0;loop2<BIGLIST_SIZE;loop2++) {
  100. loop1->items[loop2].used = false;
  101. }
  102. }
  103. }
  104. }
  105. bool find(T item)
  106. {
  107. biglist *loop1 = this;
  108. int loop2;
  109. while (loop1 != nullptr) {
  110. for(loop2=0;loop2 < BIGLIST_SIZE;loop2++) {
  111. if ((items[loop2].used) && (items[loop2].item == item)) {
  112. return true;
  113. }
  114. }
  115. loop1 = loop1->next;
  116. }
  117. return false;
  118. }
  119. biglist_item<T> *add_to_end(T item)
  120. {
  121. // Adds an item to the end of the list.
  122. // Empty items that are not at the end of the list are ignored.
  123. biglist *loop1 = this;
  124. biglist *loop1_previous = nullptr; // loop1->previous.
  125. biglist *empty_block_parent = nullptr; // the parent of a block of empty items.
  126. int loop2;
  127. biglist *empty1 = nullptr;
  128. int empty2;
  129. bool has_items; // Does this block have items?
  130. while (true) {
  131. has_items = false;
  132. for(loop2=0;loop2 < BIGLIST_SIZE;loop2++) {
  133. if (loop1->items[loop2].used) {
  134. has_items = true;
  135. empty1 = nullptr;
  136. } else {
  137. if (empty1 == nullptr) {
  138. empty1 = loop1;
  139. empty2 = loop2;
  140. }
  141. }
  142. }
  143. if ((!has_items) && (loop1_previous != nullptr)) {
  144. // This entire list should be moved to the end.
  145. // This fixes the problem of a list that keeps getting stuff added to and deleted from.
  146. empty_block_parent = loop1_previous;
  147. }
  148. loop1_previous = loop1;
  149. loop1 = loop1->next;
  150. if (loop1 == nullptr) { // Terminating condition of the loop.
  151. if ((loop1_previous != nullptr) && (has_items) && (empty_block_parent != nullptr) && (empty1 == nullptr)) {
  152. // Move that block of empty items to the end of this list.
  153. empty1 = empty_block_parent->next;
  154. empty2 = 0;
  155. empty_block_parent->next = empty1->next;
  156. loop1_previous->next = empty1;
  157. empty1->next = nullptr;
  158. }
  159. break;
  160. }
  161. }
  162. while (empty1 != nullptr) {
  163. if (empty1->items[empty2].used) {
  164. if (++empty2 >= BIGLIST_SIZE) {
  165. empty2 = 0;
  166. empty1 = empty1->next;
  167. }
  168. } else {
  169. empty1->items[empty2].used = true;
  170. empty1->items[empty2].item = item;
  171. return empty1->items+empty2;
  172. }
  173. }
  174. empty1 = new biglist<T>();
  175. if (empty1 == nullptr) {
  176. return nullptr; // Memory error.
  177. }
  178. empty1->items[0].used = true;
  179. empty1->items[0].item = item;
  180. empty1->next = nullptr;
  181. loop1 = this;
  182. while (loop1->next != nullptr) {
  183. loop1 = loop1->next;
  184. }
  185. loop1->next = empty1;
  186. return empty1->items;
  187. }
  188. biglist_item<T> *add(T item)
  189. {
  190. // Adds the item. Returns the added item or nullptr if out of memory.
  191. biglist *loop1 = this;
  192. int loop2;
  193. biglist *empty1 = nullptr;
  194. int empty2;
  195. while (loop1 != nullptr) {
  196. for(loop2=0;loop2 < BIGLIST_SIZE;loop2++) {
  197. if (loop1->items[loop2].used) {
  198. if (loop1->items[loop2].item == item) {
  199. return loop1->items+loop2;
  200. }
  201. } else {
  202. if (empty1 == nullptr) {
  203. empty1 = loop1;
  204. empty2 = loop2;
  205. }
  206. }
  207. }
  208. loop1 = loop1->next;
  209. }
  210. while (empty1 != nullptr) {
  211. if (empty1->items[empty2].used) {
  212. if (++empty2 >= BIGLIST_SIZE) {
  213. empty2 = 0;
  214. empty1 = empty1->next;
  215. }
  216. } else {
  217. empty1->items[empty2].used = true;
  218. empty1->items[empty2].item = item;
  219. return empty1->items+empty2;
  220. }
  221. }
  222. empty1 = new biglist<T>();
  223. if (empty1 == nullptr) {
  224. return nullptr; // Memory error.
  225. }
  226. empty1->items[0].used = true;
  227. empty1->items[0].item = item;
  228. empty1->next = next;
  229. next = empty1;
  230. return empty1->items;
  231. }
  232. void remove(T item)
  233. {
  234. biglist *loop1 = this;
  235. int loop2;
  236. while (loop1 != nullptr) {
  237. for(loop2=0;loop2 < BIGLIST_SIZE;loop2++) {
  238. if ((items[loop2].used) && (items[loop2].item == item)) {
  239. items[loop2].used = false;
  240. break;
  241. }
  242. }
  243. loop1 = loop1->next;
  244. }
  245. return;
  246. }
  247. int length()
  248. {
  249. biglist *loop1 = this;
  250. int loop2;
  251. int total = 0;
  252. while (loop1 != nullptr) {
  253. for(loop2=0;loop2 < BIGLIST_SIZE;loop2++) {
  254. if (items[loop2].used) {
  255. total++;
  256. }
  257. }
  258. loop1 = loop1->next;
  259. }
  260. return total;
  261. }
  262. };
  263. template <class T>
  264. class biglist_iterator
  265. {
  266. private:
  267. biglist<T> *_list;
  268. biglist<T> *_current;
  269. int _currentpos;
  270. public:
  271. biglist_item<T> *row;
  272. T item;
  273. void movenext()
  274. {
  275. do
  276. {
  277. if (_current == nullptr) {
  278. row = nullptr;
  279. return;
  280. }
  281. if (++_currentpos >= biglist<T>::BIGLIST_SIZE) {
  282. _currentpos = 0;
  283. _current = _current->next;
  284. if (_current == nullptr) {
  285. row = nullptr;
  286. return;
  287. }
  288. }
  289. } while (!_current->items[_currentpos].used);
  290. row = _current->items+_currentpos;
  291. item = row->item;
  292. return;
  293. }
  294. void movefirst()
  295. {
  296. row = nullptr;
  297. _current = _list;
  298. _currentpos = -1;
  299. movenext();
  300. }
  301. biglist_iterator(biglist<T> *list)
  302. {
  303. _list = list;
  304. item = nullptr;
  305. movefirst();
  306. }
  307. bool eof()
  308. {
  309. return _current == nullptr;
  310. }
  311. };
  312. #endif