module Radix.Word.Debug
  ( S (..)
  , validBelow
  ) where

import           Radix.Word.Foundation

import           Data.Bits



-- | Branch side.
data S = L -- ^ Left. Masked bit of the prefix above this node must be @0@.
       | R -- ^ Right. Masked bit of the prefix above this node must be @1@.
         deriving Int -> S -> ShowS
[S] -> ShowS
S -> String
(Int -> S -> ShowS) -> (S -> String) -> ([S] -> ShowS) -> Show S
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> S -> ShowS
showsPrec :: Int -> S -> ShowS
$cshow :: S -> String
show :: S -> String
$cshowList :: [S] -> ShowS
showList :: [S] -> ShowS
Show

-- | Check whether the key below aligns with the side the branch is on.
validBelow :: Prefix -> S -> Key -> Bool
validBelow :: Key -> S -> Key -> Bool
validBelow Key
p1 S
s Key
p2 =
  let q :: Key
q = Key
p2 Key -> Key -> Key
forall a. Bits a => a -> a -> a
.&. (Key
p1 Key -> Key -> Key
forall a. Bits a => a -> a -> a
.&. Key -> Key
forall a. Num a => a -> a
negate Key
p1)
  in Bool -> Bool
not (Key -> Key -> Bool
beyond Key
p1 Key
p2) Bool -> Bool -> Bool
&& case S
s of
                             S
L -> Key
q Key -> Key -> Bool
forall a. Eq a => a -> a -> Bool
== Key
0
                             S
R -> Key
q Key -> Key -> Bool
forall a. Eq a => a -> a -> Bool
/= Key
0