module Mischief.ECS.EntityDef where

import GHC.Base (Word (W#), Word#, compareWord#, eqWord#, isTrue#)

-- |
-- __UNLIFTED__
--
-- Points to an unique entity. The first @Word#@ is an id, the second is a generation.
--
-- It is guaranteed that there can't be two alive entities with the same id.
data Entity = Entity (# Word#, Word# #)

instance Show Entity where
  show :: Entity -> String
  show :: Entity -> String
show (Entity (# Word#
id, Word#
0## #)) = Word -> String
forall a. Show a => a -> String
show (Word# -> Word
W# Word#
id) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"v" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"."
  show (Entity (# Word#
id, Word#
gen #)) = Word -> String
forall a. Show a => a -> String
show (Word# -> Word
W# Word#
id) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"v" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word -> String
forall a. Show a => a -> String
show (Word# -> Word
W# Word#
gen)

instance Eq Entity where
  (==) :: Entity -> Entity -> Bool
  == :: Entity -> Entity -> Bool
(==) (Entity (# Word#
id1, Word#
gen1 #)) (Entity (# Word#
id2, Word#
gen2 #)) = Int# -> Bool
isTrue# (Word# -> Word# -> Int#
eqWord# Word#
id1 Word#
id2) Bool -> Bool -> Bool
&& Int# -> Bool
isTrue# (Word# -> Word# -> Int#
eqWord# Word#
gen1 Word#
gen2)

instance Ord Entity where
  compare :: Entity -> Entity -> Ordering
  compare :: Entity -> Entity -> Ordering
compare (Entity (# Word#
id1, Word#
gen1 #)) (Entity (# Word#
id2, Word#
gen2 #)) =
    case Word# -> Word# -> Ordering
compareWord# Word#
id1 Word#
id2 of
      Ordering
EQ -> Word# -> Word# -> Ordering
compareWord# Word#
gen1 Word#
gen2
      Ordering
x -> Ordering
x

newtype Entity# = Entity# (# Word#, Word# #)

liftEntity :: Entity# -> Entity
liftEntity :: Entity# -> Entity
liftEntity (Entity# (# Word#, Word# #)
a) = (# Word#, Word# #) -> Entity
Entity (# Word#, Word# #)
a

unliftEntity :: Entity -> Entity#
unliftEntity :: Entity -> Entity#
unliftEntity (Entity (# Word#, Word# #)
a) = (# Word#, Word# #) -> Entity#
Entity# (# Word#, Word# #)
a

eqEntity# :: Entity# -> Entity# -> Bool
eqEntity# :: Entity# -> Entity# -> Bool
eqEntity# (Entity# (# Word#
id1, Word#
gen1 #)) (Entity# (# Word#
id2, Word#
gen2 #)) = Int# -> Bool
isTrue# (Word# -> Word# -> Int#
eqWord# Word#
id1 Word#
id2) Bool -> Bool -> Bool
&& Int# -> Bool
isTrue# (Word# -> Word# -> Int#
eqWord# Word#
gen1 Word#
gen2)

compareEntity# :: Entity# -> Entity# -> Ordering
compareEntity# :: Entity# -> Entity# -> Ordering
compareEntity# (Entity# (# Word#
id1, Word#
gen1 #)) (Entity# (# Word#
id2, Word#
gen2 #)) =
  case Word# -> Word# -> Ordering
compareWord# Word#
id1 Word#
id2 of
    Ordering
EQ -> Word# -> Word# -> Ordering
compareWord# Word#
gen1 Word#
gen2
    Ordering
x -> Ordering
x