birch 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/ext/birch/birch.c +131 -58
  2. data/lib/birch/version.rb +1 -1
  3. metadata +2 -2
data/ext/birch/birch.c CHANGED
@@ -1,5 +1,9 @@
1
1
  #include <ruby.h>
2
2
 
3
+ static VALUE birch;
4
+ static VALUE birch_tree;
5
+ static VALUE birch_edge;
6
+
3
7
  /*
4
8
  * Initialize the node with its value and id.
5
9
  * Setup containers for the children, features
@@ -14,15 +18,14 @@ static VALUE birch_initialize(VALUE self, VALUE value, VALUE id)
14
18
  VALUE edges;
15
19
 
16
20
  parent = Qnil;
17
- children = rb_ary_new();
21
+ children = rb_ary_new();
18
22
  children_hash = rb_hash_new();
19
23
  features = rb_hash_new();
20
24
  edges = rb_ary_new();
21
25
 
22
26
  rb_iv_set(self, "@value", value);
23
27
  rb_iv_set(self, "@id", id);
24
-
25
- rb_iv_set(self, "@parent", parent);
28
+ rb_iv_set(self, "@parent", parent);
26
29
  rb_iv_set(self, "@children", children);
27
30
  rb_iv_set(self, "@children_hash", children_hash);
28
31
  rb_iv_set(self, "@features", features);
@@ -31,6 +34,29 @@ static VALUE birch_initialize(VALUE self, VALUE value, VALUE id)
31
34
  return self;
32
35
  }
33
36
 
37
+ static VALUE birch_edge_initialize(int argc, VALUE* argv, VALUE self) {
38
+
39
+ VALUE node_a;
40
+ VALUE node_b;
41
+ VALUE directed;
42
+ VALUE direction;
43
+
44
+ if (argc < 2 || argc > 4) {
45
+ rb_raise(rb_eArgError, "Wrong number of arguments.");
46
+ }
47
+
48
+ rb_iv_set(self, "@node_a", argv[0]);
49
+ rb_iv_set(self, "@node_b", argv[1]);
50
+ rb_iv_set(self, "@directed", argv[2]);
51
+
52
+ if (argc == 4) {
53
+ rb_iv_set(self, "@direction", argv[3]);
54
+ } else {
55
+ rb_iv_set(self, "@direction", Qnil);
56
+ }
57
+
58
+ }
59
+
34
60
  /* Return the root of the tree. */
35
61
  static VALUE birch_root(VALUE self) {
36
62
 
@@ -56,37 +82,48 @@ static VALUE birch_root(VALUE self) {
56
82
  }
57
83
  }
58
84
 
