This commit is contained in:
2022-07-27 12:49:23 +01:00
parent 6554d219dc
commit 8d17ce66e9
106 changed files with 2911 additions and 2678 deletions
+48 -42
View File
@@ -2,74 +2,80 @@
--{-# LANGUAGE DeriveGeneric #-}
{-# OPTIONS_GHC -Wno-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{- |
WARNING: orphan instances concerning Aeson classes and Data.Strict.IntMap datatypes have been introduced.
The warnings have been disabled.
-}
-}
module IntMapHelp
( module Data.Strict.IntMap
--( module Data.IntMap.Strict
, newKey
, insertNewKey
, insertWithNewKeys
, unsafeSwapKeys
, safeSwapKeys
, findIndex
) where
import GHC.Generics
-- ( module Data.Strict.IntMap
(
module Data.IntMap.Strict,
newKey,
insertNewKey,
insertWithNewKeys,
unsafeSwapKeys,
safeSwapKeys,
findIndex,
) where
import Data.Aeson
import Data.IntMap.Strict
import GHC.Generics
import LensHelp
--import Data.IntMap.Strict
import Data.Strict.IntMap
import Data.Strict.Containers.Lens
--import Data.Strict.IntMap
--import Data.Strict.Containers.Lens
--import qualified Data.Strict.IntMap as IM
--import qualified Data.Strict.IntMap.Autogen.Strict as IM
--deriving instance Generic (IntMap a)
--deriving instance (Generic a) => Generic (IM.IntMap a)
instance ToJSON1 IntMap where
liftToJSON t tol = liftToJSON to' tol' . toList
where
to' = liftToJSON2 toJSON toJSONList t tol
tol' = liftToJSONList2 toJSON toJSONList t tol
liftToEncoding t tol = liftToEncoding to' tol' . toList
where
to' = liftToEncoding2 toEncoding toEncodingList t tol
tol' = liftToEncodingList2 toEncoding toEncodingList t tol
instance ToJSON a => ToJSON (IntMap a) where
toJSON = toJSON1
toEncoding = toEncoding1
instance FromJSON a => FromJSON (IntMap a) where
parseJSON = fmap fromList . parseJSON
--instance ToJSON1 IntMap where
-- liftToJSON t tol = liftToJSON to' tol' . toList
-- where
-- to' = liftToJSON2 toJSON toJSONList t tol
-- tol' = liftToJSONList2 toJSON toJSONList t tol
--
-- liftToEncoding t tol = liftToEncoding to' tol' . toList
-- where
-- to' = liftToEncoding2 toEncoding toEncodingList t tol
-- tol' = liftToEncodingList2 toEncoding toEncodingList t tol
--
--instance ToJSON a => ToJSON (IntMap a) where
-- toJSON = toJSON1
-- toEncoding = toEncoding1
--instance FromJSON a => FromJSON (IntMap a) where
-- parseJSON = fmap fromList . parseJSON
--instance ToJSON a => ToJSON (IntMap a) where
-- toEncoding = genericToEncoding defaultOptions
{- | Find a key value one higher than any key in the map, or zero if the map is
- empty -}
- empty
-}
newKey :: IntMap a -> Int
newKey = maybe 0 ((+ 1) . fst) . lookupMax
{- | Insert an element with some new key. -}
-- | 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 -}
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. -}
insertWithNewKeys xs m = union m $ fromList $ zip [newKey m ..] xs
-- | Swaps two keys. Unsafe: assumes both keys exist.
unsafeSwapKeys :: Int -> Int -> IntMap a -> IntMap a
unsafeSwapKeys i j m = insert i (m ! j) $ insert j (m ! i) m
safeSwapKeys :: Int -> Int -> IntMap a -> IntMap a
safeSwapKeys i j m = m
& at i .~ (m ^? ix j)
& at j .~ (m ^? ix i)
safeSwapKeys i j m =
m
& at i .~ (m ^? ix j)
& at j .~ (m ^? ix i)
-- ideally this should short circuit, I am not sure whether it does or not
-- though