birch 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  require 'mkmf'
2
2
 
3
- create_makefile('birch/birch')
3
+ create_makefile('birch/native')
@@ -41,7 +41,7 @@ static VALUE birch_edge_initialize(int argc, VALUE* argv, VALUE self) {
41
41
  VALUE directed;
42
42
  VALUE direction;
43
43
 
44
- if (argc < 2 || argc > 4) {
44
+ if (argc < 3 || argc > 4) {
45
45
  rb_raise(rb_eArgError, "Wrong number of arguments.");
46
46
  }
47
47
 
@@ -64,7 +64,7 @@ static VALUE birch_root(VALUE self) {
64
64
  VALUE parent;
65
65
  VALUE tmp;
66
66
 
67
- parent = rb_iv_get(ancestor, "@parent");
67
+ parent = rb_iv_get(self, "@parent");
68
68
 
69
69
  if (parent == Qnil) {
70
70
  return self;
@@ -145,7 +145,6 @@ static VALUE birch_unset(VALUE self, VALUE feature) {
145
145
 
146
146
  /* Return the total number of nodes in the subtree */
147
147
  static VALUE birch_size(VALUE self) {
148
-
149
148
 
150
149
  VALUE children;
151
150
  int sum;
@@ -154,17 +153,14 @@ static VALUE birch_size(VALUE self) {
154
153
 
155
154
  children = rb_iv_get(self, "@children");
156
155
  len = RARRAY_LEN(children);
157
- sum = 0;
158
-
156
+
157
+ sum = 0;
159
158
  for (i=0; i<len; i++) {
160
- sum += rb_funcall(
161
- RARRAY_PTR(children)[i],
162
- rb_intern("size"), 0
163
- );
159
+ sum += NUM2INT(rb_funcall(RARRAY_PTR(children)[i], rb_intern("size"), 0));
164
160
  }
165
161
 
166
- return INT2NUM(sum);
167
-
162
+ // subtree size is our node size (1) + all childen sizes
163
+ return INT2NUM(1 + sum);
168
164
  }
169
165
 
170
166
  /* Iterate over each children of the node. */
@@ -294,12 +290,12 @@ static VALUE birch_link(VALUE self, VALUE edge) {
294
290
 
295
291
  /* Remove from parent and set as root */
296
292
  static VALUE birch_set_as_root(VALUE self) {
297
-
293
+
298
294
  // Set the parent to be nil.
299
295
  rb_iv_set(self, "@parent", Qnil);
300
296
 
301
297
  // Return self.
302
- return self;
298
+ return Qnil;
303
299
 
304
300
  }
305
301
 
@@ -314,7 +310,7 @@ static VALUE birch_remove(VALUE self, VALUE id) {
314
310
  );
315
311
 
316
312
  // Raise an exception if the value can't be found.
317
- if (val == Qundef) {
313
+ if (val == Qnil) {
318
314
  rb_raise(rb_eArgError,
319
315
  "Given ID is not a children of this node.");
320
316
  return Qnil;
@@ -343,7 +339,8 @@ static VALUE birch_remove_all(VALUE self) {
343
339
  long children_len;
344
340
 
345
341
  children = rb_iv_get(self, "@children");
346
-
342
+ children_len = RARRAY_LEN(children);
343
+
347
344
  for (i = 0; i < children_len; i++) {
348
345
  rb_funcall(
349
346
  RARRAY_PTR(children)[i],
@@ -354,7 +351,7 @@ static VALUE birch_remove_all(VALUE self) {
354
351
  rb_iv_set(self, "@children", rb_ary_new());
355
352
  rb_iv_set(self, "@children_hash", rb_hash_new());
356
353
 
357
- return self;
354
+ return Qnil;
358
355
  }
359
356
 
360
357
  /* This class is a node for an N-ary tree data structure
@@ -365,7 +362,7 @@ static VALUE birch_remove_all(VALUE self) {
365
362
  * RubyTree is licensed under the BSD license and can
366
363
  * be found at http://rubytree.rubyforge.org/rdoc/.
367
364
  */
368
- void Init_birch(void) {
365
+ void Init_native(void) {
369
366
 
370
367
  birch = rb_define_module("Birch");
371
368
 
@@ -417,10 +414,10 @@ void Init_birch(void) {
417
414
  birch_edge = rb_define_class_under(birch, "Edge", rb_cObject);
418
415
 
419
416
  // 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);
417
+ rb_attr(birch_edge, rb_intern("node_a"), 1, 0, 0);
418
+ rb_attr(birch_edge, rb_intern("node_b"), 1, 0, 0);
419
+ rb_attr(birch_edge, rb_intern("directed"), 1, 0, 0);
420
+ rb_attr(birch_edge, rb_intern("direction"), 1, 0, 0);
424
421
 
425
422
  rb_define_method(birch_edge, "initialize", birch_edge_initialize, -1);
426
423
 
@@ -1 +1,6 @@
1
- require 'birch/birch'
1
+ if RUBY_PLATFORM =~ /java/
2
+ require 'birch/pure'
3
+ else
4
+ require 'birch/native'
5
+ end
6
+ require 'birch/version'
@@ -0,0 +1,132 @@
1
+ module Birch
2
+
3
+ class Tree
4
+
5
+ attr_accessor :id, :value
6
+ attr_accessor :parent, :features
7
+ attr_reader :children, :edges
8
+
9
+ def initialize(value, id)
10
+ @value, @id = value, id
11
+ @parent = nil
12
+ @children = []
13
+ @children_hash = {}
14
+ @features = {}
15
+ @edges = []
16
+ end
17
+
18
+ def root
19
+ @parent.nil? ? self : root(parent)
20
+ end
21
+
22
+ def add(node_or_nodes)
23
+ nodes = [node_or_nodes].flatten
24
+ nodes.each do |node|
25
+ @children << node
26
+ @children_hash[node.id] = node
27
+ node.parent = self
28
+ end
29
+ nodes.first
30
+ end
31
+ alias_method :<<, :add
32
+
33
+ def get(feature)
34
+ case feature
35
+ when :id
36
+ @id
37
+ when :value
38
+ @value
39
+ else
40
+ @features[feature]
41
+ end
42
+ end
43
+ alias_method :[], :get
44
+
45
+ def set(feature, value)
46
+ @features[feature] = value
47
+ end
48
+ alias_method :[]=, :set
49
+
50
+ def unset(feature)
51
+ @features.delete(feature)
52
+ end
53
+
54
+ def size
55
+ 1 + (@children.map(&:size).reduce(:+) || 0)
56
+ end
57
+
58
+ def each
59
+ @children.each{|c| yield c}
60
+ end
61
+
62
+ def find(id_or_node)
63
+ id = id_or_node.is_a?(Tree) ? id_or_node.id : id_or_node
64
+ return @children_hash[id] if @children_hash.has_key?(id)
65
+ @children.each{|c| r = c.find(id); return r if r}
66
+ nil
67
+ end
68
+
69
+ def is_leaf?; @children.empty?; end
70
+ def is_root?; @parent.nil?; end
71
+ def has_children?; !@children.empty?; end
72
+ def has_edges?; !@edges.empty?; end
73
+ def has_parent?; !@parent.nil? ;end
74
+ def has_features?; !@features.empty?; end
75
+
76
+ def has_feature?(feature)
77
+ @features.has_key?(feature)
78
+ end
79
+
80
+ alias_method :has?, :has_feature?
81
+
82
+ def link(edge)
83
+ @edges << edge
84
+ edge
85
+ end
86
+
87
+ def set_as_root!
88
+ if has_parent?
89
+ @parent = nil
90
+ else
91
+ raise "Node is already the root."
92
+ end
93
+ nil
94
+ end
95
+
96
+ def remove(id)
97
+ node = @children_hash.delete(id)
98
+ if node.nil?
99
+ raise ArgumentError,
100
+ "Given ID is not a children of this node."
101
+ end
102
+ @children.delete(node)
103
+ node.set_as_root!
104
+ node
105
+ end
106
+
107
+ def remove_all!
108
+ @children.each{|c| c.set_as_root!}
109
+ @children = []
110
+ @children_hash = {}
111
+ nil
112
+ end
113
+ end
114
+
115
+ class Edge
116
+ attr_reader :node_a, :node_b
117
+ attr_reader :directed, :direction
118
+
119
+ def initialize(node_a, node_b, directed = false, direction = 0)
120
+ unless [3, 4].include?(args.size)
121
+ raise ArgumentError, "Wrong number of arguments."
122
+ end
123
+ @node_a = args[0]
124
+ @node_b = args[1]
125
+ @directed = args[2]
126
+ @direction = args.size == 4 ? args[3] : nil
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+
@@ -1,3 +1,3 @@
1
- class Birch
2
- VERSION = '0.0.2'
1
+ module Birch
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,27 +1,62 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: birch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Louis Mullie
9
+ - Colin Surprenant
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2012-07-29 00:00:00.000000000 Z
13
- dependencies: []
13
+ date: 2012-12-12 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.12.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 2.12.0
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
14
47
  description: ! ' '
15
48
  email:
16
49
  - louis.mullie@gmail.com
50
+ - colin.surprenant@gmail.com
17
51
  executables: []
18
52
  extensions:
19
53
  - ext/birch/extconf.rb
20
54
  extra_rdoc_files: []
21
55
  files:
56
+ - lib/birch/pure.rb
22
57
  - lib/birch/version.rb
23
58
  - lib/birch.rb
24
- - ext/birch/birch.c
59
+ - ext/birch/native.c
25
60
  - ext/birch/extconf.rb
26
61
  homepage: https://github.com/louismullie/birch
27
62
  licenses: []
@@ -46,5 +81,5 @@ rubyforge_project:
46
81
  rubygems_version: 1.8.24
47
82
  signing_key:
48
83
  specification_version: 3
49
- summary: Birch is a generic tree implementation for Ruby, in C.
84
+ summary: A basic Ruby tree implementation with an optional C extension for speed.
50
85
  test_files: []