Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Push to development

  • Loading branch information...
commit bb7dd64364ca4ae6e1126222b1ca9e74361d5706 1 parent d63622f
@samdunne authored
View
20 LICENSE.txt
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Sam Dunne
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
5 lib/rdata.rb
@@ -1,8 +1,13 @@
require 'rdata/version'
require "rdata/stack.rb"
+require "rdata/queue.rb"
module RData
def self.Stack
Stack.new
end
+
+ def self.Queue
+ Queue.new
+ end
end
View
23 lib/rdata/queue.rb
@@ -4,18 +4,35 @@ class Queue
def initialize
@queue = Array[]
- @head, @tail = 0
+ @head = 0
+ @tail = 0
+ end
+
+ def head
+ return @head
+ end
+
+ def tail
+ return @tail
+ end
+
+ def length
+ return (@head - @tail)
end
def enqueue(x)
@queue[@tail] = x
- @tail = (@tail == @length) ? 1 : @tail - 1
+ @tail = (@tail == self.length) ? 1 : (@tail + 1)
end
def dequeue
x = @head
- @head = (@head == @length) ? 1 : @head + 1
+ @head = (@head == self.length) ? 1 : (@head + 1)
return x
+ end
+
+ def is_empty?
+ (self.length == 0) ? true : false
end
end
View
2  lib/rdata/stack.rb
@@ -36,7 +36,7 @@ def pop
end
def is_empty?
- (@top == 0) ? 'true' : 'false'
+ (@top == 0) ? true : false
end
end
View
14 spec/rdata_spec.rb
@@ -1,11 +1,23 @@
require 'spec_helper'
+# Stack Tests
describe RData do
it "should return data for a stack" do
element_data = RData.Stack
- #element_data.should be_nil
element_data.push(1).should == 1
element_data.pop.should == 1
element_data.is_empty?.should be_true
end
+end
+
+# Queue Tests
+describe RData do
+ it "should return data from a queue" do
+ queue = RData.Queue
+ queue.enqueue(2).should == 1
+ queue.dequeue.should == 0
+ queue.is_empty?.should be_true
+ queue.enqueue(3).should == 2
+ queue.is_empty?.should be_false
+ end
end
View
0  version
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.