common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // # ifdef __cplusplus
  2. // extern "C" {
  3. // # endif
  4. // #ifndef LIST_H
  5. // # define LIST_H
  6. //===============================================================================================================================================================================================================200
  7. // DEFINE/INCLUDE
  8. //===============================================================================================================================================================================================================200
  9. //======================================================================================================================================================150
  10. // INCLUDE (for some reason these are not recognized when defined in main file before this one is included)
  11. //======================================================================================================================================================150
  12. #include <stdint.h> // (in path known to compiler) needed by uint32_t
  13. #include <stdbool.h> // (in path known to compiler) needed by true/false, bool
  14. #include <stdlib.h> // (in path known to compiler) needed by malloc
  15. //======================================================================================================================================================150
  16. // DEFINE
  17. //======================================================================================================================================================150
  18. #define fp float
  19. #define Version "1.5"
  20. #ifdef WINDOWS
  21. #define bool char
  22. #define false 0
  23. #define true 1
  24. #endif
  25. /* #define DEFAULT_ORDER 256 */
  26. #ifdef RD_WG_SIZE_0_0
  27. #define DEFAULT_ORDER RD_WG_SIZE_0_0
  28. #elif defined(RD_WG_SIZE_0)
  29. #define DEFAULT_ORDER RD_WG_SIZE_0
  30. #elif defined(RD_WG_SIZE)
  31. #define DEFAULT_ORDER RD_WG_SIZE
  32. #else
  33. #define DEFAULT_ORDER 256
  34. #endif
  35. #ifdef RD_WG_SIZE_1_0
  36. #define DEFAULT_ORDER_2 RD_WG_SIZE_1_0
  37. #elif defined(RD_WG_SIZE_1)
  38. #define DEFAULT_ORDER_2 RD_WG_SIZE_1
  39. #elif defined(RD_WG_SIZE)
  40. #define DEFAULT_ORDER_2 RD_WG_SIZE
  41. #else
  42. #define DEFAULT_ORDER_2 256
  43. #endif
  44. #define malloc(size) ({ \
  45. void *_tmp; \
  46. \
  47. if (!(_tmp = malloc(size))) { \
  48. fprintf(stderr, "Allocation failed at %s:%d!\n", __FILE__, __LINE__); \
  49. exit(-1); \
  50. } \
  51. \
  52. _tmp; \
  53. })
  54. //======================================================================================================================================================150
  55. // STRUCTURES
  56. //======================================================================================================================================================150
  57. // struct list_item;
  58. typedef struct list_item list_item_t;
  59. typedef struct list_t {
  60. list_item_t *head, *tail;
  61. uint32_t length;
  62. int32_t (*compare)(const void *key, const void *with);
  63. void (*datum_delete)(void *);
  64. } list_t;
  65. typedef list_item_t *list_iterator_t;
  66. typedef list_item_t *list_reverse_iterator_t;
  67. /* Type representing the record
  68. * to which a given key refers.
  69. * In a real B+ tree system, the
  70. * record would hold data (in a database)
  71. * or a file (in an operating system)
  72. * or some other information.
  73. * Users can rewrite this part of the code
  74. * to change the type and content
  75. * of the value field.
  76. */
  77. typedef struct record {
  78. int value;
  79. } record;
  80. /* Type representing a node in the B+ tree.
  81. * This type is general enough to serve for both
  82. * the leaf and the internal node.
  83. * The heart of the node is the array
  84. * of keys and the array of corresponding
  85. * pointers. The relation between keys
  86. * and pointers differs between leaves and
  87. * internal nodes. In a leaf, the index
  88. * of each key equals the index of its corresponding
  89. * pointer, with a maximum of order - 1 key-pointer
  90. * pairs. The last pointer points to the
  91. * leaf to the right (or NULL in the case
  92. * of the rightmost leaf).
  93. * In an internal node, the first pointer
  94. * refers to lower nodes with keys less than
  95. * the smallest key in the keys array. Then,
  96. * with indices i starting at 0, the pointer
  97. * at i + 1 points to the subtree with keys
  98. * greater than or equal to the key in this
  99. * node at index i.
  100. * The num_keys field is used to keep
  101. * track of the number of valid keys.
  102. * In an internal node, the number of valid
  103. * pointers is always num_keys + 1.
  104. * In a leaf, the number of valid pointers
  105. * to data is always num_keys. The
  106. * last leaf pointer points to the next leaf.
  107. */
  108. typedef struct node {
  109. void ** pointers;
  110. int * keys;
  111. struct node * parent;
  112. bool is_leaf;
  113. int num_keys;
  114. struct node * next; // Used for queue.
  115. } node;
  116. //
  117. typedef struct knode {
  118. int location;
  119. int indices [DEFAULT_ORDER + 1];
  120. int keys [DEFAULT_ORDER + 1];
  121. bool is_leaf;
  122. int num_keys;
  123. } knode;
  124. struct list_item {
  125. struct list_item *pred, *next;
  126. void *datum;
  127. };
  128. //===============================================================================================================================================================================================================200
  129. // PROTOTYPES
  130. //===============================================================================================================================================================================================================200
  131. //======================================================================================================================================================150
  132. // Other
  133. //======================================================================================================================================================150
  134. void
  135. list_item_init( list_item_t *li,
  136. void *datum);
  137. void
  138. list_item_delete( list_item_t *li,
  139. void (*datum_delete)(void *datum));
  140. void
  141. list_insert_item_tail( list_t *l,
  142. list_item_t *i);
  143. void
  144. list_insert_item_before(list_t *l,
  145. list_item_t *next,
  146. list_item_t *i);
  147. void
  148. list_insert_item_after( list_t *l,
  149. list_item_t *pred,
  150. list_item_t *i);
  151. void
  152. list_insert_item_sorted(list_t *l,
  153. list_item_t *i);
  154. //======================================================================================================================================================150
  155. // ???
  156. //======================================================================================================================================================150
  157. void
  158. list_init( list_t *l,
  159. int32_t (*compare)(const void *key, const void *with),
  160. void (*datum_delete)(void *datum));
  161. void
  162. list_delete(list_t *l);
  163. void
  164. list_reset(list_t *l);
  165. void
  166. list_insert_head( list_t *l,
  167. void *v);
  168. void
  169. list_insert_tail( list_t *l,
  170. void *v);
  171. void
  172. list_insert_before(list_t *l,
  173. list_item_t *next,
  174. void *v);
  175. void
  176. list_insert_after( list_t *l,
  177. list_item_t *pred,
  178. void *v);
  179. void
  180. list_insert_sorted( list_t *l,
  181. void *v);
  182. void
  183. list_insert_item_head( list_t *l,
  184. list_item_t *i);
  185. void
  186. list_remove_item( list_t *l,
  187. list_item_t *i);
  188. void
  189. list_remove_head(list_t *l);
  190. void
  191. list_remove_tail(list_t *l);
  192. list_item_t *
  193. list_find_item( list_t *l,
  194. void *datum);
  195. list_item_t *
  196. list_get_head_item(list_t *l);
  197. list_item_t *
  198. list_get_tail_item(list_t *l);
  199. void *
  200. list_find( list_t *l,
  201. void *datum);
  202. void *
  203. list_get_head(list_t *l);
  204. void *
  205. list_get_tail(list_t *l);
  206. uint32_t
  207. list_get_length(list_t *l);
  208. bool
  209. list_is_empty(list_t *l);
  210. bool
  211. list_not_empty(list_t *l);
  212. void
  213. list_visit_items( list_t *l,
  214. void (*visitor)(void *v));
  215. void *
  216. list_item_get_datum(list_item_t *li);
  217. void
  218. list_iterator_init( list_t *l,
  219. list_iterator_t *li);
  220. void
  221. list_iterator_delete(list_iterator_t *li);
  222. void
  223. list_iterator_next(list_iterator_t *li);
  224. void
  225. list_iterator_prev(list_iterator_t *li);
  226. void *
  227. list_iterator_get_datum(list_iterator_t *li);
  228. bool
  229. list_iterator_is_valid(list_iterator_t *li);
  230. void
  231. list_reverse_iterator_init( list_t *l,
  232. list_iterator_t *li);
  233. void
  234. list_reverse_iterator_delete(list_iterator_t *li);
  235. void
  236. list_reverse_iterator_next(list_iterator_t *li);
  237. void
  238. list_reverse_iterator_prev(list_iterator_t *li);
  239. void *
  240. list_reverse_iterator_get_datum(list_iterator_t *li);
  241. bool
  242. list_reverse_iterator_is_valid(list_reverse_iterator_t *li);
  243. //======================================================================================================================================================150
  244. // Output and utility
  245. //======================================================================================================================================================150
  246. void *
  247. kmalloc(int size);
  248. long
  249. transform_to_cuda( node *n,
  250. bool verbose); //returns actual mem used in a long
  251. void
  252. usage_1( void );
  253. void
  254. usage_2( void );
  255. void
  256. enqueue( node * new_node );
  257. node *
  258. dequeue( void );
  259. int
  260. height( node * root );
  261. int
  262. path_to_root( node * root,
  263. node * child );
  264. void
  265. print_leaves( node * root );
  266. void
  267. print_tree( node * root );
  268. node *
  269. find_leaf( node * root,
  270. int key,
  271. bool verbose );
  272. record *
  273. find( node * root,
  274. int key,
  275. bool verbose );
  276. int
  277. cut( int length );
  278. //======================================================================================================================================================150
  279. // Insertion
  280. //======================================================================================================================================================150
  281. record *
  282. make_record(int value);
  283. node *
  284. make_node( void );
  285. node *
  286. make_leaf( void );
  287. int
  288. get_left_index( node * parent,
  289. node * left);
  290. node *
  291. insert_into_leaf( node * leaf,
  292. int key, record * pointer );
  293. node *
  294. insert_into_leaf_after_splitting( node * root,
  295. node * leaf,
  296. int key,
  297. record * pointer);
  298. node *
  299. insert_into_node( node * root,
  300. node * parent,
  301. int left_index,
  302. int key,
  303. node * right);
  304. node *
  305. insert_into_node_after_splitting( node * root,
  306. node * parent,
  307. int left_index,
  308. int key,
  309. node * right);
  310. node *
  311. insert_into_parent( node * root,
  312. node * left,
  313. int key,
  314. node * right);
  315. node *
  316. insert_into_new_root( node * left,
  317. int key,
  318. node * right);
  319. node *
  320. start_new_tree( int key,
  321. record * pointer);
  322. node *
  323. insert( node * root,
  324. int key,
  325. int value );
  326. //======================================================================================================================================================150
  327. // Deletion
  328. //======================================================================================================================================================150
  329. int
  330. get_neighbor_index(node * n );
  331. node *
  332. adjust_root(node * root);
  333. node *
  334. coalesce_nodes( node * root,
  335. node * n,
  336. node * neighbor,
  337. int neighbor_index,
  338. int k_prime);
  339. node *
  340. redistribute_nodes( node * root,
  341. node * n,
  342. node * neighbor,
  343. int neighbor_index,
  344. int k_prime_index,
  345. int k_prime);
  346. node *
  347. delete_entry( node * root,
  348. node * n,
  349. int key,
  350. void * pointer );
  351. node *
  352. deleteVal( node * root,
  353. int key );
  354. //===============================================================================================================================================================================================================200
  355. // HEADER
  356. //===============================================================================================================================================================================================================200
  357. // int main( int argc,
  358. // char *argv []);
  359. //===============================================================================================================================================================================================================200
  360. // END
  361. //===============================================================================================================================================================================================================200
  362. // #endif
  363. // # ifdef __cplusplus
  364. // }
  365. // # endif