File:  [Research Unix] / researchv10no / cmd / sml / src / util / strghash.sml
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 17:21:35 2018 UTC (8 years, 1 month ago) by root
Branches: belllabs, MAIN
CVS tags: researchv10, HEAD
researchv10 Norman

(* Copyright 1989 by AT&T Bell Laboratories *)
(* util/strghash.sml *)

structure StrgHash =
struct

  val prime = 8388593 (* largest prime less than 2^23 *)
  val base = 128

(* the simple version --
    fun hashString(str: string) : int =
        let fun loop (0,n) = n
	      | loop (i,n) = 
	          let val i = i-1
		      val n' = (base * n + ordof(str,i)) 
		   in loop (i, (n' - prime * (n' div prime)))
		  end
	 in loop (size str,0)
	end
*)

  fun hashString(str: string) : int =
      let val l = size str
       in case l
	    of 0 => 0
	     | 1 => ord str
	     | 2 => ordof(str,0) + base * ordof(str,1)
	     | 3 => ordof(str,0) + base * (ordof(str,1) + base * ordof(str,2))
	     | _ =>
		let fun loop (0,n) = n
		      | loop (i,n) = 
			  let val i = i-1
			      val n' = (base * n + ordof(str,i)) 
			   in loop (i, (n' - prime * (n' div prime)))
			  end
		 in loop (l,0)
		end
      end

end (* structure StrgHash *)




unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.