Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@woodie
Last active December 15, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodie/5179448 to your computer and use it in GitHub Desktop.
Save woodie/5179448 to your computer and use it in GitHub Desktop.
# A Queue of fixed size, using an array for storage
class FixedQueue
attr_reader added:int, removed:int
def initialize(capacity:int)
@added = 0
@removed = 0
@capacity = capacity
@data = Object[capacity]
end
def isEmpty?
(@removed >= @added)
end
def isFull?
(@added >= @removed + @capacity)
end
def add(obj:Object)
if isFull?
false
else
@data[@added % @capacity] = obj
@added += 1
true
end
end
def remove
if isEmpty?
nil
else
@removed += 1
@data[(@removed - 1) % @capacity]
end
end
def to_s
out = ""
@removed.upto(@added - 1) do |i|
out += ", #{@data[i % @capacity]}"
end
"{" + out.substring(out.length > 0 ? 1 : 0) + " }"
end
end
# rvm jruby
# rspec -fs spec/fixed_queue_spec.rb
require 'java'
import 'cis.FixedQueue'
describe FixedQueue, 'once queue is created' do
before(:each) do
@capacity = 9
@queue = FixedQueue.new(@capacity)
end
context 'when empty' do
it 'should respond as empty' do
@queue.should be_empty
end
it 'should not respond as full' do
@queue.should_not be_full
end
it 'should return null on remove()' do
@queue.remove.should be_false
end
it 'should report none added() or removed()' do
@queue.added.should == 0
@queue.removed.should == 0
end
it 'should provide string representation' do
@queue.to_s.should eq "{ }"
end
end
context 'when items have been added' do
context 'when under capacity' do
before(:each) do
%w{foo bar baz}.each {|s| @queue.add(s)}
end
it 'should not report empty' do
@queue.should_not be_empty
end
it 'should respond true on add(obj)' do
@queue.should be_add('zippy')
end
it 'should report number added() and removed()' do
@queue.added.should == 3
@queue.removed.should == 0
end
it 'should provide string representation' do
@queue.to_s.should eq "{ foo, bar, baz }"
end
end
context 'when at capacity' do
before(:each) do
@capacity.times {@queue.add('foo')}
end
it 'should respond as full' do
@queue.should be_full
end
it 'should respond false on add(obj)' do
@capacity.times {@queue.add('foo')}
@queue.should_not be_add('foo')
end
end
context 'when beyond initial capacity after remove()' do
before(:each) do
@capacity.times {@queue.add('foo')}
@capacity.times {@queue.remove}
%w{foo bar baz}.each {|s| @queue.add(s)}
end
it 'should report number added() and removed()' do
@queue.added.should == @capacity + 3
@queue.removed.should == @capacity
end
it 'should provide string representation' do
@queue.to_s.should eq "{ foo, bar, baz }"
end
end
end
context 'when items are removed' do
before(:each) do
%w{foo bar baz}.each {|s| @queue.add(s)}
end
it 'should return first Object added: (FIFO)' do
@queue.remove.should eq "foo"
@queue.remove.should eq "bar"
@queue.to_s.should eq "{ baz }"
end
it 'should still return null on remove() once empty' do
3.times {@queue.remove}
@queue.remove.should be_false
end
end
end
__END__
$ rvm jruby
$ rspec -fs spec/fixed_queue_spec.rb
Java::Cis::FixedQueue once queue is created
when empty
should respond as empty
should not respond as full
should return null on remove()
should report none added() or removed()
should provide string representation
when items have been added
when under capacity
should not report empty
should respond true on add(obj)
should report number added() and removed()
should provide string representation
when at capacity
should respond as full
should respond false on add(obj)
when beyond initial capacity after remove()
should report number added() and removed()
should provide string representation
when items are removed
should return first Object added: (FIFO)
should still return null on remove() once empty
Finished in 0.165 seconds
15 examples, 0 failures
// A Queue of fixed size, using an array for storage
package cis;
public class FixedQueue {
private int added = 0;
private int removed = 0;
private int capacity;
private Object[] data;
public int added() { return added; }
public int removed() { return removed; }
public FixedQueue(int capacity) {
this.capacity = capacity;
this.data = new Object[capacity];
}
public boolean isEmpty() {
return (removed >= added);
}
public boolean isFull() {
return (added >= removed + capacity);
}
public boolean add(Object obj) {
if (isFull()) {
return false;
} else {
data[added % capacity] = obj;
added++;
return true;
}
}
public Object remove() {
if (isEmpty()) {
return null;
} else {
removed++;
return data[(removed - 1) % capacity];
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = removed; i < added; i++) {
sb.append(", " + data[i % capacity]);
}
return "{" + sb.substring(sb.length() > 0 ? 1 : 0) + " }";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment