root / Ganeti / HTools / Container.hs @ 669d7e3d
History | View | Annotate | Download (1.9 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 Ganeti.HTools.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 |
, keys |
25 |
) where |
26 |
|
27 |
import qualified Data.IntMap as IntMap |
28 |
|
29 |
type Key = IntMap.Key |
30 |
type Container = IntMap.IntMap |
31 |
|
32 |
-- | Create an empty container. |
33 |
empty :: Container a |
34 |
empty = IntMap.empty |
35 |
|
36 |
-- | Returns the number of elements in the map. |
37 |
size :: Container a -> Int |
38 |
size = IntMap.size |
39 |
|
40 |
-- | Locate a key in the map (must exist). |
41 |
find :: Key -> Container a -> a |
42 |
find k c = c IntMap.! k |
43 |
|
44 |
-- | Locate a keyin the map returning a default value if not existing. |
45 |
findWithDefault :: a -> Key -> Container a -> a |
46 |
findWithDefault = IntMap.findWithDefault |
47 |
|
48 |
-- | Add or update one element to the map. |
49 |
add :: Key -> a -> Container a -> Container a |
50 |
add k v c = IntMap.insert k v c |
51 |
|
52 |
-- | Remove an element from the map. |
53 |
remove :: Key -> Container a -> Container a |
54 |
remove = IntMap.delete |
55 |
|
56 |
-- | Return the list of values in the map. |
57 |
elems :: Container a -> [a] |
58 |
elems = IntMap.elems |
59 |
|
60 |
-- | Return the list of keys in the map. |
61 |
keys :: Container a -> [Key] |
62 |
keys = IntMap.keys |
63 |
|
64 |
-- | Create a map from an association list. |
65 |
fromAssocList :: [(Key, a)] -> Container a |
66 |
fromAssocList = IntMap.fromList |
67 |
|
68 |
-- | Create a map from an association list with a combining function. |
69 |
fromListWith :: (a -> a -> a) -> [(Key, a)] -> Container a |
70 |
fromListWith = IntMap.fromListWith |
71 |
|
72 |
-- | Fold over the values of the map. |
73 |
fold :: (a -> b -> b) -> b -> Container a -> b |
74 |
fold = IntMap.fold |
75 |
|
76 |
-- | Add or update two elements of the map. |
77 |
addTwo :: Key -> a -> Key -> a -> Container a -> Container a |
78 |
addTwo k1 v1 k2 v2 c = add k1 v1 $ add k2 v2 c |