robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

taorefactor(path): pathcmp() #41035f0a0182285

master
6.7 KiB254 linesraw
1// map.c: Hash maps and sets
2//
3// parts of the implementation derived from khash.h as part of klib (MIT license)
4//
5// NOTE: Callers must manage memory (allocate) for keys and values.
6//       Map and Set does not make its own copy of the key or value.
7
8#include <stdbool.h>
9#include <string.h>
10
11#include "auto/config.h"
12#include "nvim/ascii_defs.h"
13#include "nvim/charset.h"
14#include "nvim/map_defs.h"
15#include "nvim/mbyte.h"
16#include "nvim/memory.h"
17#include "nvim/path.h"
18
19#define equal_simple(x, y) ((x) == (y))
20
21#define hash_uint64_t(key) (uint32_t)((key) >> 33^(key)^(key) << 11)
22#define equal_uint64_t equal_simple
23#define hash_uint32_t(x) (x)
24#define equal_uint32_t equal_simple
25#define hash_int(x) hash_uint32_t((uint32_t)(x))
26#define equal_int equal_simple
27#define hash_int64_t(key) hash_uint64_t((uint64_t)key)
28#define equal_int64_t equal_simple
29
30#ifdef ARCH_64
31# define hash_ptr_t(key) hash_uint64_t((uint64_t)(key))
32# define equal_ptr_t(a, b) equal_uint64_t((uint64_t)(a), (uint64_t)(b))
33#elif defined(ARCH_32)
34# define hash_ptr_t(key) hash_uint32_t((uint32_t)(key))
35# define equal_ptr_t(a, b) equal_uint32_t((uint32_t)(a), (uint32_t)(b))
36#endif
37
38static inline uint32_t hash_cstr_t(const char *s)
39{
40  uint32_t h = 0;
41  for (size_t i = 0; s[i]; i++) {
42    h = (h << 5) - h + (uint8_t)s[i];
43  }
44  return h;
45}
46
47#define equal_cstr_t strequal
48
49/// Hash/equality for path strings. On case-insensitive platforms, case-fold first (via
50/// path_fold_char, the same fold used by path_cmp) so the hash↔equality invariant holds.
51///
52/// We additionally:
53///   - drop a leading drive letter "[A-Za-z]:" on Windows, so that "C:\foo" and "\foo" land in
54///     the same bucket (path_cmp considers them equal when the current drive matches the
55///     explicit drive letter). Two paths with *different* explicit drives ("C:\foo" vs "D:\foo")
56///     may now share a bucket — that's a permitted collision; equal_path_t still rejects them.
57///   - ignore a single trailing path separator.
58static inline uint32_t hash_path_t(const char *p)
59{
60  uint32_t h = 0;
61#ifdef MSWIN
62  if (ASCII_ISALPHA(*p) && p[1] == ':') {
63    p += 2;
64  }
65#endif
66  bool ic = false;
67#ifdef CASE_INSENSITIVE_FILENAME
68  ic = true;
69#endif
70  const char *start = p;
71  for (int len = 0; *p; p += len) {
72    if (vim_ispathsep_nocolon(*p)
73        && p[1] == NUL
74        && start != p
75        && !vim_ispathsep(p[-1])) {
76      break;
77    }
78    int c = path_fold_char(ic, p, &len);
79    h = (h << 5) - h + (uint32_t)c;
80  }
81  return h;
82}
83
84static inline bool equal_path_t(const char *a, const char *b)
85{
86  if (a == b) {
87    return true;
88  }
89  if (a == NULL || b == NULL) {
90    return false;
91  }
92  bool ic = false;
93#ifdef CASE_INSENSITIVE_FILENAME
94  ic = true;
95#endif
96  return path_cmp(ic, a, b, MAXPATHL) == 0;
97}
98
99static inline uint32_t hash_HlEntry(HlEntry ae)
100{
101  const uint8_t *data = (const uint8_t *)&ae;
102  uint32_t h = 0;
103  for (size_t i = 0; i < sizeof(ae); i++) {
104    h = (h << 5) - h + data[i];
105  }
106  return h;
107}
108
109static inline bool equal_HlEntry(HlEntry ae1, HlEntry ae2)
110{
111  return memcmp(&ae1, &ae2, sizeof(ae1)) == 0;
112}
113
114static inline uint32_t hash_ColorKey(ColorKey ae)
115{
116  const uint8_t *data = (const uint8_t *)&ae;
117  uint32_t h = 0;
118  for (size_t i = 0; i < sizeof(ae); i++) {
119    h = (h << 5) - h + data[i];
120  }
121  return h;
122}
123
124static inline bool equal_ColorKey(ColorKey ae1, ColorKey ae2)
125{
126  return memcmp(&ae1, &ae2, sizeof(ae1)) == 0;
127}
128
129// TODO(bfredl): this could be _less_ for the h->hash part as this is now small (4 bytes per value)
130#define UPPER_FILL 0.77
131
132#define roundup32(x) (--(x), (x) |= (x)>>1, (x) |= (x)>>2, (x) |= (x)>>4, (x) |= (x)>>8, \
133                      (x) |= (x)>>16, ++(x))
134
135// h->hash must either be NULL or an already valid pointer
136void mh_realloc(MapHash *h, uint32_t n_min_buckets)
137{
138  xfree(h->hash);
139  uint32_t n_buckets = n_min_buckets < 16 ? 16 : n_min_buckets;
140  roundup32(n_buckets);
141  // sets all buckets to EMPTY
142  h->hash = xcalloc(n_buckets, sizeof *h->hash);
143  h->n_occupied = h->size = 0;
144  h->n_buckets = n_buckets;
145  h->upper_bound = (uint32_t)(h->n_buckets * UPPER_FILL + 0.5);
146}
147
148void mh_clear(MapHash *h)
149{
150  if (h->hash) {
151    memset(h->hash, 0, h->n_buckets * sizeof(*h->hash));
152    h->size = h->n_occupied = 0;
153    h->n_keys = 0;
154  }
155}
156
157#define KEY_NAME(x) x##int
158#include "nvim/map_key_impl.c.h"
159#define VAL_NAME(x) quasiquote(x, ptr_t)
160#include "nvim/map_value_impl.c.h"
161#undef VAL_NAME
162#define VAL_NAME(x) quasiquote(x, String)
163#include "nvim/map_value_impl.c.h"
164#undef VAL_NAME
165#define VAL_NAME(x) quasiquote(x, StcClick)
166#include "nvim/map_value_impl.c.h"
167#undef VAL_NAME
168#define VAL_NAME(x) quasiquote(x, StcClicks)
169#include "nvim/map_value_impl.c.h"
170#undef VAL_NAME
171#undef KEY_NAME
172
173#define KEY_NAME(x) x##ptr_t
174#include "nvim/map_key_impl.c.h"
175#define VAL_NAME(x) quasiquote(x, ptr_t)
176#include "nvim/map_value_impl.c.h"
177#undef VAL_NAME
178#undef KEY_NAME
179
180#define KEY_NAME(x) x##cstr_t
181#include "nvim/map_key_impl.c.h"
182#define VAL_NAME(x) quasiquote(x, ptr_t)
183#include "nvim/map_value_impl.c.h"
184#undef VAL_NAME
185#define VAL_NAME(x) quasiquote(x, int)
186#include "nvim/map_value_impl.c.h"
187#undef VAL_NAME
188#undef KEY_NAME
189
190#define KEY_NAME(x) x##path_t
191#include "nvim/map_key_impl.c.h"
192#undef KEY_NAME
193
194#define KEY_NAME(x) x##String
195#include "nvim/map_key_impl.c.h"
196#define VAL_NAME(x) quasiquote(x, int)
197#include "nvim/map_value_impl.c.h"
198#undef VAL_NAME
199#undef KEY_NAME
200
201#define KEY_NAME(x) x##uint32_t
202#include "nvim/map_key_impl.c.h"
203#define VAL_NAME(x) quasiquote(x, ptr_t)
204#include "nvim/map_value_impl.c.h"
205#undef VAL_NAME
206#define VAL_NAME(x) quasiquote(x, uint32_t)
207#include "nvim/map_value_impl.c.h"
208#undef VAL_NAME
209#undef KEY_NAME
210
211#define KEY_NAME(x) x##uint64_t
212#include "nvim/map_key_impl.c.h"
213#define VAL_NAME(x) quasiquote(x, ptr_t)
214#include "nvim/map_value_impl.c.h"
215#undef VAL_NAME
216#define VAL_NAME(x) quasiquote(x, int)
217#include "nvim/map_value_impl.c.h"
218#undef VAL_NAME
219#define VAL_NAME(x) quasiquote(x, MTDamagePair)
220#include "nvim/map_value_impl.c.h"
221#undef VAL_NAME
222#undef KEY_NAME
223
224#define KEY_NAME(x) x##int64_t
225#include "nvim/map_key_impl.c.h"
226#define VAL_NAME(x) quasiquote(x, ptr_t)
227#include "nvim/map_value_impl.c.h"
228#undef VAL_NAME
229#define VAL_NAME(x) quasiquote(x, int64_t)
230#include "nvim/map_value_impl.c.h"
231#undef VAL_NAME
232#undef KEY_NAME
233
234#define KEY_NAME(x) x##HlEntry
235#include "nvim/map_key_impl.c.h"
236#undef KEY_NAME
237
238#define KEY_NAME(x) x##ColorKey
239#include "nvim/map_key_impl.c.h"
240#define VAL_NAME(x) quasiquote(x, ColorItem)
241#include "nvim/map_value_impl.c.h"
242#undef VAL_NAME
243#undef KEY_NAME
244
245/// Deletes a key:value pair from a string:pointer map, and frees the
246/// storage of both key and value.
247///
248void pmap_del2(PMap(cstr_t) *map, const char *key)
249{
250  cstr_t key_alloc = NULL;
251  ptr_t val = pmap_del(cstr_t)(map, key, &key_alloc);
252  xfree((void *)key_alloc);
253  xfree(val);
254}