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

Add sub/superset checking methods to Hash #7500

Merged
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
55 changes: 55 additions & 0 deletions spec/std/hash_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,61 @@ describe "Hash" do
end
end

context "subset/superset operators" do
h1 = {"a" => 1, "b" => 2}
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
h2 = {"a" => 1, "b" => 2, "c" => 3}
h3 = {"c" => 3}
h4 = {} of Nil => Nil

describe "#proper_subset_of?" do
it do
h1.proper_subset_of?(h2).should be_true
h2.proper_subset_of?(h1).should be_false
h1.proper_subset_of?(h1).should be_false
h1.proper_subset_of?(h3).should be_false
h1.proper_subset_of?(h4).should be_false
end

it "handles edge case where both values are nil" do
{"a" => nil}.proper_subset_of?({"b" => nil, "c" => nil}).should be_false
end
end

describe "#subset_of?" do
it do
h1.subset_of?(h2).should be_true
h2.subset_of?(h1).should be_false
h1.subset_of?(h1).should be_true
h1.subset_of?(h3).should be_false
h1.subset_of?(h4).should be_false
end

it "handles edge case where both values are nil" do
{"a" => nil}.subset_of?({"b" => nil}).should be_false
end
end

describe "#proper_superset_of?" do
it do
h1.proper_superset_of?(h2).should be_false
h2.proper_superset_of?(h1).should be_true
h1.proper_superset_of?(h1).should be_false
h1.proper_superset_of?(h3).should be_false
h1.proper_superset_of?(h4).should be_true
end
end

describe "#superset_of?" do
it do
h1.superset_of?(h2).should be_false
h2.superset_of?(h1).should be_true
h1.superset_of?(h1).should be_true
h1.superset_of?(h3).should be_false
h1.superset_of?(h4).should be_true
end
end
end

describe "[]" do
it "gets" do
a = {1 => 2}
Expand Down
28 changes: 28 additions & 0 deletions src/hash.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,34 @@ class Hash(K, V)
true
end

# Returns `true` if `self` is a subset of *other*.
def proper_subset_of?(other : Hash)
return false if other.size <= size
all? do |key, value|
other_value = other.fetch(key) { return false }
other_value == value
end
end

# Returns `true` if `self` is a subset of *other* or equals to *other*.
def subset_of?(other : Hash)
return false if other.size < size
all? do |key, value|
other_value = other.fetch(key) { return false }
other_value == value
end
end

# Returns `true` if *other* is a subset of `self`.
def superset_of?(other : Hash)
other.subset_of?(self)
end

# Returns `true` if *other* is a subset of `self` or equals to `self`.
def proper_superset_of?(other : Hash)
other.proper_subset_of?(self)
end

# See `Object#hash(hasher)`
def hash(hasher)
# The hash value must be the same regardless of the
Expand Down