range_list 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -3
- data/lib/range_list/version.rb +1 -1
- data/lib/range_list.rb +26 -28
- data/range_list.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3bbec9e50fbfcf613d6371f9851c1410269819c738d362bbe0db73bf6ab1ed6
|
4
|
+
data.tar.gz: 48b42d5f37e67577cc92528b4f3e898b58f81f7781fa4ea388347572fb70d90a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6f784320a3ab3b9b1c7714c9c7350aef3824b222a28ca0b1278722a728564b91c6c40faf3bbb8ea8a2926795daa56ca8f3db740cec6fd8833371bb60214ddd8
|
7
|
+
data.tar.gz: 98c20d0456e3c84432dfa9d3d09438c4887606dead589cfb1006407569b3c7f1330e0e3401fba5562b0c1c0c3dba408ad43fa0ea18f99232775ce7dcef69c3ee
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
range_list (1.
|
5
|
-
|
4
|
+
range_list (1.2.0)
|
5
|
+
rbtree (~> 0.4.5)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
@@ -15,6 +15,7 @@ GEM
|
|
15
15
|
ast (~> 2.4.1)
|
16
16
|
rainbow (3.1.1)
|
17
17
|
rake (13.0.6)
|
18
|
+
rbtree (0.4.5)
|
18
19
|
regexp_parser (2.2.1)
|
19
20
|
rexml (3.2.5)
|
20
21
|
rubocop (1.25.1)
|
@@ -44,7 +45,6 @@ GEM
|
|
44
45
|
standard (1.7.2)
|
45
46
|
rubocop (= 1.25.1)
|
46
47
|
rubocop-performance (= 1.13.2)
|
47
|
-
treemap (1.0.3)
|
48
48
|
unicode-display_width (2.1.0)
|
49
49
|
webrick (1.7.0)
|
50
50
|
yard (0.9.27)
|
data/lib/range_list/version.rb
CHANGED
data/lib/range_list.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "
|
3
|
+
require "rbtree"
|
4
4
|
require_relative "range_list/version"
|
5
5
|
require_relative "range_list/errors"
|
6
6
|
|
@@ -8,7 +8,7 @@ class RangeList
|
|
8
8
|
include Enumerable
|
9
9
|
|
10
10
|
def initialize
|
11
|
-
@tree =
|
11
|
+
@tree = RBTree.new
|
12
12
|
end
|
13
13
|
|
14
14
|
# Add range into current range list.
|
@@ -23,27 +23,27 @@ class RangeList
|
|
23
23
|
return self if empty_range?(range)
|
24
24
|
|
25
25
|
# Get real range start.
|
26
|
-
start_floor_entry = tree.
|
27
|
-
range_start = if !start_floor_entry.nil? && start_floor_entry
|
28
|
-
start_floor_entry
|
26
|
+
start_floor_entry = tree.upper_bound(range[0])
|
27
|
+
range_start = if !start_floor_entry.nil? && start_floor_entry[1] >= range[0]
|
28
|
+
start_floor_entry[0]
|
29
29
|
else
|
30
30
|
range[0]
|
31
31
|
end
|
32
32
|
|
33
33
|
# Get real range end.
|
34
|
-
end_floor_entry = tree.
|
35
|
-
range_end = if !end_floor_entry.nil? && end_floor_entry
|
36
|
-
end_floor_entry
|
34
|
+
end_floor_entry = tree.upper_bound(range[1])
|
35
|
+
range_end = if !end_floor_entry.nil? && end_floor_entry[1] >= range[1]
|
36
|
+
end_floor_entry[1]
|
37
37
|
else
|
38
38
|
range[1]
|
39
39
|
end
|
40
40
|
|
41
41
|
# Insert or replace new range.
|
42
|
-
tree
|
42
|
+
tree[range_start] = range_end
|
43
43
|
|
44
44
|
# Remove keys between range, exclude start, include end.
|
45
|
-
between_maps = tree.
|
46
|
-
between_maps.
|
45
|
+
between_maps = tree.bound(range[0] + 1, range[1])
|
46
|
+
between_maps.to_a.each { |key, _value| tree.delete(key) }
|
47
47
|
|
48
48
|
self
|
49
49
|
end
|
@@ -59,20 +59,20 @@ class RangeList
|
|
59
59
|
return self if empty_range?(range)
|
60
60
|
|
61
61
|
# Insert end lower entry
|
62
|
-
end_lower_entry = tree.
|
63
|
-
if !end_lower_entry.nil? && end_lower_entry
|
64
|
-
tree
|
62
|
+
end_lower_entry = tree.upper_bound(range[1] - 1)
|
63
|
+
if !end_lower_entry.nil? && end_lower_entry[1] > range[1]
|
64
|
+
tree[range[1]] = end_lower_entry[1]
|
65
65
|
end
|
66
66
|
|
67
67
|
# Relace start lower entry
|
68
|
-
start_lower_entry = tree.
|
69
|
-
if !start_lower_entry.nil? && start_lower_entry
|
70
|
-
tree
|
68
|
+
start_lower_entry = tree.upper_bound(range[0] - 1)
|
69
|
+
if !start_lower_entry.nil? && start_lower_entry[1] > range[0]
|
70
|
+
tree[start_lower_entry[0]] = range[0]
|
71
71
|
end
|
72
72
|
|
73
73
|
# Remove keys between range, include start, exclude end
|
74
|
-
between_maps = tree.
|
75
|
-
between_maps.
|
74
|
+
between_maps = tree.bound(range[0], range[1] - 1)
|
75
|
+
between_maps.to_a.each { |key, _value| tree.delete(key) }
|
76
76
|
|
77
77
|
self
|
78
78
|
end
|
@@ -94,8 +94,8 @@ class RangeList
|
|
94
94
|
# Return false when range is empty.
|
95
95
|
return false if empty_range?(range)
|
96
96
|
|
97
|
-
start_floor_entry = tree.
|
98
|
-
!start_floor_entry.nil? && start_floor_entry
|
97
|
+
start_floor_entry = tree.upper_bound(range[0])
|
98
|
+
!start_floor_entry.nil? && start_floor_entry[1] >= range[1]
|
99
99
|
end
|
100
100
|
|
101
101
|
# Returns true if current ranges contains any element of the range.
|
@@ -109,8 +109,8 @@ class RangeList
|
|
109
109
|
# Return false when range is empty.
|
110
110
|
return false if empty_range?(range)
|
111
111
|
|
112
|
-
end_lower_entry = tree.
|
113
|
-
!end_lower_entry.nil? && end_lower_entry
|
112
|
+
end_lower_entry = tree.upper_bound(range[1] - 1)
|
113
|
+
!end_lower_entry.nil? && end_lower_entry[1] > range[0]
|
114
114
|
end
|
115
115
|
|
116
116
|
# Returns true if current ranges contains the element.
|
@@ -120,17 +120,15 @@ class RangeList
|
|
120
120
|
def contains?(element)
|
121
121
|
raise ArgumentError, "`element` should be `Integer` type." unless element.is_a?(Integer)
|
122
122
|
|
123
|
-
floor_entry = tree.
|
124
|
-
!floor_entry.nil? && floor_entry
|
123
|
+
floor_entry = tree.upper_bound(element)
|
124
|
+
!floor_entry.nil? && floor_entry[1] > element
|
125
125
|
end
|
126
126
|
|
127
127
|
# Give RangeList iterative ability.
|
128
128
|
# @yield [range_start, range_end] give the range element to the block
|
129
129
|
def each(&block)
|
130
130
|
if block
|
131
|
-
|
132
|
-
# see https://github.com/davidkellis/treemap/issues/1
|
133
|
-
tree.empty? ? self : tree.each(&block)
|
131
|
+
tree.each(&block)
|
134
132
|
else
|
135
133
|
enum_for(:each)
|
136
134
|
end
|
data/range_list.gemspec
CHANGED
@@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
|
|
34
34
|
|
35
35
|
# Uncomment to register a new dependency of your gem
|
36
36
|
# spec.add_dependency "example-gem", "~> 1.0"
|
37
|
-
spec.add_dependency "
|
37
|
+
spec.add_dependency "rbtree", "~> 0.4.5"
|
38
38
|
|
39
39
|
# For more information and examples about making a new gem, check out our
|
40
40
|
# guide at: https://bundler.io/guides/creating_gem.html
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: range_list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Song Huang
|
@@ -11,19 +11,19 @@ cert_chain: []
|
|
11
11
|
date: 2022-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: rbtree
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.4.5
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.4.5
|
27
27
|
description:
|
28
28
|
email:
|
29
29
|
- songhuangcn@gmail.com
|