-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Containers for intervals, with efficient search. -- -- Ordered containers of intervals, with efficient search for all keys -- containing a point or overlapping an interval. See the example code on -- the home page for a quick introduction. @package IntervalMap @version 0.5.0.0 -- | A conservative implementation of Intervals, mostly for use as keys in -- a IntervalMap. -- -- This should really be a typeclass, so you could have a tuple be an -- instance of Interval, but that is currently not possible in standard -- Haskell. -- -- The contructor names of the half-open intervals seem somewhat clumsy, -- and I'm open to suggestions for better names. module Data.IntervalMap.Interval -- | Intervals with endpoints of type a. -- -- Read and Show use mathematical notation with square -- brackets for closed and parens for open intervals. This is better for -- human readability, but is not a valid Haskell expression. Closed -- intervals look like a list, open intervals look like a tuple, and -- half-open intervals look like mismatched parens. data Interval a -- | Including lower bound, excluding upper IntervalCO :: !a -> !a -> Interval a -- | Closed at both ends ClosedInterval :: !a -> !a -> Interval a -- | Open at both ends OpenInterval :: !a -> !a -> Interval a -- | Excluding lower bound, including upper IntervalOC :: !a -> !a -> Interval a -- | Get the lower bound. lowerBound :: Interval a -> a -- | Get the upper bound. upperBound :: Interval a -> a -- | Does the interval include its lower bound? leftClosed :: Interval a -> Bool -- | Does the interval include its upper bound? rightClosed :: Interval a -> Bool -- | Is the interval empty? isEmpty :: (Ord a) => Interval a -> Bool -- | Do the two intervals overlap? overlaps :: (Ord a) => Interval a -> Interval a -> Bool -- | Does the first interval completely contain the second? subsumes :: (Ord a) => Interval a -> Interval a -> Bool -- | Interval strictly before another? True if the upper bound of the first -- interval is below the lower bound of the second. before :: Ord a => Interval a -> Interval a -> Bool -- | Interval strictly after another? Same as 'flip before'. after :: Ord a => Interval a -> Interval a -> Bool -- | Like compare, but considering the upper bound first. compareByUpper :: Ord a => Interval a -> Interval a -> Ordering -- | If the intervals overlap combine them into one. combine :: (Ord a) => Interval a -> Interval a -> Maybe (Interval a) -- | Is a point strictly less than lower bound? below :: (Ord a) => a -> Interval a -> Bool -- | Does the interval contain a given point? inside :: (Ord a) => a -> Interval a -> Bool -- | Is a point strictly greater than upper bound? above :: (Ord a) => a -> Interval a -> Bool instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.IntervalMap.Interval.Interval a) instance GHC.Show.Show a => GHC.Show.Show (Data.IntervalMap.Interval.Interval a) instance GHC.Read.Read a => GHC.Read.Read (Data.IntervalMap.Interval.Interval a) instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.IntervalMap.Interval.Interval a) instance GHC.Base.Functor Data.IntervalMap.Interval.Interval instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.IntervalMap.Interval.Interval a) -- | Type class for IntervalMap keys. -- -- As there is no sensible default, no instances for prelude types are -- provided (E.g. you might want to have tuples as closed intervals in -- one case, and open in another). -- -- Empty intervals, i.e. intervals where 'lowerBound >= upperBound' -- should be avoided if possible. If you must use empty intervals, you -- need to provide implementations for all operations, as the default -- implementations do not necessarily work correctly. for example, the -- default implementation of inside returns True if the -- point is equal to the lowerBound of a left-closed interval even if it -- is larger than the upper bound. module Data.IntervalMap.Generic.Interval -- | Intervals with endpoints of type e. A minimal instance -- declaration for a closed interval needs only to define -- lowerBound and upperBound. class Ord e => Interval i e | i -> e where leftClosed _ = True rightClosed _ = True a `before` b = upperBound a < lowerBound b || (upperBound a == lowerBound b && not (rightClosed a && leftClosed b)) a `after` b = b `before` a a `subsumes` b = (lowerBound a < lowerBound b || (lowerBound a == lowerBound b && (leftClosed a || not (leftClosed b)))) && (upperBound a > upperBound b || (upperBound a == upperBound b && (rightClosed a || not (rightClosed b)))) a `overlaps` b = (lowerBound a < upperBound b || (lowerBound a == upperBound b && leftClosed a && rightClosed b)) && (upperBound a > lowerBound b || (upperBound a == lowerBound b && rightClosed a && leftClosed b)) p `below` i = case compare p (lowerBound i) of { LT -> True EQ -> not (leftClosed i) GT -> False } p `above` i = case compare p (upperBound i) of { LT -> False EQ -> not (rightClosed i) GT -> True } p `inside` i = not ((p `above` i) || (p `below` i)) isEmpty i | leftClosed i && rightClosed i = lowerBound i > upperBound i | otherwise = lowerBound i >= upperBound i -- | lower bound lowerBound :: Interval i e => i -> e -- | upper bound upperBound :: Interval i e => i -> e -- | Does the interval include its lower bound? Default is True for all -- values, i.e. closed intervals. leftClosed :: Interval i e => i -> Bool -- | Does the interval include its upper bound bound? Default is True for -- all values, i.e. closed intervals. rightClosed :: Interval i e => i -> Bool -- | Interval strictly before another? True if the upper bound of the first -- interval is below the lower bound of the second. before :: Interval i e => i -> i -> Bool -- | Interval strictly after another? Same as 'flip before'. after :: Interval i e => i -> i -> Bool -- | Does the first interval completely contain the second? subsumes :: Interval i e => i -> i -> Bool -- | Do the two intervals overlap? overlaps :: Interval i e => i -> i -> Bool -- | Is a point strictly less than lower bound? below :: Interval i e => e -> i -> Bool -- | Is a point strictly greater than upper bound? above :: Interval i e => e -> i -> Bool -- | Does the interval contain a given point? inside :: Interval i e => e -> i -> Bool -- | Is the interval empty? isEmpty :: Interval i e => i -> Bool genericEquals :: (Interval i e, Eq e) => i -> i -> Bool genericCompare :: (Interval i e, Ord e) => i -> i -> Ordering instance GHC.Classes.Ord a => Data.IntervalMap.Generic.Interval.Interval (Data.IntervalMap.Interval.Interval a) a -- | An implementation of maps from intervals to values. The key intervals -- may overlap, and the implementation contains efficient search -- functions for all keys containing a point or overlapping an interval. -- Closed, open, and half-open intervals can be contained in the same -- map. -- -- This module implements the same functions as -- Data.IntervalMap.Generic.Strict, but with value-lazy semantics. module Data.IntervalMap.Generic.Lazy -- | Intervals with endpoints of type e. A minimal instance -- declaration for a closed interval needs only to define -- lowerBound and upperBound. class Ord e => Interval i e | i -> e where leftClosed _ = True rightClosed _ = True a `before` b = upperBound a < lowerBound b || (upperBound a == lowerBound b && not (rightClosed a && leftClosed b)) a `after` b = b `before` a a `subsumes` b = (lowerBound a < lowerBound b || (lowerBound a == lowerBound b && (leftClosed a || not (leftClosed b)))) && (upperBound a > upperBound b || (upperBound a == upperBound b && (rightClosed a || not (rightClosed b)))) a `overlaps` b = (lowerBound a < upperBound b || (lowerBound a == upperBound b && leftClosed a && rightClosed b)) && (upperBound a > lowerBound b || (upperBound a == lowerBound b && rightClosed a && leftClosed b)) p `below` i = case compare p (lowerBound i) of { LT -> True EQ -> not (leftClosed i) GT -> False } p `above` i = case compare p (upperBound i) of { LT -> False EQ -> not (rightClosed i) GT -> True } p `inside` i = not ((p `above` i) || (p `below` i)) isEmpty i | leftClosed i && rightClosed i = lowerBound i > upperBound i | otherwise = lowerBound i >= upperBound i -- | lower bound lowerBound :: Interval i e => i -> e -- | upper bound upperBound :: Interval i e => i -> e -- | Does the interval include its lower bound? Default is True for all -- values, i.e. closed intervals. leftClosed :: Interval i e => i -> Bool -- | Does the interval include its upper bound bound? Default is True for -- all values, i.e. closed intervals. rightClosed :: Interval i e => i -> Bool -- | Interval strictly before another? True if the upper bound of the first -- interval is below the lower bound of the second. before :: Interval i e => i -> i -> Bool -- | Interval strictly after another? Same as 'flip before'. after :: Interval i e => i -> i -> Bool -- | Does the first interval completely contain the second? subsumes :: Interval i e => i -> i -> Bool -- | Do the two intervals overlap? overlaps :: Interval i e => i -> i -> Bool -- | Is a point strictly less than lower bound? below :: Interval i e => e -> i -> Bool -- | Is a point strictly greater than upper bound? above :: Interval i e => e -> i -> Bool -- | Does the interval contain a given point? inside :: Interval i e => e -> i -> Bool -- | Is the interval empty? isEmpty :: Interval i e => i -> Bool -- | A map from intervals of type k to values of type v. data IntervalMap k v -- | O(log n). Lookup value for given key. Calls error if the -- key is not in the map. -- -- Use lookup or findWithDefault instead of this function, -- unless you are absolutely sure that the key is present in the map. (!) :: (Interval k e, Ord k) => IntervalMap k v -> k -> v -- | Same as difference. (\\) :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(1). Is the map empty? null :: IntervalMap k v -> Bool -- | O(n). Number of keys in the map. -- -- Caution: unlike size, which takes constant time, this is linear -- in the number of keys! size :: IntervalMap k v -> Int -- | O(log n). Does the map contain the given key? See also -- notMember. member :: (Ord k) => k -> IntervalMap k v -> Bool -- | O(log n). Does the map not contain the given key? See also -- member. notMember :: (Ord k) => k -> IntervalMap k v -> Bool -- | O(log n). Look up the given key in the map, returning the value -- (Just value), or Nothing if the key is not in -- the map. lookup :: (Ord k) => k -> IntervalMap k v -> Maybe v -- | O(log n). The expression (findWithDefault def k -- map) returns the value at key k or returns default value -- def when the key is not in the map. findWithDefault :: Ord k => a -> k -> IntervalMap k a -> a -- | Return the submap of key intervals containing the given point. This is -- the second element of the value of splitAt: -- --
-- m `containing` p == let (_,m',_) = m `splitAt` p in m' ---- -- O(n), since potentially all keys could contain the point. -- O(log n) average case. This is also the worst case for maps -- containing no overlapping keys. containing :: (Interval k e) => IntervalMap k v -> e -> IntervalMap k v -- | Return the submap of key intervals overlapping (intersecting) the -- given interval. This is the second element of the value of -- splitIntersecting: -- --
-- m `intersecting` i == let (_,m',_) = m `splitIntersecting` i in m' ---- -- O(n), since potentially all keys could intersect the interval. -- O(log n) average case, if few keys intersect the interval. intersecting :: (Interval k e) => IntervalMap k v -> k -> IntervalMap k v -- | Return the submap of key intervals completely inside the given -- interval. -- -- O(n), since potentially all keys could be inside the interval. -- O(log n) average case, if few keys are inside the interval. within :: (Interval k e) => IntervalMap k v -> k -> IntervalMap k v -- | O(1). The empty map. empty :: IntervalMap k v -- | O(1). A map with one entry. singleton :: k -> v -> IntervalMap k v -- | O(log n). Insert a new key/value pair. If the map already -- contains the key, its value is changed to the new value. insert :: (Interval k e, Ord k) => k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Insert with a function, combining new value and old -- value. insertWith f key value mp will insert the pair -- (key, value) into mp if key does not exist in the map. If the -- key does exist, the function will insert the pair (key, f -- new_value old_value). insertWith :: (Interval k e, Ord k) => (v -> v -> v) -> k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Insert with a function, combining key, new value and -- old value. insertWithKey f key value mp will insert -- the pair (key, value) into mp if key does not exist in the -- map. If the key does exist, the function will insert the pair -- (key, f key new_value old_value). Note that the key passed to -- f is the same key passed to insertWithKey. insertWithKey :: (Interval k e, Ord k) => (k -> v -> v -> v) -> k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Combine insert with old values retrieval. insertLookupWithKey :: (Interval k e, Ord k) => (k -> v -> v -> v) -> k -> v -> IntervalMap k v -> (Maybe v, IntervalMap k v) -- | O(log n). Delete a key from the map. If the map does not -- contain the key, it is returned unchanged. delete :: (Interval k e, Ord k) => k -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update a value at a specific key with the result of -- the provided function. When the key is not a member of the map, the -- original map is returned. adjust :: Ord k => (a -> a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). Adjust a value at a specific key. When the key is not -- a member of the map, the original map is returned. adjustWithKey :: Ord k => (k -> a -> a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). The expression (update f k map) -- updates the value x at k (if it is in the map). If -- (f x) is Nothing, the element is deleted. If it is -- (Just y), the key k is bound to the new value -- y. update :: (Interval k e, Ord k) => (a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). The expression (updateWithKey f k -- map) updates the value x at k (if it is in the -- map). If (f k x) is Nothing, the element is deleted. -- If it is (Just y), the key k is bound to the -- new value y. updateWithKey :: (Interval k e, Ord k) => (k -> a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). Lookup and update. See also updateWithKey. The -- function returns changed value, if it is updated. Returns the original -- key value if the map entry is deleted. updateLookupWithKey :: (Interval k e, Ord k) => (k -> a -> Maybe a) -> k -> IntervalMap k a -> (Maybe a, IntervalMap k a) -- | O(log n). The expression (alter f k map) alters -- the value x at k, or absence thereof. alter -- can be used to insert, delete, or update a value in a Map. In -- short : lookup k (alter f k m) = f (lookup k -- m). alter :: (Interval k e, Ord k) => (Maybe a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(n+m). The expression (union t1 t2) takes the -- left-biased union of t1 and t2. It prefers -- t1 when duplicate keys are encountered, i.e. -- (union == unionWith const). union :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | O(n+m). Union with a combining function. unionWith :: (Interval k e, Ord k) => (a -> a -> a) -> IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | O(n+m). Union with a combining function. unionWithKey :: (Interval k e, Ord k) => (k -> a -> a -> a) -> IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | The union of a list of maps: (unions == foldl -- union empty). unions :: (Interval k e, Ord k) => [IntervalMap k a] -> IntervalMap k a -- | The union of a list of maps, with a combining operation: -- (unionsWith f == foldl (unionWith f) -- empty). unionsWith :: (Interval k e, Ord k) => (a -> a -> a) -> [IntervalMap k a] -> IntervalMap k a -- | O(n+m). Difference of two maps. Return elements of the first -- map not existing in the second map. difference :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Difference with a combining function. When two equal -- keys are encountered, the combining function is applied to the values -- of these keys. If it returns Nothing, the element is discarded -- (proper set difference). If it returns (Just y), the -- element is updated with a new value y. differenceWith :: (Interval k e, Ord k) => (a -> b -> Maybe a) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Difference with a combining function. When two equal -- keys are encountered, the combining function is applied to the key and -- both values. If it returns Nothing, the element is discarded -- (proper set difference). If it returns (Just y), the -- element is updated with a new value y. differenceWithKey :: (Interval k e, Ord k) => (k -> a -> b -> Maybe a) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Intersection of two maps. Return data in the first map -- for the keys existing in both maps. (intersection m1 m2 == -- intersectionWith const m1 m2). intersection :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Intersection with a combining function. intersectionWith :: (Interval k e, Ord k) => (a -> b -> c) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k c -- | O(n+m). Intersection with a combining function. intersectionWithKey :: (Interval k e, Ord k) => (k -> a -> b -> c) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k c -- | O(n). Map a function over all values in the map. map :: (a -> b) -> IntervalMap k a -> IntervalMap k b -- | O(n). Map a function over all values in the map. mapWithKey :: (k -> a -> b) -> IntervalMap k a -> IntervalMap k b -- | O(n). The function mapAccum threads an accumulating -- argument through the map in ascending order of keys. mapAccum :: (a -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c) -- | O(n). The function mapAccumWithKey threads an -- accumulating argument through the map in ascending order of keys. mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c) -- | O(n). The function mapAccumRWithKey threads an -- accumulating argument through the map in descending order of keys. mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c) -- | O(n log n). mapKeys f s is the map obtained by -- applying f to each key of s. -- -- The size of the result may be smaller if f maps two or more -- distinct keys to the same new key. In this case the value at the -- smallest of these keys is retained. mapKeys :: (Interval k2 e, Ord k2) => (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a -- | O(n log n). mapKeysWith c f s is the map -- obtained by applying f to each key of s. -- -- The size of the result may be smaller if f maps two or more -- distinct keys to the same new key. In this case the associated values -- will be combined using c. mapKeysWith :: (Interval k2 e, Ord k2) => (a -> a -> a) -> (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a -- | O(n). mapKeysMonotonic f s == mapKeys f -- s, but works only when f is strictly monotonic. That is, -- for any values x and y, if x < -- y then f x < f y. The precondition is -- not checked. mapKeysMonotonic :: (Interval k2 e, Ord k2) => (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a -- | O(n). Fold the values in the map using the given -- right-associative binary operator, such that foldr f z == -- foldr f z . elems. foldr :: (a -> b -> b) -> b -> IntervalMap k a -> b -- | O(n). Fold the values in the map using the given -- left-associative binary operator, such that foldl f z == -- foldl f z . elems. foldl :: (b -> a -> b) -> b -> IntervalMap k a -> b -- | O(n). Fold the keys and values in the map using the given -- right-associative binary operator, such that foldrWithKey f -- z == foldr (uncurry f) z . toAscList. foldrWithKey :: (k -> v -> a -> a) -> a -> IntervalMap k v -> a -- | O(n). Fold the keys and values in the map using the given -- left-associative binary operator, such that foldlWithKey f -- z == foldl (\z' (kx, x) -> f z' kx x) z . -- toAscList. foldlWithKey :: (a -> k -> v -> a) -> a -> IntervalMap k v -> a -- | O(n log n). Build a new map by combining successive key/value -- pairs. flattenWith :: (Ord k, Interval k e) => ((k, v) -> (k, v) -> Maybe (k, v)) -> IntervalMap k v -> IntervalMap k v -- | O(n). Build a new map by combining successive key/value pairs. -- Same as flattenWith, but works only when the combining -- functions returns strictly monotonic key values. flattenWithMonotonic :: (Interval k e) => ((k, v) -> (k, v) -> Maybe (k, v)) -> IntervalMap k v -> IntervalMap k v -- | O(n). List of all values in the map, in ascending order of -- their keys. elems :: IntervalMap k v -> [v] -- | O(n). List of all keys in the map, in ascending order. keys :: IntervalMap k v -> [k] -- | O(n). Set of the keys. keysSet :: (Ord k) => IntervalMap k v -> Set k -- | Same as toAscList. assocs :: IntervalMap k v -> [(k, v)] -- | O(n). The list of all key/value pairs contained in the map, in -- no particular order. toList :: IntervalMap k v -> [(k, v)] -- | O(n log n). Build a map from a list of key/value pairs. See -- also fromAscList. If the list contains more than one value for -- the same key, the last value for the key is retained. fromList :: (Interval k e, Ord k) => [(k, v)] -> IntervalMap k v -- | O(n log n). Build a map from a list of key/value pairs with a -- combining function. See also fromAscListWith. fromListWith :: (Interval k e, Ord k) => (a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n log n). Build a map from a list of key/value pairs with a -- combining function. See also fromAscListWith. fromListWithKey :: (Interval k e, Ord k) => (k -> a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n). The list of all key/value pairs contained in the map, in -- ascending order of keys. toAscList :: IntervalMap k v -> [(k, v)] -- | O(n). The list of all key/value pairs contained in the map, in -- descending order of keys. toDescList :: IntervalMap k v -> [(k, v)] -- | O(n). Build a map from an ascending list in linear time. The -- precondition (input list is ascending) is not checked. fromAscList :: (Interval k e, Eq k) => [(k, v)] -> IntervalMap k v -- | O(n). Build a map from an ascending list in linear time with a -- combining function for equal keys. The precondition (input list is -- ascending) is not checked. fromAscListWith :: (Interval k e, Eq k) => (a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n). Build a map from an ascending list in linear time with a -- combining function for equal keys. The precondition (input list is -- ascending) is not checked. fromAscListWithKey :: (Interval k e, Eq k) => (k -> a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n). Build a map from an ascending list of elements with -- distinct keys in linear time. The precondition is not checked. fromDistinctAscList :: (Interval k e) => [(k, v)] -> IntervalMap k v -- | O(n). Filter values satisfying a predicate. filter :: (Interval k e) => (a -> Bool) -> IntervalMap k a -> IntervalMap k a -- | O(n). Filter keys/values satisfying a predicate. filterWithKey :: (Interval k e) => (k -> a -> Bool) -> IntervalMap k a -> IntervalMap k a -- | O(n). Partition the map according to a predicate. The first map -- contains all elements that satisfy the predicate, the second all -- elements that fail the predicate. See also split. partition :: (Interval k e) => (a -> Bool) -> IntervalMap k a -> (IntervalMap k a, IntervalMap k a) -- | O(n). Partition the map according to a predicate. The first map -- contains all elements that satisfy the predicate, the second all -- elements that fail the predicate. See also split. partitionWithKey :: (Interval k e) => (k -> a -> Bool) -> IntervalMap k a -> (IntervalMap k a, IntervalMap k a) -- | O(n). Map values and collect the Just results. mapMaybe :: (Interval k e) => (a -> Maybe b) -> IntervalMap k a -> IntervalMap k b -- | O(n). Map keys/values and collect the Just results. mapMaybeWithKey :: (Interval k e) => (k -> a -> Maybe b) -> IntervalMap k a -> IntervalMap k b -- | O(n). Map values and separate the Left and Right -- results. mapEither :: (Interval k e) => (a -> Either b c) -> IntervalMap k a -> (IntervalMap k b, IntervalMap k c) -- | O(n). Map keys/values and separate the Left and -- Right results. mapEitherWithKey :: (Interval i k) => (i -> a -> Either b c) -> IntervalMap i a -> (IntervalMap i b, IntervalMap i c) -- | O(n). The expression (split k map) is a pair -- (map1,map2) where the keys in map1 are smaller than -- k and the keys in map2 larger than k. Any -- key equal to k is found in neither map1 nor -- map2. split :: (Interval i k, Ord i) => i -> IntervalMap i a -> (IntervalMap i a, IntervalMap i a) -- | O(n). The expression (splitLookup k map) splits -- a map just like split but also returns lookup k -- map. splitLookup :: (Interval i k, Ord i) => i -> IntervalMap i a -> (IntervalMap i a, Maybe a, IntervalMap i a) -- | O(n). Split around a point. Splits the map into three submaps: -- intervals below the point, intervals containing the point, and -- intervals above the point. splitAt :: (Interval i k, Ord i) => IntervalMap i a -> k -> (IntervalMap i a, IntervalMap i a, IntervalMap i a) -- | O(n). Split around an interval. Splits the set into three -- subsets: intervals below the given interval, intervals intersecting -- the given interval, and intervals above the given interval. splitIntersecting :: (Interval i k, Ord i) => IntervalMap i a -> i -> (IntervalMap i a, IntervalMap i a, IntervalMap i a) -- | O(n+m). This function is defined as (isSubmapOf = -- isSubmapOfBy (==)). isSubmapOf :: (Ord k, Eq a) => IntervalMap k a -> IntervalMap k a -> Bool -- | O(n+m). The expression (isSubmapOfBy f t1 t2) -- returns True if all keys in t1 are in tree -- t2, and f returns True when applied to their -- respective values. isSubmapOfBy :: Ord k => (a -> b -> Bool) -> IntervalMap k a -> IntervalMap k b -> Bool -- | O(n+m). Is this a proper submap? (ie. a submap but not equal). -- Defined as (isProperSubmapOf = isProperSubmapOfBy -- (==)). isProperSubmapOf :: (Ord k, Eq a) => IntervalMap k a -> IntervalMap k a -> Bool -- | O(n+m). Is this a proper submap? (ie. a submap but not equal). -- The expression (isProperSubmapOfBy f m1 m2) returns -- True when m1 and m2 are not equal, all keys -- in m1 are in m2, and when f returns -- True when applied to their respective values. isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> IntervalMap k a -> IntervalMap k b -> Bool -- | O(log n). Returns the smallest key and its associated value. -- Calls error if the map is empty. findMin :: IntervalMap k v -> (k, v) -- | O(log n). Returns the largest key and its associated value. -- Calls error if the map is empty. findMax :: IntervalMap k v -> (k, v) -- | Returns the key with the largest endpoint and its associated value. If -- there is more than one key with that endpoint, return the rightmost. -- -- O(n), since all keys could have the same endpoint. O(log -- n) average case. findLast :: (Interval k e) => IntervalMap k v -> (k, v) -- | O(log n). Remove the smallest key from the map. Return the -- empty map if the map is empty. deleteMin :: (Interval k e, Ord k) => IntervalMap k v -> IntervalMap k v -- | O(log n). Remove the largest key from the map. Return the empty -- map if the map is empty. deleteMax :: (Interval k e, Ord k) => IntervalMap k v -> IntervalMap k v -- | O(log n). Delete and return the smallest key. deleteFindMin :: (Interval k e, Ord k) => IntervalMap k v -> ((k, v), IntervalMap k v) -- | O(log n). Delete and return the largest key. deleteFindMax :: (Interval k e, Ord k) => IntervalMap k v -> ((k, v), IntervalMap k v) -- | O(log n). Update or delete value at minimum key. updateMin :: (Interval k e, Ord k) => (v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update or delete value at maximum key. updateMax :: (Interval k e, Ord k) => (v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update or delete value at minimum key. updateMinWithKey :: (Interval k e, Ord k) => (k -> v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update or delete value at maximum key. updateMaxWithKey :: (Interval k e, Ord k) => (k -> v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Retrieves the value associated with minimal key of -- the map, and the map stripped of that element, or Nothing if -- passed an empty map. minView :: (Interval k e, Ord k) => IntervalMap k a -> Maybe (a, IntervalMap k a) -- | O(log n). Retrieves the value associated with maximal key of -- the map, and the map stripped of that element, or Nothing if -- passed an empty map. maxView :: (Interval k e, Ord k) => IntervalMap k a -> Maybe (a, IntervalMap k a) -- | O(log n). Retrieves the minimal (key,value) pair of the map, -- and the map stripped of that element, or Nothing if passed an -- empty map. -- --
-- minViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a") -- minViewWithKey empty == Nothing --minViewWithKey :: (Interval k e, Ord k) => IntervalMap k a -> Maybe ((k, a), IntervalMap k a) -- | O(log n). Retrieves the maximal (key,value) pair of the map, -- and the map stripped of that element, or Nothing if passed an -- empty map. maxViewWithKey :: (Interval k e, Ord k) => IntervalMap k a -> Maybe ((k, a), IntervalMap k a) -- | Check red-black-tree and interval search augmentation invariants. For -- testing/debugging only. valid :: (Interval i k, Ord i) => IntervalMap i v -> Bool -- | The height of the tree. For testing/debugging only. height :: IntervalMap k v -> Int -- | The maximum height of a red-black tree with the given number of nodes. -- For testing/debugging only. maxHeight :: Int -> Int -- | Tree statistics (size, height, maxHeight size). For testing/debugging -- only. showStats :: IntervalMap k a -> (Int, Int, Int) -- | An implementation of maps from intervals to values. The key intervals -- may overlap, and the implementation contains efficient search -- functions for all keys containing a point or overlapping an interval. -- Closed, open, and half-open intervals can be contained in the same -- map. -- -- The functions in this module are strict in both the keys and the -- values. If you need value-lazy maps, use Data.IntervalMap.Lazy -- instead. The IntervalMap type itself is shared between the lazy and -- strict modules, meaning that the same IntervalMap value can be passed -- to functions in both modules (although that is rarely needed). -- -- An IntervalMap cannot contain duplicate keys - if you need to map a -- key to multiple values, use a collection as the value type, for -- example: IntervalMap k [v]. -- -- It is an error to insert an empty interval into a map. This -- precondition is not checked by the various construction functions. -- -- Since many function names (but not the type name) clash with -- Prelude names, this module is usually imported -- qualified, e.g. -- --
-- import Data.Generic.IntervalMap.Strict (IntervalMap) -- import qualified Data.Generic.IntervalMap.Strict as IM ---- -- It offers most of the same functions as Map, but the key type -- must be an instance of Interval. Some functions differ in -- asymptotic performance (for example size) or have not been -- tuned for efficiency as much as their equivalents in Map (in -- particular the various set functions). -- -- In addition, there are functions specific to maps of intervals, for -- example to search for all keys containing a given point or contained -- in a given interval. -- -- The implementation is a red-black tree augmented with the maximum -- upper bound of all keys. -- -- Parts of this implementation are based on code from the Map -- implementation, (c) Daan Leijen 2002, (c) Andriy Palamarchuk 2008. The -- red-black tree deletion is based on code from llrbtree by Kazu -- Yamamoto. Of course, any errors are mine. module Data.IntervalMap.Generic.Strict -- | Intervals with endpoints of type e. A minimal instance -- declaration for a closed interval needs only to define -- lowerBound and upperBound. class Ord e => Interval i e | i -> e where leftClosed _ = True rightClosed _ = True a `before` b = upperBound a < lowerBound b || (upperBound a == lowerBound b && not (rightClosed a && leftClosed b)) a `after` b = b `before` a a `subsumes` b = (lowerBound a < lowerBound b || (lowerBound a == lowerBound b && (leftClosed a || not (leftClosed b)))) && (upperBound a > upperBound b || (upperBound a == upperBound b && (rightClosed a || not (rightClosed b)))) a `overlaps` b = (lowerBound a < upperBound b || (lowerBound a == upperBound b && leftClosed a && rightClosed b)) && (upperBound a > lowerBound b || (upperBound a == lowerBound b && rightClosed a && leftClosed b)) p `below` i = case compare p (lowerBound i) of { LT -> True EQ -> not (leftClosed i) GT -> False } p `above` i = case compare p (upperBound i) of { LT -> False EQ -> not (rightClosed i) GT -> True } p `inside` i = not ((p `above` i) || (p `below` i)) isEmpty i | leftClosed i && rightClosed i = lowerBound i > upperBound i | otherwise = lowerBound i >= upperBound i -- | lower bound lowerBound :: Interval i e => i -> e -- | upper bound upperBound :: Interval i e => i -> e -- | Does the interval include its lower bound? Default is True for all -- values, i.e. closed intervals. leftClosed :: Interval i e => i -> Bool -- | Does the interval include its upper bound bound? Default is True for -- all values, i.e. closed intervals. rightClosed :: Interval i e => i -> Bool -- | Interval strictly before another? True if the upper bound of the first -- interval is below the lower bound of the second. before :: Interval i e => i -> i -> Bool -- | Interval strictly after another? Same as 'flip before'. after :: Interval i e => i -> i -> Bool -- | Does the first interval completely contain the second? subsumes :: Interval i e => i -> i -> Bool -- | Do the two intervals overlap? overlaps :: Interval i e => i -> i -> Bool -- | Is a point strictly less than lower bound? below :: Interval i e => e -> i -> Bool -- | Is a point strictly greater than upper bound? above :: Interval i e => e -> i -> Bool -- | Does the interval contain a given point? inside :: Interval i e => e -> i -> Bool -- | Is the interval empty? isEmpty :: Interval i e => i -> Bool -- | A map from intervals of type k to values of type v. data IntervalMap k v -- | O(log n). Lookup value for given key. Calls error if the -- key is not in the map. -- -- Use lookup or findWithDefault instead of this function, -- unless you are absolutely sure that the key is present in the map. (!) :: (Interval k e, Ord k) => IntervalMap k v -> k -> v -- | Same as difference. (\\) :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(1). Is the map empty? null :: IntervalMap k v -> Bool -- | O(n). Number of keys in the map. -- -- Caution: unlike size, which takes constant time, this is linear -- in the number of keys! size :: IntervalMap k v -> Int -- | O(log n). Does the map contain the given key? See also -- notMember. member :: (Ord k) => k -> IntervalMap k v -> Bool -- | O(log n). Does the map not contain the given key? See also -- member. notMember :: (Ord k) => k -> IntervalMap k v -> Bool -- | O(log n). Look up the given key in the map, returning the value -- (Just value), or Nothing if the key is not in -- the map. lookup :: (Ord k) => k -> IntervalMap k v -> Maybe v -- | O(log n). The expression (findWithDefault def k -- map) returns the value at key k or returns default value -- def when the key is not in the map. -- --
-- findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x' -- findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a' --findWithDefault :: Ord k => a -> k -> IntervalMap k a -> a -- | Return the submap of key intervals containing the given point. This is -- the second element of the value of splitAt: -- --
-- m `containing` p == let (_,m',_) = m `splitAt` p in m' ---- -- O(n), since potentially all keys could contain the point. -- O(log n) average case. This is also the worst case for maps -- containing no overlapping keys. containing :: (Interval k e) => IntervalMap k v -> e -> IntervalMap k v -- | Return the submap of key intervals overlapping (intersecting) the -- given interval. This is the second element of the value of -- splitIntersecting: -- --
-- m `intersecting` i == let (_,m',_) = m `splitIntersecting` i in m' ---- -- O(n), since potentially all keys could intersect the interval. -- O(log n) average case, if few keys intersect the interval. intersecting :: (Interval k e) => IntervalMap k v -> k -> IntervalMap k v -- | Return the submap of key intervals completely inside the given -- interval. -- -- O(n), since potentially all keys could be inside the interval. -- O(log n) average case, if few keys are inside the interval. within :: (Interval k e) => IntervalMap k v -> k -> IntervalMap k v -- | O(1). The empty map. empty :: IntervalMap k v -- | O(1). A map with one entry. singleton :: k -> v -> IntervalMap k v -- | O(log n). Insert a new key/value pair. If the map already -- contains the key, its value is changed to the new value. insert :: (Interval k e, Ord k) => k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Insert with a function, combining new value and old -- value. insertWith f key value mp will insert the pair -- (key, value) into mp if key does not exist in the map. If the -- key does exist, the function will insert the pair (key, f -- new_value old_value). insertWith :: (Interval k e, Ord k) => (v -> v -> v) -> k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Insert with a function, combining key, new value and -- old value. insertWithKey f key value mp will insert -- the pair (key, value) into mp if key does not exist in the -- map. If the key does exist, the function will insert the pair -- (key, f key new_value old_value). Note that the key passed to -- f is the same key passed to insertWithKey. insertWithKey :: (Interval k e, Ord k) => (k -> v -> v -> v) -> k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Combine insert with old values retrieval. insertLookupWithKey :: (Interval k e, Ord k) => (k -> v -> v -> v) -> k -> v -> IntervalMap k v -> (Maybe v, IntervalMap k v) -- | O(log n). Delete a key from the map. If the map does not -- contain the key, it is returned unchanged. delete :: (Interval k e, Ord k) => k -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update a value at a specific key with the result of -- the provided function. When the key is not a member of the map, the -- original map is returned. adjust :: Ord k => (a -> a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). Adjust a value at a specific key. When the key is not -- a member of the map, the original map is returned. adjustWithKey :: Ord k => (k -> a -> a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). The expression (update f k map) -- updates the value x at k (if it is in the map). If -- (f x) is Nothing, the element is deleted. If it is -- (Just y), the key k is bound to the new value -- y. update :: (Interval k e, Ord k) => (a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). The expression (updateWithKey f k -- map) updates the value x at k (if it is in the -- map). If (f k x) is Nothing, the element is deleted. -- If it is (Just y), the key k is bound to the -- new value y. updateWithKey :: (Interval k e, Ord k) => (k -> a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). Lookup and update. See also updateWithKey. The -- function returns changed value, if it is updated. Returns the original -- key value if the map entry is deleted. updateLookupWithKey :: (Interval k e, Ord k) => (k -> a -> Maybe a) -> k -> IntervalMap k a -> (Maybe a, IntervalMap k a) -- | O(log n). The expression (alter f k map) alters -- the value x at k, or absence thereof. alter -- can be used to insert, delete, or update a value in a Map. In -- short : lookup k (alter f k m) = f (lookup k -- m). alter :: (Interval k e, Ord k) => (Maybe a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(n+m). The expression (union t1 t2) takes the -- left-biased union of t1 and t2. It prefers -- t1 when duplicate keys are encountered, i.e. -- (union == unionWith const). union :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | O(n+m). Union with a combining function. unionWith :: (Interval k e, Ord k) => (a -> a -> a) -> IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | O(n+m). Union with a combining function. unionWithKey :: (Interval k e, Ord k) => (k -> a -> a -> a) -> IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | The union of a list of maps: (unions == foldl -- union empty). unions :: (Interval k e, Ord k) => [IntervalMap k a] -> IntervalMap k a -- | The union of a list of maps, with a combining operation: -- (unionsWith f == foldl (unionWith f) -- empty). unionsWith :: (Interval k e, Ord k) => (a -> a -> a) -> [IntervalMap k a] -> IntervalMap k a -- | O(n+m). Difference of two maps. Return elements of the first -- map not existing in the second map. difference :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Difference with a combining function. When two equal -- keys are encountered, the combining function is applied to the values -- of these keys. If it returns Nothing, the element is discarded -- (proper set difference). If it returns (Just y), the -- element is updated with a new value y. differenceWith :: (Interval k e, Ord k) => (a -> b -> Maybe a) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Difference with a combining function. When two equal -- keys are encountered, the combining function is applied to the key and -- both values. If it returns Nothing, the element is discarded -- (proper set difference). If it returns (Just y), the -- element is updated with a new value y. differenceWithKey :: (Interval k e, Ord k) => (k -> a -> b -> Maybe a) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Intersection of two maps. Return data in the first map -- for the keys existing in both maps. (intersection m1 m2 == -- intersectionWith const m1 m2). intersection :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Intersection with a combining function. intersectionWith :: (Interval k e, Ord k) => (a -> b -> c) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k c -- | O(n+m). Intersection with a combining function. intersectionWithKey :: (Interval k e, Ord k) => (k -> a -> b -> c) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k c -- | O(n). Map a function over all values in the map. map :: (a -> b) -> IntervalMap k a -> IntervalMap k b -- | O(n). Map a function over all values in the map. mapWithKey :: (k -> a -> b) -> IntervalMap k a -> IntervalMap k b -- | O(n). The function mapAccum threads an accumulating -- argument through the map in ascending order of keys. -- --
-- let f a b = (a ++ b, b ++ "X")
-- mapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
--
mapAccum :: (a -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c)
-- | O(n). The function mapAccumWithKey threads an
-- accumulating argument through the map in ascending order of keys.
--
--
-- let f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
-- mapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
--
mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c)
-- | O(n). The function mapAccumRWithKey threads an
-- accumulating argument through the map in descending order of keys.
mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c)
-- | O(n log n). mapKeys f s is the map obtained by
-- applying f to each key of s.
--
-- The size of the result may be smaller if f maps two or more
-- distinct keys to the same new key. In this case the value at the
-- smallest of these keys is retained.
mapKeys :: (Interval k2 e, Ord k2) => (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a
-- | O(n log n). mapKeysWith c f s is the map
-- obtained by applying f to each key of s.
--
-- The size of the result may be smaller if f maps two or more
-- distinct keys to the same new key. In this case the associated values
-- will be combined using c.
mapKeysWith :: (Interval k2 e, Ord k2) => (a -> a -> a) -> (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a
-- | O(n). mapKeysMonotonic f s == mapKeys f
-- s, but works only when f is strictly monotonic. That is,
-- for any values x and y, if x <
-- y then f x < f y. The precondition is
-- not checked.
mapKeysMonotonic :: (Interval k2 e, Ord k2) => (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a
-- | O(n). Fold the values in the map using the given
-- right-associative binary operator, such that foldr f z ==
-- foldr f z . elems.
foldr :: (a -> b -> b) -> b -> IntervalMap k a -> b
-- | O(n). Fold the values in the map using the given
-- left-associative binary operator, such that foldl f z ==
-- foldl f z . elems.
foldl :: (b -> a -> b) -> b -> IntervalMap k a -> b
-- | O(n). Fold the keys and values in the map using the given
-- right-associative binary operator, such that foldrWithKey f
-- z == foldr (uncurry f) z . toAscList.
foldrWithKey :: (k -> v -> a -> a) -> a -> IntervalMap k v -> a
-- | O(n). Fold the keys and values in the map using the given
-- left-associative binary operator, such that foldlWithKey f
-- z == foldl (\z' (kx, x) -> f z' kx x) z .
-- toAscList.
foldlWithKey :: (a -> k -> v -> a) -> a -> IntervalMap k v -> a
-- | O(n log n). Build a new map by combining successive key/value
-- pairs.
flattenWith :: (Ord k, Interval k e) => ((k, v) -> (k, v) -> Maybe (k, v)) -> IntervalMap k v -> IntervalMap k v
-- | O(n). Build a new map by combining successive key/value pairs.
-- Same as flattenWith, but works only when the combining
-- functions returns strictly monotonic key values.
flattenWithMonotonic :: (Interval k e) => ((k, v) -> (k, v) -> Maybe (k, v)) -> IntervalMap k v -> IntervalMap k v
-- | O(n). List of all values in the map, in ascending order of
-- their keys.
elems :: IntervalMap k v -> [v]
-- | O(n). List of all keys in the map, in ascending order.
keys :: IntervalMap k v -> [k]
-- | O(n). Set of the keys.
keysSet :: (Ord k) => IntervalMap k v -> Set k
-- | Same as toAscList.
assocs :: IntervalMap k v -> [(k, v)]
-- | O(n). The list of all key/value pairs contained in the map, in
-- no particular order.
toList :: IntervalMap k v -> [(k, v)]
-- | O(n log n). Build a map from a list of key/value pairs. See
-- also fromAscList. If the list contains more than one value for
-- the same key, the last value for the key is retained.
fromList :: (Interval k e, Ord k) => [(k, v)] -> IntervalMap k v
-- | O(n log n). Build a map from a list of key/value pairs with a
-- combining function. See also fromAscListWith.
fromListWith :: (Interval k e, Ord k) => (a -> a -> a) -> [(k, a)] -> IntervalMap k a
-- | O(n log n). Build a map from a list of key/value pairs with a
-- combining function. See also fromAscListWith.
fromListWithKey :: (Interval k e, Ord k) => (k -> a -> a -> a) -> [(k, a)] -> IntervalMap k a
-- | O(n). The list of all key/value pairs contained in the map, in
-- ascending order of keys.
toAscList :: IntervalMap k v -> [(k, v)]
-- | O(n). The list of all key/value pairs contained in the map, in
-- descending order of keys.
toDescList :: IntervalMap k v -> [(k, v)]
-- | O(n). Build a map from an ascending list in linear time. The
-- precondition (input list is ascending) is not checked.
fromAscList :: (Interval k e, Eq k) => [(k, v)] -> IntervalMap k v
-- | O(n). Build a map from an ascending list in linear time with a
-- combining function for equal keys. The precondition (input list is
-- ascending) is not checked.
fromAscListWith :: (Interval k e, Eq k) => (a -> a -> a) -> [(k, a)] -> IntervalMap k a
-- | O(n). Build a map from an ascending list in linear time with a
-- combining function for equal keys. The precondition (input list is
-- ascending) is not checked.
fromAscListWithKey :: (Interval k e, Eq k) => (k -> a -> a -> a) -> [(k, a)] -> IntervalMap k a
-- | O(n). Build a map from an ascending list of elements with
-- distinct keys in linear time. The precondition is not checked.
fromDistinctAscList :: (Interval k e) => [(k, v)] -> IntervalMap k v
-- | O(n). Filter values satisfying a predicate.
filter :: (Interval k e) => (a -> Bool) -> IntervalMap k a -> IntervalMap k a
-- | O(n). Filter keys/values satisfying a predicate.
filterWithKey :: (Interval k e) => (k -> a -> Bool) -> IntervalMap k a -> IntervalMap k a
-- | O(n). Partition the map according to a predicate. The first map
-- contains all elements that satisfy the predicate, the second all
-- elements that fail the predicate. See also split.
partition :: (Interval k e) => (a -> Bool) -> IntervalMap k a -> (IntervalMap k a, IntervalMap k a)
-- | O(n). Partition the map according to a predicate. The first map
-- contains all elements that satisfy the predicate, the second all
-- elements that fail the predicate. See also split.
partitionWithKey :: (Interval k e) => (k -> a -> Bool) -> IntervalMap k a -> (IntervalMap k a, IntervalMap k a)
-- | O(n). Map values and collect the Just results.
mapMaybe :: (Interval k e) => (a -> Maybe b) -> IntervalMap k a -> IntervalMap k b
-- | O(n). Map keys/values and collect the Just results.
mapMaybeWithKey :: (Interval k e) => (k -> a -> Maybe b) -> IntervalMap k a -> IntervalMap k b
-- | O(n). Map values and separate the Left and Right
-- results.
mapEither :: (Interval k e) => (a -> Either b c) -> IntervalMap k a -> (IntervalMap k b, IntervalMap k c)
-- | O(n). Map keys/values and separate the Left and
-- Right results.
mapEitherWithKey :: (Interval k e) => (k -> a -> Either b c) -> IntervalMap k a -> (IntervalMap k b, IntervalMap k c)
-- | O(n). The expression (split k map) is a pair
-- (map1,map2) where the keys in map1 are smaller than
-- k and the keys in map2 larger than k. Any
-- key equal to k is found in neither map1 nor
-- map2.
split :: (Interval i k, Ord i) => i -> IntervalMap i a -> (IntervalMap i a, IntervalMap i a)
-- | O(n). The expression (splitLookup k map) splits
-- a map just like split but also returns lookup k
-- map.
splitLookup :: (Interval i k, Ord i) => i -> IntervalMap i a -> (IntervalMap i a, Maybe a, IntervalMap i a)
-- | O(n). Split around a point. Splits the map into three submaps:
-- intervals below the point, intervals containing the point, and
-- intervals above the point.
splitAt :: (Interval i k, Ord i) => IntervalMap i a -> k -> (IntervalMap i a, IntervalMap i a, IntervalMap i a)
-- | O(n). Split around an interval. Splits the set into three
-- subsets: intervals below the given interval, intervals intersecting
-- the given interval, and intervals above the given interval.
splitIntersecting :: (Interval i k, Ord i) => IntervalMap i a -> i -> (IntervalMap i a, IntervalMap i a, IntervalMap i a)
-- | O(n+m). This function is defined as (isSubmapOf =
-- isSubmapOfBy (==)).
isSubmapOf :: (Ord k, Eq a) => IntervalMap k a -> IntervalMap k a -> Bool
-- | O(n+m). The expression (isSubmapOfBy f t1 t2)
-- returns True if all keys in t1 are in tree
-- t2, and f returns True when applied to their
-- respective values.
isSubmapOfBy :: Ord k => (a -> b -> Bool) -> IntervalMap k a -> IntervalMap k b -> Bool
-- | O(n+m). Is this a proper submap? (ie. a submap but not equal).
-- Defined as (isProperSubmapOf = isProperSubmapOfBy
-- (==)).
isProperSubmapOf :: (Ord k, Eq a) => IntervalMap k a -> IntervalMap k a -> Bool
-- | O(n+m). Is this a proper submap? (ie. a submap but not equal).
-- The expression (isProperSubmapOfBy f m1 m2) returns
-- True when m1 and m2 are not equal, all keys
-- in m1 are in m2, and when f returns
-- True when applied to their respective values.
isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> IntervalMap k a -> IntervalMap k b -> Bool
-- | O(log n). Returns the smallest key and its associated value.
-- Calls error if the map is empty.
findMin :: IntervalMap k v -> (k, v)
-- | O(log n). Returns the largest key and its associated value.
-- Calls error if the map is empty.
findMax :: IntervalMap k v -> (k, v)
-- | Returns the key with the largest endpoint and its associated value. If
-- there is more than one key with that endpoint, return the rightmost.
--
-- O(n), since all keys could have the same endpoint. O(log
-- n) average case.
findLast :: (Interval k e) => IntervalMap k v -> (k, v)
-- | O(log n). Remove the smallest key from the map. Return the
-- empty map if the map is empty.
deleteMin :: (Interval k e, Ord k) => IntervalMap k v -> IntervalMap k v
-- | O(log n). Remove the largest key from the map. Return the empty
-- map if the map is empty.
deleteMax :: (Interval k e, Ord k) => IntervalMap k v -> IntervalMap k v
-- | O(log n). Delete and return the smallest key.
deleteFindMin :: (Interval k e, Ord k) => IntervalMap k v -> ((k, v), IntervalMap k v)
-- | O(log n). Delete and return the largest key.
deleteFindMax :: (Interval k e, Ord k) => IntervalMap k v -> ((k, v), IntervalMap k v)
-- | O(log n). Update or delete value at minimum key.
updateMin :: (Interval k e, Ord k) => (v -> Maybe v) -> IntervalMap k v -> IntervalMap k v
-- | O(log n). Update or delete value at maximum key.
updateMax :: (Interval k e, Ord k) => (v -> Maybe v) -> IntervalMap k v -> IntervalMap k v
-- | O(log n). Update or delete value at minimum key.
updateMinWithKey :: (Interval k e, Ord k) => (k -> v -> Maybe v) -> IntervalMap k v -> IntervalMap k v
-- | O(log n). Update or delete value at maximum key.
updateMaxWithKey :: (Interval k e, Ord k) => (k -> v -> Maybe v) -> IntervalMap k v -> IntervalMap k v
-- | O(log n). Retrieves the value associated with minimal key of
-- the map, and the map stripped of that element, or Nothing if
-- passed an empty map.
minView :: (Interval k e, Ord k) => IntervalMap k a -> Maybe (a, IntervalMap k a)
-- | O(log n). Retrieves the value associated with maximal key of
-- the map, and the map stripped of that element, or Nothing if
-- passed an empty map.
maxView :: (Interval k e, Ord k) => IntervalMap k a -> Maybe (a, IntervalMap k a)
-- | O(log n). Retrieves the minimal (key,value) pair of the map,
-- and the map stripped of that element, or Nothing if passed an
-- empty map.
--
-- -- minViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a") -- minViewWithKey empty == Nothing --minViewWithKey :: (Interval k e, Ord k) => IntervalMap k a -> Maybe ((k, a), IntervalMap k a) -- | O(log n). Retrieves the maximal (key,value) pair of the map, -- and the map stripped of that element, or Nothing if passed an -- empty map. maxViewWithKey :: (Interval k e, Ord k) => IntervalMap k a -> Maybe ((k, a), IntervalMap k a) -- | Check red-black-tree and interval search augmentation invariants. For -- testing/debugging only. valid :: (Interval i k, Ord i) => IntervalMap i v -> Bool -- | The height of the tree. For testing/debugging only. height :: IntervalMap k v -> Int -- | The maximum height of a red-black tree with the given number of nodes. -- For testing/debugging only. maxHeight :: Int -> Int -- | Tree statistics (size, height, maxHeight size). For testing/debugging -- only. showStats :: IntervalMap k a -> (Int, Int, Int) -- | An implementation of sets of intervals. The intervals may overlap, and -- the implementation contains efficient search functions for all -- intervals containing a point or overlapping a given interval. Closed, -- open, and half-open intervals can be contained in the same set. -- -- It is an error to insert an empty interval into a set. This -- precondition is not checked by the various construction functions. -- -- Since many function names (but not the type name) clash with -- Prelude names, this module is usually imported -- qualified, e.g. -- --
-- import Data.IntervalSet.Strict (IntervalSet) -- import qualified Data.IntervalSet.Strict as IS ---- -- It offers most of the same functions as Set, but the member -- type must be an instance of Interval. The findMin and -- findMax functions deviate from their set counterparts in being -- total and returning a Maybe value. Some functions differ in -- asymptotic performance (for example size) or have not been -- tuned for efficiency as much as their equivalents in Set. -- -- In addition, there are functions specific to sets of intervals, for -- example to search for all intervals containing a given point or -- contained in a given interval. -- -- The implementation is a red-black tree augmented with the maximum -- upper bound of all keys. -- -- Parts of this implementation are based on code from the Map -- implementation, (c) Daan Leijen 2002, (c) Andriy Palamarchuk 2008. The -- red-black tree deletion is based on code from llrbtree by Kazu -- Yamamoto. Of course, any errors are mine. module Data.IntervalSet -- | Intervals with endpoints of type e. A minimal instance -- declaration for a closed interval needs only to define -- lowerBound and upperBound. class Ord e => Interval i e | i -> e where leftClosed _ = True rightClosed _ = True a `before` b = upperBound a < lowerBound b || (upperBound a == lowerBound b && not (rightClosed a && leftClosed b)) a `after` b = b `before` a a `subsumes` b = (lowerBound a < lowerBound b || (lowerBound a == lowerBound b && (leftClosed a || not (leftClosed b)))) && (upperBound a > upperBound b || (upperBound a == upperBound b && (rightClosed a || not (rightClosed b)))) a `overlaps` b = (lowerBound a < upperBound b || (lowerBound a == upperBound b && leftClosed a && rightClosed b)) && (upperBound a > lowerBound b || (upperBound a == lowerBound b && rightClosed a && leftClosed b)) p `below` i = case compare p (lowerBound i) of { LT -> True EQ -> not (leftClosed i) GT -> False } p `above` i = case compare p (upperBound i) of { LT -> False EQ -> not (rightClosed i) GT -> True } p `inside` i = not ((p `above` i) || (p `below` i)) isEmpty i | leftClosed i && rightClosed i = lowerBound i > upperBound i | otherwise = lowerBound i >= upperBound i -- | lower bound lowerBound :: Interval i e => i -> e -- | upper bound upperBound :: Interval i e => i -> e -- | Does the interval include its lower bound? Default is True for all -- values, i.e. closed intervals. leftClosed :: Interval i e => i -> Bool -- | Does the interval include its upper bound bound? Default is True for -- all values, i.e. closed intervals. rightClosed :: Interval i e => i -> Bool -- | Interval strictly before another? True if the upper bound of the first -- interval is below the lower bound of the second. before :: Interval i e => i -> i -> Bool -- | Interval strictly after another? Same as 'flip before'. after :: Interval i e => i -> i -> Bool -- | Does the first interval completely contain the second? subsumes :: Interval i e => i -> i -> Bool -- | Do the two intervals overlap? overlaps :: Interval i e => i -> i -> Bool -- | Is a point strictly less than lower bound? below :: Interval i e => e -> i -> Bool -- | Is a point strictly greater than upper bound? above :: Interval i e => e -> i -> Bool -- | Does the interval contain a given point? inside :: Interval i e => e -> i -> Bool -- | Is the interval empty? isEmpty :: Interval i e => i -> Bool -- | A set of intervals of type k. data IntervalSet k Nil :: IntervalSet k Node :: !Color -> !k -> !k -> !(IntervalSet k) -> !(IntervalSet k) -> IntervalSet k -- | Same as difference. (\\) :: (Interval k e, Ord k) => IntervalSet k -> IntervalSet k -> IntervalSet k -- | O(1). Is the set empty? null :: IntervalSet k -> Bool -- | O(n). Number of keys in the set. -- -- Caution: unlike size, this takes linear time! size :: IntervalSet k -> Int -- | O(log n). Does the set contain the given value? See also -- notMember. member :: (Ord k) => k -> IntervalSet k -> Bool -- | O(log n). Does the set not contain the given value? See also -- member. notMember :: (Ord k) => k -> IntervalSet k -> Bool -- | Return the set of all intervals containing the given point. This is -- the second element of the value of splitAt: -- --
-- set `containing` p == let (_,s,_) = set `splitAt` p in s ---- -- O(n), since potentially all intervals could contain the point. -- O(log n) average case. This is also the worst case for sets -- containing no overlapping intervals. containing :: (Interval k e) => IntervalSet k -> e -> IntervalSet k -- | Return the set of all intervals overlapping (intersecting) the given -- interval. This is the second element of the result of -- splitIntersecting: -- --
-- set `intersecting` i == let (_,s,_) = set `splitIntersecting` i in s ---- -- O(n), since potentially all values could intersect the -- interval. O(log n) average case, if few values intersect the -- interval. intersecting :: (Interval k e) => IntervalSet k -> k -> IntervalSet k -- | Return the set of all intervals which are completely inside the given -- interval. -- -- O(n), since potentially all values could be inside the -- interval. O(log n) average case, if few keys are inside the -- interval. within :: (Interval k e) => IntervalSet k -> k -> IntervalSet k -- | O(1). The empty set. empty :: IntervalSet k -- | O(1). A set with one entry. singleton :: k -> IntervalSet k -- | O(log n). Insert a new value. If the set already contains an -- element equal to the value, it is replaced by the new value. insert :: (Interval k e, Ord k) => k -> IntervalSet k -> IntervalSet k -- | O(log n). Delete an element from the set. If the set does not -- contain the value, it is returned unchanged. delete :: (Interval k e, Ord k) => k -> IntervalSet k -> IntervalSet k -- | O(n+m). The expression (union t1 t2) takes the -- left-biased union of t1 and t2. It prefers -- t1 when duplicate elements are encountered. union :: (Interval k e, Ord k) => IntervalSet k -> IntervalSet k -> IntervalSet k -- | The union of a list of sets: (unions == foldl -- union empty). unions :: (Interval k e, Ord k) => [IntervalSet k] -> IntervalSet k -- | O(n+m). Difference of two sets. Return elements of the first -- set not existing in the second set. difference :: (Interval k e, Ord k) => IntervalSet k -> IntervalSet k -> IntervalSet k -- | O(n+m). Intersection of two sets. Return elements in the first -- set also existing in the second set. intersection :: (Interval k e, Ord k) => IntervalSet k -> IntervalSet k -> IntervalSet k -- | O(n log n). Map a function over all values in the set. -- -- The size of the result may be smaller if f maps two or more -- distinct elements to the same value. map :: (Interval a e1, Interval b e2, Ord b) => (a -> b) -> IntervalSet a -> IntervalSet b -- | O(n). mapMonotonic f s == map f s, but -- works only when f is strictly monotonic. That is, for any -- values x and y, if x < y then -- f x < f y. The precondition is not -- checked. mapMonotonic :: (Interval k2 e, Ord k2) => (k1 -> k2) -> IntervalSet k1 -> IntervalSet k2 -- | O(n). Fold the values in the set using the given -- right-associative binary operator, such that foldr f z == -- foldr f z . elems. foldr :: (k -> b -> b) -> b -> IntervalSet k -> b -- | O(n). Fold the values in the set using the given -- left-associative binary operator, such that foldl f z == -- foldl f z . elems. foldl :: (b -> k -> b) -> b -> IntervalSet k -> b -- | O(n). A strict version of foldl. Each application of the -- operator is evaluated before using the result in the next application. -- This function is strict in the starting value. foldl' :: (b -> k -> b) -> b -> IntervalSet k -> b -- | O(n). A strict version of foldr. Each application of the -- operator is evaluated before using the result in the next application. -- This function is strict in the starting value. foldr' :: (k -> b -> b) -> b -> IntervalSet k -> b -- | O(n log n). Build a new set by combining successive values. flattenWith :: (Ord a, Interval a e) => (a -> a -> Maybe a) -> IntervalSet a -> IntervalSet a -- | O(n). Build a new set by combining successive values. Same as -- flattenWith, but works only when the combining functions -- returns strictly monotonic values. flattenWithMonotonic :: (Interval a e) => (a -> a -> Maybe a) -> IntervalSet a -> IntervalSet a -- | O(n). List of all values in the set, in ascending order. elems :: IntervalSet k -> [k] -- | O(n). The list of all values in the set, in no particular -- order. toList :: IntervalSet k -> [k] -- | O(n log n). Build a set from a list of elements. See also -- fromAscList. If the list contains duplicate values, the last -- value is retained. fromList :: (Interval k e, Ord k) => [k] -> IntervalSet k -- | O(n). The list of all values contained in the set, in ascending -- order. toAscList :: IntervalSet k -> [k] -- | O(n). The list of all values in the set, in descending order. toDescList :: IntervalSet k -> [k] -- | O(n). Build a set from an ascending list in linear time. The -- precondition (input list is ascending) is not checked. fromAscList :: (Interval k e, Eq k) => [k] -> IntervalSet k -- | O(n). Build a set from an ascending list of distinct elements -- in linear time. The precondition is not checked. fromDistinctAscList :: (Interval k e) => [k] -> IntervalSet k -- | O(n). Filter values satisfying a predicate. filter :: (Interval k e) => (k -> Bool) -> IntervalSet k -> IntervalSet k -- | O(n). Partition the set according to a predicate. The first set -- contains all elements that satisfy the predicate, the second all -- elements that fail the predicate. See also split. partition :: (Interval k e) => (k -> Bool) -> IntervalSet k -> (IntervalSet k, IntervalSet k) -- | O(n). The expression (split k set) is a pair -- (set1,set2) where the elements in set1 are smaller -- than k and the elements in set2 larger than -- k. Any key equal to k is found in neither -- set1 nor set2. split :: (Interval i k, Ord i) => i -> IntervalSet i -> (IntervalSet i, IntervalSet i) -- | O(n). The expression (splitMember k set) splits -- a set just like split but also returns member k -- set. splitMember :: (Interval i k, Ord i) => i -> IntervalSet i -> (IntervalSet i, Bool, IntervalSet i) -- | O(n). Split around a point. Splits the set into three subsets: -- intervals below the point, intervals containing the point, and -- intervals above the point. splitAt :: (Interval i k, Ord i) => IntervalSet i -> k -> (IntervalSet i, IntervalSet i, IntervalSet i) -- | O(n). Split around an interval. Splits the set into three -- subsets: intervals below the given interval, intervals intersecting -- the given interval, and intervals above the given interval. splitIntersecting :: (Interval i k, Ord i) => IntervalSet i -> i -> (IntervalSet i, IntervalSet i, IntervalSet i) -- | O(n+m). Is the first set a subset of the second set? This is -- always true for equal sets. isSubsetOf :: (Ord k) => IntervalSet k -> IntervalSet k -> Bool -- | O(n+m). Is the first set a proper subset of the second set? -- (i.e. a subset but not equal). isProperSubsetOf :: (Ord k) => IntervalSet k -> IntervalSet k -> Bool -- | O(log n). Returns the minimal value in the set. findMin :: IntervalSet k -> Maybe k -- | O(log n). Returns the maximal value in the set. findMax :: IntervalSet k -> Maybe k -- | Returns the interval with the largest endpoint. If there is more than -- one interval with that endpoint, return the rightmost. -- -- O(n), since all intervals could have the same endpoint. -- O(log n) average case. findLast :: (Interval k e) => IntervalSet k -> Maybe k -- | O(log n). Remove the smallest element from the set. Return the -- empty set if the set is empty. deleteMin :: (Interval k e, Ord k) => IntervalSet k -> IntervalSet k -- | O(log n). Remove the largest element from the set. Return the -- empty set if the set is empty. deleteMax :: (Interval k e, Ord k) => IntervalSet k -> IntervalSet k -- | O(log n). Delete and return the smallest element. deleteFindMin :: (Interval k e, Ord k) => IntervalSet k -> (k, IntervalSet k) -- | O(log n). Delete and return the largest element. deleteFindMax :: (Interval k e, Ord k) => IntervalSet k -> (k, IntervalSet k) -- | O(log n). Retrieves the minimal element of the set, and the set -- stripped of that element, or Nothing if passed an empty set. minView :: (Interval k e, Ord k) => IntervalSet k -> Maybe (k, IntervalSet k) -- | O(log n). Retrieves the maximal element of the set, and the set -- stripped of that element, or Nothing if passed an empty set. maxView :: (Interval k e, Ord k) => IntervalSet k -> Maybe (k, IntervalSet k) -- | Check red-black-tree and interval search augmentation invariants. For -- testing/debugging only. valid :: (Interval i k, Ord i) => IntervalSet i -> Bool instance GHC.Classes.Eq Data.IntervalSet.Color instance GHC.Classes.Eq k => GHC.Classes.Eq (Data.IntervalSet.IntervalSet k) instance GHC.Classes.Ord k => GHC.Classes.Ord (Data.IntervalSet.IntervalSet k) instance (Data.IntervalMap.Generic.Interval.Interval i k, GHC.Classes.Ord i) => GHC.Base.Monoid (Data.IntervalSet.IntervalSet i) instance Data.Foldable.Foldable Data.IntervalSet.IntervalSet instance Control.DeepSeq.NFData k => Control.DeepSeq.NFData (Data.IntervalSet.IntervalSet k) instance (GHC.Classes.Ord k, GHC.Read.Read k, Data.IntervalMap.Generic.Interval.Interval i k, GHC.Classes.Ord i, GHC.Read.Read i) => GHC.Read.Read (Data.IntervalSet.IntervalSet i) instance GHC.Show.Show k => GHC.Show.Show (Data.IntervalSet.IntervalSet k) -- | An implementation of maps from intervals to values. The key intervals -- may overlap, and the implementation contains efficient search -- functions for all keys containing a point or overlapping an interval. -- Closed, open, and half-open intervals can be contained in the same -- map. -- -- The functions in this module are strict in both the keys and the -- values. If you need value-lazy maps, use Data.IntervalMap.Lazy -- instead. The IntervalMap type itself is shared between the lazy and -- strict modules, meaning that the same IntervalMap value can be passed -- to functions in both modules (although that is rarely needed). -- -- An IntervalMap cannot contain duplicate keys - if you need to map a -- key to multiple values, use a collection as the value type, for -- example: IntervalMap k [v]. -- -- It is an error to insert an empty interval into a map. This -- precondition is not checked by the various construction functions. -- -- Since many function names (but not the type name) clash with -- Prelude names, this module is usually imported -- qualified, e.g. -- --
-- import Data.IntervalMap (IvMap) -- import qualified Data.IntervalMap as IvMap ---- -- It offers most of the same functions as Map, but uses -- Interval k instead of just k as the key type. -- Some of the functions need stricter type constraints to maintain the -- additional information for efficient interval searching, for example -- fromDistinctAscList needs an Ord k constraint. -- Also, some functions differ in asymptotic performance (for example -- size) or have not been tuned for efficiency as much as their -- equivalents in Map (in particular the various set functions). -- -- In addition, there are functions specific to maps of intervals, for -- example to search for all keys containing a given point or contained -- in a given interval. -- -- To stay compatible with standard Haskell, this implementation uses a -- fixed data type for intervals, and not a multi-parameter type class. -- Thus, it's currently not possible to define e.g. a 2-tuple as an -- instance of interval and use that map key. Instead, you must convert -- your keys to Interval. -- -- The implementation is a red-black tree augmented with the maximum -- upper bound of all keys. -- -- Parts of this implementation are based on code from the Map -- implementation, (c) Daan Leijen 2002, (c) Andriy Palamarchuk 2008. The -- red-black tree deletion is based on code from llrbtree by Kazu -- Yamamoto. Of course, any errors are mine. module Data.IntervalMap.Strict -- | Intervals with endpoints of type a. -- -- Read and Show use mathematical notation with square -- brackets for closed and parens for open intervals. This is better for -- human readability, but is not a valid Haskell expression. Closed -- intervals look like a list, open intervals look like a tuple, and -- half-open intervals look like mismatched parens. data Interval a -- | Including lower bound, excluding upper IntervalCO :: !a -> !a -> Interval a -- | Closed at both ends ClosedInterval :: !a -> !a -> Interval a -- | Open at both ends OpenInterval :: !a -> !a -> Interval a -- | Excluding lower bound, including upper IntervalOC :: !a -> !a -> Interval a type IntervalMap k v = IntervalMap (Interval k) v -- | O(log n). Lookup value for given key. Calls error if the -- key is not in the map. -- -- Use lookup or findWithDefault instead of this function, -- unless you are absolutely sure that the key is present in the map. (!) :: (Interval k e, Ord k) => IntervalMap k v -> k -> v -- | Same as difference. (\\) :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | Test whether the structure is empty. The default implementation is -- optimized for structures that are similar to cons-lists, because there -- is no general way to do better. null :: Foldable t => forall a. t a -> Bool -- | O(n). Number of keys in the map. -- -- Caution: unlike size, which takes constant time, this is linear -- in the number of keys! size :: IntervalMap k v -> Int -- | O(log n). Does the map contain the given key? See also -- notMember. member :: (Ord k) => k -> IntervalMap k v -> Bool -- | O(log n). Does the map not contain the given key? See also -- member. notMember :: (Ord k) => k -> IntervalMap k v -> Bool -- | lookup key assocs looks up a key in an association -- list. lookup :: Eq a => a -> [(a, b)] -> Maybe b -- | O(log n). The expression (findWithDefault def k -- map) returns the value at key k or returns default value -- def when the key is not in the map. -- --
-- findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x' -- findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a' --findWithDefault :: Ord k => a -> k -> IntervalMap k a -> a -- | Return the submap of key intervals containing the given point. This is -- the second element of the value of splitAt: -- --
-- m `containing` p == let (_,m',_) = m `splitAt` p in m' ---- -- O(n), since potentially all keys could contain the point. -- O(log n) average case. This is also the worst case for maps -- containing no overlapping keys. containing :: (Interval k e) => IntervalMap k v -> e -> IntervalMap k v -- | Return the submap of key intervals overlapping (intersecting) the -- given interval. This is the second element of the value of -- splitIntersecting: -- --
-- m `intersecting` i == let (_,m',_) = m `splitIntersecting` i in m' ---- -- O(n), since potentially all keys could intersect the interval. -- O(log n) average case, if few keys intersect the interval. intersecting :: (Interval k e) => IntervalMap k v -> k -> IntervalMap k v -- | Return the submap of key intervals completely inside the given -- interval. -- -- O(n), since potentially all keys could be inside the interval. -- O(log n) average case, if few keys are inside the interval. within :: (Interval k e) => IntervalMap k v -> k -> IntervalMap k v -- | O(1). The empty map. empty :: IntervalMap k v -- | O(1). A map with one entry. singleton :: k -> v -> IntervalMap k v -- | O(log n). Insert a new key/value pair. If the map already -- contains the key, its value is changed to the new value. insert :: (Interval k e, Ord k) => k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Insert with a function, combining new value and old -- value. insertWith f key value mp will insert the pair -- (key, value) into mp if key does not exist in the map. If the -- key does exist, the function will insert the pair (key, f -- new_value old_value). insertWith :: (Interval k e, Ord k) => (v -> v -> v) -> k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Insert with a function, combining key, new value and -- old value. insertWithKey f key value mp will insert -- the pair (key, value) into mp if key does not exist in the -- map. If the key does exist, the function will insert the pair -- (key, f key new_value old_value). Note that the key passed to -- f is the same key passed to insertWithKey. insertWithKey :: (Interval k e, Ord k) => (k -> v -> v -> v) -> k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Combine insert with old values retrieval. insertLookupWithKey :: (Interval k e, Ord k) => (k -> v -> v -> v) -> k -> v -> IntervalMap k v -> (Maybe v, IntervalMap k v) -- | O(log n). Delete a key from the map. If the map does not -- contain the key, it is returned unchanged. delete :: (Interval k e, Ord k) => k -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update a value at a specific key with the result of -- the provided function. When the key is not a member of the map, the -- original map is returned. adjust :: Ord k => (a -> a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). Adjust a value at a specific key. When the key is not -- a member of the map, the original map is returned. adjustWithKey :: Ord k => (k -> a -> a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). The expression (update f k map) -- updates the value x at k (if it is in the map). If -- (f x) is Nothing, the element is deleted. If it is -- (Just y), the key k is bound to the new value -- y. update :: (Interval k e, Ord k) => (a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). The expression (updateWithKey f k -- map) updates the value x at k (if it is in the -- map). If (f k x) is Nothing, the element is deleted. -- If it is (Just y), the key k is bound to the -- new value y. updateWithKey :: (Interval k e, Ord k) => (k -> a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). Lookup and update. See also updateWithKey. The -- function returns changed value, if it is updated. Returns the original -- key value if the map entry is deleted. updateLookupWithKey :: (Interval k e, Ord k) => (k -> a -> Maybe a) -> k -> IntervalMap k a -> (Maybe a, IntervalMap k a) -- | O(log n). The expression (alter f k map) alters -- the value x at k, or absence thereof. alter -- can be used to insert, delete, or update a value in a Map. In -- short : lookup k (alter f k m) = f (lookup k -- m). alter :: (Interval k e, Ord k) => (Maybe a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(n+m). The expression (union t1 t2) takes the -- left-biased union of t1 and t2. It prefers -- t1 when duplicate keys are encountered, i.e. -- (union == unionWith const). union :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | O(n+m). Union with a combining function. unionWith :: (Interval k e, Ord k) => (a -> a -> a) -> IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | O(n+m). Union with a combining function. unionWithKey :: (Interval k e, Ord k) => (k -> a -> a -> a) -> IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | The union of a list of maps: (unions == foldl -- union empty). unions :: (Interval k e, Ord k) => [IntervalMap k a] -> IntervalMap k a -- | The union of a list of maps, with a combining operation: -- (unionsWith f == foldl (unionWith f) -- empty). unionsWith :: (Interval k e, Ord k) => (a -> a -> a) -> [IntervalMap k a] -> IntervalMap k a -- | O(n+m). Difference of two maps. Return elements of the first -- map not existing in the second map. difference :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Difference with a combining function. When two equal -- keys are encountered, the combining function is applied to the values -- of these keys. If it returns Nothing, the element is discarded -- (proper set difference). If it returns (Just y), the -- element is updated with a new value y. differenceWith :: (Interval k e, Ord k) => (a -> b -> Maybe a) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Difference with a combining function. When two equal -- keys are encountered, the combining function is applied to the key and -- both values. If it returns Nothing, the element is discarded -- (proper set difference). If it returns (Just y), the -- element is updated with a new value y. differenceWithKey :: (Interval k e, Ord k) => (k -> a -> b -> Maybe a) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Intersection of two maps. Return data in the first map -- for the keys existing in both maps. (intersection m1 m2 == -- intersectionWith const m1 m2). intersection :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Intersection with a combining function. intersectionWith :: (Interval k e, Ord k) => (a -> b -> c) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k c -- | O(n+m). Intersection with a combining function. intersectionWithKey :: (Interval k e, Ord k) => (k -> a -> b -> c) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k c -- | map f xs is the list obtained by applying f -- to each element of xs, i.e., -- --
-- map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn] -- map f [x1, x2, ...] == [f x1, f x2, ...] --map :: (a -> b) -> [a] -> [b] -- | O(n). Map a function over all values in the map. mapWithKey :: (k -> a -> b) -> IntervalMap k a -> IntervalMap k b -- | O(n). The function mapAccum threads an accumulating -- argument through the map in ascending order of keys. -- --
-- let f a b = (a ++ b, b ++ "X")
-- mapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
--
mapAccum :: (a -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c)
-- | O(n). The function mapAccumWithKey threads an
-- accumulating argument through the map in ascending order of keys.
--
--
-- let f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
-- mapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
--
mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c)
-- | O(n). The function mapAccumRWithKey threads an
-- accumulating argument through the map in descending order of keys.
mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c)
-- | O(n log n). mapKeys f s is the map obtained by
-- applying f to each key of s.
--
-- The size of the result may be smaller if f maps two or more
-- distinct keys to the same new key. In this case the value at the
-- smallest of these keys is retained.
mapKeys :: (Interval k2 e, Ord k2) => (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a
-- | O(n log n). mapKeysWith c f s is the map
-- obtained by applying f to each key of s.
--
-- The size of the result may be smaller if f maps two or more
-- distinct keys to the same new key. In this case the associated values
-- will be combined using c.
mapKeysWith :: (Interval k2 e, Ord k2) => (a -> a -> a) -> (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a
-- | O(n). mapKeysMonotonic f s == mapKeys f
-- s, but works only when f is strictly monotonic. That is,
-- for any values x and y, if x <
-- y then f x < f y. The precondition is
-- not checked.
mapKeysMonotonic :: (Interval k2 e, Ord k2) => (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a
-- | Right-associative fold of a structure.
--
-- -- foldr f z = foldr f z . toList --foldr :: Foldable t => forall a b. (a -> b -> b) -> b -> t a -> b -- | Left-associative fold of a structure. -- --
-- foldl f z = foldl f z . toList --foldl :: Foldable t => forall b a. (b -> a -> b) -> b -> t a -> b -- | O(n). Fold the keys and values in the map using the given -- right-associative binary operator, such that foldrWithKey f -- z == foldr (uncurry f) z . toAscList. foldrWithKey :: (k -> v -> a -> a) -> a -> IntervalMap k v -> a -- | O(n). Fold the keys and values in the map using the given -- left-associative binary operator, such that foldlWithKey f -- z == foldl (\z' (kx, x) -> f z' kx x) z . -- toAscList. foldlWithKey :: (a -> k -> v -> a) -> a -> IntervalMap k v -> a -- | O(n). Combine overlapping intervals. flattenWith :: (Ord k) => (v -> v -> v) -> IntervalMap k v -> IntervalMap k v -- | O(n). List of all values in the map, in ascending order of -- their keys. elems :: IntervalMap k v -> [v] -- | O(n). List of all keys in the map, in ascending order. keys :: IntervalMap k v -> [k] -- | O(n). Set of the keys. keysSet :: (Ord k) => IntervalMap k v -> Set k -- | Same as toAscList. assocs :: IntervalMap k v -> [(k, v)] -- | O(n). The list of all key/value pairs contained in the map, in -- no particular order. toList :: IntervalMap k v -> [(k, v)] -- | O(n log n). Build a map from a list of key/value pairs. See -- also fromAscList. If the list contains more than one value for -- the same key, the last value for the key is retained. fromList :: (Interval k e, Ord k) => [(k, v)] -> IntervalMap k v -- | O(n log n). Build a map from a list of key/value pairs with a -- combining function. See also fromAscListWith. fromListWith :: (Interval k e, Ord k) => (a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n log n). Build a map from a list of key/value pairs with a -- combining function. See also fromAscListWith. fromListWithKey :: (Interval k e, Ord k) => (k -> a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n). The list of all key/value pairs contained in the map, in -- ascending order of keys. toAscList :: IntervalMap k v -> [(k, v)] -- | O(n). The list of all key/value pairs contained in the map, in -- descending order of keys. toDescList :: IntervalMap k v -> [(k, v)] -- | O(n). Build a map from an ascending list in linear time. The -- precondition (input list is ascending) is not checked. fromAscList :: (Interval k e, Eq k) => [(k, v)] -> IntervalMap k v -- | O(n). Build a map from an ascending list in linear time with a -- combining function for equal keys. The precondition (input list is -- ascending) is not checked. fromAscListWith :: (Interval k e, Eq k) => (a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n). Build a map from an ascending list in linear time with a -- combining function for equal keys. The precondition (input list is -- ascending) is not checked. fromAscListWithKey :: (Interval k e, Eq k) => (k -> a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n). Build a map from an ascending list of elements with -- distinct keys in linear time. The precondition is not checked. fromDistinctAscList :: (Interval k e) => [(k, v)] -> IntervalMap k v -- | filter, applied to a predicate and a list, returns the list of -- those elements that satisfy the predicate; i.e., -- --
-- filter p xs = [ x | x <- xs, p x] --filter :: (a -> Bool) -> [a] -> [a] -- | O(n). Filter keys/values satisfying a predicate. filterWithKey :: (Interval k e) => (k -> a -> Bool) -> IntervalMap k a -> IntervalMap k a -- | O(n). Partition the map according to a predicate. The first map -- contains all elements that satisfy the predicate, the second all -- elements that fail the predicate. See also split. partition :: (Interval k e) => (a -> Bool) -> IntervalMap k a -> (IntervalMap k a, IntervalMap k a) -- | O(n). Partition the map according to a predicate. The first map -- contains all elements that satisfy the predicate, the second all -- elements that fail the predicate. See also split. partitionWithKey :: (Interval k e) => (k -> a -> Bool) -> IntervalMap k a -> (IntervalMap k a, IntervalMap k a) -- | O(n). Map values and collect the Just results. mapMaybe :: (Interval k e) => (a -> Maybe b) -> IntervalMap k a -> IntervalMap k b -- | O(n). Map keys/values and collect the Just results. mapMaybeWithKey :: (Interval k e) => (k -> a -> Maybe b) -> IntervalMap k a -> IntervalMap k b -- | O(n). Map values and separate the Left and Right -- results. mapEither :: (Interval k e) => (a -> Either b c) -> IntervalMap k a -> (IntervalMap k b, IntervalMap k c) -- | O(n). Map keys/values and separate the Left and -- Right results. mapEitherWithKey :: (Interval k e) => (k -> a -> Either b c) -> IntervalMap k a -> (IntervalMap k b, IntervalMap k c) -- | O(n). The expression (split k map) is a pair -- (map1,map2) where the keys in map1 are smaller than -- k and the keys in map2 larger than k. Any -- key equal to k is found in neither map1 nor -- map2. split :: (Interval i k, Ord i) => i -> IntervalMap i a -> (IntervalMap i a, IntervalMap i a) -- | O(n). The expression (splitLookup k map) splits -- a map just like split but also returns lookup k -- map. splitLookup :: (Interval i k, Ord i) => i -> IntervalMap i a -> (IntervalMap i a, Maybe a, IntervalMap i a) -- | O(n). Split around a point. Splits the map into three submaps: -- intervals below the point, intervals containing the point, and -- intervals above the point. splitAt :: (Interval i k, Ord i) => IntervalMap i a -> k -> (IntervalMap i a, IntervalMap i a, IntervalMap i a) -- | O(n). Split around an interval. Splits the set into three -- subsets: intervals below the given interval, intervals intersecting -- the given interval, and intervals above the given interval. splitIntersecting :: (Interval i k, Ord i) => IntervalMap i a -> i -> (IntervalMap i a, IntervalMap i a, IntervalMap i a) -- | O(n+m). This function is defined as (isSubmapOf = -- isSubmapOfBy (==)). isSubmapOf :: (Ord k, Eq a) => IntervalMap k a -> IntervalMap k a -> Bool -- | O(n+m). The expression (isSubmapOfBy f t1 t2) -- returns True if all keys in t1 are in tree -- t2, and f returns True when applied to their -- respective values. isSubmapOfBy :: Ord k => (a -> b -> Bool) -> IntervalMap k a -> IntervalMap k b -> Bool -- | O(n+m). Is this a proper submap? (ie. a submap but not equal). -- Defined as (isProperSubmapOf = isProperSubmapOfBy -- (==)). isProperSubmapOf :: (Ord k, Eq a) => IntervalMap k a -> IntervalMap k a -> Bool -- | O(n+m). Is this a proper submap? (ie. a submap but not equal). -- The expression (isProperSubmapOfBy f m1 m2) returns -- True when m1 and m2 are not equal, all keys -- in m1 are in m2, and when f returns -- True when applied to their respective values. isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> IntervalMap k a -> IntervalMap k b -> Bool -- | O(log n). Returns the smallest key and its associated value. -- Calls error if the map is empty. findMin :: IntervalMap k v -> (k, v) -- | O(log n). Returns the largest key and its associated value. -- Calls error if the map is empty. findMax :: IntervalMap k v -> (k, v) -- | Returns the key with the largest endpoint and its associated value. If -- there is more than one key with that endpoint, return the rightmost. -- -- O(n), since all keys could have the same endpoint. O(log -- n) average case. findLast :: (Interval k e) => IntervalMap k v -> (k, v) -- | O(log n). Remove the smallest key from the map. Return the -- empty map if the map is empty. deleteMin :: (Interval k e, Ord k) => IntervalMap k v -> IntervalMap k v -- | O(log n). Remove the largest key from the map. Return the empty -- map if the map is empty. deleteMax :: (Interval k e, Ord k) => IntervalMap k v -> IntervalMap k v -- | O(log n). Delete and return the smallest key. deleteFindMin :: (Interval k e, Ord k) => IntervalMap k v -> ((k, v), IntervalMap k v) -- | O(log n). Delete and return the largest key. deleteFindMax :: (Interval k e, Ord k) => IntervalMap k v -> ((k, v), IntervalMap k v) -- | O(log n). Update or delete value at minimum key. updateMin :: (Interval k e, Ord k) => (v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update or delete value at maximum key. updateMax :: (Interval k e, Ord k) => (v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update or delete value at minimum key. updateMinWithKey :: (Interval k e, Ord k) => (k -> v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update or delete value at maximum key. updateMaxWithKey :: (Interval k e, Ord k) => (k -> v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Retrieves the value associated with minimal key of -- the map, and the map stripped of that element, or Nothing if -- passed an empty map. minView :: (Interval k e, Ord k) => IntervalMap k a -> Maybe (a, IntervalMap k a) -- | O(log n). Retrieves the value associated with maximal key of -- the map, and the map stripped of that element, or Nothing if -- passed an empty map. maxView :: (Interval k e, Ord k) => IntervalMap k a -> Maybe (a, IntervalMap k a) -- | O(log n). Retrieves the minimal (key,value) pair of the map, -- and the map stripped of that element, or Nothing if passed an -- empty map. -- --
-- minViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a") -- minViewWithKey empty == Nothing --minViewWithKey :: (Interval k e, Ord k) => IntervalMap k a -> Maybe ((k, a), IntervalMap k a) -- | O(log n). Retrieves the maximal (key,value) pair of the map, -- and the map stripped of that element, or Nothing if passed an -- empty map. maxViewWithKey :: (Interval k e, Ord k) => IntervalMap k a -> Maybe ((k, a), IntervalMap k a) -- | Check red-black-tree and interval search augmentation invariants. For -- testing/debugging only. valid :: (Interval i k, Ord i) => IntervalMap i v -> Bool -- | The height of the tree. For testing/debugging only. height :: IntervalMap k v -> Int -- | The maximum height of a red-black tree with the given number of nodes. -- For testing/debugging only. maxHeight :: Int -> Int -- | Tree statistics (size, height, maxHeight size). For testing/debugging -- only. showStats :: IntervalMap k a -> (Int, Int, Int) -- | An implementation of maps from intervals to values. The key intervals -- may overlap, and the implementation contains efficient search -- functions for all keys containing a point or overlapping an interval. -- Closed, open, and half-open intervals can be contained in the same -- map. -- -- This module implements the same functions as -- Data.IntervalMap.Strict, but with value-lazy semantics. module Data.IntervalMap.Lazy -- | Intervals with endpoints of type a. -- -- Read and Show use mathematical notation with square -- brackets for closed and parens for open intervals. This is better for -- human readability, but is not a valid Haskell expression. Closed -- intervals look like a list, open intervals look like a tuple, and -- half-open intervals look like mismatched parens. data Interval a -- | Including lower bound, excluding upper IntervalCO :: !a -> !a -> Interval a -- | Closed at both ends ClosedInterval :: !a -> !a -> Interval a -- | Open at both ends OpenInterval :: !a -> !a -> Interval a -- | Excluding lower bound, including upper IntervalOC :: !a -> !a -> Interval a type IntervalMap k v = IntervalMap (Interval k) v -- | O(log n). Lookup value for given key. Calls error if the -- key is not in the map. -- -- Use lookup or findWithDefault instead of this function, -- unless you are absolutely sure that the key is present in the map. (!) :: (Interval k e, Ord k) => IntervalMap k v -> k -> v -- | Same as difference. (\\) :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(1). Is the map empty? null :: IntervalMap k v -> Bool -- | O(n). Number of keys in the map. -- -- Caution: unlike size, which takes constant time, this is linear -- in the number of keys! size :: IntervalMap k v -> Int -- | O(log n). Does the map contain the given key? See also -- notMember. member :: (Ord k) => k -> IntervalMap k v -> Bool -- | O(log n). Does the map not contain the given key? See also -- member. notMember :: (Ord k) => k -> IntervalMap k v -> Bool -- | O(log n). Look up the given key in the map, returning the value -- (Just value), or Nothing if the key is not in -- the map. lookup :: (Ord k) => k -> IntervalMap k v -> Maybe v -- | O(log n). The expression (findWithDefault def k -- map) returns the value at key k or returns default value -- def when the key is not in the map. findWithDefault :: Ord k => a -> k -> IntervalMap k a -> a -- | Return the submap of key intervals containing the given point. This is -- the second element of the value of splitAt: -- --
-- m `containing` p == let (_,m',_) = m `splitAt` p in m' ---- -- O(n), since potentially all keys could contain the point. -- O(log n) average case. This is also the worst case for maps -- containing no overlapping keys. containing :: (Interval k e) => IntervalMap k v -> e -> IntervalMap k v -- | Return the submap of key intervals overlapping (intersecting) the -- given interval. This is the second element of the value of -- splitIntersecting: -- --
-- m `intersecting` i == let (_,m',_) = m `splitIntersecting` i in m' ---- -- O(n), since potentially all keys could intersect the interval. -- O(log n) average case, if few keys intersect the interval. intersecting :: (Interval k e) => IntervalMap k v -> k -> IntervalMap k v -- | Return the submap of key intervals completely inside the given -- interval. -- -- O(n), since potentially all keys could be inside the interval. -- O(log n) average case, if few keys are inside the interval. within :: (Interval k e) => IntervalMap k v -> k -> IntervalMap k v -- | O(1). The empty map. empty :: IntervalMap k v -- | O(1). A map with one entry. singleton :: k -> v -> IntervalMap k v -- | O(log n). Insert a new key/value pair. If the map already -- contains the key, its value is changed to the new value. insert :: (Interval k e, Ord k) => k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Insert with a function, combining new value and old -- value. insertWith f key value mp will insert the pair -- (key, value) into mp if key does not exist in the map. If the -- key does exist, the function will insert the pair (key, f -- new_value old_value). insertWith :: (Interval k e, Ord k) => (v -> v -> v) -> k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Insert with a function, combining key, new value and -- old value. insertWithKey f key value mp will insert -- the pair (key, value) into mp if key does not exist in the -- map. If the key does exist, the function will insert the pair -- (key, f key new_value old_value). Note that the key passed to -- f is the same key passed to insertWithKey. insertWithKey :: (Interval k e, Ord k) => (k -> v -> v -> v) -> k -> v -> IntervalMap k v -> IntervalMap k v -- | O(log n). Combine insert with old values retrieval. insertLookupWithKey :: (Interval k e, Ord k) => (k -> v -> v -> v) -> k -> v -> IntervalMap k v -> (Maybe v, IntervalMap k v) -- | O(log n). Delete a key from the map. If the map does not -- contain the key, it is returned unchanged. delete :: (Interval k e, Ord k) => k -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update a value at a specific key with the result of -- the provided function. When the key is not a member of the map, the -- original map is returned. adjust :: Ord k => (a -> a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). Adjust a value at a specific key. When the key is not -- a member of the map, the original map is returned. adjustWithKey :: Ord k => (k -> a -> a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). The expression (update f k map) -- updates the value x at k (if it is in the map). If -- (f x) is Nothing, the element is deleted. If it is -- (Just y), the key k is bound to the new value -- y. update :: (Interval k e, Ord k) => (a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). The expression (updateWithKey f k -- map) updates the value x at k (if it is in the -- map). If (f k x) is Nothing, the element is deleted. -- If it is (Just y), the key k is bound to the -- new value y. updateWithKey :: (Interval k e, Ord k) => (k -> a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(log n). Lookup and update. See also updateWithKey. The -- function returns changed value, if it is updated. Returns the original -- key value if the map entry is deleted. updateLookupWithKey :: (Interval k e, Ord k) => (k -> a -> Maybe a) -> k -> IntervalMap k a -> (Maybe a, IntervalMap k a) -- | O(log n). The expression (alter f k map) alters -- the value x at k, or absence thereof. alter -- can be used to insert, delete, or update a value in a Map. In -- short : lookup k (alter f k m) = f (lookup k -- m). alter :: (Interval k e, Ord k) => (Maybe a -> Maybe a) -> k -> IntervalMap k a -> IntervalMap k a -- | O(n+m). The expression (union t1 t2) takes the -- left-biased union of t1 and t2. It prefers -- t1 when duplicate keys are encountered, i.e. -- (union == unionWith const). union :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | O(n+m). Union with a combining function. unionWith :: (Interval k e, Ord k) => (a -> a -> a) -> IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | O(n+m). Union with a combining function. unionWithKey :: (Interval k e, Ord k) => (k -> a -> a -> a) -> IntervalMap k a -> IntervalMap k a -> IntervalMap k a -- | The union of a list of maps: (unions == foldl -- union empty). unions :: (Interval k e, Ord k) => [IntervalMap k a] -> IntervalMap k a -- | The union of a list of maps, with a combining operation: -- (unionsWith f == foldl (unionWith f) -- empty). unionsWith :: (Interval k e, Ord k) => (a -> a -> a) -> [IntervalMap k a] -> IntervalMap k a -- | O(n+m). Difference of two maps. Return elements of the first -- map not existing in the second map. difference :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Difference with a combining function. When two equal -- keys are encountered, the combining function is applied to the values -- of these keys. If it returns Nothing, the element is discarded -- (proper set difference). If it returns (Just y), the -- element is updated with a new value y. differenceWith :: (Interval k e, Ord k) => (a -> b -> Maybe a) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Difference with a combining function. When two equal -- keys are encountered, the combining function is applied to the key and -- both values. If it returns Nothing, the element is discarded -- (proper set difference). If it returns (Just y), the -- element is updated with a new value y. differenceWithKey :: (Interval k e, Ord k) => (k -> a -> b -> Maybe a) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Intersection of two maps. Return data in the first map -- for the keys existing in both maps. (intersection m1 m2 == -- intersectionWith const m1 m2). intersection :: (Interval k e, Ord k) => IntervalMap k a -> IntervalMap k b -> IntervalMap k a -- | O(n+m). Intersection with a combining function. intersectionWith :: (Interval k e, Ord k) => (a -> b -> c) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k c -- | O(n+m). Intersection with a combining function. intersectionWithKey :: (Interval k e, Ord k) => (k -> a -> b -> c) -> IntervalMap k a -> IntervalMap k b -> IntervalMap k c -- | O(n). Map a function over all values in the map. map :: (a -> b) -> IntervalMap k a -> IntervalMap k b -- | O(n). Map a function over all values in the map. mapWithKey :: (k -> a -> b) -> IntervalMap k a -> IntervalMap k b -- | O(n). The function mapAccum threads an accumulating -- argument through the map in ascending order of keys. mapAccum :: (a -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c) -- | O(n). The function mapAccumWithKey threads an -- accumulating argument through the map in ascending order of keys. mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c) -- | O(n). The function mapAccumRWithKey threads an -- accumulating argument through the map in descending order of keys. mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> IntervalMap k b -> (a, IntervalMap k c) -- | O(n log n). mapKeys f s is the map obtained by -- applying f to each key of s. -- -- The size of the result may be smaller if f maps two or more -- distinct keys to the same new key. In this case the value at the -- smallest of these keys is retained. mapKeys :: (Interval k2 e, Ord k2) => (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a -- | O(n log n). mapKeysWith c f s is the map -- obtained by applying f to each key of s. -- -- The size of the result may be smaller if f maps two or more -- distinct keys to the same new key. In this case the associated values -- will be combined using c. mapKeysWith :: (Interval k2 e, Ord k2) => (a -> a -> a) -> (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a -- | O(n). mapKeysMonotonic f s == mapKeys f -- s, but works only when f is strictly monotonic. That is, -- for any values x and y, if x < -- y then f x < f y. The precondition is -- not checked. mapKeysMonotonic :: (Interval k2 e, Ord k2) => (k1 -> k2) -> IntervalMap k1 a -> IntervalMap k2 a -- | O(n). Fold the values in the map using the given -- right-associative binary operator, such that foldr f z == -- foldr f z . elems. foldr :: (a -> b -> b) -> b -> IntervalMap k a -> b -- | O(n). Fold the values in the map using the given -- left-associative binary operator, such that foldl f z == -- foldl f z . elems. foldl :: (b -> a -> b) -> b -> IntervalMap k a -> b -- | O(n). Fold the keys and values in the map using the given -- right-associative binary operator, such that foldrWithKey f -- z == foldr (uncurry f) z . toAscList. foldrWithKey :: (k -> v -> a -> a) -> a -> IntervalMap k v -> a -- | O(n). Fold the keys and values in the map using the given -- left-associative binary operator, such that foldlWithKey f -- z == foldl (\z' (kx, x) -> f z' kx x) z . -- toAscList. foldlWithKey :: (a -> k -> v -> a) -> a -> IntervalMap k v -> a -- | O(n). The list of all key/value pairs contained in the map, in -- descending order of keys. flattenWith :: (Ord k) => (v -> v -> v) -> IntervalMap k v -> IntervalMap k v -- | O(n). List of all values in the map, in ascending order of -- their keys. elems :: IntervalMap k v -> [v] -- | O(n). List of all keys in the map, in ascending order. keys :: IntervalMap k v -> [k] -- | O(n). Set of the keys. keysSet :: (Ord k) => IntervalMap k v -> Set k -- | Same as toAscList. assocs :: IntervalMap k v -> [(k, v)] -- | O(n). The list of all key/value pairs contained in the map, in -- no particular order. toList :: IntervalMap k v -> [(k, v)] -- | O(n log n). Build a map from a list of key/value pairs. See -- also fromAscList. If the list contains more than one value for -- the same key, the last value for the key is retained. fromList :: (Interval k e, Ord k) => [(k, v)] -> IntervalMap k v -- | O(n log n). Build a map from a list of key/value pairs with a -- combining function. See also fromAscListWith. fromListWith :: (Interval k e, Ord k) => (a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n log n). Build a map from a list of key/value pairs with a -- combining function. See also fromAscListWith. fromListWithKey :: (Interval k e, Ord k) => (k -> a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n). The list of all key/value pairs contained in the map, in -- ascending order of keys. toAscList :: IntervalMap k v -> [(k, v)] -- | O(n). The list of all key/value pairs contained in the map, in -- descending order of keys. toDescList :: IntervalMap k v -> [(k, v)] -- | O(n). Build a map from an ascending list in linear time. The -- precondition (input list is ascending) is not checked. fromAscList :: (Interval k e, Eq k) => [(k, v)] -> IntervalMap k v -- | O(n). Build a map from an ascending list in linear time with a -- combining function for equal keys. The precondition (input list is -- ascending) is not checked. fromAscListWith :: (Interval k e, Eq k) => (a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n). Build a map from an ascending list in linear time with a -- combining function for equal keys. The precondition (input list is -- ascending) is not checked. fromAscListWithKey :: (Interval k e, Eq k) => (k -> a -> a -> a) -> [(k, a)] -> IntervalMap k a -- | O(n). Build a map from an ascending list of elements with -- distinct keys in linear time. The precondition is not checked. fromDistinctAscList :: (Interval k e) => [(k, v)] -> IntervalMap k v -- | O(n). Filter values satisfying a predicate. filter :: (Interval k e) => (a -> Bool) -> IntervalMap k a -> IntervalMap k a -- | O(n). Filter keys/values satisfying a predicate. filterWithKey :: (Interval k e) => (k -> a -> Bool) -> IntervalMap k a -> IntervalMap k a -- | O(n). Partition the map according to a predicate. The first map -- contains all elements that satisfy the predicate, the second all -- elements that fail the predicate. See also split. partition :: (Interval k e) => (a -> Bool) -> IntervalMap k a -> (IntervalMap k a, IntervalMap k a) -- | O(n). Partition the map according to a predicate. The first map -- contains all elements that satisfy the predicate, the second all -- elements that fail the predicate. See also split. partitionWithKey :: (Interval k e) => (k -> a -> Bool) -> IntervalMap k a -> (IntervalMap k a, IntervalMap k a) -- | O(n). Map values and collect the Just results. mapMaybe :: (Interval k e) => (a -> Maybe b) -> IntervalMap k a -> IntervalMap k b -- | O(n). Map keys/values and collect the Just results. mapMaybeWithKey :: (Interval k e) => (k -> a -> Maybe b) -> IntervalMap k a -> IntervalMap k b -- | O(n). Map values and separate the Left and Right -- results. mapEither :: (Interval k e) => (a -> Either b c) -> IntervalMap k a -> (IntervalMap k b, IntervalMap k c) -- | O(n). Map keys/values and separate the Left and -- Right results. mapEitherWithKey :: (Interval i k) => (i -> a -> Either b c) -> IntervalMap i a -> (IntervalMap i b, IntervalMap i c) -- | O(n). The expression (split k map) is a pair -- (map1,map2) where the keys in map1 are smaller than -- k and the keys in map2 larger than k. Any -- key equal to k is found in neither map1 nor -- map2. split :: (Interval i k, Ord i) => i -> IntervalMap i a -> (IntervalMap i a, IntervalMap i a) -- | O(n). The expression (splitLookup k map) splits -- a map just like split but also returns lookup k -- map. splitLookup :: (Interval i k, Ord i) => i -> IntervalMap i a -> (IntervalMap i a, Maybe a, IntervalMap i a) -- | O(n). Split around a point. Splits the map into three submaps: -- intervals below the point, intervals containing the point, and -- intervals above the point. splitAt :: (Interval i k, Ord i) => IntervalMap i a -> k -> (IntervalMap i a, IntervalMap i a, IntervalMap i a) -- | O(n). Split around an interval. Splits the set into three -- subsets: intervals below the given interval, intervals intersecting -- the given interval, and intervals above the given interval. splitIntersecting :: (Interval i k, Ord i) => IntervalMap i a -> i -> (IntervalMap i a, IntervalMap i a, IntervalMap i a) -- | O(n+m). This function is defined as (isSubmapOf = -- isSubmapOfBy (==)). isSubmapOf :: (Ord k, Eq a) => IntervalMap k a -> IntervalMap k a -> Bool -- | O(n+m). The expression (isSubmapOfBy f t1 t2) -- returns True if all keys in t1 are in tree -- t2, and f returns True when applied to their -- respective values. isSubmapOfBy :: Ord k => (a -> b -> Bool) -> IntervalMap k a -> IntervalMap k b -> Bool -- | O(n+m). Is this a proper submap? (ie. a submap but not equal). -- Defined as (isProperSubmapOf = isProperSubmapOfBy -- (==)). isProperSubmapOf :: (Ord k, Eq a) => IntervalMap k a -> IntervalMap k a -> Bool -- | O(n+m). Is this a proper submap? (ie. a submap but not equal). -- The expression (isProperSubmapOfBy f m1 m2) returns -- True when m1 and m2 are not equal, all keys -- in m1 are in m2, and when f returns -- True when applied to their respective values. isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> IntervalMap k a -> IntervalMap k b -> Bool -- | O(log n). Returns the smallest key and its associated value. -- Calls error if the map is empty. findMin :: IntervalMap k v -> (k, v) -- | O(log n). Returns the largest key and its associated value. -- Calls error if the map is empty. findMax :: IntervalMap k v -> (k, v) -- | Returns the key with the largest endpoint and its associated value. If -- there is more than one key with that endpoint, return the rightmost. -- -- O(n), since all keys could have the same endpoint. O(log -- n) average case. findLast :: (Interval k e) => IntervalMap k v -> (k, v) -- | O(log n). Remove the smallest key from the map. Return the -- empty map if the map is empty. deleteMin :: (Interval k e, Ord k) => IntervalMap k v -> IntervalMap k v -- | O(log n). Remove the largest key from the map. Return the empty -- map if the map is empty. deleteMax :: (Interval k e, Ord k) => IntervalMap k v -> IntervalMap k v -- | O(log n). Delete and return the smallest key. deleteFindMin :: (Interval k e, Ord k) => IntervalMap k v -> ((k, v), IntervalMap k v) -- | O(log n). Delete and return the largest key. deleteFindMax :: (Interval k e, Ord k) => IntervalMap k v -> ((k, v), IntervalMap k v) -- | O(log n). Update or delete value at minimum key. updateMin :: (Interval k e, Ord k) => (v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update or delete value at maximum key. updateMax :: (Interval k e, Ord k) => (v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update or delete value at minimum key. updateMinWithKey :: (Interval k e, Ord k) => (k -> v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Update or delete value at maximum key. updateMaxWithKey :: (Interval k e, Ord k) => (k -> v -> Maybe v) -> IntervalMap k v -> IntervalMap k v -- | O(log n). Retrieves the value associated with minimal key of -- the map, and the map stripped of that element, or Nothing if -- passed an empty map. minView :: (Interval k e, Ord k) => IntervalMap k a -> Maybe (a, IntervalMap k a) -- | O(log n). Retrieves the value associated with maximal key of -- the map, and the map stripped of that element, or Nothing if -- passed an empty map. maxView :: (Interval k e, Ord k) => IntervalMap k a -> Maybe (a, IntervalMap k a) -- | O(log n). Retrieves the minimal (key,value) pair of the map, -- and the map stripped of that element, or Nothing if passed an -- empty map. -- --
-- minViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a") -- minViewWithKey empty == Nothing --minViewWithKey :: (Interval k e, Ord k) => IntervalMap k a -> Maybe ((k, a), IntervalMap k a) -- | O(log n). Retrieves the maximal (key,value) pair of the map, -- and the map stripped of that element, or Nothing if passed an -- empty map. maxViewWithKey :: (Interval k e, Ord k) => IntervalMap k a -> Maybe ((k, a), IntervalMap k a) -- | Check red-black-tree and interval search augmentation invariants. For -- testing/debugging only. valid :: (Interval i k, Ord i) => IntervalMap i v -> Bool -- | The height of the tree. For testing/debugging only. height :: IntervalMap k v -> Int -- | The maximum height of a red-black tree with the given number of nodes. -- For testing/debugging only. maxHeight :: Int -> Int -- | Tree statistics (size, height, maxHeight size). For testing/debugging -- only. showStats :: IntervalMap k a -> (Int, Int, Int) -- | An implementation of maps from intervals to values. The key intervals -- may overlap, and the implementation contains efficient search -- functions for all keys containing a point or overlapping an interval. -- Closed, open, and half-open intervals can be contained in the same -- map. -- -- This module re-exports the value lazy Data.IntervalMap.Lazy -- API, plus several value strict functions from -- Data.IntervalMap.Strict. module Data.IntervalMap -- | Deprecated. As of version 0.3, replaced by insertWith. -- -- O(log n). Same as insertWith, but the combining function -- is applied strictly. This is often the most desirable behavior. -- -- For example, to update a counter: -- --
-- insertWith' (+) k 1 m --insertWith' :: Ord k => (a -> a -> a) -> Interval k -> a -> IntervalMap k a -> IntervalMap k a -- | Deprecated. As of version 0.3, replaced by -- insertWithKey. -- -- O(log n). Same as insertWithKey, but the combining -- function is applied strictly. insertWithKey' :: Ord k => (Interval k -> a -> a -> a) -> Interval k -> a -> IntervalMap k a -> IntervalMap k a -- | Deprecated. As of version 0.3, replaced by -- insertLookupWithKey. -- -- O(log n). A strict version of insertLookupWithKey. insertLookupWithKey' :: Ord k => (Interval k -> a -> a -> a) -> Interval k -> a -> IntervalMap k a -> (Maybe a, IntervalMap k a) -- | Deprecated. As of version 0.5, replaced by foldr. -- -- O(n). Fold the values in the map using the given -- right-associative binary operator. This function is an equivalent of -- foldr and is present for compatibility only. fold :: (a -> b -> b) -> b -> IntervalMap k a -> b -- | Deprecated. As of version 0.3, replaced by foldrWithKey. -- -- O(n). Fold the keys and values in the map using the given -- right-associative binary operator. This function is an equivalent of -- foldrWithKey and is present for compatibility only. foldWithKey :: (Interval k -> a -> b -> b) -> b -> IntervalMap k a -> b