set 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -1
- data/README.md +33 -4
- data/lib/set.rb +64 -66
- data/set.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 710e7b1fc1bf38f1d6f54154861953313835891a7b8f30ee52e7663c914f34c7
|
4
|
+
data.tar.gz: 7f3ecde9cc9456dea795c5977821a8634fcc5e58ddc59ab074675305c8b81abc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ae9bb8e072bd98d7aa2e3fd9baae6f57e49c63e3ccf68705b13a33554c5ac2a5c9544f926f1598c4219f677727b95dd1598f803a4e8f67402c28fa14340cb90
|
7
|
+
data.tar.gz: 250dafb092c99682f9b5437bbbf2abeeaf6a9e91f3ce6a789f21d92d2f03bd2fa40e47dfe7761ab9dc779cee84b2357c08b8f309ec203e9340c842bee4f9facc
|
data/CHANGELOG.md
CHANGED
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.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,29 @@
|
|
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
|
46
42
|
#
|
47
|
-
# The comparison operators
|
48
|
-
# shorthand for the {proper_,}{subset?,superset?} methods.
|
49
|
-
#
|
50
|
-
#
|
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).
|
51
47
|
#
|
52
|
-
#
|
48
|
+
# ## Example
|
53
49
|
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
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
|
+
# ```
|
62
60
|
#
|
63
|
-
#
|
61
|
+
# ## Contact
|
64
62
|
#
|
65
|
-
#
|
63
|
+
# - Akinori MUSHA <<knu@iDaemons.org>> (current maintainer)
|
66
64
|
#
|
67
65
|
class Set
|
68
66
|
include Enumerable
|
@@ -199,9 +197,9 @@ class Set
|
|
199
197
|
end
|
200
198
|
|
201
199
|
# Returns self if no arguments are given. Otherwise, converts the
|
202
|
-
# set to another with klass.new(self, *args, &block)
|
200
|
+
# set to another with `klass.new(self, *args, &block)`.
|
203
201
|
#
|
204
|
-
# In subclasses, returns klass.new(self, *args, &block) unless
|
202
|
+
# In subclasses, returns `klass.new(self, *args, &block)` unless
|
205
203
|
# overridden.
|
206
204
|
def to_set(klass = Set, *args, &block)
|
207
205
|
return self if instance_of?(Set) && klass == Set && block.nil? && args.empty?
|
@@ -318,8 +316,8 @@ class Set
|
|
318
316
|
# Returns true if the set and the given set have at least one
|
319
317
|
# element in common.
|
320
318
|
#
|
321
|
-
#
|
322
|
-
#
|
319
|
+
# Set[1, 2, 3].intersect? Set[4, 5] #=> false
|
320
|
+
# Set[1, 2, 3].intersect? Set[3, 4] #=> true
|
323
321
|
def intersect?(set)
|
324
322
|
set.is_a?(Set) or raise ArgumentError, "value must be a set"
|
325
323
|
if size < set.size
|
@@ -330,10 +328,10 @@ class Set
|
|
330
328
|
end
|
331
329
|
|
332
330
|
# Returns true if the set and the given set have no element in
|
333
|
-
# common. This method is the opposite of
|
331
|
+
# common. This method is the opposite of `intersect?`.
|
334
332
|
#
|
335
|
-
#
|
336
|
-
#
|
333
|
+
# Set[1, 2, 3].disjoint? Set[3, 4] #=> false
|
334
|
+
# Set[1, 2, 3].disjoint? Set[4, 5] #=> true
|
337
335
|
def disjoint?(set)
|
338
336
|
!intersect?(set)
|
339
337
|
end
|
@@ -347,7 +345,7 @@ class Set
|
|
347
345
|
self
|
348
346
|
end
|
349
347
|
|
350
|
-
# Adds the given object to the set and returns self. Use
|
348
|
+
# Adds the given object to the set and returns self. Use `merge` to
|
351
349
|
# add many elements at once.
|
352
350
|
#
|
353
351
|
# Set[1, 2].add(3) #=> #<Set: {1, 2, 3}>
|
@@ -369,8 +367,8 @@ class Set
|
|
369
367
|
add(o) unless include?(o)
|
370
368
|
end
|
371
369
|
|
372
|
-
# Deletes the given object from the set and returns self. Use
|
373
|
-
# delete many items at once.
|
370
|
+
# Deletes the given object from the set and returns self. Use
|
371
|
+
# `subtract` to delete many items at once.
|
374
372
|
def delete(o)
|
375
373
|
@hash.delete(o)
|
376
374
|
self
|
@@ -404,7 +402,7 @@ class Set
|
|
404
402
|
self
|
405
403
|
end
|
406
404
|
|
407
|
-
# Replaces the elements with ones returned by collect()
|
405
|
+
# Replaces the elements with ones returned by `collect()`.
|
408
406
|
# Returns an enumerator if no block is given.
|
409
407
|
def collect!
|
410
408
|
block_given? or return enum_for(__method__) { size }
|
@@ -496,8 +494,8 @@ class Set
|
|
496
494
|
alias intersection &
|
497
495
|
|
498
496
|
# 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))
|
497
|
+
# and the given enumerable object. `(set ^ enum)` is equivalent to
|
498
|
+
# `((set | enum) - (set & enum))`.
|
501
499
|
#
|
502
500
|
# Set[1, 2] ^ Set[2, 3] #=> #<Set: {3, 1}>
|
503
501
|
# Set[1, 'b', 'c'] ^ ['b', 'd'] #=> #<Set: {"d", 1, "c"}>
|
@@ -553,20 +551,20 @@ class Set
|
|
553
551
|
#
|
554
552
|
# Used in case statements:
|
555
553
|
#
|
556
|
-
#
|
554
|
+
# require 'set'
|
557
555
|
#
|
558
|
-
#
|
559
|
-
#
|
560
|
-
#
|
561
|
-
#
|
562
|
-
#
|
563
|
-
#
|
564
|
-
#
|
556
|
+
# case :apple
|
557
|
+
# when Set[:potato, :carrot]
|
558
|
+
# "vegetable"
|
559
|
+
# when Set[:apple, :banana]
|
560
|
+
# "fruit"
|
561
|
+
# end
|
562
|
+
# # => "fruit"
|
565
563
|
#
|
566
564
|
# Or by itself:
|
567
565
|
#
|
568
|
-
#
|
569
|
-
#
|
566
|
+
# Set[1, 2, 3] === 2 #=> true
|
567
|
+
# Set[1, 2, 3] === 4 #=> false
|
570
568
|
#
|
571
569
|
alias === include?
|
572
570
|
|
@@ -575,12 +573,12 @@ class Set
|
|
575
573
|
# called once for each element of the set, passing the element as
|
576
574
|
# parameter.
|
577
575
|
#
|
578
|
-
#
|
579
|
-
#
|
580
|
-
#
|
581
|
-
#
|
582
|
-
#
|
583
|
-
#
|
576
|
+
# require 'set'
|
577
|
+
# files = Set.new(Dir.glob("*.rb"))
|
578
|
+
# hash = files.classify { |f| File.mtime(f).year }
|
579
|
+
# hash #=> {2000=>#<Set: {"a.rb", "b.rb"}>,
|
580
|
+
# # 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
|
581
|
+
# # 2002=>#<Set: {"f.rb"}>}
|
584
582
|
#
|
585
583
|
# Returns an enumerator if no block is given.
|
586
584
|
def classify # :yields: o
|
@@ -602,13 +600,13 @@ class Set
|
|
602
600
|
# if block.call(o1, o2) is true. Otherwise, elements o1 and o2 are
|
603
601
|
# in common if block.call(o1) == block.call(o2).
|
604
602
|
#
|
605
|
-
#
|
606
|
-
#
|
607
|
-
#
|
608
|
-
#
|
609
|
-
#
|
610
|
-
#
|
611
|
-
#
|
603
|
+
# require 'set'
|
604
|
+
# numbers = Set[1, 3, 4, 6, 9, 10, 11]
|
605
|
+
# set = numbers.divide { |i,j| (i - j).abs == 1 }
|
606
|
+
# set #=> #<Set: {#<Set: {1}>,
|
607
|
+
# # #<Set: {11, 9, 10}>,
|
608
|
+
# # #<Set: {3, 4}>,
|
609
|
+
# # #<Set: {6}>}>
|
612
610
|
#
|
613
611
|
# Returns an enumerator if no block is given.
|
614
612
|
def divide(&func)
|
@@ -685,7 +683,7 @@ end
|
|
685
683
|
|
686
684
|
module Enumerable
|
687
685
|
# Makes a set from the enumerable object with given arguments.
|
688
|
-
# Needs to
|
686
|
+
# Needs to `require "set"` to use this method.
|
689
687
|
def to_set(klass = Set, *args, &block)
|
690
688
|
klass.new(self, *args, &block)
|
691
689
|
end
|
data/set.gemspec
CHANGED
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akinori MUSHA
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-12-
|
11
|
+
date: 2020-12-22 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:
|
@@ -35,7 +35,7 @@ licenses:
|
|
35
35
|
metadata:
|
36
36
|
homepage_uri: https://github.com/ruby/set
|
37
37
|
source_code_uri: https://github.com/ruby/set
|
38
|
-
changelog_uri: https://github.com/ruby/set/blob/v1.0.
|
38
|
+
changelog_uri: https://github.com/ruby/set/blob/v1.0.1/CHANGELOG.md
|
39
39
|
post_install_message:
|
40
40
|
rdoc_options: []
|
41
41
|
require_paths:
|