native_btree 0.2.1 → 0.4.0

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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -3
  3. data/CMakeLists.txt +3 -3
  4. data/Gemfile +6 -3
  5. data/README.md +51 -3
  6. data/ext/native_btree/CMakeLists.txt +29 -7
  7. data/ext/native_btree/comparator.c +9 -0
  8. data/ext/native_btree/constructor.c +34 -7
  9. data/ext/native_btree/conversion.c +25 -1
  10. data/ext/native_btree/glib2_68/CMakeLists.txt +3 -0
  11. data/ext/native_btree/glib2_68/additional_iterators.c +84 -0
  12. data/ext/native_btree/glib_module.c +45 -0
  13. data/ext/native_btree/include/common.h +7 -9
  14. data/ext/native_btree/include/comparator.h +3 -0
  15. data/ext/native_btree/include/glib_module.h +6 -0
  16. data/ext/native_btree/include/iterators.h +10 -2
  17. data/ext/native_btree/include/native_btree.h +82 -5
  18. data/ext/native_btree/include/rbtree_type.h +10 -3
  19. data/ext/native_btree/instance.c +15 -3
  20. data/ext/native_btree/iterators.c +55 -1
  21. data/ext/native_btree/native_btree.c +55 -20
  22. data/ext/native_btree/rbtree_type.c +36 -10
  23. data/ext/native_btree/search.c +94 -0
  24. data/lib/native_btree/native_btree.bundle +0 -0
  25. data/lib/native_btree/version.rb +1 -1
  26. data/spec/debug.rb +20 -2
  27. data/spec/native_btree_class_spec.rb +16 -0
  28. data/spec/native_btree_conversion_spec.rb +70 -0
  29. data/spec/native_btree_instance_spec.rb +8 -101
  30. data/spec/native_btree_int_instance_spec.rb +188 -0
  31. data/spec/native_btree_iterators_spec.rb +90 -0
  32. data/spec/native_btree_module_spec.rb +20 -0
  33. data/spec/native_btree_search_spec.rb +210 -0
  34. metadata +12 -8
  35. data/ext/native_btree/Makefile +0 -456
  36. data/ext/native_btree/include/btree.h +0 -63
  37. data/ext/native_btree/include/constructor.h +0 -11
  38. data/ext/native_btree/include/conversion.h +0 -8
  39. data/ext/native_btree/include/instance.h +0 -22
  40. data/lib/native_btree/native_btree.so +0 -0
@@ -0,0 +1,188 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe NativeBtree do
4
+
5
+ describe NativeBtree::Btree do
6
+ let(:tree) do
7
+ described_class.new(described_class::INT_COMPARATOR)
8
+ end
9
+
10
+ describe "#[]= method" do
11
+
12
+ it "respond to" do
13
+ expect(tree).to respond_to('[]=')
14
+ end
15
+
16
+ it 'set key/value pair' do
17
+ tree[2] = 22
18
+ expect(tree[2]).to be 22
19
+ end
20
+
21
+ it "pass key arg and value" do
22
+ expect(tree[:key] = 10).to be 10
23
+ end
24
+
25
+ it "Raise ArgumentError if key was not passed" do
26
+ expect { tree[] = 1 }.to raise_error(ArgumentError)
27
+ end
28
+
29
+ it 'Call comparator while set value' do
30
+ comparator = spy('comparator')
31
+ def comparator.to_proc
32
+ proc do |a, b|
33
+ comparator.call()
34
+ a - b
35
+ end
36
+ end
37
+
38
+ btree = described_class.new(&comparator)
39
+ btree[1] = 1
40
+ btree[2] = 2
41
+ expect(comparator).to have_received(:call)
42
+ end
43
+
44
+ it 'has a set alias' do
45
+ expect(tree).to respond_to('set')
46
+ end
47
+ end
48
+
49
+ describe "#[] method" do
50
+ it "respond to" do
51
+ expect(tree).to respond_to('[]')
52
+ end
53
+
54
+ it 'return expected value if found' do
55
+ tree[3] = 'a'
56
+ tree[4] = 'b'
57
+ expect(tree[3]).to be 'a'
58
+ end
59
+
60
+ it 'return nil if not found' do
61
+ tree[1] = '1'
62
+ tree[5] = '5'
63
+ expect(tree[2]).to be_nil
64
+ end
65
+
66
+ it 'has a get alias' do
67
+ expect(tree).to respond_to('get')
68
+ end
69
+ end
70
+
71
+ describe "#height method" do
72
+ it "respond to" do
73
+ expect(tree).to respond_to(:height)
74
+ end
75
+
76
+ it 'return tree height with items' do
77
+ tree[1] = 11
78
+ tree[2] = 22
79
+ tree[3] = 33
80
+ tree[4] = 44
81
+ expect(tree.height).to be 3
82
+ end
83
+
84
+ it 'return 0 if empty tree' do
85
+ expect(tree.height).to be 0
86
+ end
87
+ end
88
+
89
+ describe "#size method" do
90
+ it "respond to" do
91
+ expect(tree).to respond_to(:size)
92
+ end
93
+
94
+ it 'return count of nodes' do
95
+ tree[1] = 1
96
+ tree[2] = 2
97
+ tree[3] = 3
98
+ expect(tree.size).to be 3
99
+ end
100
+
101
+ it 'return 0 if empty tree' do
102
+ expect(tree.size).to be 0
103
+ end
104
+
105
+ end
106
+
107
+ describe "#delete method" do
108
+ it "respond to" do
109
+ expect(tree).to respond_to(:delete)
110
+ end
111
+
112
+ it 'delete key value pair' do
113
+ tree[2] = 22
114
+ tree[3] = 33
115
+ tree.delete(3)
116
+ expect(tree[3]).to be_nil
117
+ end
118
+
119
+ it "return nil if not found" do
120
+ tree[3] = 33
121
+ expect(tree.delete(4)).to be_nil
122
+ end
123
+
124
+ it "return value if key is found" do
125
+ tree[2] = 22
126
+ tree[3] = 33
127
+ expect(tree.delete(2)).to be 22
128
+ end
129
+
130
+ it "call block with key if not found" do
131
+ tree[2] = 22
132
+ block = ->(key) { "#{key} is not found" }
133
+ expect(tree.delete(7, &block)).to be == "7 is not found"
134
+ end
135
+ end
136
+
137
+ describe "#clear method" do
138
+ it "respond to" do
139
+ expect(tree).to respond_to(:clear)
140
+ end
141
+
142
+ it 'clear tree' do
143
+ tree[1] = 11
144
+ tree[2] = 22
145
+ tree[3] = 33
146
+ expect(tree.size).to be 3
147
+ tree.clear()
148
+ expect(tree.size).to be 0
149
+ end
150
+
151
+ it 'return self' do
152
+ tree[1] = 11
153
+ expect(tree.clear()).to be tree
154
+ end
155
+ end
156
+
157
+ describe "#include? method" do
158
+ it "respond to" do
159
+ expect(tree).to respond_to(:include?)
160
+ end
161
+
162
+ it 'return true is key exists' do
163
+ tree[3] = 33
164
+ expect(tree.include?(3)).to be true
165
+ end
166
+
167
+ it 'return false if key not exists' do
168
+ tree[3] = 33
169
+ expect(tree.include?(4)).to be false
170
+ end
171
+ end
172
+
173
+ describe "#empty? method" do
174
+ it 'respond to' do
175
+ expect(tree).to respond_to(:empty?)
176
+ end
177
+
178
+ it 'return false if nodes exists' do
179
+ tree[3] = 33
180
+ expect(tree.empty?).to be false
181
+ end
182
+
183
+ it 'return true if nodes not exists' do
184
+ expect(tree.empty?).to be true
185
+ end
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe NativeBtree do
4
+
5
+ describe NativeBtree::Btree do
6
+ let(:tree) do
7
+ described_class.new do |a, b|
8
+ a - b
9
+ end
10
+ end
11
+
12
+ describe "#each method" do
13
+ it "respond to" do
14
+ expect(tree).to respond_to(:each)
15
+ end
16
+
17
+ it 'yield in to block value first' do
18
+ tree[2] = 22
19
+
20
+ value = nil
21
+ tree.each { |v| value = v }
22
+
23
+ expect(value).to be 22
24
+ end
25
+
26
+ it 'yield in to block key second' do
27
+ tree[2] = 22
28
+
29
+ key = nil
30
+ tree.each { |_v, k| key = k }
31
+
32
+ expect(key).to be 2
33
+ end
34
+
35
+ it 'yield ordered keys' do
36
+ tree[16] = 16
37
+ tree[0] = 0
38
+ tree[5] = 5
39
+ tree[-4] = -4
40
+ tree[7] = 7
41
+
42
+ check = [-4, 0, 5, 7, 16]
43
+ result = []
44
+ tree.each { |value| result << value }
45
+
46
+ expect(result).to eq(check)
47
+ end
48
+ end
49
+
50
+ describe "#each_key method" do
51
+ it "respond to" do
52
+ expect(tree).to respond_to(:each_key)
53
+ end
54
+
55
+ it 'yield ordered keys' do
56
+ tree[16] = 160
57
+ tree[0] = 0
58
+ tree[5] = 50
59
+ tree[-4] = -40
60
+ tree[7] = 70
61
+
62
+ check = [-4, 0, 5, 7, 16]
63
+ result = []
64
+ tree.each_key { |key| result << key }
65
+
66
+ expect(result).to eq(check)
67
+ end
68
+ end
69
+
70
+ describe "#each_value method" do
71
+ it "respond to" do
72
+ expect(tree).to respond_to(:each_value)
73
+ end
74
+
75
+ it 'yield values by ordered keys' do
76
+ tree[16] = 160
77
+ tree[0] = 0
78
+ tree[5] = 50
79
+ tree[-4] = -40
80
+ tree[7] = 70
81
+
82
+ check = [-40, 0, 50, 70, 160]
83
+ result = []
84
+ tree.each_value { |value| result << value }
85
+
86
+ expect(result).to eq(check)
87
+ end
88
+ end
89
+ end
90
+ end
@@ -12,4 +12,24 @@ RSpec.describe NativeBtree do
12
12
  it "Btree is class" do
13
13
  expect(described_class::Btree.class).to be Class
14
14
  end
15
+
16
+ it "Has Glib contstant" do
17
+ expect(described_class.const_defined?(:Glib)).to be true
18
+ end
19
+
20
+ it "Glib is module" do
21
+ expect(described_class::Glib.class).to be Module
22
+ end
23
+
24
+ it "Has MAJOR_VERSION contstant" do
25
+ expect(described_class::Glib.const_defined?(:MAJOR_VERSION)).to be true
26
+ end
27
+
28
+ it "Has MINOR_VERSION contstant" do
29
+ expect(described_class::Glib.const_defined?(:MINOR_VERSION)).to be true
30
+ end
31
+
32
+ it "Has MICRO_VERSION contstant" do
33
+ expect(described_class::Glib.const_defined?(:MICRO_VERSION)).to be true
34
+ end
15
35
  end
@@ -0,0 +1,210 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe NativeBtree do
4
+
5
+ describe NativeBtree::Btree do
6
+ let(:tree) do
7
+ described_class.new do |a, b|
8
+ a - b
9
+ end
10
+ end
11
+
12
+ describe "#filter method" do
13
+ it "respond to" do
14
+ expect(tree).to respond_to(:filter)
15
+ end
16
+
17
+ it 'raise error if block not given' do
18
+ expect { tree.filter() }.to raise_error(LocalJumpError)
19
+ end
20
+
21
+ it 'return Btree instance' do
22
+ expect(tree.filter() { nil }).to be_kind_of(described_class)
23
+ end
24
+
25
+ it 'filter tree by values' do
26
+ tree[1] = 11
27
+ tree[5] = 90
28
+ tree[2] = 32
29
+ tree[100] = 15
30
+ tree[46] = 8
31
+
32
+ expect(tree.filter { |v| v > 20 }.to_a).to match_array([[2, 32], [5, 90]])
33
+ end
34
+
35
+ it 'filter tree by keys' do
36
+ tree[1] = 11
37
+ tree[5] = 90
38
+ tree[2] = 32
39
+ tree[100] = 15
40
+ tree[46] = 8
41
+
42
+ expect(tree.filter { |_v, k| k > 20 }.to_a).to match_array([[46, 8], [100, 15]])
43
+ end
44
+
45
+ it 'return new object' do
46
+ tree[1] = 11
47
+ tree[5] = 90
48
+ tree[2] = 32
49
+ tree[100] = 15
50
+ tree[46] = 8
51
+
52
+ result = tree.filter { |_v, k| k > 20 }
53
+
54
+ expect(tree.equal?(result)).to be false
55
+ end
56
+
57
+ it 'construct correct tree with int comparator' do
58
+ tree = described_class.new(described_class::INT_COMPARATOR)
59
+
60
+ tree[1] = 11
61
+ tree[5] = 90
62
+ tree[2] = 32
63
+ tree[100] = 15
64
+ tree[46] = 8
65
+
66
+ expect(tree.filter { |v| v > 20 }.to_a).to match_array([[2, 32], [5, 90]])
67
+ end
68
+ end
69
+
70
+ describe "#filter! method" do
71
+ it "respond to" do
72
+ expect(tree).to respond_to(:filter!)
73
+ end
74
+
75
+ it 'raise error if block not given' do
76
+ expect { tree.filter!() }.to raise_error(LocalJumpError)
77
+ end
78
+
79
+ it 'return Btree instance' do
80
+ expect(tree.filter!() { nil }).to be_kind_of(described_class)
81
+ end
82
+
83
+ it 'return origin tree' do
84
+ tree[1] = 11
85
+ tree[5] = 90
86
+ tree[2] = 32
87
+ tree[100] = 15
88
+ tree[46] = 8
89
+
90
+ result = tree.filter! { |_v, k| k > 20 }
91
+
92
+ expect(tree.equal?(result)).to be true
93
+ end
94
+
95
+ it 'filter tree by values' do
96
+ tree[1] = 11
97
+ tree[5] = 90
98
+ tree[2] = 32
99
+ tree[100] = 15
100
+ tree[46] = 8
101
+
102
+ expect(tree.filter! { |v| v > 20 }.to_a)
103
+ .to match_array([[2, 32], [5, 90]])
104
+ end
105
+
106
+ it 'filter tree by keys' do
107
+ tree[1] = 11
108
+ tree[5] = 90
109
+ tree[2] = 32
110
+ tree[100] = 15
111
+ tree[46] = 8
112
+
113
+ expect(tree.filter! { |_v, k| k > 20 }.to_a)
114
+ .to match_array([[46, 8], [100, 15]])
115
+ end
116
+
117
+ end
118
+
119
+ describe "#select method" do
120
+ it "respond to" do
121
+ expect(tree).to respond_to(:select)
122
+ end
123
+ end
124
+
125
+ describe "#select! method" do
126
+ it "respond to" do
127
+ expect(tree).to respond_to(:select!)
128
+ end
129
+ end
130
+
131
+ if NativeBtree::Glib::MAJOR_VERSION == 2 &&
132
+ NativeBtree::Glib::MINOR_VERSION >= 68
133
+
134
+ describe "select_before method" do
135
+ it 'respond_to' do
136
+ expect(tree).to respond_to(:select_before)
137
+ end
138
+
139
+ it 'return expected collection' do
140
+ tree[1] = 11
141
+ tree[2] = 32
142
+ tree[5] = 90
143
+ tree[46] = 8
144
+ tree[100] = 15
145
+
146
+ expect(tree.select_before(5).to_a)
147
+ .to match_array([[1, 11], [2, 32], [5, 90]])
148
+ end
149
+
150
+ it 'return correct tree if one target' do
151
+ tree[100] = 15
152
+ tree[46] = 8
153
+ tree[5] = 90
154
+
155
+ expect(tree.select_before(5).to_a)
156
+ .to match_array([[5, 90]])
157
+ end
158
+
159
+ it 'return empty tree if no targets' do
160
+ tree[100] = 15
161
+ tree[46] = 8
162
+
163
+ expect(tree.select_before(5).to_a)
164
+ .to match_array([])
165
+ end
166
+ end
167
+
168
+ describe "select_after" do
169
+ it "respond to" do
170
+ expect(tree).to respond_to(:select_after)
171
+ end
172
+
173
+ it 'return expected collection' do
174
+ tree[1] = 11
175
+ tree[5] = 90
176
+ tree[2] = 32
177
+ tree[100] = 15
178
+ tree[46] = 8
179
+
180
+ expect(tree.select_after(5).to_a)
181
+ .to match_array([[5, 90], [46, 8], [100, 15]])
182
+ end
183
+
184
+ it 'return correct tree if one target' do
185
+ tree[3] = 15
186
+ tree[4] = 8
187
+ tree[5] = 90
188
+
189
+ expect(tree.select_after(5).to_a)
190
+ .to match_array([[5, 90]])
191
+ end
192
+
193
+ it 'return empty tree if no targets' do
194
+ tree[1] = 11
195
+ tree[2] = 32
196
+
197
+ expect(tree.select_after(5).to_a)
198
+ .to match_array([])
199
+ end
200
+ end
201
+
202
+ describe "select_between" do
203
+ xit "respond to" do
204
+ expect(tree).to respond_to(:select_between)
205
+ end
206
+ end
207
+
208
+ end
209
+ end
210
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: native_btree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Feodorov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-07 00:00:00.000000000 Z
11
+ date: 2022-09-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby bindings to GTree balanced binary tree from GLib library.
14
14
  email:
@@ -25,17 +25,16 @@ files:
25
25
  - README.md
26
26
  - Rakefile
27
27
  - ext/native_btree/CMakeLists.txt
28
- - ext/native_btree/Makefile
29
28
  - ext/native_btree/comparator.c
30
29
  - ext/native_btree/constructor.c
31
30
  - ext/native_btree/conversion.c
32
31
  - ext/native_btree/extconf_cmake.rb
33
- - ext/native_btree/include/btree.h
32
+ - ext/native_btree/glib2_68/CMakeLists.txt
33
+ - ext/native_btree/glib2_68/additional_iterators.c
34
+ - ext/native_btree/glib_module.c
34
35
  - ext/native_btree/include/common.h
35
36
  - ext/native_btree/include/comparator.h
36
- - ext/native_btree/include/constructor.h
37
- - ext/native_btree/include/conversion.h
38
- - ext/native_btree/include/instance.h
37
+ - ext/native_btree/include/glib_module.h
39
38
  - ext/native_btree/include/iterators.h
40
39
  - ext/native_btree/include/native_btree.h
41
40
  - ext/native_btree/include/rbtree_type.h
@@ -43,14 +42,19 @@ files:
43
42
  - ext/native_btree/iterators.c
44
43
  - ext/native_btree/native_btree.c
45
44
  - ext/native_btree/rbtree_type.c
45
+ - ext/native_btree/search.c
46
46
  - lib/native_btree.rb
47
- - lib/native_btree/native_btree.so
47
+ - lib/native_btree/native_btree.bundle
48
48
  - lib/native_btree/version.rb
49
49
  - native_btree.gemspec
50
50
  - spec/debug.rb
51
51
  - spec/native_btree_class_spec.rb
52
+ - spec/native_btree_conversion_spec.rb
52
53
  - spec/native_btree_instance_spec.rb
54
+ - spec/native_btree_int_instance_spec.rb
55
+ - spec/native_btree_iterators_spec.rb
53
56
  - spec/native_btree_module_spec.rb
57
+ - spec/native_btree_search_spec.rb
54
58
  - spec/spec_helper.rb
55
59
  homepage: https://github.com/unixs/ruby-native-btree
56
60
  licenses: