26 lines
880 B
Haskell
26 lines
880 B
Haskell
module IntMapHelp
|
|
( module Data.IntMap.Strict
|
|
, newKey
|
|
, insertNewKey
|
|
, insertWithNewKeys
|
|
, swapKeys
|
|
)
|
|
where
|
|
--import Data.List hiding (foldr,insert)
|
|
import Data.IntMap.Strict
|
|
{- | Find a key value one higher than any key in the map, or zero if the map is
|
|
- empty -}
|
|
newKey :: IntMap a -> Int
|
|
newKey = maybe 0 ((+ 1) . fst) . lookupMax
|
|
{- | Insert an element with some new key. -}
|
|
insertNewKey :: a -> IntMap a -> IntMap a
|
|
insertNewKey x m = case lookupMax m of
|
|
Nothing -> singleton 0 x
|
|
Just (k,_) -> insert (k+1) x m
|
|
{- | Insert a list of values with new keys -}
|
|
insertWithNewKeys :: [a] -> IntMap a -> IntMap a
|
|
insertWithNewKeys xs m = union m $ fromList $ zip [newKey m..] xs
|
|
{- | Swaps two keys. Unsafe: assumes both keys exist. -}
|
|
swapKeys :: Int -> Int -> IntMap a -> IntMap a
|
|
swapKeys i j m = insert i (m ! j) $ insert j (m ! i) m
|