| 1 | /* Esl_red_black.h -- data structures and function headers for red-black trees */ |
| 2 | /* Note: these routines were designed for the case where one builds a tree of data during |
| 3 | execution and then extracts the contents of the tree in some order. They do not currently |
| 4 | support deletes in a way that maintains the red-black properties of the tree */ |
| 5 | |
| 6 | #ifndef ESL_RED_BLACK_INCLUDED |
| 7 | #define ESL_RED_BLACK_INCLUDED |
| 8 | #include <esl_config.h> |
| 9 | |
| 10 | #define ESL_RED_BLACK_COLOR_RED 0 |
| 11 | #define ESL_RED_BLACK_COLOR_BLACK 1 |
| 12 | |
| 13 | typedef struct esl_red_black_doublekey_s { |
| 14 | void *contents; // Pointer to the data object stored at this node. Leave NULL if you have no |
| 15 | // Contents other than the key field |
| 16 | |
| 17 | double key; // key that this node will be sorted on |
| 18 | struct esl_red_black_doublekey_s *parent; // Pointer to this node's parent |
| 19 | struct esl_red_black_doublekey_s *large; // Pointer to this node's larger key value child |
| 20 | struct esl_red_black_doublekey_s *small; // Pointer to this node's smaller key value child |
| 21 | |
| 22 | uint64_t color; // No, we don't need 64 bits for what is really a one-bit value, but this |
| 23 | // will make the structure an even number of 64-bit quantities. |
| 24 | } ESL_RED_BLACK_DOUBLEKEY; |
| 25 | |
| 26 | |
| 27 | // creates, initializes, and returns an esl_red_black_doublekey object. Returns NULL if |
| 28 | // it was unable to allocate memory |
| 29 | ESL_RED_BLACK_DOUBLEKEY * esl_red_black_doublekey_Create(); |
| 30 | |
| 31 | |
| 32 | // Destroys an ESL_RED_BLACK_DOUBLEKEY object by recursively freeing all of the nodes in the tree |
| 33 | void esl_red_black_doublekey_Destroy(ESL_RED_BLACK_DOUBLEKEY *tree); |
| 34 | |
| 35 | // Destroys an ESL_RED_BLACK_DOUBLEKEY tree that has been converted into a doubly-linked list |
| 36 | void esl_red_black_doublekey_linked_list_Destroy(ESL_RED_BLACK_DOUBLEKEY *head, ESL_RED_BLACK_DOUBLEKEY *tail); |
| 37 | |
| 38 | // Creates and initializes <number> esl_red_black_doublekey objects, links them into a list |
| 39 | // using their larger pointers, and returns a pointer to the head of the list. Allocates |
| 40 | // all of the memory required in one malloc, so should be faster than calling |
| 41 | // esl_red_black_doublekey <number> times. Returns NULL if it was unable to allocate memory |
| 42 | ESL_RED_BLACK_DOUBLEKEY * esl_red_black_doublekey_pool_Create(int number); |
| 43 | |
| 44 | // attempts to insert the specified node into the specified tree, using its key |
| 45 | // to determine where the node should go in the tree. Returns the tree with the inserted node |
| 46 | // if the insertion was successful, NULL otherwise (only occurs if there was already a node |
| 47 | // in the tree with the same key). |
| 48 | ESL_RED_BLACK_DOUBLEKEY * esl_red_black_doublekey_insert(ESL_RED_BLACK_DOUBLEKEY *tree, ESL_RED_BLACK_DOUBLEKEY *node); |
| 49 | |
| 50 | // looks for a node with key = keyval. Returns its contents pointer if it finds one |
| 51 | // returns NULL otherwise. Note that, since we're using doubles as key values, |
| 52 | // all of the standard concerns about equality comparisons apply |
| 53 | void * esl_red_black_doublekey_lookup(ESL_RED_BLACK_DOUBLEKEY *tree, double keyval); |
| 54 | |
| 55 | |
| 56 | // Converts the input red-black tree into a doubly-linked list. Returns eslOK if the conversion |
| 57 | // succeeded. Returns a pointer to the node with the largest key in head, a pointer to the node |
| 58 | // with the smallest key in tail |
| 59 | int esl_red_black_doublekey_convert_to_sorted_linked(ESL_RED_BLACK_DOUBLEKEY *tree, ESL_RED_BLACK_DOUBLEKEY **head, ESL_RED_BLACK_DOUBLEKEY **tail); |
| 60 | |
| 61 | #endif // ESL_RED_BLACK_INCLUDED |