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
| | /* string-map.c - associative arrays of strings
*
*
* Copyright © 2016 David Bremner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
*
* Author: David Bremner <david@tethera.net>
*/
#include "notmuch-private.h"
/* Create a new notmuch_string_map_t object, with 'ctx' as its
* talloc owner.
*
* This function can return NULL in case of out-of-memory.
*/
typedef struct _notmuch_string_pair_t {
char *key;
char *value;
} notmuch_string_pair_t;
struct _notmuch_string_map {
notmuch_bool_t sorted;
size_t length;
notmuch_string_pair_t *pairs;
};
notmuch_string_map_t *
_notmuch_string_map_create (const void *ctx)
{
notmuch_string_map_t *map;
map = talloc (ctx, notmuch_string_map_t);
if (unlikely (map == NULL))
return NULL;
map->length = 0;
map->pairs = NULL;
map->sorted = TRUE;
return map;
}
void
_notmuch_string_map_append (notmuch_string_map_t *map,
const char *key,
const char *value)
{
map->length++;
map->sorted = FALSE;
if (map->pairs)
map->pairs = talloc_realloc (map, map->pairs, notmuch_string_pair_t, map->length + 1);
else
map->pairs = talloc_array (map, notmuch_string_pair_t, map->length + 1);
map->pairs[map->length - 1].key = talloc_strdup (map, key);
map->pairs[map->length - 1].value = talloc_strdup (map, value);
/* Add sentinel */
map->pairs[map->length].key = NULL;
map->pairs[map->length].value = NULL;
}
static int
cmppair (const void *pa, const void *pb)
{
notmuch_string_pair_t *a = (notmuch_string_pair_t *) pa;
notmuch_string_pair_t *b = (notmuch_string_pair_t *) pb;
return strcmp (a->key, b->key);
}
static void
_notmuch_string_map_sort (notmuch_string_map_t *map)
{
if (map->length == 0)
return;
if (map->sorted)
return;
qsort (map->pairs, map->length, sizeof (notmuch_string_pair_t), cmppair);
map->sorted = TRUE;
}
static notmuch_bool_t
string_cmp (const char *a, const char *b, notmuch_bool_t exact)
{
if (exact)
return (strcmp (a, b));
else
return (strncmp (a, b, strlen (a)));
}
static notmuch_string_pair_t *
bsearch_first (notmuch_string_pair_t *array, size_t len, const char *key, notmuch_bool_t exact)
{
size_t first = 0;
size_t last = len - 1;
size_t mid;
if (len <= 0)
return NULL;
while (last > first) {
mid = (first + last) / 2;
int sign = string_cmp (key, array[mid].key, exact);
if (sign <= 0)
last = mid;
else
first = mid + 1;
}
if (string_cmp (key, array[first].key, exact) == 0)
return array + first;
else
return NULL;
}
const char *
_notmuch_string_map_get (notmuch_string_map_t *map, const char *key)
{
notmuch_string_pair_t *pair;
/* this means that calling append invalidates iterators */
_notmuch_string_map_sort (map);
pair = bsearch_first (map->pairs, map->length, key, TRUE);
if (! pair)
return NULL;
return pair->value;
}
|