-
Notifications
You must be signed in to change notification settings - Fork 733
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refined to generic tournament tree by abstract min/inf
- Loading branch information
1 parent
907991c
commit cccabe6
Showing
3 changed files
with
49 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,73 @@ | ||
-- TournamentTr.hs | ||
-- Copyright (C) 2013 Liu Xinyu (liuxinyu95@gmail.com) | ||
-- | ||
-- | ||
-- This program is free software: you can redistribute it and/or modify | ||
-- it under the terms of the GNU General Public License as published by | ||
-- the Free Software Foundation, either version 3 of the License, or | ||
-- (at your option) any later version. | ||
-- | ||
-- | ||
-- This program 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 General Public License for more details. | ||
-- | ||
-- | ||
-- You should have received a copy of the GNU General Public License | ||
-- along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
-- Tournament tree based selection sort | ||
-- [1] Donald E. Knuth. ``The Art of Computer Programming, Volume 3: Sorting and Searching (2nd Edition)''. | ||
-- [1] Donald E. Knuth. ``The Art of Computer Programming, Volume 3: Sorting and Searching (2nd Edition)''. | ||
-- Addison-Wesley Professional; 2 edition (May 4, 1998) ISBN-10: 0201896850 ISBN-13: 978-0201896855 | ||
|
||
module TrounamentTr where | ||
|
||
import Test.QuickCheck -- for verification purpose only | ||
import Data.List(sort) -- for verification purpose only | ||
import Test.QuickCheck -- for verification | ||
import Data.List (sort, sortBy) -- for verification | ||
|
||
-- Note: in order to derive from Ord for free, the order must be: | ||
-- Note: in order to derive from Ord for free, the order must be: | ||
-- negative infinity, regular, then positive infinity | ||
data Infinite a = NegInf | Only a | Inf deriving (Eq, Show, Ord) | ||
|
||
only (Only x) = x | ||
|
||
data Tr a = Empty | Br (Tr a) a (Tr a) deriving Show | ||
data Tr a = Empty | Br (Tr a) (Infinite a) (Tr a) deriving Show | ||
|
||
key (Br _ k _ ) = k | ||
|
||
wrap x = Br Empty (Only x) Empty | ||
|
||
branch t1 t2 = Br t1 (min (key t1) (key t2)) t2 | ||
only (Only x) = x | ||
|
||
minBy p a b = if p a b then a else b | ||
|
||
branch p t1 t2 = Br t1 (minBy p (key t1) (key t2)) t2 | ||
|
||
fromList :: (Ord a) => [a] -> Tr (Infinite a) | ||
fromList = build . (map wrap) where | ||
-- fromList :: (Ord a) => (Infinite a -> Infinite a -> Bool) -> [a] -> Tr a | ||
fromList p xs = build $ map wrap xs where | ||
build [] = Empty | ||
build [t] = t | ||
build ts = build $ pair ts | ||
pair (t1:t2:ts) = (branch t1 t2):pair ts | ||
build ts = build $ pair ts | ||
pair (t1:t2:ts) = (branch p t1 t2) : pair ts | ||
pair ts = ts | ||
|
||
pop (Br Empty _ Empty) = Br Empty Inf Empty | ||
pop (Br l k r) | k == key l = let l' = pop l in Br l' (min (key l') (key r)) r | ||
| k == key r = let r' = pop r in Br l (min (key l) (key r')) r' | ||
|
||
pop p inf = delMin where | ||
delMin (Br Empty _ Empty) = Br Empty inf Empty | ||
delMin (Br l k r) | k == key l = let l' = delMin l in Br l' (minBy p (key l') (key r)) r | ||
| k == key r = let r' = delMin r in Br l (minBy p (key l) (key r')) r' | ||
|
||
top = only . key | ||
|
||
tsort :: (Ord a) => [a] -> [a] | ||
tsort = sort' . fromList where | ||
-- tsortBy :: (Ord a) => (Infinite a -> Infinite a -> Bool) -> Infinite a -> [a] -> [a] | ||
tsortBy p inf xs = sort' $ fromList p xs where | ||
sort' Empty = [] | ||
sort' (Br _ Inf _) = [] | ||
sort' t = (top t) : (sort' $ pop t) | ||
sort' t | inf == key t = [] | ||
| otherwise = (top t) : (sort' $ pop p inf t) | ||
|
||
tsort = tsortBy (<=) Inf | ||
|
||
prop_tsort :: [Int]->Bool | ||
prop_tsort :: [Int] -> Bool | ||
prop_tsort xs = (sort xs) == (tsort xs) | ||
|
||
prop_tsort_des :: [Int] -> Bool | ||
prop_tsort_des xs = (sortBy (flip compare) xs) == (tsortBy (>=) NegInf xs) | ||
|
||
testAll = do | ||
quickCheck prop_tsort | ||
quickCheck prop_tsort_des |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters