Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / PeerMap.hs @ 28f19313

History | View | Annotate | Download (2.8 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 5182e970 Iustin Pop
import Data.Ord (comparing)
46 e4f08c46 Iustin Pop
47 608efcce Iustin Pop
import Ganeti.HTools.Types
48 608efcce Iustin Pop
49 608efcce Iustin Pop
type Key = Ndx
50 e4f08c46 Iustin Pop
type Elem = Int
51 e4f08c46 Iustin Pop
type PeerMap = [(Key, Elem)]
52 e4f08c46 Iustin Pop
53 9188aeef Iustin Pop
-- * Initialization functions
54 9188aeef Iustin Pop
55 9188aeef Iustin Pop
-- | Create a new empty map.
56 e4f08c46 Iustin Pop
empty :: PeerMap
57 e4f08c46 Iustin Pop
empty = []
58 e4f08c46 Iustin Pop
59 9188aeef Iustin Pop
-- | Our reverse-compare function.
60 e4f08c46 Iustin Pop
pmCompare :: (Key, Elem) -> (Key, Elem) -> Ordering
61 5182e970 Iustin Pop
pmCompare a b = comparing snd b a
62 e4f08c46 Iustin Pop
63 9188aeef Iustin Pop
-- | Add or update (via a custom function) an element.
64 e4f08c46 Iustin Pop
addWith :: (Elem -> Elem -> Elem) -> Key -> Elem -> PeerMap -> PeerMap
65 e4f08c46 Iustin Pop
addWith fn k v lst =
66 bbd8efd2 Iustin Pop
    case lookup k lst of
67 bbd8efd2 Iustin Pop
      Nothing -> insertBy pmCompare (k, v) lst
68 bbd8efd2 Iustin Pop
      Just o -> insertBy pmCompare (k, fn o v) (remove k lst)
69 e4f08c46 Iustin Pop
70 17c59f4b Iustin Pop
-- | Create a PeerMap from an association list, with possible duplicates
71 17c59f4b Iustin Pop
accumArray :: (Elem -> Elem -> Elem) -- ^ function used to merge the elements
72 17c59f4b Iustin Pop
              -> [(Key, Elem)]       -- ^ source data
73 17c59f4b Iustin Pop
              -> PeerMap             -- ^ results
74 bbd8efd2 Iustin Pop
accumArray _  [] = empty
75 bbd8efd2 Iustin Pop
accumArray fn ((k, v):xs) = addWith fn k v $ accumArray fn xs
76 e4f08c46 Iustin Pop
77 9188aeef Iustin Pop
-- * Basic operations
78 9188aeef Iustin Pop
79 9188aeef Iustin Pop
-- | Returns either the value for a key or zero if not found
80 e4f08c46 Iustin Pop
find :: Key -> PeerMap -> Elem
81 9f6dcdea Iustin Pop
find k = fromMaybe 0 . lookup k
82 e4f08c46 Iustin Pop
83 9188aeef Iustin Pop
-- | Add an element to a peermap, overwriting the previous value
84 e4f08c46 Iustin Pop
add :: Key -> Elem -> PeerMap -> PeerMap
85 9f6dcdea Iustin Pop
add = addWith (flip const)
86 e4f08c46 Iustin Pop
87 9188aeef Iustin Pop
-- | Remove an element from a peermap
88 e4f08c46 Iustin Pop
remove :: Key -> PeerMap -> PeerMap
89 bbd8efd2 Iustin Pop
remove _ [] = []
90 bbd8efd2 Iustin Pop
remove k ((x@(x', _)):xs) = if k == x'
91 bbd8efd2 Iustin Pop
                            then xs
92 9f6dcdea Iustin Pop
                            else x:remove k xs
93 e4f08c46 Iustin Pop
94 9188aeef Iustin Pop
-- | Find the maximum element.
95 9188aeef Iustin Pop
--
96 9188aeef Iustin Pop
-- Since this is a sorted list, we just get the value at the head of
97 9188aeef Iustin Pop
-- the list, or zero for a null list
98 e4f08c46 Iustin Pop
maxElem :: PeerMap -> Elem
99 bbd8efd2 Iustin Pop
maxElem (x:_) = snd x
100 bbd8efd2 Iustin Pop
maxElem _ = 0