More flexible size hints
[darcs-mirrors/vector.git] / Data / Vector / Stream / Size.hs
1 module Data.Vector.Stream.Size (
2 Size(..), smaller, larger, toMax, upperBound
3 ) where
4
5 data Size = Exact Int
6 | Max Int
7 | Unknown
8 deriving( Eq, Show )
9
10 instance Num Size where
11 Exact m + Exact n = Exact (m+n)
12 Exact m + Max n = Max (m+n)
13
14 Max m + Exact n = Max (m+n)
15 Max m + Max n = Max (m+n)
16
17 _ + _ = Unknown
18
19
20 Exact m - Exact n = Exact (m-n)
21 Exact m - Max n = Max m
22
23 Max m - Exact n = Max (m-n)
24 Max m - Max n = Max m
25 Max m - Unknown = Max m
26
27 _ - _ = Unknown
28
29
30 fromInteger n = Exact (fromInteger n)
31
32
33 smaller :: Size -> Size -> Size
34 smaller (Exact m) (Exact n) = Exact (m `min` n)
35 smaller (Exact m) (Max n) = Max (m `min` n)
36 smaller (Exact m) Unknown = Max m
37 smaller (Max m) (Exact n) = Max (m `min` n)
38 smaller (Max m) (Max n) = Max (m `min` n)
39 smaller (Max m) Unknown = Max m
40 smaller Unknown (Exact n) = Max n
41 smaller Unknown (Max n) = Max n
42 smaller Unknown Unknown = Unknown
43
44 larger :: Size -> Size -> Size
45 larger (Exact m) (Exact n) = Exact (m `max` n)
46 larger (Exact m) (Max n) | m >= n = Exact m
47 | otherwise = Max n
48 larger (Max m) (Exact n) | n >= m = Exact n
49 | otherwise = Max m
50 larger (Max m) (Max n) = Max (m `max` n)
51 larger _ _ = Unknown
52
53 toMax :: Size -> Size
54 toMax (Exact n) = Max n
55 toMax (Max n) = Max n
56 toMax Unknown = Unknown
57
58 lowerBound :: Size -> Int
59 lowerBound (Exact n) = n
60 lowerBound _ = 0
61
62 upperBound :: Size -> Maybe Int
63 upperBound (Exact n) = Just n
64 upperBound (Max n) = Just n
65 upperBound Unknown = Nothing
66