A version of Set
from elm/core
that is implemented with a Dict
from elm/core
as storage.
The values can be any type but the user needs to specify a
conversion/hash-function for every action that needs to compare values with
each other.
The hash function can map the values to any comparable type, that is Int
,
Float
, Time
, Char
, Bool
and tuples or list of comparable types.
A set of values. The values can be of any type.
import ManualSet as Set exposing (Set)
pets : Set String Pet
pets
= Set.fromList petToString
[ Dog "Max"
, Cat "Jerry"
, Bird "Ace"
]
type Pet
= Dog String
| Cat String
| Bird String
petToString : Pet -> String
petToString pet = case pet of
Dog name -> "Dog: " ++ name
Cat name -> "Cat: " ++ name
Bird name -> "Bird: " ++ name
empty : Set comparable v
Creates an empty set.
Complexity: O(1)
singleton : (v -> comparable) -> v -> Set comparable v
Creates an set, containing only one value.
Complexity: O(1)
insert : (v -> comparable) -> v -> Set comparable v -> Set comparable v
Inserts a value into the set. A value might be replaced if it's hash collides with the hash of some other value.
Complexity: O(log n)
remove : (v -> comparable) -> v -> Set comparable v -> Set comparable v
Removes a value from the set.
Complexity: O(log n)
isEmpty : Set comparable v -> Basics.Bool
Determine if a set is empty.
isEmpty empty == True
Complexity: O(1)
member : (v -> comparable) -> v -> Set comparable v -> Basics.Bool
Determine if a value is in a set.
member petToString (Cat "Jerry") pets == True
member petToString (Bird "Jerry") pets == False
Complexity: O(log n)
size : Set comparable v -> Basics.Int
Determine the number of elements in a set.
Complexity: O(n)
eq : (v -> comparable1) -> (v -> comparable2) -> Set comparable1 v -> Set comparable2 v -> Basics.Bool
Checks if the two sets contain the same elements.
set1 = fromList abs [1]
set2 = fromList abs [-1]
eq identity identity set1 set2 == False
eq abs abs set1 set2 == True
Complexity: O(n * log n)
toList : Set comparable v -> List v
Converts the set into a list of its values.
Complexity: O(n)
fromList : (v -> comparable) -> List v -> Set comparable v
Converts a list of values into a set.
Complexity: O(n * log n)
map : (v2 -> comparable) -> (v1 -> v2) -> Set comparable v1 -> Set comparable v2
Applies a function to all values in a set.
Complexity: O(n)
foldl : (v -> a -> a) -> a -> Set comparable v -> a
Folds over the values in a set from lowest to highest (depending on the string representation).
Complexity: O(n)
foldr : (v -> a -> a) -> a -> Set comparable v -> a
Folds over the values in a set from highest to lowest (depending on the string representation).
Complexity: O(n)
filter : (v -> Basics.Bool) -> Set comparable v -> Set comparable v
Keeps all the value for which the function yields True.
Complexity: O(n)
partition : (v -> Basics.Bool) -> Set comparable v -> ( Set comparable v, Set comparable v )
Partitions a set into two subsets, according to some function. The first set will contain the values for which the function yields True and the second set will contain the values for which the function yields False.
Complexity: O(n * log n)
union : (v -> comparable3) -> Set comparable1 v -> Set comparable2 v -> Set comparable3 v
Creates the union of two sets. If two values have the same comparable representation then the value from the first set is in the resulting set.
Complexity: O(n * log n)
intersect : (v -> comparable2) -> Set comparable1 v -> Set comparable2 v -> Set comparable1 v
Keeps all values which comparable representation is contained in both sets. Preference is given to values in the first set.
Complexity: O(n * log n)
diff : (v -> comparable1) -> Set comparable1 v -> Set comparable2 v -> Set comparable1 v
Keeps all values from the first set which don't appear in the second set.
Complexity: O(n * log n)
reHash : (v -> comparable2) -> Set comparable1 v -> Set comparable2 v
Creates a new set where all values are rehashed with the given function. If two keys have a collision under the new hashing the key-value-pair with the higher value under the old hashing is kept.
(fromList identity [-1,1] |> reHash abs |> toList) == [1]
(fromList negate [-1,1] |> reHash abs |> toList) == [-1]