circular_queue 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -0
- data/circular_queue.gemspec +1 -1
- data/lib/circular_queue.rb +27 -0
- data/spec/circular_queue_spec.rb +13 -0
- metadata +8 -6
data/README.md
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://secure.travis-ci.org/alindeman/circular_queue.png)](http://travis-ci.org/alindeman/circular_queue)
|
4
4
|
|
5
|
+
Data structure that uses a single, fixed-size buffer as if it were connected
|
6
|
+
end-to-end.
|
7
|
+
|
5
8
|
**circular_queue** *requires* Ruby 1.9 or a Ruby implementation that
|
6
9
|
supports 1.9 syntax and standard library (e.g., JRuby in 1.9 mode;
|
7
10
|
Rubinius 2.0 should work if/when Mutex#sleep is implemented).
|
data/circular_queue.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "circular_queue"
|
6
|
-
s.version = "0.0.
|
6
|
+
s.version = "0.0.4"
|
7
7
|
s.authors = ["Andy Lindeman"]
|
8
8
|
s.email = ["alindeman@gmail.com"]
|
9
9
|
s.homepage = "https://github.com/alindeman/circular_queue"
|
data/lib/circular_queue.rb
CHANGED
@@ -109,12 +109,39 @@ class CircularQueue
|
|
109
109
|
@size == @capacity
|
110
110
|
end
|
111
111
|
|
112
|
+
# Returns thee first/oldest item in the queue
|
113
|
+
# @return [Object]
|
114
|
+
# Peek at first item without removing
|
115
|
+
def front
|
116
|
+
@mutex.synchronize do
|
117
|
+
@data[@front]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns the last/most recent item in the queue
|
122
|
+
# @return [Object]
|
123
|
+
# Peek at last item without removing
|
124
|
+
def back
|
125
|
+
@mutex.synchronize do
|
126
|
+
@data[(@back - 1) % @capacity]
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
112
130
|
# Returns the number of threads waiting for items to arrive in the queue
|
113
131
|
# @return [Integer] number of threads waiting
|
114
132
|
def num_waiting
|
115
133
|
@waiting.length
|
116
134
|
end
|
117
135
|
|
136
|
+
# Returns the data in the queue
|
137
|
+
# @return [Array] the queue
|
138
|
+
# Allows for easy iteration of queue from front to back
|
139
|
+
def data
|
140
|
+
@mutex.synchronize do
|
141
|
+
@data.clone.rotate @front
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
118
145
|
private
|
119
146
|
|
120
147
|
def enq_item(item)
|
data/spec/circular_queue_spec.rb
CHANGED
@@ -13,6 +13,8 @@ describe CircularQueue do
|
|
13
13
|
describe "adding items" do
|
14
14
|
it "accepts new items" do
|
15
15
|
subject.enq(1234)
|
16
|
+
subject.front.should == 1234
|
17
|
+
subject.back.should == 1234
|
16
18
|
subject.deq.should == 1234
|
17
19
|
end
|
18
20
|
|
@@ -21,6 +23,17 @@ describe CircularQueue do
|
|
21
23
|
subject.size.should == 1
|
22
24
|
end
|
23
25
|
|
26
|
+
it "allows for peeking at first and last items" do
|
27
|
+
subject.enq(1)
|
28
|
+
|
29
|
+
subject.front.should == 1
|
30
|
+
subject.back.should == 1
|
31
|
+
|
32
|
+
2.upto(capacity) { |i| subject.enq(i) }
|
33
|
+
subject.front.should == 1
|
34
|
+
subject.back.should == capacity
|
35
|
+
end
|
36
|
+
|
24
37
|
it "presents the appearance of accepting infinite items" do
|
25
38
|
1.upto(capacity * 2) { |i| subject.enq(i) }
|
26
39
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: circular_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -91,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
91
|
version: '0'
|
92
92
|
segments:
|
93
93
|
- 0
|
94
|
-
hash: -
|
94
|
+
hash: -2396495672156037732
|
95
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
96
|
none: false
|
97
97
|
requirements:
|
@@ -100,13 +100,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
version: '0'
|
101
101
|
segments:
|
102
102
|
- 0
|
103
|
-
hash: -
|
103
|
+
hash: -2396495672156037732
|
104
104
|
requirements: []
|
105
105
|
rubyforge_project: circular_queue
|
106
|
-
rubygems_version: 1.8.
|
106
|
+
rubygems_version: 1.8.24
|
107
107
|
signing_key:
|
108
108
|
specification_version: 3
|
109
109
|
summary: Data structure that uses a single, fixed-size buffer as if it were connected
|
110
110
|
end-to-end
|
111
|
-
test_files:
|
111
|
+
test_files:
|
112
|
+
- spec/circular_queue_spec.rb
|
113
|
+
- spec/spec_helper.rb
|
112
114
|
has_rdoc:
|