renaudb-batfish 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +8 -0
- data/Rakefile +0 -13
- data/batfish.gemspec +14 -9
- data/lib/batfish.rb +5 -2
- data/lib/data/bktree.rb +8 -8
- data/lib/data/linkedlist.rb +183 -0
- data/lib/data/queue.rb +72 -0
- data/lib/data/stack.rb +66 -0
- data/lib/data/trie.rb +10 -10
- data/test/test_linkedlist.rb +119 -0
- data/test/test_queue.rb +46 -0
- data/test/test_stack.rb +46 -0
- data/test/test_trie.rb +12 -9
- metadata +11 -3
- data/VERSION +0 -1
data/README.rdoc
CHANGED
@@ -4,6 +4,10 @@ Just a bunch of functions.
|
|
4
4
|
|
5
5
|
Batfish is a collection of useful algorithms and data structures that I developed through the years for various projects. I decided to publish in case my hard drive comes to crash and for everyone to use.
|
6
6
|
|
7
|
+
http://renaudbourassa.com/projects/batfish/
|
8
|
+
|
9
|
+
http://github.com/renaudb/batfish/
|
10
|
+
|
7
11
|
== Install
|
8
12
|
|
9
13
|
If you haven't already, add github's gem server to your sources:
|
@@ -38,5 +42,9 @@ Here is a list of the algorithms and data structures batfish implements.
|
|
38
42
|
|
39
43
|
=== Data Structures
|
40
44
|
|
45
|
+
- Linked List
|
46
|
+
- Sorted Linked List
|
47
|
+
- Stack
|
48
|
+
- Queue
|
41
49
|
- BK-Tree
|
42
50
|
- Trie
|
data/Rakefile
CHANGED
@@ -2,19 +2,6 @@ require 'rake'
|
|
2
2
|
require 'rake/testtask'
|
3
3
|
require 'rake/rdoctask'
|
4
4
|
|
5
|
-
begin
|
6
|
-
require 'jeweler'
|
7
|
-
Jeweler::Tasks.new do |gemspec|
|
8
|
-
gemspec.name = "batfish"
|
9
|
-
gemspec.summary = "Just a bunch of functions."
|
10
|
-
gemspec.email = "me@renaudbourassa.com"
|
11
|
-
gemspec.homepage = "http://github.com/renaudb/batfish"
|
12
|
-
gemspec.authors = ["Renaud Bourassa"]
|
13
|
-
end
|
14
|
-
rescue LoadError
|
15
|
-
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
16
|
-
end
|
17
|
-
|
18
5
|
Rake::TestTask.new do |t|
|
19
6
|
t.test_files = FileList['test/test*.rb']
|
20
7
|
t.verbose = true
|
data/batfish.gemspec
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
1
|
# -*- encoding: utf-8 -*-
|
5
2
|
|
6
3
|
Gem::Specification.new do |s|
|
7
4
|
s.name = %q{batfish}
|
8
|
-
s.version = "0.1.
|
5
|
+
s.version = "0.1.1"
|
9
6
|
|
10
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
8
|
s.authors = ["Renaud Bourassa"]
|
12
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-09-23}
|
13
10
|
s.email = %q{me@renaudbourassa.com}
|
14
11
|
s.extra_rdoc_files = [
|
15
|
-
|
12
|
+
"README.rdoc"
|
16
13
|
]
|
17
14
|
s.files = [
|
18
|
-
|
15
|
+
"README.rdoc",
|
19
16
|
"Rakefile",
|
20
|
-
"VERSION",
|
21
17
|
"batfish.gemspec",
|
22
18
|
"lib/batfish.rb",
|
23
19
|
"lib/data/bktree.rb",
|
20
|
+
"lib/data/linkedlist.rb",
|
21
|
+
"lib/data/queue.rb",
|
22
|
+
"lib/data/stack.rb",
|
24
23
|
"lib/data/trie.rb",
|
25
24
|
"test/helper.rb",
|
26
25
|
"test/test_bktree.rb",
|
26
|
+
"test/test_linkedlist.rb",
|
27
|
+
"test/test_queue.rb",
|
28
|
+
"test/test_stack.rb",
|
27
29
|
"test/test_trie.rb"
|
28
30
|
]
|
29
31
|
s.homepage = %q{http://github.com/renaudb/batfish}
|
@@ -32,8 +34,11 @@ Gem::Specification.new do |s|
|
|
32
34
|
s.rubygems_version = %q{1.3.3}
|
33
35
|
s.summary = %q{Just a bunch of functions.}
|
34
36
|
s.test_files = [
|
35
|
-
|
37
|
+
"test/helper.rb",
|
36
38
|
"test/test_bktree.rb",
|
39
|
+
"test/test_linkedlist.rb",
|
40
|
+
"test/test_queue.rb",
|
41
|
+
"test/test_stack.rb",
|
37
42
|
"test/test_trie.rb"
|
38
43
|
]
|
39
44
|
|
data/lib/batfish.rb
CHANGED
@@ -1,2 +1,5 @@
|
|
1
|
-
require 'data/
|
2
|
-
require 'data/
|
1
|
+
require File.dirname(__FILE__) + '/data/linkedlist'
|
2
|
+
require File.dirname(__FILE__) + '/data/stack'
|
3
|
+
require File.dirname(__FILE__) + '/data/queue'
|
4
|
+
require File.dirname(__FILE__) + '/data/trie'
|
5
|
+
require File.dirname(__FILE__) + '/data/bktree'
|
data/lib/data/bktree.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Batfish
|
2
2
|
|
3
|
-
# A BK-tree is a
|
3
|
+
# A BK-tree is a data structure used for
|
4
4
|
# nearest neighbor lookup in a metric space.
|
5
5
|
# A metric space is any space with the
|
6
6
|
# following properties:
|
@@ -9,18 +9,18 @@ module Batfish
|
|
9
9
|
# * d(x,y) = d(y,x)
|
10
10
|
# * d(x,z) <= d(x,y) + d(y,z)
|
11
11
|
#
|
12
|
-
# To use this
|
12
|
+
# To use this data structure, the objects added
|
13
13
|
# to it must have a distance_to(other) class method
|
14
14
|
# defined.
|
15
15
|
class BKTree
|
16
16
|
attr_reader :root
|
17
17
|
|
18
|
-
#
|
19
|
-
def initialize
|
18
|
+
# Initializes a BKTree.
|
19
|
+
def initialize()
|
20
20
|
@root = nil
|
21
21
|
end
|
22
22
|
|
23
|
-
#
|
23
|
+
# Adds an object to the tree.
|
24
24
|
# Returns the tree itself so several
|
25
25
|
# adds may be chained together.
|
26
26
|
def add(obj)
|
@@ -34,7 +34,7 @@ module Batfish
|
|
34
34
|
alias_method :<<, :add
|
35
35
|
|
36
36
|
# Returns true if the tree is empty.
|
37
|
-
def empty?
|
37
|
+
def empty?()
|
38
38
|
return @root.nil? ? true : false
|
39
39
|
end
|
40
40
|
|
@@ -100,7 +100,7 @@ module Batfish
|
|
100
100
|
class Node
|
101
101
|
attr_reader :value, :children
|
102
102
|
|
103
|
-
#
|
103
|
+
# Initializes a BKTree Node with
|
104
104
|
# the given object as its value
|
105
105
|
# or nil if no object is given.
|
106
106
|
def initialize(obj=nil)
|
@@ -108,7 +108,7 @@ module Batfish
|
|
108
108
|
@children = {}
|
109
109
|
end
|
110
110
|
|
111
|
-
#
|
111
|
+
# Adds a node to a node in
|
112
112
|
# a recursive way.
|
113
113
|
def add(obj)
|
114
114
|
distance = obj.distance_to(@value)
|
@@ -0,0 +1,183 @@
|
|
1
|
+
module Batfish
|
2
|
+
|
3
|
+
# A Linked List is a data structure
|
4
|
+
# that consists of a sequence of data
|
5
|
+
# records such that each record point
|
6
|
+
# to the following record of the
|
7
|
+
# sequence.
|
8
|
+
class LinkedList
|
9
|
+
attr_reader :first
|
10
|
+
|
11
|
+
# Initializes a Linked List.
|
12
|
+
def initialize()
|
13
|
+
@first = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns true if the linked list
|
17
|
+
# is empty.
|
18
|
+
def empty?()
|
19
|
+
return @first.nil?
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns true if the given object
|
23
|
+
# is present in the tree.
|
24
|
+
def include?(obj)
|
25
|
+
self.each do |node|
|
26
|
+
if node == obj
|
27
|
+
return true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
return false
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns the length of the linked
|
34
|
+
# list.
|
35
|
+
def length()
|
36
|
+
length = 0
|
37
|
+
self.each { length += 1 }
|
38
|
+
return length
|
39
|
+
end
|
40
|
+
|
41
|
+
# Inserts an object at the beginning
|
42
|
+
# of the linked list. Returns the
|
43
|
+
# linked list itself so several
|
44
|
+
# inserts may be chained together.
|
45
|
+
def insert_begin(obj)
|
46
|
+
node = Node.new(obj)
|
47
|
+
node.next, @first = @first, node
|
48
|
+
return self
|
49
|
+
end
|
50
|
+
alias_method :<<, :insert_begin
|
51
|
+
|
52
|
+
# Inserts an object after a given
|
53
|
+
# object in the linked list.
|
54
|
+
# Returns nil if the given object
|
55
|
+
# can't be found otherwise returns
|
56
|
+
# the linked list itself so several
|
57
|
+
# inserts may be chained together.
|
58
|
+
def insert_after(obj, after)
|
59
|
+
node = Node.new(obj)
|
60
|
+
cur = @first
|
61
|
+
while cur
|
62
|
+
if cur.value == after
|
63
|
+
cur.next, node.next = node, cur.next
|
64
|
+
return self
|
65
|
+
end
|
66
|
+
cur = cur.next
|
67
|
+
end
|
68
|
+
return nil
|
69
|
+
end
|
70
|
+
|
71
|
+
# Removes the object at the beginning
|
72
|
+
# of the linked list. Returns the
|
73
|
+
# removed object.
|
74
|
+
def delete_begin()
|
75
|
+
node = @first
|
76
|
+
@first = @first.next
|
77
|
+
return node.value
|
78
|
+
end
|
79
|
+
|
80
|
+
# Removes the object after a given
|
81
|
+
# object in the linked list. Returns
|
82
|
+
# nil if the object can't be found,
|
83
|
+
# otherwise returns the removed
|
84
|
+
# object.
|
85
|
+
def delete_after(after)
|
86
|
+
cur = @first
|
87
|
+
while cur
|
88
|
+
if cur.value == after
|
89
|
+
node = cur.next
|
90
|
+
cur.next = node.next
|
91
|
+
return node
|
92
|
+
end
|
93
|
+
cur = cur.next
|
94
|
+
end
|
95
|
+
return nil
|
96
|
+
end
|
97
|
+
|
98
|
+
# Removes a given object in the linked
|
99
|
+
# list. Returns nil if the object
|
100
|
+
# can't be found, otherwise returns
|
101
|
+
# the removed object.
|
102
|
+
def delete(obj)
|
103
|
+
cur = @first
|
104
|
+
if cur.value == obj
|
105
|
+
@first = @first.next
|
106
|
+
end
|
107
|
+
while cur.next do
|
108
|
+
if cur.next.value == obj
|
109
|
+
node = cur.next
|
110
|
+
cur.next = node.next
|
111
|
+
return node.value
|
112
|
+
end
|
113
|
+
cur = cur.next
|
114
|
+
end
|
115
|
+
return nil
|
116
|
+
end
|
117
|
+
|
118
|
+
# Calls block once for each object
|
119
|
+
# in the stack, starting with the
|
120
|
+
# object at the beginning.
|
121
|
+
def each()
|
122
|
+
cur = @first
|
123
|
+
while cur do
|
124
|
+
yield cur.value
|
125
|
+
cur = cur.next
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# This class represents a node in
|
130
|
+
# a Linked List.
|
131
|
+
class Node
|
132
|
+
attr_accessor :next
|
133
|
+
attr_reader :value
|
134
|
+
|
135
|
+
# Initializes a Linked List Node with
|
136
|
+
# the given object as its value
|
137
|
+
# or nil if no object is given.
|
138
|
+
def initialize(obj=nil)
|
139
|
+
@value = obj
|
140
|
+
@next = nil
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# A Sorted Linked List is a data
|
146
|
+
# structure that consists of an
|
147
|
+
# ordered sequence of data records
|
148
|
+
# such that each record point to
|
149
|
+
# the following record of the sequence.
|
150
|
+
class SortedLinkedList < LinkedList
|
151
|
+
|
152
|
+
# Inserts an object in the linked list,
|
153
|
+
# keeping the objects ordered.
|
154
|
+
def insert(obj)
|
155
|
+
node = Node.new(obj)
|
156
|
+
if @first.nil?
|
157
|
+
@first = node
|
158
|
+
return self
|
159
|
+
end
|
160
|
+
prev = nil
|
161
|
+
cur = @first
|
162
|
+
while cur
|
163
|
+
if block_given?
|
164
|
+
if yield(cur.value, obj) >= 0
|
165
|
+
break
|
166
|
+
end
|
167
|
+
elsif obj <= cur.value
|
168
|
+
break
|
169
|
+
end
|
170
|
+
prev = cur
|
171
|
+
cur = cur.next
|
172
|
+
end
|
173
|
+
node.next = cur
|
174
|
+
if prev
|
175
|
+
prev.next = node
|
176
|
+
else
|
177
|
+
@first = node
|
178
|
+
end
|
179
|
+
return self
|
180
|
+
end
|
181
|
+
alias_method :<<, :insert
|
182
|
+
end
|
183
|
+
end
|
data/lib/data/queue.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
module Batfish
|
2
|
+
|
3
|
+
# A Queue is a first in, first out
|
4
|
+
# (FIFO) data structure characterized
|
5
|
+
# by two fundamental operations:
|
6
|
+
# enqueue and dequeue.
|
7
|
+
class Queue
|
8
|
+
attr_reader :first, :last
|
9
|
+
|
10
|
+
# Initializes a Queue.
|
11
|
+
def initialize()
|
12
|
+
@first = nil
|
13
|
+
@last = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns true if the queue is
|
17
|
+
# empty.
|
18
|
+
def empty?()
|
19
|
+
return @first.nil?
|
20
|
+
end
|
21
|
+
|
22
|
+
# Adds an object to the back
|
23
|
+
# of the queue. Returns the
|
24
|
+
# queue itself so several
|
25
|
+
# enqueue may be chained.
|
26
|
+
def enqueue(obj)
|
27
|
+
node = Node.new(obj)
|
28
|
+
if @first.nil?
|
29
|
+
@first = @last = node
|
30
|
+
else
|
31
|
+
@last.next = node
|
32
|
+
@last = node
|
33
|
+
end
|
34
|
+
return self
|
35
|
+
end
|
36
|
+
alias_method :<<, :enqueue
|
37
|
+
|
38
|
+
# Removes the object at
|
39
|
+
# the front of the queue
|
40
|
+
# and returns it.
|
41
|
+
def dequeue()
|
42
|
+
node = @first
|
43
|
+
@first = @first.next
|
44
|
+
return node.value
|
45
|
+
end
|
46
|
+
|
47
|
+
# Calls block once for each
|
48
|
+
# object in the queue, starting
|
49
|
+
# with the one at the front.
|
50
|
+
def each()
|
51
|
+
cur = @first
|
52
|
+
while cur
|
53
|
+
yield cur.value
|
54
|
+
cur = cur.next
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# This class represents a node
|
59
|
+
# in a queue.
|
60
|
+
class Node
|
61
|
+
attr_accessor :value, :next, :prev
|
62
|
+
|
63
|
+
# Initializes a Queue Node with
|
64
|
+
# the given object as its value
|
65
|
+
# or nil if no object is given.
|
66
|
+
def initialize(obj=nil)
|
67
|
+
@value = obj
|
68
|
+
@next = nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/data/stack.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
module Batfish
|
2
|
+
|
3
|
+
# A Stack is a last in, first out
|
4
|
+
# (LIFO) data structure characterized
|
5
|
+
# by two fundamental operations:
|
6
|
+
# push and pop.
|
7
|
+
class Stack
|
8
|
+
attr_reader :first
|
9
|
+
|
10
|
+
# Initializes a Stack.
|
11
|
+
def initialize()
|
12
|
+
@first = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns true if the stack is
|
16
|
+
# empty.
|
17
|
+
def empty?()
|
18
|
+
return @first.nil?
|
19
|
+
end
|
20
|
+
|
21
|
+
# Adds an object to the top of
|
22
|
+
# the list. Returns the stack
|
23
|
+
# itself so several push may be
|
24
|
+
# chained.
|
25
|
+
def push(obj)
|
26
|
+
node = Node.new(obj)
|
27
|
+
node.next = @first
|
28
|
+
@first = node
|
29
|
+
return self
|
30
|
+
end
|
31
|
+
alias_method :<<, :push
|
32
|
+
|
33
|
+
# Removes the object at the top
|
34
|
+
# of the stack and returns it.
|
35
|
+
def pop()
|
36
|
+
node = @first
|
37
|
+
@first = @first.next
|
38
|
+
return node.value
|
39
|
+
end
|
40
|
+
|
41
|
+
# Calls block once for each object
|
42
|
+
# in the stack, starting with the
|
43
|
+
# one on the top.
|
44
|
+
def each()
|
45
|
+
cur = @first
|
46
|
+
while cur
|
47
|
+
yield cur.value
|
48
|
+
cur = cur.next
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# This class represents a node in
|
53
|
+
# a Stack.
|
54
|
+
class Node
|
55
|
+
attr_accessor :value, :next, :prev
|
56
|
+
|
57
|
+
# Initializes a Stack Node with
|
58
|
+
# the given object as its value
|
59
|
+
# or nil if no object is given.
|
60
|
+
def initialize(obj=nil)
|
61
|
+
@value = obj
|
62
|
+
@next = nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/data/trie.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Batfish
|
2
2
|
|
3
|
-
# A Trie is a
|
3
|
+
# A Trie is a data structure used to
|
4
4
|
# store key value pairs where the key
|
5
5
|
# is a string. It does so by through
|
6
6
|
# a tree-like structure where each edge
|
@@ -11,8 +11,8 @@ module Batfish
|
|
11
11
|
class Trie
|
12
12
|
attr_reader :root
|
13
13
|
|
14
|
-
#
|
15
|
-
def initialize
|
14
|
+
# Initializes a Trie.
|
15
|
+
def initialize()
|
16
16
|
@root = Node.new()
|
17
17
|
end
|
18
18
|
|
@@ -59,13 +59,13 @@ module Batfish
|
|
59
59
|
end
|
60
60
|
|
61
61
|
# Returns true if the trie is empty.
|
62
|
-
def empty?
|
62
|
+
def empty?()
|
63
63
|
return @root.children.empty? ? true : false
|
64
64
|
end
|
65
65
|
|
66
66
|
# Calls block once for each key value
|
67
67
|
# pair in the trie.
|
68
|
-
def each
|
68
|
+
def each()
|
69
69
|
@root.each("") do |k, v|
|
70
70
|
yield(k, v)
|
71
71
|
end
|
@@ -76,7 +76,7 @@ module Batfish
|
|
76
76
|
class Node
|
77
77
|
attr_reader :value, :children
|
78
78
|
|
79
|
-
#
|
79
|
+
# Initializes a Trie Node with
|
80
80
|
# the given object as its value
|
81
81
|
# or nil if no object is given.
|
82
82
|
def initialize(value=nil)
|
@@ -84,7 +84,7 @@ module Batfish
|
|
84
84
|
@children = {}
|
85
85
|
end
|
86
86
|
|
87
|
-
#
|
87
|
+
# Adds a node to a node in a
|
88
88
|
# recursive way.
|
89
89
|
def add(key, value)
|
90
90
|
if key.empty?
|
@@ -98,7 +98,7 @@ module Batfish
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
#
|
101
|
+
# Deletes a key value pair from
|
102
102
|
# a node in a recursive way.
|
103
103
|
def delete(key)
|
104
104
|
if key.empty?
|
@@ -138,8 +138,8 @@ module Batfish
|
|
138
138
|
# Calls block once for each key
|
139
139
|
# value pairs under self.
|
140
140
|
def each(key, &block)
|
141
|
-
if value
|
142
|
-
yield(key, value)
|
141
|
+
if @value
|
142
|
+
yield(key, @value)
|
143
143
|
end
|
144
144
|
@children.each do |letter, child|
|
145
145
|
child.each(key + letter, &block)
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/helper'
|
3
|
+
|
4
|
+
include Batfish
|
5
|
+
|
6
|
+
class TestLinkedList < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@ll = LinkedList.new()
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_empty?
|
13
|
+
assert(@ll.empty?)
|
14
|
+
@ll.insert_begin(1)
|
15
|
+
@ll.insert_begin(2)
|
16
|
+
@ll << 3 << 4 << 5
|
17
|
+
@ll.delete_begin()
|
18
|
+
@ll.delete_begin()
|
19
|
+
@ll.delete_begin()
|
20
|
+
@ll.delete_begin()
|
21
|
+
@ll.delete_begin()
|
22
|
+
assert(@ll.empty?)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_insert
|
26
|
+
@ll.insert_begin(5)
|
27
|
+
@ll.insert_begin(3)
|
28
|
+
@ll.insert_begin(1)
|
29
|
+
@ll.insert_after(2, 1)
|
30
|
+
@ll.insert_after(4, 3)
|
31
|
+
cur = @ll.first
|
32
|
+
i = 1
|
33
|
+
while cur
|
34
|
+
assert_equal(i, cur.value)
|
35
|
+
i += 1
|
36
|
+
cur = cur.next
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_delete
|
41
|
+
@ll << 9 << 8 << 7 << 6 << 5 << 4 << 3 << 2 << 1
|
42
|
+
@ll.delete(8)
|
43
|
+
@ll.delete(5)
|
44
|
+
@ll.delete(2)
|
45
|
+
assert(!@ll.delete(99))
|
46
|
+
@ll.delete_begin
|
47
|
+
@ll.delete_begin
|
48
|
+
@ll.delete_after(7)
|
49
|
+
@ll.delete_after(4)
|
50
|
+
elements = [4,7]
|
51
|
+
cur = @ll.first
|
52
|
+
elements.each do |el|
|
53
|
+
assert_equal(el, cur.value)
|
54
|
+
cur = cur.next
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_include?
|
59
|
+
@ll << 1
|
60
|
+
assert(@ll.include?(1))
|
61
|
+
@ll.delete(1)
|
62
|
+
assert(!@ll.include?(1))
|
63
|
+
@ll << 1
|
64
|
+
assert(@ll.include?(1))
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_length
|
68
|
+
@ll << 1 << 2 << 3 << 4 << 5
|
69
|
+
assert_equal(5, @ll.length)
|
70
|
+
@ll.delete_begin()
|
71
|
+
@ll.delete_begin()
|
72
|
+
assert_equal(3, @ll.length)
|
73
|
+
@ll << 6
|
74
|
+
assert_equal(4, @ll.length)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_each
|
78
|
+
elements = [1,2,3,4,5]
|
79
|
+
elements.each do |el|
|
80
|
+
@ll << el
|
81
|
+
end
|
82
|
+
i = elements.length
|
83
|
+
@ll.each do |el|
|
84
|
+
assert_equal(i, el)
|
85
|
+
i -= 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class TestSortedLinkedList < Test::Unit::TestCase
|
91
|
+
|
92
|
+
def setup
|
93
|
+
@sll = SortedLinkedList.new()
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_insert
|
97
|
+
elements = [5,1,3,4,2,6]
|
98
|
+
elements.each do |el|
|
99
|
+
@sll << el
|
100
|
+
end
|
101
|
+
i = 1
|
102
|
+
@sll.each do |el|
|
103
|
+
assert_equal(i, el)
|
104
|
+
i += 1
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_insert_block
|
109
|
+
elements = [5,1,3,4,2,6]
|
110
|
+
elements.each do |el|
|
111
|
+
@sll.insert(el) { |x,y| -x <=> -y }
|
112
|
+
end
|
113
|
+
i = elements.length
|
114
|
+
@sll.each do |el|
|
115
|
+
assert_equal(i, el)
|
116
|
+
i -= 1
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/test/test_queue.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/helper'
|
3
|
+
|
4
|
+
include Batfish
|
5
|
+
|
6
|
+
class TestQueue < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@q = Queue.new()
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_empty?
|
13
|
+
assert(@q.empty?)
|
14
|
+
@q.enqueue(1)
|
15
|
+
@q.enqueue(2)
|
16
|
+
@q << 3 << 4
|
17
|
+
@q.dequeue()
|
18
|
+
@q.dequeue()
|
19
|
+
@q.dequeue()
|
20
|
+
@q.dequeue()
|
21
|
+
assert(@q.empty?)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_dequeue
|
25
|
+
@q << 1 << 2 << 3
|
26
|
+
assert_equal(1, @q.dequeue())
|
27
|
+
assert_equal(2, @q.dequeue())
|
28
|
+
@q << 4 << 5
|
29
|
+
assert_equal(3, @q.dequeue())
|
30
|
+
assert_equal(4, @q.dequeue())
|
31
|
+
assert_equal(5, @q.dequeue())
|
32
|
+
assert(@q.empty?)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_each
|
36
|
+
elements = [1,2,3,4,5,6]
|
37
|
+
elements.each do |el|
|
38
|
+
@q << el
|
39
|
+
end
|
40
|
+
i = 0
|
41
|
+
@q.each do |el|
|
42
|
+
assert_equal(elements[i], el)
|
43
|
+
i += 1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/test/test_stack.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/helper'
|
3
|
+
|
4
|
+
include Batfish
|
5
|
+
|
6
|
+
class TestStack < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@s = Stack.new()
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_empty?
|
13
|
+
assert(@s.empty?)
|
14
|
+
@s.push(5)
|
15
|
+
@s.push(7)
|
16
|
+
@s << 1 << 2
|
17
|
+
@s.pop()
|
18
|
+
@s.pop()
|
19
|
+
@s.pop()
|
20
|
+
@s.pop()
|
21
|
+
assert(@s.empty?)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_pop
|
25
|
+
@s << 1 << 5 << 9
|
26
|
+
assert_equal(9, @s.pop)
|
27
|
+
assert_equal(5, @s.pop)
|
28
|
+
@s << 3 << 4
|
29
|
+
assert_equal(4, @s.pop)
|
30
|
+
assert_equal(3, @s.pop)
|
31
|
+
assert_equal(1, @s.pop)
|
32
|
+
assert(@s.empty?)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_each
|
36
|
+
elements = [1,5,9,13,8,5]
|
37
|
+
elements.each do |el|
|
38
|
+
@s << el
|
39
|
+
end
|
40
|
+
i = elements.length - 1
|
41
|
+
@s.each do |el|
|
42
|
+
assert_equal(el, elements[i])
|
43
|
+
i -= 1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/test/test_trie.rb
CHANGED
@@ -82,16 +82,19 @@ class TestTrie < Test::Unit::TestCase
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def test_each
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
85
|
+
words = {
|
86
|
+
"dinosaur" => 0,
|
87
|
+
"tyrannosaurus" => 4,
|
88
|
+
"diplotomodon" => 2,
|
89
|
+
"tyrannotitan" => 5,
|
90
|
+
"raptor" => 3,
|
91
|
+
"diplodocus" => 1
|
92
|
+
}
|
93
|
+
words.each do |k,v|
|
94
|
+
@t.add(k, v)
|
95
|
+
end
|
92
96
|
@t.each do |k, v|
|
93
|
-
assert_equal(
|
94
|
-
i += 1
|
97
|
+
assert_equal(words[k.downcase], v)
|
95
98
|
end
|
96
99
|
end
|
97
100
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: renaudb-batfish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Renaud Bourassa
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-23 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,13 +24,18 @@ extra_rdoc_files:
|
|
24
24
|
files:
|
25
25
|
- README.rdoc
|
26
26
|
- Rakefile
|
27
|
-
- VERSION
|
28
27
|
- batfish.gemspec
|
29
28
|
- lib/batfish.rb
|
30
29
|
- lib/data/bktree.rb
|
30
|
+
- lib/data/linkedlist.rb
|
31
|
+
- lib/data/queue.rb
|
32
|
+
- lib/data/stack.rb
|
31
33
|
- lib/data/trie.rb
|
32
34
|
- test/helper.rb
|
33
35
|
- test/test_bktree.rb
|
36
|
+
- test/test_linkedlist.rb
|
37
|
+
- test/test_queue.rb
|
38
|
+
- test/test_stack.rb
|
34
39
|
- test/test_trie.rb
|
35
40
|
has_rdoc: false
|
36
41
|
homepage: http://github.com/renaudb/batfish
|
@@ -62,4 +67,7 @@ summary: Just a bunch of functions.
|
|
62
67
|
test_files:
|
63
68
|
- test/helper.rb
|
64
69
|
- test/test_bktree.rb
|
70
|
+
- test/test_linkedlist.rb
|
71
|
+
- test/test_queue.rb
|
72
|
+
- test/test_stack.rb
|
65
73
|
- test/test_trie.rb
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.1.0
|