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
- , maxNameLen
, findByName
) where
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)
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
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) =>
- Container a -> String -> m Key
+ Container a -> String -> m a
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