Revision bbd8efd2 Ganeti/HTools/PeerMap.hs
b/Ganeti/HTools/PeerMap.hs | ||
---|---|---|
64 | 64 |
-- | Add or update (via a custom function) an element. |
65 | 65 |
addWith :: (Elem -> Elem -> Elem) -> Key -> Elem -> PeerMap -> PeerMap |
66 | 66 |
addWith fn k v lst = |
67 |
let r = lookup k lst |
|
68 |
in |
|
69 |
case r of |
|
70 |
Nothing -> insertBy pmCompare (k, v) lst |
|
71 |
Just o -> insertBy pmCompare (k, fn o v) (remove k lst) |
|
67 |
case lookup k lst of |
|
68 |
Nothing -> insertBy pmCompare (k, v) lst |
|
69 |
Just o -> insertBy pmCompare (k, fn o v) (remove k lst) |
|
72 | 70 |
|
73 | 71 |
-- | Create a PeerMap from an association list, with possible duplicates |
74 | 72 |
accumArray :: (Elem -> Elem -> Elem) -- ^ function used to merge the elements |
75 | 73 |
-> [(Key, Elem)] -- ^ source data |
76 | 74 |
-> PeerMap -- ^ results |
77 |
accumArray fn lst = |
|
78 |
case lst of |
|
79 |
[] -> empty |
|
80 |
(k, v):xs -> addWith fn k v $ accumArray fn xs |
|
75 |
accumArray _ [] = empty |
|
76 |
accumArray fn ((k, v):xs) = addWith fn k v $ accumArray fn xs |
|
81 | 77 |
|
82 | 78 |
-- * Basic operations |
83 | 79 |
|
... | ... | |
91 | 87 |
|
92 | 88 |
-- | Remove an element from a peermap |
93 | 89 |
remove :: Key -> PeerMap -> PeerMap |
94 |
remove k c = case c of
|
|
95 |
[] -> []
|
|
96 |
(x@(x', _)):xs -> if k == x' then xs
|
|
90 |
remove _ [] = []
|
|
91 |
remove k ((x@(x', _)):xs) = if k == x'
|
|
92 |
then xs
|
|
97 | 93 |
else x:remove k xs |
98 | 94 |
|
99 | 95 |
-- | Find the maximum element. |
... | ... | |
101 | 97 |
-- Since this is a sorted list, we just get the value at the head of |
102 | 98 |
-- the list, or zero for a null list |
103 | 99 |
maxElem :: PeerMap -> Elem |
104 |
maxElem c = if null c then 0 else snd . head $ c |
|
100 |
maxElem (x:_) = snd x |
|
101 |
maxElem _ = 0 |
Also available in: Unified diff