File: mjl_patricia.c

package info (click to toggle)
scamper 20161113-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,928 kB
  • ctags: 8,226
  • sloc: ansic: 83,690; sh: 4,102; makefile: 376; perl: 171
file content (403 lines) | stat: -rw-r--r-- 9,158 bytes parent folder | download | duplicates (2)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * mjl_patricia
 *
 * Adapted from the patricia trie in "Robert Sedgewick's Algorithms in C++"
 * and from the Java implementation by Josh Hentosh and Robert Sedgewick.
 *
 * Copyright (C) 2016 Matthew Luckie. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY Matthew Luckie ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL Matthew Luckie BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: mjl_patricia.c,v 1.3 2016/08/26 10:11:58 mjl Exp $
 *
 */

#ifndef lint
static const char rcsid[] =
  "$Id: mjl_patricia.c,v 1.3 2016/08/26 10:11:58 mjl Exp $";
#endif

#include <stdlib.h>
#include <assert.h>

#if defined(DMALLOC)
#include <dmalloc.h>
#endif

#include "mjl_patricia.h"

struct patricia
{
  patricia_node_t *head;
  patricia_bit_t   bit;
  patricia_cmp_t   cmp;
  patricia_fbd_t   fbd;
  int              count;
};

struct patricia_node
{
  void            *item;
  int              bit;
  patricia_node_t *left;
  patricia_node_t *right;
};

void *patricia_find(const patricia_t *trie, const void *item)
{
  patricia_node_t *p, *x = trie->head;

  do
    {
      p = x;
      if(x->bit != 0 && trie->bit(item, x->bit) == 0)
	x = x->left;
      else
	x = x->right;
    }
  while(p->bit < x->bit);

  if(x->item != NULL && trie->cmp(item, x->item) == 0)
    return x->item;
  return NULL;
}

#ifndef DMALLOC
patricia_node_t *patricia_insert(patricia_t *trie, void *item)
#else
patricia_node_t *patricia_insert_dm(patricia_t *trie, void *item,
				    const char *file, const int line)
#endif
{
  patricia_node_t *t, *p, *x = trie->head;
  size_t len;
  int b;

  do
    {
      p = x;
      if(x->bit != 0 && trie->bit(item, x->bit) == 0)
	x = x->left;
      else
	x = x->right;
    }
  while(p->bit < x->bit);

  b = 0;
  if(x->item != NULL)
    {
      /* cannot insert the item if it is already in the trie */
      if(trie->cmp(item, x->item) == 0)
	return NULL;

      /*
       * find the left most bit position where the two items differ
       *
       * note: trie->fbd should return the same as the following
       * computation.
       *
       * while(trie->bit(x->item, b) == trie->bit(item, b))
       *   b++;
       *
       */
      b = trie->fbd(x->item, item);
    }

  /* travel down the trie to that point */
  x = trie->head;
  do
    {
      p = x;
      if(x->bit != 0 && trie->bit(item, x->bit) == 0)
	x = x->left;
      else
	x = x->right;
    }
  while(p->bit < x->bit && x->bit < b);

  /* insert a new node at this point */
  len = sizeof(patricia_node_t);
#ifndef DMALLOC
  t = malloc(len);
#else
  t = dmalloc_malloc(file, line, len, DMALLOC_FUNC_MALLOC, 0, 0);
#endif
  if(t == NULL)
    return NULL;
  t->item = item;
  t->bit = b;
  if(t->bit != 0 && trie->bit(item, t->bit) == 0)
    {
      t->left = t;
      t->right = x;
    }
  else
    {
      t->left = x;
      t->right = t;
    }

  if(p->bit != 0 && trie->bit(item, p->bit) == 0)
    p->left = t;
  else
    p->right = t;

  trie->count++;
  return t;
}

static int patricia_gpx_node(patricia_t *trie, patricia_node_t *node,
			     patricia_node_t **g, patricia_node_t **p,
			     patricia_node_t **x)
{
  *p = trie->head;
  *x = trie->head;

  do
    {
      *g = *p; *p = *x;
      if((*x)->bit != 0 && trie->bit(node->item, (*x)->bit) == 0)
	*x = (*x)->left;
      else
	*x = (*x)->right;
    }
  while((*p)->bit < (*x)->bit);

  if(node != *x)
    return 0;
  return 1;
}

static int patricia_gpx_item(patricia_t *trie, const void *item,
			     patricia_node_t **g, patricia_node_t **p,
			     patricia_node_t **x)
{
  *p = trie->head;
  *x = trie->head;

  do
    {
      *g = *p; *p = *x;
      if((*x)->bit != 0 && trie->bit(item, (*x)->bit) == 0)
	*x = (*x)->left;
      else
	*x = (*x)->right;
    }
  while((*p)->bit < (*x)->bit);

  if((*x)->item == NULL || trie->cmp((*x)->item, item) != 0)
    return 0;
  return 1;
}

