[project @ 2005-01-19 22:33:32 by ralf]
[haskell-directory.git] / Data / IntMap.hs
index 2562c2e..46c6bee 100644 (file)
@@ -1,35 +1,39 @@
 {-# OPTIONS -cpp -fglasgow-exts #-} 
--------------------------------------------------------------------------------- 
-{-| Module      :  Data.IntMap
-    Copyright   :  (c) Daan Leijen 2002
-    License     :  BSD-style
-    Maintainer  :  libraries@haskell.org
-    Stability   :  provisional
-    Portability :  portable
-
-  An efficient implementation of maps from integer keys to values. 
-  
-  This module is intended to be imported @qualified@, to avoid name
-  clashes with Prelude functions.  eg.
-
-  >  import Data.IntMap as Map
-
-  The implementation is based on /big-endian patricia trees/. This data structure 
-  performs especially well on binary operations like 'union' and 'intersection'. However,
-  my benchmarks show that it is also (much) faster on insertions and deletions when 
-  compared to a generic size-balanced map implementation (see "Map" and "Data.FiniteMap").
-   
-  *  Chris Okasaki and Andy Gill,  \"/Fast Mergeable Integer Maps/\",
-     Workshop on ML, September 1998, pages 77--86, <http://www.cse.ogi.edu/~andy/pub/finite.htm>
-
-  *  D.R. Morrison, \"/PATRICIA -- Practical Algorithm To Retrieve Information
-     Coded In Alphanumeric/\", Journal of the ACM, 15(4), October 1968, pages 514--534.
+-----------------------------------------------------------------------------
+-- Module      :  Data.IntMap
+-- Copyright   :  (c) Daan Leijen 2002
+-- License     :  BSD-style
+-- Maintainer  :  libraries@haskell.org
+-- Stability   :  provisional
+-- Portability :  portable
+--
+-- An efficient implementation of maps from integer keys to values.
+--
+-- This module is intended to be imported @qualified@, to avoid name
+-- clashes with "Prelude" functions.  eg.
+--
+-- >  import Data.IntMap as Map
+--
+-- The implementation is based on /big-endian patricia trees/.  This data
+-- structure performs especially well on binary operations like 'union'
+-- and 'intersection'.  However, my benchmarks show that it is also
+-- (much) faster on insertions and deletions when compared to a generic
+-- size-balanced map implementation (see "Data.Map" and "Data.FiniteMap").
+--
+--    * Chris Okasaki and Andy Gill,  \"/Fast Mergeable Integer Maps/\",
+--     Workshop on ML, September 1998, pages 77-86,
+--     <http://www.cse.ogi.edu/~andy/pub/finite.htm>
+--
+--    * D.R. Morrison, \"/PATRICIA -- Practical Algorithm To Retrieve
+--     Information Coded In Alphanumeric/\", Journal of the ACM, 15(4),
+--     October 1968, pages 514-534.
+--
+-- Many operations have a worst-case complexity of /O(min(n,W))/.
+-- This means that the operation can become linear in the number of
+-- elements with a maximum of /W/ -- the number of bits in an 'Int'
+-- (32 or 64).
+-----------------------------------------------------------------------------
 
-  Many operations have a worst-case complexity of /O(min(n,W))/. This means that the
-  operation can become linear in the number of elements 
-  with a maximum of /W/ -- the number of bits in an 'Int' (32 or 64). 
--}
---------------------------------------------------------------------------------- 
 module Data.IntMap  ( 
             -- * Map type
               IntMap, Key          -- instance Eq,Show
@@ -133,6 +137,7 @@ import Data.Bits
 import Data.Int
 import Data.Monoid
 import qualified Data.IntSet as IntSet
+import Data.Typeable
 
 {-
 -- just for testing
@@ -142,20 +147,24 @@ import List (nub,sort)
 import qualified List
 -}  
 
-#ifdef __GLASGOW_HASKELL__
-{--------------------------------------------------------------------
-  GHC: use unboxing to get @shiftRL@ inlined.
---------------------------------------------------------------------}
+#if __GLASGOW_HASKELL__
+import Data.Generics.Basics
+import Data.Generics.Instances
+#endif
+
 #if __GLASGOW_HASKELL__ >= 503
 import GHC.Word
 import GHC.Exts ( Word(..), Int(..), shiftRL# )
-#else
+#elif __GLASGOW_HASKELL__
 import Word
 import GlaExts ( Word(..), Int(..), shiftRL# )
+#else
+import Data.Word
 #endif
 
 infixl 9 \\{-This comment teaches CPP correct behaviour -}
 
+-- A "Nat" is a natural machine word (an unsigned Int)
 type Nat = Word
 
 natFromInt :: Key -> Nat
@@ -165,53 +174,16 @@ intFromNat :: Nat -> Key
 intFromNat w = fromIntegral w
 
 shiftRL :: Nat -> Key -> Nat
-shiftRL (W# x) (I# i)
-  = W# (shiftRL# x i)
-
-#elif __HUGS__
+#if __GLASGOW_HASKELL__
 {--------------------------------------------------------------------
- Hugs: 
- * raises errors on boundary values when using 'fromIntegral'
-   but not with the deprecated 'fromInt/toInt'. 
- * Older Hugs doesn't define 'Word'.
- * Newer Hugs defines 'Word' in the Prelude but no operations.
+  GHC: use unboxing to get @shiftRL@ inlined.
 --------------------------------------------------------------------}
-import Data.Word
-infixl 9 \\
-
-type Nat = Word32   -- illegal on 64-bit platforms!
-
-natFromInt :: Key -> Nat
-natFromInt i = fromInt i
-
-intFromNat :: Nat -> Key
-intFromNat w = toInt w
-
-shiftRL :: Nat -> Key -> Nat
-shiftRL x i   = shiftR x i
-
+shiftRL (W# x) (I# i)
+  = W# (shiftRL# x i)
 #else
-{--------------------------------------------------------------------
-  'Standard' Haskell
-  * A "Nat" is a natural machine word (an unsigned Int)
---------------------------------------------------------------------}
-import Data.Word
-infixl 9 \\
-
-type Nat = Word
-
-natFromInt :: Key -> Nat
-natFromInt i = fromIntegral i
-
-intFromNat :: Nat -> Key
-intFromNat w = fromIntegral w
-
-shiftRL :: Nat -> Key -> Nat
-shiftRL w i   = shiftR w i
-
+shiftRL x i   = shiftR x i
 #endif
 
-
 {--------------------------------------------------------------------
   Operators
 --------------------------------------------------------------------}
@@ -238,6 +210,23 @@ type Mask   = Int
 type Key    = Int
 
 {--------------------------------------------------------------------
+  A Data instance  
+--------------------------------------------------------------------}
+
+#if __GLASGOW_HASKELL__
+
+-- This instance preserves data abstraction at the cost of inefficiency.
+-- We omit reflection services for the sake of data abstraction.
+
+instance Data a => Data (IntMap a) where
+  gfoldl f z im = z fromList `f` (toList im)
+  toConstr _    = error "toConstr"
+  gunfold _ _   = error "gunfold"
+  dataTypeOf _  = mkNorepType "Data.IntMap.IntMap"
+
+#endif
+
+{--------------------------------------------------------------------
   Query
 --------------------------------------------------------------------}
 -- | /O(1)/. Is the map empty?
@@ -1001,6 +990,13 @@ showMap (x:xs)
     showElem (k,x)  = shows k . showString ":=" . shows x
   
 {--------------------------------------------------------------------
+  Typeable
+--------------------------------------------------------------------}
+
+#include "Typeable.h"
+INSTANCE_TYPEABLE1(IntMap,intMapTc,"IntMap")
+
+{--------------------------------------------------------------------
   Debugging
 --------------------------------------------------------------------}
 -- | /O(n)/. Show the tree that implements the map. The tree is shown