compsci 0.0.3.1 → 0.1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +63 -14
- data/VERSION +1 -1
- data/compsci.gemspec +5 -22
- data/examples/binary_tree.rb +11 -6
- data/examples/complete_tree.rb +47 -0
- data/examples/heap.rb +19 -7
- data/examples/tree.rb +41 -0
- data/lib/compsci.rb +1 -2
- data/lib/compsci/complete_tree.rb +109 -0
- data/lib/compsci/fibonacci.rb +31 -30
- data/lib/compsci/fit.rb +135 -135
- data/lib/compsci/heap.rb +14 -108
- data/lib/compsci/names.rb +132 -0
- data/lib/compsci/node.rb +67 -0
- data/lib/compsci/timer.rb +30 -29
- data/lib/compsci/tree.rb +48 -138
- data/test/bench/tree.rb +2 -2
- data/test/complete_tree.rb +131 -0
- data/test/heap.rb +121 -34
- data/test/names.rb +96 -0
- data/test/node.rb +89 -0
- data/test/tree.rb +49 -237
- metadata +24 -2
data/test/tree.rb
CHANGED
@@ -3,69 +3,6 @@ require 'minitest/autorun'
|
|
3
3
|
|
4
4
|
include CompSci
|
5
5
|
|
6
|
-
describe Node do
|
7
|
-
before do
|
8
|
-
@martin_sheen = Node.new 'martin'
|
9
|
-
@charlie_sheen = Node.new 'charlie'
|
10
|
-
@emilio_estevez = Node.new 'emilio'
|
11
|
-
end
|
12
|
-
|
13
|
-
it "must start with no children" do
|
14
|
-
[@martin_sheen, @charlie_sheen, @emilio_estevez].each { |n|
|
15
|
-
n.children.must_be_empty
|
16
|
-
}
|
17
|
-
end
|
18
|
-
|
19
|
-
it "must track children" do
|
20
|
-
@charlie_sheen.add_parent(@martin_sheen)
|
21
|
-
@martin_sheen.children.must_include @charlie_sheen
|
22
|
-
|
23
|
-
@martin_sheen.children.wont_include @emilio_estevez
|
24
|
-
@martin_sheen.add_child @emilio_estevez
|
25
|
-
@martin_sheen.children.must_include @emilio_estevez
|
26
|
-
end
|
27
|
-
|
28
|
-
it "must create children from scalars" do
|
29
|
-
@martin_sheen.new_child 'fake_emilio'
|
30
|
-
@martin_sheen.children.size.must_equal 1
|
31
|
-
@martin_sheen.children.first.value.must_equal 'fake_emilio'
|
32
|
-
@martin_sheen.children.wont_include @emilio_estevez
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
describe ChildNode do
|
37
|
-
before do
|
38
|
-
@martin_sheen = ChildNode.new 'martin'
|
39
|
-
@charlie_sheen = ChildNode.new 'charlie'
|
40
|
-
@emilio_estevez = ChildNode.new 'emilio'
|
41
|
-
end
|
42
|
-
|
43
|
-
it "must start with neither parent nor children" do
|
44
|
-
[@martin_sheen, @charlie_sheen, @emilio_estevez].each { |n|
|
45
|
-
n.parent.nil?.must_equal true
|
46
|
-
n.children.must_be_empty
|
47
|
-
}
|
48
|
-
end
|
49
|
-
|
50
|
-
it "must track parent and children" do
|
51
|
-
@charlie_sheen.add_parent(@martin_sheen)
|
52
|
-
@charlie_sheen.parent.must_equal @martin_sheen
|
53
|
-
@martin_sheen.children.must_include @charlie_sheen
|
54
|
-
|
55
|
-
@martin_sheen.children.wont_include @emilio_estevez
|
56
|
-
@martin_sheen.add_child @emilio_estevez
|
57
|
-
@martin_sheen.children.must_include @emilio_estevez
|
58
|
-
@emilio_estevez.parent.must_equal @martin_sheen
|
59
|
-
end
|
60
|
-
|
61
|
-
it "must create children from scalars" do
|
62
|
-
@martin_sheen.new_child 'fake_emilio'
|
63
|
-
@martin_sheen.children.size.must_equal 1
|
64
|
-
@martin_sheen.children.first.value.must_equal 'fake_emilio'
|
65
|
-
@martin_sheen.children.wont_include @emilio_estevez
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
6
|
describe Tree do
|
70
7
|
before do
|
71
8
|
@tree = Tree.new(Node, 42)
|
@@ -127,25 +64,64 @@ describe Tree do
|
|
127
64
|
end
|
128
65
|
|
129
66
|
describe NaryTree do
|
67
|
+
describe "with Node" do
|
68
|
+
before do
|
69
|
+
@tree = NaryTree.new(Node, 42, child_slots: 3)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "must have an open parent" do
|
73
|
+
@tree.open_parent?(@tree.root).must_equal true
|
74
|
+
@tree.open_parent?(@tree.open_parent).must_equal true
|
75
|
+
end
|
76
|
+
|
77
|
+
it "must push a value onto an open parent" do
|
78
|
+
op = @tree.open_parent
|
79
|
+
opc = op.children.size
|
80
|
+
@tree.push 5
|
81
|
+
@tree.open_parent.children.size.must_equal opc + 1
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "with ChildNode" do
|
86
|
+
before do
|
87
|
+
@tree = NaryTree.new(ChildNode, 42, child_slots: 4)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "must have an open parent" do
|
91
|
+
@tree.open_parent?(@tree.root).must_equal true
|
92
|
+
@tree.open_parent?(@tree.open_parent).must_equal true
|
93
|
+
end
|
94
|
+
|
95
|
+
it "must push a value onto an open parent" do
|
96
|
+
op = @tree.open_parent
|
97
|
+
opc = op.children.size
|
98
|
+
@tree.push 5
|
99
|
+
@tree.open_parent.children.size.must_equal opc + 1
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "BinaryTree" do
|
130
105
|
before do
|
131
|
-
@tree =
|
106
|
+
@tree = BinaryTree.new(Node, 42)
|
132
107
|
end
|
133
108
|
|
134
|
-
it "must have
|
135
|
-
@tree.
|
136
|
-
@tree.open_parent?(@tree.open_parent).must_equal true
|
109
|
+
it "must have 2 child_slots" do
|
110
|
+
@tree.child_slots.must_equal 2
|
137
111
|
end
|
138
112
|
|
139
|
-
it "must
|
140
|
-
|
141
|
-
|
142
|
-
@tree.push
|
143
|
-
@tree.
|
113
|
+
it "must to_s" do
|
114
|
+
item_count = 31
|
115
|
+
# tree already has a root node
|
116
|
+
(item_count - 1).times { @tree.push rand 99 }
|
117
|
+
str = @tree.to_s
|
118
|
+
line_count = str.split("\n").size
|
119
|
+
line_count.must_equal Math.log(item_count + 1, 2).ceil
|
144
120
|
end
|
145
121
|
|
146
122
|
describe "searching" do
|
147
123
|
before do
|
148
|
-
@tree = NaryTree.new(
|
124
|
+
@tree = NaryTree.new(Node, 42, child_slots: 2)
|
149
125
|
99.times { |i| @tree.push i }
|
150
126
|
end
|
151
127
|
|
@@ -198,167 +174,3 @@ describe NaryTree do
|
|
198
174
|
end
|
199
175
|
end
|
200
176
|
end
|
201
|
-
|
202
|
-
describe BinaryTree do
|
203
|
-
before do
|
204
|
-
@tree = BinaryTree.new(ChildNode, 42)
|
205
|
-
end
|
206
|
-
|
207
|
-
it "must have 2 child_slots" do
|
208
|
-
@tree.child_slots.must_equal 2
|
209
|
-
end
|
210
|
-
|
211
|
-
it "must to_s" do
|
212
|
-
item_count = 31
|
213
|
-
# tree already has a root node
|
214
|
-
(item_count - 1).times { @tree.push rand 99 }
|
215
|
-
str = @tree.to_s
|
216
|
-
line_count = str.split("\n").size
|
217
|
-
line_count.must_equal Math.log(item_count + 1, 2).ceil
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
describe CompleteBinaryTree do
|
222
|
-
it "must calculate a parent index" do
|
223
|
-
valid = {
|
224
|
-
1 => 0,
|
225
|
-
2 => 0,
|
226
|
-
3 => 1,
|
227
|
-
4 => 1,
|
228
|
-
5 => 2,
|
229
|
-
6 => 2,
|
230
|
-
7 => 3,
|
231
|
-
8 => 3,
|
232
|
-
9 => 4,
|
233
|
-
10 => 4,
|
234
|
-
}
|
235
|
-
|
236
|
-
invalid = {
|
237
|
-
0 => -1,
|
238
|
-
-1 => -1,
|
239
|
-
-2 => -2,
|
240
|
-
}
|
241
|
-
valid.each { |idx, pidx|
|
242
|
-
CompleteBinaryTree.parent_idx(idx).must_equal pidx
|
243
|
-
}
|
244
|
-
invalid.each { |idx, pidx|
|
245
|
-
CompleteBinaryTree.parent_idx(idx).must_equal pidx
|
246
|
-
}
|
247
|
-
end
|
248
|
-
|
249
|
-
it "must calculate children indices" do
|
250
|
-
valid = {
|
251
|
-
0 => [1, 2],
|
252
|
-
1 => [3, 4],
|
253
|
-
2 => [5, 6],
|
254
|
-
3 => [7, 8],
|
255
|
-
4 => [9, 10],
|
256
|
-
5 => [11, 12],
|
257
|
-
6 => [13, 14],
|
258
|
-
7 => [15, 16],
|
259
|
-
8 => [17, 18],
|
260
|
-
9 => [19, 20],
|
261
|
-
10 => [21, 22],
|
262
|
-
}
|
263
|
-
|
264
|
-
invalid = {
|
265
|
-
-3 => [-5, -4],
|
266
|
-
-2 => [-3, -2],
|
267
|
-
-1 => [-1, 0],
|
268
|
-
}
|
269
|
-
|
270
|
-
valid.each { |idx, cidx|
|
271
|
-
CompleteBinaryTree.children_idx(idx).must_equal cidx
|
272
|
-
}
|
273
|
-
invalid.each { |idx, cidx|
|
274
|
-
CompleteBinaryTree.children_idx(idx).must_equal cidx
|
275
|
-
}
|
276
|
-
end
|
277
|
-
|
278
|
-
describe "instance" do
|
279
|
-
before do
|
280
|
-
@array = (0..99).sort_by { rand }
|
281
|
-
@empty = CompleteBinaryTree.new
|
282
|
-
@nonempty = CompleteBinaryTree.new(store: @array)
|
283
|
-
end
|
284
|
-
|
285
|
-
it "must have a size" do
|
286
|
-
@empty.size.must_equal 0
|
287
|
-
@nonempty.size.must_equal @array.size
|
288
|
-
end
|
289
|
-
|
290
|
-
it "must have a last_idx, nil when empty" do
|
291
|
-
@empty.last_idx.nil?.must_equal true
|
292
|
-
@nonempty.last_idx.must_equal 99
|
293
|
-
end
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
describe CompleteNaryTree do
|
298
|
-
it "must calculate a parent index for N=3" do
|
299
|
-
valid = {
|
300
|
-
1 => 0,
|
301
|
-
2 => 0,
|
302
|
-
3 => 0,
|
303
|
-
4 => 1,
|
304
|
-
5 => 1,
|
305
|
-
6 => 1,
|
306
|
-
7 => 2,
|
307
|
-
8 => 2,
|
308
|
-
9 => 2,
|
309
|
-
10 => 3,
|
310
|
-
}
|
311
|
-
|
312
|
-
invalid = {
|
313
|
-
0 => -1,
|
314
|
-
-1 => -1,
|
315
|
-
-2 => -1,
|
316
|
-
}
|
317
|
-
valid.each { |idx, pidx|
|
318
|
-
CompleteNaryTree.parent_idx(idx, 3).must_equal pidx
|
319
|
-
}
|
320
|
-
invalid.each { |idx, pidx|
|
321
|
-
CompleteNaryTree.parent_idx(idx, 3).must_equal pidx
|
322
|
-
}
|
323
|
-
end
|
324
|
-
|
325
|
-
it "must calculate children indices for N=3" do
|
326
|
-
valid = {
|
327
|
-
0 => [1, 2, 3],
|
328
|
-
1 => [4, 5, 6],
|
329
|
-
2 => [7, 8, 9],
|
330
|
-
3 => [10, 11, 12],
|
331
|
-
}
|
332
|
-
|
333
|
-
invalid = {
|
334
|
-
-3 => [-8, -7, -6],
|
335
|
-
-2 => [-5, -4, -3],
|
336
|
-
-1 => [-2, -1, 0],
|
337
|
-
}
|
338
|
-
|
339
|
-
valid.each { |idx, cidx|
|
340
|
-
CompleteNaryTree.children_idx(idx, 3).must_equal cidx
|
341
|
-
}
|
342
|
-
invalid.each { |idx, cidx|
|
343
|
-
CompleteNaryTree.children_idx(idx, 3).must_equal cidx
|
344
|
-
}
|
345
|
-
end
|
346
|
-
|
347
|
-
describe "instance" do
|
348
|
-
before do
|
349
|
-
@array = (0..99).sort_by { rand }
|
350
|
-
@empty = CompleteNaryTree.new(child_slots: 5)
|
351
|
-
@nonempty = CompleteNaryTree.new(store: @array, child_slots: 3)
|
352
|
-
end
|
353
|
-
|
354
|
-
it "must have a size" do
|
355
|
-
@empty.size.must_equal 0
|
356
|
-
@nonempty.size.must_equal @array.size
|
357
|
-
end
|
358
|
-
|
359
|
-
it "must have a last_idx, nil when empty" do
|
360
|
-
@empty.last_idx.nil?.must_equal true
|
361
|
-
@nonempty.last_idx.must_equal 99
|
362
|
-
end
|
363
|
-
end
|
364
|
-
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: compsci
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rick Hull
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
description: Trees, Heaps, Timers, Error fitting, etc
|
28
42
|
email:
|
29
43
|
executables: []
|
@@ -35,19 +49,27 @@ files:
|
|
35
49
|
- VERSION
|
36
50
|
- compsci.gemspec
|
37
51
|
- examples/binary_tree.rb
|
52
|
+
- examples/complete_tree.rb
|
38
53
|
- examples/heap.rb
|
54
|
+
- examples/tree.rb
|
39
55
|
- lib/compsci.rb
|
56
|
+
- lib/compsci/complete_tree.rb
|
40
57
|
- lib/compsci/fibonacci.rb
|
41
58
|
- lib/compsci/fit.rb
|
42
59
|
- lib/compsci/heap.rb
|
60
|
+
- lib/compsci/names.rb
|
61
|
+
- lib/compsci/node.rb
|
43
62
|
- lib/compsci/timer.rb
|
44
63
|
- lib/compsci/tree.rb
|
45
64
|
- test/bench/fibonacci.rb
|
46
65
|
- test/bench/heap.rb
|
47
66
|
- test/bench/tree.rb
|
67
|
+
- test/complete_tree.rb
|
48
68
|
- test/fibonacci.rb
|
49
69
|
- test/fit.rb
|
50
70
|
- test/heap.rb
|
71
|
+
- test/names.rb
|
72
|
+
- test/node.rb
|
51
73
|
- test/timer.rb
|
52
74
|
- test/tree.rb
|
53
75
|
homepage: https://github.com/rickhull/compsci
|