{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FunctionalDependencies, Safe #-} {- This module is part of Chatty. Copyleft (c) 2014 Marvin Cohrs All wrongs reversed. Sharing is an act of love, not crime. Please share Chatty with everyone you like. Chatty 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. Chatty 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 Chatty. If not, see . -} -- | Provides an unbalanced BST with a focus on the root. module Data.Chatty.Focus (Focus, focusSelect) where import Data.Chatty.BST import Data.Chatty.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