rpatricia.git  about / heads / tags
see http://www.goto.info.waseda.ac.jp/~tatsuya/rpatricia/
blob 03f6588111671d776fba331fbffab1c016cd3d19 9875 bytes (raw)
$ git show HEAD:ext/rpatricia/rpatricia.c	# shows this blob on the CLI

  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
 
/*
 *   rpatricia.c - Ruby wrapper of Net::Patricia
 *   Tatsuya Mori <mori.tatsuya@gmail.com>
 */

#include "ruby.h"
#include <stdlib.h>
#include "patricia.h"
#include <assert.h>

size_t patricia_walk_inorder(patricia_node_t *node, void_fn_t func);
char * prefix_toa2x(prefix_t *prefix, char *buff, int with_len);
char * prefix_toa2(prefix_t *prefix, char *buff);

static VALUE cPatricia, cNode;
static VALUE sym_AF_INET, sym_AF_INET6;

static void dummy(void) {}

static VALUE
p_destroy (VALUE self)
{
  patricia_tree_t *tree;

  Data_Get_Struct(self, patricia_tree_t, tree);
  Clear_Patricia(tree, dummy);
  tree->head = NULL; /* Clear_Patricia() should do this, actually */

  return Qtrue;
}

static void
p_node_mark (void *ptr)
{
  patricia_node_t *node = ptr;

  rb_gc_mark((VALUE)node->data);
}

static VALUE
wrap_node(patricia_node_t *orig)
{
  patricia_node_t *node = ALLOC(patricia_node_t);

  memcpy(node, orig, sizeof(patricia_node_t));

  return Data_Wrap_Struct(cNode, p_node_mark, -1, node);
}

static void
my_ascii2prefix(patricia_tree_t *tree, VALUE str, prefix_t *prefix)
{
  char *cstr = StringValueCStr(str);
  prefix_t *ok = ascii2prefix(cstr, prefix);

  if (!ok)
    rb_raise(rb_eArgError, "invalid prefix: %s", cstr);

  if (prefix->bitlen > tree->maxbits)
    rb_raise(rb_eArgError, "prefix length (%u) larger than maxbits (%u)",
             prefix->bitlen, tree->maxbits);
}

static VALUE
p_add (int argc, VALUE *argv, VALUE self)
{
  VALUE user_data;
  patricia_tree_t *tree;
  patricia_node_t *node;
  prefix_t prefix;

  if (argc > 2 || argc < 1) 
    return Qnil;

  Data_Get_Struct(self, patricia_tree_t, tree);
  my_ascii2prefix(tree, argv[0], &prefix);
  node = patricia_lookup(tree, &prefix);

  if (argc == 2) {
    user_data = argv[1];

    /* for backwards compatibility, we always dup and return new strings */
    if (TYPE(user_data) == T_STRING)
      user_data = rb_str_dup(user_data);
  } else {
    user_data = rb_str_new(NULL, 0);
  }
  PATRICIA_DATA_SET(node, user_data);

  /* node will be freed when parent is freed */
  return wrap_node(node);
}

static VALUE
p_remove (VALUE self, VALUE r_key)
{
  patricia_tree_t *tree;
  patricia_node_t *node;
  prefix_t prefix;

  Data_Get_Struct(self, patricia_tree_t, tree);
  my_ascii2prefix(tree, r_key, &prefix);
  node = patricia_search_exact(tree, &prefix);

  if (node) {
    patricia_remove (tree, node);
    return Qtrue;
  } else {
    return Qfalse;
  }
}

static VALUE
p_match (VALUE self, VALUE r_key)
{
  patricia_tree_t *tree;
  patricia_node_t *node;
  prefix_t prefix;
  
  Data_Get_Struct(self, patricia_tree_t, tree);
  my_ascii2prefix(tree, r_key, &prefix);
  node = patricia_search_best(tree, &prefix);

  return node ? wrap_node(node) : Qnil;
}

static VALUE
p_include (VALUE self, VALUE r_key)
{
  patricia_tree_t *tree;
  patricia_node_t *node;
  prefix_t prefix;

  Data_Get_Struct(self, patricia_tree_t, tree);
  my_ascii2prefix(tree, r_key, &prefix);
  node = patricia_search_best(tree, &prefix);

  return node ? Qtrue : Qfalse;
}

static VALUE
p_match_exact (VALUE self, VALUE r_key)
{
  patricia_tree_t *tree;
  patricia_node_t *node;
  prefix_t prefix;

  Data_Get_Struct(self, patricia_tree_t, tree);
  my_ascii2prefix(tree, r_key, &prefix);
  node = patricia_search_exact(tree, &prefix);

  return node ? wrap_node(node) : Qnil;
}

static VALUE
p_num_nodes (VALUE self)
{
  int n;
  patricia_tree_t *tree;

  Data_Get_Struct(self, patricia_tree_t, tree);
  n = tree->head ? patricia_walk_inorder(tree->head, dummy) : 0;

  return INT2NUM(n);
}

/* needed for Ruby 1.8.6, in 1.8.7 and later */
#ifndef HAVE_RB_STR_SET_LEN
static void
rb_str_set_len(VALUE str, long len)
{
  RSTRING(str)->len = len;
  RSTRING(str)->ptr[len] = '\0';
}
#endif

static VALUE
p_print_nodes (int argc, VALUE *argv, VALUE self)
{
  ID id_printf = rb_intern("printf");
  VALUE fmt = rb_str_new2("node: %s\n");
  VALUE buf = rb_str_new(0, 0);
  char *cbuf;
  patricia_tree_t *tree;
  patricia_node_t *node;
  VALUE out;
  Data_Get_Struct(self, patricia_tree_t, tree);

  rb_scan_args(argc, argv, "01", &out);
  if (NIL_P(out))
    out = rb_stdout;

  if (tree->head) {
    PATRICIA_WALK(tree->head, node) {
      rb_str_resize(buf, PATRICIA_MAXSTRLEN);
      cbuf = RSTRING_PTR(buf);
      prefix_toa2x(node->prefix, cbuf, 1);
      rb_str_set_len(buf, strlen(cbuf));
      rb_funcall(out, id_printf, 2, fmt, buf);
    } PATRICIA_WALK_END;
  }
  return Qtrue;
}

static VALUE
p_nodes (VALUE self)
{
  VALUE buf = rb_str_new(0, 0);
  char *cbuf;
  patricia_tree_t *tree;
  patricia_node_t *node;
  VALUE hash;

  Data_Get_Struct(self, patricia_tree_t, tree);

  hash = rb_hash_new();
  if (tree->head) {
    PATRICIA_WALK(tree->head, node) {
      rb_str_resize(buf, PATRICIA_MAXSTRLEN);
      cbuf = RSTRING_PTR(buf);
      prefix_toa2x(node->prefix, cbuf, 1);
      rb_str_set_len(buf, strlen(cbuf));
      rb_hash_aset(hash, buf, (VALUE)node->data);
    } PATRICIA_WALK_END;
  }
  return hash;
}

static VALUE
p_data (VALUE self)
{
  VALUE user_data;
  patricia_node_t *node;
  Data_Get_Struct(self, patricia_node_t, node);

  user_data = (VALUE)node->data;

  /* for backwards compatibility, we always dup and return new strings */
  if (TYPE(user_data) == T_STRING)
    user_data = rb_str_dup(user_data);

  return user_data;
}

static VALUE
p_network (VALUE self)
{
  patricia_node_t *node;
  VALUE str = rb_str_new(0, PATRICIA_MAXSTRLEN);
  char *cstr = RSTRING_PTR(str);

  Data_Get_Struct(self, patricia_node_t, node);
  prefix_toa2x(node->prefix, cstr, 0);
  rb_str_set_len(str, strlen(cstr));

  return str;
}

static VALUE
p_prefix (VALUE self)
{
  patricia_node_t *node;
  VALUE str = rb_str_new(0, INET6_ADDRSTRLEN);
  char *cstr = RSTRING_PTR(str);

  Data_Get_Struct(self, patricia_node_t, node);
  prefix_toa2(node->prefix, cstr);
  rb_str_set_len(str, strlen(cstr));

  return str;
}

static VALUE
p_prefixlen (VALUE self)
{
  patricia_node_t *node;
  Data_Get_Struct(self, patricia_node_t, node);
  return INT2NUM(node->prefix->bitlen);
}

/* called during GC for each node->data attached to a Patricia tree */
static void
p_tree_mark_each(prefix_t *prefix, void *data)
{
  VALUE user_data = (VALUE)data;

  rb_gc_mark(user_data);
}

static void
p_tree_mark (void *ptr)
{
  patricia_tree_t *tree = ptr;

  if (tree)
    patricia_process(tree, p_tree_mark_each);
}

