bad.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. #include <string.h>
  2. #include "bad.h"
  3. /*
  4. * License: bad.h and bad.c are
  5. *
  6. * Copyright (C) 2012-2013 beoran (beoran@beoran.net)
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. *
  26. */
  27. int bad_mini(int one, int two) {
  28. return (one < two) ? one : two;
  29. }
  30. double bad_mind(double one, double two) {
  31. return (one < two) ? one : two;
  32. }
  33. float bad_minf(float one, float two) {
  34. return (one < two) ? one : two;
  35. }
  36. int bad_maxi(int one, int two) {
  37. return (one > two) ? one : two;
  38. }
  39. double bad_maxd(double one, double two) {
  40. return (one > two) ? one : two;
  41. }
  42. float bad_maxf(float one, float two) {
  43. return (one > two) ? one : two;
  44. }
  45. int bad_comparei(int one, int two) {
  46. if (one < two) return -1;
  47. if (one > two) return 1;
  48. return 0;
  49. }
  50. int bad_compared(double one, double two) {
  51. if (one < two) return -1;
  52. if (one > two) return 1;
  53. return 0;
  54. }
  55. int bad_comparef(float one, float two) {
  56. if (one < two) return -1;
  57. if (one > two) return 1;
  58. return 0;
  59. }
  60. int bad_clampi(int value , int min, int max) {
  61. if (value < min) return min;
  62. if (value > max) return max;
  63. return value;
  64. }
  65. double bad_clampd(double value , double min, double max) {
  66. if (value < min) return min;
  67. if (value > max) return max;
  68. return value;
  69. }
  70. float bad_clampf(float value , float min, float max) {
  71. if (value < min) return min;
  72. if (value > max) return max;
  73. return value;
  74. }
  75. int bad_outofboundsi(int value, int min, int max) {
  76. if (value < min) return TRUE;
  77. return (value >= max);
  78. }
  79. int bad_outofboundsd(int value, double min, double max) {
  80. if (value < min) return TRUE;
  81. return (value >= max);
  82. }
  83. int bad_outofboundsf(int value, float min, float max) {
  84. if (value < min) return TRUE;
  85. return (value >= max);
  86. }
  87. /**
  88. * Struct: BadListNode
  89. *
  90. * BadListNode is an intrusive doubly linked list.
  91. * These lists do not contain any pointers to the data member that is part of
  92. * the list, rather, you use them by including them into the struct that
  93. * needs to be included in the intrusive linked list, hence the "intrusive".
  94. * Use the macro bad_container to get the containing struct back.
  95. *
  96. * For example : struct Foo { int value ; BadListNode list } ...
  97. * struct Foo foo, bar; badlistnode_init(&foo.list); badlistnode_init(&bar.list);
  98. * badlistnode_add(&foo.list, &bar.list);
  99. *
  100. */
  101. /**
  102. * Function: badlistnode_initall
  103. *
  104. * Fully initializes a non-NULL intrusive linked list.
  105. *
  106. * Parameters:
  107. * self - BadListNode to initialize
  108. * next - Next BadListNode list node to link to or NULL
  109. * prev - Previous BadListNode list node to link to or NULL
  110. *
  111. * Returns:
  112. * self
  113. */
  114. BadListNode * badlistnode_initall(BadListNode * self, BadListNode * next, BadListNode * prev) {
  115. if (!self) return NULL;
  116. self->next = next;
  117. self->prev = prev;
  118. return self;
  119. }
  120. /**
  121. * Function: badlistnode_init
  122. *
  123. * Initializes the intrusive linked list. Next and prev are set to NULL.
  124. *
  125. * Parameters:
  126. * self - BadListNode to initialize
  127. *
  128. * Returns:
  129. * self
  130. */
  131. BadListNode * badlistnode_init(BadListNode * self) {
  132. return badlistnode_initall(self, NULL, NULL);
  133. }
  134. /**
  135. * Function: badlistnode_initempty
  136. *
  137. * Initializes the intrusive linked list to be empty. Next is set to self
  138. * to signal this to badlistnode_isempty.
  139. *
  140. * Parameters:
  141. * self - BadListNode to initialize as empty
  142. *
  143. * Returns:
  144. * self
  145. */
  146. BadListNode * badlistnode_initempty(BadListNode * self) {
  147. return badlistnode_initall(self, self, NULL);
  148. }
  149. /**
  150. * Function: badlistnode_isempty
  151. *
  152. * Returns true if the list is empty, false if not.
  153. *
  154. * Parameters:
  155. * self - BadListNode to check.
  156. *
  157. * Returns:
  158. * TRUE if empty, FALSE if not. A NULL list is also empty.
  159. */
  160. bool badlistnode_isempty(BadListNode * self) {
  161. if(!self) return TRUE;
  162. return badlistnode_next(self) == self;
  163. }
  164. /**
  165. * Function: badlistnode_unlink
  166. *
  167. * Unlinks self from the list it is part of.
  168. * Does NOT clean up any data asssociated with the container of the intrusive
  169. * list and also doesn't free self, since self should be part of the container
  170. * of the intrusive list.
  171. *
  172. * Parameters:
  173. * self - BadListNode to remove from whole of list. May be NULL.
  174. *
  175. * Returns:
  176. * self
  177. */
  178. BadListNode * badlistnode_unlink(BadListNode * self) {
  179. if(!self) return NULL;
  180. if(self->prev) { self->prev->next = self->next; }
  181. if(self->next) { self->next->prev = self->prev; }
  182. self->prev = NULL;
  183. self->next = NULL;
  184. return self;
  185. }
  186. /**
  187. * Function: badlistnode_add
  188. *
  189. * Appends other after self.
  190. *
  191. * Parameters:
  192. * self - BadListNode to append to. IF NULL, returns other, since that becomes
  193. * the base of the list.
  194. * other - BadListNode to append to self.
  195. *
  196. * Returns:
  197. * The new "first" element of the list.
  198. */
  199. BadListNode * badlistnode_add(BadListNode * self, BadListNode * other) {
  200. if(!self) return other;
  201. if(!other) return self;
  202. self->next = other;
  203. other->prev = self;
  204. return other;
  205. }
  206. /**
  207. * Function: badlistnode_next
  208. *
  209. * Returns the next element in the list
  210. *
  211. * Parameters:
  212. * self - BadListNode
  213. *
  214. * Returns:
  215. * the next element in the list, or NULL if no next item.
  216. */
  217. BadListNode * badlistnode_next(BadListNode * self) {
  218. if(!self) return NULL;
  219. return self->next;
  220. }
  221. /**
  222. * Function: badlistnode_prev
  223. *
  224. * Returns the previous element in the list
  225. *
  226. * Parameters:
  227. * self - BadListNode
  228. *
  229. * Returns:
  230. * the next element in the list, or NULL if no next item.
  231. */
  232. BadListNode * badlistnode_prev(BadListNode * self) {
  233. if(!self) return NULL;
  234. return self->prev;
  235. }
  236. /**
  237. * Function: badlistnode_first
  238. *
  239. * Returns the first element in the list, by dumb iteration.
  240. *
  241. * Parameters:
  242. * self - BadListNode
  243. *
  244. * Returns:
  245. * the first link in the list, or NULL if self is NULL.
  246. */
  247. BadListNode * badlistnode_first(BadListNode * self) {
  248. BadListNode * aid = self;
  249. if(!aid) return NULL;
  250. while (aid->prev) {
  251. aid = aid->prev;
  252. }
  253. return aid;
  254. }
  255. /**
  256. * Function: badlistnode_last
  257. *
  258. * Returns the last element in the list, by dumb iteration.
  259. *
  260. * Parameters:
  261. * self - BadListNode
  262. *
  263. * Returns:
  264. * the last link in the list, or NULL if self is NULL.
  265. */
  266. BadListNode * badlistnode_last(BadListNode * self) {
  267. BadListNode * aid = self;
  268. if(!aid) return NULL;
  269. while (aid->next) {
  270. aid = aid->next;
  271. }
  272. return aid;
  273. }
  274. /**
  275. * Function: badlistnode_push
  276. *
  277. * Appends other to the end of the list by dumb iteration.
  278. *
  279. * Parameters:
  280. * self - BadListNode
  281. *
  282. * Returns:
  283. * other, or NULL if self or other is NULL.
  284. */
  285. BadListNode * badlistnode_push(BadListNode * self, BadListNode * other) {
  286. BadListNode * aid;
  287. aid = badlistnode_last(self);
  288. return badlistnode_add(aid, other);
  289. }
  290. /**
  291. * Function: badlistnode_unshift
  292. *
  293. * Prepends other to the beginning of the list by dumb iteration.
  294. *
  295. * Parameters:
  296. * self - BadListNode
  297. *
  298. * Returns:
  299. * other, or NULL if self or other is NULL. Other is now also the new
  300. * beginning of the list.
  301. */
  302. BadListNode * badlistnode_unshift(BadListNode * self, BadListNode * other) {
  303. BadListNode * aid;
  304. aid = badlistnode_first(self);
  305. badlistnode_add(other, aid);
  306. return other;
  307. }
  308. /**
  309. * Function: badlistnode_shift
  310. *
  311. * Removes the first element from the list by dumb iteration.
  312. *
  313. * Parameters:
  314. * self - BadListNode
  315. *
  316. * Returns:
  317. * list node that was removed, or NULL if self is NULL.
  318. */
  319. BadListNode *
  320. badlistnode_shift(BadListNode * self) {
  321. BadListNode * aid;
  322. aid = badlistnode_first(self);
  323. return badlistnode_unlink(aid);
  324. }
  325. /**
  326. * Function: badlistnode_pop
  327. *
  328. * Removes the last element from the list by dumb iteration.
  329. *
  330. * Parameters:
  331. * self - BadListNode
  332. *
  333. * Returns:
  334. * list node that was removed, or NULL if self is NULL.
  335. */
  336. BadListNode *
  337. badlistnode_pop(BadListNode * self) {
  338. BadListNode * aid;
  339. aid = badlistnode_last(self);
  340. return badlistnode_unlink(aid);
  341. }
  342. /**
  343. * Function: badlistnode_data
  344. *
  345. * Parameters:
  346. * self - list node to get the data of.
  347. * offset - Use offsetof to calculate this offset.
  348. *
  349. * Returns:
  350. * the data for this node of the singly linked list.
  351. *
  352. */
  353. void *
  354. badlistnode_data(BadListNode * self, int offset) {
  355. if(!self) return NULL;
  356. return (void *)((char *)self - offset);
  357. }
  358. /**
  359. * Function: badlistnode_search
  360. *
  361. * Parameters:
  362. * self - list node to search.
  363. * offset - Comparer function, get passed each element of the BadListNode * and
  364. * tofind, must return 0 if the item searched for is found.
  365. *
  366. * Returns:
  367. * the BadListNode found, or NULL if not found.
  368. *
  369. */
  370. BadListNode *
  371. badlistnode_search(BadListNode * self, BadListNodeCompare * compare, BadListNode * tofind) {
  372. BadListNode * aid;
  373. if (badlistnode_isempty(self)) return NULL;
  374. for(aid = self; aid ; aid = aid->next) {
  375. int cmp = compare(aid, tofind);
  376. if (cmp == 0) return aid;
  377. }
  378. return NULL;
  379. }
  380. /**
  381. * Function: badlistnode_searchvalue
  382. *
  383. * Parameters:
  384. * self - list node to search.
  385. * offset - Comparer function, get passed each element of the BadListNode * and
  386. * tofind, must return 0 if the item searched for is found.
  387. * tofind - Arbitrary pointer to a VALUE (not a BadListNode *).
  388. * Returns:
  389. * the BadListNode found, or NULL if not found.
  390. *
  391. */
  392. BadListNode *
  393. badlistnode_searchvalue(BadListNode * self, BadListNodeSearchValue * compare, void * tofind) {
  394. BadListNode * aid;
  395. if (badlistnode_isempty(self)) return NULL;
  396. for(aid = self; aid ; aid = aid->next) {
  397. int cmp = compare(aid, tofind);
  398. if (cmp == 0) return aid;
  399. }
  400. return NULL;
  401. }
  402. /*
  403. * Function: badlistnode_unlink
  404. *
  405. * A shorthand for badlistnode_unlink(badlistnode_search(self, compare, toremove));
  406. */
  407. BadListNode *
  408. badlistnode_remove(BadListNode * self, BadListNodeCompare * compare, BadListNode * toremove) {
  409. return badlistnode_unlink(badlistnode_search(self, compare, toremove));
  410. }
  411. /*
  412. * Function: badlistnode_each
  413. *
  414. * Iterates over all list elements starting from self.
  415. * It's safe to unlink the element passed to BadListNodeEach.
  416. * The iteration stops if BadListNodeEach returns non-zero.
  417. */
  418. int badlistnode_each(BadListNode * self, BadListNodeEach * each, void * data) {
  419. BadListNode * aid, * next;
  420. int res = 0;
  421. aid = self;
  422. while(aid) {
  423. next = badlistnode_next(aid);
  424. res = each(aid, data);
  425. if(res) return res;
  426. aid = next;
  427. }
  428. return res;
  429. }
  430. BadList * badlist_init(BadList * self) {
  431. if (!self) return NULL;
  432. self->head = NULL;
  433. self->tail = NULL;
  434. self->size = 0;
  435. return self;
  436. }
  437. BadList * badlist_add(BadList * self, BadListNode * node) {
  438. if (!self) return NULL;
  439. if (!self->tail) {
  440. self->head = node;
  441. badlistnode_initall(node, NULL, NULL);
  442. } else {
  443. self->tail->next = node;
  444. node->prev = self->tail;
  445. node->next = NULL;
  446. }
  447. self->tail = node;
  448. self->size++;
  449. return self;
  450. }
  451. BadList * badlist_shift(BadList * self, BadListNode * node) {
  452. if (!self) return NULL;
  453. if (!self->head) {
  454. self->tail = node;
  455. badlistnode_initall(node, NULL, NULL);
  456. } else {
  457. self->head->prev = node;
  458. node->prev = NULL;
  459. node->next = self->head;
  460. }
  461. self->head = node;
  462. self->size++;
  463. return self;
  464. }
  465. BadList * badlist_remove(BadList * self, BadListNode * node) {
  466. if(self->tail == node) {
  467. self->tail = node->prev;
  468. }
  469. if(self->head == node) {
  470. self->head = node->next;
  471. }
  472. if (!self->tail) { self->head = NULL; }
  473. if (!self->head) { self->tail = NULL; }
  474. badlistnode_unlink(node);
  475. self->size--;
  476. return self;
  477. }
  478. BadListNode * badlist_head(BadList * self) {
  479. if (!self) return NULL;
  480. return self->head;
  481. }
  482. BadListNode * badlist_tail(BadList * self) {
  483. if (!self) return NULL;
  484. return self->tail;
  485. }
  486. int badlist_size(BadList * self) {
  487. if (!self) return 0;
  488. return self->size;
  489. }
  490. BadBitree *
  491. badbitree_initall(BadBitree * self, BadBitree * left, BadBitree * right,
  492. BadBitree * up) {
  493. if(!self) return NULL;
  494. self->left = left;
  495. self->right = right;
  496. self->up = up;
  497. return self;
  498. }
  499. BadBitree *
  500. badbitree_init(BadBitree * self) {
  501. return badbitree_initall(self, NULL, NULL, NULL);
  502. }
  503. BadBitree *
  504. badbitree_left(BadBitree * self) {
  505. if(!self) return NULL;
  506. return self->left;
  507. }
  508. BadBitree *
  509. badbitree_right(BadBitree * self) {
  510. if(!self) return NULL;
  511. return self->right;
  512. }
  513. BadBitree *
  514. badbitree_up(BadBitree * self) {
  515. if(!self) return NULL;
  516. return self->right;
  517. }
  518. BadBitree *
  519. badbitree_up_(BadBitree * self, BadBitree * newup) {
  520. if(!self) return NULL;
  521. return self->up = newup;
  522. }
  523. BadBitree *
  524. badbitree_left_(BadBitree * self, BadBitree * newleft) {
  525. if(!self) return NULL;
  526. badbitree_up_(newleft, self);
  527. return self->left = newleft;
  528. }
  529. BadBitree *
  530. badbitree_right_(BadBitree * self, BadBitree * newright) {
  531. if(!self) return NULL;
  532. badbitree_up_(newright, self);
  533. return self->right = newright;
  534. }
  535. bool badbitree_isleaf(BadBitree * self) {
  536. if(!self) return FALSE;
  537. if(badbitree_right(self)) return FALSE;
  538. if(badbitree_left(self)) return FALSE;
  539. return TRUE;
  540. }
  541. BadAatree *
  542. badaatree_initall(BadAatree * self, BadAatree * left, BadAatree * right,
  543. BadAatree * up, int level) {
  544. if(!badbitree_initall(&self->tree, &left->tree, &right->tree, &up->tree))
  545. return NULL;
  546. self->level = level;
  547. return self;
  548. }
  549. BadAatree *
  550. badaatree_init(BadAatree * self) {
  551. return badaatree_initall(self, NULL, NULL, NULL, 1);
  552. }
  553. #define bad_bi2aa(PTR) bad_container(PTR, BadAatree, tree)
  554. BadAatree *
  555. badaatree_left(BadAatree * self) {
  556. if(!self) return NULL;
  557. return bad_bi2aa(badbitree_left(&self->tree));
  558. }
  559. BadAatree *
  560. badaatree_right(BadAatree * self) {
  561. if(!self) return NULL;
  562. return bad_bi2aa(badbitree_right(&self->tree));
  563. }
  564. BadAatree *
  565. badaatree_up(BadAatree * self) {
  566. if(!self) return NULL;
  567. return bad_bi2aa(badbitree_up(&self->tree));
  568. }
  569. BadAatree *
  570. badaatree_left_(BadAatree * self, BadAatree * other) {
  571. if(!self) return NULL;
  572. return bad_bi2aa(badbitree_left_(&self->tree, &other->tree));
  573. }
  574. BadAatree *
  575. badaatree_right_(BadAatree * self, BadAatree * other) {
  576. if(!self) return NULL;
  577. return bad_bi2aa(badbitree_right_(&self->tree, &other->tree));
  578. }
  579. BadAatree *
  580. badaatree_up_(BadAatree * self, BadAatree * other) {
  581. if(!self) return NULL;
  582. return bad_bi2aa(badbitree_up_(&self->tree, &other->tree));
  583. }
  584. int badaatree_level(BadAatree * self) {
  585. if(!self) return -1;
  586. return self->level;
  587. }
  588. int badaatree_level_(BadAatree * self, int newlevel) {
  589. if(!self) return -1;
  590. return self->level = newlevel;
  591. }
  592. bool badaatree_isleaf(BadAatree * self) {
  593. if(!self) return FALSE;
  594. return badbitree_isleaf(&self->tree);
  595. }
  596. BadAatree * badaatree_skew(BadAatree * self) {
  597. BadAatree * left, * leftleft;
  598. if(!self) return NULL;
  599. left = badaatree_left(self);
  600. if(!left) return self;
  601. leftleft = badaatree_left(left);
  602. if(!leftleft) return self;
  603. if(badaatree_level(self) == badaatree_level(leftleft)) {
  604. BadAatree * left = badaatree_left(self);
  605. badaatree_left_(self, badaatree_right(left));
  606. badaatree_right_(left, self);
  607. return left;
  608. }
  609. return self;
  610. }
  611. BadAatree * badaatree_split(BadAatree * self) {
  612. BadAatree * right, * rightright;
  613. if(!self) return NULL;
  614. right = badaatree_right(self);
  615. if(!right) return self;
  616. rightright = badaatree_right(right);
  617. if(!rightright) return self;
  618. if (badaatree_level(self) == badaatree_level(rightright)) {
  619. badaatree_right_(self, badaatree_left(right));
  620. badaatree_left_(right, self);
  621. badaatree_level_(right, badaatree_level(right) + 1);
  622. return right;
  623. }
  624. return self;
  625. }
  626. BadAatree * badaatree_insert(BadAatree * self, BadAatree * node,
  627. BadAatreeMethods * methods) {
  628. int cmp;
  629. BadAatree * aid;
  630. if(!self) { return node; }
  631. cmp = methods->compare(self, node);
  632. if(cmp < 0) {
  633. aid = badaatree_insert(badaatree_left(self), node, methods);
  634. badaatree_left_(self, aid);
  635. } else if (cmp > 0) {
  636. aid = badaatree_insert(badaatree_right(self), node, methods);
  637. badaatree_right_(self, aid);
  638. } else { /* Ignore duplicates for now,... */
  639. }
  640. self = badaatree_skew(self);
  641. self = badaatree_split(self);
  642. return self;
  643. }
  644. BadAatree * badaatree_search(BadAatree * self, BadAatree * node,
  645. BadAatreeMethods * methods) {
  646. int cmp;
  647. BadAatree * aid;
  648. if(!self) { return NULL; }
  649. cmp = methods->compare(self, node);
  650. if(cmp < 0) {
  651. return badaatree_search(badaatree_left(self), node, methods);
  652. } else if (cmp > 0) {
  653. return badaatree_search(badaatree_right(self), node, methods);
  654. } else { /* Found the item! */
  655. return self;
  656. }
  657. }
  658. BadAatree * badaatree_leveldownall(BadAatree * self) {
  659. BadAatree * right = badaatree_right(self);
  660. int lowest = bad_mini(badaatree_level(self), badaatree_level(right)) + 1;
  661. if (lowest < badaatree_level(self)) {
  662. badaatree_level_(self, lowest);
  663. if (lowest < badaatree_level(right)) {
  664. badaatree_level_(right, lowest);
  665. }
  666. }
  667. return self;
  668. }
  669. BadAatree * badaatree_successor(BadAatree * self) {
  670. BadAatree * aid, *next;
  671. if(!self) return NULL;
  672. aid = badaatree_right(self);
  673. if(!aid) return NULL;
  674. next = badaatree_left(aid);
  675. while (next) {
  676. aid = next;
  677. next = badaatree_left(aid);
  678. };
  679. return aid;
  680. }
  681. BadAatree * badaatree_predecessor(BadAatree * self) {
  682. BadAatree * aid, *next;
  683. if(!self) return NULL;
  684. aid = badaatree_left(self);
  685. if(!aid) return NULL;
  686. next = badaatree_right(aid);
  687. while (next) {
  688. aid = next;
  689. next = badaatree_right(aid);
  690. };
  691. return aid;
  692. }
  693. BadAatree * badaatree_delete(BadAatree * self, BadAatree * node,
  694. BadAatreeMethods * methods) {
  695. BadAatree * aid;
  696. int cmp;
  697. if(!self) { return self; }
  698. cmp = methods->compare(self, node);
  699. if(cmp < 0) {
  700. aid = badaatree_delete(badaatree_left(self), node, methods);
  701. badaatree_left_(self, aid);
  702. } else if (cmp > 0) {
  703. aid = badaatree_delete(badaatree_right(self), node, methods);
  704. badaatree_right_(self, aid);
  705. } else { /* Found the value ! */
  706. if(badaatree_isleaf(self)) {
  707. badaatree_init(self); /* call init to unlink the tree's up pointer. */
  708. return NULL;
  709. } else if (!badaatree_left(self)) {
  710. BadAatree *
  711. left = badaatree_successor(self);
  712. aid = badaatree_delete(left, badaatree_right(self), methods);
  713. badaatree_right_(self, aid);
  714. methods->setvalue(self, left);
  715. } else {
  716. BadAatree *
  717. right = badaatree_predecessor(self);
  718. aid = badaatree_delete(right, badaatree_left(self), methods);
  719. badaatree_left_(self, aid);
  720. methods->setvalue(self, right);
  721. }
  722. }
  723. /* Rebalance */
  724. badaatree_leveldownall(self);
  725. self = badaatree_skew(self);
  726. aid = badaatree_right(self);
  727. aid = badaatree_skew(aid);
  728. badaatree_right_(self, aid);
  729. aid = badaatree_right(badaatree_right(self));
  730. aid = badaatree_skew(aid);
  731. badaatree_right_(badaatree_right(self), aid);
  732. self = badaatree_split(self);
  733. aid = badaatree_right(self);
  734. aid = badaatree_skew(aid);
  735. badaatree_right_(self, aid);
  736. return self;
  737. }
  738. void badaatree_printgraph(BadAatree * self, int level) {
  739. if(level == 0) {
  740. printf("digraph { \n");
  741. }
  742. level++;
  743. if(badaatree_left(self)) {
  744. printf("t%p -> t%p [color=red];\n", self, badaatree_left(self));
  745. badaatree_printgraph(badaatree_left(self), level);
  746. }
  747. if(badaatree_right(self)) {
  748. printf("t%p -> t%p [color=green];\n", self, badaatree_right(self));
  749. badaatree_printgraph(badaatree_right(self), level);
  750. }
  751. level--;
  752. if(level == 0) {
  753. printf("} \n");
  754. }
  755. }
  756. BadChildtree *
  757. badchildtree_initall(BadChildtree * self, BadChildtree * parent, BadChildtree * child,
  758. BadChildtree * next_sibling, BadChildtree * previous_sibling) {
  759. if(!self) return NULL;
  760. self->parent = parent;
  761. self->child = child;
  762. self->next = next_sibling;
  763. self->previous = previous_sibling;
  764. return self;
  765. }
  766. BadChildtree *
  767. badchildtree_initnull(BadChildtree * self) {
  768. return badchildtree_initall(self, NULL, NULL, NULL, NULL);
  769. }
  770. /* An empty badchildtree is simply a NULL tree. */
  771. bool
  772. badchildtree_isempty(BadChildtree * self) {
  773. return !self;
  774. }
  775. BadChildtree *
  776. badchildtree_parent(BadChildtree * self) {
  777. if (badchildtree_isempty(self)) return NULL;
  778. return self->parent;
  779. }
  780. BadChildtree *
  781. badchildtree_child(BadChildtree * self) {
  782. if (badchildtree_isempty(self)) return NULL;
  783. return self->child;
  784. }
  785. BadChildtree *
  786. badchildtree_next(BadChildtree * self) {
  787. if (badchildtree_isempty(self)) return NULL;
  788. return self->next;
  789. }
  790. /* Returns the last sibling of self, or NULL if no siblings or if self is NULL. */
  791. BadChildtree *
  792. badchildtree_lastsibling(BadChildtree * self) {
  793. BadChildtree * aid, * next;
  794. if (badchildtree_isempty(self)) return NULL;
  795. aid = self;
  796. next = badchildtree_next(aid);
  797. /* Skip until the end of the list. */
  798. while(next) {
  799. aid = next;
  800. next = badchildtree_next(aid);
  801. }
  802. return aid;
  803. }
  804. /* Returns the last child of the tree, or NULL if no children or nself is NULL. */
  805. BadChildtree *
  806. badchildtree_lastchild(BadChildtree * self) {
  807. if (badchildtree_isempty(self)) return NULL;
  808. return badchildtree_lastsibling(self->child);
  809. }
  810. /* Returns the previous sibling of self or NULL if no previous child. */
  811. BadChildtree *
  812. badchildtree_previous(BadChildtree * self) {
  813. if (badchildtree_isempty(self)) return NULL;
  814. return self->previous;
  815. }
  816. /* Inserts the sibling as a sibling of self, right after self. Sets parent
  817. link of the sibling too. */
  818. BadChildtree *
  819. badchildtree_insertsibling(BadChildtree * self, BadChildtree * sibling) {
  820. BadChildtree * next;
  821. if (badchildtree_isempty(self)) return sibling;
  822. if (badchildtree_isempty(sibling)) return self;
  823. next = self->next;
  824. self->next = sibling;
  825. sibling->next = next;
  826. sibling->previous = self;
  827. if (next) {
  828. next->previous = sibling;
  829. }
  830. sibling->parent = self->parent;
  831. return self;
  832. }
  833. /* Appends sibling as the last sibling of self.
  834. * Returns the new root element of the tree. If self is NULL, sibling is
  835. * returned as that will be the new root of the BadChildtree.
  836. */
  837. BadChildtree *
  838. badchildtree_appendsibling(BadChildtree * self, BadChildtree * sibling) {
  839. BadChildtree * last;
  840. if (badchildtree_isempty(self)) return sibling;
  841. last = badchildtree_lastsibling(self);
  842. if(!last) {
  843. last = self;
  844. }
  845. badchildtree_insertsibling(last, sibling);
  846. return self;
  847. }
  848. /* Appends child as the last child sibling of self.
  849. * Returns the new root element of the tree. If self is NULL, child is
  850. * returned as that will be the new root of the BadChildtree.
  851. */
  852. BadChildtree *
  853. badchildtree_appendchild(BadChildtree * self, BadChildtree * child) {
  854. BadChildtree * last;
  855. if (badchildtree_isempty(self)) return child;
  856. last = badchildtree_lastchild(self);
  857. if(!last) {
  858. self->child = child;
  859. } else {
  860. badchildtree_insertsibling(last, child);
  861. }
  862. child->parent = self;
  863. return self;
  864. }
  865. /* Shows the tree in a text form on stdout, suitable for dot to concert to a graph.
  866. */
  867. void badchildtree_printgraph(BadChildtree * self, int level) {
  868. BadChildtree * aid;
  869. if(level == 0) {
  870. printf("digraph { \n");
  871. }
  872. level++;
  873. for (aid = badchildtree_child(self) ; aid ; aid = badchildtree_next(aid)) {
  874. printf("t%p -> t%p [color=red];\n", self, aid);
  875. if (badchildtree_parent(aid) == self) {
  876. printf("t%p -> t%p [color=blue];\n", aid, badchildtree_parent(aid));
  877. }
  878. badchildtree_printgraph(aid, level);
  879. }
  880. level--;
  881. if(level == 0) {
  882. printf("} \n");
  883. }
  884. }
  885. /* Playing with a boxed C pointer. Avoids double frees or unintentional reuse.
  886. */
  887. struct BadBoxptr_ {
  888. void * ptr_private;
  889. };
  890. struct BadBoxptr_ badboxptr_make(void * ptr) {
  891. struct BadBoxptr_ self = {ptr};
  892. return self;
  893. }
  894. struct BadBoxptr_ badboxptr_malloc(size_t size) {
  895. return badboxptr_make(malloc(size));
  896. }
  897. void * badboxptr_borrow(struct BadBoxptr_ * self) {
  898. return self->ptr_private;
  899. }
  900. int badboxptr_isnull(struct BadBoxptr_ * self) {
  901. return (!self->ptr_private);
  902. }
  903. void * badboxptr_index(struct BadBoxptr_ * self, size_t offset) {
  904. if(badboxptr_isnull(self)) return NULL;
  905. return self->ptr_private + offset;
  906. }
  907. void * badboxptr_rindex(struct BadBoxptr_ * self, size_t offset) {
  908. if(badboxptr_isnull(self)) return NULL;
  909. return self->ptr_private - offset;
  910. }
  911. /* Takes ownership of data from box pointer "from".
  912. * From box pointer will be set to NULL.
  913. */
  914. struct BadBoxptr_ * badboxptr_own(struct BadBoxptr_ * self, struct BadBoxptr_ * from) {
  915. self->ptr_private = from->ptr_private;
  916. from->ptr_private = NULL;
  917. return self->ptr_private;
  918. }
  919. struct BadBoxptr_ *
  920. badboxptr_realloc(struct BadBoxptr_ * self, size_t size) {
  921. void * old = self->ptr_private;
  922. void * aid = realloc(old, size);
  923. if(aid) { self->ptr_private = aid; }
  924. return self;
  925. }
  926. void
  927. badboxptr_free(struct BadBoxptr_ * self) {
  928. free(self->ptr_private);
  929. self->ptr_private = NULL;
  930. }
  931. /* Another more simple approach, but you cannot forget o use
  932. * bad_xfree(&pointer), then...
  933. */
  934. void bad_xfree(void ** ptrptr) {
  935. if(!ptrptr) return;
  936. free(*ptrptr);
  937. (*ptrptr) = NULL;
  938. }
  939. void* bad_xrealloc(void ** ptrptr, size_t size) {
  940. void*aid;
  941. if(!ptrptr) return NULL;
  942. aid = realloc(*ptrptr,size);
  943. if(aid) {
  944. (*ptrptr) = aid;
  945. }
  946. return aid;
  947. }
  948. struct BadVar_ badvar_makeint(int value) {
  949. struct BadVar_ result;
  950. result.type = BADVAR_INT;
  951. result.value.i = value;
  952. return result;
  953. }
  954. struct BadVar_ badvar_makedouble(double value) {
  955. struct BadVar_ result;
  956. result.type = BADVAR_DOUBLE;
  957. result.value.d = value;
  958. return result;
  959. }
  960. struct BadVar_ badvar_makeptr(void * value) {
  961. struct BadVar_ result;
  962. result.type = BADVAR_PTR;
  963. result.value.ptr = value;
  964. return result;
  965. }
  966. struct BadVar_ badvar_makecstr(char * value) {
  967. struct BadVar_ result;
  968. result.type = BADVAR_PTR;
  969. result.value.ptr = value;
  970. return result;
  971. }
  972. struct BadVar_ badvar_makefptr(BadFunction * value) {
  973. struct BadVar_ result;
  974. result.type = BADVAR_FPTR;
  975. result.value.fptr= value;
  976. return result;
  977. }
  978. void * badvar_ptr(BadVar self) {
  979. if (self.type != BADVAR_PTR) return NULL;
  980. return self.value.ptr;
  981. }
  982. BadFunction * badvar_fptr(BadVar self) {
  983. if (self.type != BADVAR_FPTR) return NULL;
  984. return self.value.fptr;
  985. }
  986. char * badvar_cstr(BadVar self) {
  987. if (self.type != BADVAR_CSTR) return NULL;
  988. return self.value.cstr;
  989. }
  990. int badvar_int(BadVar self) {
  991. if (self.type != BADVAR_INT) return 0;
  992. return self.value.i;
  993. }
  994. double badvar_double(BadVar self) {
  995. if (self.type != BADVAR_DOUBLE) return 0.0;
  996. return self.value.d;
  997. }
  998. void badvar_store(BadVar * self, void * ptr) {
  999. switch(self->type) {
  1000. case BADVAR_PTR : (*((void **)ptr)) = self->value.ptr;
  1001. break;
  1002. case BADVAR_FPTR : (*((BadFunction **)ptr)) = self->value.fptr;
  1003. break;
  1004. case BADVAR_CSTR : (*((char**)ptr)) = self->value.cstr;
  1005. break;
  1006. case BADVAR_INT : (*((int*)ptr)) = self->value.i;
  1007. break;
  1008. case BADVAR_DOUBLE: (*((double*)ptr)) = self->value.d;
  1009. break;
  1010. default:
  1011. break;
  1012. }
  1013. }
  1014. BadVar * badvar_load(BadVar * self, int type, void * ptr) {
  1015. self->type = type;
  1016. switch(self->type) {
  1017. case BADVAR_PTR : self->value.ptr = ptr;
  1018. break;
  1019. case BADVAR_FPTR : self->value.fptr = (BadFunction*) ptr;
  1020. break;
  1021. case BADVAR_CSTR : self->value.cstr = (char*) ptr;
  1022. break;
  1023. case BADVAR_INT : self->value.i = (*((int *)ptr));
  1024. break;
  1025. case BADVAR_DOUBLE: self->value.d = (*((double*)ptr));
  1026. break;
  1027. default:
  1028. break;
  1029. }
  1030. return self;
  1031. }
  1032. BadVar badvar_make(int type, void * valptr) {
  1033. BadVar self;
  1034. badvar_load(&self, type, valptr);
  1035. return self;
  1036. }
  1037. int badvar_toarrayva(int argc, BadVar argv[], char * fmt, va_list args) {
  1038. int index;
  1039. for (index = 0; index < argc; index ++) {
  1040. BadVar * var = argv + index;
  1041. int type = (int) fmt[index];
  1042. if (type == 0) return index;
  1043. switch(type) {
  1044. case BADVAR_PTR :
  1045. argv[index] = badvar_makeptr(va_arg(args, void *));
  1046. break;
  1047. case BADVAR_FPTR :
  1048. argv[index] = badvar_makefptr(va_arg(args, BadFunction *));
  1049. break;
  1050. case BADVAR_CSTR :
  1051. argv[index] = badvar_makecstr(va_arg(args, char *));
  1052. break;
  1053. case BADVAR_INT :
  1054. argv[index] = badvar_makeint(va_arg(args, int));
  1055. break;
  1056. case BADVAR_DOUBLE:
  1057. argv[index] = badvar_makedouble(va_arg(args, double));
  1058. break;
  1059. default:
  1060. break;
  1061. }
  1062. }
  1063. return index;
  1064. }
  1065. int badvar_toarraylen(int argc, BadVar argv[], char * fmt, ...) {
  1066. va_list args;
  1067. int result;
  1068. va_start(args, fmt);
  1069. result = badvar_toarrayva(argc, argv, fmt, args);
  1070. va_end(args);
  1071. return result;
  1072. }
  1073. int badvar_toarray(BadVar argv[], char * fmt, ...) {
  1074. va_list args;
  1075. int argc;
  1076. int result;
  1077. argc = strlen(fmt);
  1078. va_start(args, fmt);
  1079. result = badvar_toarrayva(argc, argv, fmt, args);
  1080. va_end(args);
  1081. return result;
  1082. }
  1083. int badvar_fromarrayva(BadVar argv[], int argc, va_list args) {
  1084. int index;
  1085. for (index = 0; index < argc; index ++) {
  1086. BadVar * var = argv + index;
  1087. switch(var->type) {
  1088. case BADVAR_PTR :
  1089. (*va_arg(args, void **)) = badvar_ptr(argv[index]);
  1090. break;
  1091. case BADVAR_FPTR :
  1092. (*va_arg(args, BadFunction **)) = badvar_fptr(argv[index]);
  1093. break;
  1094. case BADVAR_CSTR :
  1095. (*va_arg(args, char **)) = badvar_cstr(argv[index]);
  1096. break;
  1097. case BADVAR_INT :
  1098. (*va_arg(args, int *)) = badvar_int(argv[index]);
  1099. break;
  1100. case BADVAR_DOUBLE:
  1101. (*va_arg(args, double *)) = badvar_double(argv[index]);
  1102. break;
  1103. default:
  1104. break;
  1105. }
  1106. }
  1107. return index;
  1108. }
  1109. int badvar_fromarray(BadVar argv[], int argc, ...) {
  1110. va_list args;
  1111. int result;
  1112. va_start(args, argc);
  1113. result = badvar_fromarrayva(argv, argc, args);
  1114. va_end(args);
  1115. return result;
  1116. }
  1117. /** This may not be very useful since it's hard to define. */
  1118. struct BadVarList_ {
  1119. struct BadVar_ var;
  1120. struct BadListNode_ list;
  1121. };
  1122. struct BadVarList_ *
  1123. badvarlist_init(struct BadVarList_ * self, struct BadVar_ var) {
  1124. self->var = var;
  1125. badlistnode_init(&self->list);
  1126. return self;
  1127. }
  1128. BadVarList *
  1129. badvarlist_initva(BadVarList * self, char * format, va_list args) {
  1130. if((!self) || (!format)) { return NULL; }
  1131. for( ; (*format) ; format++) {
  1132. }
  1133. return self;
  1134. }
  1135. BadVarList *
  1136. badvarlist_initf(BadVarList * self, char * format, ...) {
  1137. BadVarList * result;
  1138. va_list args;
  1139. va_start(args, format);
  1140. result = badvarlist_initva(self, format, args);
  1141. va_end(args);
  1142. return result;
  1143. }
  1144. /*
  1145. This even works on gcc!
  1146. struct try_structfunc2_ { int value; int error ;} try_structfunc(int x, int y) {
  1147. struct try_structfunc2_ result = { 1, 1};
  1148. return result;
  1149. }
  1150. */
  1151. /* Generic array handling that use no structs itself. */
  1152. /* New generic array. */
  1153. void * badgar_new(size_t nmemb, size_t size) {
  1154. return calloc(nmemb, size);
  1155. }
  1156. /* Resizes the array. Returns NULL on failiure (it leaves arr untouched then)
  1157. * On success arr is overwritten with the new reallocated pointer and
  1158. * non-null is returned.
  1159. */
  1160. void * badgar_resize(void ** arr, size_t * nmemb, size_t size, int delta) {
  1161. void * res;
  1162. void * old;
  1163. size_t old_nmemb;
  1164. size_t res_nmemb;
  1165. size_t copy_nmemb;
  1166. if (!arr) return NULL;
  1167. if (!nmemb) return NULL;
  1168. old = (*arr);
  1169. old_nmemb = (*nmemb);
  1170. if(!(old)) return NULL;
  1171. res_nmemb = (size_t)((int)old_nmemb + delta);
  1172. if(res_nmemb < 1) return NULL;
  1173. res = realloc(old, res_nmemb * size);
  1174. (*arr) = res;
  1175. (*nmemb) = res_nmemb;
  1176. return res;
  1177. }
  1178. /* Gets a pointer to an element of the generic array. Returns NULL on range error
  1179. * or if arr is null. */
  1180. void * badgar_get(void * arr, size_t nmemb, size_t size, size_t index) {
  1181. char * ptr = arr; /* char * pointer avoids strict aliasing. */
  1182. if(!arr) return NULL;
  1183. if (index >= nmemb) return NULL;
  1184. return ptr + (index * size);
  1185. }
  1186. /* Stores data in the generic array arr using memmove. */
  1187. void * badgar_put(void * arr, size_t nmemb, size_t size, size_t index, void * data)
  1188. {
  1189. char * ptr = arr; /* char * pointer avoids strict aliasing. */
  1190. if(!arr) return NULL;
  1191. if (index >= nmemb) return NULL;
  1192. memmove(ptr + (index * size), data, size);
  1193. return ptr + (index * size);
  1194. }
  1195. /* Just a wrapper for free(). Retruns NULL */
  1196. void * badgar_free(void * arr) {
  1197. free(arr);
  1198. return NULL;
  1199. }
  1200. /* badpar is for generic arrays of void pointers */
  1201. void * badpar_new(size_t nmemb) {
  1202. return badgar_new(nmemb, sizeof(void*));
  1203. }
  1204. void * badpar_resize(void ** arr, size_t * nmemb, int delta) {
  1205. return badgar_resize(arr, nmemb, sizeof(void*), delta);
  1206. }
  1207. void * badpar_get(void * arr, size_t nmemb, size_t index) {
  1208. return badgar_get(arr, nmemb, sizeof(void*), index);
  1209. }
  1210. void * badpar_put(void * arr, size_t nmemb, size_t index, void * data) {
  1211. return badgar_put(arr, nmemb, sizeof(void*), index, data);
  1212. }
  1213. void * badpar_free(void * arr) {
  1214. return badgar_free(arr);
  1215. }