RubyDataStructures 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
File without changes
data/Gemfile CHANGED
File without changes
data/LICENSE CHANGED
File without changes
data/README.md CHANGED
@@ -22,10 +22,25 @@ Usage
22
22
  The `RubyDatStructures` library can be easily imported by running:
23
23
 
24
24
  require 'rubygems'
25
- require 'RubyDatStructures'
25
+ require 'RubyDataStructures'
26
26
 
27
27
  The usage of each of the data structures is described below.
28
28
 
29
+ You can enable macros to more conveniently create these data structures by:
30
+
31
+ require 'RubyDataStructures/macros'
32
+
33
+ @stack = stack(3)
34
+ @fifo = fifo_stack(5)
35
+ @arr = multi_array(1,2,3)
36
+ ...
37
+
38
+ ## Tests
39
+
40
+ Run tests/specs from console:
41
+
42
+ rake test
43
+
29
44
  ## Multi Dimensional Array
30
45
 
31
46
  A `multi dimensional array` can be initialized by running:
@@ -72,6 +87,10 @@ Examples:
72
87
  my_stach.push(3)
73
88
  my_stack.pop # Return `3`
74
89
 
90
+ *Notes:*
91
+ Why not use the internal `#push` and `#pop` methods of Array that already makes an Array simulate a stack? Why create the initial array with the full max length?
92
+ Perhaps make this a config option?
93
+
75
94
  ## Queue
76
95
 
77
96
  A `Queue` can be initialized by calling:
data/RELEASE_NOTES CHANGED
@@ -1,3 +1,14 @@
1
+ = Announce: Ruby DataStructures Release 1.1.0
2
+
3
+ - Added FifoStack.
4
+ - Added macros
5
+ - Extra Documentation
6
+ - Rakefile for running tests
7
+
8
+ = Announce: Ruby DataStructures Release 1.0.0
9
+
10
+ Basic data structures in place
11
+
1
12
  = Announce: Ruby DataStructures Release 0.0.4
2
13
  Date Mar 5, 2011
3
14
  Release 0.0.4 is just a dummy release to fix some gem push issues.
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  require 'bundler'
2
- Bundler::GemHelper.install_tasks
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList['test/*_test.rb']
9
+ t.verbose = true
10
+ end
File without changes
@@ -4,6 +4,7 @@ require 'RubyDataStructures/ruby_data_structures'
4
4
  # All RubyDataStructures
5
5
  require 'RubyDataStructures/multi_dimensional_array'
6
6
  require 'RubyDataStructures/stack_as_array'
7
+ require 'RubyDataStructures/fifo_stack'
7
8
  require 'RubyDataStructures/queue_as_array'
8
9
  require 'RubyDataStructures/singly_linked_list'
9
10
  require 'RubyDataStructures/singly_linked_list/element'
File without changes
File without changes
@@ -0,0 +1,17 @@
1
+ class RubyDataStructures::FifoStack < RubyDataStructures::StackAsArray
2
+ def push(element)
3
+
4
+ if self.full?
5
+ @array.shift
6
+ @top -= 1
7
+ end
8
+
9
+ if self.empty?
10
+ @top = 0
11
+ else
12
+ @top = @top + 1
13
+ end
14
+
15
+ @array[@top] = element
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ def fifo_stack size=1
2
+ RubyDataStructures::FifoStack.new size
3
+ end
4
+
5
+ def stack size=1
6
+ RubyDataStructures::Stack.new size
7
+ end
8
+
9
+ def multi_array(*dimensions)
10
+ RubyDataStructures::MultiDimensionalArray.new(*dimensions)
11
+ end
12
+
13
+ def max_heap(*array)
14
+ RubyDataStructures::MaxHeap.build(array.flatten)
15
+ end
16
+
17
+ def linked_list(type = :single)
18
+ case type
19
+ when :single
20
+ RubyDataStructures::SinglyLinkedList.new
21
+ when :double, :dbl
22
+ RubyDataStructures::DoublyLinkedList.new
23
+ else
24
+ raise ArgumentError, "Unknown or unsupported kind of Linked List: #{type}, must be: :single or :double"
25
+ end
26
+ end
27
+
28
+ def aqueue size
29
+ RubyDataStructures::QueueAsArray.new size
30
+ end
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -1,4 +1,6 @@
1
1
  class RubyDataStructures::StackAsArray
2
+ attr_reader :length, :top
3
+
2
4
  # Initializes a stack of size +size+
3
5
  # The value of +top+ for an empty stack is +nil+
4
6
  def initialize(size = 1)
@@ -53,6 +55,23 @@ class RubyDataStructures::StackAsArray
53
55
  @array = Array.new(@length)
54
56
  @top = nil
55
57
  end
58
+ alias_method :reset!, :reset
59
+
60
+ def size
61
+ @array.size
62
+ end
63
+
64
+ def first
65
+ @array.first
66
+ end
56
67
 
