yet_another_range_list 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 380dd1f35360cdee97a14273f566eb744efdec0c1b5ff7ffeb02d977eb6c2127
4
+ data.tar.gz: b710ede01dbf42129ca3220c478e01c9bf988a195b9e32e4b56c1a026d4a411c
5
+ SHA512:
6
+ metadata.gz: ff66046a6c6a22e3e3f14fdd7c5f53295772cc53c3c65de0fb4b58d61b7b5ab1ccf073b4afc35a932030654e2eeb58e8bbfbef719a11565187c39ace0897444e
7
+ data.tar.gz: fcf35b4ff4082186377b775b59e060b22faec04b61ad1015c645189bbe13dba14e82523e1bfcb7ba4dbcf97a31fa852b93682f1868a2e4745eb44400b4cb6885
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.4
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Bran Liang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # YARL (Yet Another Range List)
2
+
3
+ YARL is a Ruby gem that provides efficient functionality for managing and manipulating range lists using a [segment tree](https://en.wikipedia.org/wiki/Segment_tree) data structure.
4
+
5
+ ## Features
6
+
7
+ - Add and remove ranges with ease
8
+ - Efficient range manipulation using segment trees
9
+ - Configurable maximum and minimum values
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'yet_another_range_list'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```
22
+ $ bundle install
23
+ ```
24
+
25
+ Or install it yourself as:
26
+
27
+ ```
28
+ $ gem install yet_another_range_list
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ### Basic Operations
34
+
35
+ ```ruby
36
+ require 'yet_another_range_list'
37
+
38
+ rl = YARL::RangeList.new
39
+
40
+ # Add ranges
41
+ rl.add([1, 5])
42
+ rl.add([3, 8])
43
+ rl << [10, 20]
44
+
45
+ # Remove ranges
46
+ rl.remove([10, 11])
47
+ rl.delete([15, 17])
48
+
49
+ # Print the current range list (Left bound inclusive, and right bound exclusive)
50
+ rl.print
51
+ # => [1, 8) [11, 15) [17, 20)
52
+
53
+ # Get current ranges (Both bound inclusively)
54
+ rl.to_a
55
+ # => [[1, 7], [11, 14], [17, 19]]
56
+ ```
57
+
58
+ ### Configuration
59
+
60
+ By default the support integer range is between `0` and `4294967295` (The max number for unsigned 32bit integer), you can customize the limit when initializing a `RangeList`:
61
+
62
+ ```ruby
63
+ rl = YARL::RangeList.new(-1000, 1000)
64
+ ```
65
+
66
+ ## Performance and Complexity
67
+
68
+ YARL uses a segment tree data structure to efficiently manage range operations. This allows for better performance compared to naive implementations, especially for large datasets or frequent operations.
69
+
70
+ ### Time Complexity
71
+
72
+ - **Initialization**: O(1).
73
+ - **Add/Remove Range**: O(log n) per operation, where n is the range of possible values (max_n - min_n).
74
+ - **Print/To Array**: O(log n).
75
+
76
+ ### Space Complexity
77
+
78
+ - O(m), where m is the number of distinct intervals that have been added or removed. This is because new nodes in the tree are only created when a range is modified.
79
+
80
+ ## Development
81
+
82
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
83
+
84
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
89
+
90
+ ## Code of Conduct
91
+
92
+ Everyone interacting in the YARL project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://jihulab.com/branliang1/yarl/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
@@ -0,0 +1,7 @@
1
+ module YARL
2
+ module Constants
3
+ # The maximum value of a 32-bit unsigned integer
4
+ DEFAULT_MAX_N = (1 << 32) - 1
5
+ DEFAULT_MIN_N = 0
6
+ end
7
+ end
data/lib/YARL/node.rb ADDED
@@ -0,0 +1,27 @@
1
+ module YARL
2
+ class Node
3
+ attr_reader :l, :r
4
+ attr_accessor :left, :right, :covered, :lazy_op
5
+ def initialize(l, r)
6
+ # l and r are the left and right bounds of the range
7
+ @l = l
8
+ @r = r
9
+
10
+ # left and right are the children of the node
11
+ @left = nil
12
+ @right = nil
13
+
14
+ # covered is true if the range is completely covered in the segment tree
15
+ @covered = false
16
+
17
+ # lazy_op is the operation to be applied to the node
18
+ @lazy_op = nil
19
+ end
20
+
21
+ def print(indent = 0)
22
+ puts "#{' ' * indent}[#{l}, #{r}] #{covered ? '(Y)' : ''} #{lazy_op ? '(LZ)' : ''}"
23
+ left&.print(indent + 2)
24
+ right&.print(indent + 2)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,6 @@
1
+ module YARL
2
+ module Operation
3
+ ADDING = 1
4
+ REMOVING = 2
5
+ end
6
+ end
@@ -0,0 +1,61 @@
1
+ module YARL
2
+ class RangeList
3
+ include Enumerable
4
+
5
+ def initialize(min = Constants::DEFAULT_MIN_N, max = Constants::DEFAULT_MAX_N)
6
+ @tree = SegmentTree.new(min, max)
7
+ @min = min
8
+ @max = max
9
+ end
10
+
11
+ # Add a range to the list
12
+ #
13
+ # @param range [Array<Integer>] the range to add
14
+ # @return [Self]
15
+ # @raise [RangeError] if the range is out of bounds
16
+ # @raise [RangeError] if the left bound is greater than the right bound
17
+ def add(range)
18
+ if range[0] < @min || range[1] > @max
19
+ raise RangeError, "Range [#{range[0]}, #{range[1]}) out of bounds. Valid range: [#{@min}, #{@max}]"
20
+ end
21
+
22
+ if range[0] > range[1]
23
+ raise RangeError, "Left bound must be less than or equal to right bound"
24
+ end
25
+
26
+ @tree.update(range[0], range[1] - 1, Operation::ADDING)
27
+ self
28
+ end
29
+ alias_method :<<, :add
30
+
31
+ # Remove a range from the list
32
+ #
33
+ # @param range [Array<Integer>] the range to remove
34
+ # @return [Self]
35
+ def remove(range)
36
+ @tree.update(range[0], range[1] - 1, Operation::REMOVING)
37
+ self
38
+ end
39
+ alias_method :delete, :remove
40
+
41
+ def each(&block)
42
+ @tree.ranges.each do |range|
43
+ block.call(range)
44
+ end
45
+ end
46
+
47
+ # Print the ranges in the list, separated by a space
48
+ # Left bound is inclusive, right bound is exclusive
49
+ #
50
+ # @return [void]
51
+ def print
52
+ ranges_str = @tree.ranges.map do |range|
53
+ "[#{range[0]}, #{range[1] + 1})"
54
+ end.join(" ")
55
+
56
+ puts ranges_str
57
+ end
58
+ alias_method :to_s, :print
59
+ alias_method :inspect, :print
60
+ end
61
+ end
@@ -0,0 +1,76 @@
1
+ module YARL
2
+ class SegmentTree
3
+ attr_reader :root
4
+ def initialize(min, max)
5
+ @root = Node.new(min, max)
6
+ end
7
+
8
+ def update(start, finish, operation = Operation::ADDING, node = @root)
9
+ if start <= node.l && finish >= node.r
10
+ node.lazy_op = operation
11
+ node.left = nil
12
+ node.right = nil
13
+ case operation
14
+ when Operation::ADDING
15
+ node.covered = true
16
+ when Operation::REMOVING
17
+ node.covered = false
18
+ else
19
+ raise ArgumentError, "Invalid operation"
20
+ end
21
+ return
22
+ end
23
+
24
+ # Push down the operation to the children
25
+ mid = (node.l + node.r) >> 1
26
+ node.left ||= Node.new(node.l, mid)
27
+ node.right ||= Node.new(mid + 1, node.r)
28
+ if node.lazy_op
29
+ [node.left, node.right].each do |child|
30
+ child.lazy_op = node.lazy_op
31
+ case node.lazy_op
32
+ when Operation::ADDING
33
+ child.covered = true
34
+ when Operation::REMOVING
35
+ child.covered = false
36
+ else
37
+ raise ArgumentError, "Invalid operation"
38
+ end
39
+ end
40
+ node.lazy_op = nil
41
+ end
42
+
43
+ if start <= mid
44
+ update(start, finish, operation, node.left)
45
+ end
46
+
47
+ if finish > mid
48
+ update(start, finish, operation, node.right)
49
+ end
50
+
51
+ # Push up the operation to the parent
52
+ node.covered = node.left.covered && node.right.covered
53
+ nil
54
+ end
55
+
56
+ def ranges(node = @root, acc = [])
57
+ if node.covered
58
+ last = acc.last
59
+ # Merge the ranges if they are adjacent
60
+ if last && last[1] + 1 == node.l
61
+ last[1] = node.r
62
+ else
63
+ acc << [node.l, node.r]
64
+ end
65
+ else
66
+ ranges(node.left, acc) if node.left
67
+ ranges(node.right, acc) if node.right
68
+ end
69
+ acc
70
+ end
71
+
72
+ def print
73
+ @root.print
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YARL
4
+ VERSION = "0.1.0"
5
+ end
data/lib/YARL.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "YARL/version"
4
+ require_relative "YARL/constants"
5
+ require_relative "YARL/operation"
6
+ require_relative "YARL/node"
7
+ require_relative "YARL/segment_tree"
8
+ require_relative "YARL/range_list"
9
+
10
+ module YARL
11
+ class Error < StandardError; end
12
+ class RangeError < Error; end
13
+ end
data/sig/YARL.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module YARL
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yet_another_range_list
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bran Liang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - lby89757@hotmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".ruby-version"
21
+ - CODE_OF_CONDUCT.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/YARL.rb
26
+ - lib/YARL/constants.rb
27
+ - lib/YARL/node.rb
28
+ - lib/YARL/operation.rb
29
+ - lib/YARL/range_list.rb
30
+ - lib/YARL/segment_tree.rb
31
+ - lib/YARL/version.rb
32
+ - sig/YARL.rbs
33
+ homepage: https://jihulab.com/branliang1/yarl
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://jihulab.com/branliang1/yarl
38
+ source_code_uri: https://jihulab.com/branliang1/yarl
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.5.15
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Yet Another Range List library to handle integer ranges.
58
+ test_files: []