Copyright | (c) Justin Le 2018 |
---|---|
License | BSD3 |
Maintainer | justin@jle.im |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Unsafe internal-use functions used in the implementation of
Data.IntMap.NonEmpty. These functions can potentially be used to
break the abstraction of NEIntMap
and produce unsound maps, so be
wary!
Synopsis
- data NEIntMap a = NEIntMap {
- neimK0 :: !Key
- neimV0 :: a
- neimIntMap :: !(IntMap a)
- type Key = Int
- singleton :: Key -> a -> NEIntMap a
- nonEmptyMap :: IntMap a -> Maybe (NEIntMap a)
- withNonEmpty :: r -> (NEIntMap a -> r) -> IntMap a -> r
- fromList :: NonEmpty (Key, a) -> NEIntMap a
- toList :: NEIntMap a -> NonEmpty (Key, a)
- map :: (a -> b) -> NEIntMap a -> NEIntMap b
- insertWith :: (a -> a -> a) -> Key -> a -> NEIntMap a -> NEIntMap a
- union :: NEIntMap a -> NEIntMap a -> NEIntMap a
- unions :: Foldable1 f => f (NEIntMap a) -> NEIntMap a
- elems :: NEIntMap a -> NonEmpty a
- size :: NEIntMap a -> Int
- toMap :: NEIntMap a -> IntMap a
- foldr :: (a -> b -> b) -> b -> NEIntMap a -> b
- foldr' :: (a -> b -> b) -> b -> NEIntMap a -> b
- foldr1 :: (a -> a -> a) -> NEIntMap a -> a
- foldl :: (a -> b -> a) -> a -> NEIntMap b -> a
- foldl' :: (a -> b -> a) -> a -> NEIntMap b -> a
- foldl1 :: (a -> a -> a) -> NEIntMap a -> a
- traverseWithKey :: Applicative t => (Key -> a -> t b) -> NEIntMap a -> t (NEIntMap b)
- traverseWithKey1 :: Apply t => (Key -> a -> t b) -> NEIntMap a -> t (NEIntMap b)
- foldMapWithKey :: Semigroup m => (Key -> a -> m) -> NEIntMap a -> m
- traverseMapWithKey :: Applicative t => (Key -> a -> t b) -> IntMap a -> t (IntMap b)
- insertMinMap :: Key -> a -> IntMap a -> IntMap a
- insertMaxMap :: Key -> a -> IntMap a -> IntMap a
- valid :: NEIntMap a -> Bool
- lookupMinMap :: IntMap a -> Maybe (Key, a)
- lookupMaxMap :: IntMap a -> Maybe (Key, a)
Non-Empty IntMap type
A non-empty (by construction) map from integer keys to values a
. At
least one key-value pair exists in an
at all times.NEIntMap
v
Functions that take an NEIntMap
can safely operate on it with the
assumption that it has at least one key-value pair.
Functions that return an NEIntMap
provide an assurance that the result
has at least one key-value pair.
Data.IntMap.NonEmpty re-exports the API of Data.IntMap, faithfully
reproducing asymptotics, typeclass constraints, and semantics.
Functions that ensure that input and output maps are both non-empty
(like insert
) return NEIntMap
, but functions that
might potentially return an empty map (like delete
)
return a IntMap
instead.
You can directly construct an NEIntMap
with the API from
Data.IntMap.NonEmpty; it's more or less the same as constructing a normal
IntMap
, except you don't have access to empty
. There are also
a few ways to construct an NEIntMap
from a IntMap
:
- The
nonEmptyMap
smart constructor will convert a
into aIntMap
k a
, returningMaybe
(NEIntMap
k a)Nothing
if the originalIntMap
was empty. - You can use the
insertIntMap
family of functions to insert a value into aIntMap
to create a guaranteedNEIntMap
. - You can use the
IsNonEmpty
andIsEmpty
patterns to "pattern match" on aIntMap
to reveal it as either containing aNEIntMap
or an empty map. withNonEmpty
offers a continuation-based interface for deconstructing aIntMap
and treating it as if it were anNEIntMap
.
You can convert an NEIntMap
into a IntMap
with toMap
or
IsNonEmpty
, essentially "obscuring" the non-empty
property from the type.
NEIntMap | |
|
Instances
Functor NEIntMap Source # | |
Foldable NEIntMap Source # | Traverses elements in order of ascending keys. WARNING: |
Defined in Data.IntMap.NonEmpty.Internal fold :: Monoid m => NEIntMap m -> m # foldMap :: Monoid m => (a -> m) -> NEIntMap a -> m # foldMap' :: Monoid m => (a -> m) -> NEIntMap a -> m # foldr :: (a -> b -> b) -> b -> NEIntMap a -> b # foldr' :: (a -> b -> b) -> b -> NEIntMap a -> b # foldl :: (b -> a -> b) -> b -> NEIntMap a -> b # foldl' :: (b -> a -> b) -> b -> NEIntMap a -> b # foldr1 :: (a -> a -> a) -> NEIntMap a -> a # foldl1 :: (a -> a -> a) -> NEIntMap a -> a # elem :: Eq a => a -> NEIntMap a -> Bool # maximum :: Ord a => NEIntMap a -> a # minimum :: Ord a => NEIntMap a -> a # | |
Traversable NEIntMap Source # | Traverses elements in order of ascending keys WARNING: Different than for the |
Eq1 NEIntMap Source # | |
Ord1 NEIntMap Source # | |
Defined in Data.IntMap.NonEmpty.Internal | |
Read1 NEIntMap Source # | |
Defined in Data.IntMap.NonEmpty.Internal | |
Show1 NEIntMap Source # | |
Comonad NEIntMap Source # |
Since: 0.1.1.0 |
Alt NEIntMap Source # | Since: 0.3.4.4 |
Defined in Data.IntMap.NonEmpty.Internal | |
Invariant NEIntMap Source # | Since: 0.3.4.4 |
Defined in Data.IntMap.NonEmpty.Internal | |
Foldable1 NEIntMap Source # | Traverses elements in order of ascending keys WARNING: |
Defined in Data.IntMap.NonEmpty.Internal | |
Traversable1 NEIntMap Source # | Traverses elements in order of ascending keys WARNING: |
Eq a => Eq (NEIntMap a) Source # | |
Data a => Data (NEIntMap a) Source # | |
Defined in Data.IntMap.NonEmpty.Internal gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> NEIntMap a -> c (NEIntMap a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (NEIntMap a) # toConstr :: NEIntMap a -> Constr # dataTypeOf :: NEIntMap a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (NEIntMap a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (NEIntMap a)) # gmapT :: (forall b. Data b => b -> b) -> NEIntMap a -> NEIntMap a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> NEIntMap a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> NEIntMap a -> r # gmapQ :: (forall d. Data d => d -> u) -> NEIntMap a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> NEIntMap a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> NEIntMap a -> m (NEIntMap a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> NEIntMap a -> m (NEIntMap a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> NEIntMap a -> m (NEIntMap a) # | |
Ord a => Ord (NEIntMap a) Source # | |
Defined in Data.IntMap.NonEmpty.Internal | |
Read e => Read (NEIntMap e) Source # | |
Show a => Show (NEIntMap a) Source # | |
Semigroup (NEIntMap a) Source # | Left-biased union |
NFData a => NFData (NEIntMap a) Source # | |
Defined in Data.IntMap.NonEmpty.Internal | |
FromJSON a => FromJSON (NEIntMap a) Source # | |
Defined in Data.IntMap.NonEmpty.Internal parseJSON :: Value -> Parser (NEIntMap a) parseJSONList :: Value -> Parser [NEIntMap a] | |
ToJSON a => ToJSON (NEIntMap a) Source # | |
Defined in Data.IntMap.NonEmpty.Internal toEncoding :: NEIntMap a -> Encoding toJSONList :: [NEIntMap a] -> Value toEncodingList :: [NEIntMap a] -> Encoding |
singleton :: Key -> a -> NEIntMap a Source #
O(1). A map with a single element.
singleton 1 'a' == fromList ((1, 'a') :| []) size (singleton 1 'a') == 1
nonEmptyMap :: IntMap a -> Maybe (NEIntMap a) Source #
O(log n). Smart constructor for an NEIntMap
from a IntMap
. Returns
Nothing
if the IntMap
was originally actually empty, and
with an Just
nNEIntMap
, if the IntMap
was not empty.
nonEmptyMap
and
form an
isomorphism: they are perfect structure-preserving inverses of
eachother.maybe
empty
toMap
See IsNonEmpty
for a pattern synonym that lets you
"match on" the possiblity of a IntMap
being an NEIntMap
.
nonEmptyMap (Data.IntMap.fromList [(3,"a"), (5,"b")]) == Just (fromList ((3,"a") :| [(5,"b")]))
:: r | value to return if map is empty |
-> (NEIntMap a -> r) | function to apply if map is not empty |
-> IntMap a | |
-> r |
O(log n). A general continuation-based way to consume a IntMap
as if
it were an NEIntMap
.
will take a withNonEmpty
def fIntMap
. If map is
empty, it will evaluate to def
. Otherwise, a non-empty map NEIntMap
will be fed to the function f
instead.
nonEmptyMap
==withNonEmpty
Nothing
Just
fromList :: NonEmpty (Key, a) -> NEIntMap a Source #
O(n*log n). Build a non-empty map from a non-empty 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 ((5,"a") :| [(3,"b"), (5, "c")]) == fromList ((5,"c") :| [(3,"b")]) fromList ((5,"c") :| [(3,"b"), (5, "a")]) == fromList ((5,"a") :| [(3,"b")])
toList :: NEIntMap a -> NonEmpty (Key, a) Source #
O(n). Convert the map to a non-empty list of key/value pairs.
toList (fromList ((5,"a") :| [(3,"b")])) == ((3,"b") :| [(5,"a")])
map :: (a -> b) -> NEIntMap a -> NEIntMap b Source #
O(n). IntMap a function over all values in the map.
map (++ "x") (fromList ((5,"a") :| [(3,"b")])) == fromList ((3, "bx") :| [(5, "ax")])
insertWith :: (a -> a -> a) -> Key -> a -> NEIntMap a -> NEIntMap a Source #
O(log n). Insert with a function, combining new value and old value.
will insert the pair (key, value) into
insertWith
f key value mpmp
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)
.
See insertIntMapWith
for a version where the first
argument is a IntMap
.
insertWith (++) 5 "xxx" (fromList ((5,"a") :| [(3,"b")])) == fromList ((3, "b") :| [(5, "xxxa")]) insertWith (++) 7 "xxx" (fromList ((5,"a") :| [(3,"b")])) == fromList ((3, "b") :| [(5, "a"), (7, "xxx")])
union :: NEIntMap a -> NEIntMap a -> NEIntMap a Source #
O(m*log(n/m + 1)), m <= n.
The expression (
) takes the left-biased union of union
t1 t2t1
and
t2
. It prefers t1
when duplicate keys are encountered, i.e.
(
).union
== unionWith
const
union (fromList ((5, "a") :| [(3, "b")])) (fromList ((5, "A") :| [(7, "C")])) == fromList ((3, "b") :| [(5, "a"), (7, "C")])
unions :: Foldable1 f => f (NEIntMap a) -> NEIntMap a Source #
The left-biased union of a non-empty list of maps.
unions (fromList ((5, "a") :| [(3, "b")]) :| [fromList ((5, "A") :| [(7, "C")]), fromList ((5, "A3") :| [(3, "B3")])]) == fromList [(3, "b"), (5, "a"), (7, "C")] unions (fromList ((5, "A3") :| [(3, "B3")]) :| [fromList ((5, "A") :| [(7, "C")]), fromList ((5, "a") :| [(3, "b")])]) == fromList ((3, "B3") :| [(5, "A3"), (7, "C")])
elems :: NEIntMap a -> NonEmpty a Source #
O(n). Return all elements of the map in the ascending order of their keys.
elems (fromList ((5,"a") :| [(3,"b")])) == ("b" :| ["a"])
size :: NEIntMap a -> Int Source #
O(1). The number of elements in the map. Guaranteed to be greater than zero.
size (singleton 1 'a') == 1 size (fromList ((1,'a') :| [(2,'c'), (3,'b')])) == 3
toMap :: NEIntMap a -> IntMap a Source #
O(log n).
Convert a non-empty map back into a normal possibly-empty map, for usage
with functions that expect IntMap
.
Can be thought of as "obscuring" the non-emptiness of the map in its
type. See the IsNotEmpty
pattern.
nonEmptyMap
and
form an isomorphism: they
are perfect structure-preserving inverses of eachother.maybe
empty
toMap
toMap (fromList ((3,"a") :| [(5,"b")])) == Data.IntMap.fromList [(3,"a"), (5,"b")]
Folds
foldr' :: (a -> b -> b) -> b -> NEIntMap a -> b Source #
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.
foldl' :: (a -> b -> a) -> a -> NEIntMap b -> a Source #
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.
Traversals
traverseWithKey :: Applicative t => (Key -> a -> t b) -> NEIntMap a -> t (NEIntMap b) Source #
O(n).
That is, behaves exactly like a regular traverseWithKey
f m == fromList
$ traverse
((k, v) -> (,) k $ f k v) (toList
m)traverse
except that the traversing
function also has access to the key associated with a value.
Use traverseWithKey1
whenever possible (if your Applicative
also has Apply
instance). This version is provided only for types
that do not have Apply
instance, since Apply
is not at the moment
(and might not ever be) an official superclass of Applicative
.
WARNING: Differs from Data.IntMap.traverseWithKey
, which traverses
positive items first, then negative items.
traverseWithKey
f =unwrapApplicative
.traverseWithKey1
(\k -> WrapApplicative . f k)
traverseWithKey1 :: Apply t => (Key -> a -> t b) -> NEIntMap a -> t (NEIntMap b) Source #
O(n).
traverseWithKey1
f m == fromList
$ traverse1
((k, v) -> (,) k $ f k v) (toList
m)
That is, behaves exactly like a regular traverse1
except that the traversing
function also has access to the key associated with a value.
WARNING: Differs from Data.IntMap.traverseWithKey
, which traverses
positive items first, then negative items.
Is more general than traverseWithKey
, since works with all Apply
,
and not just Applicative
.
foldMapWithKey :: Semigroup m => (Key -> a -> m) -> NEIntMap a -> m Source #
O(n). Fold the keys and values in the map using the given semigroup, such that
foldMapWithKey
f =fold1
.mapWithKey
f
WARNING: Differs from Data.IntMap.foldMapWithKey
, which traverses
positive items first, then negative items.
This can be an asymptotically faster than
foldrWithKey
or foldlWithKey
for
some monoids.
traverseMapWithKey :: Applicative t => (Key -> a -> t b) -> IntMap a -> t (IntMap b) Source #
O(n). A fixed version of traverseWithKey
that
traverses items in ascending order of keys.
Unsafe IntMap Functions
insertMinMap :: Key -> a -> IntMap a -> IntMap a Source #
O(log n). Insert new key and value into a map where keys are
strictly greater than the new key. That is, the new key must be
strictly less than all keys present in the IntMap
. /The precondition
is not checked./
At the moment this is simply an alias for Data.IntSet.insert
, but it's
left here as a placeholder in case this eventually gets implemented in
a more efficient way.
insertMaxMap :: Key -> a -> IntMap a -> IntMap a Source #
O(log n). Insert new key and value into a map where keys are
strictly less than the new key. That is, the new key must be
strictly greater than all keys present in the IntMap
. /The
precondition is not checked./
At the moment this is simply an alias for Data.IntSet.insert
, but it's
left here as a placeholder in case this eventually gets implemented in
a more efficient way.
Debug
CPP compatibility
lookupMinMap :: IntMap a -> Maybe (Key, a) Source #
CPP for new functions not in old containers ---------------------------------------------
Compatibility layer for lookupMinMap
.
lookupMaxMap :: IntMap a -> Maybe (Key, a) Source #
Compatibility layer for lookupMaxMap
.