57
- #TODO: Implement *each* method
68
+ def last
69
+ @array[@top]
70
+ end
71
+
72
+ def each
73
+ @array.each do
74
+ yield
75
+ end
76
+ end
58
77
  end
@@ -1,3 +1,3 @@
1
1
  module Rubydatastructures
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
File without changes
File without changes
File without changes
@@ -0,0 +1,54 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'RubyDataStructures'
5
+
6
+ class AutoStackTest < Test::Unit::TestCase
7
+ def test_fifo_stack
8
+ stack = RubyDataStructures::FifoStack.new(2)
9
+
10
+ assert stack.empty?
11
+ assert !stack.singleton?
12
+ assert !stack.full?
13
+ assert stack.length == 2
14
+ # assert stack.size == 0
15
+
16
+ assert_raise RuntimeError, "Stack Underflow - The stack is empty" do
17
+ stack.pop
18
+ end
19
+
20
+ stack.push(1)
21
+ assert !stack.empty?
22
+ assert stack.singleton?
23
+ assert !stack.full?
24
+ assert stack.first == 1
25
+ assert stack.length == 2
26
+ assert stack.last == 1
27
+ # assert stack.size == 1
28
+
29
+ stack.push(2)
30
+ assert !stack.empty?
31
+ assert !stack.singleton?
32
+ assert stack.full?
33
+ assert stack.first == 1
34
+ assert stack.last == 2
35
+
36
+ stack.push(3)
37
+ assert !stack.empty?
38
+ assert !stack.singleton?
39
+ assert stack.full?
40
+ assert stack.size == 2
41
+ assert stack.first == 2
42
+ assert stack.last == 3
43
+
44
+ stack.push(4)
45
+ assert !stack.empty?
46
+ assert !stack.singleton?
47
+ assert stack.full?
48
+ assert stack.first == 3
49
+ assert stack.last == 4
50
+ assert stack.pop == 4
51
+ assert !stack.full?
52
+ assert stack.size == 2
53
+ end
54
+ end
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
metadata CHANGED
@@ -1,33 +1,23 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: RubyDataStructures
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 0
9
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Satyaram B V
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-05-16 00:00:00 +05:30
18
- default_executable:
12
+ date: 2012-11-25 00:00:00.000000000 Z
19
13
  dependencies: []
20
-
21
14
  description: Implementation of standard data structures in Ruby
22
- email:
15
+ email:
23
16
  - bvsatyaramATgmailDOTcom
24
17
  executables: []
25
-
26
18
  extensions: []
27
-
28
19
  extra_rdoc_files: []
29
-
30
- files:
20
+ files:
31
21
  - .gitignore
32
22
  - Gemfile
33
23
  - LICENSE
@@ -38,6 +28,8 @@ files:
38
28
  - lib/RubyDataStructures.rb
39
29
  - lib/RubyDataStructures/doubly_linked_list.rb
40
30
  - lib/RubyDataStructures/doubly_linked_list/element.rb
31
+ - lib/RubyDataStructures/fifo_stack.rb
32
+ - lib/RubyDataStructures/macros.rb
41
33
  - lib/RubyDataStructures/max_heap.rb
42
34
  - lib/RubyDataStructures/multi_dimensional_array.rb
43
35
  - lib/RubyDataStructures/queue_as_array.rb
@@ -49,41 +41,35 @@ files:
49
41
  - test/RubyDataStructureTest.rb
50
42
  - test/doubly_linked_list_element_test.rb
51
43
  - test/doubly_linked_list_test.rb
44
+ - test/fifo_stack_test.rb
52
45
  - test/max_heap_test.rb
53
46
  - test/multi_dimensional_array_test.rb
54
47
  - test/queue_as_array_test.rb
55
48
  - test/singly_linked_list_element_test.rb
56
49
  - test/singly_linked_list_test.rb
57
50
  - test/stack_as_array_test.rb
58
- has_rdoc: true
59
51
  homepage: http://bvsatyaram.com
60
52
  licenses: []
61
-
62
53
  post_install_message:
63
54
  rdoc_options: []
64
-
65
- require_paths:
55
+ require_paths:
66
56
  - lib
67
- required_ruby_version: !ruby/object:Gem::Requirement
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- segments:
72
- - 0
73
- version: "0"
74
- required_rubygems_version: !ruby/object:Gem::Requirement
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- segments:
79
- - 0
80
- version: "0"
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
81
69
  requirements: []
82
-
83
70
  rubyforge_project: RubyDataStructures
84
- rubygems_version: 1.3.6
71
+ rubygems_version: 1.8.24
85
72
  signing_key:
86
73
  specification_version: 3
87
74
  summary: Implementation of standard data structures in Ruby
88
75
  test_files: []
89
-