Commit 99da29a1 authored by Yiannis Tsiouris's avatar Yiannis Tsiouris

Define a type for Freq

parent dac8563f
...@@ -20,6 +20,8 @@ module Table( new ...@@ -20,6 +20,8 @@ module Table( new
import Data.Array (Array, elems, listArray, (!), (//)) import Data.Array (Array, elems, listArray, (!), (//))
type Freq = [Int]
-- Note: Hash tables have a fixed number of slots but each slot can store -- Note: Hash tables have a fixed number of slots but each slot can store
-- a list of vertices. The functions is_member/3 and insert/3 -- a list of vertices. The functions is_member/3 and insert/3
-- expect its slot argument to be in range. -- expect its slot argument to be in range.
...@@ -44,58 +46,58 @@ insert x i t = t // [(i, x : t ! i)] ...@@ -44,58 +46,58 @@ insert x i t = t // [(i, x : t ! i)]
-- the output is a list of integers where the number at position I -- the output is a list of integers where the number at position I
-- indicates how many slots of T are filled with I entries; -- indicates how many slots of T are filled with I entries;
-- the sum of the output lists equals the number of slots of T. -- the sum of the output lists equals the number of slots of T.
get_freq :: Array Int [Int] -> [Int] get_freq :: Array Int [Int] -> Freq
get_freq t = elems $ foldl (flip inc) freqArr freqs get_freq t = elems $ foldl (flip inc) freqArr freqs
where freqs = map length $ elems t where freqs = map length $ elems t
maxFreq = foldl max (head freqs) (tail freqs) maxFreq = foldl max (head freqs) (tail freqs)
freqArr = listArray (0, maxFreq) $ cycle [0] freqArr = listArray (0, maxFreq) $ cycle [0]
-- freq_to_slots computes the number of slots from a table fill frequency. -- freq_to_slots computes the number of slots from a table fill frequency.
freq_to_slots :: [Int] -> Int freq_to_slots :: Freq -> Int
freq_to_slots = sum freq_to_slots = sum
-- freq_to_nonempty_slots computes the number of non empty slots from a table -- freq_to_nonempty_slots computes the number of non empty slots from a table
-- fill frequency. -- fill frequency.
freq_to_nonempty_slots :: [Int] -> Int freq_to_nonempty_slots :: Freq -> Int
freq_to_nonempty_slots = sum . tail freq_to_nonempty_slots = sum . tail
-- freq_to_vertices computes the number of vertices from a table fill frequency. -- freq_to_vertices computes the number of vertices from a table fill frequency.
freq_to_vertices :: [Int] -> Int freq_to_vertices :: Freq -> Int
freq_to_vertices f = snd $ foldl (\(i, x) n -> (i + 1, (i * n + x))) (0, 0) f freq_to_vertices f = snd $ foldl (\(i, x) n -> (i + 1, (i * n + x))) (0, 0) f
-- max_freq returns the maximum fill frequency. -- max_freq returns the maximum fill frequency.
max_freq :: [Int] -> Int max_freq :: Freq -> Int
max_freq f = length f - 1 max_freq f = length f - 1
-- avg_freq returns the average fill frequency -- avg_freq returns the average fill frequency
avg_freq :: [Int] -> Float avg_freq :: Freq -> Float
avg_freq f = (fi $ freq_to_vertices f) / (fi $ freq_to_slots f) avg_freq f = (fi $ freq_to_vertices f) / (fi $ freq_to_slots f)
-- avg_nonempty_freq returns the average fill frequency of non empty slots. -- avg_nonempty_freq returns the average fill frequency of non empty slots.
avg_nonempty_freq :: [Int] -> Float avg_nonempty_freq :: Freq -> Float
avg_nonempty_freq f = avg_nonempty_freq f =
case freq_to_vertices f of case freq_to_vertices f of
verts | verts > 0 -> (fi verts) / (fi $ freq_to_nonempty_slots f) verts | verts > 0 -> (fi verts) / (fi $ freq_to_nonempty_slots f)
otherwise -> 0.0 otherwise -> 0.0
-- fill_deg determines the filling degree of the table. -- fill_deg determines the filling degree of the table.
fill_deg :: [Int] -> Float fill_deg :: Freq -> Float
fill_deg f = (fi $ freq_to_nonempty_slots f) / (fi $ freq_to_slots f) fill_deg f = (fi $ freq_to_nonempty_slots f) / (fi $ freq_to_slots f)
-- sum_freqs/2 sums two fill frequencies. -- sum_freqs/2 sums two fill frequencies.
sum_freqs2 :: [Int] -> [Int] -> [Int] sum_freqs2 :: Freq -> Freq -> [Int]
sum_freqs2 [] sumF = sumF sum_freqs2 [] sumF = sumF
sum_freqs2 f [] = f sum_freqs2 f [] = f
sum_freqs2 (n : f) (m : sumF) = n + m : sum_freqs2 f sumF sum_freqs2 (n : f) (m : sumF) = n + m : sum_freqs2 f sumF
-- sum_freqs/1 sums a list of fill frequencies. -- sum_freqs/1 sums a list of fill frequencies.
sum_freqs :: [[Int]] -> [Int] sum_freqs :: [Freq] -> [Int]
sum_freqs fs = foldl (flip sum_freqs2) [] fs sum_freqs fs = foldl (flip sum_freqs2) [] fs
{- {- XXX: Fix below functions to type-check!
-- freq_to_stat produces a readable statistics from a table fill frequency; -- freq_to_stat produces a readable statistics from a table fill frequency;
-- the input frequency F is itself part of the statistics -- the input frequency F is itself part of the statistics
freq_to_stat :: [Int] -> [(String, a)] freq_to_stat :: Freq -> [(String, a)]
freq_to_stat frequency = [ --("freq", frequency) freq_to_stat frequency = [ --("freq", frequency)
("size", freq_to_vertices frequency) ("size", freq_to_vertices frequency)
, ("slots", freq_to_slots frequency) , ("slots", freq_to_slots frequency)
...@@ -108,7 +110,7 @@ freq_to_stat frequency = [ --("freq", frequency) ...@@ -108,7 +110,7 @@ freq_to_stat frequency = [ --("freq", frequency)
-- freq_from_stat extracts a table fill frequency from a statistics Stat -- freq_from_stat extracts a table fill frequency from a statistics Stat
-- (assuming Stat was produced by freq_to_stat/1, otherwise returns []); -- (assuming Stat was produced by freq_to_stat/1, otherwise returns []);
freq_from_stat :: [(String, a)] -> [Int] freq_from_stat :: [(String, a)] -> Freq
freq_from_stat stat = freq_from_stat stat =
case "freq" `lookup` stat of case "freq" `lookup` stat of
Just val -> val Just val -> val
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment