set 1.0.0 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/test.yml +2 -4
- data/CHANGELOG.md +22 -1
- data/README.md +33 -4
- data/lib/set/sorted_set.rb +1 -1
- data/lib/set.rb +244 -78
- data/set.gemspec +2 -4
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e23398781865843cd7ba5286dca1a65ff241807ba8196020407b986bc74270b1
|
4
|
+
data.tar.gz: 19c57ed5aa898b0e4ee695df08aeccfb0d313cd785d4dd318941d5bb68291595
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88ee0f430a9b381d557f4c8bbe161c2199207ffcbaeb79d78daec9b9dc3e4ebb9fbc3a397ae82c5ffeba0b73782e9363ee19512e0b1b0372628ef4c6d715365a
|
7
|
+
data.tar.gz: d05a97bb9a5cb900eb303ce85926b73e04530fd44ca0cc85e9ca09e8413af5f5a3ad812cf4409a9fd5f2bc191632e8673d9fc3c89ebb62b8fa13fcf6a3c643ff
|
data/.github/workflows/test.yml
CHANGED
@@ -11,14 +11,12 @@ jobs:
|
|
11
11
|
os: [ ubuntu-latest, macos-latest ]
|
12
12
|
runs-on: ${{ matrix.os }}
|
13
13
|
steps:
|
14
|
-
- uses: actions/checkout@
|
14
|
+
- uses: actions/checkout@v3
|
15
15
|
- name: Set up Ruby
|
16
16
|
uses: ruby/setup-ruby@v1
|
17
17
|
with:
|
18
18
|
ruby-version: ${{ matrix.ruby }}
|
19
19
|
- name: Install dependencies
|
20
|
-
run:
|
21
|
-
gem install bundler --no-document
|
22
|
-
bundle install
|
20
|
+
run: bundle install
|
23
21
|
- name: Run test
|
24
22
|
run: rake test
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
# Set Changelog
|
2
2
|
|
3
|
-
|
3
|
+
# 1.0.2 (2021-10-25)
|
4
|
+
|
5
|
+
* Enhancements
|
6
|
+
* Allow Set#intersect? and #disjoint? to accept array argument \[[Feature #17838][]] [#18][] ([@jeremyevans][])
|
7
|
+
* Make Set#pretty_print IRB::ColorPrinter friendly [#17][] ([@k-tsj][])
|
8
|
+
* Improve documentation [#16][], [#20][] ([@BurdetteLamar][])
|
9
|
+
* Follow the license change in Ruby and add BSD-2-Clause
|
10
|
+
|
11
|
+
# 1.0.1 (2020-12-22)
|
12
|
+
|
13
|
+
* Enhancements
|
14
|
+
* Eliminate warnings
|
15
|
+
|
16
|
+
# 1.0.0 (2020-12-21)
|
4
17
|
|
5
18
|
This is the first release of set as a gem. Here lists the changes since the version bundled with Ruby 2.7.
|
6
19
|
|
@@ -14,6 +27,14 @@ This is the first release of set as a gem. Here lists the changes since the ver
|
|
14
27
|
[#2]: https://github.com/ruby/set/pull/2
|
15
28
|
[#4]: https://github.com/ruby/set/pull/4
|
16
29
|
[#5]: https://github.com/ruby/set/pull/5
|
30
|
+
[#16]: https://github.com/ruby/set/pull/16
|
31
|
+
[#17]: https://github.com/ruby/set/pull/17
|
32
|
+
[#18]: https://github.com/ruby/set/pull/18
|
33
|
+
[#20]: https://github.com/ruby/set/pull/20
|
34
|
+
[Feature #17838]: https://bugs.ruby-lang.org/issues/17838
|
17
35
|
|
36
|
+
[@BurdetteLamar]: https://github.com/BurdetteLamar
|
37
|
+
[@jeremyevans]: https://github.com/jeremyevans
|
38
|
+
[@k-tsj]: https://github.com/k-tsj
|
18
39
|
[@knu]: https://github.com/knu
|
19
40
|
[@marcandre]: https://github.com/marcandre
|
data/README.md
CHANGED
@@ -1,8 +1,39 @@
|
|
1
1
|
# Set
|
2
2
|
|
3
|
-
|
3
|
+
This library provides the Set class, which deals with a collection of
|
4
|
+
unordered values with no duplicates. It is a hybrid of Array's
|
5
|
+
intuitive inter-operation facilities and Hash's fast lookup.
|
4
6
|
|
5
|
-
|
7
|
+
The method `to_set` is added to Enumerable for convenience.
|
8
|
+
|
9
|
+
Set implements a collection of unordered values with no duplicates.
|
10
|
+
This is a hybrid of Array's intuitive inter-operation facilities and
|
11
|
+
Hash's fast lookup.
|
12
|
+
|
13
|
+
Set is easy to use with Enumerable objects (implementing `each`).
|
14
|
+
Most of the initializer methods and binary operators accept generic
|
15
|
+
Enumerable objects besides sets and arrays. An Enumerable object can
|
16
|
+
be converted to Set using the `to_set` method.
|
17
|
+
|
18
|
+
Set uses Hash as storage, so you must note the following points:
|
19
|
+
|
20
|
+
* Equality of elements is determined according to Object#eql? and
|
21
|
+
Object#hash. Use Set#compare_by_identity to make a set compare its
|
22
|
+
elements by their identity.
|
23
|
+
|
24
|
+
* Set assumes that the identity of each element does not change while
|
25
|
+
it is stored. Modifying an element of a set will render the set to
|
26
|
+
an unreliable state.
|
27
|
+
|
28
|
+
* When a string is to be stored, a frozen copy of the string is stored
|
29
|
+
instead unless the original string is already frozen.
|
30
|
+
|
31
|
+
### Comparison
|
32
|
+
|
33
|
+
The comparison operators `<`, `>`, `<=`, and `>=` are implemented as
|
34
|
+
shorthand for the {proper_,}{subset?,superset?} methods. The `<=>`
|
35
|
+
operator reflects this order, or return `nil` for sets that both have
|
36
|
+
distinct elements (`{x, y}` vs. `{x, z}` for example).
|
6
37
|
|
7
38
|
## Installation
|
8
39
|
|
@@ -37,8 +68,6 @@ s2.subset?(s1) #=> true
|
|
37
68
|
|
38
69
|
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.
|
39
70
|
|
40
|
-
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
41
|
-
|
42
71
|
## Contributing
|
43
72
|
|
44
73
|
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/set.
|
data/lib/set/sorted_set.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
begin
|
2
2
|
require 'sorted_set'
|
3
3
|
rescue ::LoadError
|
4
|
-
raise "The `SortedSet` class has been extracted from the `set` library." \
|
4
|
+
raise "The `SortedSet` class has been extracted from the `set` library. " \
|
5
5
|
"You must use the `sorted_set` gem or other alternatives."
|
6
6
|
end
|
data/lib/set.rb
CHANGED
@@ -1,35 +1,31 @@
|
|
1
|
-
#--
|
2
1
|
# frozen_string_literal: true
|
2
|
+
# :markup: markdown
|
3
3
|
#
|
4
4
|
# set.rb - defines the Set class
|
5
|
-
|
6
|
-
# Copyright (c) 2002-
|
5
|
+
#
|
6
|
+
# Copyright (c) 2002-2020 Akinori MUSHA <knu@iDaemons.org>
|
7
7
|
#
|
8
8
|
# Documentation by Akinori MUSHA and Gavin Sinclair.
|
9
9
|
#
|
10
10
|
# All rights reserved. You can redistribute and/or modify it under the same
|
11
11
|
# terms as Ruby.
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
# == Overview
|
16
|
-
#
|
12
|
+
|
13
|
+
|
14
|
+
##
|
17
15
|
# This library provides the Set class, which deals with a collection
|
18
16
|
# of unordered values with no duplicates. It is a hybrid of Array's
|
19
17
|
# intuitive inter-operation facilities and Hash's fast lookup.
|
20
18
|
#
|
21
|
-
# The method
|
22
|
-
|
23
|
-
|
19
|
+
# The method `to_set` is added to Enumerable for convenience.
|
24
20
|
#
|
25
21
|
# Set implements a collection of unordered values with no duplicates.
|
26
22
|
# This is a hybrid of Array's intuitive inter-operation facilities and
|
27
23
|
# Hash's fast lookup.
|
28
24
|
#
|
29
|
-
# Set is easy to use with Enumerable objects (implementing
|
25
|
+
# Set is easy to use with Enumerable objects (implementing `each`).
|
30
26
|
# Most of the initializer methods and binary operators accept generic
|
31
27
|
# Enumerable objects besides sets and arrays. An Enumerable object
|
32
|
-
# can be converted to Set using the
|
28
|
+
# can be converted to Set using the `to_set` method.
|
33
29
|
#
|
34
30
|
# Set uses Hash as storage, so you must note the following points:
|
35
31
|
#
|
@@ -42,27 +38,186 @@
|
|
42
38
|
# * When a string is to be stored, a frozen copy of the string is
|
43
39
|
# stored instead unless the original string is already frozen.
|
44
40
|
#
|
45
|
-
#
|
41
|
+
# ## Comparison
|
42
|
+
#
|
43
|
+
# The comparison operators `<`, `>`, `<=`, and `>=` are implemented as
|
44
|
+
# shorthand for the {proper_,}{subset?,superset?} methods. The `<=>`
|
45
|
+
# operator reflects this order, or return `nil` for sets that both
|
46
|
+
# have distinct elements (`{x, y}` vs. `{x, z}` for example).
|
47
|
+
#
|
48
|
+
# ## Example
|
49
|
+
#
|
50
|
+
# ```ruby
|
51
|
+
# require 'set'
|
52
|
+
# s1 = Set[1, 2] #=> #<Set: {1, 2}>
|
53
|
+
# s2 = [1, 2].to_set #=> #<Set: {1, 2}>
|
54
|
+
# s1 == s2 #=> true
|
55
|
+
# s1.add("foo") #=> #<Set: {1, 2, "foo"}>
|
56
|
+
# s1.merge([2, 6]) #=> #<Set: {1, 2, "foo", 6}>
|
57
|
+
# s1.subset?(s2) #=> false
|
58
|
+
# s2.subset?(s1) #=> true
|
59
|
+
# ```
|
60
|
+
#
|
61
|
+
# ## Contact
|
62
|
+
#
|
63
|
+
# - Akinori MUSHA <<knu@iDaemons.org>> (current maintainer)
|
64
|
+
#
|
65
|
+
# ## What's Here
|
66
|
+
#
|
67
|
+
# First, what's elsewhere. \Class \Set:
|
68
|
+
#
|
69
|
+
# - Inherits from {class Object}[https://docs.ruby-lang.org/en/master/Object.html#class-Object-label-What-27s+Here].
|
70
|
+
# - Includes {module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-What-27s+Here],
|
71
|
+
# which provides dozens of additional methods.
|
72
|
+
#
|
73
|
+
# In particular, class \Set does not have many methods of its own
|
74
|
+
# for fetching or for iterating.
|
75
|
+
# Instead, it relies on those in \Enumerable.
|
76
|
+
#
|
77
|
+
# Here, class \Set provides methods that are useful for:
|
78
|
+
#
|
79
|
+
# - [Creating a Set](#class-Set-label-Methods+for+Creating+a+Set)
|
80
|
+
# - [Set Operations](#class-Set-label-Methods+for+Set+Operations)
|
81
|
+
# - [Comparing](#class-Set-label-Methods+for+Comparing)
|
82
|
+
# - [Querying](#class-Set-label-Methods+for+Querying)
|
83
|
+
# - [Assigning](#class-Set-label-Methods+for+Assigning)
|
84
|
+
# - [Deleting](#class-Set-label-Methods+for+Deleting)
|
85
|
+
# - [Converting](#class-Set-label-Methods+for+Converting)
|
86
|
+
# - [Iterating](#class-Set-label-Methods+for+Iterating)
|
87
|
+
# - [And more....](#class-Set-label-Other+Methods)
|
46
88
|
#
|
47
|
-
#
|
48
|
-
# shorthand for the {proper_,}{subset?,superset?} methods.
|
49
|
-
# The <=> operator reflects this order, or return `nil` for
|
50
|
-
# sets that both have distinct elements ({x, y} vs. {x, z} for example).
|
89
|
+
# ### Methods for Creating a \Set
|
51
90
|
#
|
52
|
-
#
|
91
|
+
# - ::[]:
|
92
|
+
# Returns a new set containing the given objects.
|
93
|
+
# - ::new:
|
94
|
+
# Returns a new set containing either the given objects
|
95
|
+
# (if no block given) or the return values from the called block
|
96
|
+
# (if a block given).
|
53
97
|
#
|
54
|
-
#
|
55
|
-
# s1 = Set[1, 2] #=> #<Set: {1, 2}>
|
56
|
-
# s2 = [1, 2].to_set #=> #<Set: {1, 2}>
|
57
|
-
# s1 == s2 #=> true
|
58
|
-
# s1.add("foo") #=> #<Set: {1, 2, "foo"}>
|
59
|
-
# s1.merge([2, 6]) #=> #<Set: {1, 2, "foo", 6}>
|
60
|
-
# s1.subset?(s2) #=> false
|
61
|
-
# s2.subset?(s1) #=> true
|
98
|
+
# ### Methods for \Set Operations
|
62
99
|
#
|
63
|
-
#
|
100
|
+
# - [|](#method-i-7C) (aliased as #union and #+):
|
101
|
+
# Returns a new set containing all elements from +self+
|
102
|
+
# and all elements from a given enumerable (no duplicates).
|
103
|
+
# - [&](#method-i-26) (aliased as #intersection):
|
104
|
+
# Returns a new set containing all elements common to +self+
|
105
|
+
# and a given enumerable.
|
106
|
+
# - [-](#method-i-2D) (aliased as #difference):
|
107
|
+
# Returns a copy of +self+ with all elements
|
108
|
+
# in a given enumerable removed.
|
109
|
+
# - [\^](#method-i-5E):
|
110
|
+
# Returns a new set containing all elements from +self+
|
111
|
+
# and a given enumerable except those common to both.
|
64
112
|
#
|
65
|
-
#
|
113
|
+
# ### Methods for Comparing
|
114
|
+
#
|
115
|
+
# - [<=>](#method-i-3C-3D-3E):
|
116
|
+
# Returns -1, 0, or 1 as +self+ is less than, equal to,
|
117
|
+
# or greater than a given object.
|
118
|
+
# - [==](#method-i-3D-3D):
|
119
|
+
# Returns whether +self+ and a given enumerable are equal,
|
120
|
+
# as determined by Object#eql?.
|
121
|
+
# - \#compare_by_identity?:
|
122
|
+
# Returns whether the set considers only identity
|
123
|
+
# when comparing elements.
|
124
|
+
#
|
125
|
+
# ### Methods for Querying
|
126
|
+
#
|
127
|
+
# - \#length (aliased as #size):
|
128
|
+
# Returns the count of elements.
|
129
|
+
# - \#empty?:
|
130
|
+
# Returns whether the set has no elements.
|
131
|
+
# - \#include? (aliased as #member? and #===):
|
132
|
+
# Returns whether a given object is an element in the set.
|
133
|
+
# - \#subset? (aliased as [<=](#method-i-3C-3D)):
|
134
|
+
# Returns whether a given object is a subset of the set.
|
135
|
+
# - \#proper_subset? (aliased as [<](#method-i-3C)):
|
136
|
+
# Returns whether a given enumerable is a proper subset of the set.
|
137
|
+
# - \#superset? (aliased as [>=](#method-i-3E-3D])):
|
138
|
+
# Returns whether a given enumerable is a superset of the set.
|
139
|
+
# - \#proper_superset? (aliased as [>](#method-i-3E)):
|
140
|
+
# Returns whether a given enumerable is a proper superset of the set.
|
141
|
+
# - \#disjoint?:
|
142
|
+
# Returns +true+ if the set and a given enumerable
|
143
|
+
# have no common elements, +false+ otherwise.
|
144
|
+
# - \#intersect?:
|
145
|
+
# Returns +true+ if the set and a given enumerable:
|
146
|
+
# have any common elements, +false+ otherwise.
|
147
|
+
# - \#compare_by_identity?:
|
148
|
+
# Returns whether the set considers only identity
|
149
|
+
# when comparing elements.
|
150
|
+
#
|
151
|
+
# ### Methods for Assigning
|
152
|
+
#
|
153
|
+
# - \#add (aliased as #<<):
|
154
|
+
# Adds a given object to the set; returns +self+.
|
155
|
+
# - \#add?:
|
156
|
+
# If the given object is not an element in the set,
|
157
|
+
# adds it and returns +self+; otherwise, returns +nil+.
|
158
|
+
# - \#merge:
|
159
|
+
# Adds each given object to the set; returns +self+.
|
160
|
+
# - \#replace:
|
161
|
+
# Replaces the contents of the set with the contents
|
162
|
+
# of a given enumerable.
|
163
|
+
#
|
164
|
+
# ### Methods for Deleting
|
165
|
+
#
|
166
|
+
# - \#clear:
|
167
|
+
# Removes all elements in the set; returns +self+.
|
168
|
+
# - \#delete:
|
169
|
+
# Removes a given object from the set; returns +self+.
|
170
|
+
# - \#delete?:
|
171
|
+
# If the given object is an element in the set,
|
172
|
+
# removes it and returns +self+; otherwise, returns +nil+.
|
173
|
+
# - \#subtract:
|
174
|
+
# Removes each given object from the set; returns +self+.
|
175
|
+
# - \#delete_if - Removes elements specified by a given block.
|
176
|
+
# - \#select! (aliased as #filter!):
|
177
|
+
# Removes elements not specified by a given block.
|
178
|
+
# - \#keep_if:
|
179
|
+
# Removes elements not specified by a given block.
|
180
|
+
# - \#reject!
|
181
|
+
# Removes elements specified by a given block.
|
182
|
+
#
|
183
|
+
# ### Methods for Converting
|
184
|
+
#
|
185
|
+
# - \#classify:
|
186
|
+
# Returns a hash that classifies the elements,
|
187
|
+
# as determined by the given block.
|
188
|
+
# - \#collect! (aliased as #map!):
|
189
|
+
# Replaces each element with a block return-value.
|
190
|
+
# - \#divide:
|
191
|
+
# Returns a hash that classifies the elements,
|
192
|
+
# as determined by the given block;
|
193
|
+
# differs from #classify in that the block may accept
|
194
|
+
# either one or two arguments.
|
195
|
+
# - \#flatten:
|
196
|
+
# Returns a new set that is a recursive flattening of +self+.
|
197
|
+
# \#flatten!:
|
198
|
+
# Replaces each nested set in +self+ with the elements from that set.
|
199
|
+
# - \#inspect (aliased as #to_s):
|
200
|
+
# Returns a string displaying the elements.
|
201
|
+
# - \#join:
|
202
|
+
# Returns a string containing all elements, converted to strings
|
203
|
+
# as needed, and joined by the given record separator.
|
204
|
+
# - \#to_a:
|
205
|
+
# Returns an array containing all set elements.
|
206
|
+
# - \#to_set:
|
207
|
+
# Returns +self+ if given no arguments and no block;
|
208
|
+
# with a block given, returns a new set consisting of block
|
209
|
+
# return values.
|
210
|
+
#
|
211
|
+
# ### Methods for Iterating
|
212
|
+
#
|
213
|
+
# - \#each:
|
214
|
+
# Calls the block with each successive element; returns +self+.
|
215
|
+
#
|
216
|
+
# ### Other Methods
|
217
|
+
#
|
218
|
+
# - \#reset:
|
219
|
+
# Resets the internal state; useful if an object
|
220
|
+
# has been modified while an element in the set.
|
66
221
|
#
|
67
222
|
class Set
|
68
223
|
include Enumerable
|
@@ -199,9 +354,9 @@ class Set
|
|
199
354
|
end
|
200
355
|
|
201
356
|
# Returns self if no arguments are given. Otherwise, converts the
|
202
|
-
# set to another with klass.new(self, *args, &block)
|
357
|
+
# set to another with `klass.new(self, *args, &block)`.
|
203
358
|
#
|
204
|
-
# In subclasses, returns klass.new(self, *args, &block) unless
|
359
|
+
# In subclasses, returns `klass.new(self, *args, &block)` unless
|
205
360
|
# overridden.
|
206
361
|
def to_set(klass = Set, *args, &block)
|
207
362
|
return self if instance_of?(Set) && klass == Set && block.nil? && args.empty?
|
@@ -315,25 +470,35 @@ class Set
|
|
315
470
|
end
|
316
471
|
end
|
317
472
|
|
318
|
-
# Returns true if the set and the given
|
473
|
+
# Returns true if the set and the given enumerable have at least one
|
319
474
|
# element in common.
|
320
475
|
#
|
321
|
-
#
|
322
|
-
#
|
476
|
+
# Set[1, 2, 3].intersect? Set[4, 5] #=> false
|
477
|
+
# Set[1, 2, 3].intersect? Set[3, 4] #=> true
|
478
|
+
# Set[1, 2, 3].intersect? 4..5 #=> false
|
479
|
+
# Set[1, 2, 3].intersect? [3, 4] #=> true
|
323
480
|
def intersect?(set)
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
481
|
+
case set
|
482
|
+
when Set
|
483
|
+
if size < set.size
|
484
|
+
any? { |o| set.include?(o) }
|
485
|
+
else
|
486
|
+
set.any? { |o| include?(o) }
|
487
|
+
end
|
488
|
+
when Enumerable
|
328
489
|
set.any? { |o| include?(o) }
|
490
|
+
else
|
491
|
+
raise ArgumentError, "value must be enumerable"
|
329
492
|
end
|
330
493
|
end
|
331
494
|
|
332
|
-
# Returns true if the set and the given
|
333
|
-
# common. This method is the opposite of
|
495
|
+
# Returns true if the set and the given enumerable have
|
496
|
+
# no element in common. This method is the opposite of `intersect?`.
|
334
497
|
#
|
335
|
-
#
|
336
|
-
#
|
498
|
+
# Set[1, 2, 3].disjoint? Set[3, 4] #=> false
|
499
|
+
# Set[1, 2, 3].disjoint? Set[4, 5] #=> true
|
500
|
+
# Set[1, 2, 3].disjoint? [3, 4] #=> false
|
501
|
+
# Set[1, 2, 3].disjoint? 4..5 #=> true
|
337
502
|
def disjoint?(set)
|
338
503
|
!intersect?(set)
|
339
504
|
end
|
@@ -347,7 +512,7 @@ class Set
|
|
347
512
|
self
|
348
513
|
end
|
349
514
|
|
350
|
-
# Adds the given object to the set and returns self. Use
|
515
|
+
# Adds the given object to the set and returns self. Use `merge` to
|
351
516
|
# add many elements at once.
|
352
517
|
#
|
353
518
|
# Set[1, 2].add(3) #=> #<Set: {1, 2, 3}>
|
@@ -369,8 +534,8 @@ class Set
|
|
369
534
|
add(o) unless include?(o)
|
370
535
|
end
|
371
536
|
|
372
|
-
# Deletes the given object from the set and returns self. Use
|
373
|
-
# delete many items at once.
|
537
|
+
# Deletes the given object from the set and returns self. Use
|
538
|
+
# `subtract` to delete many items at once.
|
374
539
|
def delete(o)
|
375
540
|
@hash.delete(o)
|
376
541
|
self
|
@@ -404,7 +569,7 @@ class Set
|
|
404
569
|
self
|
405
570
|
end
|
406
571
|
|
407
|
-
# Replaces the elements with ones returned by collect()
|
572
|
+
# Replaces the elements with ones returned by `collect()`.
|
408
573
|
# Returns an enumerator if no block is given.
|
409
574
|
def collect!
|
410
575
|
block_given? or return enum_for(__method__) { size }
|
@@ -496,8 +661,8 @@ class Set
|
|
496
661
|
alias intersection &
|
497
662
|
|
498
663
|
# Returns a new set containing elements exclusive between the set
|
499
|
-
# and the given enumerable object. (set ^ enum) is equivalent to
|
500
|
-
# ((set | enum) - (set & enum))
|
664
|
+
# and the given enumerable object. `(set ^ enum)` is equivalent to
|
665
|
+
# `((set | enum) - (set & enum))`.
|
501
666
|
#
|
502
667
|
# Set[1, 2] ^ Set[2, 3] #=> #<Set: {3, 1}>
|
503
668
|
# Set[1, 'b', 'c'] ^ ['b', 'd'] #=> #<Set: {"d", 1, "c"}>
|
@@ -553,20 +718,20 @@ class Set
|
|
553
718
|
#
|
554
719
|
# Used in case statements:
|
555
720
|
#
|
556
|
-
#
|
721
|
+
# require 'set'
|
557
722
|
#
|
558
|
-
#
|
559
|
-
#
|
560
|
-
#
|
561
|
-
#
|
562
|
-
#
|
563
|
-
#
|
564
|
-
#
|
723
|
+
# case :apple
|
724
|
+
# when Set[:potato, :carrot]
|
725
|
+
# "vegetable"
|
726
|
+
# when Set[:apple, :banana]
|
727
|
+
# "fruit"
|
728
|
+
# end
|
729
|
+
# # => "fruit"
|
565
730
|
#
|
566
731
|
# Or by itself:
|
567
732
|
#
|
568
|
-
#
|
569
|
-
#
|
733
|
+
# Set[1, 2, 3] === 2 #=> true
|
734
|
+
# Set[1, 2, 3] === 4 #=> false
|
570
735
|
#
|
571
736
|
alias === include?
|
572
737
|
|
@@ -575,12 +740,12 @@ class Set
|
|
575
740
|
# called once for each element of the set, passing the element as
|
576
741
|
# parameter.
|
577
742
|
#
|
578
|
-
#
|
579
|
-
#
|
580
|
-
#
|
581
|
-
#
|
582
|
-
#
|
583
|
-
#
|
743
|
+
# require 'set'
|
744
|
+
# files = Set.new(Dir.glob("*.rb"))
|
745
|
+
# hash = files.classify { |f| File.mtime(f).year }
|
746
|
+
# hash #=> {2000=>#<Set: {"a.rb", "b.rb"}>,
|
747
|
+
# # 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
|
748
|
+
# # 2002=>#<Set: {"f.rb"}>}
|
584
749
|
#
|
585
750
|
# Returns an enumerator if no block is given.
|
586
751
|
def classify # :yields: o
|
@@ -602,13 +767,13 @@ class Set
|
|
602
767
|
# if block.call(o1, o2) is true. Otherwise, elements o1 and o2 are
|
603
768
|
# in common if block.call(o1) == block.call(o2).
|
604
769
|
#
|
605
|
-
#
|
606
|
-
#
|
607
|
-
#
|
608
|
-
#
|
609
|
-
#
|
610
|
-
#
|
611
|
-
#
|
770
|
+
# require 'set'
|
771
|
+
# numbers = Set[1, 3, 4, 6, 9, 10, 11]
|
772
|
+
# set = numbers.divide { |i,j| (i - j).abs == 1 }
|
773
|
+
# set #=> #<Set: {#<Set: {1}>,
|
774
|
+
# # #<Set: {11, 9, 10}>,
|
775
|
+
# # #<Set: {3, 4}>,
|
776
|
+
# # #<Set: {6}>}>
|
612
777
|
#
|
613
778
|
# Returns an enumerator if no block is given.
|
614
779
|
def divide(&func)
|
@@ -669,13 +834,14 @@ class Set
|
|
669
834
|
alias to_s inspect
|
670
835
|
|
671
836
|
def pretty_print(pp) # :nodoc:
|
672
|
-
pp.
|
673
|
-
|
674
|
-
pp.
|
675
|
-
pp.
|
837
|
+
pp.group(1, sprintf('#<%s:', self.class.name), '>') {
|
838
|
+
pp.breakable
|
839
|
+
pp.group(1, '{', '}') {
|
840
|
+
pp.seplist(self) { |o|
|
841
|
+
pp.pp o
|
842
|
+
}
|
676
843
|
}
|
677
844
|
}
|
678
|
-
pp.text "}>"
|
679
845
|
end
|
680
846
|
|
681
847
|
def pretty_print_cycle(pp) # :nodoc:
|
@@ -685,10 +851,10 @@ end
|
|
685
851
|
|
686
852
|
module Enumerable
|
687
853
|
# Makes a set from the enumerable object with given arguments.
|
688
|
-
# Needs to
|
854
|
+
# Needs to `require "set"` to use this method.
|
689
855
|
def to_set(klass = Set, *args, &block)
|
690
856
|
klass.new(self, *args, &block)
|
691
|
-
end
|
857
|
+
end unless method_defined?(:to_set)
|
692
858
|
end
|
693
859
|
|
694
860
|
autoload :SortedSet, "#{__dir__}/set/sorted_set"
|
data/set.gemspec
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "set"
|
3
|
-
spec.version = "1.0.
|
3
|
+
spec.version = "1.0.3"
|
4
4
|
spec.authors = ["Akinori MUSHA"]
|
5
5
|
spec.email = ["knu@idaemons.org"]
|
6
6
|
|
7
7
|
spec.summary = %q{Provides a class to deal with collections of unordered, unique values}
|
8
8
|
spec.description = %q{Provides a class to deal with collections of unordered, unique values}
|
9
9
|
spec.homepage = "https://github.com/ruby/set"
|
10
|
-
spec.
|
10
|
+
spec.licenses = ["Ruby", "BSD-2-Clause"]
|
11
11
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
12
12
|
|
13
13
|
spec.metadata["homepage_uri"] = spec.homepage
|
@@ -19,7 +19,5 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
20
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
21
|
end
|
22
|
-
spec.bindir = "exe"
|
23
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
22
|
spec.require_paths = ["lib"]
|
25
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: set
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akinori MUSHA
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Provides a class to deal with collections of unordered, unique values
|
14
14
|
email:
|
@@ -17,6 +17,7 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- ".github/dependabot.yml"
|
20
21
|
- ".github/workflows/test.yml"
|
21
22
|
- ".gitignore"
|
22
23
|
- CHANGELOG.md
|
@@ -31,11 +32,12 @@ files:
|
|
31
32
|
- set.gemspec
|
32
33
|
homepage: https://github.com/ruby/set
|
33
34
|
licenses:
|
35
|
+
- Ruby
|
34
36
|
- BSD-2-Clause
|
35
37
|
metadata:
|
36
38
|
homepage_uri: https://github.com/ruby/set
|
37
39
|
source_code_uri: https://github.com/ruby/set
|
38
|
-
changelog_uri: https://github.com/ruby/set/blob/v1.0.
|
40
|
+
changelog_uri: https://github.com/ruby/set/blob/v1.0.3/CHANGELOG.md
|
39
41
|
post_install_message:
|
40
42
|
rdoc_options: []
|
41
43
|
require_paths:
|
@@ -51,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
53
|
- !ruby/object:Gem::Version
|
52
54
|
version: '0'
|
53
55
|
requirements: []
|
54
|
-
rubygems_version: 3.
|
56
|
+
rubygems_version: 3.4.0.dev
|
55
57
|
signing_key:
|
56
58
|
specification_version: 4
|
57
59
|
summary: Provides a class to deal with collections of unordered, unique values
|