Skip to content

Commit

Permalink
Judy 0.1
Browse files Browse the repository at this point in the history
darcs-hash:20090926040830-cba2c-d66df56b55d85e0bb2acaa062eb372530315dcac
  • Loading branch information
donsbot committed Sep 26, 2009
0 parents commit 65dd7fd
Show file tree
Hide file tree
Showing 11 changed files with 650 additions and 0 deletions.
436 changes: 436 additions & 0 deletions Data/Judy.hsc

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions LICENSE
@@ -0,0 +1,30 @@
Copyright (c) 2009 Don Stewart

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. 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.

3. Neither the name of the author nor the names of his contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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.
1 change: 1 addition & 0 deletions NOTES
@@ -0,0 +1 @@
Need C shims to the macros.
3 changes: 3 additions & 0 deletions Setup.lhs
@@ -0,0 +1,3 @@
#!/usr/bin/env runhaskell
> import Distribution.Simple
> main = defaultMain
6 changes: 6 additions & 0 deletions cbits/haskell-judy.c
@@ -0,0 +1,6 @@
#include <Judy.h>

/* void *hs_judy_pointer_error(void) { return PJERR; } */

void hs_judyl_free(void *ptr) { JudyLFreeArray(ptr, PJE0); }

1 change: 1 addition & 0 deletions includes/haskell-judy.h
@@ -0,0 +1 @@
void hs_judyl_free(void *ptr);
34 changes: 34 additions & 0 deletions judy.cabal
@@ -0,0 +1,34 @@
name: judy
version: 0.1
homepage: http://code.haskell.org/~dons/code/judy
synopsis: Fast, scalable, mutable dynamic arrays, maps and hashes
description: Fast, scalable. mutable dynamic arrays, maps and hashes
category: Data
license: BSD3
license-file: LICENSE
copyright: (c) 2008, Don Stewart <dons@galois.com>
author: Don Stewart
maintainer: Don Stewart <dons@galois.com>
cabal-version: >= 1.2.0
build-type: Simple
tested-with: GHC ==6.8.2

library
exposed-modules: Data.Judy

extensions: CPP,
GeneralizedNewtypeDeriving,
EmptyDataDecls,
TypeSynonymInstances

ghc-options: -Wall -fvia-C -O2
cc-options: -O3
build-depends: base, ghc-prim

extra-libraries: Judy

c-sources: cbits/haskell-judy.c
include-dirs: includes
includes: haskell-judy.h
install-includes: haskell-judy.h

34 changes: 34 additions & 0 deletions tests/examples/A.hs
@@ -0,0 +1,34 @@
import Control.Monad
import qualified Data.Judy as J
import qualified Data.IntMap as I
import Data.List

import Control.Monad
import qualified Data.Judy as J

main = do
j <- J.new :: IO (J.JudyL Int)
forM_ [1..10000000] $ \n -> J.insert n (fromIntegral n :: Int) j
v <- J.lookup 100 j
print v


{-
main = do
j <- J.new :: IO (J.JudyL Bool)
forM_ [1..10000000] $ \n -> J.insert n True j
-- forM_ [1..10000000] $ \n -> J.delete n j
J.insert 100 False j
v <- J.lookup 100 j
print v
-}

{-
-- 7.164s total
main = do
let h = foldl' (\b a -> I.insert a (fromIntegral a) b) I.empty [1..10000000] :: I.IntMap Int
print (I.size h)
print (I.lookup 100 h)
-}
9 changes: 9 additions & 0 deletions tests/examples/B.hs
@@ -0,0 +1,9 @@
import Control.Monad
import qualified Data.Judy as J

main = do
j <- J.new
forM_ [1..10000000] $ \n -> J.insert n n j
J.delete 100 j
v <- J.lookup 100 j
print v
8 changes: 8 additions & 0 deletions tests/examples/Old.hs
@@ -0,0 +1,8 @@
import Control.Monad
import qualified Data.HashTable as H

main = do
m <- H.new (==) H.hashInt
forM_ [1..10000000] $ \n -> H.insert m n n
v <- H.lookup m 100
print v
88 changes: 88 additions & 0 deletions tests/examples/judyhash.c
@@ -0,0 +1,88 @@
// Sample program to show how to use Judy as a collision
// handler within a Hash table.
//
// cc -DHASHSIZE=256 hash.c ..

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#define JUDYERROR_SAMPLE 1 // use default Judy error handler
#include <Judy.h>

// Needed for timing routines
#include <sys/time.h>

// Start of timing routines ========================================

struct timeval TBeg, TEnd;

#define STARTTm gettimeofday(&TBeg, NULL)
#define ENDTm gettimeofday(&TEnd, NULL)
#define DeltaUSec \
( ((double)TEnd.tv_sec * 1000000.0 + (double)TEnd.tv_usec) \
- ((double)TBeg.tv_sec * 1000000.0 + (double)TBeg.tv_usec) )

// End of timing routines ========================================
// Define Hash table size if not in compile line ===================
// Set HASHSIZE 1 for straight Judy

#ifndef HASHSIZE
#define HASHSIZE (1 << 8) // hash table size 256
#endif

// Seed for pseudo-random counter ==================================

#define INITN 123456 // first Index to store

static uint32_t // Placed here for INLINE possibility
Random(uint32_t Seed) // produce 2^32 -1 numbers by different counting
{
if ((int32_t)Seed < 0) { Seed += Seed; Seed ^= 16611; }
else { Seed += Seed; }
return(Seed);
}

// Hash Table ======================================================

Pvoid_t JArray[HASHSIZE] = { NULL }; // Declare static hash table

int main(int argc, char *argv[])
{
Word_t Count;
Word_t Index;
Word_t *PValue;
Word_t NumIndexes = 10000; // default first parameter

if (argc > 1) NumIndexes = strtoul(argv[1], NULL, 0);
// Load up the CPU cache for small measurements:

for (Count = 0; Count < HASHSIZE; Count++) JArray[Count] = NULL;
printf("Begin storing %lu random numbers in a Judy scalable hash array\n",
NumIndexes);
Index = INITN;
STARTTm;
for (Count = 0; Count < NumIndexes; Count++)
{
Index = Random(Index);
JLI(PValue, JArray[Index % HASHSIZE], Index/HASHSIZE);
*PValue += 1; // bump count of duplicate Indexes
}
ENDTm;

printf("Insertion of %lu indexes took %6.3f microseconds per index\n",
NumIndexes, DeltaUSec/NumIndexes);
Index = INITN; // start the same number sequence over
STARTTm;

for (Count = 0; Count < NumIndexes; Count++)
{
Index = Random(Index);
JLG(PValue, JArray[Index % HASHSIZE], Index/HASHSIZE);
if (*PValue != 1)
printf("%lu dups of %lu\n", *PValue - 1, Index);
}
ENDTm;
printf("Retrieval of %lu indexes took %6.3f microseconds per index\n",
NumIndexes, DeltaUSec/NumIndexes);
return(0);
}

0 comments on commit 65dd7fd

Please sign in to comment.