ds 0.0.2 → 0.0.3
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/.gitignore +2 -0
- data/README.rdoc +490 -0
- data/Rakefile +1 -1
- data/lib/ds.rb +17 -19
- data/lib/ds/ext/array_x.rb +35 -0
- data/lib/ds/graphs/graph.rb +36 -32
- data/lib/ds/graphs/graph_as_matrix.rb +22 -23
- data/lib/ds/graphs/graph_as_tri_matrix.rb +1 -2
- data/lib/ds/lists/list.rb +18 -21
- data/lib/ds/matrixes/array_2d.rb +4 -0
- data/lib/ds/pair.rb +14 -0
- data/lib/ds/queues/priority_queue.rb +36 -0
- data/lib/ds/queues/queue.rb +1 -1
- data/lib/ds/sets/ordered_set.rb +32 -0
- data/lib/ds/trees/binary_heap.rb +45 -14
- data/lib/ds/trees/binary_search_tree.rb +3 -1
- data/lib/ds/trees/binary_tree.rb +11 -5
- data/lib/ds/trees/complete_binary_tree.rb +24 -16
- data/lib/ds/trees/tree.rb +3 -3
- data/lib/ds/trees/tree_walker.rb +7 -6
- data/lib/ds/trees/trie.rb +14 -20
- data/lib/ds/version.rb +1 -1
- data/test/help.rb +12 -3
- data/test/test_array2d.rb +14 -4
- data/test/test_array_x.rb +27 -7
- data/test/test_binary_heap.rb +19 -1
- data/test/test_binary_search_tree.rb +8 -1
- data/test/test_binary_tree.rb +7 -1
- data/test/test_complete_binary_tree.rb +34 -6
- data/test/test_digraph.rb +1 -1
- data/test/test_graph.rb +10 -1
- data/test/test_list.rb +59 -2
- data/test/test_ordered_set.rb +28 -0
- data/test/test_pair.rb +26 -0
- data/test/test_priority_queue.rb +134 -0
- data/test/test_queue.rb +3 -3
- data/test/test_stack.rb +0 -2
- data/test/test_tree.rb +32 -9
- data/test/test_tree_walker.rb +22 -0
- data/test/test_trie.rb +20 -2
- metadata +13 -81
- data/doc/Array.html +0 -264
- data/doc/DS.html +0 -292
- data/doc/DS/Array2D.html +0 -345
- data/doc/DS/BinaryHeap.html +0 -493
- data/doc/DS/BinarySearchTree.html +0 -313
- data/doc/DS/BinaryTree.html +0 -433
- data/doc/DS/CompleteBinaryTree.html +0 -550
- data/doc/DS/CyclicList.html +0 -234
- data/doc/DS/Digraph.html +0 -299
- data/doc/DS/Edge.html +0 -283
- data/doc/DS/ExpandableArray.html +0 -316
- data/doc/DS/Graph.html +0 -739
- data/doc/DS/GraphAsList.html +0 -361
- data/doc/DS/GraphAsMatrix.html +0 -633
- data/doc/DS/GraphAsTriMatrix.html +0 -274
- data/doc/DS/List.html +0 -1263
- data/doc/DS/ListElement.html +0 -344
- data/doc/DS/Queue.html +0 -517
- data/doc/DS/Ring.html +0 -323
- data/doc/DS/Stack.html +0 -407
- data/doc/DS/Tree.html +0 -770
- data/doc/DS/TreeWalker.html +0 -561
- data/doc/DS/TriMatrix.html +0 -338
- data/doc/created.rid +0 -25
- data/doc/ds/graphs/digraph_rb.html +0 -52
- data/doc/ds/graphs/edge_rb.html +0 -52
- data/doc/ds/graphs/graph_as_list_rb.html +0 -52
- data/doc/ds/graphs/graph_as_matrix_rb.html +0 -52
- data/doc/ds/graphs/graph_as_tri_matrix_rb.html +0 -52
- data/doc/ds/graphs/graph_rb.html +0 -52
- data/doc/ds/lists/cyclic_list_rb.html +0 -52
- data/doc/ds/lists/list_element_rb.html +0 -52
- data/doc/ds/lists/list_rb.html +0 -52
- data/doc/ds/lists/ring_rb.html +0 -52
- data/doc/ds/matrixes/array_2d_rb.html +0 -52
- data/doc/ds/matrixes/expandable_array_rb.html +0 -52
- data/doc/ds/matrixes/tri_matrix_rb.html +0 -52
- data/doc/ds/queues/queue_rb.html +0 -52
- data/doc/ds/stacks/stack_rb.html +0 -52
- data/doc/ds/trees/binary_heap_rb.html +0 -52
- data/doc/ds/trees/binary_search_tree_rb.html +0 -52
- data/doc/ds/trees/binary_tree_rb.html +0 -52
- data/doc/ds/trees/complete_binary_tree_rb.html +0 -52
- data/doc/ds/trees/tree_rb.html +0 -52
- data/doc/ds/trees/tree_walker_rb.html +0 -52
- data/doc/ds/version_rb.html +0 -52
- data/doc/ds_rb.html +0 -98
- data/doc/ext/ext_rb.html +0 -52
- data/doc/images/brick.png +0 -0
- data/doc/images/brick_link.png +0 -0
- data/doc/images/bug.png +0 -0
- data/doc/images/bullet_black.png +0 -0
- data/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/images/date.png +0 -0
- data/doc/images/find.png +0 -0
- data/doc/images/loadingAnimation.gif +0 -0
- data/doc/images/macFFBgHack.png +0 -0
- data/doc/images/package.png +0 -0
- data/doc/images/page_green.png +0 -0
- data/doc/images/page_white_text.png +0 -0
- data/doc/images/page_white_width.png +0 -0
- data/doc/images/plugin.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tag_green.png +0 -0
- data/doc/images/wrench.png +0 -0
- data/doc/images/wrench_orange.png +0 -0
- data/doc/images/zoom.png +0 -0
- data/doc/index.html +0 -375
- data/doc/js/darkfish.js +0 -116
- data/doc/js/jquery.js +0 -32
- data/doc/js/quicksearch.js +0 -114
- data/doc/js/thickbox-compressed.js +0 -10
- data/doc/rdoc.css +0 -763
- data/lib/ds/ext/ext.rb +0 -36
- data/lib/ds/graphs/graph_as_list.rb +0 -48
data/Rakefile
CHANGED
data/lib/ds.rb
CHANGED
|
@@ -1,25 +1,13 @@
|
|
|
1
1
|
require "ds/version"
|
|
2
2
|
|
|
3
|
-
require "ds/ext/
|
|
3
|
+
require "ds/ext/array_x"
|
|
4
|
+
require "ds/pair"
|
|
4
5
|
|
|
5
6
|
require "ds/matrixes/expandable_array"
|
|
6
7
|
require "ds/matrixes/array_2d"
|
|
7
8
|
require "ds/matrixes/tri_matrix"
|
|
8
9
|
|
|
9
|
-
require "ds/
|
|
10
|
-
require "ds/graphs/graph_as_list"
|
|
11
|
-
require "ds/graphs/graph_as_matrix"
|
|
12
|
-
require "ds/graphs/graph_as_tri_matrix"
|
|
13
|
-
require "ds/graphs/graph"
|
|
14
|
-
require "ds/graphs/digraph"
|
|
15
|
-
|
|
16
|
-
require "ds/trees/tree"
|
|
17
|
-
require "ds/trees/tree_walker"
|
|
18
|
-
require "ds/trees/binary_tree"
|
|
19
|
-
require "ds/trees/binary_search_tree"
|
|
20
|
-
require "ds/trees/complete_binary_tree"
|
|
21
|
-
require "ds/trees/binary_heap"
|
|
22
|
-
require "ds/trees/trie"
|
|
10
|
+
require "ds/sets/ordered_set"
|
|
23
11
|
|
|
24
12
|
require "ds/lists/list_element"
|
|
25
13
|
require "ds/lists/list"
|
|
@@ -28,12 +16,22 @@ require "ds/lists/ring"
|
|
|
28
16
|
|
|
29
17
|
require "ds/stacks/stack"
|
|
30
18
|
require "ds/queues/queue"
|
|
19
|
+
require "ds/queues/priority_queue"
|
|
31
20
|
|
|
21
|
+
require "ds/trees/tree"
|
|
22
|
+
require "ds/trees/tree_walker"
|
|
23
|
+
require "ds/trees/binary_tree"
|
|
24
|
+
require "ds/trees/binary_search_tree"
|
|
25
|
+
require "ds/trees/complete_binary_tree"
|
|
26
|
+
require "ds/trees/binary_heap"
|
|
27
|
+
require "ds/trees/trie"
|
|
32
28
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
require "ds/graphs/edge"
|
|
30
|
+
require "ds/graphs/graph_as_list"
|
|
31
|
+
require "ds/graphs/graph_as_matrix"
|
|
32
|
+
require "ds/graphs/graph_as_tri_matrix"
|
|
33
|
+
require "ds/graphs/graph"
|
|
34
|
+
require "ds/graphs/digraph"
|
|
37
35
|
|
|
38
36
|
|
|
39
37
|
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
if size == 2
|
|
9
|
+
return false if self[0] > self[1] and order == :asc
|
|
10
|
+
return false if self[0] < self[1] and order == :desc
|
|
11
|
+
else
|
|
12
|
+
case order
|
|
13
|
+
when :asc
|
|
14
|
+
(size-2).times{ |i| return false if self[i] > self[i+1] }
|
|
15
|
+
when :desc
|
|
16
|
+
(size-2).times{ |i| return false if self[i] < self[i+1] }
|
|
17
|
+
else
|
|
18
|
+
return (self[0] < self[1])? sorted?(:asc) : sorted?(:desc)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
true
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
#Checks if array has duplicates.
|
|
26
|
+
def duplications?
|
|
27
|
+
size != uniq.size
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
#Returns array tail.
|
|
31
|
+
def tail
|
|
32
|
+
self[1..-1]
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
data/lib/ds/graphs/graph.rb
CHANGED
|
@@ -6,14 +6,12 @@ module DS
|
|
|
6
6
|
#Create new graph from array of edges. Second parameter determines
|
|
7
7
|
#graph internal implementation: :list (Adjency List), :tri_matrix (Triangular
|
|
8
8
|
#Matrix), :matrix (Matrix).
|
|
9
|
-
def initialize(edges,store = :
|
|
9
|
+
def initialize(edges,store = :matrix)
|
|
10
10
|
case store
|
|
11
11
|
when :matrix
|
|
12
12
|
@g = GraphAsMatrix.new(edges)
|
|
13
13
|
when :tri_matrix
|
|
14
14
|
@g = GraphAsTriMatrix.new(edges)
|
|
15
|
-
else
|
|
16
|
-
@g = GraphAsList.new(edges)
|
|
17
15
|
end
|
|
18
16
|
end
|
|
19
17
|
|
|
@@ -23,14 +21,47 @@ module DS
|
|
|
23
21
|
new(edges,:tri_matrix)
|
|
24
22
|
end
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
#Adds new edge to graph.
|
|
25
|
+
def add(x,y,weight=1)
|
|
26
|
+
e = Edge.new(x,y,weight)
|
|
27
|
+
add_edges([e])
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
#Adds new edges to graph.
|
|
31
|
+
def add_edges(edges)
|
|
32
|
+
@g.add_edges(edges)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
#Removes conection between vertex x and y.
|
|
30
37
|
def remove(x,y)
|
|
31
38
|
@g.remove(x,y)
|
|
32
39
|
end
|
|
33
40
|
|
|
41
|
+
#Returns all neighbors for given vertex.
|
|
42
|
+
def neighbors v
|
|
43
|
+
@g.neighbors v
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
#Returns vertex degree.
|
|
47
|
+
def degree x
|
|
48
|
+
@g.degree x
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
#Checks if two elements are connected.
|
|
52
|
+
def edge? x,y
|
|
53
|
+
@g.edge? x,y
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
#Returns Edge(x,y) if exist.
|
|
57
|
+
def get_edge x, y
|
|
58
|
+
@g.get_edge x,y
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def vertex_size
|
|
62
|
+
@g.vertex_size
|
|
63
|
+
end
|
|
64
|
+
|
|
34
65
|
def each_vertex &block
|
|
35
66
|
@g.each_vertex &block
|
|
36
67
|
end
|
|
@@ -39,9 +70,6 @@ module DS
|
|
|
39
70
|
@g.each_edge &block
|
|
40
71
|
end
|
|
41
72
|
|
|
42
|
-
def neighbors v
|
|
43
|
-
@g.neighbors v
|
|
44
|
-
end
|
|
45
73
|
|
|
46
74
|
def bfs s
|
|
47
75
|
colors = {}
|
|
@@ -78,30 +106,6 @@ module DS
|
|
|
78
106
|
res
|
|
79
107
|
end
|
|
80
108
|
|
|
81
|
-
def add_edges(edges)
|
|
82
|
-
@g.add_edges(edges)
|
|
83
|
-
end
|
|
84
109
|
|
|
85
|
-
#Returns vertex degree. Second parameter determines direction - :in incoming
|
|
86
|
-
#edges, :out - outcoming edges, :all - incoming and outcoming edges.
|
|
87
|
-
def degree x
|
|
88
|
-
@g.degree x
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def edge? x,y
|
|
92
|
-
@g.edge? x,y
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
def get_edge x, y
|
|
96
|
-
@g.get_edge x,y
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
def vertex_size
|
|
100
|
-
@g.vertex_size
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
def edge_size
|
|
104
|
-
@g.vmax+1
|
|
105
|
-
end
|
|
106
110
|
end
|
|
107
111
|
end
|
|
@@ -5,38 +5,31 @@ module DS
|
|
|
5
5
|
@store = Array2D.new
|
|
6
6
|
@max = 0
|
|
7
7
|
|
|
8
|
-
@
|
|
9
|
-
|
|
10
|
-
@map = [] #maps objects to matrix indexes.
|
|
8
|
+
@map = OrderedSet.new #maps objects to matrix indexes.
|
|
11
9
|
|
|
12
10
|
add_edges(edges)
|
|
13
11
|
end
|
|
14
12
|
|
|
15
|
-
#Checks if two elements are connected.
|
|
16
|
-
def edge? x,y
|
|
17
|
-
v1 = @map.index(x)
|
|
18
|
-
v2 = @map.index(y)
|
|
19
|
-
@store[v1,v2] > 0
|
|
20
|
-
end
|
|
21
|
-
|
|
22
13
|
#Adds new edges to graph.
|
|
23
14
|
def add_edges(edges)
|
|
24
15
|
for e in edges
|
|
25
|
-
x = @map.
|
|
26
|
-
y = @map.
|
|
16
|
+
x = @map.push e.from
|
|
17
|
+
y = @map.push e.to
|
|
27
18
|
|
|
28
19
|
@store[x,y] = e.weight
|
|
20
|
+
|
|
29
21
|
@max = [@max, x, y].max
|
|
30
|
-
@v = @v + 1
|
|
31
22
|
end
|
|
32
23
|
end
|
|
33
24
|
|
|
34
25
|
#Returns all neighbors for given vertex.
|
|
35
26
|
def neighbors(v)
|
|
36
27
|
n = []
|
|
28
|
+
vertexes = @map.to_a
|
|
37
29
|
v = @map.index(v)
|
|
30
|
+
|
|
38
31
|
0.upto @max do |i|
|
|
39
|
-
n <<
|
|
32
|
+
n << vertexes[i] if @store[v,i] > 0
|
|
40
33
|
end
|
|
41
34
|
n
|
|
42
35
|
end
|
|
@@ -46,11 +39,12 @@ module DS
|
|
|
46
39
|
v1 = @map.index(x)
|
|
47
40
|
v2 = @map.index(y)
|
|
48
41
|
|
|
42
|
+
vertexes = @map.to_a
|
|
43
|
+
|
|
49
44
|
@store[v1,v2] = 0
|
|
50
|
-
if (degree
|
|
45
|
+
if (degree vertexes[@max]) == 0
|
|
51
46
|
@max -= 1
|
|
52
47
|
end
|
|
53
|
-
@v -= 1
|
|
54
48
|
end
|
|
55
49
|
|
|
56
50
|
#Returns Edge(x,y) if exist.
|
|
@@ -58,19 +52,22 @@ module DS
|
|
|
58
52
|
s = @map.index x
|
|
59
53
|
t = @map.index y
|
|
60
54
|
if @store[s,t] > 0
|
|
61
|
-
Edge.new(
|
|
55
|
+
Edge.new(x, y, @store[s,t])
|
|
62
56
|
else
|
|
63
57
|
nil
|
|
64
58
|
end
|
|
65
59
|
end
|
|
66
60
|
|
|
67
|
-
|
|
68
|
-
|
|
61
|
+
#Checks if two elements are connected.
|
|
62
|
+
def edge? x,y
|
|
63
|
+
v1 = @map.index(x)
|
|
64
|
+
v2 = @map.index(y)
|
|
65
|
+
@store[v1,v2] > 0
|
|
69
66
|
end
|
|
70
|
-
|
|
67
|
+
|
|
71
68
|
#Returns vertex counter.
|
|
72
69
|
def vertex_size
|
|
73
|
-
@
|
|
70
|
+
@map.size
|
|
74
71
|
end
|
|
75
72
|
|
|
76
73
|
|
|
@@ -98,14 +95,16 @@ module DS
|
|
|
98
95
|
|
|
99
96
|
#Vertex iterator
|
|
100
97
|
def each_vertex
|
|
101
|
-
|
|
98
|
+
vertexes = @map.to_a
|
|
99
|
+
(0..@max).each {|v| yield vertexes[v]}
|
|
102
100
|
end
|
|
103
101
|
|
|
104
102
|
#Edge iterator
|
|
105
103
|
def each_edge
|
|
104
|
+
vertexes = @map.to_a
|
|
106
105
|
for v0 in 0..@max
|
|
107
106
|
for v1 in 0..v0-1
|
|
108
|
-
yield Edge.new(
|
|
107
|
+
yield Edge.new(vertexes[v0],vertexes[v1],@store[v0,v1]) if @store[v0,v1] > 0
|
|
109
108
|
end
|
|
110
109
|
end
|
|
111
110
|
end
|
data/lib/ds/lists/list.rb
CHANGED
|
@@ -55,7 +55,7 @@ module DS
|
|
|
55
55
|
el = el.next
|
|
56
56
|
end
|
|
57
57
|
|
|
58
|
-
raise "Element not found" unless el
|
|
58
|
+
raise ListError, "Element not found" unless el
|
|
59
59
|
|
|
60
60
|
prev.next = el.next
|
|
61
61
|
el.next = nil
|
|
@@ -106,7 +106,7 @@ module DS
|
|
|
106
106
|
end
|
|
107
107
|
|
|
108
108
|
if el.nil?
|
|
109
|
-
raise "List element not found"
|
|
109
|
+
raise ListError, "List element not found"
|
|
110
110
|
else
|
|
111
111
|
prev.next = x
|
|
112
112
|
x.next = el
|
|
@@ -212,9 +212,17 @@ module DS
|
|
|
212
212
|
b = b.next
|
|
213
213
|
end
|
|
214
214
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
215
|
+
if prev.nil?
|
|
216
|
+
prev = a
|
|
217
|
+
a = a.next
|
|
218
|
+
prev.next = nil
|
|
219
|
+
self.head = a
|
|
220
|
+
b = b.next
|
|
221
|
+
else
|
|
222
|
+
a = a.next
|
|
223
|
+
prev.next = a
|
|
224
|
+
b = b.next
|
|
225
|
+
end
|
|
218
226
|
end
|
|
219
227
|
self
|
|
220
228
|
end
|
|
@@ -267,37 +275,26 @@ module DS
|
|
|
267
275
|
|
|
268
276
|
@head = prev
|
|
269
277
|
self
|
|
270
|
-
|
|
271
278
|
end
|
|
272
279
|
|
|
273
|
-
#Prints list.
|
|
274
|
-
def print
|
|
275
|
-
each { |e| p }
|
|
276
|
-
end
|
|
277
280
|
|
|
278
281
|
#Converts list to array.
|
|
279
282
|
def to_a
|
|
280
|
-
map { |e| e}
|
|
283
|
+
map { |e| e.data}
|
|
281
284
|
end
|
|
282
285
|
|
|
283
286
|
#Default list iterator.
|
|
284
287
|
def each
|
|
285
288
|
elem = @head
|
|
286
289
|
while elem
|
|
287
|
-
yield elem
|
|
290
|
+
yield elem
|
|
288
291
|
elem = elem.next
|
|
289
292
|
end
|
|
290
293
|
end
|
|
291
294
|
|
|
292
|
-
|
|
293
|
-
elem = @head
|
|
294
|
-
i = 0
|
|
295
|
-
while elem
|
|
296
|
-
yield elem,i
|
|
297
|
-
elem = elem.next
|
|
298
|
-
i = i + 1
|
|
299
|
-
end
|
|
300
|
-
end
|
|
295
|
+
end
|
|
301
296
|
|
|
297
|
+
class ListError < StandardError
|
|
302
298
|
end
|
|
299
|
+
|
|
303
300
|
end
|
data/lib/ds/matrixes/array_2d.rb
CHANGED
data/lib/ds/pair.rb
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module DS
|
|
2
|
+
class PriorityQueue < Queue
|
|
3
|
+
|
|
4
|
+
#Create new priority queue. Internaly uses heap to store elements.
|
|
5
|
+
def initialize
|
|
6
|
+
@store = BinaryHeap.new {|parent,child| parent.key >= child.key}
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
#Adds element to queue with given priority.
|
|
11
|
+
def enqueue x, priority
|
|
12
|
+
pair = Pair.new(priority,x)
|
|
13
|
+
@store.insert(pair)
|
|
14
|
+
self
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
alias :push :enqueue
|
|
18
|
+
|
|
19
|
+
#Removes element with highest priority from queue .
|
|
20
|
+
def dequeue
|
|
21
|
+
if x = @store.shift
|
|
22
|
+
x.value
|
|
23
|
+
else
|
|
24
|
+
x
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
alias :shift :dequeue
|
|
29
|
+
|
|
30
|
+
#Returns element with highest priority.
|
|
31
|
+
def peek
|
|
32
|
+
@store.data.first.value
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
end
|