123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- #include "brl.mod/blitz.mod/blitz.h"
- #include "brl.mod/blitz.mod/tree/tree.h"
- #define generic_compare(x, y) (((x) > (y)) - ((x) < (y)))
- /* +++++++++++++++++++++++++++++++++++++++++++++++++++++ */
- struct intmap_node {
- struct avl_root link;
- int key;
- BBOBJECT value;
- };
- static int compare_intmap_nodes(const void *x, const void *y) {
- struct intmap_node * node_x = (struct intmap_node *)x;
- struct intmap_node * node_y = (struct intmap_node *)y;
- return generic_compare(node_x->key, node_y->key);
- }
- void bmx_map_intmap_clear(struct avl_root ** root) {
- struct intmap_node *node;
- struct intmap_node *tmp;
- if (*root == 0) return; // already cleared?
- avl_for_each_entry_safe(node, tmp, *root, link)
- {
- avl_del(&node->link, root);
- GC_FREE(node);
- }
- }
- int bmx_map_intmap_isempty(struct avl_root * root) {
- return root == 0;
- }
- void bmx_map_intmap_insert( int key, BBObject *value, struct avl_root ** root ) {
- struct intmap_node * node = (struct intmap_node *)GC_malloc_uncollectable(sizeof(struct intmap_node));
- node->key = key;
- node->value = value;
-
- struct intmap_node * old_node = (struct intmap_node *)avl_map(&node->link, compare_intmap_nodes, root);
- if (&node->link != &old_node->link) {
- // key already exists. Store the value in this node.
- old_node->value = value;
- // delete the new node, since we don't need it
- GC_FREE(node);
- }
- }
- int bmx_map_intmap_contains(int key, struct avl_root * root) {
- struct intmap_node node;
- node.key = key;
-
- struct intmap_node * found = (struct intmap_node *)TREE_SEARCH(&node, compare_intmap_nodes, root);
- if (found) {
- return 1;
- } else {
- return 0;
- }
- }
- BBObject * bmx_map_intmap_valueforkey(int key, struct avl_root * root) {
- struct intmap_node node;
- node.key = key;
-
- struct intmap_node * found = (struct intmap_node *)TREE_SEARCH(&node, compare_intmap_nodes, root);
-
- if (found) {
- return found->value;
- }
-
- return &bbNullObject;
- }
- int bmx_map_intmap_remove(int key, struct avl_root ** root) {
- struct intmap_node node;
- node.key = key;
-
- struct intmap_node * found = (struct intmap_node *)TREE_SEARCH(&node, compare_intmap_nodes, *root);
-
- if (found) {
- avl_del(&found->link, root);
- GC_FREE(found);
- return 1;
- } else {
- return 0;
- }
- }
- struct intmap_node * bmx_map_intmap_nextnode(struct intmap_node * node) {
- return (struct intmap_node *)TREE_SUCCESSOR(node);
- }
- struct intmap_node * bmx_map_intmap_firstnode(struct avl_root * root) {
- return (struct intmap_node *)TREE_MIN(root);
- }
- int bmx_map_intmap_key(struct intmap_node * node) {
- return node->key;
- }
- BBObject * bmx_map_intmap_value(struct intmap_node * node) {
- return node->value;
- }
- int bmx_map_intmap_hasnext(struct intmap_node * node, struct avl_root * root) {
- if (!root) {
- return 0;
- }
-
- if (!node) {
- return 1;
- }
-
- return (TREE_SUCCESSOR(node) != 0) ? 1 : 0;
- }
- void bmx_map_intmap_copy(struct avl_root ** dst_root, struct avl_root * src_root) {
- struct intmap_node *src_node;
- struct intmap_node *tmp;
- avl_for_each_entry_safe(src_node, tmp, src_root, link) {
- bmx_map_intmap_insert(src_node->key, src_node->value, dst_root);
- }
- }
- /* +++++++++++++++++++++++++++++++++++++++++++++++++++++ */
- struct ptrmap_node {
- struct avl_root link;
- void * key;
- BBOBJECT value;
- };
- static int compare_ptrmap_nodes(const void *x, const void *y) {
- struct ptrmap_node * node_x = (struct ptrmap_node *)x;
- struct ptrmap_node * node_y = (struct ptrmap_node *)y;
- return generic_compare(node_x->key, node_y->key);
- }
- void bmx_map_ptrmap_clear(struct avl_root ** root) {
- struct ptrmap_node *node;
- struct ptrmap_node *tmp;
- if (*root == 0) return; // already cleared?
- avl_for_each_entry_safe(node, tmp, *root, link) {
- avl_del(&node->link, root);
- GC_FREE(node);
- }
- }
- int bmx_map_ptrmap_isempty(struct avl_root * root) {
- return root == 0;
- }
- void bmx_map_ptrmap_insert( void * key, BBObject *value, struct avl_root ** root ) {
- struct ptrmap_node * node = (struct ptrmap_node *)GC_malloc_uncollectable(sizeof(struct ptrmap_node));
- node->key = key;
- node->value = value;
-
- struct ptrmap_node * old_node = (struct ptrmap_node *)avl_map(&node->link, compare_ptrmap_nodes, root);
- if (&node->link != &old_node->link) {
- // key already exists. Store the value in this node.
- old_node->value = value;
- // delete the new node, since we don't need it
- GC_FREE(node);
- }
- }
- int bmx_map_ptrmap_contains(void * key, struct avl_root * root) {
- struct ptrmap_node node;
- node.key = key;
-
- struct ptrmap_node * found = (struct ptrmap_node *)TREE_SEARCH(&node, compare_ptrmap_nodes, root);
- if (found) {
- return 1;
- } else {
- return 0;
- }
- }
- BBObject * bmx_map_ptrmap_valueforkey(void * key, struct avl_root * root) {
- struct ptrmap_node node;
- node.key = key;
-
- struct ptrmap_node * found = (struct ptrmap_node *) TREE_SEARCH(&node, compare_ptrmap_nodes, root);
-
- if (found) {
- return found->value;
- }
- return &bbNullObject;
- }
- int bmx_map_ptrmap_remove(void * key, struct avl_root ** root) {
- struct ptrmap_node node;
- node.key = key;
-
- struct ptrmap_node * found = (struct ptrmap_node *)TREE_SEARCH(&node, compare_ptrmap_nodes, *root);
-
- if (found) {
- avl_del(&found->link, root);
- GC_FREE(found);
- return 1;
- } else {
- return 0;
- }
- }
- struct ptrmap_node * bmx_map_ptrmap_nextnode(struct ptrmap_node * node) {
- return (struct ptrmap_node *)TREE_SUCCESSOR(node);
- }
- struct ptrmap_node * bmx_map_ptrmap_firstnode(struct avl_root * root) {
- return (struct ptrmap_node *)TREE_MIN(root);
- }
- void * bmx_map_ptrmap_key(struct ptrmap_node * node) {
- return node->key;
- }
- BBObject * bmx_map_ptrmap_value(struct ptrmap_node * node) {
- return node->value;
- }
- int bmx_map_ptrmap_hasnext(struct ptrmap_node * node, struct avl_root * root) {
- if (!root) {
- return 0;
- }
-
- if (!node) {
- return 1;
- }
-
- return (TREE_SUCCESSOR(node) != 0) ? 1 : 0;
- }
- void bmx_map_ptrmap_copy(struct avl_root ** dst_root, struct avl_root * src_root) {
- struct ptrmap_node *src_node;
- struct ptrmap_node *tmp;
- avl_for_each_entry_safe(src_node, tmp, src_root, link) {
- bmx_map_ptrmap_insert(src_node->key, src_node->value, dst_root);
- }
- }
- /* +++++++++++++++++++++++++++++++++++++++++++++++++++++ */
- struct stringmap_node {
- struct avl_root link;
- BBString * key;
- BBOBJECT value;
- };
- static int compare_stringmap_nodes(const void *x, const void *y) {
- struct stringmap_node * node_x = (struct stringmap_node *)x;
- struct stringmap_node * node_y = (struct stringmap_node *)y;
- return generic_compare(node_x->key->hash, node_y->key->hash);
- }
- void bmx_map_stringmap_clear(struct avl_root ** root) {
- struct stringmap_node *node;
- struct stringmap_node *tmp;
- if (*root == 0) return; // already cleared?
- avl_for_each_entry_safe(node, tmp, *root, link) {
- avl_del(&node->link, root);
- GC_FREE(node);
- }
- }
- int bmx_map_stringmap_isempty(struct avl_root * root) {
- return root == 0;
- }
- void bmx_map_stringmap_insert( BBString * key, BBObject *value, struct avl_root ** root) {
- struct stringmap_node * node = (struct stringmap_node *)GC_malloc_uncollectable(sizeof(struct stringmap_node));
- node->key = key;
- node->value = value;
-
- struct stringmap_node * old_node = (struct stringmap_node *)avl_map(&node->link, compare_stringmap_nodes, root);
- if (&node->link != &old_node->link) {
- // key already exists. Store the value in this node.
- old_node->value = value;
- // delete the new node, since we don't need it
- GC_FREE(node);
- }
- }
- int bmx_map_stringmap_contains(BBString * key, struct avl_root * root) {
- struct stringmap_node node;
- node.key = key;
-
- struct stringmap_node * found = (struct stringmap_node *)TREE_SEARCH(&node, compare_stringmap_nodes, root);
- if (found) {
- return 1;
- } else {
- return 0;
- }
- }
- BBObject * bmx_map_stringmap_valueforkey(BBString * key, struct avl_root * root) {
- struct stringmap_node node;
- node.key = key;
-
- struct stringmap_node * found = (struct stringmap_node *)TREE_SEARCH(&node, compare_stringmap_nodes, root);
-
- if (found) {
- return found->value;
- }
-
- return &bbNullObject;
- }
- int bmx_map_stringmap_remove(BBString * key, struct avl_root ** root) {
- struct stringmap_node node;
- node.key = key;
-
- struct stringmap_node * found = (struct stringmap_node *)TREE_SEARCH(&node, compare_stringmap_nodes, *root);
-
- if (found) {
- avl_del(&found->link, root);
- GC_FREE(found);
- return 1;
- } else {
- return 0;
- }
- }
- struct stringmap_node * bmx_map_stringmap_nextnode(struct stringmap_node * node) {
- return (struct stringmap_node *)TREE_SUCCESSOR(node);
- }
- struct stringmap_node * bmx_map_stringmap_firstnode(struct avl_root * root) {
- return (struct stringmap_node *)TREE_MIN(root);
- }
- BBString * bmx_map_stringmap_key(struct stringmap_node * node) {
- return node->key;
- }
- BBObject * bmx_map_stringmap_value(struct stringmap_node * node) {
- return node->value;
- }
- int bmx_map_stringmap_hasnext(struct stringmap_node * node, struct avl_root * root) {
- if (!root) {
- return 0;
- }
-
- if (!node) {
- return 1;
- }
-
- return (TREE_SUCCESSOR(node) != 0) ? 1 : 0;
- }
- void bmx_map_stringmap_copy(struct avl_root ** dst_root, struct avl_root * src_root) {
- struct stringmap_node *src_node;
- struct stringmap_node *tmp;
- avl_for_each_entry_safe(src_node, tmp, src_root, link) {
- bmx_map_stringmap_insert(src_node->key, src_node->value, dst_root);
- }
- }
- /* +++++++++++++++++++++++++++++++++++++++++++++++++++++ */
- struct objectmap_node {
- struct avl_root link;
- BBOBJECT key;
- BBOBJECT value;
- };
- static int compare_objectmap_nodes(const void *x, const void *y) {
- struct objectmap_node * node_x = (struct objectmap_node *)x;
- struct objectmap_node * node_y = (struct objectmap_node *)y;
- return node_x->key->clas->Compare(node_x->key, node_y->key);
- }
- void bmx_map_objectmap_clear(struct avl_root ** root) {
- struct objectmap_node *node;
- struct objectmap_node *tmp;
- if (*root == 0) return; // already cleared?
- avl_for_each_entry_safe(node, tmp, *root, link) {
- avl_del(&node->link, root);
- GC_FREE(node);
- }
- }
- int bmx_map_objectmap_isempty(struct avl_root * root) {
- return root == 0;
- }
- void bmx_map_objectmap_insert( BBObject * key, BBObject *value, struct avl_root ** root) {
- struct objectmap_node * node = (struct objectmap_node *)GC_malloc_uncollectable(sizeof(struct objectmap_node));
- node->key = key;
- node->value = value;
-
- struct objectmap_node * old_node = (struct objectmap_node *)avl_map(&node->link, compare_objectmap_nodes, root);
- if (&node->link != &old_node->link) {
- // key already exists. Store the value in this node.
- old_node->value = value;
- // delete the new node, since we don't need it
- GC_FREE(node);
- }
- }
- int bmx_map_objectmap_contains(BBObject * key, struct avl_root * root) {
- struct objectmap_node node;
- node.key = key;
-
- struct objectmap_node * found = (struct objectmap_node *)TREE_SEARCH(&node, compare_objectmap_nodes, root);
- if (found) {
- return 1;
- } else {
- return 0;
- }
- }
- BBObject * bmx_map_objectmap_valueforkey(BBObject * key, struct avl_root * root) {
- struct objectmap_node node;
- node.key = key;
-
- struct objectmap_node * found = (struct objectmap_node *)TREE_SEARCH(&node, compare_objectmap_nodes, root);
-
- if (found) {
- return found->value;
- }
-
- return &bbNullObject;
- }
- int bmx_map_objectmap_remove(BBObject * key, struct avl_root ** root) {
- struct objectmap_node node;
- node.key = key;
-
- struct objectmap_node * found = (struct objectmap_node *)TREE_SEARCH(&node, compare_objectmap_nodes, *root);
-
- if (found) {
- avl_del(&found->link, root);
- GC_FREE(found);
- return 1;
- } else {
- return 0;
- }
- }
- struct objectmap_node * bmx_map_objectmap_nextnode(struct objectmap_node * node) {
- return (struct objectmap_node *)TREE_SUCCESSOR(node);
- }
- struct objectmap_node * bmx_map_objectmap_firstnode(struct avl_root * root) {
- return (struct objectmap_node *)TREE_MIN(root);
- }
- BBObject * bmx_map_objectmap_key(struct objectmap_node * node) {
- return node->key;
- }
- BBObject * bmx_map_objectmap_value(struct objectmap_node * node) {
- return node->value;
- }
- int bmx_map_objectmap_hasnext(struct objectmap_node * node, struct avl_root * root) {
- if (!root) {
- return 0;
- }
-
- if (!node) {
- return 1;
- }
-
- return (TREE_SUCCESSOR(node) != 0) ? 1 : 0;
- }
- void bmx_map_objectmap_copy(struct avl_root ** dst_root, struct avl_root * src_root) {
- struct objectmap_node *src_node;
- struct objectmap_node *tmp;
- avl_for_each_entry_safe(src_node, tmp, src_root, link) {
- bmx_map_objectmap_insert(src_node->key, src_node->value, dst_root);
- }
- }
|