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