From 44137e07b0f02a5b1af5149bca8912cbfad2d21f Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Fri, 26 Aug 2022 09:33:23 +0530 Subject: [PATCH] datatypes: use generic op overloading for `difference` and `equal` (#15530) --- vlib/datatypes/set.v | 34 +++++++++++++++++++++++----------- vlib/datatypes/set_test.v | 8 ++++---- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/vlib/datatypes/set.v b/vlib/datatypes/set.v index 3286ba2722..fecf4c799e 100644 --- a/vlib/datatypes/set.v +++ b/vlib/datatypes/set.v @@ -21,7 +21,7 @@ pub fn (mut set Set) remove(element T) { } // pick returns an arbitrary element of set, if set is not empty. -pub fn (mut set Set) pick() ?T { +pub fn (set Set) pick() ?T { for k, _ in set.elements { return k } @@ -46,8 +46,14 @@ pub fn (mut set Set) clear() { set.elements = map[T]u8{} } -// checks whether the two given sets are equal (i.e. contain all and only the same elements). -pub fn (mut l Set) equal(r Set) bool { +// equal checks whether the two given sets are equal (i.e. contain all and only the same elements). +[deprecated: 'use set1 == set2 instead'] +pub fn (l Set) equal(r Set) bool { + return l == r +} + +// == checks whether the two given sets are equal (i.e. contain all and only the same elements). +pub fn (l Set) == (r Set) bool { if l.elements.len != r.elements.len { return false } @@ -59,18 +65,18 @@ pub fn (mut l Set) equal(r Set) bool { return true } -// checks whether the set is empty. -pub fn (mut set Set) is_empty() bool { +// is_empty checks whether the set is empty or not. +pub fn (set Set) is_empty() bool { return set.size() == 0 } // size returns the number of elements in the set. -pub fn (mut set Set) size() int { +pub fn (set Set) size() int { return set.elements.len } // copy returns a copy of all the elements in the set. -pub fn (mut set Set) copy() Set { +pub fn (set Set) copy() Set { return Set{ elements: set.elements.clone() } @@ -84,7 +90,7 @@ pub fn (mut set Set) add_all(elements []T) { } // @union returns the union of the two sets. -pub fn (mut l Set) @union(r Set) Set { +pub fn (l Set) @union(r Set) Set { mut set := l for e, _ in r.elements { set.add(e) @@ -93,7 +99,7 @@ pub fn (mut l Set) @union(r Set) Set { } // intersection returns the intersection of sets. -pub fn (mut l Set) intersection(r Set) Set { +pub fn (l Set) intersection(r Set) Set { mut set := l for e, _ in l.elements { if !r.exists(e) { @@ -109,7 +115,13 @@ pub fn (mut l Set) intersection(r Set) Set { } // difference returns the difference of sets. -pub fn (mut l Set) difference(r Set) Set { +[deprecated: 'use set1 - set2 instead'] +pub fn (l Set) difference(r Set) Set { + return l - r +} + +// - returns the difference of sets. +pub fn (l Set) - (r Set) Set { mut set := l for e, _ in l.elements { if r.exists(e) { @@ -120,7 +132,7 @@ pub fn (mut l Set) difference(r Set) Set { } // subset returns true if the set `r` is a subset of the set `l`. -pub fn (mut l Set) subset(r Set) bool { +pub fn (l Set) subset(r Set) bool { for e, _ in r.elements { if e !in l.elements { return false diff --git a/vlib/datatypes/set_test.v b/vlib/datatypes/set_test.v index eef8e1d890..97e0e9ce7c 100644 --- a/vlib/datatypes/set_test.v +++ b/vlib/datatypes/set_test.v @@ -49,9 +49,9 @@ fn test_equal() { mut first_set := Set{} mut second_set := Set{} first_set.add('foo') - assert second_set.equal(first_set) == false + assert second_set != first_set second_set.add('foo') - assert second_set.equal(first_set) + assert second_set == first_set } fn test_is_empty() { @@ -91,7 +91,7 @@ fn test_difference() { mut second_set := Set{} first_set.add_all(['foo', 'bar', 'baz']) second_set.add_all(['bar', 'baz', 'boo']) - mut third_set := first_set.difference(second_set) + mut third_set := first_set - second_set assert third_set.exists('foo') assert third_set.exists('bar') == false assert third_set.exists('baz') == false @@ -101,7 +101,7 @@ fn test_difference() { third_set.clear() first_set.add_all(['bar', 'baz', 'boo']) second_set.add_all(['foo', 'bar', 'baz']) - third_set = first_set.difference(second_set) + third_set = first_set - second_set assert third_set.exists('foo') == false assert third_set.exists('bar') == false assert third_set.exists('baz') == false