aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/map.c
blob: 3e449d2eb16c5971c2a8019c36f9ff25c1646245 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

//
// map.c: khash.h wrapper
//
// NOTE: Callers must manage memory (allocate) for keys and values.
//       khash.h does not make its own copy of the key or value.
//

#include <lauxlib.h>
#include <lua.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "nvim/lib/khash.h"
#include "nvim/map.h"
#include "nvim/map_defs.h"
#include "nvim/memory.h"
#include "nvim/vim.h"

#define cstr_t_hash kh_str_hash_func
#define cstr_t_eq kh_str_hash_equal
#define uint64_t_hash kh_int64_hash_func
#define uint64_t_eq kh_int64_hash_equal
#define uint32_t_hash kh_int_hash_func
#define uint32_t_eq kh_int_hash_equal
#define int_hash kh_int_hash_func
#define int_eq kh_int_hash_equal
#define handle_T_hash kh_int_hash_func
#define handle_T_eq kh_int_hash_equal
#define KittyKey_hash kh_int_hash_func
#define KittyKey_eq kh_int_hash_equal

#if defined(ARCH_64)
# define ptr_t_hash(key) uint64_t_hash((uint64_t)(key))
# define ptr_t_eq(a, b) uint64_t_eq((uint64_t)(a), (uint64_t)(b))
#elif defined(ARCH_32)
# define ptr_t_hash(key) uint32_t_hash((uint32_t)(key))
# define ptr_t_eq(a, b) uint32_t_eq((uint32_t)(a), (uint32_t)(b))
#endif

#define INITIALIZER(T, U) T##_##U##_initializer
#define INITIALIZER_DECLARE(T, U, ...) const U INITIALIZER(T, U) = __VA_ARGS__
#define DEFAULT_INITIALIZER { 0 }
#define SSIZE_INITIALIZER { -1 }

#define MAP_IMPL(T, U, ...) \
  INITIALIZER_DECLARE(T, U, __VA_ARGS__); \
  __KHASH_IMPL(T##_##U##_map, , T, U, 1, T##_hash, T##_eq) \
  void map_##T##_##U##_destroy(Map(T, U) *map) \
  { \
    kh_dealloc(T##_##U##_map, &map->table); \
  } \
  U map_##T##_##U##_get(Map(T, U) *map, T key) \
  { \
    khiter_t k; \
    if ((k = kh_get(T##_##U##_map, &map->table, key)) == kh_end(&map->table)) { \
      return INITIALIZER(T, U); \
    } \
    return kh_val(&map->table, k); \
  } \
  bool map_##T##_##U##_has(Map(T, U) *map, T key) \
  { \
    return kh_get(T##_##U##_map, &map->table, key) != kh_end(&map->table); \
  } \
  T map_##T##_##U##_key(Map(T, U) *map, T key) \
  { \
    khiter_t k; \
    if ((k = kh_get(T##_##U##_map, &map->table, key)) == kh_end(&map->table)) { \
      abort();  /* Caller must check map_has(). */ \
    } \
    return kh_key(&map->table, k); \
  } \
  U map_##T##_##U##_put(Map(T, U) *map, T key, U value) \
  { \
    int ret; \
    U rv = INITIALIZER(T, U); \
    khiter_t k = kh_put(T##_##U##_map, &map->table, key, &ret); \
    if (!ret) { \
      rv = kh_val(&map->table, k); \
    } \
    kh_val(&map->table, k) = value; \
    return rv; \
  } \
  U *map_##T##_##U##_ref(Map(T, U) *map, T key, bool put) \
  { \
    int ret; \
    khiter_t k; \
    if (put) { \
      k = kh_put(T##_##U##_map, &map->table, key, &ret); \
      if (ret) { \
        kh_val(&map->table, k) = INITIALIZER(T, U); \
      } \
    } else { \
      k = kh_get(T##_##U##_map, &map->table, key); \
      if (k == kh_end(&map->table)) { \
        return NULL; \
      } \
    } \
    return &kh_val(&map->table, k); \
  } \
  U map_##T##_##U##_del(Map(T, U) *map, T key) \
  { \
    U rv = INITIALIZER(T, U); \
    khiter_t k; \
    if ((k = kh_get(T##_##U##_map, &map->table, key)) != kh_end(&map->table)) { \
      rv = kh_val(&map->table, k); \
      kh_del(T##_##U##_map, &map->table, k); \
    } \
    return rv; \
  } \
  void map_##T##_##U##_clear(Map(T, U) *map) \
  { \
    kh_clear(T##_##U##_map, &map->table); \
  }

static inline khint_t String_hash(String s)
{
  khint_t h = 0;
  for (size_t i = 0; i < s.size && s.data[i]; i++) {
    h = (h << 5) - h + (uint8_t)s.data[i];
  }
  return h;
}

static inline bool String_eq(String a, String b)
{
  if (a.size != b.size) {
    return false;
  }
  return memcmp(a.data, b.data, a.size) == 0;
}

static inline khint_t HlEntry_hash(HlEntry ae)
{
  const uint8_t *data = (const uint8_t *)&ae;
  khint_t h = 0;
  for (size_t i = 0; i < sizeof(ae); i++) {
    h = (h << 5) - h + data[i];
  }
  return h;
}

static inline bool HlEntry_eq(HlEntry ae1, HlEntry ae2)
{
  return memcmp(&ae1, &ae2, sizeof(ae1)) == 0;
}

static inline khint_t ColorKey_hash(ColorKey ae)
{
  const uint8_t *data = (const uint8_t *)&ae;
  khint_t h = 0;
  for (size_t i = 0; i < sizeof(ae); i++) {
    h = (h << 5) - h + data[i];
  }
  return h;
}

static inline bool ColorKey_eq(ColorKey ae1, ColorKey ae2)
{
  return memcmp(&ae1, &ae2, sizeof(ae1)) == 0;
}

MAP_IMPL(int, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(ptr_t, int, DEFAULT_INITIALIZER)
MAP_IMPL(int, int, DEFAULT_INITIALIZER)
MAP_IMPL(int, cstr_t, DEFAULT_INITIALIZER)
MAP_IMPL(cstr_t, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(cstr_t, int, DEFAULT_INITIALIZER)
MAP_IMPL(ptr_t, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(uint32_t, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(uint64_t, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(uint64_t, ssize_t, SSIZE_INITIALIZER)
MAP_IMPL(uint64_t, uint64_t, DEFAULT_INITIALIZER)
MAP_IMPL(uint32_t, uint32_t, DEFAULT_INITIALIZER)
MAP_IMPL(handle_T, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(HlEntry, int, DEFAULT_INITIALIZER)
MAP_IMPL(String, handle_T, 0)
MAP_IMPL(String, int, DEFAULT_INITIALIZER)
MAP_IMPL(int, String, DEFAULT_INITIALIZER)

MAP_IMPL(ColorKey, ColorItem, COLOR_ITEM_INITIALIZER)

MAP_IMPL(KittyKey, cstr_t, DEFAULT_INITIALIZER)

/// Deletes a key:value pair from a string:pointer map, and frees the
/// storage of both key and value.
///
void pmap_del2(PMap(cstr_t) *map, const char *key)
{
  if (pmap_has(cstr_t)(map, key)) {
    void *k = (void *)pmap_key(cstr_t)(map, key);
    void *v = pmap_get(cstr_t)(map, key);
    pmap_del(cstr_t)(map, key);
    xfree(k);
    xfree(v);
  }
}