Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Container.hs @ ec18dca9

History | View | Annotate | Download (1.9 kB)

1
{-| Module abstracting the node and instance container implementation.
2

    
3
This is currently implemented on top of an 'IntMap', which seems to
4
give the best performance for our workload.
5

    
6
-}
7

    
8
module Ganeti.HTools.Container
9
    (
10
     -- * Types
11
     Container
12
     -- * Creation
13
    , empty
14
    , fromAssocList
15
     -- * Query
16
    , size
17
    , find
18
     -- * Update
19
    , add
20
    , addTwo
21
    , remove
22
    , IntMap.map
23
    -- * Conversion
24
    , elems
25
    , keys
26
    ) where
27

    
28
import qualified Data.IntMap as IntMap
29

    
30
type Key = IntMap.Key
31
type Container = IntMap.IntMap
32

    
33
-- | Create an empty container.
34
empty :: Container a
35
empty = IntMap.empty
36

    
37
-- | Returns the number of elements in the map.
38
size :: Container a -> Int
39
size = IntMap.size
40

    
41
-- | Locate a key in the map (must exist).
42
find :: Key -> Container a -> a
43
find k c = c IntMap.! k
44

    
45
-- | Locate a keyin the map returning a default value if not existing.
46
findWithDefault :: a -> Key -> Container a -> a
47
findWithDefault = IntMap.findWithDefault
48

    
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
52

    
53
-- | Remove an element from the map.
54
remove :: Key -> Container a -> Container a
55
remove = IntMap.delete
56

    
57
-- | Return the list of values in the map.
58
elems :: Container a -> [a]
59
elems = IntMap.elems
60

    
61
-- | Return the list of keys in the map.
62
keys :: Container a -> [Key]
63
keys = IntMap.keys
64

    
65
-- | Create a map from an association list.
66
fromAssocList :: [(Key, a)] -> Container a
67
fromAssocList = IntMap.fromList
68

    
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
72

    
73
-- | Fold over the values of the map.
74
fold :: (a -> b -> b) -> b -> Container a -> b
75
fold = IntMap.fold
76

    
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