static-hash-0.0.1/0000755000000000000000000000000011701527162012110 5ustar0000000000000000static-hash-0.0.1/LICENSE0000644000000000000000000000276511701527162013127 0ustar0000000000000000Copyright (c) 2009, IIJ Innovation Institute Inc. 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 copyright holders 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 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. static-hash-0.0.1/Setup.hs0000644000000000000000000000005611701527162013545 0ustar0000000000000000import Distribution.Simple main = defaultMain static-hash-0.0.1/static-hash.cabal0000644000000000000000000000144611701527162015311 0ustar0000000000000000Name: static-hash Version: 0.0.1 Author: Kazu Yamamoto Maintainer: Kazu Yamamoto License: BSD3 License-File: LICENSE Synopsis: Immutable hash Description: Pure immutable hash whose lookup is O(1) Category: Data Structures Cabal-Version: >= 1.6 Build-Type: Simple library if impl(ghc >= 6.12) GHC-Options: -Wall -fno-warn-unused-do-bind else GHC-Options: -Wall Exposed-Modules: Data.StaticHash Build-Depends: base >= 4 && < 5, primes, hashable, array, containers Source-Repository head Type: git Location: git://github.com/kazu-yamamoto/static-hash.git static-hash-0.0.1/Data/0000755000000000000000000000000011701527162012761 5ustar0000000000000000static-hash-0.0.1/Data/StaticHash.hs0000644000000000000000000000534311701527162015355 0ustar0000000000000000{-| Pure immutable hash whose lookup is O(1) on the average, but O(N) in the worst case. -} module Data.StaticHash ( StaticHash , fromList, fromList', lookup ) where import Data.Array import Data.Function import Data.Hashable import Data.List (groupBy,sortBy) import Data.Map (Map) import qualified Data.Map as M import Data.Numbers.Primes import Data.Ord import Prelude hiding (lookup) ---------------------------------------------------------------- {-| Data type for immutable hashes. -} newtype StaticHash k v = StaticHash (Array Int (Some k v)) deriving Show data Some k v = None | One k v | More (Map k v) deriving Show type Hash = Int ---------------------------------------------------------------- {-| Creating 'StaticHash' from a list. A prime around the length of the list x 2 is chosen for the size of array. This may prevent collisions. -} fromList :: (Eq k, Ord k, Hashable k) => [(k,v)] -> StaticHash k v fromList xs = fromList' (length xs) xs {-| Creating 'StaticHash' from a list and its size. -} fromList' :: (Eq k, Ord k, Hashable k) => Int -> [(k,v)] -> StaticHash k v fromList' len xs = StaticHash $ array (0,p-1) $ toIxKV xs where threshold = len * 2 -- hoping collision free p = findPrime threshold hashfunc = (`hashBy` p) toIxKV = fil 0 p . unify . hashGroup hashfunc findPrime :: Int -> Int findPrime threshold = head $ filter (>= threshold) primes hashGroup :: (Eq k, Ord k, Hashable k) => (k -> Hash) -> [(k,v)] -> [[(Hash,(k,v))]] hashGroup hashfunc xs = gs where hs = map (\kv@(k,_) -> (hashfunc k, kv)) xs gs = groupBy ((==) `on` fst) $ sortBy (comparing fst) hs unify :: Ord k => [[(Hash, (k, v))]] -> [(Hash, Some k v)] unify = map hashKeyVal hashKeyVal :: Ord k => [(Hash,(k,v))] -> (Hash,Some k v) hashKeyVal xs = (h, toSome kvs) where (h:_,kvs) = unzip xs toSome :: Ord k => [(k,v)] -> Some k v toSome [] = None toSome [(k,v)] = One k v toSome kvs = More (M.fromList kvs) fil :: Int -> Int -> [(Hash, Some k v)] -> [(Hash, Some k v)] fil i lim [] | i < lim = (i,None) : fil (i+1) lim [] | otherwise = [] fil i lim kvs@((k,v):rest) | i < k = (i,None) : fil (i+1) lim kvs | otherwise = (k,v) : fil (k+1) lim rest ---------------------------------------------------------------- {-| Looking up 'StaticHash'. -} lookup :: (Eq k, Ord k, Hashable k) => k -> StaticHash k v -> Maybe v lookup key (StaticHash hs) = case hs ! i of None -> Nothing One k v | key == k -> Just v | otherwise -> Nothing More m -> M.lookup key m where (_,p') = bounds hs p = p' + 1 i = key `hashBy` p ---------------------------------------------------------------- hashBy :: Hashable k => k -> Int -> Int hashBy k p = hash k `mod` p