ordered_ds 1.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ordered_ds.rb +56 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6202df63f7be8dfd7e47c05b1f4df1fac04921f1aa8a5bfa551d07e76f36c6b
4
+ data.tar.gz: 6b787b88249091dbbf956a4f9a93eecb74c3aa9d31f249cc49de191d069d0fe3
5
+ SHA512:
6
+ metadata.gz: c133ea1c52fe17ab14c8e75e2ab785b1487ac6546bb3b5713c91e3e719519b2155258f187fd1dc613597a78b74381fbf31e98c11c1bd889233b531ed51374c4a
7
+ data.tar.gz: '096d548ad2745206fb0e14097fbe893e097c44cad955e7429b6ce6b9f3accf54d27723c6564fa4fafd030ca7c3e4650a3faacfd92e690322b170b7dc73948583'
data/lib/ordered_ds.rb ADDED
@@ -0,0 +1,56 @@
1
+ class PQ
2
+
3
+ def initialize array = [], heapify = true, &is_unordered
4
+ raise ArgumentError.new 'PQ init' unless array.class == Array &&
5
+ (heapify == true || heapify == false) && block_given?
6
+ @a, @z, @u = array, array.size, is_unordered
7
+ return if @a.empty? || !heapify
8
+ i = 1 << Math.log(@z, 2).floor
9
+ sink i while (i -= 1) >= 0
10
+ end
11
+
12
+ def size = @z
13
+
14
+ def empty? = @z == 0
15
+
16
+ def top = @z > 0 ? @a.first : nil
17
+
18
+ def << x
19
+ @a[i = @z] = x
20
+ @z += 1
21
+ while i > 0
22
+ p = (i - (i[0] ^ 1)) / 2
23
+ break unless @u.call @a[p], @a[i]
24
+ @a[p], @a[i] = @a[i], @a[p]
25
+ i = p
26
+ end
27
+ end
28
+
29
+ def pop
30
+ return nil unless r = top
31
+ @a[0] = @a[@z -= 1]
32
+ sink 0
33
+ r
34
+ end
35
+
36
+ private
37
+
38
+ def sink p
39
+ while (c = p * 2 + 1) < @z
40
+ c += 1 if c + 1 < @z && @u.(@a[c], @a[c + 1])
41
+ break unless @u.call @a[p], @a[c]
42
+ @a[p], @a[c] = @a[c], @a[p]
43
+ p = c
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ class MinQ < PQ
50
+ def initialize(a = [], h = true) = super(a, h) { _1 > _2 }
51
+ end
52
+
53
+ class MaxQ < PQ
54
+ def initialize(a = [], h = true) = super(a, h) { _1 < _2 }
55
+ end
56
+
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ordered_ds
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Viktor Reznov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ordered_ds.rb
20
+ homepage: https://github.com/alantudyk/ordered_ds
21
+ licenses:
22
+ - Unlicense
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.3.5
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Read the code.
43
+ test_files: []