Statistics
| Branch: | Tag: | Revision:

root / src / Container.hs @ e4f08c46

History | View | Annotate | Download (1.8 kB)

1 e4f08c46 Iustin Pop
{-| Module abstracting the node and instance container implementation.
2 e4f08c46 Iustin Pop
3 e4f08c46 Iustin Pop
This is currently implemented on top of an 'IntMap', which seems to
4 e4f08c46 Iustin Pop
give the best performance for our workload.
5 e4f08c46 Iustin Pop
6 e4f08c46 Iustin Pop
-}
7 e4f08c46 Iustin Pop
8 e4f08c46 Iustin Pop
module Container
9 e4f08c46 Iustin Pop
    (
10 e4f08c46 Iustin Pop
     -- * Types
11 e4f08c46 Iustin Pop
     Container
12 e4f08c46 Iustin Pop
     -- * Creation
13 e4f08c46 Iustin Pop
    , empty
14 e4f08c46 Iustin Pop
    , fromAssocList
15 e4f08c46 Iustin Pop
     -- * Query
16 e4f08c46 Iustin Pop
    , size
17 e4f08c46 Iustin Pop
    , find
18 e4f08c46 Iustin Pop
     -- * Update
19 e4f08c46 Iustin Pop
    , add
20 e4f08c46 Iustin Pop
    , addTwo
21 e4f08c46 Iustin Pop
    , remove
22 e4f08c46 Iustin Pop
    -- * Conversion
23 e4f08c46 Iustin Pop
    , elems
24 e4f08c46 Iustin Pop
    ) where
25 e4f08c46 Iustin Pop
26 e4f08c46 Iustin Pop
import qualified Data.IntMap as IntMap
27 e4f08c46 Iustin Pop
28 e4f08c46 Iustin Pop
type Key = IntMap.Key
29 e4f08c46 Iustin Pop
type Container = IntMap.IntMap
30 e4f08c46 Iustin Pop
31 e4f08c46 Iustin Pop
-- | Create an empty container.
32 e4f08c46 Iustin Pop
empty :: Container a
33 e4f08c46 Iustin Pop
empty = IntMap.empty
34 e4f08c46 Iustin Pop
35 e4f08c46 Iustin Pop
-- | Returns the number of elements in the map.
36 e4f08c46 Iustin Pop
size :: Container a -> Int
37 e4f08c46 Iustin Pop
size = IntMap.size
38 e4f08c46 Iustin Pop
39 e4f08c46 Iustin Pop
-- | Locate a key in the map (must exist).
40 e4f08c46 Iustin Pop
find :: Key -> Container a -> a
41 e4f08c46 Iustin Pop
find k c = c IntMap.! k
42 e4f08c46 Iustin Pop
43 e4f08c46 Iustin Pop
-- | Locate a keyin the map returning a default value if not existing.
44 e4f08c46 Iustin Pop
findWithDefault :: a -> Key -> Container a -> a
45 e4f08c46 Iustin Pop
findWithDefault = IntMap.findWithDefault
46 e4f08c46 Iustin Pop
47 e4f08c46 Iustin Pop
-- | Add or update one element to the map.
48 e4f08c46 Iustin Pop
add :: Key -> a -> Container a -> Container a
49 e4f08c46 Iustin Pop
add k v c = IntMap.insert k v c
50 e4f08c46 Iustin Pop
51 e4f08c46 Iustin Pop
-- | Remove an element from the map.
52 e4f08c46 Iustin Pop
remove :: Key -> Container a -> Container a
53 e4f08c46 Iustin Pop
remove = IntMap.delete
54 e4f08c46 Iustin Pop
55 e4f08c46 Iustin Pop
-- | Return the list of values in the map.
56 e4f08c46 Iustin Pop
elems :: Container a -> [a]
57 e4f08c46 Iustin Pop
elems = IntMap.elems
58 e4f08c46 Iustin Pop
59 e4f08c46 Iustin Pop
-- | Create a map from an association list.
60 e4f08c46 Iustin Pop
fromAssocList :: [(Key, a)] -> Container a
61 e4f08c46 Iustin Pop
fromAssocList = IntMap.fromList
62 e4f08c46 Iustin Pop
63 e4f08c46 Iustin Pop
-- | Create a map from an association list with a combining function.
64 e4f08c46 Iustin Pop
fromListWith :: (a -> a -> a) -> [(Key, a)] -> Container a
65 e4f08c46 Iustin Pop
fromListWith = IntMap.fromListWith
66 e4f08c46 Iustin Pop
67 e4f08c46 Iustin Pop
-- | Fold over the values of the map.
68 e4f08c46 Iustin Pop
fold :: (a -> b -> b) -> b -> Container a -> b
69 e4f08c46 Iustin Pop
fold = IntMap.fold
70 e4f08c46 Iustin Pop
71 e4f08c46 Iustin Pop
-- | Add or update two elements of the map.
72 e4f08c46 Iustin Pop
addTwo :: Key -> a -> Key -> a -> Container a -> Container a
73 e4f08c46 Iustin Pop
addTwo k1 v1 k2 v2 c = add k1 v1 $ add k2 v2 c