timo-weike / generic-collections / AutoSet

A wrapping for ManualSet.Set which stores a hash-function for later use. So after creating a Set there is not need to always specify the hash-function. This makes it more reliable and less error prone to use.

Like ManualSet.Set can this Set hold value of any type.

Sets


type Set comparable v

A set of values. The values can be of any type.

import AutoSet 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

Build

empty : (v -> comparable) -> Set comparable v

Creates an empty set that uses the given function for hashing.

Complexity: O(1)

singleton : (v -> comparable) -> v -> Set comparable v

Creates an set containing only one value and the hash-function.

Complexity: O(1)

insert : 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 -> Set comparable v -> Set comparable v

Removes a value from the set.

Complexity: O(log n)

Query

isEmpty : Set comparable v -> Basics.Bool

Determine if a set is empty.

isEmpty (empty someFunction) == True

Complexity: O(1)

member : v -> Set comparable v -> Basics.Bool

Determine if a value is in a set.

member (Cat "Jerry") pets == True
member (Bird "Jerry") pets == False

Complexity: O(log n)

size : Set comparable v -> Basics.Int

Determine the number of elements in a set.

size pets == 3

eq : Set comparable1 v -> Set comparable2 v -> Basics.Bool

Checks if the two sets contain the same elements.

Complexity: O(n * log n)

Lists

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)

Transform

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)

Combine

union : Set comparable1 v -> Set comparable2 v -> Set comparable2 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 : 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 : 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)

Hashing related

replaceMapping : (v -> comparable2) -> Set comparable1 v -> Set comparable2 v

Creates a new set where all value are rehashed using the new hash-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] |> replaceMapping abs |> toList) == [1]
(fromList negate [-1,1] |> replaceMapping abs |> toList) == [-1]