Revision ebf38064 htools/Ganeti/HTools/PeerMap.hs
b/htools/Ganeti/HTools/PeerMap.hs | ||
---|---|---|
8 | 8 |
|
9 | 9 |
{- |
10 | 10 |
|
11 |
Copyright (C) 2009 Google Inc. |
|
11 |
Copyright (C) 2009, 2011 Google Inc.
|
|
12 | 12 |
|
13 | 13 |
This program is free software; you can redistribute it and/or modify |
14 | 14 |
it under the terms of the GNU General Public License as published by |
... | ... | |
28 | 28 |
-} |
29 | 29 |
|
30 | 30 |
module Ganeti.HTools.PeerMap |
31 |
( PeerMap
|
|
32 |
, Key
|
|
33 |
, Elem
|
|
34 |
, empty
|
|
35 |
, accumArray
|
|
36 |
, Ganeti.HTools.PeerMap.find
|
|
37 |
, add
|
|
38 |
, remove
|
|
39 |
, maxElem
|
|
40 |
) where
|
|
31 |
( PeerMap |
|
32 |
, Key |
|
33 |
, Elem |
|
34 |
, empty |
|
35 |
, accumArray |
|
36 |
, Ganeti.HTools.PeerMap.find |
|
37 |
, add |
|
38 |
, remove |
|
39 |
, maxElem |
|
40 |
) where |
|
41 | 41 |
|
42 | 42 |
import Data.Maybe (fromMaybe) |
43 | 43 |
import Data.List |
... | ... | |
70 | 70 |
-- | Add or update (via a custom function) an element. |
71 | 71 |
addWith :: (Elem -> Elem -> Elem) -> Key -> Elem -> PeerMap -> PeerMap |
72 | 72 |
addWith fn k v lst = |
73 |
case lookup k lst of
|
|
74 |
Nothing -> insertBy pmCompare (k, v) lst
|
|
75 |
Just o -> insertBy pmCompare (k, fn o v) (remove k lst)
|
|
73 |
case lookup k lst of |
|
74 |
Nothing -> insertBy pmCompare (k, v) lst |
|
75 |
Just o -> insertBy pmCompare (k, fn o v) (remove k lst) |
|
76 | 76 |
|
77 | 77 |
-- | Create a PeerMap from an association list, with possible duplicates. |
78 | 78 |
accumArray :: (Elem -> Elem -> Elem) -- ^ function used to merge the elements |
Also available in: Unified diff