{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FunctionalDependencies, TypeFamilies #-}

{-
  This module is part of Antisplice.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Antisplice is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Antisplice is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Antisplice. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides an unbalanced BST with a focus on the root.
module Game.Antisplice.Utils.Focus (Focus, focusSelect) where

import Game.Antisplice.Utils.BST
import Game.Antisplice.Utils.None

-- | An unbalanced BST with a focus on the root.
newtype Focus a = Focus { runFocus :: BST a }

instance None (Focus a) where
  none = Focus none

instance Indexable i o v => AnyBST Focus i o v where
  anyBstInsert i t = Focus $ bstInsert i $ runFocus t
  anyBstRemove o t = Focus $ bstRemove o $ runFocus t
  anyBstMax t = bstMax $ runFocus t
  anyBstMin t = bstMin $ runFocus t
  anyBstLookup o t = bstLookup o $ runFocus t
  anyBstHead t = bstHead $ runFocus t
  anyBstEmpty = Focus none
  anyBstInorder t = bstInorder $ runFocus t

focusSelect' :: Indexable i o v => o -> BST i -> Maybe (BST i)
focusSelect' _ EmptyBST = Nothing
focusSelect' o t@(BST a l r)
  | o == indexOf a = Just t
  | o < indexOf a = case focusSelect' o l of
      Just (BST a' l' r') -> Just $ BST a' l' (BST a r' r)
      Nothing -> Nothing
  | otherwise = case focusSelect' o r of
      Nothing -> Nothing
      Just (BST a' l' r') -> Just $ BST a' (BST a l l') r'

focusInsert' :: Indexable i o v => i -> BST i -> BST i
focusInsert' i = unjust . focusSelect' (indexOf i) . bstInsert i
  where unjust (Just j) = j

-- | Rotate the tree such that the given element becomes the root node.
focusSelect o t = fmap Focus $ focusSelect' o $ runFocus t