Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Container.hs @ 4333a887

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
    , Key
13
     -- * Creation
14
    , empty
15
    , fromAssocList
16
     -- * Query
17
    , size
18
    , find
19
     -- * Update
20
    , add
21
    , addTwo
22
    , remove
23
    , IntMap.map
24
    , IntMap.mapAccum
25
    -- * Conversion
26
    , elems
27
    , keys
28
    ) where
29

    
30
import qualified Data.IntMap as IntMap
31

    
32
type Key = IntMap.Key
33
type Container = IntMap.IntMap
34

    
35
-- | Create an empty container.
36
empty :: Container a
37
empty = IntMap.empty
38

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

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

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

    
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
54

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

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

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

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

    
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
74

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

    
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