mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
datatypes: make Set's methods public (#15489)
This commit is contained in:
parent
661d70a976
commit
6d501b073a
@ -6,22 +6,22 @@ mut:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checks the element is exists.
|
// checks the element is exists.
|
||||||
fn (set Set<T>) exists(element T) bool {
|
pub fn (set Set<T>) exists(element T) bool {
|
||||||
return element in set.elements
|
return element in set.elements
|
||||||
}
|
}
|
||||||
|
|
||||||
// adds the element to set, if it is not present already.
|
// adds the element to set, if it is not present already.
|
||||||
fn (mut set Set<T>) add(element T) {
|
pub fn (mut set Set<T>) add(element T) {
|
||||||
set.elements[element] = 1
|
set.elements[element] = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// removes the element from set.
|
// removes the element from set.
|
||||||
fn (mut set Set<T>) remove(element T) {
|
pub fn (mut set Set<T>) remove(element T) {
|
||||||
set.elements.delete(element)
|
set.elements.delete(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pick returns an arbitrary element of set, if set is not empty.
|
// pick returns an arbitrary element of set, if set is not empty.
|
||||||
fn (mut set Set<T>) pick() ?T {
|
pub fn (mut set Set<T>) pick() ?T {
|
||||||
for k, _ in set.elements {
|
for k, _ in set.elements {
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
@ -29,25 +29,25 @@ fn (mut set Set<T>) pick() ?T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// rest returns the set consisting of all elements except for the arbitrary element.
|
// rest returns the set consisting of all elements except for the arbitrary element.
|
||||||
fn (mut set Set<T>) rest() ?[]T {
|
pub fn (mut set Set<T>) rest() ?[]T {
|
||||||
element := set.pick()?
|
element := set.pick()?
|
||||||
return set.elements.keys().filter(it != element)
|
return set.elements.keys().filter(it != element)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pop returns an arbitrary element and deleting it from set.
|
// pop returns an arbitrary element and deleting it from set.
|
||||||
fn (mut set Set<T>) pop() ?T {
|
pub fn (mut set Set<T>) pop() ?T {
|
||||||
element := set.pick()?
|
element := set.pick()?
|
||||||
set.elements.delete(element)
|
set.elements.delete(element)
|
||||||
return element
|
return element
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete all elements of set.
|
// delete all elements of set.
|
||||||
fn (mut set Set<T>) clear() {
|
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).
|
// checks whether the two given sets are equal (i.e. contain all and only the same elements).
|
||||||
fn (mut l Set<T>) equal(r Set<T>) bool {
|
pub fn (mut l Set<T>) equal(r Set<T>) bool {
|
||||||
if l.elements.len != r.elements.len {
|
if l.elements.len != r.elements.len {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -60,31 +60,31 @@ fn (mut l Set<T>) equal(r Set<T>) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checks whether the set is empty.
|
// checks whether the set is empty.
|
||||||
fn (mut set Set<T>) is_empty() bool {
|
pub fn (mut 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.
|
||||||
fn (mut set Set<T>) size() int {
|
pub fn (mut 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.
|
||||||
fn (mut set Set<T>) copy() Set<T> {
|
pub fn (mut set Set<T>) copy() Set<T> {
|
||||||
return Set<T>{
|
return Set<T>{
|
||||||
elements: set.elements.clone()
|
elements: set.elements.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add_all adds the whole `elements` array to the set
|
// add_all adds the whole `elements` array to the set
|
||||||
fn (mut set Set<T>) add_all(elements []T) {
|
pub fn (mut set Set<T>) add_all(elements []T) {
|
||||||
for element in elements {
|
for element in elements {
|
||||||
set.add(element)
|
set.add(element)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// @union returns the union of the two sets.
|
// @union returns the union of the two sets.
|
||||||
fn (mut l Set<T>) @union(r Set<T>) Set<T> {
|
pub fn (mut 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 +93,7 @@ fn (mut l Set<T>) @union(r Set<T>) Set<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// intersection returns the intersection of sets.
|
// intersection returns the intersection of sets.
|
||||||
fn (mut l Set<T>) intersection(r Set<T>) Set<T> {
|
pub fn (mut 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 +109,7 @@ fn (mut l Set<T>) intersection(r Set<T>) Set<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// difference returns the difference of sets.
|
// difference returns the difference of sets.
|
||||||
fn (mut l Set<T>) difference(r Set<T>) Set<T> {
|
pub fn (mut l Set<T>) difference(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 +120,7 @@ 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`.
|
||||||
fn (mut l Set<T>) subset(r Set<T>) bool {
|
pub fn (mut 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
|
||||||
|
Loading…
Reference in New Issue
Block a user