algorithmix 0.0.0.1 → 0.0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +0 -1
- data/README.md +3 -1
- data/lib/algorithmix/data_structure/generic/queue.rb +316 -0
- data/lib/algorithmix/data_structure/generic/stack.rb +335 -0
- data/lib/algorithmix/error.rb +3 -0
- data/lib/algorithmix/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8e724de2b470e9bba5bb91835d8838b8a1c9539
|
4
|
+
data.tar.gz: 9f2cfd73e638455a1a5c77ef9d84e39fc8242f7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72ea4e1a6a1edded2b5aba0a94e93488971ea0663be32e4857be9c760343be171492756a3f90aee7bb6471c84fa65596a42180a454681fc9de778a90cca93569
|
7
|
+
data.tar.gz: 5a393b92e1b96b896f7f4283ca6d904913e690bd21d8554135e32ef7af9f5cc599f26c03e25f6882d9d24e26cc5fb3bf98fd2ff64f7d25c5d7108f17b860205b
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -9,7 +9,9 @@ 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
|
-
![Gem](https://img.shields.io/badge/min-0.0.0.
|
12
|
+
![Gem](https://img.shields.io/badge/min-0.0.0.2-green.svg) [**Queue**](https://github.com/monzita/algorithmix/wiki/Queue) implementation & option for duplication of elements in [**Stack**](https://github.com/monzita/algorithmix/wiki/Stack)
|
13
|
+
|
14
|
+
![Gem](https://img.shields.io/badge/min-0.0.0.1-green.svg) [**Stack**](https://github.com/monzita/algorithmix/wiki/Stack) implementation is released.
|
13
15
|
|
14
16
|
## Installation
|
15
17
|
|
@@ -0,0 +1,316 @@
|
|
1
|
+
# Documentation: https://github.com/monzita/algorithmix/wiki/Queue
|
2
|
+
|
3
|
+
module Algorithmix
|
4
|
+
module DataStructure
|
5
|
+
module Generic
|
6
|
+
|
7
|
+
class Queue
|
8
|
+
|
9
|
+
# Creates a new queue.
|
10
|
+
#
|
11
|
+
# @param obj [#to_a] Any objects which responds to #to_a method
|
12
|
+
# @kwarg copy [true, false] If is set to true, will duplicate the given object. By default is set to false.
|
13
|
+
# @return [Queue] a newly create queue
|
14
|
+
def initialize(obj = nil, copy: false)
|
15
|
+
@container = []
|
16
|
+
obj.nil? ? nil : from_obj(obj, copy)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Assigns content of an object, to stack's content, removing previous elements.
|
20
|
+
#
|
21
|
+
# @param obj [#to_a] Any object which responds to #to_a method.
|
22
|
+
# @kwarg copy [true, false] If is set to true, will duplicate the given object. By default is set to false.
|
23
|
+
# @return self object [Queue]
|
24
|
+
def assign(obj, copy: false)
|
25
|
+
from_obj(obj, copy)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Inserts a value at the end of the queue.
|
29
|
+
#
|
30
|
+
# @param value
|
31
|
+
# @return [Queue] self object
|
32
|
+
def push(value)
|
33
|
+
@container << value
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
# (see #push)
|
38
|
+
def <<(value)
|
39
|
+
push(value)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Removes the front element of the queue.
|
43
|
+
#
|
44
|
+
# @return front element
|
45
|
+
# @raise Algorithmix::EmptyContainerError if the queue is empty.
|
46
|
+
def pop
|
47
|
+
raise EmptyContainerError, "The queue is empty." if @container.empty?
|
48
|
+
@container.shift
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns the front element of the queue, or nil if it's empty.
|
52
|
+
def front
|
53
|
+
@container.first
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns number of elements in the queue.
|
57
|
+
def size
|
58
|
+
@container.size
|
59
|
+
end
|
60
|
+
|
61
|
+
# Checks if the queue is empty.
|
62
|
+
def empty?
|
63
|
+
@container.empty?
|
64
|
+
end
|
65
|
+
|
66
|
+
# Compares contents of the self object and that given as argument.
|
67
|
+
#
|
68
|
+
# @param queue [Queue]
|
69
|
+
# @return [true, false]
|
70
|
+
# @raise ArgumentError, if given object is not a queue.
|
71
|
+
def ==(queue)
|
72
|
+
raise ArgumentError, "Undefined method Queue#== for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
73
|
+
@container == queue.to_a
|
74
|
+
end
|
75
|
+
|
76
|
+
# (see #==)
|
77
|
+
def eql?(queue)
|
78
|
+
raise ArgumentError, "Undefined method Queue#eql? for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
79
|
+
self == queue
|
80
|
+
end
|
81
|
+
|
82
|
+
# Compares contents of the self object and that given as argument.
|
83
|
+
#
|
84
|
+
# @param queue [Queue]
|
85
|
+
# @return [true, false]
|
86
|
+
# @raise ArgumentError, if given object is not a queue.
|
87
|
+
def !=(queue)
|
88
|
+
raise ArgumentError, "Undefined method Queue#!= for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
89
|
+
@container != queue.to_a
|
90
|
+
end
|
91
|
+
|
92
|
+
# (see #!=)
|
93
|
+
def diff?(queue)
|
94
|
+
raise ArgumentError, "Undefined method Queue#diff? for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
95
|
+
self != queue
|
96
|
+
end
|
97
|
+
|
98
|
+
# Compares contents of the self object and that given as argument.
|
99
|
+
#
|
100
|
+
# @param queue [Queue]
|
101
|
+
# @return 1, if content of the first queue is greater than content of the second
|
102
|
+
# 0, if contents of both queues are equal
|
103
|
+
# -1, if content of the second queue is greater than content of the first
|
104
|
+
# @raise ArgumentError, if given object is not a queue.
|
105
|
+
def <=>(queue)
|
106
|
+
raise ArgumentError, "Undefined method Queue#<=> for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
107
|
+
@container <=> queue.to_a
|
108
|
+
end
|
109
|
+
|
110
|
+
# Concatenates contents of two queues.
|
111
|
+
#
|
112
|
+
# @param queue [Queue]
|
113
|
+
# @return [Queue] a new queue object
|
114
|
+
def +(queue)
|
115
|
+
raise ArgumentError, "Undefined method Queue#+ for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
116
|
+
Queue.new(@container + queue.to_a)
|
117
|
+
end
|
118
|
+
|
119
|
+
# (see #+)
|
120
|
+
def concat(queue)
|
121
|
+
raise ArgumentError, "Undefined method Queue#concat for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
122
|
+
self + queue
|
123
|
+
end
|
124
|
+
|
125
|
+
# (see #+)
|
126
|
+
def merge(queue)
|
127
|
+
raise ArgumentError, "Undefined method Queue#merge for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
128
|
+
self + queue
|
129
|
+
end
|
130
|
+
|
131
|
+
# Same as #+, but modifies the current object
|
132
|
+
def concat!(queue)
|
133
|
+
raise ArgumentError, "Undefined method Queue#concat! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
134
|
+
@container += queue.to_a
|
135
|
+
self
|
136
|
+
end
|
137
|
+
|
138
|
+
# Same as #+, but modifies the current object
|
139
|
+
def merge!(queue)
|
140
|
+
raise ArgumentError, "Undefined method Queue#merge! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
141
|
+
@container += queue.to_a
|
142
|
+
self
|
143
|
+
end
|
144
|
+
|
145
|
+
# Unites contents of the self queue and queue given as argument.
|
146
|
+
#
|
147
|
+
# @param queue [Queue]
|
148
|
+
# @return [Queue] a new queue
|
149
|
+
# @raise ArgumentError, if given object is not a queue
|
150
|
+
def |(queue)
|
151
|
+
raise ArgumentError, "Undefined method Queue#| for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
152
|
+
Queue.new(@container | queue.to_a)
|
153
|
+
end
|
154
|
+
|
155
|
+
# (see #|)
|
156
|
+
def union(queue)
|
157
|
+
raise ArgumentError, "Undefined method Queue#union for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
158
|
+
self | queue
|
159
|
+
end
|
160
|
+
|
161
|
+
# Same as #union, but modifies the current object
|
162
|
+
def union!(queue)
|
163
|
+
raise ArgumentError, "Undefined method Queue#union! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
164
|
+
@container |= queue.to_a
|
165
|
+
self
|
166
|
+
end
|
167
|
+
|
168
|
+
# Finds the intersection of the self object and queue given as argument.
|
169
|
+
#
|
170
|
+
# @param queue [Queue]
|
171
|
+
# @return [Queue] a new queue
|
172
|
+
# @raise ArgumentError, if given object is not a queue
|
173
|
+
def &(queue)
|
174
|
+
raise ArgumentError, "Undefined method Queue#& for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
175
|
+
Queue.new(@container & queue.to_a)
|
176
|
+
end
|
177
|
+
|
178
|
+
# (see #&)
|
179
|
+
def intersect(queue)
|
180
|
+
raise ArgumentError, "Undefined method Queue#intersect for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
181
|
+
self & queue
|
182
|
+
end
|
183
|
+
|
184
|
+
# Same as #&, but modifies the current object.
|
185
|
+
def intersect!(queue)
|
186
|
+
raise ArgumentError, "Undefined method Queue#intersect! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
187
|
+
@container &= queue.to_a
|
188
|
+
self
|
189
|
+
end
|
190
|
+
|
191
|
+
# Finds the difference of the current queue and queue given as argument.
|
192
|
+
#
|
193
|
+
# @param queue [Queue]
|
194
|
+
# @return [Queue] a new queue
|
195
|
+
# @raise ArgumentError, if given object is not a queue
|
196
|
+
def -(queue)
|
197
|
+
raise ArgumentError, "Undefined method Queue#- for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
198
|
+
Queue.new(@container - queue.to_a)
|
199
|
+
end
|
200
|
+
|
201
|
+
# (see #-)
|
202
|
+
def difference(queue)
|
203
|
+
raise ArgumentError, "Undefined method Queue#difference for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
204
|
+
self - queue
|
205
|
+
end
|
206
|
+
|
207
|
+
# Same as #-, but modifies the current queue.
|
208
|
+
def difference!(queue)
|
209
|
+
raise ArgumentError, "Undefined method Queue#difference! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
210
|
+
@container -= queue.to_a
|
211
|
+
self
|
212
|
+
end
|
213
|
+
|
214
|
+
# Finds the symmetric difference of the self object and queue given as argument.
|
215
|
+
#
|
216
|
+
# @param queue [Queue]
|
217
|
+
# @return [Queue] a new queue
|
218
|
+
# @raise ArgumentError, if given object is not a queue
|
219
|
+
def ^(queue)
|
220
|
+
raise ArgumentError, "Undefined method Queue#^ for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
221
|
+
Queue.new((@container | queue.to_a) - (@container & queue.to_a))
|
222
|
+
end
|
223
|
+
|
224
|
+
# (see #^)
|
225
|
+
def symmetric_difference(queue)
|
226
|
+
raise ArgumentError, "Undefined method Queue#symmetric_difference for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
227
|
+
self ^ queue
|
228
|
+
end
|
229
|
+
|
230
|
+
# Same as #^, but modifies the current object.
|
231
|
+
def symmetric_difference!(queue)
|
232
|
+
raise ArgumentError, "Undefined method Queue#symmetric_difference! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
|
233
|
+
@container = (@container | queue.to_a) - (@container & queue.to_a)
|
234
|
+
end
|
235
|
+
|
236
|
+
# Converts the queue to an array with elements.
|
237
|
+
def to_a
|
238
|
+
@container
|
239
|
+
end
|
240
|
+
|
241
|
+
# Clears content of the queue.
|
242
|
+
def clear
|
243
|
+
@container = []
|
244
|
+
self
|
245
|
+
end
|
246
|
+
|
247
|
+
# Filters elements of the queue by given condition.
|
248
|
+
#
|
249
|
+
# @param &block
|
250
|
+
# @return [Queue] a new queue
|
251
|
+
def select(&block)
|
252
|
+
Queue.new(@container.select { |e| block.call(e) })
|
253
|
+
end
|
254
|
+
|
255
|
+
# Same as #select, but modofies the current object.
|
256
|
+
def select!(&block)
|
257
|
+
@container.select! { |e| block.call(e) }
|
258
|
+
self
|
259
|
+
end
|
260
|
+
|
261
|
+
# (see #select)
|
262
|
+
def filter(&block)
|
263
|
+
select(&block)
|
264
|
+
end
|
265
|
+
|
266
|
+
# (see #select!)
|
267
|
+
def filter!(&block)
|
268
|
+
select!(&block)
|
269
|
+
end
|
270
|
+
|
271
|
+
# (see #select)
|
272
|
+
def find_all(&block)
|
273
|
+
select(&block)
|
274
|
+
end
|
275
|
+
|
276
|
+
# (see #select!)
|
277
|
+
def find_all!(&block)
|
278
|
+
select!(&block)
|
279
|
+
end
|
280
|
+
|
281
|
+
# Applies a function to each element of the queue.
|
282
|
+
#
|
283
|
+
# @param &block
|
284
|
+
# @return [Queue] a new queue
|
285
|
+
def map(&block)
|
286
|
+
Queue.new(@container.map { |e| block.call(e)})
|
287
|
+
end
|
288
|
+
|
289
|
+
# Same as #map, but modifies the current object.
|
290
|
+
def map!(&block)
|
291
|
+
@container.map! { |e| block.call(e) }
|
292
|
+
self
|
293
|
+
end
|
294
|
+
|
295
|
+
# (see #map)
|
296
|
+
def apply(&block)
|
297
|
+
map(&block)
|
298
|
+
end
|
299
|
+
|
300
|
+
# (see #map!)
|
301
|
+
def apply!(&block)
|
302
|
+
map!(&block)
|
303
|
+
end
|
304
|
+
|
305
|
+
private
|
306
|
+
|
307
|
+
def from_obj(obj, copy)
|
308
|
+
raise ArgumentError, "Object doesn't respond to #to_a method" unless obj.respond_to?(:to_a)
|
309
|
+
|
310
|
+
@container = copy ? obj.send(:to_a).dup : obj.send(:to_a)
|
311
|
+
self
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
@@ -0,0 +1,335 @@
|
|
1
|
+
# Documentation: https://github.com/monzita/algorithmix/wiki/Stack
|
2
|
+
|
3
|
+
module Algorithmix
|
4
|
+
module DataStructure
|
5
|
+
module Generic
|
6
|
+
|
7
|
+
class Stack
|
8
|
+
|
9
|
+
# Creates a new stack.
|
10
|
+
#
|
11
|
+
# @param obj [#to_a] any object which responds to #to_a method.
|
12
|
+
# @return [Stack] a new stack object
|
13
|
+
def initialize(obj = nil, copy: false)
|
14
|
+
|
15
|
+
@container = []
|
16
|
+
obj.nil? ? nil : from_obj(obj, copy)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Copies the content of an object to the content of the stack, removing
|
20
|
+
# previous elements inserted in it.
|
21
|
+
#
|
22
|
+
# @param obj [#to_a] any object which responds to to_a method
|
23
|
+
# @return [Stack] self object, modified
|
24
|
+
def assign(obj, copy: false)
|
25
|
+
from_obj(obj, copy)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Inserts a new element at the top of the stack.
|
29
|
+
#
|
30
|
+
# @param value
|
31
|
+
# @return [Stack] self object, modified
|
32
|
+
def push(value)
|
33
|
+
@container << value
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
# (see #push)
|
38
|
+
def <<(value)
|
39
|
+
push(value)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Removes the top element of the stack.
|
43
|
+
#
|
44
|
+
# @return top element of the stack
|
45
|
+
# @raise Algorithmix::EmptyContainerError, if there are no elements in the stack.
|
46
|
+
def pop
|
47
|
+
raise EmptyContainerError, "The stack is empty." if @container.empty?
|
48
|
+
@container.pop
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns the number of elements in the stack.
|
52
|
+
def size
|
53
|
+
@container.size
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the top element of the stack, without removing it.
|
57
|
+
# If the stack is empty, returns nil.
|
58
|
+
def top
|
59
|
+
@container.last
|
60
|
+
end
|
61
|
+
|
62
|
+
# Checks if the stack contains any elements.
|
63
|
+
def empty?
|
64
|
+
@container.empty?
|
65
|
+
end
|
66
|
+
|
67
|
+
# Compares the self stack with given as argument stack.
|
68
|
+
#
|
69
|
+
# @param [Stack]
|
70
|
+
# @return [true, false]
|
71
|
+
def ==(stack)
|
72
|
+
raise ArgumentError, "Undefined method Stack#== for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
73
|
+
@container == stack.to_a
|
74
|
+
end
|
75
|
+
|
76
|
+
# (see #==)
|
77
|
+
def eql?(stack)
|
78
|
+
raise ArgumentError, "Undefined method Stack#eql? for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
79
|
+
self == stack
|
80
|
+
end
|
81
|
+
|
82
|
+
# (see #==)
|
83
|
+
def !=(stack)
|
84
|
+
raise ArgumentError, "Undefined method Stack#!= for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
85
|
+
@container != stack.to_a
|
86
|
+
end
|
87
|
+
|
88
|
+
# (see #==)
|
89
|
+
def diff?(stack)
|
90
|
+
raise ArgumentError, "Undefined method Stack#diff? for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
91
|
+
self != stack
|
92
|
+
end
|
93
|
+
|
94
|
+
# Compares the self stack with given as argument stck.
|
95
|
+
#
|
96
|
+
# @return 1, if content of the first stack is greater than content of the second.
|
97
|
+
# 0, if both stacks have equal contents
|
98
|
+
# -1, if content of the first stack is less than content of the second.
|
99
|
+
# @raise ArgumentError, if given object is not a stack
|
100
|
+
def <=>(stack)
|
101
|
+
raise ArgumentError, "Undefined method Stack#<=> for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
102
|
+
@container <=> stack.to_a
|
103
|
+
end
|
104
|
+
|
105
|
+
# Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.
|
106
|
+
#
|
107
|
+
# @param [Stack]
|
108
|
+
# @return [Stack] a new stack object, without modifying any of given stacks.
|
109
|
+
# @raise ArgumentError, if given object is not a stack
|
110
|
+
def +(stack)
|
111
|
+
raise ArgumentError, "Undefined method Stack#+ for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
112
|
+
Stack.new(@container + stack.to_a)
|
113
|
+
end
|
114
|
+
|
115
|
+
# (see #+)
|
116
|
+
def merge(stack)
|
117
|
+
raise ArgumentError, "Undefined method Stack#merge for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
118
|
+
self + stack
|
119
|
+
end
|
120
|
+
|
121
|
+
# Merges contents of the self stack, and stack given as argument.
|
122
|
+
# Modifies self object.
|
123
|
+
#
|
124
|
+
# @param [Stack]
|
125
|
+
# @return [Stack] self object
|
126
|
+
# @raise ArgumentError, if given object is not a stack
|
127
|
+
def merge!(stack)
|
128
|
+
raise ArgumentError, "Undefined method Stack#merge! for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
129
|
+
@container += stack.to_a
|
130
|
+
self
|
131
|
+
end
|
132
|
+
|
133
|
+
# (see #+)
|
134
|
+
def concat(stack)
|
135
|
+
raise ArgumentError, "Undefined method Stack#concat for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
136
|
+
self + stack
|
137
|
+
end
|
138
|
+
|
139
|
+
# (see #merge!)
|
140
|
+
def concat!(stack)
|
141
|
+
raise ArgumentError, "Undefined method Stack#concat! for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
142
|
+
@container += stack.to_a
|
143
|
+
self
|
144
|
+
end
|
145
|
+
|
146
|
+
# Finds intersection of contents of the self stack and stack given as argument, and returns a new stack
|
147
|
+
# with the result.
|
148
|
+
#
|
149
|
+
# @param [Stack]
|
150
|
+
# @return [Stack] a new stack
|
151
|
+
# @raise ArgumentError, if given object is not a stack
|
152
|
+
def &(stack)
|
153
|
+
raise ArgumentError, "Undefined method Stack#& for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
154
|
+
Stack.new(@container & stack.to_a)
|
155
|
+
end
|
156
|
+
|
157
|
+
# (see #&)
|
158
|
+
def intersect(stack)
|
159
|
+
raise ArgumentError, "Undefined method Stack#intersect for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
160
|
+
self & stack
|
161
|
+
end
|
162
|
+
|
163
|
+
# Like intersect, finds intersection of the self stack object and stack given as argument, but modifies the
|
164
|
+
# object to which operation was called.
|
165
|
+
def intersect!(stack)
|
166
|
+
raise ArgumentError, "Undefined method Stack#intersect! for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
167
|
+
@container &= stack.to_a
|
168
|
+
self
|
169
|
+
end
|
170
|
+
|
171
|
+
# Finds difference of the self stack object and that given as argument.
|
172
|
+
#
|
173
|
+
# @param [Stack]
|
174
|
+
# @return [Stack] a new stack
|
175
|
+
# @raise ArgumentError, if given object is not a stack
|
176
|
+
def -(stack)
|
177
|
+
raise ArgumentError, "Undefined method Stack#- for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
178
|
+
Stack.new(@container - stack.to_a)
|
179
|
+
end
|
180
|
+
|
181
|
+
# (see #-)
|
182
|
+
def difference(stack)
|
183
|
+
raise ArgumentError, "Undefined method Stack#difference for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
184
|
+
self - stack
|
185
|
+
end
|
186
|
+
|
187
|
+
# As difference, finds the difference of the stack and that given as argument, but
|
188
|
+
# modifies self object.
|
189
|
+
#
|
190
|
+
# @param [Stack]
|
191
|
+
# @return [Stack] self object
|
192
|
+
# @raise ArgumentError, if given object is not a stack
|
193
|
+
def difference!(stack)
|
194
|
+
raise ArgumentError, "Undefined method Stack#difference! for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
195
|
+
@container -= stack.to_a
|
196
|
+
self
|
197
|
+
end
|
198
|
+
|
199
|
+
# Finds union of the self object and stack given as argument.
|
200
|
+
#
|
201
|
+
# @param [Stack]
|
202
|
+
# @return [Stack] a new stack
|
203
|
+
# @raise ArgumentError, if given object is not a stack
|
204
|
+
def |(stack)
|
205
|
+
raise ArgumentError, "Undefined method Stack#| for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
206
|
+
Stack.new(@container | stack.to_a)
|
207
|
+
end
|
208
|
+
|
209
|
+
# (see #|)
|
210
|
+
def union(stack)
|
211
|
+
raise ArgumentError, "Undefined method Stack#union for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
212
|
+
self | stack
|
213
|
+
end
|
214
|
+
|
215
|
+
# Like #|, finds the union of the self object and the stack given as argument, but
|
216
|
+
# keeps the result in the current stack.
|
217
|
+
#
|
218
|
+
# @param [Stack]
|
219
|
+
# @return [Stack] self object
|
220
|
+
# @raise ArgumentError, if given object is not a stack
|
221
|
+
def union!(stack)
|
222
|
+
raise ArgumentError, "Undefined method Stack#union! for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
223
|
+
@container |= stack.to_a
|
224
|
+
self
|
225
|
+
end
|
226
|
+
|
227
|
+
# Finds the symmetric difference of the stack and that given as argument.
|
228
|
+
#
|
229
|
+
# @param [Stack]
|
230
|
+
# @return [Stack] a new stack
|
231
|
+
# @raise ArgumentError, if given object is not a stack
|
232
|
+
def ^(stack)
|
233
|
+
raise ArgumentError, "Undefined method Stack#^ for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
234
|
+
Stack.new((@container | stack.to_a) - (@container & stack.to_a))
|
235
|
+
end
|
236
|
+
|
237
|
+
# (see #^)
|
238
|
+
def symmetric_difference(stack)
|
239
|
+
raise ArgumentError, "Undefined method Stack#symmetric_difference for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
240
|
+
self ^ stack
|
241
|
+
end
|
242
|
+
|
243
|
+
# Finds the symmetric difference of the self object and stack given as argument.
|
244
|
+
#
|
245
|
+
# @param [Stack]
|
246
|
+
# @return [Stack] self object, modified
|
247
|
+
# @raise ArgumentError, if given object is not a stack
|
248
|
+
def symmetric_difference!(stack)
|
249
|
+
raise ArgumentError, "Undefined method Stack#symmetric_difference! for #{stack}:#{stack.class}" unless stack.is_a?(Stack)
|
250
|
+
@container = (@container | stack.to_a) - (@container & stack.to_a)
|
251
|
+
self
|
252
|
+
end
|
253
|
+
|
254
|
+
# Returns current object as array.
|
255
|
+
def to_a
|
256
|
+
@container
|
257
|
+
end
|
258
|
+
|
259
|
+
# Clears content of current stack.
|
260
|
+
def clear
|
261
|
+
@container = []
|
262
|
+
self
|
263
|
+
end
|
264
|
+
|
265
|
+
# Filters elements of the stack, by given condition.
|
266
|
+
#
|
267
|
+
# @param &block represents condition by which elements of the stack will be filtered
|
268
|
+
# @return [Stack] a new stack object
|
269
|
+
def select(&block)
|
270
|
+
Stack.new(@container.select { |e| block.call(e) })
|
271
|
+
end
|
272
|
+
|
273
|
+
# Same as #select, but modifying the self object.
|
274
|
+
def select!(&block)
|
275
|
+
@container.select! { |e| block.call(e)}
|
276
|
+
self
|
277
|
+
end
|
278
|
+
|
279
|
+
# (see #select)
|
280
|
+
def filter(&block)
|
281
|
+
select(&block)
|
282
|
+
end
|
283
|
+
|
284
|
+
# (see #select!)
|
285
|
+
def filter!(&block)
|
286
|
+
select!(&block)
|
287
|
+
end
|
288
|
+
|
289
|
+
# (see #select)
|
290
|
+
def find_all(&block)
|
291
|
+
select(&block)
|
292
|
+
end
|
293
|
+
|
294
|
+
# (see #select!)
|
295
|
+
def find_all!(&block)
|
296
|
+
select!(&block)
|
297
|
+
end
|
298
|
+
|
299
|
+
# Applies a function to each element of the stack.
|
300
|
+
#
|
301
|
+
# @param &block
|
302
|
+
# @return [Stack] a new stack
|
303
|
+
def map(&block)
|
304
|
+
Stack.new(@container.map { |e| block.call(e)})
|
305
|
+
end
|
306
|
+
|
307
|
+
# Same as #map, but the result is kept in the current object.
|
308
|
+
#
|
309
|
+
# @param &block
|
310
|
+
def map!(&block)
|
311
|
+
@container.map! { |e| block.call(e) }
|
312
|
+
self
|
313
|
+
end
|
314
|
+
|
315
|
+
# (see #map)
|
316
|
+
def apply(&block)
|
317
|
+
map(&block)
|
318
|
+
end
|
319
|
+
|
320
|
+
# (see #map!)
|
321
|
+
def apply!(&block)
|
322
|
+
map!(&block)
|
323
|
+
end
|
324
|
+
|
325
|
+
private
|
326
|
+
|
327
|
+
def from_obj(obj, copy)
|
328
|
+
raise ArgumentError, "Object doesn't respond to #to_a method" unless obj.respond_to?(:to_a)
|
329
|
+
@container = copy ? obj.send(:to_a).dup : obj.send(:to_a)
|
330
|
+
self
|
331
|
+
end
|
332
|
+
end
|
333
|
+
end
|
334
|
+
end
|
335
|
+
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.2
|
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-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,6 +86,9 @@ files:
|
|
86
86
|
- bin/console
|
87
87
|
- bin/setup
|
88
88
|
- lib/algorithmix.rb
|
89
|
+
- lib/algorithmix/data_structure/generic/queue.rb
|
90
|
+
- lib/algorithmix/data_structure/generic/stack.rb
|
91
|
+
- lib/algorithmix/error.rb
|
89
92
|
- lib/algorithmix/version.rb
|
90
93
|
homepage: https://github.com/monzita/algorithmix/wiki
|
91
94
|
licenses:
|