X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/669d7e3d1ce8f832a7ff4b11de4f2bcab9494c27..99b63608c593b37bead28376777fa95e49825a51:/Ganeti/HTools/Container.hs diff --git a/Ganeti/HTools/Container.hs b/Ganeti/HTools/Container.hs index 0fb2c7f..d24bb79 100644 --- a/Ganeti/HTools/Container.hs +++ b/Ganeti/HTools/Container.hs @@ -5,74 +5,88 @@ give the best performance for our workload. -} +{- + +Copyright (C) 2009 Google Inc. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301, USA. + +-} + module Ganeti.HTools.Container ( -- * Types Container + , Key -- * Creation - , empty + , IntMap.empty + , IntMap.singleton , fromAssocList -- * Query - , size + , IntMap.size + , IntMap.null , find + , IntMap.findMax -- * Update , add , addTwo - , remove + , IntMap.map + , IntMap.mapAccum + , IntMap.filter -- * Conversion - , elems - , keys + , IntMap.elems + , IntMap.keys + -- * Element functions + , nameOf + , findByName ) where import qualified Data.IntMap as IntMap +import qualified Ganeti.HTools.Types as T + type Key = IntMap.Key type Container = IntMap.IntMap --- | Create an empty container. -empty :: Container a -empty = IntMap.empty - --- | Returns the number of elements in the map. -size :: Container a -> Int -size = IntMap.size - -- | Locate a key in the map (must exist). find :: Key -> Container a -> a -find k c = c IntMap.! k - --- | Locate a keyin the map returning a default value if not existing. -findWithDefault :: a -> Key -> Container a -> a -findWithDefault = IntMap.findWithDefault +find k = (IntMap.! k) -- | Add or update one element to the map. add :: Key -> a -> Container a -> Container a -add k v c = IntMap.insert k v c - --- | Remove an element from the map. -remove :: Key -> Container a -> Container a -remove = IntMap.delete - --- | Return the list of values in the map. -elems :: Container a -> [a] -elems = IntMap.elems - --- | Return the list of keys in the map. -keys :: Container a -> [Key] -keys = IntMap.keys +add = IntMap.insert -- | Create a map from an association list. fromAssocList :: [(Key, a)] -> Container a fromAssocList = IntMap.fromList --- | Create a map from an association list with a combining function. -fromListWith :: (a -> a -> a) -> [(Key, a)] -> Container a -fromListWith = IntMap.fromListWith - --- | Fold over the values of the map. -fold :: (a -> b -> b) -> b -> Container a -> b -fold = IntMap.fold - -- | Add or update two elements of the map. addTwo :: Key -> a -> Key -> a -> Container a -> Container a -addTwo k1 v1 k2 v2 c = add k1 v1 $ add k2 v2 c +addTwo k1 v1 k2 v2 = add k1 v1 . add k2 v2 + +-- | Compute the name of an element in a container. +nameOf :: (T.Element a) => Container a -> Key -> String +nameOf c k = T.nameOf $ find k c + +-- | Find an element by name in a Container; this is a very slow function. +findByName :: (T.Element a, Monad m) => + Container a -> String -> m a +findByName c n = + let all_elems = IntMap.elems c + result = filter ((n `elem`) . T.allNames) all_elems + in case result of + [item] -> return item + _ -> fail $ "Wrong number of elems found with name " ++ n