algorithms 0.2.0 → 0.3.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.
@@ -1,4 +1,34 @@
1
1
  $: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
2
2
  require 'algorithms'
3
3
 
4
- # TODO: KD Tree Spec
4
+ describe Containers::KDTree do
5
+ it "should work for a documented example" do
6
+ kdtree = Containers::KDTree.new( {0 => [4, 3], 1 => [3, 0], 2 => [-1, 2], 3 => [6, 4],
7
+ 4 => [3, -5], 5 => [-2, -5] })
8
+ closest_2 = kdtree.find_nearest([0, 0], 2)
9
+ closest_2.should eql([[5, 2], [9, 1]])
10
+ end
11
+
12
+ it "should work for real-life example from facebook puzzle" do
13
+ points = {}
14
+ input = File.open(File.join(File.dirname(__FILE__), 'kd_test_in.txt'), 'r')
15
+
16
+ # Populate points hash
17
+ input.each_line do |line|
18
+ break if line.empty?
19
+ n, x, y = line.split(/\s+/)
20
+ points[n.to_i] = [x.to_f, y.to_f]
21
+ end
22
+
23
+ out = ""
24
+ kdtree = Containers::KDTree.new(points)
25
+ points.sort{ |(k1, v1), (k2, v2)| k1 <=> k2 }.each { |id, point|
26
+ nearest_4 = kdtree.find_nearest(point, 4)
27
+ out << "#{id} #{nearest_4[1..-1].collect{ |n| n[1] }.join(',')}\n"
28
+ }
29
+
30
+ expected = File.read(File.join(File.dirname(__FILE__), 'kd_expected_out.txt'))
31
+ expected.should eql(out)
32
+ end
33
+
34
+ end
@@ -0,0 +1,27 @@
1
+ $: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
2
+ require 'algorithms'
3
+
4
+ describe "map gc mark test" do
5
+ it "should mark ruby object references" do
6
+ anon_key_class = Class.new do
7
+ attr :value
8
+ def initialize(x); @value = x; end
9
+ def <=>(other); value <=> other.value; end
10
+ end
11
+ anon_val_class = Class.new
12
+ @rbtree = Containers::CRBTreeMap.new
13
+ @splaytree = Containers::CSplayTreeMap.new
14
+ 100.times { |x|
15
+ @rbtree[anon_key_class.new(x)] = anon_val_class.new
16
+ @splaytree[anon_key_class.new(x)] = anon_val_class.new
17
+ }
18
+ # Mark and sweep
19
+ ObjectSpace.garbage_collect
20
+ # Check if any instances were swept
21
+ count = 0
22
+ ObjectSpace.each_object(anon_key_class) { |x| count += 1 }
23
+ count.should eql(200)
24
+ ObjectSpace.each_object(anon_val_class) { |x| count += 1 }
25
+ count.should eql(400)
26
+ end
27
+ end
@@ -19,6 +19,10 @@ describe "empty splaytree", :shared => true do
19
19
  it "should return nil for #delete" do
20
20
  @tree.delete(:non_existing).should be_nil
21
21
  end
22
+
23
+ it "should return nil for #get" do
24
+ @tree[4235].should be_nil
25
+ end
22
26
  end
23
27
 
24
28
  describe "non-empty splaytree", :shared => true do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: algorithms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kanwei Li
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-29 00:00:00 -04:00
12
+ date: 2009-11-16 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,15 +22,16 @@ extensions:
22
22
  - ext/containers/rbtree_map/extconf.rb
23
23
  - ext/containers/splaytree_map/extconf.rb
24
24
  extra_rdoc_files:
25
+ - README.markdown
25
26
  - ext/containers/deque/deque.c
26
27
  - ext/containers/deque/extconf.rb
27
28
  - ext/containers/rbtree_map/extconf.rb
28
29
  - ext/containers/rbtree_map/rbtree.c
29
30
  - ext/containers/splaytree_map/extconf.rb
30
31
  - ext/containers/splaytree_map/splaytree.c
32
+ - lib/algorithms.rb
31
33
  - lib/algorithms/search.rb
32
34
  - lib/algorithms/sort.rb
33
- - lib/algorithms.rb
34
35
  - lib/containers/deque.rb
35
36
  - lib/containers/heap.rb
36
37
  - lib/containers/kd_tree.rb
@@ -41,8 +42,11 @@ extra_rdoc_files:
41
42
  - lib/containers/stack.rb
42
43
  - lib/containers/suffix_array.rb
43
44
  - lib/containers/trie.rb
44
- - README.markdown
45
45
  files:
46
+ - History.txt
47
+ - Manifest
48
+ - README.markdown
49
+ - Rakefile
46
50
  - algorithms.gemspec
47
51
  - benchmarks/deque.rb
48
52
  - benchmarks/sorts.rb
@@ -53,10 +57,9 @@ files:
53
57
  - ext/containers/rbtree_map/rbtree.c
54
58
  - ext/containers/splaytree_map/extconf.rb
55
59
  - ext/containers/splaytree_map/splaytree.c
56
- - History.txt
60
+ - lib/algorithms.rb
57
61
  - lib/algorithms/search.rb
58
62
  - lib/algorithms/sort.rb
59
- - lib/algorithms.rb
60
63
  - lib/containers/deque.rb
61
64
  - lib/containers/heap.rb
62
65
  - lib/containers/kd_tree.rb
@@ -67,16 +70,15 @@ files:
67
70
  - lib/containers/stack.rb
68
71
  - lib/containers/suffix_array.rb
69
72
  - lib/containers/trie.rb
70
- - Manifest
71
- - Rakefile
72
- - README.markdown
73
73
  - spec/deque_gc_mark_spec.rb
74
74
  - spec/deque_spec.rb
75
75
  - spec/heap_spec.rb
76
+ - spec/kd_expected_out.txt
77
+ - spec/kd_test_in.txt
76
78
  - spec/kd_tree_spec.rb
79
+ - spec/map_gc_mark_spec.rb
77
80
  - spec/priority_queue_spec.rb
78
81
  - spec/queue_spec.rb
79
- - spec/rb_tree_map_gc_mark_spec.rb
80
82
  - spec/rb_tree_map_spec.rb
81
83
  - spec/search_spec.rb
82
84
  - spec/sort_spec.rb
@@ -1,25 +0,0 @@
1
- $: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
2
- require 'algorithms'
3
-
4
- if defined? Containers::CRBTreeMap
5
- describe "CRBTreeMap" do
6
- it "should mark ruby object references" do
7
- anon_key_class = Class.new do
8
- attr :value
9
- def initialize(x); @value = x; end
10
- def <=>(other); value <=> other.value; end
11
- end
12
- anon_val_class = Class.new
13
- @tree = Containers::CRBTreeMap.new
14
- 100.times { |x| @tree[anon_key_class.new(x)] = anon_val_class.new }
15
- # Mark and sweep
16
- ObjectSpace.garbage_collect
17
- # Check if any instances were swept
18
- count = 0
19
- ObjectSpace.each_object(anon_key_class) { |x| count += 1 }
20
- count.should eql(100)
21
- ObjectSpace.each_object(anon_val_class) { |x| count += 1 }
22
- count.should eql(200)
23
- end
24
- end
25
- end