1 {-| Module abstracting the node and instance container implementation.
3 This is currently implemented on top of an 'IntMap', which seems to
4 give the best performance for our workload.
8 module Ganeti.HTools.Container
27 import qualified Data.IntMap as IntMap
30 type Container = IntMap.IntMap
32 -- | Create an empty container.
36 -- | Returns the number of elements in the map.
37 size :: Container a -> Int
40 -- | Locate a key in the map (must exist).
41 find :: Key -> Container a -> a
42 find k c = c IntMap.! k
44 -- | Locate a keyin the map returning a default value if not existing.
45 findWithDefault :: a -> Key -> Container a -> a
46 findWithDefault = IntMap.findWithDefault
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
52 -- | Remove an element from the map.
53 remove :: Key -> Container a -> Container a
54 remove = IntMap.delete
56 -- | Return the list of values in the map.
57 elems :: Container a -> [a]
60 -- | Return the list of keys in the map.
61 keys :: Container a -> [Key]
64 -- | Create a map from an association list.
65 fromAssocList :: [(Key, a)] -> Container a
66 fromAssocList = IntMap.fromList
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
72 -- | Fold over the values of the map.
73 fold :: (a -> b -> b) -> b -> Container a -> b
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