summaryrefslogtreecommitdiff
path: root/libnm-util/nm-string-table.c
blob: 5cb056f93441cbf4852784c560b5e3599b0d6571 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */

/*
 * Dan Williams <dcbw@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * (C) Copyright 2014 Red Hat, Inc.
 */

#include "nm-string-table.h"

#include <string.h>

const NMStringTableCompareFunc COMPARE_DEFAULT = &strcmp;

typedef struct NMStringTable {
	guint size;
	NMStringTableCompareFunc compare;

	void *_offset;
} NMStringTable;

#define OFFSET_STRING(table)     ((const char **) ((&((table)->_offset))))
#define OFFSET_DATA(table)       ((void **) &(OFFSET_STRING (table)[(table)->size + 1]))
#define OFFSET_STATICSTR(table)  ((const char *) &(OFFSET_DATA (table)[(table)->size + 1]))

#ifndef G_DISABLE_ASSERT
struct prepare_sort_data
{
	NMStringTableCompareFunc compare;
	gboolean duplicate;
};

static gint
prepare_sort_check (gconstpointer a, gconstpointer b, gpointer user_data)
{
	const NMStringTableTuple *a1 = a;
	const NMStringTableTuple *a2 = b;
	struct prepare_sort_data *data = user_data;
	int c;

	c = (data->compare) (a1->key, a2->key);
	if (c == 0)
		data->duplicate = TRUE;
	return c;
}
#else
static gint
prepare_sort_direct (gconstpointer a, gconstpointer b, gpointer user_data)
{
	const NMStringTableTuple *a1 = a;
	const NMStringTableTuple *a2 = b;

	return ((NMStringTableCompareFunc) user_data) (a1->key, a2->key);
}
#endif

static NMStringTable *
_new (NMStringTableCompareFunc compare, GArray *prepare)
{
	NMStringTable *self;
	guint i;
	const char **tab_str;
	const char *tab_static;
	void **tab_dat;
	size_t len;
	GString *static_strings;
	guint argc = prepare ? prepare->len : 0;

	if (!compare)
		compare = COMPARE_DEFAULT;

	if (argc > G_MAXINT) {
		g_array_free (prepare, TRUE);
		g_return_val_if_reached (NULL);
		return NULL;
	}

	static_strings = g_string_new ("");

	if (prepare) {
#ifndef G_DISABLE_ASSERT
		struct prepare_sort_data sort_data = { .compare = compare };

		g_array_sort_with_data (prepare, prepare_sort_check, &sort_data);
		if (sort_data.duplicate) {
			g_string_free (static_strings, TRUE);
			g_array_free (prepare, TRUE);
			g_return_val_if_reached (NULL);
		}
#else
		g_array_sort_with_data (prepare, prepare_sort_direct, compare);
#endif

		for (i = 0; i < argc; i++) {
			NMStringTableTuple *data = &g_array_index (prepare, NMStringTableTuple, i);
			char *new_key = &static_strings->str[static_strings->len];

			g_string_append (static_strings, data->key);
			g_string_append_c (static_strings, '\0');
			data->key = new_key;
		}
	} else
		g_string_append_c (static_strings, '\0');

	len = offsetof (NMStringTable, _offset) +
	      (argc + 1) * (sizeof (const char *) + sizeof (void *)) +
	      static_strings->len;

	self = g_malloc (len);
	self->size = argc;
	self->compare = compare;

	tab_str = OFFSET_STRING (self);
	tab_dat = OFFSET_DATA (self);
	tab_static = OFFSET_STATICSTR (self);
	memcpy ((char *) tab_static, static_strings->str, static_strings->len);
	for (i = 0; i < argc; i++) {
		NMStringTableTuple *data = &g_array_index (prepare, NMStringTableTuple, i);

		(*tab_str++) = &tab_static[data->key - static_strings->str];
		(*tab_dat++) = data->data;
	}
	*tab_str = NULL;
	*tab_dat = NULL;

	g_string_free (static_strings, TRUE);

	if (prepare)
		g_array_free (prepare, TRUE);

	return self;
}

#ifndef G_DISABLE_ASSERT
static gboolean
_assert_args (GArray *prepare, int argc)
{
	guint i;

	g_return_val_if_fail (prepare, FALSE);
	g_return_val_if_fail (prepare->len == argc, FALSE);

	for (i = 0; i < prepare->len; i++ )
		g_return_val_if_fail (g_array_index (prepare, NMStringTableTuple, i).key, FALSE);
	return TRUE;
}
#define ASSERT_ARGS(prepare, argc) \
	G_STMT_START { \
		if (!_assert_args (prepare, argc)) { \
			g_array_free (prepare, TRUE); \
			return NULL; \
		} \
	} G_STMT_END
#else
#define ASSERT_ARGS(prepare, argc) G_STMT_START { ; } G_STMT_END
#endif

