mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
datatypes: use generic op overloading for difference
and equal
(#15530)
This commit is contained in:
parent
c10c8ff9e4
commit
44137e07b0
@ -21,7 +21,7 @@ pub fn (mut set Set<T>) remove(element T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// pick returns an arbitrary element of set, if set is not empty.
|
// pick returns an arbitrary element of set, if set is not empty.
|
||||||
pub fn (mut set Set<T>) pick() ?T {
|
pub fn (set Set<T>) pick() ?T {
|
||||||
for k, _ in set.elements {
|
for k, _ in set.elements {
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
@ -46,8 +46,14 @@ pub fn (mut set Set<T>) clear() {
|
|||||||
set.elements = map[T]u8{}
|
set.elements = map[T]u8{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks whether the two given sets are equal (i.e. contain all and only the same elements).
|
// equal checks whether the two given sets are equal (i.e. contain all and only the same elements).
|
||||||
pub fn (mut l Set<T>) equal(r Set<T>) bool {
|
[deprecated: 'use set1<T> == set2<T> instead']
|
||||||
|
pub fn (l Set<T>) equal(r Set<T>) 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<T>) == (r Set<T>) bool {
|
||||||
if l.elements.len != r.elements.len {
|
if l.elements.len != r.elements.len {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -59,18 +65,18 @@ pub fn (mut l Set<T>) equal(r Set<T>) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks whether the set is empty.
|
// is_empty checks whether the set is empty or not.
|
||||||
pub fn (mut set Set<T>) is_empty() bool {
|
pub fn (set Set<T>) is_empty() bool {
|
||||||
return set.size() == 0
|
return set.size() == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// size returns the number of elements in the set.
|
// size returns the number of elements in the set.
|
||||||
pub fn (mut set Set<T>) size() int {
|
pub fn (set Set<T>) size() int {
|
||||||
return set.elements.len
|
return set.elements.len
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy returns a copy of all the elements in the set.
|
// copy returns a copy of all the elements in the set.
|
||||||
pub fn (mut set Set<T>) copy() Set<T> {
|
pub fn (set Set<T>) copy() Set<T> {
|
||||||
return Set<T>{
|
return Set<T>{
|
||||||
elements: set.elements.clone()
|
elements: set.elements.clone()
|
||||||
}
|
}
|
||||||
@ -84,7 +90,7 @@ pub fn (mut set Set<T>) add_all(elements []T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// @union returns the union of the two sets.
|
// @union returns the union of the two sets.
|
||||||
pub fn (mut l Set<T>) @union(r Set<T>) Set<T> {
|
pub fn (l Set<T>) @union(r Set<T>) Set<T> {
|
||||||
mut set := l
|
mut set := l
|
||||||
for e, _ in r.elements {
|
for e, _ in r.elements {
|
||||||
set.add(e)
|
set.add(e)
|
||||||
@ -93,7 +99,7 @@ pub fn (mut l Set<T>) @union(r Set<T>) Set<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// intersection returns the intersection of sets.
|
// intersection returns the intersection of sets.
|
||||||
pub fn (mut l Set<T>) intersection(r Set<T>) Set<T> {
|
pub fn (l Set<T>) intersection(r Set<T>) Set<T> {
|
||||||
mut set := l
|
mut set := l
|
||||||
for e, _ in l.elements {
|
for e, _ in l.elements {
|
||||||
if !r.exists(e) {
|
if !r.exists(e) {
|
||||||
@ -109,7 +115,13 @@ pub fn (mut l Set<T>) intersection(r Set<T>) Set<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// difference returns the difference of sets.
|
// difference returns the difference of sets.
|
||||||
pub fn (mut l Set<T>) difference(r Set<T>) Set<T> {
|
[deprecated: 'use set1<T> - set2<T> instead']
|
||||||
|
pub fn (l Set<T>) difference(r Set<T>) Set<T> {
|
||||||
|
return l - r
|
||||||
|
}
|
||||||
|
|
||||||
|
// - returns the difference of sets.
|
||||||
|
pub fn (l Set<T>) - (r Set<T>) Set<T> {
|
||||||
mut set := l
|
mut set := l
|
||||||
for e, _ in l.elements {
|
for e, _ in l.elements {
|
||||||
if r.exists(e) {
|
if r.exists(e) {
|
||||||
@ -120,7 +132,7 @@ pub fn (mut l Set<T>) difference(r Set<T>) Set<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// subset returns true if the set `r` is a subset of the set `l`.
|
// subset returns true if the set `r` is a subset of the set `l`.
|
||||||
pub fn (mut l Set<T>) subset(r Set<T>) bool {
|
pub fn (l Set<T>) subset(r Set<T>) bool {
|
||||||
for e, _ in r.elements {
|
for e, _ in r.elements {
|
||||||
if e !in l.elements {
|
if e !in l.elements {
|
||||||
return false
|
return false
|
||||||
|
@ -49,9 +49,9 @@ fn test_equal() {
|
|||||||
mut first_set := Set<string>{}
|
mut first_set := Set<string>{}
|
||||||
mut second_set := Set<string>{}
|
mut second_set := Set<string>{}
|
||||||
first_set.add('foo')
|
first_set.add('foo')
|
||||||
assert second_set.equal(first_set) == false
|
assert second_set != first_set
|
||||||
second_set.add('foo')
|
second_set.add('foo')
|
||||||
assert second_set.equal(first_set)
|
assert second_set == first_set
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_is_empty() {
|
fn test_is_empty() {
|
||||||
@ -91,7 +91,7 @@ fn test_difference() {
|
|||||||
mut second_set := Set<string>{}
|
mut second_set := Set<string>{}
|
||||||
first_set.add_all(['foo', 'bar', 'baz'])
|
first_set.add_all(['foo', 'bar', 'baz'])
|
||||||
second_set.add_all(['bar', 'baz', 'boo'])
|
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('foo')
|
||||||
assert third_set.exists('bar') == false
|
assert third_set.exists('bar') == false
|
||||||
assert third_set.exists('baz') == false
|
assert third_set.exists('baz') == false
|
||||||
@ -101,7 +101,7 @@ fn test_difference() {
|
|||||||
third_set.clear()
|
third_set.clear()
|
||||||
first_set.add_all(['bar', 'baz', 'boo'])
|
first_set.add_all(['bar', 'baz', 'boo'])
|
||||||
second_set.add_all(['foo', 'bar', 'baz'])
|
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('foo') == false
|
||||||
assert third_set.exists('bar') == false
|
assert third_set.exists('bar') == false
|
||||||
assert third_set.exists('baz') == false
|
assert third_set.exists('baz') == false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user