Skip to content

Commit

Permalink
Change the API to use formal naming instead of operator overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Jan 18, 2021
1 parent 0b14a48 commit 81dc116
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
48 changes: 24 additions & 24 deletions spec/std/hash_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -105,43 +105,43 @@ describe "Hash" do
h3 = {"c" => 3}
h4 = {} of Nil => Nil

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

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

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

describe ">=" do
describe "#superset?" do
it do
(h1 >= h2).should be_false
(h2 >= h1).should be_true
(h1 >= h1).should be_true
(h1 >= h3).should be_false
(h1 >= h4).should be_true
h1.superset?(h2).should be_false
h2.superset?(h1).should be_true
h1.superset?(h1).should be_true
h1.superset?(h3).should be_false
h1.superset?(h4).should be_true
end
end
end
Expand Down
22 changes: 10 additions & 12 deletions src/hash.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1748,27 +1748,25 @@ class Hash(K, V)
end

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

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

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

# Returns `true` if *other* is a subset of `self` or equals to `self`.
def >=(other : Hash)
other <= self
def proper_superset?(other : Hash)
other.proper_subset?(self)
end

# See `Object#hash(hasher)`
Expand Down

0 comments on commit 81dc116

Please sign in to comment.