src/hash_map.c

changeset 709
1e8ba59e7911
parent 691
65baf7f45ac8
child 829
7d4e31d295af
     1.1 --- a/src/hash_map.c	Sun May 21 14:56:10 2023 +0200
     1.2 +++ b/src/hash_map.c	Sun May 21 15:07:31 2023 +0200
     1.3 @@ -26,10 +26,12 @@
     1.4   * POSSIBILITY OF SUCH DAMAGE.
     1.5   */
     1.6  
     1.7 -#include <string.h>
     1.8  #include "cx/hash_map.h"
     1.9  #include "cx/utils.h"
    1.10  
    1.11 +#include <string.h>
    1.12 +#include <assert.h>
    1.13 +
    1.14  struct cx_hash_map_element_s {
    1.15      /** A pointer to the next element in the current bucket. */
    1.16      struct cx_hash_map_element_s *next;
    1.17 @@ -334,13 +336,30 @@
    1.18      }
    1.19  }
    1.20  
    1.21 -static CxIterator cx_hash_map_iterator(CxMap const *map) {
    1.22 +static CxIterator cx_hash_map_iterator(
    1.23 +        CxMap const *map,
    1.24 +        enum cx_map_iterator_type type
    1.25 +) {
    1.26      CxIterator iter;
    1.27  
    1.28      iter.src_handle = map;
    1.29      iter.base.valid = cx_hash_map_iter_valid;
    1.30      iter.base.next = cx_hash_map_iter_next;
    1.31 -    iter.base.current = cx_hash_map_iter_current_entry;
    1.32 +
    1.33 +    switch (type) {
    1.34 +        case CX_MAP_ITERATOR_PAIRS:
    1.35 +            iter.base.current = cx_hash_map_iter_current_entry;
    1.36 +            break;
    1.37 +        case CX_MAP_ITERATOR_KEYS:
    1.38 +            iter.base.current = cx_hash_map_iter_current_key;
    1.39 +            break;
    1.40 +        case CX_MAP_ITERATOR_VALUES:
    1.41 +            iter.base.current = cx_hash_map_iter_current_value;
    1.42 +            break;
    1.43 +        default:
    1.44 +            assert(false);
    1.45 +    }
    1.46 +
    1.47      iter.base.flag_removal = cx_hash_map_iter_flag_rm;
    1.48      iter.base.remove = false;
    1.49      iter.base.mutating = false;
    1.50 @@ -370,40 +389,6 @@
    1.51      return iter;
    1.52  }
    1.53  
    1.54 -static CxIterator cx_hash_map_iterator_keys(CxMap const *map) {
    1.55 -    CxIterator iter = cx_hash_map_iterator(map);
    1.56 -    iter.base.current = cx_hash_map_iter_current_key;
    1.57 -    return iter;
    1.58 -}
    1.59 -
    1.60 -static CxIterator cx_hash_map_iterator_values(CxMap const *map) {
    1.61 -    CxIterator iter = cx_hash_map_iterator(map);
    1.62 -    iter.base.current = cx_hash_map_iter_current_value;
    1.63 -    return iter;
    1.64 -}
    1.65 -
    1.66 -static CxMutIterator cx_hash_map_mut_iterator(CxMap *map) {
    1.67 -    CxIterator it = cx_hash_map_iterator(map);
    1.68 -    it.base.mutating = true;
    1.69 -
    1.70 -    // we know the iterators share the same memory layout
    1.71 -    CxMutIterator iter;
    1.72 -    memcpy(&iter, &it, sizeof(CxMutIterator));
    1.73 -    return iter;
    1.74 -}
    1.75 -
    1.76 -static CxMutIterator cx_hash_map_mut_iterator_keys(CxMap *map) {
    1.77 -    CxMutIterator iter = cx_hash_map_mut_iterator(map);
    1.78 -    iter.base.current = cx_hash_map_iter_current_key;
    1.79 -    return iter;
    1.80 -}
    1.81 -
    1.82 -static CxMutIterator cx_hash_map_mut_iterator_values(CxMap *map) {
    1.83 -    CxMutIterator iter = cx_hash_map_mut_iterator(map);
    1.84 -    iter.base.current = cx_hash_map_iter_current_value;
    1.85 -    return iter;
    1.86 -}
    1.87 -
    1.88  static cx_map_class cx_hash_map_class = {
    1.89          cx_hash_map_destructor,
    1.90          cx_hash_map_clear,
    1.91 @@ -411,11 +396,6 @@
    1.92          cx_hash_map_get,
    1.93          cx_hash_map_remove,
    1.94          cx_hash_map_iterator,
    1.95 -        cx_hash_map_iterator_keys,
    1.96 -        cx_hash_map_iterator_values,
    1.97 -        cx_hash_map_mut_iterator,
    1.98 -        cx_hash_map_mut_iterator_keys,
    1.99 -        cx_hash_map_mut_iterator_values,
   1.100  };
   1.101  
   1.102  CxMap *cxHashMapCreate(

mercurial