summaryrefslogtreecommitdiffstats
path: root/libdimension/dictionary.c
blob: ea6bbdc88f6e73ada7703363611108207349d92c (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
/*************************************************************************
 * Copyright (C) 2010-2011 Tavian Barnes <tavianator@tavianator.com>     *
 *                                                                       *
 * This file is part of The Dimension Library.                           *
 *                                                                       *
 * The Dimension 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 3 of the *
 * License, or (at your option) any later version.                       *
 *                                                                       *
 * The Dimension 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 program.  If not, see                         *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

/**
 * @file
 * Associative arrays, implemented with PATRICIA tries.
 */

#include "dimension.h"

struct dmnsn_dictionary {
  size_t obj_size;       /**< The size of the objects in the trie. */
  char *prefix;          /**< The local string prefix of the current node. */
  void *value;           /**< The node's stored object, if it's a leaf. */
  dmnsn_array *children; /**< The node's children. */
};

dmnsn_dictionary *
dmnsn_new_dictionary(size_t obj_size)
{
  dmnsn_dictionary *dict = dmnsn_malloc(sizeof(dmnsn_dictionary));
  dict->obj_size = obj_size;
  dict->prefix   = dmnsn_strdup("");
  dict->value    = NULL;
  dict->children = dmnsn_new_array(sizeof(dmnsn_dictionary *));
  return dict;
}

void
dmnsn_delete_dictionary(dmnsn_dictionary *dict)
{
  if (dict) {
    dmnsn_free(dict->prefix);
    dmnsn_free(dict->value);
    DMNSN_ARRAY_FOREACH (dmnsn_dictionary **, subtrie, dict->children) {
      dmnsn_delete_dictionary(*subtrie);
    }
    dmnsn_delete_array(dict->children);

    dmnsn_free(dict);
  }
}

bool
dmnsn_dictionary_get(const dmnsn_dictionary *dict, const char *key, void *obj)
{
  const void *value = dmnsn_dictionary_at(dict, key);
  if (value) {
    memcpy(obj, value, dict->obj_size);
    return true;
  } else {
    return false;
  }
}

void *
dmnsn_dictionary_at(const dmnsn_dictionary *dict, const char *key)
{
  /*
   * PATRICIA trie search: O(k), where k is the length of the longest string
   * in the trie.
   */

  size_t len = strlen(dict->prefix);
  if (strncmp(key, dict->prefix, len) != 0)
    return NULL;
  key += len;

  while (true) {
    if (*key == '\0' && dict->value) {
      return dict->value;
    } else {
      dmnsn_dictionary **first = dmnsn_array_first(dict->children), **subtrie;
      ptrdiff_t size = dmnsn_array_size(dict->children);
      for (subtrie = first; subtrie - first < size; ++subtrie) {
        len = strlen((*subtrie)->prefix);
        if (strncmp(key, (*subtrie)->prefix, len) == 0) {
          dict = *subtrie;
          key += len;
          break;
        }
      }

      if (subtrie - first == size)
        break;
    }
  }

  return NULL;
}

void
dmnsn_dictionary_insert(dmnsn_dictionary *dict, const char *key,
                        const void *obj)
{
  /*
   * PATRICIA trie insertion: O(k), where k is the length of the longest string
   * in the trie.
   */

  while (true) {
    if (dict->prefix[0] == '\0' && !dict->value
        && dmnsn_array_size(dict->children) == 0)
    {
      /* Replace an empty tree with a single-element tree */
      dict->prefix = dmnsn_realloc(dict->prefix, strlen(key) + 1);
      strcpy(dict->prefix, key);

      dict->value = dmnsn_malloc(dict->obj_size);
      memcpy(dict->value, obj, dict->obj_size);
      break;
    }

    char *prefix = dict->prefix;
    while (*prefix == *key && *prefix && *key) {
      ++prefix;
      ++key;
    }

    if (*key == '\0' && *prefix == '\0') {
      /* Complete match */
      if (!dict->value) {
        dict->value = dmnsn_malloc(dict->obj_size);
      }
      memcpy(dict->value, obj, dict->obj_size);
      break;
    } else if (*prefix == '\0') {
      /* Partial match; key starts with prefix */
      dmnsn_dictionary **first = dmnsn_array_first(dict->children), **subtrie;
      ptrdiff_t size = dmnsn_array_size(dict->children);
      for (subtrie = first; subtrie - first < size; ++subtrie) {
        if ((*subtrie)->prefix[0] == key[0]) {
          dict = *subtrie;
          break;
        }
      }

      if (subtrie - first == size) {
        /* No submatch found, add a new child */
        dmnsn_dictionary *child = dmnsn_new_dictionary(dict->obj_size);
        dmnsn_array_push(dict->children, &child);
        dict = child;
      }
    } else {
      /* Split the tree */
      dmnsn_dictionary *copy = dmnsn_new_dictionary(dict->obj_size);
      copy->prefix = dmnsn_realloc(copy->prefix, strlen(prefix) + 1);
      strcpy(copy->prefix, prefix);
      *prefix = '\0';

      copy->value = dict->value;
      dict->value = NULL;

      dmnsn_array *temp = copy->children;
      copy->children = dict->children;
      dict->children = temp;

      dmnsn_dictionary *subtrie = dmnsn_new_dictionary(dict->obj_size);
      dmnsn_array_push(dict->children, &copy);
      dmnsn_array_push(dict->children, &subtrie);
      dict = subtrie;
    }
  }
}

bool
dmnsn_dictionary_remove(dmnsn_dictionary *dict, const char *key)
{
  /*
   * PATRICIA trie removal: O(k), where k is the length of the longest string
   * in the trie.
   *
   * This implementation doesn't actually collapse the tree back upwards if a
   * node is left with only one child, to reduce complexity.
   */

  size_t len = strlen(dict->prefix);
  if (strncmp(key, dict->prefix, len) != 0)
    return false;
  key += len;

  while (true) {
    if (*key == '\0' && dict->value) {
      dmnsn_free(dict->value);
      dict->value = NULL;
      return true;
    } else {
      dmnsn_dictionary **first = dmnsn_array_first(dict->children), **subtrie;
      ptrdiff_t size = dmnsn_array_size(dict->children);
      for (subtrie = first; subtrie - first < size; ++subtrie) {
        len = strlen((*subtrie)->prefix);
        if (strncmp(key, (*subtrie)->prefix, len) == 0) {
          dict = *subtrie;
          key += len;
          break;
        }
      }

      if (subtrie - first == size)
        break;
    }
  }

  return false;
}

void
dmnsn_dictionary_apply(dmnsn_dictionary *dict, dmnsn_callback_fn *callback)
{
  if (dict->value) {
    callback(dict->value);
  }

  DMNSN_ARRAY_FOREACH (dmnsn_dictionary **, subtrie, dict->children) {
    dmnsn_dictionary_apply(*subtrie, callback);
  }
}