module Data.Ix.List where
import Control.Exception (assert)
list2idx :: (Enum a, Bounded a) => [a] -> Int
list2idx xs = assert (not $ null xs) l2i 1 xs where
l2i k [x] = k * fromEnum x
l2i k (x:xs) = k * fromEnum x + l2i (k * c) xs where
c = fromEnum (maxBound `asTypeOf` x) fromEnum (minBound `asTypeOf` x) + 1
listAll2idx :: (Enum a, Bounded a) => [a] -> Int
listAll2idx [] = 0
listAll2idx xs = l2i 1 xs where
l2i k [x] = k * (fromEnum x +1)
l2i k (x:xs) = k * (fromEnum x +1) + l2i (k * c) xs where
c = fromEnum (maxBound `asTypeOf` x) fromEnum (minBound `asTypeOf` x) + 2
blist2idx :: (Enum a, Bounded a) => (a,a) -> [a] -> Int
blist2idx (bmin,bmax) xs = assert (not $ null xs) $ l2i 1 xs where
l2i k [x] = k * (fromEnum x emin)
l2i k (x:xs) = k * (fromEnum x emin) + l2i (k*c) xs where
c = emax emin + 1
emin = fromEnum bmin
emax = fromEnum bmax