Skip to content

Commit 80b39fa

Browse files
committed
Initial commit.
0 parents  commit 80b39fa

31 files changed

+1530
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

Data/.DS_Store

6 KB
Binary file not shown.

Data/.svn/all-wcprops

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
K 25
2+
svn:wc:ra_dav:version-url
3+
V 34
4+
/svn/ian/!svn/ver/77/PEParser/Data
5+
END
6+
Ranges.hs
7+
K 25
8+
svn:wc:ra_dav:version-url
9+
V 44
10+
/svn/ian/!svn/ver/77/PEParser/Data/Ranges.hs
11+
END

Data/.svn/entries

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
10
2+
3+
dir
4+
117
5+
https://monadgarden.cs.missouri.edu/svn/ian/PEParser/Data
6+
https://monadgarden.cs.missouri.edu/svn/ian
7+
8+
9+
10+
2011-03-01T19:02:49.793563Z
11+
77
12+
nabil
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
a694bad4-be2a-4ca3-a148-61e2d20494f3
28+
29+
Ranges.hs
30+
file
31+
32+
33+
34+
35+
2011-03-08T21:00:09.000000Z
36+
7d88c46c8309774bef0545df74ddcf64
37+
2011-03-01T19:02:49.793563Z
38+
77
39+
nabil
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
3753
62+
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{-# LANGUAGE FlexibleInstances #-}
2+
module Data.Ranges
3+
(range, ranges, Range, Ranges, inRange, inRanges, toSet, single, addRange, diffRanges)
4+
where
5+
6+
import Data.Set (Set)
7+
import qualified Data.Set as Set
8+
9+
-- Why do we need a Single instead of Range (a, a)?
10+
data Ord a => Range a = Single !a | Range !a !a | Empty
11+
instance (Ord a, Show a) => Show (Range a) where
12+
show (Single x) = concat ["(", show x, ")"]
13+
show (Range x y) = concat ["(", show x, ", ", show y, ")"]
14+
show Empty = "(Empty)"
15+
16+
newtype Ord a => Ranges a = Ranges [Range a] deriving Show
17+
18+
-- | A rather hacked-up instance.
19+
-- This is to support fast lookups using 'Data.Set' (see 'toSet').
20+
-- Ranges are equal if one is contained in the other?!
21+
instance (Ord a) => Eq (Range a) where
22+
(Single x) == (Single y) = x == y
23+
(Single a) == (Range x y) = x <= a && a <= y
24+
(Range x y) == (Single a) = x <= a && a <= y
25+
(Range lx ux) == (Range ly uy) = (lx <= uy && ux >= ly) || (ly <= ux && uy >= lx)
26+
27+
instance (Ord a) => Ord (Range a) where
28+
(Single x) <= (Single y) = x <= y
29+
(Single x) <= (Range y _) = x <= y
30+
(Range _ x) <= (Single y) = x <= y
31+
(Range _ x) <= (Range y _) = x <= y
32+
33+
-- | A range consisting of a single value.
34+
single :: (Ord a) => a -> Range a
35+
single x = Single x
36+
37+
-- | Construct a 'Range' from a lower and upper bound.
38+
range :: (Ord a) => a -> a -> Range a
39+
range l u
40+
| l <= u = Range l u
41+
| otherwise = Range u l
42+
43+
-- | Construct a 'Ranges' from a list of lower and upper bounds.
44+
ranges :: (Ord a) => [Range a] -> Ranges a
45+
ranges = Ranges . foldr (flip mergeRanges) []
46+
47+
-- | Tests if a given range contains a particular value.
48+
inRange :: (Ord a) => a -> Range a -> Bool
49+
inRange x y = Single x == y
50+
51+
-- | Tests if any of the ranges contains a particular value.
52+
inRanges :: (Ord a) => a -> Ranges a -> Bool
53+
inRanges x (Ranges xs) = or . map (x `inRange`) $ xs
54+
55+
mergeRange :: (Ord a) => Range a -> Range a -> Either (Range a) (Range a)
56+
mergeRange x y =
57+
if x == y
58+
then Right $ minMax x y
59+
else Left $ x
60+
61+
minMax :: (Ord a) => Range a -> Range a -> Range a
62+
minMax (Range lx ux) (Range ly uy) = Range (min lx ly) (max ux uy)
63+
minMax (Single _) y = y
64+
minMax x@(Range _ _) (Single _) = x
65+
66+
-- | Allows quick lookups using ranges.
67+
toSet :: (Ord a) => Ranges a -> Set (Range a)
68+
toSet (Ranges x) = Set.fromList x
69+
70+
addRange :: (Ord a) => Ranges a -> Range a -> Ranges a
71+
addRange (Ranges x) = Ranges . mergeRanges x
72+
73+
mergeRanges :: (Ord a) => [Range a] -> Range a -> [Range a]
74+
mergeRanges [] y = [y]
75+
mergeRanges (x:xs) y = case mergeRange x y of
76+
Right z -> mergeRanges xs z
77+
Left x -> x : (mergeRanges xs y)
78+
79+
-- Remove all elements from A that are a member of B
80+
diffRange :: (Ord a) => Range a -> Range a -> [Range a]
81+
diffRange Empty b = [Empty]
82+
diffRange a Empty = [a]
83+
diffRange (Single a) (Single b) = if (a == b)
84+
then [Empty]
85+
else [(Single a)]
86+
diffRange (Single b) (Range l u) = if (l < b) && (b < u)
87+
then [Empty]
88+
else [(Single b)]
89+
diffRange (Range l u) (Single b) = if (l < b) && (b < u)
90+
then [(Range l b), (Range b u)]
91+
else [(Range l u)]
92+
diffRange (Range al au) (Range bl bu) =
93+
if (bu <= al) || (au <= bl) then -- B is before or after A
94+
[(Range al au)]
95+
else if (bl <= al) && (bu < au) then -- B is to the left of A
96+
[(Range bu au)]
97+
else if (bl > al) && (bu < au) then -- B is inside A
98+
[(Range al bl), (Range bu au)]
99+
else if (bl < al) && (bu > au) then -- A is inside B
100+
[Empty]
101+
else if (bl > al) && (bu >= au) then -- B is to the right of A
102+
[(Range al bl)]
103+
else -- A is equal to B
104+
[Empty]
105+
106+
diffRanges :: (Ord a) => Range a -> Ranges a -> Ranges a
107+
diffRanges r (Ranges d) = Ranges (diffRanges' r d) where
108+
diffRanges' r [] = [r]
109+
diffRanges' r (s:rest) = case (diffRange r s) of
110+
[r'] -> diffRanges' r' rest
111+
[b, r'] -> b:(diffRanges' r' rest)

Data/PE/.svn/all-wcprops

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
K 25
2+
svn:wc:ra_dav:version-url
3+
V 32
4+
/svn/ian/!svn/ver/64/PEParser/PE
5+
END
6+
Parser.hs
7+
K 25
8+
svn:wc:ra_dav:version-url
9+
V 42
10+
/svn/ian/!svn/ver/20/PEParser/PE/Parser.hs
11+
END
12+
TODOS.txt
13+
K 25
14+
svn:wc:ra_dav:version-url
15+
V 42
16+
/svn/ian/!svn/ver/20/PEParser/PE/TODOS.txt
17+
END
18+
Utils.hs
19+
K 25
20+
svn:wc:ra_dav:version-url
21+
V 41
22+
/svn/ian/!svn/ver/20/PEParser/PE/Utils.hs
23+
END
24+
Structures.hs
25+
K 25
26+
svn:wc:ra_dav:version-url
27+
V 46
28+
/svn/ian/!svn/ver/64/PEParser/PE/Structures.hs
29+
END
30+
Trie.hs
31+
K 25
32+
svn:wc:ra_dav:version-url
33+
V 40
34+
/svn/ian/!svn/ver/25/PEParser/PE/Trie.hs
35+
END

0 commit comments

Comments
 (0)