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
30 import qualified Data.IntMap as IntMap
33 type Container = IntMap.IntMap
35 -- | Create an empty container.
39 -- | Returns the number of elements in the map.
40 size :: Container a -> Int
43 -- | Locate a key in the map (must exist).
44 find :: Key -> Container a -> a
45 find k c = c IntMap.! k
47 -- | Locate a keyin the map returning a default value if not existing.
48 findWithDefault :: a -> Key -> Container a -> a
49 findWithDefault = IntMap.findWithDefault
51 -- | Add or update one element to the map.
52 add :: Key -> a -> Container a -> Container a
53 add k v c = IntMap.insert k v c
55 -- | Remove an element from the map.
56 remove :: Key -> Container a -> Container a
57 remove = IntMap.delete
59 -- | Return the list of values in the map.
60 elems :: Container a -> [a]
63 -- | Return the list of keys in the map.
64 keys :: Container a -> [Key]
67 -- | Create a map from an association list.
68 fromAssocList :: [(Key, a)] -> Container a
69 fromAssocList = IntMap.fromList
71 -- | Create a map from an association list with a combining function.
72 fromListWith :: (a -> a -> a) -> [(Key, a)] -> Container a
73 fromListWith = IntMap.fromListWith
75 -- | Fold over the values of the map.
76 fold :: (a -> b -> b) -> b -> Container a -> b
79 -- | Add or update two elements of the map.
80 addTwo :: Key -> a -> Key -> a -> Container a -> Container a
81 addTwo k1 v1 k2 v2 c = add k1 v1 $ add k2 v2 c