RubyDataStructures 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/LICENSE +3 -0
- data/README +3 -0
- data/RELEASE_NOTES +27 -0
- data/Rakefile +2 -0
- data/RubyDataStructures.gemspec +21 -0
- data/lib/RubyDataStructures/multi_dimensional_array.rb +42 -0
- data/lib/RubyDataStructures/queue_as_array.rb +66 -0
- data/lib/RubyDataStructures/ruby_data_structures.rb +3 -0
- data/lib/RubyDataStructures/stack_as_array.rb +61 -0
- data/lib/RubyDataStructures/version.rb +3 -0
- data/lib/RubyDataStructures.rb +7 -0
- data/test/RubyDataStructureTest.rb +9 -0
- data/test/multi_dimensional_array_test.rb +20 -0
- data/test/queue_as_array_test.rb +86 -0
- data/test/stack_as_array_test.rb +88 -0
- metadata +84 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
data/README
ADDED
data/RELEASE_NOTES
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
= Announce: RubyDatStructures Release 0.0.1
|
2
|
+
|
3
|
+
Release 0.0.1 makes the project public.
|
4
|
+
|
5
|
+
$ gem install RubyDataStructures
|
6
|
+
|
7
|
+
New features:
|
8
|
+
|
9
|
+
* MultiDimensionalArray
|
10
|
+
* StackAsArray
|
11
|
+
* QueueAsArray
|
12
|
+
|
13
|
+
Deprecation Notices:
|
14
|
+
|
15
|
+
* None
|
16
|
+
|
17
|
+
Bugs Fixed:
|
18
|
+
|
19
|
+
* None
|
20
|
+
|
21
|
+
Other Changes Include:
|
22
|
+
|
23
|
+
* None
|
24
|
+
|
25
|
+
== Thanks
|
26
|
+
|
27
|
+
-- Satyaram B V
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "RubyDataStructures/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "RubyDataStructures"
|
7
|
+
s.version = Rubydatastructures::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Satyaram B V"]
|
10
|
+
s.email = ["bvsatyaramATgmailDOTcom"]
|
11
|
+
s.homepage = "http://bvsatyaram.com"
|
12
|
+
s.summary = %q{Implementation of standard data structures in Ruby}
|
13
|
+
s.description = %q{Implementation of standard data structures in Ruby}
|
14
|
+
|
15
|
+
s.rubyforge_project = "RubyDataStructures"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class RubyDataStructures::MultiDimensionalArray
|
2
|
+
def initialize(*dimensions)
|
3
|
+
@dimensions = Array.new(dimensions.length)
|
4
|
+
@factors = Array.new(dimensions.length)
|
5
|
+
product = 1
|
6
|
+
i = dimensions.length - 1
|
7
|
+
|
8
|
+
while i >= 0
|
9
|
+
@dimensions[i] = dimensions[i]
|
10
|
+
@factors[i] = product
|
11
|
+
product *= dimensions[i]
|
12
|
+
i -= 1
|
13
|
+
end
|
14
|
+
|
15
|
+
@data = Array.new(product)
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_offset(indices)
|
19
|
+
raise IndexError if indices.length != @dimensions.length
|
20
|
+
|
21
|
+
offset = 0
|
22
|
+
|
23
|
+
(0..(@dimensions.length - 1)).each do |i|
|
24
|
+
if indices[i] < 0 || indices[i] >= @dimensions[i]
|
25
|
+
raise IndexError
|
26
|
+
end
|
27
|
+
|
28
|
+
offset += @factors[i]*indices[i]
|
29
|
+
end
|
30
|
+
|
31
|
+
return offset
|
32
|
+
end
|
33
|
+
|
34
|
+
def [](*indices)
|
35
|
+
@data[self.get_offset(indices)]
|
36
|
+
end
|
37
|
+
|
38
|
+
def []=(*indices_and_value)
|
39
|
+
value = indices_and_value.pop
|
40
|
+
@data[self.get_offset(indices_and_value)] = value
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
class QueueAsArray
|
2
|
+
# Initializes a stack of size +size+
|
3
|
+
# The value of +head+ for a new stack is +nil+
|
4
|
+
# The value of +tail+ for a new stack is +0+
|
5
|
+
def initialize(size = 1)
|
6
|
+
@length = size
|
7
|
+
self.reset
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns +true+ if the queue is empty
|
11
|
+
def empty?
|
12
|
+
@head.nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns +true+ if the queue is full
|
16
|
+
def full?
|
17
|
+
@head == @tail
|
18
|
+
end
|
19
|
+
|
20
|
+
# The queue is enqueued with +element+
|
21
|
+
def enqueue(element)
|
22
|
+
if self.full?
|
23
|
+
raise "Queue Overflow - The queue is full"
|
24
|
+
end
|
25
|
+
|
26
|
+
@array[@tail] = element
|
27
|
+
|
28
|
+
if @head.nil?
|
29
|
+
@head = 0
|
30
|
+
end
|
31
|
+
|
32
|
+
if @tail == @length - 1
|
33
|
+
@tail = 0
|
34
|
+
else
|
35
|
+
@tail = @tail + 1
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# The queue is dequeued
|
40
|
+
def dequeue
|
41
|
+
if self.empty?
|
42
|
+
raise "Queue Underflow - The queue is empty"
|
43
|
+
end
|
44
|
+
|
45
|
+
x = @array[@head]
|
46
|
+
|
47
|
+
if @head == @length - 1
|
48
|
+
@head = 0
|
49
|
+
else
|
50
|
+
@head = @head + 1
|
51
|
+
end
|
52
|
+
|
53
|
+
if @head == @tail
|
54
|
+
self.reset
|
55
|
+
end
|
56
|
+
|
57
|
+
return x
|
58
|
+
end
|
59
|
+
|
60
|
+
# Resets the queue
|
61
|
+
def reset
|
62
|
+
@array = Array.new(@length)
|
63
|
+
@head = nil
|
64
|
+
@tail = 0
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class StackAsArray
|
2
|
+
# Initializes a stack of size +size+
|
3
|
+
# The value of +top+ for an empty stack is +nil+
|
4
|
+
def initialize(size = 1)
|
5
|
+
@length = size
|
6
|
+
self.reset
|
7
|
+
end
|
8
|
+
|
9
|
+
# Returns +true+ if the stack is empty
|
10
|
+
def empty?
|
11
|
+
@top.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns +true+ if the stack is full
|
15
|
+
def full?
|
16
|
+
@top == @length - 1
|
17
|
+
end
|
18
|
+
|
19
|
+
def singleton?
|
20
|
+
@top == 0
|
21
|
+
end
|
22
|
+
|
23
|
+
# Pushes an element +element+ into the stack
|
24
|
+
def push(element)
|
25
|
+
if self.full?
|
26
|
+
raise "Stack Overflow - The stack is full"
|
27
|
+
end
|
28
|
+
|
29
|
+
if self.empty?
|
30
|
+
@top = 0
|
31
|
+
else
|
32
|
+
@top = @top + 1
|
33
|
+
end
|
34
|
+
|
35
|
+
@array[@top] = element
|
36
|
+
end
|
37
|
+
|
38
|
+
# Pops the stack
|
39
|
+
def pop
|
40
|
+
if self.empty?
|
41
|
+
raise "Stack Underflow - The stack is empty"
|
42
|
+
end
|
43
|
+
|
44
|
+
x = @array[@top]
|
45
|
+
if self.singleton?
|
46
|
+
@top = nil
|
47
|
+
else
|
48
|
+
@top = @top - 1
|
49
|
+
end
|
50
|
+
|
51
|
+
return x
|
52
|
+
end
|
53
|
+
|
54
|
+
# Resets the stack
|
55
|
+
def reset
|
56
|
+
@array = Array.new(@length)
|
57
|
+
@top = nil
|
58
|
+
end
|
59
|
+
|
60
|
+
#TODO: Implement *each* method
|
61
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# To change this template, choose Tools | Templates
|
2
|
+
# and open the template in the editor.
|
3
|
+
|
4
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
require 'RubyDataStructures'
|
8
|
+
|
9
|
+
class MultiDimensionalArrayTest < Test::Unit::TestCase
|
10
|
+
def setup
|
11
|
+
@mda = RubyDataStructures::MultiDimensionalArray.new(2,3,4)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_multi_dimensional_array
|
15
|
+
assert_equal nil, @mda[1,2,3]
|
16
|
+
|
17
|
+
@mda[1,2,3] = 5
|
18
|
+
assert_equal 5, @mda[1,2,3]
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# To change this template, choose Tools | Templates
|
2
|
+
# and open the template in the editor.
|
3
|
+
|
4
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
require 'RubyDataStructures'
|
8
|
+
|
9
|
+
class QueueAsArrayTest < Test::Unit::TestCase
|
10
|
+
def test_queue_as_array
|
11
|
+
queue = RubyDataStructures::QueueAsArray.new(3)
|
12
|
+
|
13
|
+
assert queue.empty?
|
14
|
+
assert !queue.full?
|
15
|
+
|
16
|
+
assert_raise RuntimeError, "Queue Underflow - The queue is empty" do
|
17
|
+
queue.dequeue
|
18
|
+
end
|
19
|
+
|
20
|
+
queue.enqueue(1)
|
21
|
+
assert !queue.empty?
|
22
|
+
assert !queue.full?
|
23
|
+
|
24
|
+
assert_equal 1, queue.dequeue
|
25
|
+
assert queue.empty?
|
26
|
+
assert !queue.full?
|
27
|
+
|
28
|
+
queue.enqueue(1)
|
29
|
+
assert !queue.empty?
|
30
|
+
assert !queue.full?
|
31
|
+
|
32
|
+
queue.enqueue(2)
|
33
|
+
assert !queue.empty?
|
34
|
+
assert !queue.full?
|
35
|
+
|
36
|
+
queue.enqueue(3)
|
37
|
+
assert !queue.empty?
|
38
|
+
assert queue.full?
|
39
|
+
|
40
|
+
assert_raise RuntimeError, "Queue Overflow - The queue is full" do
|
41
|
+
queue.enqueue(4)
|
42
|
+
end
|
43
|
+
|
44
|
+
assert_equal 1, queue.dequeue
|
45
|
+
assert !queue.empty?
|
46
|
+
assert !queue.full?
|
47
|
+
|
48
|
+
assert_equal 2, queue.dequeue
|
49
|
+
assert !queue.empty?
|
50
|
+
assert !queue.full?
|
51
|
+
|
52
|
+
queue.enqueue(7)
|
53
|
+
assert !queue.empty?
|
54
|
+
assert !queue.full?
|
55
|
+
|
56
|
+
assert_equal 3, queue.dequeue
|
57
|
+
assert !queue.empty?
|
58
|
+
assert !queue.full?
|
59
|
+
|
60
|
+
assert_equal 7, queue.dequeue
|
61
|
+
assert queue.empty?
|
62
|
+
assert !queue.full?
|
63
|
+
|
64
|
+
assert_raise RuntimeError, "Queue Underflow - The queue is empty" do
|
65
|
+
queue.dequeue
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_queue_as_array_reset
|
70
|
+
queue = RubyDataStructures::QueueAsArray.new(3)
|
71
|
+
|
72
|
+
assert queue.empty?
|
73
|
+
assert !queue.full?
|
74
|
+
|
75
|
+
queue.enqueue(1)
|
76
|
+
queue.enqueue(2)
|
77
|
+
|
78
|
+
assert !queue.empty?
|
79
|
+
assert !queue.full?
|
80
|
+
|
81
|
+
queue.reset
|
82
|
+
|
83
|
+
assert queue.empty?
|
84
|
+
assert !queue.full?
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# To change this template, choose Tools | Templates
|
2
|
+
# and open the template in the editor.
|
3
|
+
|
4
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
require 'RubyDataStructures'
|
8
|
+
|
9
|
+
class StackAsArrayTest < Test::Unit::TestCase
|
10
|
+
def test_stack_as_array
|
11
|
+
stack = RubyDataStructures::StackAsArray.new(3)
|
12
|
+
|
13
|
+
assert stack.empty?
|
14
|
+
assert !stack.singleton?
|
15
|
+
assert !stack.full?
|
16
|
+
|
17
|
+
assert_raise RuntimeError, "Stack Underflow - The stack is empty" do
|
18
|
+
stack.pop
|
19
|
+
end
|
20
|
+
|
21
|
+
stack.push(1)
|
22
|
+
assert !stack.empty?
|
23
|
+
assert stack.singleton?
|
24
|
+
assert !stack.full?
|
25
|
+
|
26
|
+
stack.push(2)
|
27
|
+
assert !stack.empty?
|
28
|
+
assert !stack.singleton?
|
29
|
+
assert !stack.full?
|
30
|
+
|
31
|
+
stack.push(3)
|
32
|
+
assert !stack.empty?
|
33
|
+
assert !stack.singleton?
|
34
|
+
assert stack.full?
|
35
|
+
|
36
|
+
assert_raise RuntimeError, "Stack Overflow - The stack is full" do
|
37
|
+
stack.push(4)
|
38
|
+
end
|
39
|
+
|
40
|
+
assert_equal 3, stack.pop
|
41
|
+
assert !stack.empty?
|
42
|
+
assert !stack.singleton?
|
43
|
+
assert !stack.full?
|
44
|
+
|
45
|
+
assert_equal 2, stack.pop
|
46
|
+
assert !stack.empty?
|
47
|
+
assert stack.singleton?
|
48
|
+
assert !stack.full?
|
49
|
+
|
50
|
+
stack.push(7)
|
51
|
+
assert !stack.empty?
|
52
|
+
assert !stack.singleton?
|
53
|
+
assert !stack.full?
|
54
|
+
|
55
|
+
assert_equal 7, stack.pop
|
56
|
+
assert !stack.empty?
|
57
|
+
assert stack.singleton?
|
58
|
+
assert !stack.full?
|
59
|
+
|
60
|
+
assert_equal 1, stack.pop
|
61
|
+
assert stack.empty?
|
62
|
+
assert !stack.singleton?
|
63
|
+
assert !stack.full?
|
64
|
+
|
65
|
+
assert_raise RuntimeError, "Stack Underflow - The stack is empty" do
|
66
|
+
stack.pop
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_stack_as_array_reset
|
71
|
+
stack = RubyDataStructures::StackAsArray.new(3)
|
72
|
+
|
73
|
+
assert stack.empty?
|
74
|
+
assert !stack.singleton?
|
75
|
+
assert !stack.full?
|
76
|
+
|
77
|
+
stack.push(1)
|
78
|
+
stack.push(2)
|
79
|
+
|
80
|
+
assert !stack.empty?
|
81
|
+
assert !stack.full?
|
82
|
+
|
83
|
+
stack.reset
|
84
|
+
|
85
|
+
assert stack.empty?
|
86
|
+
assert !stack.full?
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: RubyDataStructures
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Satyaram B V
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-05 00:00:00 +05:30
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Implementation of standard data structures in Ruby
|
23
|
+
email:
|
24
|
+
- bvsatyaramATgmailDOTcom
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README
|
36
|
+
- RELEASE_NOTES
|
37
|
+
- Rakefile
|
38
|
+
- RubyDataStructures.gemspec
|
39
|
+
- lib/RubyDataStructures.rb
|
40
|
+
- lib/RubyDataStructures/multi_dimensional_array.rb
|
41
|
+
- lib/RubyDataStructures/queue_as_array.rb
|
42
|
+
- lib/RubyDataStructures/ruby_data_structures.rb
|
43
|
+
- lib/RubyDataStructures/stack_as_array.rb
|
44
|
+
- lib/RubyDataStructures/version.rb
|
45
|
+
- test/RubyDataStructureTest.rb
|
46
|
+
- test/multi_dimensional_array_test.rb
|
47
|
+
- test/queue_as_array_test.rb
|
48
|
+
- test/stack_as_array_test.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://bvsatyaram.com
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project: RubyDataStructures
|
79
|
+
rubygems_version: 1.3.7
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Implementation of standard data structures in Ruby
|
83
|
+
test_files: []
|
84
|
+
|