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