Skip to content

Commit 7697761

Browse files
committed
Add Set#compare_by_identity and Set#compare_by_identity?
* lib/set.rb (Set#compare_by_identity, Set#compare_by_identity?): New methods. [Feature #12210] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56589 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent cb3b7fc commit 7697761

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

Diff for: ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Sat Nov 5 18:17:54 2016 Akinori MUSHA <knu@iDaemons.org>
2+
3+
* lib/set.rb (Set#compare_by_identity, Set#compare_by_identity?):
4+
New methods. [Feature #12210]
5+
16
Sat Nov 5 18:17:08 2016 SHIBATA Hiroshi <hsbt@ruby-lang.org>
27

38
* lib/rdoc/*, test/rdoc/*: Update rdoc-5.0.0

Diff for: NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ with all sufficient information, see the ChangeLog file or Redmine
189189
* pathname
190190
* New method: Pathname#empty? [Feature#12596]
191191

192+
* set
193+
* New methods: Set#compare_by_identity and Set#compare_by_identity?.
194+
[Feature #12210]
195+
192196
* WEBrick
193197

194198
* Don't allow , as a separator [Bug #12791]

Diff for: lib/set.rb

+20-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# set.rb - defines the Set class
55
#++
6-
# Copyright (c) 2002-2013 Akinori MUSHA <knu@iDaemons.org>
6+
# Copyright (c) 2002-2016 Akinori MUSHA <knu@iDaemons.org>
77
#
88
# Documentation by Akinori MUSHA and Gavin Sinclair.
99
#
@@ -37,7 +37,8 @@
3737
# Set uses Hash as storage, so you must note the following points:
3838
#
3939
# * Equality of elements is determined according to Object#eql? and
40-
# Object#hash.
40+
# Object#hash. Use Set#compare_by_identity to make a set compare
41+
# its elements by their identity.
4142
# * Set assumes that the identity of each element does not change
4243
# while it is stored. Modifying an element of a set will render the
4344
# set to an unreliable state.
@@ -91,6 +92,23 @@ def initialize(enum = nil, &block) # :yields: o
9192
end
9293
end
9394

95+
# Makes the set compare its elements by their identity and returns
96+
# self. This method may not be supported by all subclasses of Set.
97+
def compare_by_identity
98+
if @hash.respond_to?(:compare_by_identity)
99+
@hash.compare_by_identity
100+
self
101+
else
102+
raise NotImplementedError, "#{self.class.name}\##{__method__} is not implemented"
103+
end
104+
end
105+
106+
# Returns true if the set will compare its elements by their
107+
# identity. Also see Set#compare_by_identity.
108+
def compare_by_identity?
109+
@hash.respond_to?(:compare_by_identity?) && @hash.compare_by_identity?
110+
end
111+
94112
def do_with_enum(enum, &block) # :nodoc:
95113
if enum.respond_to?(:each_entry)
96114
enum.each_entry(&block) if block

Diff for: test/test_set.rb

+19
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,25 @@ def test_inspect
715715
set1.add(set2)
716716
assert_equal(true, set1.inspect.include?('#<Set: {...}>'))
717717
end
718+
719+
def test_compare_by_identity
720+
a1, a2 = "a", "a"
721+
b1, b2 = "b", "b"
722+
c = "c"
723+
array = [a1, b1, c, a2, b2]
724+
725+
iset = Set.new.compare_by_identity
726+
assert_send([iset, :compare_by_identity?])
727+
iset.merge(array)
728+
assert_equal(5, iset.size)
729+
assert_equal(array.map(&:object_id).sort, iset.map(&:object_id).sort)
730+
731+
set = Set.new
732+
assert_not_send([set, :compare_by_identity?])
733+
set.merge(array)
734+
assert_equal(3, set.size)
735+
assert_equal(array.uniq.sort, set.sort)
736+
end
718737
end
719738

720739
class TC_SortedSet < Test::Unit::TestCase

0 commit comments

Comments
 (0)