Skip to content
Snippets Groups Projects
Commit 4d38d072 authored by sof's avatar sof
Browse files

[project @ 1999-01-23 17:43:21 by sof]

Changed the various Ix.range methods, to specifically check whether we're
dealing with an empty range or not. As it was, empty array weren't handled
properly.
parent fda7f6a2
No related branches found
No related tags found
No related merge requests found
...@@ -56,17 +56,21 @@ class ({-Show a,-} Ord a) => Ix a where ...@@ -56,17 +56,21 @@ class ({-Show a,-} Ord a) => Ix a where
\begin{code} \begin{code}
instance Ix Char where instance Ix Char where
range (c,c') = [c..c'] range (c,c')
| c <= c' = [c..c']
| otherwise = []
index b@(c,_) ci index b@(c,_) ci
| inRange b ci = fromEnum ci - fromEnum c | inRange b ci = fromEnum ci - fromEnum c
| otherwise = indexError ci b "Char" | otherwise = indexError ci b "Char"
inRange (m,n) i = m <= i && i <= n inRange (m,n) i = m <= i && i <= n
instance Ix Int where instance Ix Int where
range (m,n) = [m..n] range (m,n)
| m <= n = [m..n]
| otherwise = []
index b@(m,_) i index b@(m,_) i
| inRange b i = i - m | inRange b i = i - m
| otherwise = indexError i b "Int" | otherwise = indexError i b "Int"
inRange (m,n) i = m <= i && i <= n inRange (m,n) i = m <= i && i <= n
-- abstract these errors from the relevant index functions so that -- abstract these errors from the relevant index functions so that
...@@ -84,13 +88,17 @@ indexError i rng tp ...@@ -84,13 +88,17 @@ indexError i rng tp
---------------------------------------------------------------------- ----------------------------------------------------------------------
instance Ix Bool where -- as derived instance Ix Bool where -- as derived
range (l,u) = map toEnum [fromEnum l .. fromEnum u] range (l,u)
| l <= u = map toEnum [fromEnum l .. fromEnum u]
| otherwise = []
index (l,_) i = fromEnum i - fromEnum l index (l,_) i = fromEnum i - fromEnum l
inRange (l,u) i = fromEnum i >= fromEnum l && fromEnum i <= fromEnum u inRange (l,u) i = fromEnum i >= fromEnum l && fromEnum i <= fromEnum u
---------------------------------------------------------------------- ----------------------------------------------------------------------
instance Ix Ordering where -- as derived instance Ix Ordering where -- as derived
range (l,u) = map toEnum [fromEnum l .. fromEnum u] range (l,u)
| l <= u = map toEnum [fromEnum l .. fromEnum u]
| otherwise = []
index (l,_) i = fromEnum i - fromEnum l index (l,_) i = fromEnum i - fromEnum l
inRange (l,u) i = fromEnum i >= fromEnum l && fromEnum i <= fromEnum u inRange (l,u) i = fromEnum i >= fromEnum l && fromEnum i <= fromEnum u
...@@ -183,7 +191,10 @@ in the range for an @Ix@ pair: ...@@ -183,7 +191,10 @@ in the range for an @Ix@ pair:
{-# SPECIALISE rangeSize :: (Int,Int) -> Int #-} {-# SPECIALISE rangeSize :: (Int,Int) -> Int #-}
rangeSize :: (Ix a) => (a,a) -> Int rangeSize :: (Ix a) => (a,a) -> Int
rangeSize b@(l,h) rangeSize b@(l,h)
| l > h = 0 | l > h || isnull (range b) = 0
| otherwise = index b h + 1 | otherwise = index b h + 1
where
isnull [] = True
isnull _ = False
\end{code} \end{code}
...@@ -288,7 +288,10 @@ instance Show Integer where ...@@ -288,7 +288,10 @@ instance Show Integer where
instance Ix Integer where instance Ix Integer where
range (m,n) = [m..n] range (m,n)
| m <= n = [m..n]
| otherwise = []
index b@(m,_) i index b@(m,_) i
| inRange b i = fromInteger (i - m) | inRange b i = fromInteger (i - m)
| otherwise = indexIntegerError i b | otherwise = indexIntegerError i b
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment