algorithms 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/History.txt +139 -2
  2. data/Manifest +31 -8
  3. data/README +90 -0
  4. data/Rakefile +22 -9
  5. data/algorithms.gemspec +28 -101
  6. data/benchmark.rb +29 -27
  7. data/benchmarks/rbench.rb +16 -0
  8. data/benchmarks/rbench/column.rb +26 -0
  9. data/benchmarks/rbench/group.rb +43 -0
  10. data/benchmarks/rbench/report.rb +53 -0
  11. data/benchmarks/rbench/runner.rb +109 -0
  12. data/benchmarks/rbench/summary.rb +51 -0
  13. data/benchmarks/sorts.rb +33 -0
  14. data/ext/containers/bst/bst.c +205 -0
  15. data/ext/containers/{priority_queue → bst}/extconf.rb +1 -1
  16. data/ext/containers/deque/deque.c +233 -0
  17. data/ext/containers/deque/extconf.rb +4 -0
  18. data/ext/containers/tree_map/extconf.rb +1 -1
  19. data/ext/containers/tree_map/rbtree.c +73 -25
  20. data/lib/algorithms.rb +65 -6
  21. data/lib/algorithms/search.rb +84 -0
  22. data/lib/algorithms/sort.rb +238 -0
  23. data/lib/containers/deque.rb +176 -0
  24. data/lib/containers/heap.rb +451 -111
  25. data/lib/containers/kd_tree.rb +87 -0
  26. data/lib/containers/priority_queue.rb +107 -508
  27. data/lib/containers/queue.rb +62 -23
  28. data/lib/containers/rb_tree_map.rb +398 -0
  29. data/lib/containers/splay_tree_map.rb +274 -0
  30. data/lib/containers/stack.rb +59 -21
  31. data/lib/containers/suffix_array.rb +68 -0
  32. data/lib/containers/trie.rb +182 -0
  33. data/lib/graphs/graph.rb +25 -0
  34. data/spec/bst_spec.rb +31 -0
  35. data/spec/deque_spec.rb +108 -0
  36. data/spec/heap_spec.rb +111 -66
  37. data/spec/kd_tree_spec.rb +89 -0
  38. data/spec/priority_queue_spec.rb +71 -27
  39. data/spec/queue_spec.rb +53 -45
  40. data/spec/rb_tree_map_spec.rb +123 -0
  41. data/spec/search_spec.rb +28 -0
  42. data/spec/sort_spec.rb +28 -0
  43. data/spec/splay_tree_map_spec.rb +102 -0
  44. data/spec/stack_spec.rb +56 -49
  45. data/spec/suffix_array_spec.rb +40 -0
  46. data/spec/trie_spec.rb +59 -0
  47. metadata +54 -32
  48. data/README.txt +0 -58
  49. data/ext/containers/priority_queue/priority_queue.c +0 -948
  50. data/ext/containers/tree_map/Rakefile +0 -4
  51. data/lib/containers/hash.rb +0 -0
  52. data/lib/containers/tree_map.rb +0 -265
  53. data/spec/priority_queue_test.rb +0 -371
  54. data/spec/tree_map_spec.rb +0 -99
@@ -1,99 +0,0 @@
1
- require 'lib/algorithms'
2
-
3
- describe "(empty)", :shared => true do
4
- it "should let you put stuff in" do
5
- 100.times { |x| @tree[x] = x }
6
- @tree.size.should eql(100)
7
- end
8
-
9
- it "should return 0 for height" do
10
- @tree.height.should eql(0)
11
- end
12
-
13
- it "should return 0 for size" do
14
- @tree.size.should eql(0)
15
- end
16
-
17
- it "should return nil for #min_key and #max_key" do
18
- @tree.min_key.should eql(nil)
19
- @tree.max_key.should eql(nil)
20
- end
21
-
22
- it "should not delete" do
23
- @tree.delete(:non_existing).should eql(nil)
24
- end
25
- end
26
-
27
- describe "(non-empty)", :shared => true do
28
- before(:each) do
29
- @num_items = 100
30
- @random_array = []
31
- @num_items.times { @random_array << rand(@num_items) }
32
- @random_array.each { |x| @tree[x] = x }
33
- end
34
-
35
- it "should return correct size (uniqify items first)" do
36
- @tree.size.should eql(@random_array.uniq.size)
37
- end
38
-
39
- it "should return correct max and min keys" do
40
- @tree.min_key.should eql(@random_array.min)
41
- @tree.max_key.should eql(@random_array.max)
42
- end
43
-
44
- it "should not #contain? keys it doesn't have" do
45
- @tree.contains?(10000).should eql(false)
46
- end
47
-
48
- it "should #contain? keys it does have" do
49
- @tree.contains?(@random_array[0]).should eql(true)
50
- end
51
-
52
- it "should remove any key" do
53
- random_key = @random_array[rand(@num_items)]
54
- @tree.contains?(random_key).should eql(true)
55
- @tree.delete(random_key).should eql(random_key)
56
- @tree.contains?(random_key).should eql(false)
57
- end
58
-
59
- it "should let you iterate with #each" do
60
- counter = 0
61
- sorted_array = @random_array.uniq.sort
62
- @tree.each do |key, val|
63
- key.should eql(sorted_array[counter])
64
- counter += 1
65
- end
66
- end
67
- end
68
-
69
- describe Containers::CTreeMap do
70
- describe "empty" do
71
- before(:each) do
72
- @tree = Containers::CTreeMap.new
73
- end
74
- it_should_behave_like "(empty)"
75
- end
76
-
77
- describe "full" do
78
- before(:each) do
79
- @tree = Containers::CTreeMap.new
80
- end
81
- it_should_behave_like "(non-empty)"
82
- end
83
- end
84
-
85
- describe Containers::RubyTreeMap do
86
- describe "empty" do
87
- before(:each) do
88
- @tree = Containers::RubyTreeMap.new
89
- end
90
- it_should_behave_like "(empty)"
91
- end
92
-
93
- describe "full" do
94
- before(:each) do
95
- @tree = Containers::RubyTreeMap.new
96
- end
97
- it_should_behave_like "(non-empty)"
98
- end
99
- end