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
28 import qualified Data.IntMap as IntMap
31 type Container = IntMap.IntMap
33 -- | Create an empty container.
37 -- | Returns the number of elements in the map.
38 size :: Container a -> Int
41 -- | Locate a key in the map (must exist).
42 find :: Key -> Container a -> a
43 find k c = c IntMap.! k
45 -- | Locate a keyin the map returning a default value if not existing.
46 findWithDefault :: a -> Key -> Container a -> a
47 findWithDefault = IntMap.findWithDefault
49 -- | Add or update one element to the map.
50 add :: Key -> a -> Container a -> Container a
51 add k v c = IntMap.insert k v c
53 -- | Remove an element from the map.
54 remove :: Key -> Container a -> Container a
55 remove = IntMap.delete
57 -- | Return the list of values in the map.
58 elems :: Container a -> [a]
61 -- | Return the list of keys in the map.
62 keys :: Container a -> [Key]
65 -- | Create a map from an association list.
66 fromAssocList :: [(Key, a)] -> Container a
67 fromAssocList = IntMap.fromList
69 -- | Create a map from an association list with a combining function.
70 fromListWith :: (a -> a -> a) -> [(Key, a)] -> Container a
71 fromListWith = IntMap.fromListWith
73 -- | Fold over the values of the map.
74 fold :: (a -> b -> b) -> b -> Container a -> b
77 -- | Add or update two elements of the map.
78 addTwo :: Key -> a -> Key -> a -> Container a -> Container a
79 addTwo k1 v1 k2 v2 c = add k1 v1 $ add k2 v2 c