rpatricia.git  about / heads / tags
see http://www.goto.info.waseda.ac.jp/~tatsuya/rpatricia/
blob 75000e1bedffec5dd1c732e883db506223d3eafa 5138 bytes (raw)
$ git show reorg: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
 
/*
 *   rpatricia.c - Ruby wrapper of Net::Patricia
 *   Tatsuya Mori <mori.tatsuya@gmail.com>
 */

#include "ruby.h"
#include <stdio.h> /* printf */
#include <stdlib.h>
#include "patricia.h"

VALUE cPatricia;

void dummy(void) {}

static VALUE
p_destroy (VALUE self)
{
  patricia_tree_t *tree;
  Data_Get_Struct(self, patricia_tree_t, tree);
  Destroy_Patricia(tree, free);
  return Qtrue;
}

VALUE
p_add (int argc, VALUE *argv, VALUE self)
{
  int str_len;
  char *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);
  prefix = ascii2prefix(AF_INET, STR2CSTR(argv[0]));
  node = patricia_lookup(tree, prefix);
  Deref_Prefix(prefix);

  if (argc == 2) {
    user_data = STR2CSTR(argv[1]);
    str_len = strlen(user_data);
    node->data = (char *) malloc((str_len + 1) * sizeof(char));
    sprintf((char *)node->data, user_data);
  } else {
    node->data = (char *) malloc(sizeof(char));
    sprintf((char *)node->data, "");
  }
  return Data_Wrap_Struct(cPatricia, 0, 0, node);
}

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

  Data_Get_Struct(self, patricia_tree_t, tree);
  c_key = STR2CSTR(r_key);
  prefix = ascii2prefix (AF_INET, c_key);
  node = patricia_search_exact(tree, prefix);
  Deref_Prefix (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);
  prefix = ascii2prefix (AF_INET, STR2CSTR(r_key));
  node = patricia_search_best(tree, prefix);
  Deref_Prefix (prefix);

  if (node)
    return Data_Wrap_Struct(cPatricia, 0, 0, node);
  else 
    return 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);
  prefix = ascii2prefix (AF_INET, STR2CSTR(r_key));
  node = patricia_search_exact(tree, prefix);
  Deref_Prefix (prefix);

  if (node)
    return Data_Wrap_Struct(cPatricia, 0, 0, node);
  else 
    return Qfalse;
}

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

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

  return INT2NUM(n);
}

static VALUE
p_print_nodes (VALUE self)
{
  int n;
  char buff[32];
  patricia_tree_t *tree;
  patricia_node_t *node;
  Data_Get_Struct(self, patricia_tree_t, tree);

  PATRICIA_WALK(tree->head, node) {
    prefix_toa2x(node->prefix, buff, 1);
    printf("node: %s\n", buff);
  } PATRICIA_WALK_END;
  return Qtrue;
}

static VALUE
p_data (VALUE self)
{
  patricia_node_t *node;
  Data_Get_Struct(self, patricia_node_t, node);
  return rb_str_new2((char *)node->data);
}

static VALUE
p_network (VALUE self)
{
  char buff[32];
  patricia_node_t *node;
  Data_Get_Struct(self, patricia_node_t, node);
  prefix_toa2x (node->prefix, buff, 0);
  return rb_str_new2(buff);
}

static VALUE
p_prefix (VALUE self)
{
  char buff[32];
  patricia_node_t *node;
  Data_Get_Struct(self, patricia_node_t, node);
  prefix_toa2 (node->prefix, buff);
  return rb_str_new2(buff);
}

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

static VALUE
p_new (VALUE self) 
{
  patricia_tree_t *tree;
  tree = New_Patricia(32); /* assuming only IPv4 */
  return Data_Wrap_Struct(cPatricia, 0, 0, tree);
}

void
Init_rpatricia (void)
{
  cPatricia = rb_define_class("Patricia", rb_cObject);

  /* create new Patricia object */
  rb_define_singleton_method(cPatricia, "new", p_new, 0);

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

  /* 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); 

  /* 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, 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(cPatricia, "data", p_data, 0);
  rb_define_method(cPatricia, "show_data", p_data, 0);
  rb_define_method(cPatricia, "network", p_network, 0);
  rb_define_method(cPatricia, "prefix", p_prefix, 0);
  rb_define_method(cPatricia, "prefixlen", p_prefixlen, 0);
  //  rb_define_method(cPatricia, "family", p_family, 0); 

}

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