Simple QC tests for RPC calls
[ganeti-local] / htools / Ganeti / HTools / PeerMap.hs
index d7c6440..f178578 100644 (file)
@@ -1,5 +1,4 @@
-{-|
-  Module abstracting the peer map implementation.
+{-| Module abstracting the peer map implementation.
 
 This is abstracted separately since the speed of peermap updates can
 be a significant part of the total runtime, and as such changing the
@@ -9,7 +8,7 @@ implementation should be easy in case it's needed.
 
 {-
 
-Copyright (C) 2009 Google Inc.
+Copyright (C) 2009, 2011 Google Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -29,16 +28,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 -}
 
 module Ganeti.HTools.PeerMap
-    ( PeerMap
-    , Key
-    , Elem
-    , empty
-    , accumArray
-    , Ganeti.HTools.PeerMap.find
-    , add
-    , remove
-    , maxElem
-    ) where
+  ( PeerMap
+  , Key
+  , Elem
+  , empty
+  , accumArray
+  , Ganeti.HTools.PeerMap.find
+  , add
+  , remove
+  , maxElem
+  ) where
 
 import Data.Maybe (fromMaybe)
 import Data.List
@@ -46,8 +45,16 @@ import Data.Ord (comparing)
 
 import Ganeti.HTools.Types
 
+-- * Type definitions
+
+-- | Our key type.
 type Key = Ndx
+
+-- | Our element type.
+
 type Elem = Int
+
+-- | The definition of a peer map.
 type PeerMap = [(Key, Elem)]
 
 -- * Initialization functions
@@ -63,11 +70,11 @@ 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 =
-    case lookup k lst 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
+-- | 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
@@ -76,15 +83,15 @@ accumArray fn ((k, v):xs) = addWith fn k v $ accumArray fn xs
 
 -- * Basic operations
 
--- | Returns either the value for a key or zero if not found
+-- | Returns either the value for a key or zero if not found.
 find :: Key -> PeerMap -> Elem
 find k = fromMaybe 0 . lookup k
 
--- | Add an element to a peermap, overwriting the previous value
+-- | Add an element to a peermap, overwriting the previous value.
 add :: Key -> Elem -> PeerMap -> PeerMap
 add = addWith (flip const)
 
--- | Remove an element from a peermap
+-- | Remove an element from a peermap.
 remove :: Key -> PeerMap -> PeerMap
 remove _ [] = []
 remove k ((x@(x', _)):xs) = if k == x'