59
- /* Add a child to this node. */
60
- static VALUE birch_add(VALUE self, VALUE child) {
85
+ /* Add a child(ren) to this node. Return first child added. */
86
+ static VALUE birch_add(VALUE self, VALUE node_or_nodes) {
61
87
 
88
+ long i;
89
+ VALUE nodes;
90
+ VALUE child;
62
91
  VALUE children;
63
92
  VALUE children_hash;
64
-
65
93
  VALUE child_id;
66
94
 
67
95
  children = rb_iv_get(self, "@children");
68
- child_id = rb_funcall(child, rb_intern("id"), 0);
69
96
  children_hash = rb_iv_get(self, "@children_hash");
70
97
 
71
- rb_funcall(children, rb_intern("push"), 1, child);
72
-
73
- rb_funcall(
74
- children_hash,
75
- rb_intern("store"),
76
- 2, child_id, child
77
- );
78
-
79
- rb_iv_set(child, "@parent", self);
98
+ if (CLASS_OF(node_or_nodes) != rb_cArray) {
99
+ nodes = rb_ary_new();
100
+ rb_funcall(nodes, rb_intern("push"), 1, node_or_nodes);
101
+ } else {
102
+ nodes = node_or_nodes;
103
+ }
80
104
 
81
- return child;
105
+ for (i = 0; i < RARRAY_LEN(nodes); i++) {
106
+
107
+ child = RARRAY_PTR(nodes)[i];
108
+ child_id = rb_iv_get(child, "@id");
109
+
110
+ rb_funcall(children, rb_intern("push"), 1, child);
111
+
112
+ rb_funcall(children_hash, rb_intern("store"),
113
+ 2, child_id, child
114
+ );
115
+ rb_iv_set(child, "@parent", self);
116
+ }
117
+
118
+ return RARRAY_PTR(nodes)[0];
82
119
 
83
120
  }
84
121
 
85
122
  /* Return the feature with the supplied name. */
86
123
  static VALUE birch_get(VALUE self, VALUE feature) {
87
- if (feature == rb_intern("id")) {
124
+ if (rb_intern("id") == SYM2ID(feature)) {
88
125
  return rb_iv_get(self, "@id");
89
- } else if (feature == rb_intern("value")) {
126
+ } else if (SYM2ID(feature) == rb_intern("value")) {
90
127
  return rb_iv_get(self, "@value");
91
128
  } else {
92
129
  return rb_hash_aref(rb_iv_get(self, "@features"), feature);
@@ -147,13 +184,18 @@ static VALUE birch_each(VALUE self) {
147
184
 
148
185
  }
149
186
 
150
- static VALUE birch_find(VALUE self, VALUE id) {
187
+ static VALUE birch_find(VALUE self, VALUE id_or_node) {
151
188
 
189
+ VALUE id;
152
190
  VALUE children_hash;
153
191
  VALUE children;
154
192
  VALUE result;
155
193
  long i;
156
194
 
195
+ if (rb_obj_is_kind_of(id_or_node, birch_tree)) {
196
+ id = rb_iv_get(id_or_node, "@id");
197
+ } else { id = id_or_node; }
198
+
157
199
  children_hash = rb_iv_get(self, "@children_hash");
158
200
  result = rb_hash_aref(children_hash, id);
159
201
 
@@ -198,25 +240,25 @@ static VALUE birch_has_children(VALUE self) {
198
240
  }
199
241
  }
200
242
 
201
- /* Boolean - does the node have a parent? */
202
- static VALUE birch_has_parent(VALUE self) {
203
- if (rb_iv_get(self, "@parent") == Qnil) {
243
+ /* Boolean - does this node have children */
244
+ static VALUE birch_has_edges(VALUE self) {
245
+ if (RARRAY_LEN(rb_iv_get(self, "@edges")) == 0) {
204
246
  return Qfalse;
205
247
  } else {
206
248
  return Qtrue;
207
249
  }
208
250
  }
209
251
 
210
- /* Boolean - does the node have edges? */
211
- static VALUE birch_has_edges(VALUE self) {
212
- if (RARRAY_LEN(rb_iv_get(self, "@edges")) == 0) {
252
+ /* Boolean - does the node have a parent? */
253
+ static VALUE birch_has_parent(VALUE self) {
254
+ if (rb_iv_get(self, "@parent") == Qnil) {
213
255
  return Qfalse;
214
256
  } else {
215
257
  return Qtrue;
216
258
  }
217
259
  }
218
260
 
219
- /* Boolean - does the node have feature? */
261
+ /* Boolean - does the node have the feature? */
220
262
  static VALUE birch_has_feature(VALUE self, VALUE feature) {
221
263
  VALUE features;
222
264
  features = rb_iv_get(self, "@features");
@@ -227,6 +269,16 @@ static VALUE birch_has_feature(VALUE self, VALUE feature) {
227
269
  }
228
270
  }
229
271
 
272
+ /* Boolean - does the node have features? */
273
+ static VALUE birch_has_features(VALUE self) {
274
+ VALUE features;
275
+ features = rb_iv_get(self, "@features");
276
+ if (!RHASH(features)->ntbl) {
277
+ return Qfalse;
278
+ } else { return Qtrue; }
279
+ }
280
+
281
+
230
282
  static VALUE birch_link(VALUE self, VALUE edge) {
231
283
 
232
284
  VALUE edges;
@@ -236,6 +288,8 @@ static VALUE birch_link(VALUE self, VALUE edge) {
236
288
  edges, rb_intern("push"), 1, edge
237
289
  );
238
290
 
291
+ return edge;
292
+
239
293
  }
240
294
 
241
295
  /* Remove from parent and set as root */
@@ -313,42 +367,61 @@ static VALUE birch_remove_all(VALUE self) {
313
367
  */
314
368
  void Init_birch(void) {
315
369
 
316
- VALUE module = rb_define_module("Birch");
317
- VALUE klass = rb_define_class_under(module, "Tree", rb_cObject);
370
+ birch = rb_define_module("Birch");
371
+
372
+ /*
373
+ * Birch::Tree
374
+ */
375
+ birch_tree = rb_define_class_under(birch, "Tree", rb_cObject);
318
376
 
319
377
  // Attribute accessors
320
- rb_attr(klass, rb_intern("id"), 1, 1, 1);
321
- rb_attr(klass, rb_intern("value"), 1, 1, 1);
378
+ rb_attr(birch_tree, rb_intern("id"), 1, 1, 1);
379
+ rb_attr(birch_tree, rb_intern("value"), 1, 1, 1);
380
+ rb_attr(birch_tree, rb_intern("parent"), 1, 1, 1);
381
+ rb_attr(birch_tree, rb_intern("features"), 1, 1, 1);
322
382
 
323
383
  // Attribute readers
324
- rb_attr(klass, rb_intern("parent"), 1, 0, 0);
325
- rb_attr(klass, rb_intern("children"), 1, 0, 0);
326
- rb_attr(klass, rb_intern("features"), 1, 0, 0);
327
- rb_attr(klass, rb_intern("edges"), 1, 0, 0);
384
+ rb_attr(birch_tree, rb_intern("children"), 1, 0, 0);
385
+ rb_attr(birch_tree, rb_intern("edges"), 1, 0, 0);
328
386
 
329
387
  // Methods
330
- rb_define_method(klass, "initialize", birch_initialize, 2);
331
- rb_define_method(klass, "root", birch_root, 0);
332
- rb_define_method(klass, "<<", birch_add, 1);
333
- rb_define_method(klass, "add", birch_add, 1);
334
- rb_define_method(klass, "[]", birch_get, 1);
335
- rb_define_method(klass, "get", birch_get, 1);
336
- rb_define_method(klass, "[]=", birch_set, 2);
337
- rb_define_method(klass, "set", birch_set, 2);
338
- rb_define_method(klass, "unset", birch_unset, 1);
339
- rb_define_method(klass, "size", birch_size, 0);
340
- rb_define_method(klass, "each", birch_each, 0);
341
- rb_define_method(klass, "find", birch_find, 1);
342
- rb_define_method(klass, "is_leaf?", birch_is_leaf, 0);
343
- rb_define_method(klass, "is_root?", birch_is_root, 0);
344
- rb_define_method(klass, "has_parent?", birch_has_parent, 0);
345
- rb_define_method(klass, "has_children?", birch_has_children, 0);
346
- rb_define_method(klass, "has_edges?", birch_has_edges, 0);
347
- rb_define_method(klass, "has_feature?", birch_has_feature, 1);
348
- rb_define_method(klass, "has?", birch_has_feature, 1);
349
- rb_define_method(klass, "link", birch_link, 1);
350
- rb_define_method(klass, "set_as_root!", birch_set_as_root, 0);
351
- rb_define_method(klass, "remove", birch_remove, 1);
352
- rb_define_method(klass, "remove_all!", birch_remove_all, 0);
388
+ rb_define_method(birch_tree, "initialize", birch_initialize, 2);
389
+ rb_define_method(birch_tree, "root", birch_root, 0);
390
+ rb_define_method(birch_tree, "<<", birch_add, 1);
391
+ rb_define_method(birch_tree, "add", birch_add, 1);
392
+ rb_define_method(birch_tree, "[]", birch_get, 1);
393
+ rb_define_method(birch_tree, "get", birch_get, 1);
394
+ rb_define_method(birch_tree, "[]=", birch_set, 2);
395
+ rb_define_method(birch_tree, "set", birch_set, 2);
396
+ rb_define_method(birch_tree, "unset", birch_unset, 1);
397
+ rb_define_method(birch_tree, "size", birch_size, 0);
398
+ rb_define_method(birch_tree, "each", birch_each, 0);
399
+ rb_define_method(birch_tree, "find", birch_find, 1);
400
+ rb_define_method(birch_tree, "is_leaf?", birch_is_leaf, 0);
401
+ rb_define_method(birch_tree, "is_root?", birch_is_root, 0);
402
+ rb_define_method(birch_tree, "has_parent?", birch_has_parent, 0);
403
+ rb_define_method(birch_tree, "has_children?", birch_has_children, 0);
404
+ rb_define_method(birch_tree, "has_edges?", birch_has_edges, 0);
405
+ rb_define_method(birch_tree, "has_feature?", birch_has_feature, 1);
406
+ rb_define_method(birch_tree, "has_features?", birch_has_features, 0);
407
+ rb_define_method(birch_tree, "has?", birch_has_feature, 1);
408
+ rb_define_method(birch_tree, "link", birch_link, 1);
409
+ rb_define_method(birch_tree, "set_as_root!", birch_set_as_root, 0);
410
+ rb_define_method(birch_tree, "remove", birch_remove, 1);
411
+ rb_define_method(birch_tree, "remove_all!", birch_remove_all, 0);
412
+
413
+ /*
414
+ * Birch::Edge
415
+ */
416
+
417
+ birch_edge = rb_define_class_under(birch, "Edge", rb_cObject);
418
+
419
+ // Attribute readers
420
+ rb_attr(birch_tree, rb_intern("node_a"), 1, 0, 0);
421
+ rb_attr(birch_tree, rb_intern("node_b"), 1, 0, 0);
422
+ rb_attr(birch_tree, rb_intern("directed"), 1, 0, 0);
423
+ rb_attr(birch_tree, rb_intern("direction"), 1, 0, 0);
424
+
425
+ rb_define_method(birch_edge, "initialize", birch_edge_initialize, -1);
353
426
 
354
427
  }
data/lib/birch/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Birch
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: birch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-04 00:00:00.000000000 Z
12
+ date: 2012-07-29 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! ' '
15
15
  email: