summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AufgabeFFP8.hs119
1 files changed, 119 insertions, 0 deletions
diff --git a/AufgabeFFP8.hs b/AufgabeFFP8.hs
new file mode 100644
index 0000000..2747597
--- /dev/null
+++ b/AufgabeFFP8.hs
@@ -0,0 +1,119 @@
1module AufgabeFFP8
2where
3
4import Data.Array
5import Data.List hiding ((\\))
6import Test.QuickCheck
7
8type Nat = Int
9
10-- Minfree basic version
11
12minfree_bv :: [Int] -> Int
13minfree_bv xs = head ([0..] \\ xs)
14
15(\\) :: Eq a => [a] -> [a] -> [a]
16xs \\ ys = filter (\x -> x `notElem` ys) xs
17
18-- Minfree checklist
19
20minfree_chl :: [Int] -> Int
21minfree_chl = search . checklist
22
23search :: Array Int Bool -> Int
24search = length . takeWhile id . elems
25
26checklist :: [Int] -> Array Int Bool
27checklist xs = accumArray (||) False (0,n)
28 (zip (filter (<=n) xs) (repeat True))
29 where n = length xs
30
31-- minfree countlist
32
33minfree_col :: [Int] -> Int
34minfree_col = search0 . assocs . countlist
35
36countlist :: [Int] -> Array Int Int
37countlist xs = accumArray (+) 0 (0,n) (zip xs (repeat 1))
38 where n = length xs
39
40sort :: [Int] -> [Int]
41sort xs = concat [replicate k x | (x,k) <- assocs $ countlist xs]
42
43search0 :: [(Int, Int)] -> Int
44search0 [] = -1
45search0((i,0):_) = i
46search0 (x:xs) = search0 xs
47
48-- minfree basic daq
49
50minfree_b :: [Int] -> Int
51minfree_b xs = if (null ([0..b-1] \\ us))
52 then (head ([b..] \\ vs))
53 else (head ([0..] \\ us))
54 where
55 b = 1 + (length xs) `div` 2
56 (us, vs) = partition (<b) xs
57
58-- minfree refined daq
59
60minfree_r :: [Int] -> Int
61minfree_r = minfrom_r 0
62
63minfrom_r :: Nat -> [Nat] -> Nat
64minfrom_r a xs
65 | null xs = a
66 | length us == b-a = minfrom_r b vs
67 | otherwise = minfrom_r b us
68 where
69 b = a + 1 + (length xs) `div` 2
70 (us, vs) = partition (<b) xs
71
72-- minfree optimized daq (p262)
73
74minfree_o :: [Int] -> Int
75minfree_o xs = minfrom_o 0 (length xs, xs)
76
77minfrom_o :: Int -> (Int, [Int]) -> Int
78minfrom_o a (n, xs)
79 | n == 0 = a
80 | m == b-a = minfrom_o b (n-m, vs)
81 | otherwise = minfrom_o a (m, us)
82 where
83 (us,vs) = partition (<b) xs
84 b = a + 1 + n `div` 2
85 m = length us
86
87-- true daq implementations
88
89divideAndConquer :: (p->Bool) -> (p->s) ->(p-> [p]) -> (p-> [s] -> s)-> p -> s
90divideAndConquer indiv solve divide combine initPb = dAC initPb
91 where
92 dAC pb
93 | indiv pb = solve pb
94 | otherwise = combine pb (map dAC (divide pb))
95
96-- minfree basic daq higher order
97
98b_indiv :: Int -> [Int] -> Bool
99b_indiv l xs = length xs < l
100
101b_solve :: [Int] -> [Int]
102b_solve = id
103
104b_divide :: Int -> [Int] -> [[Int]]
105b_divide b xs = [us, vs]
106 where (us,vs) = partition (<b) xs
107
108b_combine :: Int -> [Int] -> [[Int]] -> [Int]
109b_combine b _ (us:vs:[]) = if (null ([0..b-1] \\ us))
110 then ([b..] \\ vs)
111 else ([0..] \\ us)
112
113minfree_bhof :: [Int] -> Int
114minfree_bhof xs = head $ divideAndConquer (b_indiv (length xs)) b_solve (b_divide b) (b_combine b) xs
115 where b = 1+(length xs) `div` 2
116
117-- minfree refined daq higher order
118
119