/// A set data structure. module pyth::set { use std::table::{Self, Table}; use std::vector; /// Empty struct. Used as the value type in mappings to encode a set struct Unit has store, copy, drop {} /// A set containing elements of type `A` with support for membership /// checking. struct Set has store { keys: vector, elems: Table } /// Create a new Set. public fun new(): Set { Set { keys: vector::empty(), elems: table::new(), } } /// Add a new element to the set. /// Aborts if the element already exists public fun add(set: &mut Set, key: A) { table::add(&mut set.elems, key, Unit {}); vector::push_back(&mut set.keys, key); } /// Returns true iff `set` contains an entry for `key`. public fun contains(set: &Set, key: A): bool { table::contains(&set.elems, key) } /// Removes all elements from the set public fun empty(set: &mut Set) { while (!vector::is_empty(&set.keys)) { table::remove(&mut set.elems, vector::pop_back(&mut set.keys)); } } // TODO: destroy_empty, but this is tricky because std::table doesn't // have this functionality. }