Copyright | (c) Dong Han 2017-2019 (c) Tao He 2018-2019 |
---|---|
License | BSD |
Maintainer | winterland1989@gmail.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
This module provides a simple int key value map based on sorted vector and binary search. It's particularly suitable for small sized key value collections such as deserializing intermediate representation. But can also used in various place where insertion and deletion is rare but require fast lookup.
Synopsis
- data FlatIntMap v
- sortedKeyValues :: FlatIntMap v -> Vector (IPair v)
- size :: FlatIntMap v -> Int
- null :: FlatIntMap v -> Bool
- empty :: FlatIntMap v
- map' :: (v -> v') -> FlatIntMap v -> FlatIntMap v'
- imap' :: (Int -> v -> v') -> FlatIntMap v -> FlatIntMap v'
- pack :: [IPair v] -> FlatIntMap v
- packN :: Int -> [IPair v] -> FlatIntMap v
- packR :: [IPair v] -> FlatIntMap v
- packRN :: Int -> [IPair v] -> FlatIntMap v
- unpack :: FlatIntMap v -> [IPair v]
- unpackR :: FlatIntMap v -> [IPair v]
- packVector :: Vector (IPair v) -> FlatIntMap v
- packVectorR :: Vector (IPair v) -> FlatIntMap v
- lookup :: Int -> FlatIntMap v -> Maybe v
- delete :: Int -> FlatIntMap v -> FlatIntMap v
- insert :: Int -> v -> FlatIntMap v -> FlatIntMap v
- adjust' :: (v -> v) -> Int -> FlatIntMap v -> FlatIntMap v
- merge :: forall v. FlatIntMap v -> FlatIntMap v -> FlatIntMap v
- mergeWithKey' :: forall v. (Int -> v -> v -> v) -> FlatIntMap v -> FlatIntMap v -> FlatIntMap v
- foldrWithKey :: (Int -> v -> a -> a) -> a -> FlatIntMap v -> a
- foldrWithKey' :: (Int -> v -> a -> a) -> a -> FlatIntMap v -> a
- foldlWithKey :: (a -> Int -> v -> a) -> a -> FlatIntMap v -> a
- foldlWithKey' :: (a -> Int -> v -> a) -> a -> FlatIntMap v -> a
- traverseWithKey :: Applicative t => (Int -> a -> t b) -> FlatIntMap a -> t (FlatIntMap b)
- linearSearch :: Vector (IPair v) -> Int -> Maybe v
- linearSearchR :: Vector (IPair v) -> Int -> Maybe v
- binarySearch :: Vector (IPair v) -> Int -> Either Int Int
FlatIntMap backed by sorted vector
data FlatIntMap v Source #
Instances
sortedKeyValues :: FlatIntMap v -> Vector (IPair v) Source #
size :: FlatIntMap v -> Int Source #
null :: FlatIntMap v -> Bool Source #
empty :: FlatIntMap v Source #
O(1) empty flat map.
map' :: (v -> v') -> FlatIntMap v -> FlatIntMap v' Source #
imap' :: (Int -> v -> v') -> FlatIntMap v -> FlatIntMap v' Source #
pack :: [IPair v] -> FlatIntMap v Source #
O(N*logN) Pack list of key values, on key duplication prefer left one.
packN :: Int -> [IPair v] -> FlatIntMap v Source #
O(N*logN) Pack list of key values with suggested size, on key duplication prefer left one.
packR :: [IPair v] -> FlatIntMap v Source #
O(N*logN) Pack list of key values, on key duplication prefer right one.
packRN :: Int -> [IPair v] -> FlatIntMap v Source #
O(N*logN) Pack list of key values with suggested size, on key duplication prefer right one.
unpack :: FlatIntMap v -> [IPair v] Source #
O(N) Unpack key value pairs to a list sorted by keys in ascending order.
This function works with foldr/build
fusion in base.
unpackR :: FlatIntMap v -> [IPair v] Source #
O(N) Unpack key value pairs to a list sorted by keys in descending order.
This function works with foldr/build
fusion in base.
packVector :: Vector (IPair v) -> FlatIntMap v Source #
O(N*logN) Pack vector of key values, on key duplication prefer left one.
packVectorR :: Vector (IPair v) -> FlatIntMap v Source #
O(N*logN) Pack vector of key values, on key duplication prefer right one.
delete :: Int -> FlatIntMap v -> FlatIntMap v Source #
O(N) Delete a key value pair by key.
insert :: Int -> v -> FlatIntMap v -> FlatIntMap v Source #
O(N) Insert new key value into map, replace old one if key exists.
adjust' :: (v -> v) -> Int -> FlatIntMap v -> FlatIntMap v Source #
O(N) Modify a value by key.
The value is evaluated to WHNF before writing into map.
merge :: forall v. FlatIntMap v -> FlatIntMap v -> FlatIntMap v Source #
O(n+m) Merge two FlatIntMap
, prefer right value on key duplication.
mergeWithKey' :: forall v. (Int -> v -> v -> v) -> FlatIntMap v -> FlatIntMap v -> FlatIntMap v Source #
O(n+m) Merge two FlatIntMap
with a merge function.
fold and traverse
foldrWithKey :: (Int -> v -> a -> a) -> a -> FlatIntMap v -> a Source #
O(n) Reduce this map by applying a binary operator to all elements, using the given starting value (typically the right-identity of the operator).
During folding k is in descending order.
foldrWithKey' :: (Int -> v -> a -> a) -> a -> FlatIntMap v -> a Source #
O(n) Reduce this map by applying a binary operator to all elements, using the given starting value (typically the right-identity of the operator).
During folding Int is in descending order.
foldlWithKey :: (a -> Int -> v -> a) -> a -> FlatIntMap v -> a Source #
O(n) Reduce this map by applying a binary operator to all elements, using the given starting value (typically the right-identity of the operator).
During folding Int is in ascending order.
foldlWithKey' :: (a -> Int -> v -> a) -> a -> FlatIntMap v -> a Source #
O(n) Reduce this map by applying a binary operator to all elements, using the given starting value (typically the right-identity of the operator).
During folding Int is in ascending order.
traverseWithKey :: Applicative t => (Int -> a -> t b) -> FlatIntMap a -> t (FlatIntMap b) Source #
O(n).
That is, behaves exactly like a regular traverseWithKey
f s == pack
<$> traverse
((k, v) -> (,) k <$> f k v) (unpack
m)traverse
except that the traversing
function also has access to the key associated with a value.
binary & linear search on vectors
linearSearch :: Vector (IPair v) -> Int -> Maybe v Source #
linear scan search from left to right, return the first one if exist.