dynar.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. #include "dynar.h"
  2. #include "mem.h"
  3. #define ARRAY_ROOM 16
  4. /**
  5. * Dynar is a DYNamic ARray of size elements each of elsz size.
  6. * For simplicity, dynarr is exactly sized, that is,
  7. * it does not allocate any extra elements but the amount requested.
  8. * In other words it's capacity is equal to it's size.
  9. */
  10. struct Dynar_ {
  11. char * data;
  12. // use a char * pointer since that makes pointer arithmetic easier
  13. int size;
  14. int elsz;
  15. };
  16. /** Gets the array's size. Returns 0 if self is NULL. */
  17. int dynar_size(Dynar * self) {
  18. if(!self) return 0;
  19. return self->size;
  20. }
  21. /** Gets the amount of elements in the vector. Returns 0 if self is NULL.
  22. * Alias for dynar_size(self).
  23. **/
  24. int dynar_amount(Dynar * self) {
  25. if(!self) return 0;
  26. return self->size;
  27. }
  28. /** Gets the array's element size. Returns 0 if self is NULL. */
  29. int dynar_elementsize(Dynar * self) {
  30. if(!self) return 0;
  31. return self->elsz;
  32. }
  33. /** Frees the contents of an array. Has the same effect as emptying the array. Does not call a desctructor on any elements contained! */
  34. Dynar * dynar_done(Dynar * self) {
  35. if(!self) return NULL;
  36. mem_free(self->data);
  37. self->data = NULL;
  38. self->size = 0;
  39. return self;
  40. }
  41. /** Calls a destructor on the contents of the array if it is not null.
  42. The contents are considered to be pointers. Does not call dynar_free()!!! */
  43. Dynar * dynar_destroy(Dynar * self, MemDestructor * destroy) {
  44. int index;
  45. int size = dynar_size(self);
  46. if(!self) return self;
  47. if(!destroy) return NULL;
  48. for(index = 0; index < size ; index++) {
  49. void * ptr = dynar_getptr(self, index);
  50. destroy(ptr);
  51. }
  52. return self;
  53. }
  54. /** Calls a destructor on the contents of the array if it is not null.
  55. The contents are considered to be structs. Does not call dynar_free()!!! */
  56. Dynar * dynar_destroy_structs(Dynar * self, MemDestructor * destroy) {
  57. int index;
  58. int size = dynar_size(self);
  59. if(!self) return self;
  60. if(!destroy) return NULL;
  61. for(index = 0; index < size ; index++) {
  62. void * ptr = dynar_getdata(self, index);
  63. destroy(ptr);
  64. }
  65. return self;
  66. }
  67. /** Frees an array. Returns NULL. */
  68. Dynar * dynar_free(Dynar * self) {
  69. dynar_done(self);
  70. return mem_free(self);
  71. }
  72. /** Calls a destructor on the elements of the array and then frees the array */
  73. Dynar * dynar_free_destroy(Dynar * self, MemDestructor * destroy) {
  74. dynar_destroy(self, destroy);
  75. return dynar_free(self);
  76. }
  77. /** Calls a destructor on the elements of the array and then frees the array */
  78. Dynar * dynar_destroy_structs_and_free(Dynar * self, MemDestructor * destroy) {
  79. dynar_destroy_structs(self, destroy);
  80. return dynar_free(self);
  81. }
  82. /** Allocates a new empty array. */
  83. Dynar * dynar_alloc() {
  84. return STRUCT_ALLOC(Dynar);
  85. }
  86. /** Initializes an empty array with 0 elements of size elsz each. */
  87. Dynar * dynar_initempty(Dynar * self, int elsz) {
  88. if(!self) return NULL;
  89. if(elsz < 0) return NULL;
  90. self->size = 0;
  91. self->elsz = elsz;
  92. return self;
  93. }
  94. /** Allocates a new empty array for elements of size elsz each. */
  95. Dynar * dynar_newempty(int elsz) {
  96. Dynar * res = dynar_alloc();
  97. if(!dynar_initempty(res, elsz)) {
  98. return dynar_free(res);
  99. }
  100. return res;
  101. }
  102. /** Changes the size of the dynamic array. Newsize must be >= 1. */
  103. Dynar * dynar_size_(Dynar* self, int newsize) {
  104. int elsz = dynar_elementsize(self);
  105. void * newd = NULL;
  106. int delta;
  107. if(!self) return NULL;
  108. // don't allow a newsize of 0, since that will make realloc call
  109. // free on self->data, which we don't want.
  110. if(newsize < 1) return NULL;
  111. newd = mem_realloc(self->data, newsize * elsz);
  112. if(!newd) return NULL;
  113. // if we get here realloc was successful, so it should be safe to reassign
  114. // self->data
  115. self->data = newd;
  116. // now, empty the unused new data, but only if growing! (XXX: is this ok???)
  117. delta = newsize - self->size;
  118. if(delta > 0) {
  119. memset(self->data + (self->size * self->elsz), 0, (delta * self->elsz));
  120. }
  121. self->size = newsize;
  122. return self;
  123. }
  124. /** Initializes a new array with a amount elements of size
  125. elsz each. */
  126. Dynar * dynar_init(Dynar * self, int amount, int elsz) {
  127. Dynar * aid = dynar_initempty(self, elsz);
  128. if(!aid) return NULL;
  129. if(!dynar_size_(self, amount)) return NULL;
  130. return self;
  131. }
  132. /** Initializes a new array with a capacity to store amount void* pointers.*/
  133. Dynar * dynar_initptr(Dynar * self, int amount) {
  134. return dynar_init(self, amount, sizeof(void *));
  135. }
  136. /** Allocates a new array with amount elements of size elsz each. */
  137. Dynar * dynar_new(int amount, int elsz) {
  138. Dynar * res = dynar_alloc();
  139. if(!dynar_init(res, amount, elsz)) {
  140. return dynar_free(res);
  141. }
  142. return res;
  143. }
  144. /** Allocates a new array that will be able to contain amount void * pointers.*/
  145. Dynar * dynar_newptr(int amount) {
  146. return dynar_new(amount, sizeof(void *));
  147. }
  148. /** Sets the amount of elements of the the array, but ony if
  149. * amount bigger than the bigger than the current size.
  150. * Returns NULL if the array was not grown, otherwise returns self.
  151. */
  152. Dynar * dynar_grow(Dynar * self, int amount) {
  153. int mysize = dynar_size(self);
  154. if (mysize >= amount) return NULL;
  155. return dynar_size_(self, amount);
  156. }
  157. /** Checks if the index is smaller than the array's available room . */
  158. int dynar_sizeindex_ok(Dynar * self, int index) {
  159. if(!self) return FALSE;
  160. return index < dynar_size(self);
  161. }
  162. /** Checks if the int index is smaller than the array's available room. */
  163. int dynar_index_ok(Dynar * self, int index) {
  164. if(!self) return FALSE;
  165. if (index < 0) return FALSE;
  166. return dynar_sizeindex_ok(self, (int)(index));
  167. }
  168. /** Returns a pointer to the index-th element of the array.
  169. Does no bounds checking! */
  170. void * dynar_getraw_unsafe(Dynar * self, int index) {
  171. return self->data + (index * self->elsz);
  172. }
  173. /** Copies dynar_elementsize(self) of bytes from the index-th element
  174. of the array to out, which must be pointing to a bufer of at least
  175. dynar_elementsize(self). Does no bounds checking!
  176. Returns NULL on failure, out on success.
  177. */
  178. void * dynar_getcopy_unsafe(Dynar * self, int index, void * out) {
  179. char * cptr = (char *) dynar_getraw_unsafe(self, index);
  180. int size = dynar_elementsize(self);
  181. if((!self) || (!out) || (!cptr)) return NULL;
  182. mem_move(out, cptr, size);
  183. return out;
  184. }
  185. /** Copies dynar_elementsize(self) of bytes from the data pointed to
  186. * by ptr into the location pointed to by index.
  187. * Does no bounds checking!
  188. */
  189. Dynar * dynar_putraw_unsafe(Dynar * self, int index, void * ptr) {
  190. char * cptr = (char *) dynar_getraw_unsafe(self, index);
  191. int size = dynar_elementsize(self);
  192. if((!self) || (!ptr) || (!cptr)) return NULL;
  193. mem_move(cptr, ptr, size);
  194. return self;
  195. }
  196. /** Returns a pointer to the index-th element of the array.
  197. Does bounds checking and return NULL if out of bounds */
  198. void * dynar_getraw(Dynar * self, int index) {
  199. // Bounds check
  200. if(!dynar_index_ok(self, index)) { return NULL; }
  201. return dynar_getraw_unsafe(self, index);
  202. }
  203. /** Returns a pointer to the index-th element of the array.
  204. Does bounds checking and return NULL if out of bounds */
  205. void * dynar_getcopy(Dynar * self, int index, void * ptr) {
  206. // Bounds check
  207. if(!dynar_index_ok(self, index)) { return NULL; }
  208. return dynar_getcopy_unsafe(self, index, ptr);
  209. }
  210. /** Copies the dynar_elementsize(self) of bytes from the data pointed to
  211. * by ptr into the location pointed to by index.
  212. * Does bounds checking and return NULL if ouut of bounds.
  213. */
  214. Dynar * dynar_putraw(Dynar * self, int index, void * ptr) {
  215. // Bounds check
  216. if(!dynar_index_ok(self, index)) { return NULL; }
  217. return dynar_putraw_unsafe(self, index, ptr);
  218. }
  219. /** Stores a pointer at the index of the array.
  220. * Does bounds checking. dynar_elementsize(self) sould have been
  221. * initialized as sizeof(void *) by using dynar_newptr
  222. */
  223. void * dynar_putptr(Dynar * self, int index, void * ptr) {
  224. return dynar_putraw(self, index, &ptr);
  225. // use &ptr because we want to put the contents of the pointer,
  226. // not the pointer itself.
  227. }
  228. /** Returns a pointer that was stored at the index index of the array. */
  229. void * dynar_getptr(Dynar * self, int index) {
  230. void * res = NULL;
  231. if(!dynar_getcopy(self, index, &res)) return NULL;
  232. // use &ptr because we want to fetch the contents of the pointer,
  233. // which will be copied from the element of the array.
  234. return res;
  235. }
  236. /** Copies the element that *ptr points to into this array at position
  237. index */
  238. void * dynar_putdata(Dynar * self, int index, void * ptr) {
  239. return dynar_putraw(self, index, ptr);
  240. }
  241. /** Returns a pointer to the index-th element of the array. */
  242. void * dynar_getdata(Dynar * self, int index) {
  243. return dynar_getraw(self, index);
  244. }
  245. /* Applies quick sort to the array using the given comparator. */
  246. Dynar * dynar_qsort(Dynar * self, DynarCompare * compare) {
  247. void * base; int nmemb; size_t size;
  248. if(!self) return NULL;
  249. base = self->data;
  250. nmemb = self->size;
  251. size = self->elsz;
  252. qsort(base, nmemb, size, compare);
  253. return self;
  254. }
  255. /* Applies a binary search to the array using the given comparator.
  256. User must call dynar_qsort first. */
  257. void * dynar_bsearch(Dynar * self, const void * key, DynarCompare * compare) {
  258. void * base; int nmemb; size_t size;
  259. if (!self) return NULL;
  260. base = self->data;
  261. nmemb = self->size;
  262. size = self->elsz;
  263. return bsearch(key, base, nmemb, size, compare);
  264. }
  265. /* Puts NULL in every element of this array using dynar_putptr */
  266. Dynar * dynar_putnullall(Dynar * self) {
  267. int stop = dynar_size(self);
  268. int index;
  269. for (index = 0; index < stop; index++) {
  270. dynar_putptr(self, index, NULL);
  271. }
  272. return self;
  273. }
  274. /* Iterator helper: fill in every->now as data. */
  275. Every * dynar_everynow_data(Every * every) {
  276. every->now = dynar_getdata(every->on, every->index);
  277. if(every->now) return every;
  278. return NULL;
  279. }
  280. /* Iterator helper: fill in every->now as pointer. */
  281. Every * dynar_everynow_ptr(Every * every) {
  282. every->now = dynar_getptr(every->on, every->index);
  283. if(every->now) return every;
  284. return NULL;
  285. }
  286. /* Iterator helpers: init */
  287. Every * dynar_everyinit_data(Every * every) {
  288. every->index = 0;
  289. return dynar_everynow_data(every);
  290. }
  291. /* Iterator helpers: next */
  292. Every * dynar_everynext_data(Every * every) {
  293. every->index++;
  294. return dynar_everynow_data(every);
  295. }
  296. /* Iterator helpers: put. */
  297. void * dynar_everyput_data(Every * every, void * data) {
  298. return dynar_putdata(every->on, every->index, data);
  299. }
  300. /* Iterator helpers: done. */
  301. void * dynar_everydone(Every * every) {
  302. return every;
  303. }
  304. /* Iterator helpers: init pointers */
  305. Every * dynar_everyinit_ptr(Every * every) {
  306. every->index = 0;
  307. return dynar_everynow_ptr(every);
  308. }
  309. /* Iterator helpers: next pointers */
  310. Every * dynar_everynext_ptr(Every * every) {
  311. every->index++;
  312. return dynar_everynow_ptr(every);
  313. }
  314. /* Iterator helpers: put pointers. */
  315. void * dynar_everyput_ptr(Every * every, void * data) {
  316. return dynar_putptr(every->on, every->index, data);
  317. }
  318. /* Iterator helper table. */
  319. static EveryActs dynar_every_data_acts_ = {
  320. dynar_everydone,
  321. dynar_everyinit_data,
  322. dynar_everynext_data,
  323. dynar_everyput_data
  324. };
  325. /* Iterator helper table. */
  326. static EveryActs dynar_every_ptr_acts_ = {
  327. dynar_everydone,
  328. dynar_everyinit_ptr,
  329. dynar_everynext_ptr,
  330. dynar_everyput_ptr
  331. };
  332. /** Iterates over the data. Call every_free when done. */
  333. Every * dynar_every_data(Dynar * dynar) {
  334. return every_new(&dynar_every_data_acts_);
  335. }
  336. /** Iterates over the pointers in this array. Call every_free when done. */
  337. Every * dynar_every_ptr(Dynar * dynar) {
  338. return every_new(&dynar_every_ptr_acts_);
  339. }
  340. /* Walks over the array using the each interface, accessing
  341. the data as pointers. eachdo should return non-null to break the iteration,
  342. the data thus found is returned bu this function. */
  343. void * dynar_each_ptr(Dynar * self, EachDo * eachdo, void * extra) {
  344. Each each;
  345. int index;
  346. int size = dynar_size(self);
  347. each_init(&each, self, extra);
  348. for(index = 0; index < size ; index++) {
  349. void * aid;
  350. void * ptr = dynar_getptr(self, index);
  351. each_next(&each, ptr);
  352. aid = eachdo(&each);
  353. if (aid) return aid;
  354. }
  355. return NULL;
  356. }
  357. /* Walks over the array using the Each interface, accessing
  358. the data as stored structs. */
  359. void * dynar_each_data(Dynar * self, EachDo * eachdo, void * extra) {
  360. Each each;
  361. int index;
  362. int size = dynar_size(self);
  363. each_init(&each, self, extra);
  364. for(index = 0; index < size ; index++) {
  365. void * aid;
  366. void * ptr = dynar_getdata(self, index);
  367. each_next(&each, ptr);
  368. aid = eachdo(&each);
  369. if (aid) return aid;
  370. }
  371. return NULL;
  372. }
  373. /* Walks over the array using the walker interface, accessing
  374. the data as stored pointers. */
  375. void * dynar_walkptrbetween(Dynar * self, int low, int high,
  376. Walker * walker, void * extra) {
  377. int index;
  378. int size = dynar_size(self);
  379. low = (low < 0) ? 0 : low;
  380. high = (high > size) ? size : high;
  381. for(index = low; index < high ; index++) {
  382. void * aid;
  383. void * ptr = dynar_getptr(self, index);
  384. aid = walker(ptr, extra);
  385. if (aid) return aid;
  386. }
  387. return NULL;
  388. }
  389. /* Walks over the array using the walker interface, accessing
  390. the data as stored structs. */
  391. void * dynar_walkptr(Dynar * self, Walker * walker, void * extra) {
  392. return dynar_walkptrbetween(self, 0, dynar_size(self), walker, extra);
  393. }
  394. /* Walks over the array using the walker interface, accessing
  395. the data as stored pointers. */
  396. void * dynar_walkdatabetween(Dynar * self, int low, int high,
  397. Walker * walker, void * extra) {
  398. int index;
  399. int size = dynar_size(self);
  400. low = (low < 0) ? 0 : low;
  401. high = (high > size) ? size : high;
  402. for(index = low; index < high ; index++) {
  403. void * aid;
  404. void * ptr = dynar_getdata(self, index);
  405. aid = walker(ptr, extra);
  406. if (aid) return aid;
  407. }
  408. return NULL;
  409. }
  410. /* Walks over the array using the walker interface, accessing
  411. the data as stored structs. */
  412. void * dynar_walkdata(Dynar * self, Walker * walker, void * extra) {
  413. return dynar_walkdatabetween(self, 0, dynar_size(self), walker, extra);
  414. }
  415. /* Walks over the array updating it, using the walker interface, accessing
  416. the data as stored pointers. */
  417. void * dynar_walkmapptrbetween(Dynar * self, int low, int high,
  418. Walker * walker, void * extra) {
  419. int index;
  420. int size = dynar_size(self);
  421. low = (low < 0) ? 0 : low;
  422. high = (high > size) ? size : high;
  423. for(index = low; index < high ; index++) {
  424. void * aid;
  425. void * ptr = dynar_getptr(self, index);
  426. aid = walker(ptr, extra);
  427. dynar_putptr(self, index, ptr);
  428. }
  429. return NULL;
  430. }
  431. /* Walks over the array updating it using the walker interface, accessing
  432. the data as stored structs. */
  433. void * dynar_walkmapptr(Dynar * self, Walker * walker, void * extra) {
  434. return dynar_walkmapptrbetween(self, 0, dynar_size(self), walker, extra);
  435. }
  436. /* Resizes a dynar filled with pointers, and if the dynar shrinks, calls the
  437. * given destructor on all elements that are to be removed. Returns self on success
  438. * and NULL on failure. Even in this case, the superfluous elements
  439. fmay have been removed. */
  440. Dynar * dynar_resize(Dynar * self, int newsize, MemDestructor * destroy) {
  441. int index;
  442. int last;
  443. if (!self) return NULL;
  444. last = dynar_size(self);
  445. for(index = newsize; index < last; index ++) {
  446. void * aid = dynar_getptr(self, index);
  447. dynar_putptr(self, index, destroy(aid));
  448. }
  449. return dynar_size_(self, newsize);
  450. }
  451. /** Makes a new empty dynar to contain long integers */
  452. Dynar * dynar_new_long() {
  453. return dynar_newempty(sizeof(long));
  454. }
  455. /** Stores a long integer into a dynar (preferrably created with dynar_new_long */
  456. Dynar * dynar_put_long(Dynar * self, int index, long value) {
  457. return dynar_putdata(self, index, &value);
  458. }
  459. /** Gets a long integer from a dynar */
  460. Dynar * dynar_get_long(Dynar * self, int index, long * value) {
  461. return dynar_getcopy(self, index, value);
  462. }
  463. /** Grows the size of the array by 1 and appends the long in the last position */
  464. Dynar * dynar_append_long(Dynar * self, long value) {
  465. int pos = dynar_size(self);
  466. if (!dynar_size_(self, pos+1)) return NULL;
  467. return dynar_put_long(self, pos, value);
  468. }
  469. /**
  470. * Lilis is a doubly Linked List that points to it's members via void pointers
  471. * but does not own them in the sense that it doesn't clean them up unless requested.
  472. */
  473. struct Lilis_ {
  474. Lilis * next;
  475. Lilis * prev;
  476. void * data;
  477. };
  478. /**
  479. * frees the tail of a llist only. Does nothing to self or other.
  480. */
  481. Lilis * lilis_freetail(Lilis * self) {
  482. Lilis * next, * now;
  483. if(!self) return NULL;
  484. now = self->next; // skip current node, that will be deleted in lilis_free
  485. while (now) {
  486. next = now->next; // already get next one
  487. mem_free(now); // now we can safely free the current node
  488. now = next; // set current to next one.
  489. }
  490. return self;
  491. }
  492. /** Frees the tail of the list pointed to by self. It does not free any of the
  493. emlements contained in * data. Does not alter self->prev, and doesn't
  494. free that either. */
  495. Lilis * lilis_done(Lilis * self ) {
  496. if(!self) return NULL;
  497. lilis_freetail(self);
  498. self->data = NULL;
  499. self->next = NULL;
  500. return self;
  501. }
  502. /** Frees a linked list node and it's tail. Returns NULL. */
  503. Lilis * lilis_free(Lilis * self) {
  504. lilis_done(self);
  505. return mem_free(self);
  506. }
  507. /** Allocates a new empty linked list. */
  508. Lilis * lilis_alloc() {
  509. return STRUCT_ALLOC(Lilis);
  510. }
  511. /** Initializes a linked list node */
  512. Lilis * lilis_init(Lilis * self, Lilis * next, Lilis * prev, void * data) {
  513. if(!self) return NULL;
  514. self->data = data;
  515. self->next = next;
  516. self->prev = prev;
  517. return self;
  518. }
  519. /** Initializes a new linked list node that points to nothing. */
  520. Lilis * lilis_initempty(Lilis * self) {
  521. return lilis_init(self, NULL, NULL, NULL);
  522. }
  523. /** Returns a newly allocatd linked list node. */
  524. Lilis * lilis_new(Lilis * next, Lilis * prev, void * data) {
  525. Lilis * res = lilis_alloc();
  526. if(!lilis_init(res, next, prev, data)) {
  527. return lilis_free(res);
  528. }
  529. return res;
  530. }
  531. /** Returns a newly allocated linked list node that points to nothing. */
  532. Lilis * lilis_newempty() {
  533. return lilis_new(NULL, NULL, NULL);
  534. }
  535. /** Appends a node to the current one, inserting it if needed.
  536. * returns other.
  537. */
  538. Lilis * lilis_add(Lilis * self, Lilis * other) {
  539. Lilis * oldnext = self->next;
  540. if ((!self) || (!other)) return NULL;
  541. if (oldnext) {
  542. oldnext->prev = other; // next one's previous becomes the inserted link
  543. other->next = oldnext; // inserted link's next is the old next
  544. }
  545. // these two belowear always needed even if there is no oldnext.
  546. other->prev = self; // and other's previous one is self.
  547. self->next = other; // and curent next is the inserted link
  548. return other;
  549. }
  550. /** Creates a new node and adds it */
  551. Lilis * lilis_addnew(Lilis * self, void * data) {
  552. Lilis * other = lilis_new(NULL, NULL, data);
  553. return lilis_add(self, other);
  554. }
  555. /** Removes the node that follows self, but does NOT call lilis_free on it.
  556. * returns the removed node
  557. */
  558. Lilis * lilis_removenext(Lilis * self) {
  559. Lilis * next, * nextnext;
  560. if(!self) return NULL;
  561. next = self->next;
  562. if(!next) return NULL;
  563. nextnext = next->next;
  564. if(nextnext) {
  565. nextnext->prev = self;
  566. self->next = nextnext;
  567. } else {
  568. self->next = NULL;
  569. }
  570. return next;
  571. }
  572. /** Removes self, modifies prev if this is needed, but does NOT call
  573. * lilis_free on it.
  574. * returns the removed node
  575. */
  576. Lilis * lilis_remove(Lilis * self) {
  577. Lilis * prev, * next;
  578. if(!self) return NULL;
  579. next = self->next;
  580. prev = self->prev;
  581. if(next) { // link prev node to next node, so this node is cut out.
  582. prev->next = next;
  583. next->prev = prev;
  584. } else { // no next node, prev also gets no next node.
  585. prev->next = NULL;
  586. }
  587. // finally unlink self.
  588. self->next = NULL;
  589. self->prev = NULL;
  590. return next;
  591. }
  592. /** Removes the node self, and calls lilis_free on it.
  593. * returns NULL
  594. */
  595. Lilis * lilis_erase(Lilis * self) {
  596. Lilis * eraseme = lilis_remove(self);
  597. return lilis_free(eraseme);
  598. }
  599. /** Removes the node that follows self, and calls lilis_free on it.
  600. * returns NULL
  601. */
  602. Lilis * lilis_erasenext(Lilis * self) {
  603. Lilis * eraseme = lilis_removenext(self);
  604. return lilis_free(eraseme);
  605. }
  606. /** Gets the next node in the list. */
  607. Lilis * lilis_next(Lilis * self) {
  608. if(!self) return NULL;
  609. return self->next;
  610. }
  611. /** Gets the previous node in the list. */
  612. Lilis * lilis_previous(Lilis * self) {
  613. if(!self) return NULL;
  614. return self->prev;
  615. }
  616. /** Gets the data of the list node */
  617. void * lilis_data(Lilis * self) {
  618. if(!self) return NULL;
  619. return self->data;
  620. }