Module sui::vec_set
- Struct VecSet
- Constants
- Function empty
- Function singleton
- Function insert
- Function remove
- Function contains
- Function length
- Function is_empty
- Function into_keys
- Function from_keys
- Function keys
- Function size
use std::option;
use std::vector;
Struct 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
- the intention of this data structure is only to provide the convenience of programming against a set API. Sets that need sorted iteration rather than insertion order iteration should be handwritten.
public struct VecSet<K: copy, drop> has copy, drop, store
Click to open
Fields
-
contents: vector<K>
Constants
This key already exists in the map
const EKeyAlreadyExists: u64 = 0;
This key does not exist in the map
const EKeyDoesNotExist: u64 = 1;
Function empty
Create an empty
VecSet
public fun empty<K: copy, drop>(): sui::vec_set::VecSet<K>
Click to open
Function singleton
Create a singleton
VecSet that only contains one element.
public fun singleton<K: copy, drop>(key: K): sui::vec_set::VecSet<K>
Click to open
Function 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)
Click to open
Implementation
public fun insert<K: copy + drop>(self: &mut VecSet<K>, key: K) {
assert!(!self.contains(&key), EKeyAlreadyExists);
self.contents.push_back(key)
}
Function 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)
Click to open
Implementation
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);
}
Function 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
Click to open
Function length
Return the number of entries in self
public fun length<K: copy, drop>(self: &sui::vec_set::VecSet<K>): u64
Click to open
Function 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
Click to open