{-# LANGUAGE TypeSynonymInstances #-}

module HaskellWorks.Data.Positioning
  ( Count
  , Position
  , lastPositionOf
  , toCount
  , toPosition
  ) where

import Data.Int
import Data.Word

-- | A value representing a count
type Count = Word64

-- | A value representing a position
type Position = Int64

class ToPosition a where
  -- | Convert to a position
  toPosition :: a -> Position

instance ToPosition Count where
  toPosition :: Count -> Position
toPosition = Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  {-# INLINE toPosition #-}

class ToCount a where
  -- | Convert a count to a count
  toCount :: a -> Count

instance ToCount Position where
  toCount :: Position -> Count
toCount = Position -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  {-# INLINE toCount #-}

-- | Get largest position in a sequenced container of size count.
lastPositionOf :: Count -> Position
lastPositionOf :: Count -> Position
lastPositionOf Count
c  = Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
c Position -> Position -> Position
forall a. Num a => a -> a -> a
- Position
1
{-# INLINE lastPositionOf #-}