datastructures 0.1.0
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/LICENSE +20 -0
- data/README.md +26 -0
- data/Rakefile +8 -0
- data/lib/datastructures.rb +2 -0
- data/lib/datastructures/queue.rb +42 -0
- data/lib/datastructures/version.rb +12 -0
- data/test/test_datastructures.rb +3 -0
- data/test/test_queue.rb +45 -0
- metadata +69 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Richard Smith
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
datastructures
|
2
|
+
==============
|
3
|
+
|
4
|
+
A collection of data structures in Ruby, made for my [data structures challenge](http://blahah.net/2013/08/18/algorithm-challenge)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
```bash
|
9
|
+
gem install datastructures
|
10
|
+
```
|
11
|
+
|
12
|
+
## Day 1: Queue
|
13
|
+
|
14
|
+
```
|
15
|
+
require 'datastructure'
|
16
|
+
queue = DataStructures::Queue.new
|
17
|
+
queue.enqueue('first')
|
18
|
+
queue.enqueue('second')
|
19
|
+
queue.size # => 2
|
20
|
+
queue.empty? # => false
|
21
|
+
queue.front # => 'first'
|
22
|
+
queue.back # => 'second'
|
23
|
+
queue.dequeue # => 'first'
|
24
|
+
queue.dequeue # => 'second'
|
25
|
+
queue.dequeue # => RuntimeError, "Queue underflow: nothing to dequeue"
|
26
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module DataStructures
|
2
|
+
# Implements a simple FIFO (first in, first out) queue data
|
3
|
+
# structure using an array container.
|
4
|
+
class Queue
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
self.clear
|
8
|
+
end
|
9
|
+
|
10
|
+
def size
|
11
|
+
@array.size
|
12
|
+
end
|
13
|
+
|
14
|
+
alias :length :size
|
15
|
+
|
16
|
+
def empty?
|
17
|
+
@array.empty?
|
18
|
+
end
|
19
|
+
|
20
|
+
def enqueue item
|
21
|
+
@array.push item
|
22
|
+
end
|
23
|
+
|
24
|
+
def dequeue
|
25
|
+
raise "Queue underflow: nothing to dequeue." if self.size == 0
|
26
|
+
@array.shift
|
27
|
+
end
|
28
|
+
|
29
|
+
def front
|
30
|
+
@array.first
|
31
|
+
end
|
32
|
+
|
33
|
+
def back
|
34
|
+
@array.last
|
35
|
+
end
|
36
|
+
|
37
|
+
def clear
|
38
|
+
@array = Array.new
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
data/test/test_queue.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'datastructures'
|
3
|
+
|
4
|
+
class TestQueue < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_queue
|
7
|
+
|
8
|
+
# we can create a queue
|
9
|
+
queue = DataStructures::Queue.new
|
10
|
+
|
11
|
+
# should start empty
|
12
|
+
assert queue.empty?
|
13
|
+
assert queue.size == 0
|
14
|
+
|
15
|
+
# underflow raises error
|
16
|
+
assert_raise RuntimeError, "Queue underflow: nothing to dequeue" do
|
17
|
+
queue.dequeue
|
18
|
+
end
|
19
|
+
|
20
|
+
# enqueueing increases size, removes emptiness
|
21
|
+
queue.enqueue('first')
|
22
|
+
assert !queue.empty?
|
23
|
+
assert queue.size == 1
|
24
|
+
|
25
|
+
# more enqueueing just increments size
|
26
|
+
queue.enqueue('second')
|
27
|
+
assert queue.size == 2
|
28
|
+
|
29
|
+
# dequeueing returns first in, reduces size
|
30
|
+
assert queue.dequeue == 'first'
|
31
|
+
assert queue.size == 1
|
32
|
+
|
33
|
+
# more dequeueing empties the queue
|
34
|
+
assert queue.dequeue == 'second'
|
35
|
+
assert queue.size == 0
|
36
|
+
assert queue.empty?
|
37
|
+
|
38
|
+
# trying to continue gives underflow
|
39
|
+
assert_raise RuntimeError, "Queue underflow: nothing to dequeue" do
|
40
|
+
queue.dequeue
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: datastructures
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Richard Smith
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: coveralls
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.6.7
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.6.7
|
30
|
+
description: ! ' 42 useful data structures implemented in pure Ruby for my data structures
|
31
|
+
challenge. '
|
32
|
+
email: rds45@cam.ac.uk
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- Rakefile
|
38
|
+
- lib/datastructures/queue.rb
|
39
|
+
- lib/datastructures/version.rb
|
40
|
+
- lib/datastructures.rb
|
41
|
+
- test/test_datastructures.rb
|
42
|
+
- test/test_queue.rb
|
43
|
+
- README.md
|
44
|
+
- LICENSE
|
45
|
+
homepage: https://github.com/Blahah/datastructures
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.24
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: 42 useful data structures
|
69
|
+
test_files: []
|