Add 'Read' instances for most objects
[ganeti-local] / Ganeti / HTools / Container.hs
index 937676c..d24bb79 100644 (file)
@@ -32,23 +32,25 @@ module Ganeti.HTools.Container
      Container
     , Key
      -- * Creation
      Container
     , Key
      -- * Creation
-    , empty
+    , IntMap.empty
+    , IntMap.singleton
     , fromAssocList
      -- * Query
     , fromAssocList
      -- * Query
-    , size
+    , IntMap.size
+    , IntMap.null
     , find
     , find
+    , IntMap.findMax
      -- * Update
     , add
     , addTwo
      -- * Update
     , add
     , addTwo
-    , remove
     , IntMap.map
     , IntMap.mapAccum
     , IntMap.map
     , IntMap.mapAccum
+    , IntMap.filter
     -- * Conversion
     -- * Conversion
-    , elems
-    , keys
+    , IntMap.elems
+    , IntMap.keys
     -- * Element functions
     , nameOf
     -- * Element functions
     , nameOf
-    , maxNameLen
     , findByName
     ) where
 
     , findByName
     ) where
 
@@ -59,14 +61,6 @@ import qualified Ganeti.HTools.Types as T
 type Key = IntMap.Key
 type Container = IntMap.IntMap
 
 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 = (IntMap.! k)
 -- | Locate a key in the map (must exist).
 find :: Key -> Container a -> a
 find k = (IntMap.! k)
@@ -75,18 +69,6 @@ find k = (IntMap.! k)
 add :: Key -> a -> Container a -> Container a
 add = IntMap.insert
 
 add :: Key -> a -> Container a -> Container a
 add = IntMap.insert
 
--- | 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
-
 -- | Create a map from an association list.
 fromAssocList :: [(Key, a)] -> Container a
 fromAssocList = IntMap.fromList
 -- | Create a map from an association list.
 fromAssocList :: [(Key, a)] -> Container a
 fromAssocList = IntMap.fromList
@@ -99,20 +81,12 @@ addTwo k1 v1 k2 v2 = add k1 v1 . add k2 v2
 nameOf :: (T.Element a) => Container a -> Key -> String
 nameOf c k = T.nameOf $ find k c
 
 nameOf :: (T.Element a) => Container a -> Key -> String
 nameOf c k = T.nameOf $ find k c
 
--- | Compute the maximum name length in an Element Container.
-maxNameLen :: (T.Element a) => Container a -> Int
-maxNameLen = maximum . map (length . T.nameOf) . elems
-
 -- | Find an element by name in a Container; this is a very slow function.
 findByName :: (T.Element a, Monad m) =>
 -- | Find an element by name in a Container; this is a very slow function.
 findByName :: (T.Element a, Monad m) =>
-              Container a -> String -> m Key
+              Container a -> String -> m a
 findByName c n =
 findByName c n =
-    let all_elems = elems c
-        result = filter ((== n) . T.nameOf) all_elems
-        nems = length result
-    in
-      if nems /= 1 then
-          fail $ "Wrong number of elems (" ++ show nems ++
-                   ") found with name " ++ n
-      else
-          return $ T.idxOf $ head result
+    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