ds 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ds.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require "ds/version"
2
2
 
3
- require "ext/ext"
3
+ require "ds/ext/ext"
4
4
 
5
5
  require "ds/matrixes/expandable_array"
6
6
  require "ds/matrixes/array_2d"
@@ -19,6 +19,7 @@ require "ds/trees/binary_tree"
19
19
  require "ds/trees/binary_search_tree"
20
20
  require "ds/trees/complete_binary_tree"
21
21
  require "ds/trees/binary_heap"
22
+ require "ds/trees/trie"
22
23
 
23
24
  require "ds/lists/list_element"
24
25
  require "ds/lists/list"
@@ -0,0 +1,36 @@
1
+ module DS
2
+ module ArrayX
3
+
4
+ #Checks if array is already sorted.
5
+ def sorted?(order=:any)
6
+ return true if size < 2
7
+
8
+ case order
9
+ when :asc
10
+ (size-2).times{ |i| return false if self[i] > self[i+1] }
11
+ when :desc
12
+ (size-2).times{ |i| return false if self[i] < self[i+1] }
13
+ else
14
+ return (self[0] < self[1])? sorted?(:asc) : sorted?(:desc)
15
+ end
16
+
17
+ true
18
+ end
19
+
20
+ #Pushes element e only if it is not already in the array. Returns index of
21
+ #elemnt e.
22
+ def push_uniq e
23
+ if include? e
24
+ index e
25
+ else
26
+ push e
27
+ size-1
28
+ end
29
+ end
30
+
31
+ #Tail
32
+ def tail
33
+ self[1..-1]
34
+ end
35
+ end
36
+ end
@@ -22,8 +22,8 @@ module DS
22
22
  #Adds new edges to graph.
23
23
  def add_edges(edges)
24
24
  for e in edges
25
- x = @map.push_uniq e.from
26
- y = @map.push_uniq e.to
25
+ x = @map.extend(ArrayX).push_uniq e.from
26
+ y = @map.extend(ArrayX).push_uniq e.to
27
27
 
28
28
  @store[x,y] = e.weight
29
29
  @max = [@max, x, y].max
@@ -25,7 +25,7 @@ module DS
25
25
  def BinarySearchTree.valid?(other)
26
26
  walker = TreeWalker.new(other)
27
27
  walker.traverse(:inorder)
28
- walker.visited.sorted?
28
+ walker.visited.extend(ArrayX).sorted?
29
29
  end
30
30
 
31
31
  end
