Revision 9188aeef Ganeti/HTools/PeerMap.hs

b/Ganeti/HTools/PeerMap.hs
30 30
type Elem = Int
31 31
type PeerMap = [(Key, Elem)]
32 32

  
33
-- | Create a new empty map
33
-- * Initialization functions
34

  
35
-- | Create a new empty map.
34 36
empty :: PeerMap
35 37
empty = []
36 38

  
37
-- | Our reverse-compare function
39
-- | Our reverse-compare function.
38 40
pmCompare :: (Key, Elem) -> (Key, Elem) -> Ordering
39 41
pmCompare a b = (compare `on` snd) b a
40 42

  
41
-- | Add or update (via a custom function) an element
43
-- | Add or update (via a custom function) an element.
42 44
addWith :: (Elem -> Elem -> Elem) -> Key -> Elem -> PeerMap -> PeerMap
43 45
addWith fn k v lst =
44 46
    let r = lookup k lst
......
56 58
      [] -> empty
57 59
      (k, v):xs -> addWith fn k v $ accumArray fn xs
58 60

  
61
-- * Basic operations
62

  
63
-- | Returns either the value for a key or zero if not found
59 64
find :: Key -> PeerMap -> Elem
60 65
find k c = fromMaybe 0 $ lookup k c
61 66

  
67
-- | Add an element to a peermap, overwriting the previous value
62 68
add :: Key -> Elem -> PeerMap -> PeerMap
63 69
add k v c = addWith (flip const) k v c
64 70

  
71
-- | Remove an element from a peermap
65 72
remove :: Key -> PeerMap -> PeerMap
66 73
remove k c = case c of
67 74
               [] -> []
68 75
               (x@(x', _)):xs -> if k == x' then xs
69 76
                            else x:(remove k xs)
70 77

  
71
-- | Find the maximum element. Since this is a sorted list, we just
72
-- get the first one
78
-- | Find the maximum element.
79
--
80
-- Since this is a sorted list, we just get the value at the head of
81
-- the list, or zero for a null list
73 82
maxElem :: PeerMap -> Elem
74 83
maxElem c = if null c then 0 else snd . head $ c

Also available in: Unified diff