Range (empty) → 0.1.0.0
raw patch · 4 files changed
+122/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- Data/Range.hs +70/−0
- LICENSE +30/−0
- Range.cabal +20/−0
- Setup.hs +2/−0
+ Data/Range.hs view
@@ -0,0 +1,70 @@+module Data.Range ( + Range + , empty + , singleton + , range + , bounds + , contains + , extendTo + , intersect + , union + ) where + + +data Range a = Empty | Bounds a a + deriving (Eq, Ord) + + +empty :: Range a +empty = Empty + + +singleton :: a -> Range a +singleton x = Bounds x x + + +range :: (Ord a) => a -> a -> Range a +range l u = if l <= u + then Bounds l u + else Empty + + +bounds :: Range a -> Maybe (a, a) +bounds Empty = Nothing +bounds (Bounds l u) = Just (l, u) + + +contains :: (Ord a) => Range a -> a -> Bool +contains Empty _ = False +contains (Bounds l u) x = l <= x && x <= u + + +extendTo :: (Ord a) => Range a -> a -> Range a +extendTo Empty x = singleton x +extendTo (Bounds l u) x + | x < l = Bounds x u + | x > u = Bounds l x + | otherwise = Bounds l u + + +intersect :: (Ord a) => Range a -> Range a -> Range a +intersect Empty _ = Empty +intersect _ Empty = Empty +intersect (Bounds l1 u1) (Bounds l2 u2) = range l u + where + l = max l1 l2 + u = min u1 u2 + + +union :: (Ord a) => Range a -> Range a -> Maybe (Range a) +union Empty range = Just range +union range Empty = Just range +union b1@(Bounds l1 u1) b2@(Bounds l2 u2) = case intersect b1 b2 of + Empty -> Nothing + Bounds{} -> Just $ Bounds l u + where + l = min l1 l2 + u = max u1 u2 + + +
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, Thomas Eding + +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 Thomas Eding nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT +OWNER 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.
+ Range.cabal view
@@ -0,0 +1,20 @@+-- Initial Range.cabal generated by cabal init. For further documentation, +-- see http://haskell.org/cabal/users-guide/ + +name: Range +version: 0.1.0.0 +synopsis: Data structure for managing ranges +-- description: +license: BSD3 +license-file: LICENSE +author: Thomas Eding +maintainer: thomasedingcode@gmail.com +-- copyright: +category: Data +build-type: Simple +cabal-version: >=1.8 + +library + exposed-modules: Data.Range + -- other-modules: + build-depends: base ==4.5.*
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple +main = defaultMain