ruby_collections 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +60 -2
- data/lib/ruby_collections/collections.rb +2 -1
- data/lib/ruby_collections/deque.rb +131 -0
- data/lib/ruby_collections/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0462986d07093cf4ecca10470680ebbafcda8d9
|
4
|
+
data.tar.gz: f722323cd43f7b4d5f4abb4301dfd2fb9ae57fd5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a78827b01c0fd4fc5fd2908d5f668d35f341236dce3fbd5018ce652c332fe5da1976fa47d4c730e81fadd50109b1a787a8f1f87a6e9e04acd4bc9d2a83d300a
|
7
|
+
data.tar.gz: 96b622b4028421d8624f3536e9a2d36d6540f6b0de02a5f8b7ee387d7330e895508dcb5d2ded53ff82b058b6c41ebf430651ff2c670870e1bfc30667476d945a
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
Supported Data Structures: Max Heap, Min Heap, Stack, LinkedList
|
23
|
+
Supported Data Structures: Max Heap, Min Heap, Stack, LinkedList, Deque
|
24
24
|
|
25
25
|
### RubyCollections::MaxHeap
|
26
26
|
|
@@ -72,7 +72,7 @@ list.size # => 0
|
|
72
72
|
|
73
73
|
list.header # => ""
|
74
74
|
|
75
|
-
list.
|
75
|
+
list.empty? # => true
|
76
76
|
|
77
77
|
list.add(1) # => 1 (number of elements in list)
|
78
78
|
|
@@ -98,6 +98,64 @@ list.get(1).getNext # => 12
|
|
98
98
|
|
99
99
|
```
|
100
100
|
|
101
|
+
### RubyCollections::Deque
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
|
105
|
+
list = RubyCollections::Deque.new
|
106
|
+
|
107
|
+
list.size # => 0
|
108
|
+
|
109
|
+
list.head # => nil
|
110
|
+
|
111
|
+
list.empty? # => true
|
112
|
+
|
113
|
+
list.add(2) # => 1 (number of elements in list), list: [2]
|
114
|
+
|
115
|
+
list.add(1) # => 2 (number of elements in list), list: [1,2]
|
116
|
+
|
117
|
+
list.to_s # => "[1, 2]"
|
118
|
+
|
119
|
+
# add 3 at index 1
|
120
|
+
|
121
|
+
list.add(3,1) # => 3 (number of elements in list), list: [1, 3, 2]
|
122
|
+
|
123
|
+
# add 5 at first postion
|
124
|
+
|
125
|
+
list.add_first(5) # list: [5, 1, 3, 2]
|
126
|
+
|
127
|
+
# see tail element
|
128
|
+
|
129
|
+
list.tail # => 2
|
130
|
+
|
131
|
+
#add 6 at last position
|
132
|
+
|
133
|
+
list.add_last(6) # list: [5, 1, 3, 2, 6]
|
134
|
+
|
135
|
+
list.size # => 5
|
136
|
+
|
137
|
+
# get element at index 2
|
138
|
+
|
139
|
+
list.get(2) # => 3
|
140
|
+
|
141
|
+
# set element at index 1 to 10
|
142
|
+
|
143
|
+
list.set(1, 10) # list; [5, 10, 3, 2, 6]
|
144
|
+
|
145
|
+
# remove element at index 2
|
146
|
+
|
147
|
+
list.remove(2) # list: [5, 10, 2, 6]
|
148
|
+
|
149
|
+
# remove first element
|
150
|
+
|
151
|
+
list.remove_first # list: [10, 2, 6]
|
152
|
+
|
153
|
+
# remove last element
|
154
|
+
|
155
|
+
list.remove_last # list: [10, 2]
|
156
|
+
|
157
|
+
```
|
158
|
+
|
101
159
|
## Development
|
102
160
|
|
103
161
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
module RubyCollections
|
3
|
+
class Deque
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@hash = {size: 0, front: {data: nil, next: :rear, prev: nil},
|
7
|
+
rear: {data: nil, next: nil, prev: :front}}
|
8
|
+
end
|
9
|
+
|
10
|
+
def empty?
|
11
|
+
size.zero?
|
12
|
+
end
|
13
|
+
|
14
|
+
def size
|
15
|
+
hash[:size]
|
16
|
+
end
|
17
|
+
|
18
|
+
def head
|
19
|
+
hash[front[:next]][:data]
|
20
|
+
end
|
21
|
+
|
22
|
+
def tail
|
23
|
+
hash[rear[:prev]][:data]
|
24
|
+
end
|
25
|
+
|
26
|
+
def add(data, index = 0)
|
27
|
+
return nil if index > size
|
28
|
+
if index == 0 then add_first(data) and return end
|
29
|
+
setNext(getNode(index-1), data) if index > 0 and index < size
|
30
|
+
hash[:size] += 1
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_first(data)
|
34
|
+
setNext(:front, data)
|
35
|
+
hash[:size] += 1
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_last(data)
|
39
|
+
setPrev(:rear, data)
|
40
|
+
hash[:size] += 1
|
41
|
+
end
|
42
|
+
|
43
|
+
def get(index)
|
44
|
+
return nil if index >= size
|
45
|
+
hash[getNode(index)][:data]
|
46
|
+
end
|
47
|
+
|
48
|
+
def set(index, data)
|
49
|
+
return nil if index >= size
|
50
|
+
hash[getNode(index)][:data] = data
|
51
|
+
end
|
52
|
+
|
53
|
+
def remove(index)
|
54
|
+
return nil if index >= size or index < 0
|
55
|
+
node_id = getNode(index)
|
56
|
+
hash[getNext(node_id)][:prev] = getPrev(node_id)
|
57
|
+
hash[getPrev(node_id)][:next] = getNext(node_id)
|
58
|
+
hash.delete(node_id)
|
59
|
+
hash[:size] -= 1
|
60
|
+
end
|
61
|
+
|
62
|
+
def remove_first
|
63
|
+
return nil if empty?
|
64
|
+
remove(0)
|
65
|
+
end
|
66
|
+
|
67
|
+
def remove_last
|
68
|
+
return nil if empty?
|
69
|
+
node_id = getPrev(:rear)
|
70
|
+
prev_id = getPrev(node_id)
|
71
|
+
hash[prev_id][:next] = :rear
|
72
|
+
rear[:prev] = prev_id
|
73
|
+
hash.delete(node_id)
|
74
|
+
hash[:size] -= 1
|
75
|
+
end
|
76
|
+
|
77
|
+
def to_s
|
78
|
+
return "" if empty?
|
79
|
+
data = []
|
80
|
+
node_id = :front
|
81
|
+
size.times do
|
82
|
+
node_id = getNext(node_id)
|
83
|
+
data << hash[node_id][:data]
|
84
|
+
end
|
85
|
+
return data.to_s
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def hash
|
91
|
+
@hash
|
92
|
+
end
|
93
|
+
|
94
|
+
def front
|
95
|
+
hash[:front]
|
96
|
+
end
|
97
|
+
|
98
|
+
def rear
|
99
|
+
hash[:rear]
|
100
|
+
end
|
101
|
+
|
102
|
+
def setNext(node_id, data)
|
103
|
+
uuid = SecureRandom.uuid
|
104
|
+
hash[uuid] = {data: data, next: getNext(node_id), prev: node_id}
|
105
|
+
hash[getNext(node_id)][:prev] = uuid
|
106
|
+
hash[node_id][:next] = uuid
|
107
|
+
end
|
108
|
+
|
109
|
+
def setPrev(node_id, data)
|
110
|
+
uuid = SecureRandom.uuid
|
111
|
+
hash[uuid] = {data: data, next: node_id, prev: getPrev(node_id)}
|
112
|
+
hash[getPrev(node_id)][:next] = uuid
|
113
|
+
hash[node_id][:prev] = uuid
|
114
|
+
end
|
115
|
+
|
116
|
+
def getNext(node_id)
|
117
|
+
hash[node_id][:next]
|
118
|
+
end
|
119
|
+
|
120
|
+
def getPrev(node_id)
|
121
|
+
hash[node_id][:prev]
|
122
|
+
end
|
123
|
+
|
124
|
+
def getNode(index)
|
125
|
+
node = front[:next]
|
126
|
+
index.times {node = getNext(node)}
|
127
|
+
return node
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_collections
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vikash Vikram
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- bin/setup
|
59
59
|
- lib/ruby_collections.rb
|
60
60
|
- lib/ruby_collections/collections.rb
|
61
|
+
- lib/ruby_collections/deque.rb
|
61
62
|
- lib/ruby_collections/linked_list.rb
|
62
63
|
- lib/ruby_collections/max_heap.rb
|
63
64
|
- lib/ruby_collections/min_heap.rb
|