blob: bec16456909e032dc54100ed5e4ba3e40ea8f841 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# MIT License, Copyright (c) 2023 Marvin Borner
# Set implementation using AVL trees
# can currently only store Numbers (due to std/Tree/Balanced)
:import std/Tree/Balanced T
:import std/List .
# empty set
empty T.empty ⧗ Set
# adds a number of a set
add T.insert ⧗ Number → Set → Set
# returns true if a number is in a set
has? T.has? ⧗ Number → Set → Boolean
:test (has? (+5) (add (+5) empty)) ([[1]])
:test (has? (+5) empty) ([[0]])
# count of elements in set
size T.size ⧗ Set → Number
# converts a set to a list
set→list T.tree→list ⧗ Set → (List Number)
# converts a list to a set
list→set T.list→tree ⧗ (List Number) → Set
:test (has? (+0) (list→set ((+5) : ((+3) : ((+2) : ((+1) : {}(+0))))))) ([[1]])
:test (has? (+5) (list→set ((+5) : ((+3) : ((+2) : ((+1) : {}(+0))))))) ([[1]])
:test (has? (+6) (list→set ((+5) : ((+3) : ((+2) : ((+1) : {}(+0))))))) ([[0]])
:test (has? (+7) (list→set ((+5) : ((+7) : {}(+1))))) ([[1]])
|