ds 0.0.1 → 0.0.2
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.
- data/lib/ds.rb +2 -1
- data/lib/ds/ext/ext.rb +36 -0
- data/lib/ds/graphs/graph_as_matrix.rb +2 -2
- data/lib/ds/trees/binary_search_tree.rb +1 -1
- data/lib/ds/trees/trie.rb +67 -0
- data/lib/ds/version.rb +1 -1
- data/test/test_array_x.rb +31 -0
- data/test/test_trie.rb +14 -0
- metadata +9 -6
- data/lib/ext/ext.rb +0 -15
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"
|
data/lib/ds/ext/ext.rb
ADDED
@@ -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
|
@@ -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
|
data/lib/ds/version.rb
CHANGED
@@ -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
|
+
|
data/test/test_trie.rb
ADDED
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:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
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-
|
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.
|
182
|
+
rubygems_version: 1.7.2
|
180
183
|
signing_key:
|
181
184
|
specification_version: 3
|
182
185
|
summary: Some common data structures.
|