1 {-| Module abstracting the node and instance container implementation.
3 This is currently implemented on top of an 'IntMap', which seems to
4 give the best performance for our workload.
10 Copyright (C) 2009 Google Inc.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 module Ganeti.HTools.Container
49 -- * Element functions
55 import qualified Data.IntMap as IntMap
57 import qualified Ganeti.HTools.Types as T
60 type Container = IntMap.IntMap
62 -- | Create an empty container.
66 -- | Returns the number of elements in the map.
67 size :: Container a -> Int
70 -- | Locate a key in the map (must exist).
71 find :: Key -> Container a -> a
72 find k c = c IntMap.! k
74 -- | Locate a keyin the map returning a default value if not existing.
75 findWithDefault :: a -> Key -> Container a -> a
76 findWithDefault = IntMap.findWithDefault
78 -- | Add or update one element to the map.
79 add :: Key -> a -> Container a -> Container a
80 add k v c = IntMap.insert k v c
82 -- | Remove an element from the map.
83 remove :: Key -> Container a -> Container a
84 remove = IntMap.delete
86 -- | Return the list of values in the map.
87 elems :: Container a -> [a]
90 -- | Return the list of keys in the map.
91 keys :: Container a -> [Key]
94 -- | Create a map from an association list.
95 fromAssocList :: [(Key, a)] -> Container a
96 fromAssocList = IntMap.fromList
98 -- | Create a map from an association list with a combining function.
99 fromListWith :: (a -> a -> a) -> [(Key, a)] -> Container a
100 fromListWith = IntMap.fromListWith
102 -- | Fold over the values of the map.
103 fold :: (a -> b -> b) -> b -> Container a -> b
106 -- | Add or update two elements of the map.
107 addTwo :: Key -> a -> Key -> a -> Container a -> Container a
108 addTwo k1 v1 k2 v2 c = add k1 v1 $ add k2 v2 c
110 -- | Compute the name of an element in a container.
111 nameOf :: (T.Element a) => Container a -> Key -> String
112 nameOf c k = T.nameOf $ find k c
114 -- | Compute the maximum name length in an Element Container.
115 maxNameLen :: (T.Element a) => Container a -> Int
116 maxNameLen = maximum . map (length . T.nameOf) . elems
118 -- | Find an element by name in a Container; this is a very slow function.
119 findByName :: (T.Element a, Monad m) =>
120 Container a -> String -> m Key
122 let all_elems = elems c
123 result = filter ((== n) . T.nameOf) all_elems
127 fail $ "Wrong number of elems (" ++ (show nems) ++
128 ") found with name " ++ n
130 return $ T.idxOf $ head result