Skip to content

Commit e9ef2c4

Browse files
committed
🚧✨ Add `SequenceSet#xor!"
This is simply a mutating version of `^` or `xor`. TODO: * rdoc on method * rdoc in class "What's Here"
1 parent f8cef2a commit e9ef2c4

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

‎benchmarks/sequence_set-and.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ prelude: |
3333
subtract dup.subtract(SequenceSet.new(other))
3434
end
3535
36-
# TODO: add this as a public method
37-
def xor!(other) # :nodoc:
38-
modifying!
39-
copy = dup
40-
other = SequenceSet.new(other)
41-
merge(other).subtract(other.subtract(copy.complement!))
42-
end
43-
4436
# L - (L ^ R)
4537
def and2!(other)
4638
modifying!

‎benchmarks/sequence_set-xor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ prelude: |
6666
6767
benchmark:
6868
" L ^ R": l, r = sets; l ^ r
69+
" L.xor! R": l, r = sets; l.xor! r
6970
" (L | R) - (R & L)": l, r = sets; (l | r) - (r & l)
7071
"0.5.8 (L | R) - (R & L)": l, r = sets; l.xor0 r
7172
"dup1 (L | R) - (R & L)": l, r = sets; l.xor1 r

‎lib/net/imap/sequence_set.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ def &(other) remain_frozen dup.intersect! other end
743743
# * <tt>(lhs | rhs) - (lhs & rhs)</tt>
744744
# * <tt>(lhs - rhs) | (rhs - lhs)</tt>
745745
# * <tt>(lhs ^ other) ^ (other ^ rhs)</tt>
746-
def ^(other) remain_frozen (dup | other).subtract(self & other) end
746+
def ^(other) remain_frozen dup.xor! other end
747747
alias xor :^
748748

749749
# :call-seq:
@@ -1472,6 +1472,13 @@ def intersect!(other) # :nodoc:
14721472
subtract SequenceSet.new(other).complement!
14731473
end
14741474

1475+
# TODO: document and directly test this
1476+
def xor!(other) # :nodoc:
1477+
modifying!
1478+
both = self & other
1479+
merge(other).subtract(both)
1480+
end
1481+
14751482
# Returns a new SequenceSet with a normalized string representation.
14761483
#
14771484
# The returned set's #string is sorted and deduplicated. Adjacent or

‎test/net/imap/test_sequence_set.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def compare_to_reference_set(nums, set, seqset)
104104
assert_equal xor, (lhs | rhs) - (lhs & rhs)
105105
assert_equal xor, (lhs ^ mid) ^ (mid ^ rhs)
106106
assert_equal xor, ~lhs ^ ~rhs
107+
mutable = lhs.dup
108+
assert_equal xor, mutable.xor!(rhs)
109+
assert_equal xor, mutable
107110
end
108111
end
109112

@@ -155,6 +158,7 @@ def compare_to_reference_set(nums, set, seqset)
155158
data "#subtract", ->{ _1.subtract 1 }
156159
data "#limit!", ->{ _1.limit! max: 10 }
157160
data "#intersect!", ->{ _1.intersect! SequenceSet[1..100] }
161+
data "#xor!", ->{ _1.xor! SequenceSet[9..98] }
158162
data "#complement!", :complement!
159163
data "#normalize!", :normalize!
160164
test "frozen error message" do |modification|

0 commit comments

Comments
 (0)