rubystructures 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +58 -2
- data/lib/rubystructures/queue.rb +116 -0
- data/lib/rubystructures/stack.rb +92 -0
- data/lib/rubystructures/version.rb +1 -1
- data/lib/rubystructures.rb +1 -0
- data/spec/queue_spec.rb +124 -0
- data/spec/stack_spec.rb +81 -0
- metadata +8 -2
data/README.md
CHANGED
@@ -18,8 +18,8 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
###
|
22
|
-
RubyStructures::Stack provides a First in,
|
21
|
+
### Stacks
|
22
|
+
RubyStructures::Stack provides a First in, Last out stack implementation.
|
23
23
|
|
24
24
|
Create a new Stack:
|
25
25
|
```ruby
|
@@ -60,6 +60,62 @@ Is the Stack empty?
|
|
60
60
|
# => false
|
61
61
|
```
|
62
62
|
|
63
|
+
### Queues
|
64
|
+
|
65
|
+
`RubyStructures::Queue` implements a First in, First out queue.
|
66
|
+
|
67
|
+
Create a new Queue:
|
68
|
+
```ruby
|
69
|
+
require 'rubystructures'
|
70
|
+
|
71
|
+
@queue = RubyStructures::Queue.new
|
72
|
+
```
|
73
|
+
|
74
|
+
Push a value onto the Queue:
|
75
|
+
```ruby
|
76
|
+
@queue.push(42)
|
77
|
+
# => true
|
78
|
+
```
|
79
|
+
|
80
|
+
You can also use `enqueue` as an alias for push:
|
81
|
+
```ruby
|
82
|
+
@queue.enqueue(42)
|
83
|
+
# => true
|
84
|
+
```
|
85
|
+
|
86
|
+
Pop a value off the Queue:
|
87
|
+
```ruby
|
88
|
+
@queue.pop
|
89
|
+
# => 42
|
90
|
+
```
|
91
|
+
|
92
|
+
You can also use `dequeue` as an alias for pop:
|
93
|
+
```ruby
|
94
|
+
@queue.dequeue
|
95
|
+
# => 42
|
96
|
+
```
|
97
|
+
|
98
|
+
Take a peek at the values at the front of back of the Queue without removing them:
|
99
|
+
```ruby
|
100
|
+
@queue.front
|
101
|
+
# => 42
|
102
|
+
|
103
|
+
@queue.back
|
104
|
+
# => 7
|
105
|
+
```
|
106
|
+
|
107
|
+
Get the size of the Queue:
|
108
|
+
```ruby
|
109
|
+
@queue.size
|
110
|
+
# => 4
|
111
|
+
```
|
112
|
+
|
113
|
+
Check whether or not the Queue is empty:
|
114
|
+
```ruby
|
115
|
+
@queue.empty?
|
116
|
+
# => false
|
117
|
+
```
|
118
|
+
|
63
119
|
## Contributing
|
64
120
|
|
65
121
|
1. Fork it ( https://github.com/connorjacobsen/rubystructures/fork )
|
@@ -0,0 +1,116 @@
|
|
1
|
+
module RubyStructures
|
2
|
+
class Queue
|
3
|
+
# Public: Creates a new instance of Queue.
|
4
|
+
#
|
5
|
+
# Examples
|
6
|
+
#
|
7
|
+
# @queue = Queue.new
|
8
|
+
# # => Queue
|
9
|
+
#
|
10
|
+
# Returns a new instance of Queue.
|
11
|
+
def initialize
|
12
|
+
@storage = []
|
13
|
+
end
|
14
|
+
|
15
|
+
# Public: Returns the size of the Queue.
|
16
|
+
#
|
17
|
+
# Examples
|
18
|
+
#
|
19
|
+
# @queue.size
|
20
|
+
# # => 1
|
21
|
+
#
|
22
|
+
# Returns the Integer size of the Queue.
|
23
|
+
def size
|
24
|
+
@storage.size
|
25
|
+
end
|
26
|
+
|
27
|
+
# Public: Adds a value to the Queue.
|
28
|
+
#
|
29
|
+
# value - Ruby data type, can be of any class type.
|
30
|
+
#
|
31
|
+
# Examples
|
32
|
+
#
|
33
|
+
# @queue.push(42)
|
34
|
+
# # => true
|
35
|
+
#
|
36
|
+
# Returns true if the value is successfully added to the Queue; returns
|
37
|
+
# false otherwise.
|
38
|
+
def push(value)
|
39
|
+
if @storage.insert(0, value)
|
40
|
+
true
|
41
|
+
else
|
42
|
+
false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Public: Returns and removes the next item in the Queue.
|
47
|
+
#
|
48
|
+
# Examples
|
49
|
+
#
|
50
|
+
# @queue.pop
|
51
|
+
# # => 42
|
52
|
+
#
|
53
|
+
# Returns the next value to be served by the Queue, unless
|
54
|
+
# the Queue is empty, then returns nil.
|
55
|
+
def pop
|
56
|
+
@storage.pop
|
57
|
+
end
|
58
|
+
|
59
|
+
# Public: Returns the next value to be served by the Queue without
|
60
|
+
# removing the value from the Queue.
|
61
|
+
#
|
62
|
+
# Examples
|
63
|
+
#
|
64
|
+
# @queue.front
|
65
|
+
# # => 42
|
66
|
+
#
|
67
|
+
# Returns the value of the next item to be served in the Queue.
|
68
|
+
def front
|
69
|
+
@storage.last
|
70
|
+
end
|
71
|
+
|
72
|
+
# Public: Returns the last value added to the Queue.
|
73
|
+
#
|
74
|
+
# Examples
|
75
|
+
#
|
76
|
+
# @queue.back
|
77
|
+
# # => 42
|
78
|
+
#
|
79
|
+
# Returns the value at the end of the Queue, which would be served last.
|
80
|
+
def back
|
81
|
+
@storage.first
|
82
|
+
end
|
83
|
+
|
84
|
+
# Public: Checks if whether or not the Queue is empty.
|
85
|
+
#
|
86
|
+
# Examples
|
87
|
+
#
|
88
|
+
# @queue.empty?
|
89
|
+
# # => true
|
90
|
+
#
|
91
|
+
# Returns true if the Queue is empty; false otherwise.
|
92
|
+
def empty?
|
93
|
+
@storage.size == 0
|
94
|
+
end
|
95
|
+
|
96
|
+
# Alias for Queue.push(value)
|
97
|
+
#
|
98
|
+
# Examples
|
99
|
+
#
|
100
|
+
# @queue.push(42)
|
101
|
+
# # => true
|
102
|
+
def enqueue(value)
|
103
|
+
self.push value
|
104
|
+
end
|
105
|
+
|
106
|
+
# Alias for Queue.pop
|
107
|
+
#
|
108
|
+
# Examples
|
109
|
+
#
|
110
|
+
# @queue.dequeue
|
111
|
+
# # => 42
|
112
|
+
def dequeue
|
113
|
+
self.pop
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module RubyStructures
|
2
|
+
class Stack
|
3
|
+
# Public: Creates a new instance of Stack with default values.
|
4
|
+
#
|
5
|
+
# Examples
|
6
|
+
#
|
7
|
+
# Stack.new
|
8
|
+
#
|
9
|
+
# Returns a new instance of RubyStuctures::Stack.
|
10
|
+
def initialize
|
11
|
+
@storage = Array.new
|
12
|
+
end
|
13
|
+
|
14
|
+
# Public: Returns the Integer current size of the stack.
|
15
|
+
# We can rely on the Ruby Array to provide us with the correct stack
|
16
|
+
# size, which allows us to not have a :size instance variable.
|
17
|
+
#
|
18
|
+
# Examples
|
19
|
+
#
|
20
|
+
# @stack.size
|
21
|
+
# # => 42
|
22
|
+
#
|
23
|
+
# Returns the size of the stack.
|
24
|
+
def size
|
25
|
+
@storage.size
|
26
|
+
end
|
27
|
+
|
28
|
+
# Public: Adds value to the Stack.
|
29
|
+
#
|
30
|
+
# value - Ruby data type, can be of any class type.
|
31
|
+
#
|
32
|
+
# Examples
|
33
|
+
#
|
34
|
+
# @stack.push(42)
|
35
|
+
# # => true
|
36
|
+
#
|
37
|
+
# Returns true if the value was successfully added to the stack.
|
38
|
+
def push(value)
|
39
|
+
if @storage << value
|
40
|
+
true
|
41
|
+
else
|
42
|
+
false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Public: Removes and returns the value from the top of the stack.
|
47
|
+
#
|
48
|
+
# Examples
|
49
|
+
#
|
50
|
+
# # If the stack has :size > 1, return the top value.
|
51
|
+
# @stack.pop
|
52
|
+
# # => 42
|
53
|
+
#
|
54
|
+
# # If the stack is empty, returns nil.
|
55
|
+
# @stack.pop
|
56
|
+
# # => nil
|
57
|
+
#
|
58
|
+
# Returns the value at the top of the stack, or nil if the stack is empty.
|
59
|
+
def pop
|
60
|
+
@storage.pop
|
61
|
+
end
|
62
|
+
|
63
|
+
# Public: Peek at the value on the top of the stack without removing it.
|
64
|
+
#
|
65
|
+
# Examples
|
66
|
+
#
|
67
|
+
# @stack.top
|
68
|
+
# # => 42
|
69
|
+
#
|
70
|
+
# # If the stack is empty, returns nil.
|
71
|
+
# @stack.top
|
72
|
+
# # => nil
|
73
|
+
#
|
74
|
+
# Returns the value at the stop of the Stack, but does not pop the
|
75
|
+
# value off the stack. Returns nil if the Stack is empty.
|
76
|
+
def top
|
77
|
+
@storage.last
|
78
|
+
end
|
79
|
+
|
80
|
+
# Public: Boolean check to see if the stack is empty.
|
81
|
+
#
|
82
|
+
# Examples
|
83
|
+
#
|
84
|
+
# @stack.empty?
|
85
|
+
# # => true
|
86
|
+
#
|
87
|
+
# Returns true if the Stack is empty, false otherwise.
|
88
|
+
def empty?
|
89
|
+
@storage.size == 0
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/lib/rubystructures.rb
CHANGED
data/spec/queue_spec.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'rubystructures'
|
2
|
+
|
3
|
+
describe RubyStructures::Queue do
|
4
|
+
queue = RubyStructures::Queue.new
|
5
|
+
subject { queue }
|
6
|
+
|
7
|
+
it { should respond_to :size }
|
8
|
+
|
9
|
+
# Methods
|
10
|
+
describe "#initialize" do
|
11
|
+
before(:each) { @queue = RubyStructures::Queue.new }
|
12
|
+
|
13
|
+
it "creates a Queue object" do
|
14
|
+
expect(@queue.class).to eql RubyStructures::Queue
|
15
|
+
end
|
16
|
+
|
17
|
+
it "has :size zero upon creation" do
|
18
|
+
expect(@queue.size).to eql 0
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#push" do
|
23
|
+
before { @queue = RubyStructures::Queue.new }
|
24
|
+
it "add an object to the queue" do
|
25
|
+
expect(@queue.size).to eql 0
|
26
|
+
@queue.push(42)
|
27
|
+
expect(@queue.size).to eql 1
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#pop" do
|
32
|
+
before(:each) do
|
33
|
+
@queue = RubyStructures::Queue.new
|
34
|
+
@queue.push(42)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns the correct value" do
|
38
|
+
expect(@queue.pop).to eql 42
|
39
|
+
end
|
40
|
+
|
41
|
+
it "has the correct queue size" do
|
42
|
+
expect(@queue.size).to eql 1
|
43
|
+
@queue.pop
|
44
|
+
expect(@queue.size).to eql 0
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#size" do
|
49
|
+
before(:each) do
|
50
|
+
@queue = RubyStructures::Queue.new
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns 0 when Queue is empty" do
|
54
|
+
expect(@queue.size).to eql 0
|
55
|
+
end
|
56
|
+
|
57
|
+
it "returns correct Queue size" do
|
58
|
+
@queue.push(42)
|
59
|
+
expect(@queue.size).to eql 1
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#front" do
|
64
|
+
before(:each) do
|
65
|
+
@queue = RubyStructures::Queue.new
|
66
|
+
@queue.push(42)
|
67
|
+
@queue.push(13)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns the correct value" do
|
71
|
+
expect(@queue.size).to eql 2
|
72
|
+
expect(@queue.front).to eql 42
|
73
|
+
expect(@queue.size).to eql 2
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "#back" do
|
78
|
+
before(:each) do
|
79
|
+
@queue = RubyStructures::Queue.new
|
80
|
+
@queue.push(42)
|
81
|
+
@queue.push(13)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "returns the correct value" do
|
85
|
+
expect(@queue.size).to eql 2
|
86
|
+
expect(@queue.back).to eql 13
|
87
|
+
expect(@queue.size).to eql 2
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#empty?" do
|
92
|
+
before { @queue = RubyStructures::Queue.new }
|
93
|
+
|
94
|
+
it "returns true when empty" do
|
95
|
+
expect(@queue.empty?).to eql true
|
96
|
+
end
|
97
|
+
|
98
|
+
it "returns false when not empty" do
|
99
|
+
@queue.push(42)
|
100
|
+
expect(@queue.empty?).to eql false
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "#enqueue" do
|
105
|
+
before { @queue = RubyStructures::Queue.new }
|
106
|
+
|
107
|
+
it "behaves like push" do
|
108
|
+
expect(@queue.size).to eql 0
|
109
|
+
@queue.enqueue(42)
|
110
|
+
expect(@queue.size).to eql 1
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "#dequeue" do
|
115
|
+
before { @queue = RubyStructures::Queue.new }
|
116
|
+
|
117
|
+
it "behaves like pop" do
|
118
|
+
@queue.push(42)
|
119
|
+
expect(@queue.size).to eql 1
|
120
|
+
expect(@queue.dequeue).to eql 42
|
121
|
+
expect(@queue.size).to eql 0
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
data/spec/stack_spec.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rubystructures'
|
2
|
+
|
3
|
+
describe RubyStructures::Stack do
|
4
|
+
stack = RubyStructures::Stack.new
|
5
|
+
subject { stack }
|
6
|
+
|
7
|
+
# Attributes
|
8
|
+
it { should respond_to :size }
|
9
|
+
|
10
|
+
# Methods
|
11
|
+
describe "#initialize" do
|
12
|
+
before { @stack = RubyStructures::Stack.new }
|
13
|
+
it "creates a Stack object" do
|
14
|
+
expect(@stack.class).to eql RubyStructures::Stack
|
15
|
+
end
|
16
|
+
|
17
|
+
it "has size zero upon creation" do
|
18
|
+
expect(@stack.size).to eql 0
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#push" do
|
23
|
+
before { @stack = RubyStructures::Stack.new }
|
24
|
+
it "adds an object to the stack" do
|
25
|
+
expect(@stack.size).to eql 0
|
26
|
+
@stack.push(42)
|
27
|
+
expect(@stack.size).to eql 1
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#pop" do
|
32
|
+
before { @stack = RubyStructures::Stack.new }
|
33
|
+
context "stack is empty" do
|
34
|
+
it "returns nil" do
|
35
|
+
expect(@stack.pop).to eql nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "stack is not empty" do
|
40
|
+
before { @stack.push(42) }
|
41
|
+
it "returns and removes correct value" do
|
42
|
+
expect(@stack.pop).to eql 42
|
43
|
+
expect(@stack.size).to eql 0
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#top" do
|
49
|
+
before do
|
50
|
+
@stack = RubyStructures::Stack.new
|
51
|
+
@stack.push(42)
|
52
|
+
@stack.push(13)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "returns the value of the top item" do
|
56
|
+
expect(@stack.top).to eql 13
|
57
|
+
end
|
58
|
+
|
59
|
+
it "doesn't remove any items" do
|
60
|
+
@stack.top
|
61
|
+
expect(@stack.size).to eql 2
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#empty?" do
|
66
|
+
before(:each) { @stack = RubyStructures::Stack.new }
|
67
|
+
|
68
|
+
context "when stack is empty" do
|
69
|
+
it "return true" do
|
70
|
+
expect(@stack.empty?).to eql true
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when stack is not empty" do
|
75
|
+
before { @stack.push(42) }
|
76
|
+
it "returns false" do
|
77
|
+
expect(@stack.empty?).to eql false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubystructures
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -72,8 +72,12 @@ files:
|
|
72
72
|
- README.md
|
73
73
|
- Rakefile
|
74
74
|
- lib/rubystructures.rb
|
75
|
+
- lib/rubystructures/queue.rb
|
76
|
+
- lib/rubystructures/stack.rb
|
75
77
|
- lib/rubystructures/version.rb
|
76
78
|
- rubystructures.gemspec
|
79
|
+
- spec/queue_spec.rb
|
80
|
+
- spec/stack_spec.rb
|
77
81
|
homepage: ''
|
78
82
|
licenses:
|
79
83
|
- MIT
|
@@ -99,4 +103,6 @@ rubygems_version: 1.8.23
|
|
99
103
|
signing_key:
|
100
104
|
specification_version: 3
|
101
105
|
summary: Simple, useful data structures in ruby.
|
102
|
-
test_files:
|
106
|
+
test_files:
|
107
|
+
- spec/queue_spec.rb
|
108
|
+
- spec/stack_spec.rb
|