dynar.c 20 KB

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