static void
p_tree_free (void *ptr)
{
  patricia_tree_t *tree = ptr;
  /* no need to explicitly free each node->data, GC will do it for us */
  if (tree)
    Destroy_Patricia(tree, NULL);
}

static VALUE
p_alloc(VALUE klass)
{
  return Data_Wrap_Struct(klass, p_tree_mark, p_tree_free, NULL);
}

static VALUE
p_init(int argc, VALUE *argv, VALUE self)
{
  VALUE family;
  int maxbits;

  rb_scan_args(argc, argv, "01", &family);

  if (NIL_P(family) || family == sym_AF_INET)
    maxbits = 32;
  else if (family == sym_AF_INET6)
    maxbits = 128;
  else
    rb_raise(rb_eArgError, "unknown family (must be :AF_INET6 or :AF_INET)");

  DATA_PTR(self) = New_Patricia(maxbits);
  return self;
}

static VALUE
p_init_copy(VALUE self, VALUE orig)
{
  patricia_tree_t *orig_tree;

  Data_Get_Struct(orig, patricia_tree_t, orig_tree);
  if (orig_tree->head) {
    patricia_tree_t *tree;
    patricia_node_t *orig_node, *node;
    VALUE user_data;

    DATA_PTR(self) = tree = New_Patricia(orig_tree->maxbits);

    PATRICIA_WALK(orig_tree->head, orig_node) {
      node = patricia_lookup(tree, orig_node->prefix);
      assert(node->prefix == orig_node->prefix);

      user_data = (VALUE)(orig_node->data);
      if (T_STRING == TYPE(user_data))
        user_data = rb_str_dup(user_data);
      PATRICIA_DATA_SET(node, user_data);
    } PATRICIA_WALK_END;
  }

  return self;
}

static VALUE
p_family(VALUE self)
{
  patricia_tree_t *tree;

  Data_Get_Struct(self, patricia_tree_t, tree);

  switch (tree->maxbits) {
  case 32: return sym_AF_INET;
  case 128: return sym_AF_INET6;
  }
  assert(0 && "unknown maxbits, corrupt tree");
  return Qnil;
}

void
Init_rpatricia (void)
{
  cPatricia = rb_define_class("Patricia", rb_cObject);
  cNode = rb_define_class_under(cPatricia, "Node", rb_cObject);
  sym_AF_INET = ID2SYM(rb_intern("AF_INET"));
  sym_AF_INET6 = ID2SYM(rb_intern("AF_INET6"));

  /* allocate new Patricia object, called before initialize  */
  rb_define_alloc_func(cPatricia, p_alloc);
  rb_define_private_method(cPatricia, "initialize", p_init, -1);
  rb_define_method(cPatricia, "initialize_copy", p_init_copy, 1);

  /*---------- methods to tree ----------*/
  /* add string */
  rb_define_method(cPatricia, "add", p_add, -1);
  rb_define_method(cPatricia, "add_node", p_add, -1);
  rb_define_method(cPatricia, "family", p_family, 0);

  /* match prefix */
  rb_define_method(cPatricia, "match_best", p_match, 1); 
  rb_define_method(cPatricia, "search_best", p_match, 1); 

  /* exact match  */
  rb_define_method(cPatricia, "match_exact", p_match_exact, 1); 
  rb_define_method(cPatricia, "search_exact", p_match_exact, 1); 

  /* check existence */
  rb_define_method(cPatricia, "include?", p_include, 1);

  /* removal */
  rb_define_method(cPatricia, "remove", p_remove, 1); 
  rb_define_method(cPatricia, "remove_node", p_remove, 1); 

  /* derivatives of climb */
  rb_define_method(cPatricia, "num_nodes", p_num_nodes, 0);
  rb_define_method(cPatricia, "show_nodes", p_print_nodes, -1);
  rb_define_method(cPatricia, "nodes", p_nodes, 0);

  /* destroy tree */
  rb_define_method(cPatricia, "destroy", p_destroy, 0); 
  rb_define_method(cPatricia, "clear", p_destroy, 0); 

  /*---------- methods to node ----------*/
  rb_define_method(cNode, "data", p_data, 0);
  rb_define_method(cNode, "show_data", p_data, 0);
  rb_define_method(cNode, "network", p_network, 0);
  rb_define_method(cNode, "prefix", p_prefix, 0);
  rb_define_method(cNode, "prefixlen", p_prefixlen, 0);
}

git clone https://yhbt.net/rpatricia.git