NMStringTable *
nm_string_table_new_keys_only (NMStringTableCompareFunc compare, int argc, const char **args)
{
	guint i;
	GArray *prepare = NULL;

	g_return_val_if_fail ((argc > 0 && args) || argc <= 0, NULL);

	if (argc > 0 || args) {
		NMStringTableTuple data = { 0 };

		prepare = g_array_sized_new (FALSE, FALSE, sizeof (NMStringTableTuple), argc > 0 ? argc : 0);
		if (argc > 0) {
			for (i = 0; i < argc; i++) {
				data.key = args[i];
				g_array_append_val (prepare, data);
			}
			ASSERT_ARGS (prepare, argc);
		} else if (argc < 0) {
			/* argc is negative, that means that args is NULL terminated. */
			for (; *args; args++) {
				data.key = *args;
				g_array_append_val (prepare, data);
			}
		}
	}
	return _new (compare, prepare);
}

NMStringTable *
nm_string_table_new (NMStringTableCompareFunc compare, int argc, const NMStringTableTuple *args)
{
	GArray *prepare = NULL;

	g_return_val_if_fail (!argc || args, NULL);

	if (argc > 0 || args) {
		prepare = g_array_sized_new (FALSE, FALSE, sizeof (NMStringTableTuple), 0);
		if (argc > 0) {
			g_array_append_vals (prepare, args, argc);
			ASSERT_ARGS (prepare, argc);
		} else if (argc < 0) {
			/* argc is negative, that means that args is NULL terminated. */
			for (; args->key; args++)
				g_array_append_val (prepare, *args);
		}
	}
	return _new (compare, prepare);
}


guint
nm_string_table_size (NMStringTable *self)
{
	g_return_val_if_fail (self, 0);

	return self->size;
}

static int
_lookup_by_key (NMStringTable *self, const char *key)
{
	const char *const*t_str;
	guint imin, imax, imid;

	g_return_val_if_fail (self, -1);
	g_return_val_if_fail (key, -1);

	if (self->size == 0)
		return -1;

	t_str = OFFSET_STRING (self);
	imin = 0;
	imax = self->size - 1;

	if (key >= t_str[0] && key <= t_str[imax]) {
		/* Try binary search using the pointer value. We try this
		 * optimization, because the user might lookup using one of the
		 * key values themselves.
		 */
		while (imax >= imin) {
			/* save against overflow, because size is <= INT_MAX */
			imid = (imax + imin) / 2;
			if (t_str[imid] == key)
				return imid;
			if (t_str[imid] < key)
				imin = imid + 1;
			else
				imax = imid - 1;
		}

		/* if we did not find by pointer comparison, try again using
		 * strcmp. The user has provided a substring to one of the keys...
		 */
		imin = 0;
		imax = self->size - 1;
	}
	/* binary search using the compare function. */
	while (imax >= imin) {
		int c;

		/* save against overflow, because size is <= INT_MAX */
		imid = (imax + imin) / 2;
		c = self->compare (key, t_str[imid]);

		if (!c)
			return imid;
		if (c > 0)
			imin = imid + 1;
		else
			imax = imid - 1;
	}

	return -1;
}

static gboolean
_access_at_index (NMStringTable *self, int idx, const char **out_key, gpointer **out_data)
{
	if (idx < 0) {
		if (out_key)
			*out_key = NULL;
		if (out_data)
			out_data = NULL;
		return FALSE;
	}
	if (out_key)
		*out_key = OFFSET_STRING (self)[idx];
	if (out_data)
		*out_data = &OFFSET_DATA (self)[idx];
	return TRUE;
}

gboolean
nm_string_table_lookup_by_key (NMStringTable *self, const char *key, int *out_idx, const char **out_key, gpointer **out_data)
{
	int idx = _lookup_by_key (self, key);

	if (out_idx)
		*out_idx = idx;
	return _access_at_index (self, idx, out_key, out_data);
}

gboolean
nm_string_table_lookup_by_index (NMStringTable *self, int idx, const char **out_key, gpointer **out_data)
{
	g_return_val_if_fail (self, FALSE);

	if (idx >= self->size) {
		/* don't check for idx<0, _access_at_index will handle that. */
		idx = -1;
	}
	return _access_at_index (self, idx, out_key, out_data);
}

gpointer
nm_string_table_get_data_by_key (NMStringTable *self, const char *key)
{
	int idx = _lookup_by_key (self, key);

	return idx >= 0 ? OFFSET_DATA (self)[idx] : NULL;
}

const char *const*
nm_string_table_get_keys (NMStringTable *self)
{
	g_return_val_if_fail (self, NULL);

	return OFFSET_STRING (self);
}

gpointer *
nm_string_table_get_data (NMStringTable *self)
{
	g_return_val_if_fail (self, NULL);

	return OFFSET_DATA (self);
}

NMStringTableCompareFunc
nm_string_table_get_compare_func (NMStringTable *self)
{
	g_return_val_if_fail (self, NULL);

	return self->compare;
}

void
nm_string_table_foreach (NMStringTable *self, NMStringTableForeachFunc func, void *user_data)
{
	const char **tab_str;
	void **tab_dat;
	guint i, size;

	g_return_if_fail (self);
	g_return_if_fail (func);

	tab_str = OFFSET_STRING (self);
	tab_dat = OFFSET_DATA (self);
	for (i = 0, size = self->size; i < size; i++) {
		if (!func (tab_str[i], &tab_dat[i], i, user_data))
			return;
	}
}