Skip to content

Instantly share code, notes, and snippets.

View kozross's full-sized avatar

Koz Ross kozross

View GitHub Profile
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
module Data.CRLF where
import GHC.Generics
import Control.Monad.Except
import Data.Binary
import qualified Data.Vector.Unboxed as V
data Categorical = -- some definition of a type here
data Discrete = -- some definition of a type here
data Continuous = -- some definition of a type here
type family ColumnData a where
ColumnData Categorical = V.Vector Word32
{-# LANGUAGE DataKinds, DeriveFunctor, GADTs, KindSignatures,
ScopedTypeVariables, TemplateHaskell, TypeFamilies,
TypeApplications #-}
module Data.Massiv.Array.Sized.One where
import CoercibleUtils
import Data.Coerce
import Data.Finite
import Data.Foldable
-- Suppose these are the mappings:
mappingsAsList :: [((Int,Int), Bool)]
mappingsAsList [((1,2), True), ((1,3), False), ((1,4), True),
((2,4), True), ((2,5), False), ((2,6), True)]
-- I wanna be able to efficiently request 'give me all mappings where the first part of the key is x'.
-- For instance, if I asked for x = 1, I should get [((1,2), True), ((1,3), False), ((1,4), True)]
-- I also wanna be able to efficiently request 'give me all mappings where the second part of the key is y'.
-- For instance, if I asked for y = 4, I should get [((1,4), True), ((2,4), True)]
newtype AtomP = AtomP Word64
view :: AtomP -> Maybe (Bool, Word64)
view x = let mask = complement (bit 63 .|. bit 62)
raw = coerce x in
if | testBit raw 63 -> Nothing
| testBit raw 62 -> Just (False, pext64 raw mask)
| otherwise -> Just (True, pext64 raw mask)
-- I want to expose a view of AtomP as a Maybe (Bool, Word64) on the basis of the function above.
newtype Atom =
Atom Word64
preview :: Atom -> (Maybe Bool, Word64)
preview x =
let raw = coerce x
f = shorten raw
in if | testBit raw 63 -> (Nothing, f)
| testBit raw 62 -> (Just False, f)
| otherwise -> (Just True, f)