## Revision 71e13e48 Ganeti/HTools/PeerMap.hs

b/Ganeti/HTools/PeerMap.hs
8 8
```-}
```
9 9

10 10
```module Ganeti.HTools.PeerMap
```
11
```    (
```
12
```     PeerMap,
```
13
```     Key,
```
14
```     Elem,
```
15
```     empty,
```
16
```     create,
```
17
```     accumArray,
```
18
```     Ganeti.HTools.PeerMap.find,
```
19
```     add,
```
20
```     remove,
```
21
```     maxElem
```
11
```    ( PeerMap
```
12
```    , Key
```
13
```    , Elem
```
14
```    , empty
```
15
```    , accumArray
```
16
```    , Ganeti.HTools.PeerMap.find
```
17
```    , add
```
18
```    , remove
```
19
```    , maxElem
```
22 20
```    ) where
```
23 21

24 22
```import Data.Maybe (fromMaybe)
```
......
32 30
```type Elem = Int
```
33 31
```type PeerMap = [(Key, Elem)]
```
34 32

33
```-- | Create a new empty map
```
35 34
```empty :: PeerMap
```
36 35
```empty = []
```
37 36

38
```create :: Key -> PeerMap
```
39
```create _ = []
```
40

41 37
```-- | Our reverse-compare function
```
42 38
```pmCompare :: (Key, Elem) -> (Key, Elem) -> Ordering
```
43 39
```pmCompare a b = (compare `on` snd) b a
```
......
64 60
```find k c = fromMaybe 0 \$ lookup k c
```
65 61

66 62
```add :: Key -> Elem -> PeerMap -> PeerMap
```
67
```add k v c = addWith (\_ n -> n) k v c
```
63
```add k v c = addWith (flip const) k v c
```
68 64

69 65
```remove :: Key -> PeerMap -> PeerMap
```
70 66
```remove k c = case c of
```
......
72 68
```               (x@(x', _)):xs -> if k == x' then xs
```
73 69
```                            else x:(remove k xs)
```
74 70

75
```to_list :: PeerMap -> [Elem]
```
76
```to_list c = snd \$ unzip c
```
77

71
```-- | Find the maximum element. Since this is a sorted list, we just
```
72
```-- get the first one
```
78 73
```maxElem :: PeerMap -> Elem
```
79
```maxElem c = case c of
```
80
```              [] -> 0
```
81
```              (_, v):_ -> v
```
74
```maxElem c = if null c then 0 else snd . head \$ c
```

Also available in: Unified diff