ordered_ds 1.0.0 → 1.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.
- checksums.yaml +4 -4
- data/lib/ordered_ds.rb +27 -17
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a943103e96d394a520af9c9ca6377dbd780749064c11e54aa69bfd62e07c25c
|
4
|
+
data.tar.gz: be7562dcc6eb2cb47c37d3f9f53b2014e6c86386fa4ab8be1d01e894688af54a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 518f0105e1868a804377623f33c2b3bf78efcf54ec926f433c7713cb01c0399ee0c74b8c5e00502224127f72e337941b4cfdcbc828cf81e05cfb52379f60dba9
|
7
|
+
data.tar.gz: 4153bbe301542b4a291f2a19493fcddd91767cdb00e1cc51c0c6dd86e99b831994f9778e7086e777d479f772daf248e9f1cbd038c5a5a09a4e1a7153ffc7ca6e
|
data/lib/ordered_ds.rb
CHANGED
@@ -1,43 +1,53 @@
|
|
1
1
|
class PQ
|
2
2
|
|
3
3
|
def initialize array = [], heapify = true, &is_unordered
|
4
|
-
raise ArgumentError.new 'PQ init' unless
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
raise ArgumentError.new 'PQ init' unless
|
5
|
+
array.class == Array &&
|
6
|
+
(heapify == true || heapify == false) &&
|
7
|
+
block_given?
|
8
|
+
@a, @u = array, is_unordered
|
9
|
+
return unless heapify
|
10
|
+
i = @a.size / 2
|
9
11
|
sink i while (i -= 1) >= 0
|
10
12
|
end
|
11
13
|
|
12
|
-
def size = @
|
14
|
+
def size = @a.size
|
15
|
+
def empty? = @a.empty?
|
16
|
+
def top = @a.first
|
17
|
+
def push_pop(x) = !@a.empty? && @u.(x, @a.first) ? pop_push(x) : x
|
13
18
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
19
|
+
def pop_push x
|
20
|
+
t, @a[0], = @a.first, x
|
21
|
+
sink 0
|
22
|
+
t
|
23
|
+
end
|
17
24
|
|
18
25
|
def << x
|
19
|
-
|
20
|
-
@
|
26
|
+
i = @a.size
|
27
|
+
@a << x
|
21
28
|
while i > 0
|
22
|
-
p = (i -
|
29
|
+
p = (i - 1) / 2
|
23
30
|
break unless @u.call @a[p], @a[i]
|
24
31
|
@a[p], @a[i] = @a[i], @a[p]
|
25
32
|
i = p
|
26
33
|
end
|
34
|
+
self
|
27
35
|
end
|
28
36
|
|
29
37
|
def pop
|
30
|
-
return
|
31
|
-
@a[0] = @a
|
38
|
+
return @a.pop if @a.size < 2
|
39
|
+
t, @a[0] = @a.first, @a.pop
|
32
40
|
sink 0
|
33
|
-
|
41
|
+
t
|
34
42
|
end
|
35
43
|
|
36
44
|
private
|
37
45
|
|
38
46
|
def sink p
|
39
|
-
|
40
|
-
|
47
|
+
z = @a.size
|
48
|
+
while (c = p * 2 + 1) < z
|
49
|
+
r = c + 1
|
50
|
+
c = r if r < z && @u.(@a[c], @a[r])
|
41
51
|
break unless @u.call @a[p], @a[c]
|
42
52
|
@a[p], @a[c] = @a[c], @a[p]
|
43
53
|
p = c
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ordered_ds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Viktor Reznov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|