static void patricia_gpx_remove(patricia_t *trie,
				patricia_node_t *g, patricia_node_t *p,
				patricia_node_t *x)
{
  patricia_node_t *z, *y, *c;

  y = trie->head;
  do
    {
      z = y;
      if(y->bit != 0 && trie->bit(x->item, y->bit) == 0)
	y = y->left;
      else
	y = y->right;
    }
  while(y != x);

  if(x == p)
    {
      if(x->bit != 0 && trie->bit(x->item, x->bit) == 0)
	c = x->right;
      else
	c = x->left;
      if(z->bit != 0 && trie->bit(x->item, z->bit) == 0)
	z->left = c;
      else
	z->right = c;
    }
  else
    {
      if(p->bit != 0 && trie->bit(x->item, p->bit) == 0)
	c = p->right;
      else
	c = p->left;
      if(g->bit != 0 && trie->bit(x->item, g->bit) == 0)
	g->left = c;
      else
	g->right = c;
      if(z->bit != 0 && trie->bit(x->item, z->bit) == 0)
	z->left = p;
      else
	z->right = p;
      p->left = x->left;
      p->right = x->right;
      p->bit = x->bit;
    }

  /* when the trie is empty, reset it to how it was at initialisation */
  if(--trie->count == 0)
    {
      trie->head->left = trie->head;
      trie->head->right = trie->head;
      trie->head->bit = 0;
      trie->head->item = NULL;
    }

  /* don't need the node structure anymore, so free it */
  free(x);
  if(trie->head->left == x)
    trie->head->left = trie->head;
  if(trie->head->right == x)
    trie->head->right = trie->head;

  return;
}

int patricia_remove_node(patricia_t *trie, patricia_node_t *node)
{
  patricia_node_t *g, *p, *x;
  if(patricia_gpx_node(trie, node, &g, &p, &x) == 0)
    return -1;
  patricia_gpx_remove(trie, g, p, x);
  return 0;
}

int patricia_remove_item(patricia_t *trie, const void *item)
{
  patricia_node_t *g, *p, *x;
  if(patricia_gpx_item(trie, item, &g, &p, &x) == 0)
    return -1;
  patricia_gpx_remove(trie, g, p, x);
  return 0;
}

int patricia_count(const patricia_t *trie)
{
  if(trie == NULL)
    return -1;
  return trie->count;
}

#ifndef DMALLOC
patricia_t *patricia_alloc(patricia_bit_t bit, patricia_cmp_t cmp,
			   patricia_fbd_t fbd)
#else
patricia_t *patricia_alloc_dm(patricia_bit_t bit, patricia_cmp_t cmp,
			      patricia_fbd_t fbd,
			      const char *file, const int line)
#endif
{
  patricia_t *trie = NULL;
  size_t len;

  /*
   * allocate the patricia trie data structure
   */
  len = sizeof(patricia_t);
#ifndef DMALLOC
  trie = malloc(len);
#else
  trie = dmalloc_malloc(file, line, len, DMALLOC_FUNC_MALLOC, 0, 0);
#endif
  if(trie == NULL)
    goto err;
  trie->bit = bit;
  trie->cmp = cmp;
  trie->fbd = fbd;
  trie->count = 0;

  /*
   * allocate an initial head node for the patricia trie
   */
  len = sizeof(patricia_node_t);
#ifndef DMALLOC
  trie->head = malloc(len);
#else
  trie->head = dmalloc_malloc(file, line, len, DMALLOC_FUNC_MALLOC, 0, 0);
#endif
  if(trie->head == NULL)
    goto err;
  trie->head->left = trie->head;
  trie->head->right = trie->head;
  trie->head->bit = 0;
  trie->head->item = NULL;

  return trie;

 err:
  if(trie != NULL) patricia_free(trie);
  return NULL;
}

/*
 * patricia_free_rec
 *
 * implementation by trial and error
 */
static void patricia_free_rec(patricia_t *trie, patricia_node_t *node,
			      patricia_free_t free_cb)
{
  patricia_node_t *tmp;
  if(node != node->left && node->left != NULL &&
     (node->bit <= node->left->bit || node->left->bit == 0))
    {
      tmp = node->left; node->left = NULL;
      patricia_free_rec(trie, tmp, free_cb);
    }
  if(node != node->right && node->right != NULL &&
     (node->bit <= node->right->bit || node->right->bit == 0))
    {
      tmp = node->right; node->right = NULL;
      patricia_free_rec(trie, tmp, free_cb);
    }
  if(node != trie->head)
    {
      if(free_cb != NULL)
	free_cb(node->item);
      free(node);
    }
  return;
}

void patricia_free_cb(patricia_t *trie, patricia_free_t free_cb)
{
  if(trie == NULL)
    return;
  if(trie->head != NULL)
    {
      patricia_free_rec(trie, trie->head->left, free_cb);
      if(trie->head->right != trie->head->left && trie->head->right != NULL)
	patricia_free_rec(trie, trie->head->right, free_cb);
      free(trie->head);
    }
  free(trie);
  return;
}

void patricia_free(patricia_t *trie)
{
  patricia_free_cb(trie, NULL);
  return;
}