(* -*- mode: sml -*-
 * $Id: hash.smlnj,v 1.3 2001/07/09 00:25:28 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *)

structure Test : sig
    val main : (string * string list) -> OS.Process.status
end = struct

open HashTable;

fun hashtest n =
    let
    val hx = mkTable (HashString.hashString, op =) (n, (Fail "not found"))
    fun doinserts i n =
        if i < n then (
        insert hx ((Int.fmt StringCvt.HEX i), i);
        doinserts (i+1) n
        ) else ()
    fun dolookups i c =
        if i > 0 then
        case find hx (Int.toString i) of
            SOME key => dolookups (i-1) (c+1)
          | _ => dolookups (i-1) c
        else c
    in (
    doinserts 0 n;
    dolookups n 0
    ) end;

fun atoi s = case Int.fromString s of SOME num => num | NONE => 0;

fun main(name, args) = 
    let
    val arg = hd(args @ ["1"])
    val num = atoi arg
    val result = hashtest num
    in
    print (Int.toString result) ; print "\n" ;
    OS.Process.success
    end
end

val _ = SMLofNJ.exportFn("hash", Test.main);