ranges-0.2.4/ 40777 0 0 0 11627602444 6021 5ranges-0.2.4/Data/ 40777 0 0 0 11627602444 6672 5ranges-0.2.4/Data/Ranges.hs100777 0 0 4647 11167031027 10550 0{-# LANGUAGE FlexibleInstances #-} module Data.Ranges (range, ranges, Range, Ranges, inRange, inRanges, toSet, single, addRange) where import Data.Set (Set) import qualified Data.Set as Set data Ord a => Range a = Single !a | Range !a !a instance (Ord a, Show a) => Show (Range a) where show (Single x) = concat ["(", show x, ")"] show (Range x y) = concat ["(", show x, "–", show y, ")"] newtype Ord a => Ranges a = Ranges [Range a] deriving Show -- | A rather hacked-up instance. -- This is to support fast lookups using 'Data.Set' (see 'toSet'). instance (Ord a) => Eq (Range a) where (Single x) == (Single y) = x == y (Single a) == (Range x y) = x <= a && a <= y (Range x y) == (Single a) = x <= a && a <= y (Range lx ux) == (Range ly uy) = (lx <= uy && ux >= ly) || (ly <= ux && uy >= lx) instance (Ord a) => Ord (Range a) where (Single x) <= (Single y) = x <= y (Single x) <= (Range y _) = x <= y (Range _ x) <= (Single y) = x <= y (Range _ x) <= (Range y _) = x <= y -- | A range consisting of a single value. single :: (Ord a) => a -> Range a single x = Single x -- | Construct a 'Range' from a lower and upper bound. range :: (Ord a) => a -> a -> Range a range l u | l <= u = Range l u | otherwise = error "lower bound must be smaller than upper bound" -- | Construct a 'Ranges' from a list of lower and upper bounds. ranges :: (Ord a) => [Range a] -> Ranges a ranges = Ranges . foldr (flip mergeRanges) [] -- | Tests if a given range contains a particular value. inRange :: (Ord a) => a -> Range a -> Bool inRange x y = Single x == y -- | Tests if any of the ranges contains a particular value. inRanges :: (Ord a) => a -> Ranges a -> Bool inRanges x (Ranges xs) = or . map (x `inRange`) $ xs mergeRange :: (Ord a) => Range a -> Range a -> Either (Range a) (Range a) mergeRange x y = if x == y then Right $ minMax x y else Left $ x minMax :: (Ord a) => Range a -> Range a -> Range a minMax (Range lx ux) (Range ly uy) = Range (min lx ly) (max ux uy) minMax (Single _) y = y minMax x@(Range _ _) (Single _) = x -- | Allows quick lookups using ranges. toSet :: (Ord a) => Ranges a -> Set (Range a) toSet (Ranges x) = Set.fromList x addRange :: (Ord a) => Ranges a -> Range a -> Ranges a addRange (Ranges x) = Ranges . mergeRanges x mergeRanges :: (Ord a) => [Range a] -> Range a -> [Range a] mergeRanges [] y = [y] mergeRanges (x:xs) y = case mergeRange x y of Right z -> mergeRanges xs z Left x -> x : (mergeRanges xs y) ranges-0.2.4/LICENSE100777 0 0 2707 11514655663 7142 0Copyright (c) 2010, George Pollard All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the package nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.ranges-0.2.4/ranges.cabal100777 0 0 766 11627601570 10354 0Name: ranges Version: 0.2.4 Description: Ranges and some functions allowing things like fast membership lookup on ranges with holes in them and so on. Synopsis: Ranges and various functions on them. License: BSD3 Author: George Pollard Maintainer: George Pollard Build-Type: Simple Cabal-Version: >=1.2 License-file: LICENSE Category: data Library Build-Depends: base>=2 && <5, containers>=0.2 Exposed-modules: Data.Ranges ghc-options: -O2 -Wall ranges-0.2.4/Setup.hs100777 0 0 56 11167031027 7506 0import Distribution.Simple main = defaultMain