sui::vec_set
VecSet
empty
singleton
insert
remove
contains
length
is_empty
into_keys
from_keys
keys
size
use std::option;
use std::vector;
VecSet
A set data structure backed by a vector. The set is guaranteed not to contain duplicate keys. All operations are O(N) in the size of the set
public struct VecSet<K: copy, drop> has copy, drop, store
contents: vector<K>
This key already exists in the map
const EKeyAlreadyExists: u64 = 0;
This key does not exist in the map
const EKeyDoesNotExist: u64 = 1;
empty
Create an empty VecSet
public fun empty<K: copy, drop>(): sui::vec_set::VecSet<K>
singleton
Create a singleton VecSet
that only contains one element.
public fun singleton<K: copy, drop>(key: K): sui::vec_set::VecSet<K>
public fun singleton<K: copy + drop>(key: K): VecSet<K> {
VecSet { contents: vector[key] }
}
insert
Insert a key
into self.
Aborts if key
is already present in self
.
public fun insert<K: copy, drop>(self: &mut sui::vec_set::VecSet<K>, key: K)
public fun insert<K: copy + drop>(self: &mut VecSet<K>, key: K) {
assert!(!self.contains(&key), EKeyAlreadyExists);
self.contents.push_back(key)
}
remove
Remove the entry key
from self. Aborts if key
is not present in self
.
public fun remove<K: copy, drop>(self: &mut sui::vec_set::VecSet<K>, key: &K)
public fun remove<K: copy + drop>(self: &mut VecSet<K>, key: &K) {
let idx = self.contents.find_index!(|k| k == key).destroy_or!(abort EKeyDoesNotExist);
self.contents.remove(idx);
}
contains
Return true if self
contains an entry for key
, false otherwise
public fun contains<K: copy, drop>(self: &sui::vec_set::VecSet<K>, key: &K): bool
public fun contains<K: copy + drop>(self: &VecSet<K>, key: &K): bool {
'search: {
self.contents.do_ref!(|k| if (k == key) return 'search true);
false
}
}
length
Return the number of entries in self
public fun length<K: copy, drop>(self: &sui::vec_set::VecSet<K>): u64
is_empty
Return true if self
has 0 elements, false otherwise
public fun is_empty<K: copy, drop>(self: &sui::vec_set::VecSet<K>): bool
into_keys
Unpack self
into vectors of keys.
The output keys are stored in insertion order, not sorted.
public fun into_keys<K: copy, drop>(self: sui::vec_set::VecSet<K>): vector<K>
public fun into_keys<K: copy + drop>(self: VecSet<K>): vector<K> {
let VecSet { contents } = self;
contents
}
from_keys
Construct a new VecSet
from a vector of keys.
The keys are stored in insertion order (the original keys
ordering)
and are not sorted.
public fun from_keys<K: copy, drop>(keys: vector<K>): sui::vec_set::VecSet<K>
public fun from_keys<K: copy + drop>(mut keys: vector<K>): VecSet<K> {
keys.reverse();
let mut set = empty();
while (keys.length() != 0) set.insert(keys.pop_back());
set
}
keys
Borrow the contents
of the VecSet
to access content by index
without unpacking. The contents are stored in insertion order,
not sorted.
public fun keys<K: copy, drop>(self: &sui::vec_set::VecSet<K>): &vector<K>
size
Return the number of entries in self
public fun size<K: copy, drop>(self: &sui::vec_set::VecSet<K>): u64