Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Set#{sub,super}set? to {sub,super}set_of? #10187

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions spec/std/set_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,20 @@ describe "Set" do
empty_set.subset?(empty_set).should be_true
end

it "#subset_of?" do
set = Set{1, 2, 3}
empty_set = Set(Int32).new

set.subset_of?(Set{1, 2, 3, 4}).should be_true
set.subset_of?(Set{1, 2, 3, "4"}).should be_true
set.subset_of?(Set{1, 2, 3}).should be_true
set.subset_of?(Set{1, 2}).should be_false
set.subset_of?(empty_set).should be_false

empty_set.subset_of?(Set{1}).should be_true
empty_set.subset_of?(empty_set).should be_true
end

it "check proper_subset" do
set = Set{1, 2, 3}
empty_set = Set(Int32).new
Expand All @@ -368,6 +382,20 @@ describe "Set" do
empty_set.proper_subset?(empty_set).should be_false
end

it "#proper_subset_of?" do
set = Set{1, 2, 3}
empty_set = Set(Int32).new

set.proper_subset_of?(Set{1, 2, 3, 4}).should be_true
set.proper_subset_of?(Set{1, 2, 3, "4"}).should be_true
set.proper_subset_of?(Set{1, 2, 3}).should be_false
set.proper_subset_of?(Set{1, 2}).should be_false
set.proper_subset_of?(empty_set).should be_false

empty_set.proper_subset_of?(Set{1}).should be_true
empty_set.proper_subset_of?(empty_set).should be_false
end

it "check superset" do
set = Set{1, 2, "3"}
empty_set = Set(Int32).new
Expand All @@ -382,6 +410,20 @@ describe "Set" do
empty_set.superset?(empty_set).should be_true
end

it "#superset_of?" do
set = Set{1, 2, "3"}
empty_set = Set(Int32).new

set.superset_of?(empty_set).should be_true
set.superset_of?(Set{1, 2}).should be_true
set.superset_of?(Set{1, 2, "3"}).should be_true
set.superset_of?(Set{1, 2, 3}).should be_false
set.superset_of?(Set{1, 2, 3, 4}).should be_false
set.superset_of?(Set{1, 4}).should be_false

empty_set.superset_of?(empty_set).should be_true
end

it "check proper_superset" do
set = Set{1, 2, "3"}
empty_set = Set(Int32).new
Expand All @@ -396,6 +438,20 @@ describe "Set" do
empty_set.proper_superset?(empty_set).should be_false
end

it "#proper_superset_of?" do
set = Set{1, 2, "3"}
empty_set = Set(Int32).new

set.proper_superset_of?(empty_set).should be_true
set.proper_superset_of?(Set{1, 2}).should be_true
set.proper_superset_of?(Set{1, 2, "3"}).should be_false
set.proper_superset_of?(Set{1, 2, 3}).should be_false
set.proper_superset_of?(Set{1, 2, 3, 4}).should be_false
set.proper_superset_of?(Set{1, 4}).should be_false

empty_set.proper_superset_of?(empty_set).should be_false
end

it "has object_id" do
Set(Int32).new.object_id.should be > 0
end
Expand Down
32 changes: 26 additions & 6 deletions src/set.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# s1 == s3 # => true
# s1.add(2)
# s1.concat([6, 8])
# s1.subset? s2 # => false
# s2.subset? s1 # => true
# s1.subset_of? s2 # => false
# s2.subset_of? s1 # => true
# ```
struct Set(T)
include Enumerable(T)
Expand Down Expand Up @@ -418,11 +418,16 @@ struct Set(T)
# Set{1, 5}.subset? Set{1, 3, 5} # => true
# Set{1, 3, 5}.subset? Set{1, 3, 5} # => true
# ```
def subset?(other : Set)
def subset_of?(other : Set)
return false if other.size < size
all? { |value| other.includes?(value) }
end

@[Deprecated("Use #subset_of? instead.")]
def subset?(other : Set)
subset_of?(other)
end

# Returns `true` if the set is a proper subset of the *other* set.
#
# This set must have fewer elements than the *other* set, and all
Expand All @@ -432,11 +437,16 @@ struct Set(T)
# Set{1, 5}.proper_subset? Set{1, 3, 5} # => true
# Set{1, 3, 5}.proper_subset? Set{1, 3, 5} # => false
# ```
def proper_subset?(other : Set)
def proper_subset_of?(other : Set)
return false if other.size <= size
all? { |value| other.includes?(value) }
end

@[Deprecated("Use #proper_subset_of? instead.")]
def proper_subset?(other : Set)
proper_subset_of?(other)
end

# Returns `true` if the set is a superset of the *other* set.
#
# The *other* must have the same or fewer elements than this set, and all of
Expand All @@ -446,8 +456,13 @@ struct Set(T)
# Set{1, 3, 5}.superset? Set{1, 5} # => true
# Set{1, 3, 5}.superset? Set{1, 3, 5} # => true
# ```
def superset_of?(other : Set)
other.subset_of?(self)
end

@[Deprecated("Use #superset_of? instead.")]
def superset?(other : Set)
other.subset?(self)
superset_of?(other)
end

# Returns `true` if the set is a superset of the *other* set.
Expand All @@ -459,8 +474,13 @@ struct Set(T)
# Set{1, 3, 5}.proper_superset? Set{1, 5} # => true
# Set{1, 3, 5}.proper_superset? Set{1, 3, 5} # => false
# ```
def proper_superset_of?(other : Set)
other.proper_subset_of?(self)
end

@[Deprecated("Use #proper_superset_of? instead.")]
def proper_superset?(other : Set)
other.proper_subset?(self)
proper_superset_of?(other)
end

# :nodoc:
Expand Down