maps.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright (c) 2015 Bruce Henderson
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgement in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. #include "brl.mod/blitz.mod/blitz.h"
  18. #ifdef BMX_NG
  19. #include "brl.mod/blitz.mod/tree/tree.h"
  20. #else
  21. #include "tree/tree.h"
  22. #endif
  23. #define generic_compare(x, y) (((x) > (y)) - ((x) < (y)))
  24. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  25. struct stringfloatmap_node {
  26. struct avl_root link;
  27. BBString * key;
  28. float value;
  29. };
  30. static int compare_stringfloatmap_nodes(const void *x, const void *y) {
  31. struct stringfloatmap_node * node_x = (struct stringfloatmap_node *)x;
  32. struct stringfloatmap_node * node_y = (struct stringfloatmap_node *)y;
  33. return bbStringCompare(node_x->key, node_y->key);
  34. }
  35. void bmx_map_stringfloatmap_clear(struct avl_root ** root) {
  36. struct stringfloatmap_node *node;
  37. struct stringfloatmap_node *tmp;
  38. avl_for_each_entry_safe(node, tmp, *root, link) {
  39. BBRELEASE(node->key);
  40. avl_del(&node->link, root);
  41. free(node);
  42. }
  43. }
  44. int bmx_map_stringfloatmap_isempty(struct avl_root ** root) {
  45. return *root == 0;
  46. }
  47. void bmx_map_stringfloatmap_insert( BBString * key, float value, struct avl_root ** root) {
  48. struct stringfloatmap_node * node = (struct stringfloatmap_node *)malloc(sizeof(struct stringfloatmap_node));
  49. node->key = key;
  50. BBRETAIN(key);
  51. node->value = value;
  52. struct stringfloatmap_node * old_node = (struct stringfloatmap_node *)avl_map(&node->link, compare_stringfloatmap_nodes, root);
  53. if (&node->link != &old_node->link) {
  54. BBRELEASE(old_node->key);
  55. // key already exists. Store the value in this node.
  56. old_node->value = value;
  57. // delete the new node, since we don't need it
  58. free(node);
  59. }
  60. }
  61. int bmx_map_stringfloatmap_contains(BBString * key, struct avl_root ** root) {
  62. struct stringfloatmap_node node;
  63. node.key = key;
  64. struct stringfloatmap_node * found = (struct stringfloatmap_node *)tree_search(&node, compare_stringfloatmap_nodes, *root);
  65. if (found) {
  66. return 1;
  67. } else {
  68. return 0;
  69. }
  70. }
  71. float bmx_map_stringfloatmap_valueforkey(BBString * key, struct avl_root ** root) {
  72. struct stringfloatmap_node node;
  73. node.key = key;
  74. struct stringfloatmap_node * found = (struct stringfloatmap_node *)tree_search(&node, compare_stringfloatmap_nodes, *root);
  75. if (found) {
  76. return found->value;
  77. }
  78. return 0.0f;
  79. }
  80. int bmx_map_stringfloatmap_remove(BBString * key, struct avl_root ** root) {
  81. struct stringfloatmap_node node;
  82. node.key = key;
  83. struct stringfloatmap_node * found = (struct stringfloatmap_node *)tree_search(&node, compare_stringfloatmap_nodes, *root);
  84. if (found) {
  85. BBRELEASE(found->key);
  86. avl_del(&found->link, root);
  87. free(found);
  88. return 1;
  89. } else {
  90. return 0;
  91. }
  92. }
  93. struct stringfloatmap_node * bmx_map_stringfloatmap_nextnode(struct stringfloatmap_node * node) {
  94. return tree_successor(node);
  95. }
  96. struct stringfloatmap_node * bmx_map_stringfloatmap_firstnode(struct avl_root * root) {
  97. return tree_min(root);
  98. }
  99. BBString * bmx_map_stringfloatmap_key(struct stringfloatmap_node * node) {
  100. return node->key;
  101. }
  102. float bmx_map_stringfloatmap_value(struct stringfloatmap_node * node) {
  103. return node->value;
  104. }
  105. int bmx_map_stringfloatmap_hasnext(struct stringfloatmap_node * node, struct avl_root * root) {
  106. if (!root) {
  107. return 0;
  108. }
  109. if (!node) {
  110. return 1;
  111. }
  112. return (tree_successor(node) != 0) ? 1 : 0;
  113. }
  114. void bmx_map_stringfloatmap_copy(struct avl_root ** dst_root, struct avl_root * src_root) {
  115. struct stringfloatmap_node *src_node;
  116. struct stringfloatmap_node *tmp;
  117. avl_for_each_entry_safe(src_node, tmp, src_root, link) {
  118. bmx_map_stringfloatmap_insert(src_node->key, src_node->value, dst_root);
  119. }
  120. }
  121. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++ */