advent2021-0.1.0.0: Advent of Code 2021 Solutions
Copyright(c) Eric Mertens 2021
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellNone
LanguageHaskell2010

Advent.SmallSet

Description

A compact set type for when you have very few elements to track.

Synopsis

Documentation

newtype SmallSet Source #

Sets of integers from 0 to 63 efficiently implemented using a Word64

Constructors

SmallSet Word64 

Instances

Instances details
Eq SmallSet Source # 
Instance details

Defined in Advent.SmallSet

Ord SmallSet Source # 
Instance details

Defined in Advent.SmallSet

Read SmallSet Source #

Reads a SmallSet using fromList syntax

Instance details

Defined in Advent.SmallSet

Show SmallSet Source #

Shows a SmallSet using fromList syntax

Instance details

Defined in Advent.SmallSet

HasTrie SmallSet Source #

Instance derived from: HasTrie Word64

Instance details

Defined in Advent.SmallSet

Associated Types

data (:->:) SmallSet :: Type -> Type

Methods

trie :: (SmallSet -> b) -> SmallSet :->: b

untrie :: (SmallSet :->: b) -> SmallSet -> b

enumerate :: (SmallSet :->: b) -> [(SmallSet, b)]

newtype SmallSet :->: a Source # 
Instance details

Defined in Advent.SmallSet

newtype SmallSet :->: a = T (Word64 :->: a)

fromList :: [Int] -> SmallSet Source #

Construct a set given a list of set members.

>>> fromList []
fromList []
>>> fromList [0]
fromList [0]
>>> fromList [63]
fromList [63]
>>> fromList [0,10,20]
fromList [0,10,20]

toList :: SmallSet -> [Int] Source #

Return an ordered list of the elements in the set

inRange :: Int -> Bool Source #

Predicate for integer elements that fit in a SmallSet

empty :: SmallSet Source #

The set with no members.

>>> empty
fromList []

singleton :: Int -> SmallSet Source #

Make a new set with a single element

>>> singleton 42
fromList [42]

union :: SmallSet -> SmallSet -> SmallSet Source #

Compute the union of two sets

>>> union (fromList [3,4,5,6]) (fromList [5,6,7,8])
fromList [3,4,5,6,7,8]

unions :: [SmallSet] -> SmallSet Source #

Union of the sets in a list

>>> unions []
fromList []
>>> unions [singleton 1, fromList [2,4], fromList [2,3]]
fromList [1,2,3,4]

intersection :: SmallSet -> SmallSet -> SmallSet Source #

Compute the intersection of two sets

>>> intersection (fromList [3,4,5,6]) (fromList [5,6,7,8])
fromList [5,6]

difference :: SmallSet -> SmallSet -> SmallSet Source #

Subtract the elements of the second set from the first set.

>>> difference (fromList [3,4,5,6]) (fromList [5,6,7,8])
fromList [3,4]

insert :: Int -> SmallSet -> SmallSet Source #

Add an element to a set

>>> insert 10 (fromList [3,4,5])
fromList [3,4,5,10]
>>> insert 5 (fromList [3,4,5])
fromList [3,4,5]

delete :: Int -> SmallSet -> SmallSet Source #

Remove an element from a set

>>> delete 5 (fromList [3,4,5])
fromList [3,4]
>>> delete 8 (fromList [3,4,5])
fromList [3,4,5]

member :: Int -> SmallSet -> Bool Source #

Check if a set contains an element

>>> member 8 (fromList [3,4,5])
False
>>> member 4 (fromList [3,4,5])
True

size :: SmallSet -> Int Source #

Number of elements in a set.

>>> size (fromList [1,2,3])
3
>>> size empty
0