datastructures 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +24 -0
- data/lib/datastructures.rb +2 -1
- data/lib/datastructures/stack.rb +42 -0
- data/lib/datastructures/version.rb +2 -2
- data/test/test_queue.rb +10 -2
- data/test/test_stack.rb +86 -0
- metadata +13 -11
data/README.md
CHANGED
@@ -21,6 +21,10 @@ A collection of data structures in Ruby, made for my [data structures challenge]
|
|
21
21
|
|
22
22
|
## Day 1: Queue
|
23
23
|
|
24
|
+
A queue is a simple container-based structure that mimics a real-life queue (e.g. waiting in line at the bank). It is FIFO (first-in-first-out), meaning that when you retrieve items from the queue, they are returned in the order in which they entered. Ruby Arrays provide methods that make Queue implementation trivially easy, but having them named appropriately and contained in a convenience class is worth it to see how they are implemented, and because other structures will inherit from this one. An alternate implementation could be done using a linked list.
|
25
|
+
|
26
|
+
Usage:
|
27
|
+
|
24
28
|
```ruby
|
25
29
|
require 'datastructure'
|
26
30
|
queue = DataStructures::Queue.new
|
@@ -34,3 +38,23 @@ queue.dequeue # => 'first'
|
|
34
38
|
queue.dequeue # => 'second'
|
35
39
|
queue.dequeue # => RuntimeError, "Queue underflow: nothing to dequeue"
|
36
40
|
```
|
41
|
+
|
42
|
+
## Day 2: Stack
|
43
|
+
|
44
|
+
The stack is the sibling of the queue. It mimicks a real-life stack (e.g. of paper). It is FILO (first-in-last-out), so that when items are retrieved from the stack, they are returned in the reverse of the order in which they were added. Again, Ruby Arrays provide a perfect container. As with the Queue, it could also be implemented using a linked list.
|
45
|
+
|
46
|
+
Usage:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
require 'datastructure'
|
50
|
+
stack = DataStructures::Stack.new
|
51
|
+
stack.push('first')
|
52
|
+
stack.push('second')
|
53
|
+
stack.size # => 2
|
54
|
+
stack.empty? # => false
|
55
|
+
stack.top # => 'second'
|
56
|
+
stack.bottom # => 'first'
|
57
|
+
stack.pop # => 'second'
|
58
|
+
stack.pop # => 'first'
|
59
|
+
stack.pop # => RuntimeError, "Stack underflow: nothing to pop"
|
60
|
+
```
|
data/lib/datastructures.rb
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
module DataStructures
|
2
|
+
# Implements a simple FILO (first in, last out) stack data
|
3
|
+
# structure using an array container.
|
4
|
+
class Stack
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
self.clear
|
8
|
+
end
|
9
|
+
|
10
|
+
def size
|
11
|
+
@array.size
|
12
|
+
end
|
13
|
+
|
14
|
+
alias :length :size
|
15
|
+
|
16
|
+
def empty?
|
17
|
+
@array.empty?
|
18
|
+
end
|
19
|
+
|
20
|
+
def push item
|
21
|
+
@array.push item
|
22
|
+
end
|
23
|
+
|
24
|
+
def pop
|
25
|
+
raise "Stack underflow: nothing to pop." if self.size == 0
|
26
|
+
@array.pop
|
27
|
+
end
|
28
|
+
|
29
|
+
def top
|
30
|
+
@array.last
|
31
|
+
end
|
32
|
+
|
33
|
+
def bottom
|
34
|
+
@array.first
|
35
|
+
end
|
36
|
+
|
37
|
+
def clear
|
38
|
+
@array = Array.new
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
data/test/test_queue.rb
CHANGED
@@ -73,6 +73,14 @@ class TestQueue < Test::Unit::TestCase
|
|
73
73
|
assert @queue.empty?
|
74
74
|
end
|
75
75
|
|
76
|
-
|
76
|
+
should "become empty when cleared" do
|
77
|
+
@queue.enqueue('first')
|
78
|
+
@queue.enqueue('second')
|
79
|
+
@queue.clear
|
80
|
+
assert @queue.size == 0
|
81
|
+
assert @queue.empty?
|
82
|
+
end
|
83
|
+
|
84
|
+
end # Queue context
|
77
85
|
|
78
|
-
end
|
86
|
+
end # TestQueue
|
data/test/test_stack.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'test_datastructures'
|
2
|
+
|
3
|
+
class TestStack < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "stack" do
|
6
|
+
|
7
|
+
setup do
|
8
|
+
@stack = DataStructures::Stack.new
|
9
|
+
end
|
10
|
+
|
11
|
+
should "be able to create a new instance" do
|
12
|
+
@stack = DataStructures::Stack.new
|
13
|
+
end
|
14
|
+
|
15
|
+
should "begin empty" do
|
16
|
+
assert @stack.empty?
|
17
|
+
assert @stack.size == 0
|
18
|
+
end
|
19
|
+
|
20
|
+
should "raise an error on underflow" do
|
21
|
+
assert_raise RuntimeError, "Stack underflow: nothing to pop" do
|
22
|
+
@stack.pop
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
should "stop being empty when first item added" do
|
27
|
+
@stack.push('first')
|
28
|
+
assert !@stack.empty?
|
29
|
+
assert @stack.size == 1
|
30
|
+
end
|
31
|
+
|
32
|
+
should "increment size on pushing" do
|
33
|
+
@stack.push('first')
|
34
|
+
@stack.push('second')
|
35
|
+
assert @stack.size == 2
|
36
|
+
end
|
37
|
+
|
38
|
+
should "respond to size and length" do
|
39
|
+
@stack.push('first')
|
40
|
+
assert @stack.size == @stack.length
|
41
|
+
end
|
42
|
+
|
43
|
+
should "have first item on the bottom" do
|
44
|
+
@stack.push('first')
|
45
|
+
@stack.push('second')
|
46
|
+
assert @stack.bottom == 'first'
|
47
|
+
end
|
48
|
+
|
49
|
+
should "have last item in on the top" do
|
50
|
+
@stack.push('first')
|
51
|
+
@stack.push('second')
|
52
|
+
assert @stack.top == 'second'
|
53
|
+
end
|
54
|
+
|
55
|
+
should "return top of stack and get smaller on popping" do
|
56
|
+
@stack.push('first')
|
57
|
+
@stack.push('second')
|
58
|
+
assert @stack.pop == 'second'
|
59
|
+
assert @stack.size == 1
|
60
|
+
end
|
61
|
+
|
62
|
+
should "have top and bottom items equal when length is one" do
|
63
|
+
@stack.push('first')
|
64
|
+
assert @stack.size == 1
|
65
|
+
assert @stack.top == @stack.bottom
|
66
|
+
end
|
67
|
+
|
68
|
+
should "become empty when last item popped" do
|
69
|
+
@stack.push('first')
|
70
|
+
@stack.push('second')
|
71
|
+
2.times{ @stack.pop }
|
72
|
+
assert @stack.size == 0
|
73
|
+
assert @stack.empty?
|
74
|
+
end
|
75
|
+
|
76
|
+
should "become empty when cleared" do
|
77
|
+
@stack.push('first')
|
78
|
+
@stack.push('second')
|
79
|
+
@stack.clear
|
80
|
+
assert @stack.size == 0
|
81
|
+
assert @stack.empty?
|
82
|
+
end
|
83
|
+
|
84
|
+
end # Stack context
|
85
|
+
|
86
|
+
end # TestStack
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: datastructures
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -32,33 +32,33 @@ dependencies:
|
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
33
33
|
none: false
|
34
34
|
requirements:
|
35
|
-
- -
|
35
|
+
- - ! '>='
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
37
|
+
version: '0'
|
38
38
|
type: :development
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
|
-
- -
|
43
|
+
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
45
|
+
version: '0'
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
47
|
name: shoulda
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
49
49
|
none: false
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - ! '>='
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: '0'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
none: false
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: '0'
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
63
|
name: coveralls
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,7 +75,7 @@ dependencies:
|
|
75
75
|
- - ~>
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: 0.6.7
|
78
|
-
description: ! '
|
78
|
+
description: ! ' useful data structures implemented in pure Ruby for my data structures
|
79
79
|
challenge. '
|
80
80
|
email: rds45@cam.ac.uk
|
81
81
|
executables: []
|
@@ -84,10 +84,12 @@ extra_rdoc_files: []
|
|
84
84
|
files:
|
85
85
|
- Rakefile
|
86
86
|
- lib/datastructures/queue.rb
|
87
|
+
- lib/datastructures/stack.rb
|
87
88
|
- lib/datastructures/version.rb
|
88
89
|
- lib/datastructures.rb
|
89
90
|
- test/test_datastructures.rb
|
90
91
|
- test/test_queue.rb
|
92
|
+
- test/test_stack.rb
|
91
93
|
- README.md
|
92
94
|
- LICENSE
|
93
95
|
homepage: https://github.com/Blahah/datastructures
|
@@ -113,5 +115,5 @@ rubyforge_project:
|
|
113
115
|
rubygems_version: 1.8.24
|
114
116
|
signing_key:
|
115
117
|
specification_version: 3
|
116
|
-
summary:
|
118
|
+
summary: useful data structures
|
117
119
|
test_files: []
|