algorithmix 0.0.0.4.1 → 0.0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d842f0aaccbd42b0b889d9eb1f3e8c7dac5d84a
4
- data.tar.gz: 7908a79a6005b07d6aa97f50ece276b0d4a33650
3
+ metadata.gz: 975f3659a8220362ddb0e074ad0f9c826615e31a
4
+ data.tar.gz: 450f70a7bb070aae708b3112122c2fb0cc823957
5
5
  SHA512:
6
- metadata.gz: d3ce32ef804f089f1db22aed4749e953fa643156dd8d482af0dffd9670bf74aa3fa066a0b36862d12ea69c0849e0d30e3501051ff6e25c94cb9f74f0d6cefec8
7
- data.tar.gz: 66983d3763e7d7b29c0562b7a2ad6df3289c0664b769d13e21013443dfc26ea3ab8589f091d2e77733bfffa5e048886aa529d6d5ad01cfc98bb28c7e41b45b80
6
+ metadata.gz: 5b63a0d62dee71d7b3da52450a8fccb6b945fcdb99117d72f727099aa0e86114ae945ec9b7107c1bd2ad772a3e9cb78f559edf3327cdfa64c1be857da69c9951
7
+ data.tar.gz: 3302c670ade851248a144a0c39504c00309fd7325bb12debe71f0ca2e4182d167484b8f4ea20559b5d7ab02a5cfbc5f370bdebd5d10d3b2d8de6c6684a0fdfb0
data/README.md CHANGED
@@ -9,6 +9,8 @@ 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.4.1-green.svg) Fix couple of bugs in BinaryHeap
13
+
12
14
  ![Gem](https://img.shields.io/badge/min-0.0.0.4-green.svg) [**BinaryHeap**](https://github.com/monzita/algorithmix/wiki/BinaryHeap)
13
15
 
14
16
  ![Gem](https://img.shields.io/badge/min-0.0.0.3-green.svg) [**Deque**](https://github.com/monzita/algorithmix/wiki/Deque)
@@ -10,12 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["*@*.*"]
11
11
  spec.summary = %q{The gem contains various implementations of known and uknown data structures and algorithms}
12
12
  spec.description = <<-DOC
13
- Currently the gem contains:\n
14
- 1. Stack\n
15
- 2. Queue\n
16
- 3. Deque\n
17
- 4. Binary heap\n
18
-
13
+ The gem contains various implementations of known and uknown data structures and algorithms.
14
+ /Currently the gem contains: #stack, #queue, #deque, #binary_heap, #priority_queue/
19
15
  DOC
20
16
  spec.homepage = "https://github.com/monzita/algorithmix/wiki"
21
17
  spec.license = "MIT"
@@ -93,6 +93,11 @@ module Algorithmix
93
93
  @container.size
94
94
  end
95
95
 
96
+ # (see #size)
97
+ def length
98
+ @container.size
99
+ end
100
+
96
101
  # Returns the element at given index.
97
102
  #
98
103
  # @param idx
@@ -0,0 +1,354 @@
1
+ # Documentation: https://github.com/monzita/algorithmix/wiki/PriorityQueue
2
+
3
+ module Algorithmix
4
+ module DataStructure
5
+ module Generic
6
+
7
+ class PriorityQueue
8
+ attr_reader :binary_heap
9
+
10
+ # Creates a new priority queue.
11
+ #
12
+ # @param obj [#to_a] can be any object, which responds to #to_a method
13
+ # @kwarg copy [true, false] if set to true, makes a copy of content of the given object
14
+ # @kwarg min [true, false] sets the main property of the new priority queue. By default max priority queue is created.
15
+ # @raise ArgumentError, if given object doesn't respond to to_a method.
16
+ # @return [PriorityQueue]
17
+ def initialize(obj = nil, copy: false, min: false)
18
+ @binary_heap = Heap::BinaryHeap.new(obj, copy: copy, min: min)
19
+ @max = min ? false : true
20
+ end
21
+
22
+ # Assigns content of an object, to content of the queue.
23
+ #
24
+ # @param obj [#to_a]
25
+ # @kwarg copy [true, false]
26
+ # @raise ArgumentError
27
+ # @return self object [PriorityQeue]
28
+ def assign(obj, copy: false)
29
+ @binary_heap.assign(obj, copy: copy)
30
+ self
31
+ end
32
+
33
+ # Inserts a value in the queue.
34
+ #
35
+ # @param value
36
+ # @return [PriorityQueue] self object
37
+ def push(value)
38
+ @binary_heap.insert(value)
39
+ self
40
+ end
41
+
42
+ # (see #push)
43
+ def <<
44
+ push(value)
45
+ end
46
+
47
+ # Removes the element with highest or lowest priority of the queue.
48
+ #
49
+ # @raise Algorithmix::EmptyContainerError, if the queue is empty.
50
+ # @return element with the highest or lowest priority
51
+ def pop
52
+ raise EmptyContainerError, "The priority queue is empty." if @binary_heap.empty?
53
+ @binary_heap.extract
54
+ end
55
+
56
+ # Returns the element with the highest or lowest priority without removing it from the queue.
57
+ def front
58
+ @binary_heap.top
59
+ end
60
+
61
+ # Returns number of elements in the queue.
62
+ def size
63
+ @binary_heap.size
64
+ end
65
+
66
+ # (see #size)
67
+ def length
68
+ @binary_heap.size
69
+ end
70
+
71
+ # Returns information for the content of the queue - are there any elements, or no.
72
+ def empty?
73
+ @binary_heap.empty?
74
+ end
75
+
76
+ # Comapres contents of the queue and a queue given as argument.
77
+ #
78
+ # @param priority_queue [PriorityQueue]
79
+ # @raise ArgumentError, if given object is not a PriorityQueue.
80
+ # @return [true, false] true if contents are equal, false otherwise
81
+ def ==(priority_queue)
82
+ raise ArgumentError, "Undefined method PriorityQueue#== for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
83
+ to_a == priority_queue.to_a
84
+ end
85
+
86
+ # (see #==)
87
+ def eql?(priority_queue)
88
+ raise ArgumentError, "Undefined method PriorityQueue#eql? for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
89
+ self == priority_queue
90
+ end
91
+
92
+ # Comapres contents of the queue and a queue given as argument.
93
+ #
94
+ # @param priority_queue [PriorityQueue]
95
+ # @raise ArgumentError, if given object is not a PriorityQueue.
96
+ # @return [true, false] true if contents are different, false otherwise
97
+ def !=(priority_queue)
98
+ raise ArgumentError, "Undefined method PriorityQueue#!= for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
99
+ to_a != priority_queue.to_a
100
+ end
101
+
102
+ # (see #!=)
103
+ def diff?(priority_queue)
104
+ raise ArgumentError, "Undefined method PriorityQueue#diff? for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
105
+ self != priority_queue
106
+ end
107
+
108
+ # Comapres contents of the queue and a queue given as argument.
109
+ #
110
+ # @param priority_queue [PriorityQueue]
111
+ # @raise ArgumentError, if given object is not a PriorityQueue.
112
+ # @return 1 if content of the queue is > content of the given queue, 0 if contents are euqal, -1 if content of the given queue is > content of the queue
113
+ def <=>(priority_queue)
114
+ raise ArgumentError, "Undefined method PriorityQueue#<=> for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
115
+ to_a <=> priority_queue.to_a
116
+ end
117
+
118
+ # Converts content of the queue to an array.
119
+ #
120
+ # @return array with elements of the queue
121
+ def to_a
122
+ @binary_heap.to_a
123
+ end
124
+
125
+ # Clears content of the queue.
126
+ #
127
+ # @return [PriorityQueue] self object
128
+ def clear
129
+ @binary_heap.clear
130
+ self
131
+ end
132
+
133
+ # Concatenates contents of the queue and a queue given as argument.
134
+ #
135
+ # @param priority_queue [PriorityQueue]
136
+ # @raise ArgumentError, if given object is not a priority queue
137
+ # @return [PriorityQueue] a new priority queue
138
+ def +(priority_queue)
139
+ raise ArgumentError, "Undefined method PriorityQueue#+ for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
140
+ PriorityQueue.new(@binary_heap + priority_queue.send(:binary_heap), min: !@max)
141
+ end
142
+
143
+ # (see #+)
144
+ def concat(priority_queue)
145
+ raise ArgumentError, "Undefined method PriorityQueue#concat for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
146
+ self + priority_queue
147
+ end
148
+
149
+ # (see #+)
150
+ def merge(priority_queue)
151
+ raise ArgumentError, "Undefined method PriorityQueue#merge for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
152
+ self + priority_queue
153
+ end
154
+
155
+ # Concatenates contents of the queue and a queue given as argument.
156
+ #
157
+ # @param priority_queue [PriorityQueue]
158
+ # @raise ArgumentError, if given object is not a priority queue
159
+ # @return [PriorityQueue] self object
160
+ def concat!(priority_queue)
161
+ raise ArgumentError, "Undefined method PriorityQueue#concat! for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
162
+ @binary_heap.concat!(priority_queue.send(:binary_heap))
163
+ self
164
+ end
165
+
166
+ # Concatenates contents of the queue and a queue given as argument.
167
+ #
168
+ # @param priority_queue [PriorityQueue]
169
+ # @raise ArgumentError, if given object is not a priority queue
170
+ # @return [PriorityQueue] self object
171
+ def merge!(priority_queue)
172
+ raise ArgumentError, "Undefined method PriorityQueue#merge! for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
173
+ @binary_heap.merge!(priority_queue.send(:binary_heap))
174
+ self
175
+ end
176
+
177
+ # Unites contents of the queue and a queue given as argument.
178
+ #
179
+ # @param priority_queue [PriorityQueue]
180
+ # @raise ArgumentError, if given object is not a priority queue
181
+ # @return [PriorityQueue] a new priority queue
182
+ def |(priority_queue)
183
+ raise ArgumentError, "Undefined method PriorityQueue#| for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
184
+ PriorityQueue.new(@binary_heap | priority_queue.send(:binary_heap), min: !@max)
185
+ end
186
+
187
+ # (see #|)
188
+ def union(priority_queue)
189
+ raise ArgumentError, "Undefined method PriorityQueue#union for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
190
+ self | priority_queue
191
+ end
192
+
193
+ # Unites contents of the queue and a queue given as argument.
194
+ #
195
+ # @param priority_queue [PriorityQueue]
196
+ # @raise ArgumentError, if given object is not a priority queue
197
+ # @return [PriorityQueue] self object
198
+ def union!(priority_queue)
199
+ raise ArgumentError, "Undefined method PriorityQueue#union! for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
200
+ @binary_heap = @binary_heap | priority_queue.send(:binary_heap)
201
+ self
202
+ end
203
+
204
+ # Finds the intersection of contents of the queue and a queue given as argument.
205
+ #
206
+ # @param priority_queue [PriorityQueue]
207
+ # @raise ArgumentError, if given object is not a priority queue
208
+ # @return [PriorityQueue] a new priority queue
209
+ def &(priority_queue)
210
+ raise ArgumentError, "Undefined method PriorityQueue#& for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
211
+ PriorityQueue.new(@binary_heap & priority_queue.send(:binary_heap), min: !@max)
212
+ end
213
+
214
+ # (see #&)
215
+ def intersect(priority_queue)
216
+ raise ArgumentError, "Undefined method PriorityQueue#intersect for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
217
+ self & priority_queue
218
+ end
219
+
220
+ # Finds the intersection contents of the queue and a queue given as argument.
221
+ #
222
+ # @param priority_queue [PriorityQueue]
223
+ # @raise ArgumentError, if given object is not a priority queue
224
+ # @return [PriorityQueue] self object
225
+ def intersect!(priority_queue)
226
+ raise ArgumentError, "Undefined method PriorityQueue#intersect! for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
227
+ @binary_heap.intersect!(priority_queue.send(:binary_heap))
228
+ self
229
+ end
230
+
231
+ # Finds the difference of contents of the queue and a queue given as argument.
232
+ #
233
+ # @param priority_queue [PriorityQueue]
234
+ # @raise ArgumentError, if given object is not a priority queue
235
+ # @return [PriorityQueue] a new priority queue
236
+ def -(priority_queue)
237
+ raise ArgumentError, "Undefined method PriorityQueue#- for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
238
+ PriorityQueue.new(@binary_heap - priority_queue.send(:binary_heap), min: !@max)
239
+ end
240
+
241
+ # (see #-)
242
+ def difference(priority_queue)
243
+ raise ArgumentError, "Undefined method PriorityQueue#difference for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
244
+ self - priority_queue
245
+ end
246
+
247
+ # Finds the difference of contents of the queue and a queue given as argument.
248
+ #
249
+ # @param priority_queue [PriorityQueue]
250
+ # @raise ArgumentError, if given object is not a priority queue
251
+ # @return [PriorityQueue] self object
252
+ def difference!(priority_queue)
253
+ raise ArgumentError, "Undefined method PriorityQueue#difference! for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
254
+ @binary_heap.difference!(priority_queue.send(:binary_heap))
255
+ self
256
+ end
257
+
258
+ # Finds the symmetric difference of contents of the queue and a queue given as argument.
259
+ #
260
+ # @param priority_queue [PriorityQueue]
261
+ # @raise ArgumentError, if given object is not a priority queue
262
+ # @return [PriorityQueue] a new priority queue
263
+ def ^(priority_queue)
264
+ raise ArgumentError, "Undefined method PriorityQueue#^ for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
265
+ PriorityQueue.new(@binary_heap ^ priority_queue.send(:binary_heap), min: !@max)
266
+ end
267
+
268
+ # (see #^)
269
+ def symmetric_difference(priority_queue)
270
+ raise ArgumentError, "Undefined method PriorityQueue#symmetric_difference for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
271
+ self ^ priority_queue
272
+ end
273
+
274
+ # Finds the symmetric difference of contents of the queue and a queue given as argument.
275
+ #
276
+ # @param priority_queue [PriorityQueue]
277
+ # @raise ArgumentError, if given object is not a priority queue
278
+ # @return [PriorityQueue] self object
279
+ def symmetric_difference!(priority_queue)
280
+ raise ArgumentError, "Undefined method PriorityQueue#symmetric_difference! for #{priority_queue}:#{priority_queue.class}" unless priority_queue.is_a?(PriorityQueue)
281
+ @binary_heap.symmetric_difference!(priority_queue.send(:binary_heap))
282
+ self
283
+ end
284
+
285
+ # Filters elements of the queue by given condition.
286
+ #
287
+ # @param &block
288
+ # @return [PriorityQueue] a new priority queue
289
+ def select(&block)
290
+ PriorityQueue.new(@binary_heap.select { |e| block.call(e) }, min: !@max)
291
+ end
292
+
293
+ # Filters elements of the queue by given condition.
294
+ #
295
+ # @param &block
296
+ # @return [PriorityQueue] self object
297
+ def select!(&block)
298
+ @binary_heap.select! { |e| block.call(e) }
299
+ self
300
+ end
301
+
302
+ # (see #select)
303
+ def filter(&block)
304
+ select(&block)
305
+ end
306
+
307
+ # (see #select!)
308
+ def filter!(&block)
309
+ select!(&block)
310
+ end
311
+
312
+ # (see #select)
313
+ def find_all(&block)
314
+ select(&block)
315
+ end
316
+
317
+ # (see #select!)
318
+ def find_all!(&block)
319
+ select!(&block)
320
+ end
321
+
322
+ # Applies a function to each element of the queue.
323
+ #
324
+ # @param &block
325
+ # @return [PriorityQueue] a new priority queue
326
+ def map(&block)
327
+ PriorityQueue.new(@binary_heap.map { |e| block.call(e)}, min: !@max)
328
+ end
329
+
330
+ # Applies a function to each element of the queue.
331
+ #
332
+ # @param &block
333
+ # @return [PriorityQueue] self object
334
+ def map!(&block)
335
+ @binary_heap.map! { |e| block.call(e) }
336
+ self
337
+ end
338
+
339
+ # (see #map)
340
+ def apply(&block)
341
+ map(&block)
342
+ end
343
+
344
+ # (see #map!)
345
+ def apply!(&block)
346
+ map!(&block)
347
+ end
348
+
349
+ private :binary_heap
350
+
351
+ end
352
+ end
353
+ end
354
+ end
@@ -58,6 +58,11 @@ module Algorithmix
58
58
  @container.size
59
59
  end
60
60
 
61
+ # (see #size)
62
+ def length
63
+ @container.size
64
+ end
65
+
61
66
  # Checks if the queue is empty.
62
67
  def empty?
63
68
  @container.empty?
@@ -53,6 +53,11 @@ module Algorithmix
53
53
  @container.size
54
54
  end
55
55
 
56
+ # (see #size)
57
+ def length
58
+ @container.size
59
+ end
60
+
56
61
  # Returns the top element of the stack, without removing it.
57
62
  # If the stack is empty, returns nil.
58
63
  def top
@@ -70,6 +70,11 @@ module Algorithmix
70
70
  @container.size
71
71
  end
72
72
 
73
+ # (see #size)
74
+ def length
75
+ @container.size
76
+ end
77
+
73
78
  # Returns information for current ...
74
79
  def empty?
75
80
  @container.empty?
@@ -1,3 +1,3 @@
1
1
  module Algorithmix
2
- VERSION = "0.0.0.4.1"
2
+ VERSION = "0.0.0.5"
3
3
  end
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.1
4
+ version: 0.0.0.5
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-14 00:00:00.000000000 Z
11
+ date: 2018-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,18 +66,9 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 2.0.0
69
- description: |2+
70
- Currently the gem contains:
71
-
72
- 1. Stack
73
-
74
- 2. Queue
75
-
76
- 3. Deque
77
-
78
- 4. Binary heap
79
-
80
-
69
+ description: " The gem contains various implementations of
70
+ known and uknown data structures and algorithms. \n /Currently
71
+ the gem contains: #stack, #queue, #deque, #binary_heap, #priority_queue/\n"
81
72
  email:
82
73
  - "*@*.*"
83
74
  executables: []
@@ -97,6 +88,7 @@ files:
97
88
  - bin/setup
98
89
  - lib/algorithmix.rb
99
90
  - lib/algorithmix/data_structure/generic/deque.rb
91
+ - lib/algorithmix/data_structure/generic/priority_queue.rb
100
92
  - lib/algorithmix/data_structure/generic/queue.rb
101
93
  - lib/algorithmix/data_structure/generic/stack.rb
102
94
  - lib/algorithmix/data_structure/heap/binary_heap.rb