@@ -0,0 +1,67 @@
1
+ module DS
2
+ class Trie < Tree
3
+
4
+ ALPHABET = %w{- a b c d e f g h i j k l m n o p q r s t u v w x y z}
5
+
6
+ def alphabet
7
+ self.class::ALPHABET
8
+ end
9
+
10
+ def initialize(value=nil)
11
+ @children = Array.new(alphabet.size)
12
+ @data = value
13
+ end
14
+
15
+ def insert(s, value)
16
+ priv_insert(s.scan(/./), value)
17
+ end
18
+
19
+ def find(s)
20
+ priv_find(s.scan(/./))
21
+ end
22
+
23
+ protected
24
+ def key(chr)
25
+ raise ArgumentError, "Argument chr is nil" unless chr
26
+ k=alphabet.index(chr)
27
+ if k.nil?
28
+ raise ArgumentError, "Character not found"
29
+ else
30
+ k
31
+ end
32
+ end
33
+
34
+ def priv_insert(s, value)
35
+ if s.empty?
36
+ @data = value
37
+ else
38
+ index = key(s.first)
39
+ subtree = if @children[index]
40
+ @children[index]
41
+ else
42
+ @children[index] = Trie.new
43
+ end
44
+
45
+ subtree.priv_insert(s[1..-1], value)
46
+ end
47
+ end
48
+
49
+ def priv_find(search)
50
+ if @children[0]
51
+ @children[0].value
52
+ else
53
+ if search.empty?
54
+ @data
55
+ else
56
+ index = key(search.first)
57
+ if @children[index]
58
+ @children[index].priv_find(search[1..-1])
59
+ else
60
+ nil
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -1,3 +1,3 @@
1
1
  module DS
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,31 @@
1
+ require 'help'
2
+
3
+ describe ArrayX do
4
+
5
+ before do
6
+ @asc = [1,3,6,9,10,13].extend(ArrayX)
7
+ @desc = [9,8,7,7,4,3,2].extend(ArrayX)
8
+ @empty = [].extend(ArrayX)
9
+ @one = [3].extend(ArrayX)
10
+ @not_sorted = [3,4,9,1,1,2,8].extend(ArrayX)
11
+ end
12
+
13
+ it "#sorted? should check if array is already sorted." do
14
+ assert @desc.sorted?
15
+ assert @asc.sorted?
16
+ assert @empty.sorted?
17
+ assert @one.sorted?
18
+ refute @not_sorted.sorted?
19
+ end
20
+
21
+ it "#push_uniq should push element only if it is not already in array." do
22
+ @asc.size.must_equal 6
23
+ @asc.push_uniq(3).must_equal 1
24
+ @asc.size.must_equal 6
25
+
26
+ @empty.push_uniq(3).must_equal 0
27
+ @empty.must_equal [3]
28
+ end
29
+ end
30
+
31
+
@@ -0,0 +1,14 @@
1
+ require 'help'
2
+
3
+ describe Trie do
4
+
5
+ before do
6
+ @trie = Trie.new
7
+ end
8
+
9
+ it "#insert should add new string to trie." do
10
+ @trie.insert("thing",true);
11
+ assert @trie.find("thing");
12
+ end
13
+
14
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ds
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - knife
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-07 00:00:00 Z
18
+ date: 2011-09-13 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Data structures (lists,stacks, trees, heaps, graphs..) in pure Ruby.
@@ -107,6 +107,7 @@ files:
107
107
  - doc/rdoc.css
108
108
  - ds.gemspec
109
109
  - lib/ds.rb
110
+ - lib/ds/ext/ext.rb
110
111
  - lib/ds/graphs/digraph.rb
111
112
  - lib/ds/graphs/edge.rb
112
113
  - lib/ds/graphs/graph.rb
@@ -128,10 +129,11 @@ files:
128
129
  - lib/ds/trees/complete_binary_tree.rb
129
130
  - lib/ds/trees/tree.rb
130
131
  - lib/ds/trees/tree_walker.rb
132
+ - lib/ds/trees/trie.rb
131
133
  - lib/ds/version.rb
132
- - lib/ext/ext.rb
133
134
  - test/help.rb
134
135
  - test/test_array2d.rb
136
+ - test/test_array_x.rb
135
137
  - test/test_binary_heap.rb
136
138
  - test/test_binary_search_tree.rb
137
139
  - test/test_binary_tree.rb
@@ -147,6 +149,7 @@ files:
147
149
  - test/test_tree.rb
148
150
  - test/test_tree_walker.rb
149
151
  - test/test_tri_matrix.rb
152
+ - test/test_trie.rb
150
153
  homepage: ""
151
154
  licenses: []
152
155
 
@@ -176,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
179
  requirements: []
177
180
 
178
181
  rubyforge_project: ds
179
- rubygems_version: 1.8.6
182
+ rubygems_version: 1.7.2
180
183
  signing_key:
181
184
  specification_version: 3
182
185
  summary: Some common data structures.
@@ -1,15 +0,0 @@
1
- class Array
2
- def sorted?(order = :growing)
3
- (size-2).times{ |i| return false if self[i] > self[i+1] }
4
- true
5
- end
6
-
7
- def push_uniq e
8
- if include? e
9
- index e
10
- else
11
- push e
12
- size-1
13
- end
14
- end
15
- end