Revision 525bfb36 htools/Ganeti/HTools/PeerMap.hs
b/htools/Ganeti/HTools/PeerMap.hs | ||
---|---|---|
1 |
{-| |
|
2 |
Module abstracting the peer map implementation. |
|
1 |
{-| Module abstracting the peer map implementation. |
|
3 | 2 |
|
4 | 3 |
This is abstracted separately since the speed of peermap updates can |
5 | 4 |
be a significant part of the total runtime, and as such changing the |
... | ... | |
46 | 45 |
|
47 | 46 |
import Ganeti.HTools.Types |
48 | 47 |
|
48 |
-- * Type definitions |
|
49 |
|
|
50 |
-- | Our key type. |
|
49 | 51 |
type Key = Ndx |
52 |
|
|
53 |
-- | Our element type. |
|
54 |
|
|
50 | 55 |
type Elem = Int |
56 |
|
|
57 |
-- | The definition of a peer map. |
|
51 | 58 |
type PeerMap = [(Key, Elem)] |
52 | 59 |
|
53 | 60 |
-- * Initialization functions |
... | ... | |
67 | 74 |
Nothing -> insertBy pmCompare (k, v) lst |
68 | 75 |
Just o -> insertBy pmCompare (k, fn o v) (remove k lst) |
69 | 76 |
|
70 |
-- | Create a PeerMap from an association list, with possible duplicates |
|
77 |
-- | Create a PeerMap from an association list, with possible duplicates.
|
|
71 | 78 |
accumArray :: (Elem -> Elem -> Elem) -- ^ function used to merge the elements |
72 | 79 |
-> [(Key, Elem)] -- ^ source data |
73 | 80 |
-> PeerMap -- ^ results |
... | ... | |
76 | 83 |
|
77 | 84 |
-- * Basic operations |
78 | 85 |
|
79 |
-- | Returns either the value for a key or zero if not found |
|
86 |
-- | Returns either the value for a key or zero if not found.
|
|
80 | 87 |
find :: Key -> PeerMap -> Elem |
81 | 88 |
find k = fromMaybe 0 . lookup k |
82 | 89 |
|
83 |
-- | Add an element to a peermap, overwriting the previous value |
|
90 |
-- | Add an element to a peermap, overwriting the previous value.
|
|
84 | 91 |
add :: Key -> Elem -> PeerMap -> PeerMap |
85 | 92 |
add = addWith (flip const) |
86 | 93 |
|
87 |
-- | Remove an element from a peermap |
|
94 |
-- | Remove an element from a peermap.
|
|
88 | 95 |
remove :: Key -> PeerMap -> PeerMap |
89 | 96 |
remove _ [] = [] |
90 | 97 |
remove k ((x@(x', _)):xs) = if k == x' |
Also available in: Unified diff