2 Module abstracting the peer map implementation.
4 This is abstracted separately since the speed of peermap updates can
5 be a significant part of the total runtime, and as such changing the
6 implementation should be easy in case it's needed.
10 module Ganeti.HTools.PeerMap
18 Ganeti.HTools.PeerMap.find,
24 import Data.Maybe (fromMaybe)
31 type PeerMap = [(Key, Elem)]
36 create :: Key -> PeerMap
39 -- | Our reverse-compare function
40 pmCompare :: (Key, Elem) -> (Key, Elem) -> Ordering
41 pmCompare a b = (compare `on` snd) b a
43 addWith :: (Elem -> Elem -> Elem) -> Key -> Elem -> PeerMap -> PeerMap
48 Nothing -> insertBy pmCompare (k, v) lst
49 Just o -> insertBy pmCompare (k, fn o v) (remove k lst)
51 accumArray :: (Elem -> Elem -> Elem) -> Elem -> (Key, Key) ->
52 [(Key, Elem)] -> PeerMap
53 accumArray fn _ _ lst =
56 (k, v):xs -> addWith fn k v $ accumArray fn undefined undefined xs
58 find :: Key -> PeerMap -> Elem
59 find k c = fromMaybe 0 $ lookup k c
61 add :: Key -> Elem -> PeerMap -> PeerMap
62 add k v c = addWith (\_ n -> n) k v c
64 remove :: Key -> PeerMap -> PeerMap
65 remove k c = case c of
67 (x@(x', _)):xs -> if k == x' then xs
70 to_list :: PeerMap -> [Elem]
71 to_list c = snd $ unzip c
73 maxElem :: PeerMap -> Elem