Hash-based set of keys parameterized by a key schema.


HashSet

Construct with the key schema before the type name: Key HashSet, for example Int32 HashSet.

HashSet

(keySchema -- set) Constructs an empty owning set from a key schema.
  • Stored keys must support construction. Text is not a constructible stored item here; use String for owned text.

Fields

A new HashSet is empty. An immutable set source is copied using contained assignment operations; a mutable set can be moved, leaving the source empty. Destroying the set destroys its owned arrays and keys, not objects merely referenced by contained reference fields.

A HashSet stores its keys in a HashTable, whose key requirements and iteration order apply. Reacquire key views and iterators after any insertion or erasure, and after clear, release, assignment, move, or destruction.

Methods

clear

(--) Removes stored keys and sets the item count to zero.
  • The outer bucket array retains its reserved allocation; stored keys are destroyed.

contains

(key -- cond) Reports whether key is stored.

empty

(-- cond) Reports whether the set has no stored keys.

erase

(key --) Removes key from the set.
  • On a nonempty set, a missing key reports Erasing unexisting element! and exits with status 2; on an empty set it does not.

eraseIf

(predicate --) Removes all stored keys for which predicate reports TRUE.
  • predicate has effect (key -- cond); key is an immutable view of a stored key.

insert

(key --) Constructs a stored key from key.
  • With DEBUG enabled, inserting an existing key reports The key is already in the HashTable and exits with status 2; with DEBUG disabled, duplicate keys are accepted.
  • key is copied or moved according to its view and construction support: a mutable managed key can be moved from, an immutable key is copied, and contained references retain aliasing.

iter

(-- iter) Returns an iterator of stored keys.
  • next has effect (-- item valid); it yields immutable key views.

release

(--) Releases bucket storage and sets the item count to zero.

size

(-- count) Returns the number of stored keys.

tryErase

(key -- cond) Removes key from the set and returns TRUE; returns FALSE when key is not stored.

tryInsert

(key -- cond) Inserts key and returns TRUE when key is not stored; otherwise returns FALSE.
  • When key is stored, no key is constructed; otherwise construction follows insert.

Examples

Example: eraseIf and iter

After four inserts, eraseIf removes the even keys, and iter visits the two that remain.

"HashSet"   use
"String"    use
"algorithm" use
"control"   use

{} Int32 {} [
  numbers: Int32 HashSet;
  1 @numbers.insert
  2 @numbers.insert
  3 @numbers.insert
  4 @numbers.insert
  [2 mod 0 =] @numbers.eraseIf
  sum: 0;
  numbers.iter [sum + @sum set] each
  ("size=" numbers.size LF
    "sum=" sum LF
    "has2=" 2 numbers.contains LF) printList
  0
] "main" exportFunction

Expected Output

size=2
sum=4
has2=FALSE

See also