Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / PeerMap.hs @ c9926b22

History | View | Annotate | Download (2.9 kB)

1 e4f08c46 Iustin Pop
{-|
2 e4f08c46 Iustin Pop
  Module abstracting the peer map implementation.
3 e4f08c46 Iustin Pop
4 e4f08c46 Iustin Pop
This is abstracted separately since the speed of peermap updates can
5 e4f08c46 Iustin Pop
be a significant part of the total runtime, and as such changing the
6 e4f08c46 Iustin Pop
implementation should be easy in case it's needed.
7 e4f08c46 Iustin Pop
8 e4f08c46 Iustin Pop
-}
9 e4f08c46 Iustin Pop
10 e2fa2baf Iustin Pop
{-
11 e2fa2baf Iustin Pop
12 e2fa2baf Iustin Pop
Copyright (C) 2009 Google Inc.
13 e2fa2baf Iustin Pop
14 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
15 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
16 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
17 e2fa2baf Iustin Pop
(at your option) any later version.
18 e2fa2baf Iustin Pop
19 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
20 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
21 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 e2fa2baf Iustin Pop
General Public License for more details.
23 e2fa2baf Iustin Pop
24 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
25 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
26 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 e2fa2baf Iustin Pop
02110-1301, USA.
28 e2fa2baf Iustin Pop
29 e2fa2baf Iustin Pop
-}
30 e2fa2baf Iustin Pop
31 669d7e3d Iustin Pop
module Ganeti.HTools.PeerMap
32 71e13e48 Iustin Pop
    ( PeerMap
33 71e13e48 Iustin Pop
    , Key
34 71e13e48 Iustin Pop
    , Elem
35 71e13e48 Iustin Pop
    , empty
36 71e13e48 Iustin Pop
    , accumArray
37 71e13e48 Iustin Pop
    , Ganeti.HTools.PeerMap.find
38 71e13e48 Iustin Pop
    , add
39 71e13e48 Iustin Pop
    , remove
40 71e13e48 Iustin Pop
    , maxElem
41 669d7e3d Iustin Pop
    ) where
42 e4f08c46 Iustin Pop
43 e4f08c46 Iustin Pop
import Data.Maybe (fromMaybe)
44 e4f08c46 Iustin Pop
import Data.List
45 e4f08c46 Iustin Pop
import Data.Function
46 e4f08c46 Iustin Pop
import Data.Ord
47 e4f08c46 Iustin Pop
48 608efcce Iustin Pop
import Ganeti.HTools.Types
49 608efcce Iustin Pop
50 608efcce Iustin Pop
type Key = Ndx
51 e4f08c46 Iustin Pop
type Elem = Int
52 e4f08c46 Iustin Pop
type PeerMap = [(Key, Elem)]
53 e4f08c46 Iustin Pop
54 9188aeef Iustin Pop
-- * Initialization functions
55 9188aeef Iustin Pop
56 9188aeef Iustin Pop
-- | Create a new empty map.
57 e4f08c46 Iustin Pop
empty :: PeerMap
58 e4f08c46 Iustin Pop
empty = []
59 e4f08c46 Iustin Pop
60 9188aeef Iustin Pop
-- | Our reverse-compare function.
61 e4f08c46 Iustin Pop
pmCompare :: (Key, Elem) -> (Key, Elem) -> Ordering
62 e4f08c46 Iustin Pop
pmCompare a b = (compare `on` snd) b a
63 e4f08c46 Iustin Pop
64 9188aeef Iustin Pop
-- | Add or update (via a custom function) an element.
65 e4f08c46 Iustin Pop
addWith :: (Elem -> Elem -> Elem) -> Key -> Elem -> PeerMap -> PeerMap
66 e4f08c46 Iustin Pop
addWith fn k v lst =
67 e4f08c46 Iustin Pop
    let r = lookup k lst
68 e4f08c46 Iustin Pop
    in
69 e4f08c46 Iustin Pop
      case r of
70 e4f08c46 Iustin Pop
        Nothing -> insertBy pmCompare (k, v) lst
71 e4f08c46 Iustin Pop
        Just o -> insertBy pmCompare (k, fn o v) (remove k lst)
72 e4f08c46 Iustin Pop
73 17c59f4b Iustin Pop
-- | Create a PeerMap from an association list, with possible duplicates
74 17c59f4b Iustin Pop
accumArray :: (Elem -> Elem -> Elem) -- ^ function used to merge the elements
75 17c59f4b Iustin Pop
              -> [(Key, Elem)]       -- ^ source data
76 17c59f4b Iustin Pop
              -> PeerMap             -- ^ results
77 17c59f4b Iustin Pop
accumArray fn lst =
78 e4f08c46 Iustin Pop
    case lst of
79 e4f08c46 Iustin Pop
      [] -> empty
80 17c59f4b Iustin Pop
      (k, v):xs -> addWith fn k v $ accumArray fn xs
81 e4f08c46 Iustin Pop
82 9188aeef Iustin Pop
-- * Basic operations
83 9188aeef Iustin Pop
84 9188aeef Iustin Pop
-- | Returns either the value for a key or zero if not found
85 e4f08c46 Iustin Pop
find :: Key -> PeerMap -> Elem
86 9f6dcdea Iustin Pop
find k = fromMaybe 0 . lookup k
87 e4f08c46 Iustin Pop
88 9188aeef Iustin Pop
-- | Add an element to a peermap, overwriting the previous value
89 e4f08c46 Iustin Pop
add :: Key -> Elem -> PeerMap -> PeerMap
90 9f6dcdea Iustin Pop
add = addWith (flip const)
91 e4f08c46 Iustin Pop
92 9188aeef Iustin Pop
-- | Remove an element from a peermap
93 e4f08c46 Iustin Pop
remove :: Key -> PeerMap -> PeerMap
94 e4f08c46 Iustin Pop
remove k c = case c of
95 e4f08c46 Iustin Pop
               [] -> []
96 e4f08c46 Iustin Pop
               (x@(x', _)):xs -> if k == x' then xs
97 9f6dcdea Iustin Pop
                            else x:remove k xs
98 e4f08c46 Iustin Pop
99 9188aeef Iustin Pop
-- | Find the maximum element.
100 9188aeef Iustin Pop
--
101 9188aeef Iustin Pop
-- Since this is a sorted list, we just get the value at the head of
102 9188aeef Iustin Pop
-- the list, or zero for a null list
103 e4f08c46 Iustin Pop
maxElem :: PeerMap -> Elem
104 71e13e48 Iustin Pop
maxElem c = if null c then 0 else snd . head $ c