X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/9f6dcdeab5e95b5a07fd90a206761b2873eb970a..99b63608c593b37bead28376777fa95e49825a51:/Ganeti/HTools/PeerMap.hs diff --git a/Ganeti/HTools/PeerMap.hs b/Ganeti/HTools/PeerMap.hs index 765eb41..d7c6440 100644 --- a/Ganeti/HTools/PeerMap.hs +++ b/Ganeti/HTools/PeerMap.hs @@ -42,8 +42,7 @@ module Ganeti.HTools.PeerMap import Data.Maybe (fromMaybe) import Data.List -import Data.Function -import Data.Ord +import Data.Ord (comparing) import Ganeti.HTools.Types @@ -59,25 +58,21 @@ empty = [] -- | Our reverse-compare function. pmCompare :: (Key, Elem) -> (Key, Elem) -> Ordering -pmCompare a b = (compare `on` snd) b a +pmCompare a b = comparing snd b a -- | Add or update (via a custom function) an element. addWith :: (Elem -> Elem -> Elem) -> Key -> Elem -> PeerMap -> PeerMap addWith fn k v lst = - let r = lookup k lst - in - case r of - Nothing -> insertBy pmCompare (k, v) lst - Just o -> insertBy pmCompare (k, fn o v) (remove k lst) + case lookup k lst of + Nothing -> insertBy pmCompare (k, v) lst + Just o -> insertBy pmCompare (k, fn o v) (remove k lst) -- | Create a PeerMap from an association list, with possible duplicates accumArray :: (Elem -> Elem -> Elem) -- ^ function used to merge the elements -> [(Key, Elem)] -- ^ source data -> PeerMap -- ^ results -accumArray fn lst = - case lst of - [] -> empty - (k, v):xs -> addWith fn k v $ accumArray fn xs +accumArray _ [] = empty +accumArray fn ((k, v):xs) = addWith fn k v $ accumArray fn xs -- * Basic operations @@ -91,9 +86,9 @@ add = addWith (flip const) -- | Remove an element from a peermap remove :: Key -> PeerMap -> PeerMap -remove k c = case c of - [] -> [] - (x@(x', _)):xs -> if k == x' then xs +remove _ [] = [] +remove k ((x@(x', _)):xs) = if k == x' + then xs else x:remove k xs -- | Find the maximum element. @@ -101,4 +96,5 @@ remove k c = case c of -- Since this is a sorted list, we just get the value at the head of -- the list, or zero for a null list maxElem :: PeerMap -> Elem -maxElem c = if null c then 0 else snd . head $ c +maxElem (x:_) = snd x +maxElem _ = 0