bad.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. * File: bad.h
  3. *
  4. * Bad means Beoran's Algorithms and Datastructures
  5. * (or Better Algorithms and Datastructures).
  6. * It's a generic library for C that has all sorts of useful data structures
  7. * and algorithms in it, under a permissive license.
  8. * OK, BAD may be a bad name, but I hope you find the contents not too bad. :)
  9. *
  10. * One charactersistic of Bad is that it's data structures are normally
  11. * intrusive, that is they must be included in the struct that you want
  12. * to put inside the given data structure. Also, normally the data
  13. * structures are doubly linked for fast iteration in any direction.
  14. *
  15. * License: bad.h and bad.c are
  16. *
  17. * Copyright (C) 2012-2013 beoran (beoran@beoran.net)
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this software and associated documentation files (the "Software"), to deal
  21. * in the Software without restriction, including without limitation the rights
  22. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. * copies of the Software, and to permit persons to whom the Software is
  24. * furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in
  27. * all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  35. * SOFTWARE.
  36. *
  37. */
  38. #ifndef BAD_H_INCLUDED
  39. #define BAD_H_INCLUDED
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <stdarg.h>
  43. #include <stddef.h>
  44. /* Const: FALSE.
  45. * Of course we define our own boolean value for FALSE.
  46. */
  47. #ifndef FALSE
  48. #define FALSE 0
  49. #endif
  50. /* Const: TRUE.
  51. * Of course we define our own boolean value for TRUE.
  52. */
  53. #ifndef TRUE
  54. #define TRUE (!FALSE)
  55. #endif
  56. /* Macro: bool
  57. * For integers that are used as boolean values.
  58. */
  59. #ifndef bool
  60. #define bool int
  61. #endif
  62. /* Some simple numerical comparison, min, max, etc functions. */
  63. int bad_mini(int one, int two);
  64. double bad_mind(double one, double two);
  65. float bad_minf(float one, float two);
  66. int bad_maxi(int one, int two);
  67. double bad_maxd(double one, double two);
  68. float bad_maxf(float one, float two);
  69. int bad_comparei(int one, int two);
  70. int bad_compared(double one, double two);
  71. int bad_comparef(float one, float two);
  72. int bad_clampi(int value , int min, int max) ;
  73. double bad_clampd(double value , double min, double max);
  74. float bad_clampf(float value , float min, float max);
  75. int bad_outofboundsi(int value, int min, int max);
  76. int bad_outofboundsd(int value, double min, double max);
  77. int bad_outofboundsf(int value, float min, float max);
  78. /* Macro: bad_container(PTR, TYPE, MEMBER)
  79. This macro returns, for the given pointer, a pointer to a containing struct
  80. of type TYPE, in which PTR is a member named MEMBER.
  81. This enables cool ways of type genericity and extension in plain C.
  82. */
  83. #define bad_container(PTR, TYPE, MEMBER) \
  84. ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, MEMBER)))
  85. typedef struct BadListNode_ BadListNode;
  86. typedef struct BadList_ BadList;
  87. typedef int BadListNodeCompare(BadListNode * one, BadListNode * two);
  88. typedef int BadListNodeEach(BadListNode * elem, void * data);
  89. typedef int BadListNodeSearchValue(BadListNode * elem, void * data);
  90. /* Struct: BadList_
  91. * BadListNode is a doubly linked list head.
  92. *
  93. */
  94. struct BadList_ {
  95. struct BadListNode_ * head;
  96. struct BadListNode_ * tail;
  97. int size;
  98. };
  99. /* Struct: BadListNode_
  100. * BadListNode is a doubly linked list node.
  101. *
  102. */
  103. struct BadListNode_ {
  104. struct BadListNode_ * next;
  105. struct BadListNode_ * prev;
  106. };
  107. /* Returns the list node for this list element is part of,
  108. for the give list element, and data type*/
  109. #define badlistnode_getdata(LIST, TYPE, MEMBER) \
  110. badlistnode_data(LIST, offsetof(TYPE, MEMBER))
  111. /* Shorthand for INLI_DATA(LIST, DATA, list) */
  112. #define badlistnode_listdata(LIST, TYPE) inli_getdata(LIST, TYPE, list)
  113. BadListNode * badlistnode_initempty(BadListNode * self);
  114. bool badlistnode_isempty(BadListNode * self);
  115. BadListNode * badlistnode_initall(BadListNode * self , BadListNode * next , BadListNode * prev );
  116. BadListNode * badlistnode_init(BadListNode * self);
  117. BadListNode * badlistnode_unlink(BadListNode * self);
  118. BadListNode * badlistnode_add(BadListNode * self , BadListNode * other );
  119. BadListNode * badlistnode_next(BadListNode * self );
  120. BadListNode * badlistnode_prev(BadListNode * self );
  121. BadListNode * badlistnode_first(BadListNode * self );
  122. BadListNode * badlistnode_last(BadListNode * self );
  123. BadListNode * badlistnode_push(BadListNode * self , BadListNode * other );
  124. BadListNode * badlistnode_unshift(BadListNode * self , BadListNode * other );
  125. BadListNode * badlistnode_shift(BadListNode * self );
  126. BadListNode * badlistnode_pop(BadListNode * self );
  127. void * badlistnode_data(BadListNode * self , int offset);
  128. BadListNode *
  129. badlistnode_search(BadListNode * self, BadListNodeCompare * compare, BadListNode * tofind);
  130. BadListNode *
  131. badlistnode_remove(BadListNode * self, BadListNodeCompare * compare, BadListNode * toremove);
  132. int badlistnode_each(BadListNode * self, BadListNodeEach * each, void * data);
  133. BadListNode *
  134. badlistnode_searchvalue(BadListNode * self, BadListNodeSearchValue * compare, void * tofind);
  135. BadList * badlist_init (BadList * self);
  136. BadList * badlist_add (BadList * self, BadListNode * node);
  137. BadList * badlist_shift (BadList * self, BadListNode * node);
  138. BadList * badlist_remove(BadList * self, BadListNode * node);
  139. BadListNode * badlist_head (BadList * self);
  140. BadListNode * badlist_tail (BadList * self);
  141. int badlist_size (BadList * self);
  142. typedef struct BadBitree_ BadBitree;
  143. typedef int BadBitreeCompare(BadBitree * one, BadBitree * two);
  144. struct BadBitree_ {
  145. struct BadBitree_ * up;
  146. struct BadBitree_ * left;
  147. struct BadBitree_ * right;
  148. };
  149. typedef struct BadAatree_ BadAatree;
  150. typedef int BadAatreeCompare(BadAatree * one, BadAatree * two);
  151. typedef int BadAatreeSetValue(BadAatree * to, BadAatree * from);
  152. typedef int BadAatreeCompareKey(BadAatree * self, void * key);
  153. typedef void * BadAatreeGetKey(BadAatree * self);
  154. typedef struct BadAatreeMethods_ BadAatreeMethods;
  155. struct BadAatreeMethods_ {
  156. BadAatreeCompare * compare;
  157. BadAatreeSetValue * setvalue;
  158. BadAatreeCompareKey * comparekey;
  159. BadAatreeCompareKey * getkey;
  160. };
  161. struct BadAatree_ {
  162. struct BadBitree_ tree;
  163. int level;
  164. };
  165. BadBitree *
  166. badbitree_initall(BadBitree * self, BadBitree * left, BadBitree * right,
  167. BadBitree * up);
  168. BadBitree *
  169. badbitree_init(BadBitree * self);
  170. BadBitree *
  171. badbitree_left(BadBitree * self);
  172. BadBitree *
  173. badbitree_right(BadBitree * self);
  174. BadBitree *
  175. badbitree_up(BadBitree * self);
  176. BadBitree *
  177. badbitree_up_(BadBitree * self, BadBitree * newup);
  178. BadBitree *
  179. badbitree_left_(BadBitree * self, BadBitree * newleft);
  180. BadBitree *
  181. badbitree_right_(BadBitree * self, BadBitree * newright);
  182. bool badbitree_isleaf(BadBitree * self);
  183. BadAatree *
  184. badaatree_initall(BadAatree * self, BadAatree * left, BadAatree * right,
  185. BadAatree * up, int level);
  186. BadAatree *
  187. badaatree_init(BadAatree * self);
  188. BadAatree *
  189. badaatree_left(BadAatree * self);
  190. BadAatree *
  191. badaatree_right(BadAatree * self);
  192. BadAatree *
  193. badaatree_up(BadAatree * self);
  194. BadAatree *
  195. badaatree_left_(BadAatree * self, BadAatree * other);
  196. BadAatree *
  197. badaatree_right_(BadAatree * self, BadAatree * other);
  198. BadAatree *
  199. badaatree_up_(BadAatree * self, BadAatree * other);
  200. int badaatree_level(BadAatree * self);
  201. int badaatree_level_(BadAatree * self, int newlevel);
  202. bool badaatree_isleaf(BadAatree * self);
  203. BadAatree * badaatree_skew(BadAatree * self);
  204. BadAatree * badaatree_split(BadAatree * self);
  205. BadAatree * badaatree_insert(BadAatree * self, BadAatree * node,
  206. BadAatreeMethods * methods);
  207. BadAatree * badaatree_search(BadAatree * self, BadAatree * node,
  208. BadAatreeMethods * methods);
  209. BadAatree * badaatree_searchkey(BadAatree * self, void * key,
  210. BadAatreeMethods * methods);
  211. BadAatree * badaatree_leveldownall(BadAatree * self);
  212. BadAatree * badaatree_successor(BadAatree * self);
  213. BadAatree * badaatree_predecessor(BadAatree * self);
  214. BadAatree * badaatree_delete(BadAatree * self, BadAatree * node,
  215. BadAatreeMethods * methods);
  216. BadAatree * badaatree_deletekey(BadAatree * self, void * key,
  217. BadAatreeMethods * methods);
  218. typedef struct BadChildtree_ BadChildtree;
  219. /*
  220. * Struct: BadChildtree
  221. *
  222. * A BadChildtree is a tree where every node can have any number of children.
  223. *
  224. */
  225. struct BadChildtree_ {
  226. BadChildtree * parent;
  227. BadChildtree * child;
  228. BadChildtree * next;
  229. BadChildtree * previous;
  230. };
  231. BadChildtree *
  232. badchildtree_initall(BadChildtree * self, BadChildtree * parent, BadChildtree * child,
  233. BadChildtree * next_sibling, BadChildtree * previous_sibling);
  234. BadChildtree *
  235. badchildtree_initnull(BadChildtree * self);
  236. bool
  237. badchildtree_isempty(BadChildtree * self);
  238. BadChildtree *
  239. badchildtree_parent(BadChildtree * self);
  240. BadChildtree *
  241. badchildtree_child(BadChildtree * self);
  242. BadChildtree *
  243. badchildtree_next(BadChildtree * self);
  244. BadChildtree *
  245. badchildtree_lastsibling(BadChildtree * self);
  246. BadChildtree *
  247. badchildtree_lastchild(BadChildtree * self);
  248. BadChildtree *
  249. badchildtree_previous(BadChildtree * self);
  250. BadChildtree *
  251. badchildtree_insertsibling(BadChildtree * self, BadChildtree * sibling);
  252. BadChildtree *
  253. badchildtree_appendsibling(BadChildtree * self, BadChildtree * sibling);
  254. BadChildtree *
  255. badchildtree_appendchild(BadChildtree * self, BadChildtree * child);
  256. void badchildtree_printgraph(BadChildtree * self, int level);
  257. typedef struct BadVar_ BadVar;
  258. typedef struct BadVarList_ BadVarList;
  259. typedef void BadFunction(void);
  260. typedef BadFunction * BadFunctionPtr;
  261. union BadVarUnion_ {
  262. void * ptr;
  263. BadFunction * fptr;
  264. char * cstr;
  265. int i;
  266. double d;
  267. };
  268. enum BadVarEnum_ {
  269. BADVAR_NONE = 0 ,
  270. BADVAR_PTR = 'p',
  271. BADVAR_FPTR = 'P',
  272. BADVAR_CSTR = 's',
  273. BADVAR_INT = 'i',
  274. BADVAR_DOUBLE = 'f',
  275. };
  276. struct BadVar_ {
  277. enum BadVarEnum_ type;
  278. union BadVarUnion_ value;
  279. };
  280. struct BadVar_ badvar_makeint(int value);
  281. struct BadVar_ badvar_makedouble(double value);
  282. struct BadVar_ badvar_makeptr(void * value);
  283. struct BadVar_ badvar_makecstr(char * value);
  284. struct BadVar_ badvar_makefptr(BadFunction * value);
  285. void * badvar_ptr(BadVar self);
  286. BadFunction * badvar_fptr(BadVar self);
  287. char * badvar_cstr(BadVar self);
  288. int badvar_int(BadVar self);
  289. double badvar_double(BadVar self);
  290. void badvar_store(BadVar * self, void * ptr);
  291. BadVar * badvar_load(BadVar * self, int type, void * ptr) ;
  292. BadVar badvar_make(int type, void * valptr) ;
  293. int badvar_toarrayva(int argc, BadVar argv[], char * fmt, va_list args) ;
  294. int badvar_toarraylen(int argc, BadVar argv[], char * fmt, ...);
  295. int badvar_toarray(BadVar argv[], char * fmt, ...);
  296. int badvar_fromarrayva(BadVar argv[], int argc, va_list args);
  297. int badvar_fromarray(BadVar argv[], int argc, ...);
  298. /* a "Stab" is a String Table, that is, a table of void * pointers
  299. index by strings. The Stab does not own it's pointers
  300. but it does own copies of the string keys, which
  301. it will clean up when needed. */
  302. typedef struct BadStab_ BadStab;
  303. typedef struct BadStabEntry_ BadStabEntry;
  304. struct BadStabEntry_ {
  305. char * key;
  306. void * value;
  307. };
  308. struct BadStab_ {
  309. BadStabEntry * entries;
  310. int size;
  311. };
  312. BadStab * badstab_init(BadStab * self);
  313. BadStab * badstab_done(BadStab * self);
  314. void * badstab_put(BadStab * self, char * key, void * value);
  315. void * badstab_get(BadStab * self, char * key);
  316. void * badgar_new(size_t nmemb, size_t size);
  317. void * badgar_resize(void ** arr, size_t * nmemb, size_t size, int delta);
  318. void * badgar_get(void * arr, size_t nmemb, size_t size, size_t index);
  319. void * badgar_put(void * arr, size_t nmemb, size_t size, size_t index, void * data);
  320. void * badgar_free(void * arr);
  321. void * badpar_new(size_t nmemb);
  322. void * badpar_resize(void ** arr, size_t * nmemb, int delta);
  323. void * badpar_get(void * arr, size_t nmemb, size_t index);
  324. void * badpar_put(void * arr, size_t nmemb, size_t index, void * data);
  325. void * badpar_free(void * arr);
  326. #endif /* BAD_H_INCLUDED */