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
29 import qualified Data.IntMap as IntMap
32 type Container = IntMap.IntMap
34 -- | Create an empty container.
38 -- | Returns the number of elements in the map.
39 size :: Container a -> Int
42 -- | Locate a key in the map (must exist).
43 find :: Key -> Container a -> a
44 find k c = c IntMap.! k
46 -- | Locate a keyin the map returning a default value if not existing.
47 findWithDefault :: a -> Key -> Container a -> a
48 findWithDefault = IntMap.findWithDefault
50 -- | Add or update one element to the map.
51 add :: Key -> a -> Container a -> Container a
52 add k v c = IntMap.insert k v c
54 -- | Remove an element from the map.
55 remove :: Key -> Container a -> Container a
56 remove = IntMap.delete
58 -- | Return the list of values in the map.
59 elems :: Container a -> [a]
62 -- | Return the list of keys in the map.
63 keys :: Container a -> [Key]
66 -- | Create a map from an association list.
67 fromAssocList :: [(Key, a)] -> Container a
68 fromAssocList = IntMap.fromList
70 -- | Create a map from an association list with a combining function.
71 fromListWith :: (a -> a -> a) -> [(Key, a)] -> Container a
72 fromListWith = IntMap.fromListWith
74 -- | Fold over the values of the map.
75 fold :: (a -> b -> b) -> b -> Container a -> b
78 -- | Add or update two elements of the map.
79 addTwo :: Key -> a -> Key -> a -> Container a -> Container a
80 addTwo k1 v1 k2 v2 c = add k1 v1 $ add k2 v2 c