algorithmix 0.0.0.3 → 0.0.0.4
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.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/algorithmix.gemspec +8 -4
- data/lib/algorithmix/data_structure/generic/deque.rb +394 -0
- data/lib/algorithmix/version.rb +1 -1
- metadata +11 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac9e4d6a402a8e5a2114525fc9309e68f64e44b2
|
4
|
+
data.tar.gz: 3f04fc2ca5c59e1b3287372e629bda85319cbf1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b6271b398a3905cbcf52dc50800d3098362de2fd28b3ffffd882576487549d38efb7be18b85d56335567597aa89e5250937c4d063bcc5297246739586cfa30c
|
7
|
+
data.tar.gz: 28d57ee9d13a571051cc00e0858c7d97dee1a3f307f03d541d88c507973f966495611449986a35aee4d593a0da92d30cdb5f4e1293e8cb8615dfa6134ffa5d05
|
data/README.md
CHANGED
@@ -9,6 +9,10 @@ The gem contains various implementations of known and unknown data structures an
|
|
9
9
|
:construction: Because the gem is still not ready, with each new implementation of a data structure or an algorithm, a new
|
10
10
|
minor version will be released.
|
11
11
|
|
12
|
+
 [**BinaryHeap**](https://github.com/monzita/algorithmix/wiki/BinaryHeap)
|
13
|
+
|
14
|
+
 [**Deque**](https://github.com/monzita/algorithmix/wiki/Deque)
|
15
|
+
|
12
16
|
 [**Queue**](https://github.com/monzita/algorithmix/wiki/Queue) implementation & option for duplication of elements in [**Stack**](https://github.com/monzita/algorithmix/wiki/Stack)
|
13
17
|
|
14
18
|
 [**Stack**](https://github.com/monzita/algorithmix/wiki/Stack) implementation is released.
|
data/algorithmix.gemspec
CHANGED
@@ -8,11 +8,15 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Algorithmix::VERSION
|
9
9
|
spec.authors = ["Monika Ilieva"]
|
10
10
|
spec.email = ["*@*.*"]
|
11
|
+
spec.summary = %q{The gem contains various implementations of known and uknown data structures and algorithms}
|
12
|
+
spec.description = <<-DOC
|
13
|
+
Currently the gem contains:
|
14
|
+
1. Stack
|
15
|
+
2. Queue
|
16
|
+
3. Deque
|
17
|
+
4. Binary heap
|
11
18
|
|
12
|
-
|
13
|
-
spec.description = %q{The gem contains various implementations of known and uknown data structure and algorithms, which will be useful for developers.\n
|
14
|
-
Currently the gem contains:\n
|
15
|
-
1. Stack\n2. Queue\n3. Deque\n Check the documentation for more information.}
|
19
|
+
DOC
|
16
20
|
spec.homepage = "https://github.com/monzita/algorithmix/wiki"
|
17
21
|
spec.license = "MIT"
|
18
22
|
|
@@ -0,0 +1,394 @@
|
|
1
|
+
# Documentation: https://github.com/monzita/algorithmix/wiki/Deque
|
2
|
+
|
3
|
+
module Algorithmix
|
4
|
+
module DataStructure
|
5
|
+
module Generic
|
6
|
+
|
7
|
+
class Deque
|
8
|
+
|
9
|
+
# Creates a new deque.
|
10
|
+
#
|
11
|
+
# @param obj [#to_a]
|
12
|
+
# @kwarg copy [true, false]
|
13
|
+
# @return newly created deque
|
14
|
+
# @raise ArgumentError, if given obj doesn't respond to #to_a method
|
15
|
+
def initialize(obj = nil, copy: false)
|
16
|
+
@container = []
|
17
|
+
obj.nil? ? nil : from_obj(obj, copy)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Assigns content of an obj, to content of the deque.
|
21
|
+
#
|
22
|
+
# @param obj [#to_a]
|
23
|
+
# @kwarg copy [true, false] by default is set to false, which means that no copy of the object will be made
|
24
|
+
# @raise ArgumentError, if given object doesn't respond to #to_a method
|
25
|
+
# @return self object
|
26
|
+
def assign(obj, copy: false)
|
27
|
+
from_obj(obj, copy)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Inserts a new element at the beginning of the deque.
|
31
|
+
#
|
32
|
+
# @param value
|
33
|
+
# @return self object
|
34
|
+
def push_front(value)
|
35
|
+
@container.unshift(value)
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
# (see #push_front)
|
40
|
+
def >>(value)
|
41
|
+
push_front(value)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Inserts a new element at the end of the deque.
|
45
|
+
#
|
46
|
+
# @param value
|
47
|
+
# @return self object
|
48
|
+
def push_back(value)
|
49
|
+
@container << value
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
# (see #push_back)
|
54
|
+
def <<(value)
|
55
|
+
push_back(value)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Removes first element of the deque.
|
59
|
+
#
|
60
|
+
# @raise Algorithmix::EmptyContainerError, if the deque is empty.
|
61
|
+
# @return first element of the deque
|
62
|
+
def pop_front
|
63
|
+
raise EmptyContainerError, "The deque is empty." if @container.empty?
|
64
|
+
@container.shift
|
65
|
+
end
|
66
|
+
|
67
|
+
# Removes last element of the deque.
|
68
|
+
#
|
69
|
+
# @raise Algorithmix::EmptyContainerError, if the deque is empty.
|
70
|
+
# @return last element of the deque
|
71
|
+
def pop_back
|
72
|
+
raise EmptyContainerError, "The deque is empty." if @container.empty?
|
73
|
+
@container.pop
|
74
|
+
end
|
75
|
+
|
76
|
+
# Returns first element of the deque.
|
77
|
+
def front
|
78
|
+
@container.first
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns last element of the deque.
|
82
|
+
def back
|
83
|
+
@container.last
|
84
|
+
end
|
85
|
+
|
86
|
+
# Checks if the deque is empty.
|
87
|
+
def empty?
|
88
|
+
@container.empty?
|
89
|
+
end
|
90
|
+
|
91
|
+
# Returns size of the deque.
|
92
|
+
def size
|
93
|
+
@container.size
|
94
|
+
end
|
95
|
+
|
96
|
+
# Returns the element at given index.
|
97
|
+
#
|
98
|
+
# @param idx
|
99
|
+
# @return element at given position, or nil if there is no element at that index.
|
100
|
+
def [](idx)
|
101
|
+
@container[idx]
|
102
|
+
end
|
103
|
+
|
104
|
+
# (see #[])
|
105
|
+
def at(idx)
|
106
|
+
@container[idx]
|
107
|
+
end
|
108
|
+
|
109
|
+
# Compares contents of the deque and a deque given as argument.
|
110
|
+
#
|
111
|
+
# @param deque [Deque]
|
112
|
+
# @return [true, false]
|
113
|
+
# @raise ArgumentError, if given object is not a deque
|
114
|
+
def ==(deque)
|
115
|
+
raise ArgumentError, "Undefined method Deque#== for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
116
|
+
@container == deque.to_a
|
117
|
+
end
|
118
|
+
|
119
|
+
# (see #==)
|
120
|
+
def eql?(deque)
|
121
|
+
raise ArgumentError, "Undefined method Deque#eql? for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
122
|
+
self == deque
|
123
|
+
end
|
124
|
+
|
125
|
+
# Compares contents of the deque and a deque given as argument.
|
126
|
+
#
|
127
|
+
# @param deque [Deque]
|
128
|
+
# @return [true, false]
|
129
|
+
# @raise ArgumentError, if given object is not a deque
|
130
|
+
def !=(deque)
|
131
|
+
raise ArgumentError, "Undefined method Deque#!= for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
132
|
+
@container != deque.to_a
|
133
|
+
end
|
134
|
+
|
135
|
+
# (see #!=)
|
136
|
+
def diff?(deque)
|
137
|
+
raise ArgumentError, "Undefined method Deque#diff? for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
138
|
+
self != deque
|
139
|
+
end
|
140
|
+
|
141
|
+
# Compares contents of the deque and a deque given as argument.
|
142
|
+
#
|
143
|
+
# @param deque [Deque]
|
144
|
+
# @return 1, if content of the deque is greater than content of the given deque,
|
145
|
+
# 0, if contents are equal
|
146
|
+
# -1, if content of the second deque is greater than content of self object
|
147
|
+
# @raise ArgumentError, if given object is not a deque
|
148
|
+
def <=>(deque)
|
149
|
+
raise ArgumentError, "Undefined method Deque#<=> for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
150
|
+
@container <=> deque.to_a
|
151
|
+
end
|
152
|
+
|
153
|
+
# Concatenates contents of the deque and a deque given as argument.
|
154
|
+
#
|
155
|
+
# @param deque [Deque]
|
156
|
+
# @return [Deque] a new deque
|
157
|
+
# @raise ArgumentError, if given object is not a deque
|
158
|
+
def +(deque)
|
159
|
+
raise ArgumentError, "Undefined method Deque#+ for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
160
|
+
Deque.new(@container + deque.to_a)
|
161
|
+
end
|
162
|
+
|
163
|
+
# (see #+)
|
164
|
+
def concat(deque)
|
165
|
+
raise ArgumentError, "Undefined method Deque#concat for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
166
|
+
self + deque
|
167
|
+
end
|
168
|
+
|
169
|
+
# (see #+)
|
170
|
+
def merge(deque)
|
171
|
+
raise ArgumentError, "Undefined method Deque#merge for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
172
|
+
self + deque
|
173
|
+
end
|
174
|
+
|
175
|
+
# Concatenates contents of the deque and a deque given as argument.
|
176
|
+
#
|
177
|
+
# @param deque [Deque]
|
178
|
+
# @return [Deque] self object
|
179
|
+
# @raise ArgumentError, if given object is not a deque
|
180
|
+
def concat!(deque)
|
181
|
+
raise ArgumentError, "Undefined method Deque#concat! for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
182
|
+
@container += deque.to_a
|
183
|
+
self
|
184
|
+
end
|
185
|
+
|
186
|
+
# Concatenates contents of the deque and a deque given as argument.
|
187
|
+
#
|
188
|
+
# @param deque [Deque]
|
189
|
+
# @return [Deque] self object
|
190
|
+
# @raise ArgumentError, if given object is not a deque
|
191
|
+
def merge!(deque)
|
192
|
+
raise ArgumentError, "Undefined method Deque#merge! for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
193
|
+
@container += deque.to_a
|
194
|
+
self
|
195
|
+
end
|
196
|
+
|
197
|
+
# Unites contents of the deque and a deque given as argument.
|
198
|
+
#
|
199
|
+
# @param deque [Deque]
|
200
|
+
# @return [Deque] a new deque
|
201
|
+
# @raise ArgumentError, if given object is not a deque
|
202
|
+
def |(deque)
|
203
|
+
raise ArgumentError, "Undefined method Deque#| for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
204
|
+
Deque.new(@container | deque.to_a)
|
205
|
+
end
|
206
|
+
|
207
|
+
# (see #|)
|
208
|
+
def union(deque)
|
209
|
+
raise ArgumentError, "Undefined method Deque#union for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
210
|
+
self | deque
|
211
|
+
end
|
212
|
+
|
213
|
+
# Unites contents of the deque and a deque given as argument.
|
214
|
+
#
|
215
|
+
# @param deque [Deque]
|
216
|
+
# @return [Deque] self object
|
217
|
+
# @raise ArgumentError, if given object is not a deque
|
218
|
+
def union!(deque)
|
219
|
+
raise ArgumentError, "Undefined method Deque#union! for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
220
|
+
@container |= deque.to_a
|
221
|
+
self
|
222
|
+
end
|
223
|
+
|
224
|
+
# Finds intersection of contents of the deque and a deque given as argument.
|
225
|
+
#
|
226
|
+
# @param deque [Deque]
|
227
|
+
# @return [Deque] a new deque
|
228
|
+
# @raise ArgumentError, if given object is not a deque
|
229
|
+
def &(deque)
|
230
|
+
raise ArgumentError, "Undefined method Deque#& for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
231
|
+
Deque.new(@container & deque.to_a)
|
232
|
+
end
|
233
|
+
|
234
|
+
# (see #&)
|
235
|
+
def intersect(deque)
|
236
|
+
raise ArgumentError, "Undefined method Deque#intersect for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
237
|
+
self & deque
|
238
|
+
end
|
239
|
+
|
240
|
+
# Finds intersection of contents of the deque and a deque given as argument.
|
241
|
+
#
|
242
|
+
# @param deque [Deque]
|
243
|
+
# @return [Deque] self object
|
244
|
+
# @raise ArgumentError, if given object is not a deque
|
245
|
+
def intersect!(deque)
|
246
|
+
raise ArgumentError, "Undefined method Deque#intersect! for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
247
|
+
@container &= deque.to_a
|
248
|
+
self
|
249
|
+
end
|
250
|
+
|
251
|
+
# Finds difference of contents of the deque and a deque given as argument.
|
252
|
+
#
|
253
|
+
# @param deque [Deque]
|
254
|
+
# @return [Deque] a new deque
|
255
|
+
# @raise ArgumentError, if given object is not a deque
|
256
|
+
def -(deque)
|
257
|
+
raise ArgumentError, "Undefined method Deque#- for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
258
|
+
Deque.new(@container - deque.to_a)
|
259
|
+
end
|
260
|
+
|
261
|
+
# (see #-)
|
262
|
+
def difference(deque)
|
263
|
+
raise ArgumentError, "Undefined method Deque#difference for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
264
|
+
self - deque
|
265
|
+
end
|
266
|
+
|
267
|
+
# Finds difference of contents of the deque and a deque given as argument.
|
268
|
+
#
|
269
|
+
# @param deque [Deque]
|
270
|
+
# @return [Deque] a new deque
|
271
|
+
# @raise ArgumentError, if given object is not a deque
|
272
|
+
def difference!(deque)
|
273
|
+
raise ArgumentError, "Undefined method Deque#difference! for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
274
|
+
@container -= deque.to_a
|
275
|
+
self
|
276
|
+
end
|
277
|
+
|
278
|
+
# Finds symmetric difference of contents of the deque and a deque given as argument.
|
279
|
+
#
|
280
|
+
# @param deque [Deque]
|
281
|
+
# @return [Deque] a new deque
|
282
|
+
# @raise ArgumentError, if given object is not a deque
|
283
|
+
def ^(deque)
|
284
|
+
raise ArgumentError, "Undefined method Deque#^ for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
285
|
+
Deque.new((@container | deque.to_a) - (@container & deque.to_a))
|
286
|
+
end
|
287
|
+
|
288
|
+
# (see #symmetric_difference)
|
289
|
+
def symmetric_difference(deque)
|
290
|
+
raise ArgumentError, "Undefined method Deque#symmetric_difference for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
291
|
+
self ^ deque
|
292
|
+
end
|
293
|
+
|
294
|
+
# Finds symmetric difference of contents of the deque and a deque given as argument.
|
295
|
+
#
|
296
|
+
# @param deque [Deque]
|
297
|
+
# @return [Deque] self object
|
298
|
+
# @raise ArgumentError, if given object is not a deque
|
299
|
+
def symmetric_difference!(deque)
|
300
|
+
raise ArgumentError, "Undefined method Deque#symmetric_difference! for #{deque}:#{deque.class}" unless deque.is_a?(Deque)
|
301
|
+
@container = (@container | deque.to_a) - (@container & deque.to_a)
|
302
|
+
self
|
303
|
+
end
|
304
|
+
|
305
|
+
# Converts content of the deque to an array witl elements.
|
306
|
+
def to_a
|
307
|
+
@container
|
308
|
+
end
|
309
|
+
|
310
|
+
# Clears content of the deque.
|
311
|
+
#
|
312
|
+
# @return self object
|
313
|
+
def clear
|
314
|
+
@container = []
|
315
|
+
self
|
316
|
+
end
|
317
|
+
|
318
|
+
# Filters elements of the deque by given condition.
|
319
|
+
#
|
320
|
+
# @param &block
|
321
|
+
# @return [Deque] a new deque
|
322
|
+
def select(&block)
|
323
|
+
Deque.new( @container.select { |e| block.call(e)})
|
324
|
+
end
|
325
|
+
|
326
|
+
# Filters elements of the deque by given condition.
|
327
|
+
#
|
328
|
+
# @param &block
|
329
|
+
# @return [Deque] self object
|
330
|
+
def select!(&block)
|
331
|
+
@container.select! { |e| block.call(e)}
|
332
|
+
self
|
333
|
+
end
|
334
|
+
|
335
|
+
# (see #select)
|
336
|
+
def filter(&block)
|
337
|
+
select(&block)
|
338
|
+
end
|
339
|
+
|
340
|
+
# (see #select!)
|
341
|
+
def filter!(&block)
|
342
|
+
select!(&block)
|
343
|
+
end
|
344
|
+
|
345
|
+
# (see #select)
|
346
|
+
def find_all(&block)
|
347
|
+
select(&block)
|
348
|
+
end
|
349
|
+
|
350
|
+
# (see #select!)
|
351
|
+
def find_all!(&block)
|
352
|
+
select!(&block)
|
353
|
+
end
|
354
|
+
|
355
|
+
# Applies a function to each element of the deque.
|
356
|
+
#
|
357
|
+
# @param &block
|
358
|
+
# @return [Deque] a new deque
|
359
|
+
def map(&block)
|
360
|
+
Deque.new(@container.map { |e| block.call(e)})
|
361
|
+
end
|
362
|
+
|
363
|
+
# Applies a function to each element of the deque.
|
364
|
+
#
|
365
|
+
# @param &block
|
366
|
+
# @return [Deque] self object
|
367
|
+
def map!(&block)
|
368
|
+
@container.map! { |e| block.call(e)}
|
369
|
+
self
|
370
|
+
end
|
371
|
+
|
372
|
+
# (see #map)
|
373
|
+
def apply(&block)
|
374
|
+
map(&block)
|
375
|
+
end
|
376
|
+
|
377
|
+
# (see #map!)
|
378
|
+
def apply!(&block)
|
379
|
+
map!(&block)
|
380
|
+
end
|
381
|
+
|
382
|
+
private
|
383
|
+
|
384
|
+
def from_obj(obj, copy)
|
385
|
+
raise ArgumentError, "Object doesn't respond to #to_a method" unless obj.respond_to?(:to_a)
|
386
|
+
|
387
|
+
@container = copy ? obj.send(:to_a).dup : obj.send(:to_a)
|
388
|
+
self
|
389
|
+
end
|
390
|
+
|
391
|
+
end
|
392
|
+
end
|
393
|
+
end
|
394
|
+
end
|
data/lib/algorithmix/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: algorithmix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.0.
|
4
|
+
version: 0.0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Monika Ilieva
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,10 +66,13 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 2.0.0
|
69
|
-
description:
|
70
|
-
|
71
|
-
|
72
|
-
|
69
|
+
description: |2+
|
70
|
+
Currently the gem contains:
|
71
|
+
1. Stack
|
72
|
+
2. Queue
|
73
|
+
3. Deque
|
74
|
+
4. Binary heap
|
75
|
+
|
73
76
|
email:
|
74
77
|
- "*@*.*"
|
75
78
|
executables: []
|
@@ -88,6 +91,7 @@ files:
|
|
88
91
|
- bin/console
|
89
92
|
- bin/setup
|
90
93
|
- lib/algorithmix.rb
|
94
|
+
- lib/algorithmix/data_structure/generic/deque.rb
|
91
95
|
- lib/algorithmix/data_structure/generic/queue.rb
|
92
96
|
- lib/algorithmix/data_structure/generic/stack.rb
|
93
97
|
- lib/algorithmix/error.rb
|
@@ -116,6 +120,6 @@ rubyforge_project:
|
|
116
120
|
rubygems_version: 2.6.14.1
|
117
121
|
signing_key:
|
118
122
|
specification_version: 4
|
119
|
-
summary: The gem contains various implementations of known and uknown data
|
123
|
+
summary: The gem contains various implementations of known and uknown data structures
|
120
124
|
and algorithms
|
121
125
|
test_files: []
|