aboutsummaryrefslogtreecommitdiff
path: root/bitmapped_patricia_tree.c
blob: 765b8176f35da58b30c4b2a9af2844376432b3f4 (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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// -*- mode: c; coding: utf-8 -*- */
//
// Copyright 2010, 2011, Matthias Andreas Benkard.
//
//-----------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//-----------------------------------------------------------------------------
//

// An implementation of a bitmapped Patricia tree.

//// Purpose ////
//
// The idea is to use a locally mutable, bitmapped Patricia tree as a
// variable binding store (i.e. environment) in compiled code.  In this
// way, there is no need for excessive copying when an independent
// environment must be set up (such as when initiating the processing of
// a new node in the search space).  Instead, significant amounts of
// structure can be shared between child and parent environments.

//// Motivation ////
//
// 1. Patricia trees are very amenable to structure sharing.
//
// 2. Furthermore, big-endian Patricia trees are especially efficient
//    when indices are allocated sequentially, as is the case for
//    variables in code emitted by our compiler.
//
// 3. Finally, bitmapping improves the performance of copying because
//    copying an array is much cheaper than copying an equivalent branch
//    in a tree.  As we need to shallow-copy the tree at potentially
//    each choice point, copying needs to be fast.


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

#include "bitmapped_patricia_tree.h"

#ifndef BPT_EXPLICIT_CONFIGURATION
#define CHUNK_LENGTH 5
#define KEY_LENGTH 32
#define OFFSET_MASK 0x1ffff  //((1 << chunk_length) - 1)
#define MAX_CHUNKS 7         //key_length / chunk_length + ((key_length % chunk_length == 0) ? 0 : 1)
#define LAST_CHUNK_LENGTH 2  //key_length - ((max_chunks - 1) * chunk_length)
#endif  //!BPT_EXPLICIT_CONFIGURATION

typedef struct bpt_nonempty *bpt_nonempty_t;
typedef struct bpt_node *bpt_node_t;
typedef struct bpt_leaf *bpt_leaf_t;

struct bpt {
  enum bpt_tag tag;
  int refcount;
  bool mutable;
  bpt_key_t prefix;
};

struct bpt_leaf {
  struct bpt bpt;  // poor man's inheritance
  void *value;
#ifdef BPT_ENABLE_DEALLOC_HOOKS
  void (*dealloc_hook)(bpt_key_t, void *);   // not actually used anywhere in client code
#endif
};

struct bpt_node {
  struct bpt bpt;  // poor man's inheritance
  unsigned int branching_chunk;
  bpt_key_bitmask_t bitmask;
  bpt_t *children;
};


void init_bpt_leaf(bpt_t a_leaf, bpt_key_t key, void *value) {
  bpt_leaf_t leaf = (bpt_leaf_t)a_leaf;
  leaf->bpt.tag = BPT_LEAF;
  leaf->bpt.mutable = true;
  leaf->bpt.prefix = key;
  leaf->value = value;
#ifdef BPT_ENABLE_DEALLOC_HOOKS
  leaf->dealloc_hook = NULL;
#endif
  leaf->bpt.refcount = 1;
}

void init_bpt_node(bpt_node_t node, bpt_key_t prefix, unsigned int branching_chunk) {
  node->bpt.tag = BPT_INNER_NODE;
  node->bpt.mutable = true;
  node->bpt.prefix = prefix;
  node->branching_chunk = branching_chunk;
  node->bitmask = 0;
  node->children = NULL;
  node->bpt.refcount = 1;
}


bpt_t bpt_make_leaf(bpt_key_t key, void *value) {
  bpt_leaf_t leaf = malloc(sizeof *leaf);
  init_bpt_leaf((bpt_t)leaf, key, value);
  return (bpt_t)leaf;
}

bpt_node_t bpt_make_node(bpt_key_t prefix, unsigned int branching_chunk) {
  bpt_node_t node = malloc(sizeof *node);
  init_bpt_node(node, prefix, branching_chunk);
  return node;
}


static inline unsigned int bpt_number_of_leading_zeros(bpt_key_t x);
static inline unsigned int bpt_number_of_trailing_zeros(bpt_key_t x);
static inline unsigned int bpt_popcount(bpt_key_bitmask_t key);
static unsigned int bpt_compute_child_index(bpt_key_bitmask_t bitmask, unsigned int child_number);
static inline uint_fast8_t bpt_offset_of_key(bpt_key_t key, unsigned int branching_chunk);
static bpt_key_t bpt_prefix_of_key(bpt_key_t key, unsigned int branching_chunk);
static inline unsigned int bpt_branching_chunk(bpt_t bpt);
static unsigned int bpt_find_diverging_chunk(bpt_key_t key1, bpt_key_t key2);
static void bpt_for_children(bpt_t bpt, void (*thunk)(bpt_t));


static void bpt_for_children(bpt_t bpt, void (*thunk)(bpt_t)) {
  if (bpt && bpt->tag == BPT_INNER_NODE) {
    bpt_node_t b = (bpt_node_t)bpt;
    bpt_t *iter = b->children;
    bpt_t *children_end = b->children + bpt_popcount(b->bitmask);
    while (iter < children_end) {
      thunk(*iter);
      iter++;
    }
  }
}

void *bpt_get(bpt_t bpt, bpt_key_t key) {
  void **pointer = bpt_get_pointer(bpt, key);
  if (pointer) {
    return *pointer;
  } else {
    return NULL;
  }
}

bpt_leaf_t bpt_get_leaf(bpt_t bpt, bpt_key_t key)
{
  if (!bpt) {
    return NULL;
  } else if (bpt->tag == BPT_LEAF) {
    bpt_leaf_t b = (bpt_leaf_t)bpt;
    if (bpt->prefix == key) {
      return b;
    } else {
      return NULL;
    }
  } else {
    bpt_node_t b = (bpt_node_t)bpt;
    int child_number = bpt_offset_of_key(key, b->branching_chunk);
    if ((1 << child_number) & b->bitmask) {
      int child_index = bpt_compute_child_index(b->bitmask, child_number);
      return bpt_get_leaf(b->children[child_index], key);
    } else {
      return NULL;
    }
  }
}

void **bpt_get_pointer(bpt_t bpt, bpt_key_t key)
{
  bpt_leaf_t leaf = bpt_get_leaf(bpt, key);
  if (!leaf) {
    return NULL;
  } else {
    return &leaf->value;
  }
}

bool bpt_has_key(bpt_t bpt, bpt_key_t key) {
  return (bpt_get_leaf(bpt, key) != NULL);
}

bpt_t bpt_assoc(bpt_t bpt, bpt_key_t key, void *value) {
  if (!bpt) {
    return (bpt_t)bpt_make_leaf(key, value);
  } else {
    bpt_key_t prefix = bpt->prefix;
    if (bpt_prefix_of_key(key, bpt_branching_chunk(bpt)) != prefix) {
      unsigned int diverging_chunk = bpt_find_diverging_chunk(key, prefix);
      bpt_key_t my_number_in_parent = bpt_offset_of_key(prefix, diverging_chunk);
      bpt_key_t their_number_in_parent = bpt_offset_of_key(key, diverging_chunk);
      bpt_node_t new_node = bpt_make_node(bpt_prefix_of_key(prefix, diverging_chunk), diverging_chunk);
      new_node->bitmask = (1 << my_number_in_parent) | (1 << their_number_in_parent);
      new_node->children = malloc(sizeof (*new_node->children) * 2);
      if (my_number_in_parent < their_number_in_parent) {
        new_node->children[0] = bpt;
        new_node->children[1] = bpt_make_leaf(key, value);
      } else {
        new_node->children[0] = bpt_make_leaf(key, value);
        new_node->children[1] = bpt;
      }
      bpt_retain(bpt);
      return (bpt_t)new_node;
    } else {
      if (bpt->tag == BPT_LEAF) {
        bpt_leaf_t b = (bpt_leaf_t)bpt;
        if (bpt->mutable) {
          b->value = value;
          bpt_retain(bpt);
          return bpt;
        } else {
          return (bpt_t)bpt_make_leaf(key, value);
        }
      } else {
        bpt_node_t b = (bpt_node_t)bpt;
        uint_fast8_t child_number = bpt_offset_of_key(key, b->branching_chunk);
        unsigned int child_index = bpt_compute_child_index(b->bitmask, child_number);
        if ((1 << child_number) & b->bitmask) {
          // We already have a child to pass the value to.  Do that.
          bpt_t child = b->children[child_index];
          bpt_t new_child = bpt_assoc(child, key, value);
          if (new_child == child) {
            bpt_release(child);
            bpt_retain(bpt);
            return bpt;
          } else {
            if (bpt->mutable) {
              bpt_release(child);
              b->children[child_index] = new_child;
              bpt_retain(bpt);
              return bpt;
            } else {
              bpt_node_t new_node = malloc(sizeof *new_node);
              *new_node = *b;
              new_node->bpt.refcount = 1;
              new_node->bpt.mutable = true;
              unsigned int number_of_children = bpt_popcount(b->bitmask);
              size_t size_of_child_array = sizeof (*new_node->children) * number_of_children;
              new_node->children = malloc(size_of_child_array);
              memcpy(new_node->children, b->children, size_of_child_array);
              new_node->children[child_index] = new_child;
              // Retain the children copied into the new node.
              bpt_for_children(bpt, bpt_retain);
              return (bpt_t)new_node;
            }
          }
        } else {
          // Create a new child.
          unsigned int number_of_children = bpt_popcount(b->bitmask);
          size_t new_size_of_child_array = sizeof (*b->children) * (number_of_children + 1);
          if (bpt->mutable) {
            b->children = realloc(b->children, new_size_of_child_array);
            memmove(b->children + child_index + 1, b->children + child_index, sizeof (*b->children) * (number_of_children - child_index));
            b->children[child_index] = bpt_make_leaf(key, value);
            b->bitmask |= 1 << child_number;
            bpt_retain(bpt);
            return bpt;
          } else {
            bpt_t *new_children = malloc(new_size_of_child_array);
            memcpy(new_children, b->children, sizeof (*b->children) * child_index);
            memcpy(new_children + child_index + 1,
                   b->children + child_index,
                   sizeof (*b->children) * (number_of_children - child_index));
            new_children[child_index] = bpt_make_leaf(key, value);
            bpt_node_t new_node = bpt_make_node(b->bpt.prefix, b->branching_chunk);
            new_node->children = new_children;
            new_node->bitmask = b->bitmask | (1 << child_number);
            // Retain the children copied into the new node.
            bpt_for_children(bpt, bpt_retain);
            return (bpt_t)new_node;
          }
        }
      }
    }
  }
}


bpt_t bpt_dissoc(bpt_t bpt, bpt_key_t key) {
  if (!bpt || (bpt_prefix_of_key(key, bpt_branching_chunk(bpt)) != bpt->prefix)) {
    bpt_retain(bpt);
    return bpt;
  } else if (bpt->tag == BPT_LEAF) {
    // Key matches.
    return NULL;
  } else {
    // Prefix matches.
    bpt_node_t b = (bpt_node_t)bpt;
    uint_fast8_t child_number = bpt_offset_of_key(key, b->branching_chunk);
    if ((1 << child_number) & b->bitmask) {
      unsigned int child_index = bpt_compute_child_index(b->bitmask, child_number);
      bpt_t child = b->children[child_index];
      bpt_t new_child = bpt_dissoc(child, key);
      if (new_child == child) {
        bpt_release(child);
        bpt_retain(bpt);
        return bpt;
      } else {
        unsigned int number_of_children = bpt_popcount(b->bitmask);
        if (!new_child && number_of_children == 2) {
          // When there is only a single child left, we replace ourselves
          // with that child.
          bpt_t remaining_child = b->children[1-child_index];
          bpt_retain(remaining_child);
          return remaining_child;
        } else if (bpt->mutable) {
          bpt_release(child);
          if (!new_child) {
            // We don't reallocate the array because it wouldn't really
            // gain us anything (except maybe non-confusion of a
            // conservative GC).
            memmove(b->children + child_index, b->children + child_index + 1, sizeof(*b->children) * (number_of_children - child_index - 1));
            b->bitmask &= ~(1 << child_number);
            bpt_retain(bpt);
            return bpt;
          } else {
            b->children[child_index] = new_child;
            bpt_retain(bpt);
            return bpt;
          }
        } else {
          // If all else fails, allocate a new node.
          bpt_t *new_children;
          bpt_key_bitmask_t bitmask;
          if (!new_child) {
            new_children = malloc((sizeof *new_children) * (number_of_children - 1));
            memcpy(new_children, b->children, sizeof (*b->children) * child_index);
            memcpy(new_children + child_index,
                   b->children + child_index + 1,
                   sizeof (*b->children) * (number_of_children - child_index - 1));
            bitmask = b->bitmask & ~(1 << child_number);
          } else {
            new_children = malloc((sizeof *new_children) * number_of_children);
            memcpy(new_children, b->children, sizeof (*b->children) * number_of_children);
            new_children[child_index] = new_child;
            bitmask = b->bitmask;
          }
          bpt_node_t new_node = bpt_make_node(b->bpt.prefix, b->branching_chunk);
          new_node->children = new_children;
          new_node->bitmask = bitmask;
          // Retain the children copied into the new node.
          bpt_for_children((bpt_t)new_node, bpt_retain);
          bpt_release(new_child);
          return (bpt_t)new_node;
        }
      }
    } else {
      bpt_retain(bpt);
      return bpt;
    }
  }
}


void bpt_seal(bpt_t bpt) {
  if (bpt) {
    if (bpt->mutable) {
      bpt->mutable = false;
      if (bpt->tag == BPT_INNER_NODE) {
        bpt_for_children(bpt, bpt_seal);
      }
    }
  }
}


/////////////// Helper functions ///////////////
static unsigned int bpt_compute_child_index(bpt_key_bitmask_t bitmask, unsigned int child_number) {
  // Compute the sparse array index given a flat array index.
  return bpt_popcount(bitmask & ((1 << child_number) - 1));
}

static inline uint_fast8_t bpt_offset_of_key(bpt_key_t key, unsigned int chunk_number) {
  // Little-enidan:
  //return (key >> (chunk_number * CHUNK_LENGTH)) & OFFSET_MASK;
  // Big-endian:
  int shift = 0;
  if (chunk_number <= MAX_CHUNKS - 2) {
    shift += LAST_CHUNK_LENGTH;
  }
  if (chunk_number <= MAX_CHUNKS - 3) {
    shift += ((MAX_CHUNKS - 2 - chunk_number) * CHUNK_LENGTH);
  }
  return (key >> shift) & (chunk_number == MAX_CHUNKS - 1 ? ((1 << LAST_CHUNK_LENGTH) - 1) : OFFSET_MASK);
}

static bpt_key_t bpt_prefix_of_key(bpt_key_t key, unsigned int chunk_number) {
  if (chunk_number == MAX_CHUNKS) {
    return key;
  } else {
    // Little-endian:
    //return key & ((1 << (chunk_number * CHUNK_LENGTH)) - 1)
    // Big-endian:
    return key & (((1 << (chunk_number * CHUNK_LENGTH)) - 1) << (KEY_LENGTH - (chunk_number * CHUNK_LENGTH)));
  }
}

static inline unsigned int bpt_branching_chunk(bpt_t bpt) {
  assert(bpt);
  if (bpt->tag == BPT_LEAF) {
    return MAX_CHUNKS;
  } else {
    return ((bpt_node_t)bpt)->branching_chunk;
  }
}

static inline unsigned int bpt_popcount(bpt_key_bitmask_t x) {
  return __builtin_popcountll(x);
}

static inline unsigned int bpt_number_of_leading_zeros(bpt_key_t x) {
  return __builtin_clzll(x);
}

static inline unsigned int bpt_number_of_trailing_zeros(bpt_key_t x) {
  return __builtin_ctzll(x);
}

static unsigned int bpt_find_diverging_chunk(bpt_key_t a, bpt_key_t b) {
  // Little-endian:
  //return bpt_number_of_trailing_zeros(a ^ b) / CHUNK_LENGTH;
  // Big-endian:
  return bpt_number_of_leading_zeros(a ^ b) / CHUNK_LENGTH;
}

void bpt_retain(bpt_t bpt) {
  if (bpt) {
    __sync_fetch_and_add(&bpt->refcount, 1);
  }
}

void bpt_release(bpt_t bpt) {
  if (bpt) {
    if (__sync_sub_and_fetch(&bpt->refcount, 1) == 0) {
      bpt_dealloc(bpt);
    }
  }
}

void bpt_dealloc(bpt_t bpt) {
  if (bpt) {
    if (bpt->tag == BPT_LEAF) {
      bpt_leaf_t b = (bpt_leaf_t)bpt;
#ifdef BPT_ENABLE_DEALLOC_HOOKS
      if (b->dealloc_hook) {
        b->dealloc_hook(b->bpt.prefix, b->value);
      }
#endif
      free(b);
    } else {
      bpt_node_t b = (bpt_node_t)bpt;
      bpt_for_children(bpt, bpt_release);
      free(b->children);
      free(b);
    }
  }
}

#ifdef BPT_ENABLE_DEALLOC_HOOKS
void bpt_leaf_set_dealloc_hook(bpt_leaf_t bpt, void (*hook)(bpt_key_t, void*)) {
  if (bpt) {
    bpt->dealloc_hook = hook;
  }
}

void bpt_set_dealloc_hook(bpt_t bpt, bpt_key_t key, void (*hook)(bpt_key_t, void*)) {
  bpt_leaf_set_dealloc_hook(bpt_get_leaf(bpt, key), hook);
}
#endif