basic_queue 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2bbb375a040698d29cb448e7733d70b1a849f858
4
+ data.tar.gz: d82ba51369fd8ca3a93ec5b1bb2c280e85b3bec0
5
+ SHA512:
6
+ metadata.gz: a3607fa5bea797d761120f4ed61aac6d0f5df4870ff9bf8136573d80341799298ce258484f64ac5bcb0e2c91f60ba0d5e2ed60477786e11c0801b5003ca06ae5
7
+ data.tar.gz: 7ac1ea3cbc36d07a1a5f3f9a5a24f25131caf1de09ff547a4343fb0e244ca53495b81fe0f8d796ea6d7a9fb2f359e3d13435e20facc06dfe7c8e59d8cd0502bf
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .ruby-version
24
+ .ruby-gemset
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in basic_queue.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Michael Imstepf
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # BasicQueue
2
+
3
+ A basic first-in-first-out (FIFO) queue data structure.
4
+
5
+ It supports the usual enqueue and dequeue operations, along with methods for peeking at the first item, testing if the queue is empty, clearing the queue and counting the number of items in the queue.
6
+
7
+ This implementation uses a singly-linked list with a non-static nested class for linked-list nodes. As a result, this data structure is more performant than implementing a queue with an array (see notes below).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'basic_queue'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install basic_queue
22
+
23
+ ## Usage
24
+
25
+ Create a new instance of `Queue`:
26
+
27
+ ```ruby
28
+ queue = BasicQueue::Queue.new
29
+ ```
30
+
31
+ Add items to the queue:
32
+ ```ruby
33
+ queue.enq 'Michael'
34
+ queue << 'Peter' # << is an alias method for enq(), you can use either method
35
+ ```
36
+
37
+ Check which item is next in the queue:
38
+ ```ruby
39
+ queue.peek
40
+ => "Michael"
41
+ ```
42
+
43
+ Remove item from the queue:
44
+ ```ruby
45
+ queue.deq
46
+ => "Michael"
47
+ ```
48
+
49
+ Check number of items left in the queue:
50
+ ```ruby
51
+ queue.length # size() is an alias method for length, you can use either method
52
+ => 1
53
+ ```
54
+
55
+ Clear queue:
56
+ ```ruby
57
+ queue.clear
58
+ ```
59
+
60
+ ## Performance
61
+
62
+ All methods take constant time (Θ(1)). Hence, using this data structure is more performant than using an Array since Array#unshift takes linear time (Θ(n)).
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/[my-github-username]/basic_queue/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'basic_queue/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "basic_queue"
8
+ spec.version = BasicQueue::VERSION
9
+ spec.authors = ["Michael Imstepf"]
10
+ spec.email = ["michael.imstepf@gmail.com"]
11
+ spec.summary = %q{A first-in-first-out (FIFO) queue data structure.}
12
+ spec.description = %q{It supports the usual enqueue and dequeue operations, along with methods for peeking at the first item, testing if the queue is empty and counting the number of items in the queue.}
13
+ spec.homepage = "https://github.com/michaelimstepf/basic-queue"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,130 @@
1
+ require "basic_queue/version"
2
+
3
+ module BasicQueue
4
+
5
+ # A basic first-in-first-out (FIFO) queue data structure.
6
+
7
+ # It supports the usual enqueue and dequeue operations,
8
+ # along with methods for peeking at the first item, testing
9
+ # if the queue is empty and counting the number of items in the queue.
10
+
11
+ # This implementation uses a singly-linked list with a non-static
12
+ # nested class for linked-list nodes.
13
+
14
+ # All methods take constant time (Θ(1)). Hence, it is more performant than
15
+ # an Array, since Array#unshift takes linear time (Θ(n)).
16
+
17
+ # @author Robert Sedgewick
18
+ # @author Kevin Wayne
19
+ # @author Michael Imstepf
20
+ # @see http://algs4.cs.princeton.edu/13stacks
21
+ # @see http://algs4.cs.princeton.edu/13stacks/LinkedQueue.java.html
22
+ class Queue
23
+ # Initializes the queue and sets variables.
24
+ def initialize
25
+ @length = 0
26
+ @first = nil
27
+ @last = nil
28
+ end
29
+
30
+ # Adds new item to the queue.
31
+ # @param item [Item] the item to be enqueued
32
+ # @return [Item] item
33
+ def enq(item)
34
+ # save previous old last node for use below
35
+ old_last = @last
36
+
37
+ # create new last node and update @last_item
38
+ @last = Node.new(item, nil)
39
+
40
+ if empty? # set to nil
41
+ @first = @last
42
+ else # point 2nd last node to last node
43
+ old_last.next_node = @last
44
+ end
45
+
46
+ @length += 1
47
+
48
+ item
49
+ end
50
+
51
+ # Adds new item to the queue.
52
+ # Alias method for #enq().
53
+ # @param item [Item] the item to be enqueued
54
+ # @return [Item] item
55
+ def <<(item)
56
+ enq(item)
57
+ end
58
+
59
+ # Removes and returns the item on this queue that was least recently added.
60
+ # @return [Item] dequeued item
61
+ def deq
62
+ return nil if empty?
63
+
64
+ # save previous first node for return value
65
+ old_first = @first
66
+
67
+ # update first node
68
+ @first = @first.next_node
69
+ @length -= 1
70
+
71
+ # avoid loitering
72
+ @last = nil if empty?
73
+
74
+ old_first.item
75
+ end
76
+
77
+ # Returns next item in the queue.
78
+ # @return [Item] item
79
+ def peek
80
+ empty? ? nil : @first.item
81
+ end
82
+
83
+ # Clears queue.
84
+ # @return [Boolean] true
85
+ def clear
86
+ @length = 0
87
+ @first = nil
88
+ @last = nil
89
+ true
90
+ end
91
+
92
+ # Returns length of queue.
93
+ # @return [Integer]
94
+ def length
95
+ @length
96
+ end
97
+
98
+ # Returns length of queue.
99
+ # Alias method for #length.
100
+ def size
101
+ length
102
+ end
103
+
104
+ # Checks whether the queue is empty or not.
105
+ # @return [Boolean]
106
+ def empty?
107
+ length == 0
108
+ end
109
+ end
110
+
111
+ # In arrays and queues based on arrays, the enqueue method
112
+ # (Array#unshift) is often expensive since it needs to iterate through each
113
+ # array member and shift each member to the right, resulting in linear running time.
114
+ # We use a singly linked list instead.
115
+
116
+ # @author Robert Sedgewick
117
+ # @author Kevin Wayne
118
+ # @author Michael Imstepf
119
+ # @see http://algs4.cs.princeton.edu/13stacks/LinkedQueue.java.html
120
+ class Node
121
+ attr_reader :item
122
+ attr_accessor :next_node
123
+
124
+ def initialize(item, next_node)
125
+ @item = item
126
+ @next_node = next_node
127
+ end
128
+ end
129
+
130
+ end
@@ -0,0 +1,3 @@
1
+ module BasicQueue
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,188 @@
1
+ require 'spec_helper'
2
+
3
+ describe BasicQueue::Queue do
4
+ describe '#enq' do
5
+ queue = BasicQueue::Queue.new
6
+
7
+ it 'adds item to queue' do
8
+ queue.enq 'Michael'
9
+ expect(queue.length).to eq 1
10
+ queue.enq 'Peter'
11
+ expect(queue.length).to eq 2
12
+ end
13
+ end
14
+
15
+ describe '#<<' do
16
+ queue = BasicQueue::Queue.new
17
+
18
+ it 'adds item to queue' do
19
+ queue << 'Michael'
20
+ expect(queue.length).to eq 1
21
+ queue << 'Peter'
22
+ expect(queue.length).to eq 2
23
+ end
24
+ end
25
+
26
+ describe '#deq' do
27
+ context 'when items exist in queue' do
28
+ queue = BasicQueue::Queue.new
29
+
30
+ it 'removes item from queue' do
31
+ queue.enq 'Michael'
32
+ queue.enq 'Peter'
33
+ expect(queue.deq).to eq 'Michael'
34
+ expect(queue.deq).to eq 'Peter'
35
+ end
36
+ end
37
+
38
+ context 'when queue has been instantiated without items' do
39
+ queue = BasicQueue::Queue.new
40
+
41
+ it 'returns nil' do
42
+ expect(queue.deq).to be_nil
43
+ end
44
+ end
45
+
46
+ context 'when all items have been removed from queue' do
47
+ queue = BasicQueue::Queue.new
48
+
49
+ it 'returns nil' do
50
+ queue.enq 'Michael'
51
+ queue.enq 'Peter'
52
+ expect(queue.deq).to eq 'Michael'
53
+ expect(queue.deq).to eq 'Peter'
54
+ expect(queue.deq).to be_nil
55
+ end
56
+ end
57
+
58
+ context 'when there is an underflow' do
59
+ queue = BasicQueue::Queue.new
60
+
61
+ it 'returns nil' do
62
+ queue.enq 'Michael'
63
+ queue.enq 'Peter'
64
+ expect(queue.deq).to eq 'Michael'
65
+ expect(queue.deq).to eq 'Peter'
66
+ expect(queue.deq).to eq nil
67
+ expect(queue.deq).to eq nil
68
+ expect(queue.length).to eq 0
69
+ end
70
+ end
71
+ end
72
+
73
+ describe '#clear' do
74
+ context 'when queue is empty' do
75
+ queue = BasicQueue::Queue.new
76
+
77
+ it 'clears queue' do
78
+ expect(queue.clear).to be_truthy
79
+ expect(queue.length).to eq 0
80
+ queue.enq 'Michael'
81
+ expect(queue.deq).to eq 'Michael'
82
+ end
83
+ end
84
+
85
+ context 'when queue is not empty' do
86
+ queue = BasicQueue::Queue.new
87
+
88
+ it 'clears queue' do
89
+ queue.enq 'Michael'
90
+ queue.enq 'Peter'
91
+ expect(queue.clear).to be_truthy
92
+ expect(queue.length).to eq 0
93
+ queue.enq 'Michael'
94
+ expect(queue.deq).to eq 'Michael'
95
+ end
96
+ end
97
+ end
98
+
99
+ describe '#length' do
100
+ context 'when queue has been instantiated without items' do
101
+ queue = BasicQueue::Queue.new
102
+
103
+ it 'returns 0' do
104
+ expect(queue.length).to eq 0
105
+ end
106
+ end
107
+
108
+ context 'when queue has 1 item' do
109
+ queue = BasicQueue::Queue.new
110
+
111
+ it 'returns 1' do
112
+ queue.enq 'Michael'
113
+ expect(queue.length).to eq 1
114
+ end
115
+ end
116
+
117
+ context 'when queue has 2 items' do
118
+ queue = BasicQueue::Queue.new
119
+
120
+ it 'returns 2' do
121
+ queue.enq 'Michael'
122
+ queue.enq 'Peter'
123
+ expect(queue.length).to eq 2
124
+ end
125
+ end
126
+
127
+ context 'when queue has been cleared' do
128
+ queue = BasicQueue::Queue.new
129
+
130
+ it 'returns 0' do
131
+ queue.enq 'Michael'
132
+ queue.enq 'Peter'
133
+ queue.clear
134
+ expect(queue.length).to eq 0
135
+ end
136
+ end
137
+ end
138
+
139
+ describe '#size' do
140
+ context 'when queue has been instantiated without items' do
141
+ queue = BasicQueue::Queue.new
142
+
143
+ it 'returns 0' do
144
+ expect(queue.size).to eq 0
145
+ end
146
+ end
147
+ end
148
+
149
+ describe '#empty?' do
150
+ context 'when queue is empty' do
151
+ queue = BasicQueue::Queue.new
152
+
153
+ it 'returns true' do
154
+ expect(queue.empty?).to be_truthy
155
+ end
156
+ end
157
+
158
+ context 'when queue is not empty' do
159
+ queue = BasicQueue::Queue.new
160
+
161
+ it 'returns false' do
162
+ queue.enq 'Michael'
163
+ expect(queue.empty?).to be_falsey
164
+ end
165
+ end
166
+ end
167
+
168
+ describe '#peek' do
169
+ context 'when queue is empty' do
170
+ queue = BasicQueue::Queue.new
171
+
172
+ it 'returns nil' do
173
+ expect(queue.peek).to be_nil
174
+ end
175
+ end
176
+
177
+ context 'when queue is not empty' do
178
+ queue = BasicQueue::Queue.new
179
+
180
+ it 'returns next item in the queue' do
181
+ queue.enq 'Michael'
182
+ queue.enq 'Peter'
183
+ expect(queue.peek).to eq 'Michael'
184
+ expect(queue.peek).to eq 'Michael'
185
+ end
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,13 @@
1
+ require 'basic_queue'
2
+
3
+ RSpec.configure do |config|
4
+ # Run specs in random order to surface order dependencies. If you find an
5
+ # order dependency and want to debug it, you can fix the order by providing
6
+ # the seed, which is printed after each run.
7
+ # --seed 1234
8
+ config.order = 'random'
9
+
10
+ # when a focus tag is present in RSpec, only run tests with focus tag: http://railscasts.com/episodes/285-spork
11
+ config.filter_run :focus
12
+ config.run_all_when_everything_filtered = true
13
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basic_queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Imstepf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: It supports the usual enqueue and dequeue operations, along with methods
56
+ for peeking at the first item, testing if the queue is empty and counting the number
57
+ of items in the queue.
58
+ email:
59
+ - michael.imstepf@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - basic_queue.gemspec
70
+ - lib/basic_queue.rb
71
+ - lib/basic_queue/version.rb
72
+ - spec/basic_queue_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com/michaelimstepf/basic-queue
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A first-in-first-out (FIFO) queue data structure.
98
+ test_files:
99
+ - spec/basic_queue_spec.rb
100
+ - spec/spec_helper.rb