refactor Matrix to use an ordered map

This commit is contained in:
JasterV 2025-03-26 00:42:59 +01:00
parent fda3ec56bc
commit 53c6d3f68f

View file

@ -12,17 +12,16 @@ module Day4.Matrix
) )
where where
import Data.HashMap.Lazy (HashMap)
import qualified Data.HashMap.Lazy as HashMap
import qualified Data.IntMap.Lazy as IntMap import qualified Data.IntMap.Lazy as IntMap
import Data.List (sortOn) import Data.Map.Lazy (Map)
import qualified Data.Map.Lazy as Map
import Data.Maybe import Data.Maybe
import Prelude hiding (lookup) import Prelude hiding (lookup)
newtype Matrix v = Matrix (HashMap (Int, Int) v) newtype Matrix v = Matrix (Map (Int, Int) v)
buildMatrix :: [[a]] -> Matrix a buildMatrix :: [[a]] -> Matrix a
buildMatrix xs = Matrix (go xs 0 HashMap.empty) buildMatrix xs = Matrix (go xs 0 Map.empty)
where where
go [] _ acc = acc go [] _ acc = acc
go (x : xs') row acc = go (x : xs') row acc =
@ -31,17 +30,17 @@ buildMatrix xs = Matrix (go xs 0 HashMap.empty)
parseRow [] _ acc = acc parseRow [] _ acc = acc
parseRow (x : xs') (row, column) acc = parseRow (x : xs') (row, column) acc =
let acc' = HashMap.insert (row, column) x acc let acc' = Map.insert (row, column) x acc
in parseRow xs' (row, column + 1) acc' in parseRow xs' (row, column + 1) acc'
size :: Matrix v -> Int size :: Matrix v -> Int
size (Matrix hmap) = HashMap.size hmap size (Matrix hmap) = Map.size hmap
filterWithKey :: ((Int, Int) -> v -> Bool) -> Matrix v -> Matrix v filterWithKey :: ((Int, Int) -> v -> Bool) -> Matrix v -> Matrix v
filterWithKey f (Matrix hmap) = Matrix (HashMap.filterWithKey f hmap) filterWithKey f (Matrix hmap) = Matrix (Map.filterWithKey f hmap)
lookup :: (Int, Int) -> Matrix v -> Maybe v lookup :: (Int, Int) -> Matrix v -> Maybe v
lookup position (Matrix hmap) = HashMap.lookup position hmap lookup position (Matrix hmap) = Map.lookup position hmap
lookupMultiple :: [(Int, Int)] -> Matrix v -> [v] lookupMultiple :: [(Int, Int)] -> Matrix v -> [v]
lookupMultiple positions matrix = mapMaybe (`lookup` matrix) positions lookupMultiple positions matrix = mapMaybe (`lookup` matrix) positions
@ -53,7 +52,7 @@ The values are grouped in order.
--} --}
groupWith :: forall v. ((Int, Int) -> Int) -> Matrix v -> [[v]] groupWith :: forall v. ((Int, Int) -> Int) -> Matrix v -> [[v]]
groupWith f (Matrix hmap) = groupWith f (Matrix hmap) =
let sortedEntries = sortOn fst $ HashMap.toList hmap let sortedEntries = Map.toAscList hmap
intMap = foldr (\(position, value) -> insertValue (f position) value) IntMap.empty sortedEntries intMap = foldr (\(position, value) -> insertValue (f position) value) IntMap.empty sortedEntries
in IntMap.elems intMap in IntMap.elems intMap
where where