uulib-0.9.9: Haskell Utrecht Tools Library

UU.DData.IntBag

Contents

Description

 

Synopsis

Bag type

data IntBag Source

A bag of integers.

Instances

Operators

(\\) :: IntBag -> IntBag -> IntBagSource

O(n+m). See difference.

Query

isEmpty :: IntBag -> BoolSource

O(1). Is the bag empty?

size :: IntBag -> IntSource

O(n). The number of elements in the bag.

distinctSize :: IntBag -> IntSource

O(n). Returns the number of distinct elements in the bag, ie. (distinctSize bag == length (nub (toList bag))).

member :: Int -> IntBag -> BoolSource

O(min(n,W)). Is the element in the bag?

occur :: Int -> IntBag -> IntSource

O(min(n,W)). The number of occurrences of an element in the bag.

subset :: IntBag -> IntBag -> BoolSource

O(n+m). Is this a subset of the bag?

properSubset :: IntBag -> IntBag -> BoolSource

O(n+m). Is this a proper subset? (ie. a subset and not equal)

Construction

empty :: IntBagSource

O(1). Create an empty bag.

single :: Int -> IntBagSource

O(1). Create a singleton bag.

insert :: Int -> IntBag -> IntBagSource

O(min(n,W)). Insert an element in the bag.

insertMany :: Int -> Int -> IntBag -> IntBagSource

O(min(n,W)). The expression (insertMany x count bag) inserts count instances of x in the bag bag.

delete :: Int -> IntBag -> IntBagSource

O(min(n,W)). Delete a single element.

deleteAll :: Int -> IntBag -> IntBagSource

O(min(n,W)). Delete all occurrences of an element.

Combine

union :: IntBag -> IntBag -> IntBagSource

O(n+m). Union of two bags. The union adds the elements together.

 IntBag\> union (fromList [1,1,2]) (fromList [1,2,2,3])
 {1,1,1,2,2,2,3}

difference :: IntBag -> IntBag -> IntBagSource

O(n+m). Difference between two bags.

 IntBag\> difference (fromList [1,1,2]) (fromList [1,2,2,3])
 {1}

intersection :: IntBag -> IntBag -> IntBagSource

O(n+m). Intersection of two bags.

 IntBag\> intersection (fromList [1,1,2]) (fromList [1,2,2,3])
 {1,2}

unions :: [IntBag] -> IntBagSource

The union of a list of bags.

Filter

filter :: (Int -> Bool) -> IntBag -> IntBagSource

O(n). Filter all elements that satisfy some predicate.

partition :: (Int -> Bool) -> IntBag -> (IntBag, IntBag)Source

O(n). Partition the bag according to some predicate.

Fold

fold :: (Int -> b -> b) -> b -> IntBag -> bSource

O(n). Fold over each element in the bag.

foldOccur :: (Int -> Int -> b -> b) -> b -> IntBag -> bSource

O(n). Fold over all occurrences of an element at once. In a call (foldOccur f z bag), the function f takes the element first and than the occur count.

Conversion

elems :: IntBag -> [Int]Source

O(n). The list of elements.

List

toList :: IntBag -> [Int]Source

O(n). Create a list with all elements.

fromList :: [Int] -> IntBagSource

O(n*min(n,W)). Create a bag from a list of elements.

Ordered list

toAscList :: IntBag -> [Int]Source

O(n). Create an ascending list of all elements.

fromAscList :: [Int] -> IntBagSource

O(n*min(n,W)). Create a bag from an ascending list.

fromDistinctAscList :: [Int] -> IntBagSource

O(n*min(n,W)). Create a bag from an ascending list of distinct elements.

Occurrence lists

toOccurList :: IntBag -> [(Int, Int)]Source

O(n). Create a list of element/occurrence pairs.

toAscOccurList :: IntBag -> [(Int, Int)]Source

O(n). Create an ascending list of element/occurrence pairs.

fromOccurList :: [(Int, Int)] -> IntBagSource

O(n*min(n,W)). Create a bag from a list of element/occurrence pairs.

fromAscOccurList :: [(Int, Int)] -> IntBagSource

O(n*min(n,W)). Create a bag from an ascending list of element/occurrence pairs.

IntMap

toMap :: IntBag -> IntMap IntSource

O(1). Convert to an IntMap.IntMap from elements to number of occurrences.

fromMap :: IntMap Int -> IntBagSource

O(n). Convert a IntMap.IntMap from elements to occurrences into a bag.

fromOccurMap :: IntMap Int -> IntBagSource

O(1). Convert a IntMap.IntMap from elements to occurrences into a bag. Assumes that the IntMap.IntMap contains only elements that occur at least once.

Debugging

showTree :: IntBag -> StringSource

O(n). Show the tree structure that implements the IntBag. The tree is shown as a compressed and hanging.

showTreeWith :: Bool -> Bool -> IntBag -> StringSource

O(n). The expression (showTreeWith hang wide map) shows the tree that implements the bag. The tree is shown hanging when hang is True and otherwise as a rotated tree. When wide is True an extra wide version is shown.