Files
Nim/compiler/nhashes.nim
2011-04-17 20:17:45 +02:00

139 lines
2.8 KiB
Nim
Executable File

#
#
# The Nimrod Compiler
# (c) Copyright 2011 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
import
strutils
const
SmallestSize* = (1 shl 3) - 1
DefaultSize* = (1 shl 11) - 1
BiggestSize* = (1 shl 28) - 1
type
THash* = int
PHash* = ref THash
THashFunc* = proc (str: cstring): THash
proc concHash*(h: THash, val: int): THash {.inline.} =
result = h +% val
result = result +% result shl 10
result = result xor (result shr 6)
proc finishHash*(h: THash): THash {.inline.} =
result = h +% h shl 3
result = result xor (result shr 11)
result = result +% result shl 15
proc GetDataHash*(Data: Pointer, Size: int): THash =
var
h: THash
p: cstring
i, s: int
h = 0
p = cast[cstring](Data)
i = 0
s = size
while s > 0:
h = h +% ord(p[i])
h = h +% h shl 10
h = h xor (h shr 6)
Inc(i)
Dec(s)
h = h +% h shl 3
h = h xor (h shr 11)
h = h +% h shl 15
result = THash(h)
proc hashPtr*(p: Pointer): THash =
result = (cast[THash](p)) shr 3 # skip the alignment
proc GetHash*(str: cstring): THash =
var
h: THash
i: int
h = 0
i = 0
while str[i] != '\0':
h = h +% ord(str[i])
h = h +% h shl 10
h = h xor (h shr 6)
Inc(i)
h = h +% h shl 3
h = h xor (h shr 11)
h = h +% h shl 15
result = THash(h)
proc GetHashStr*(s: string): THash =
var h: THash
h = 0
for i in countup(1, len(s)):
h = h +% ord(s[i])
h = h +% h shl 10
h = h xor (h shr 6)
h = h +% h shl 3
h = h xor (h shr 11)
h = h +% h shl 15
result = THash(h)
proc getNormalizedHash*(s: string): THash =
var
h: THash
c: Char
h = 0
for i in countup(0, len(s) + 0 - 1):
c = s[i]
if c == '_':
continue # skip _
if c in {'A'..'Z'}:
c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
h = h +% ord(c)
h = h +% h shl 10
h = h xor (h shr 6)
h = h +% h shl 3
h = h xor (h shr 11)
h = h +% h shl 15
result = THash(h)
proc GetHashStrCI*(s: string): THash =
var
h: THash
c: Char
h = 0
for i in countup(0, len(s) + 0 - 1):
c = s[i]
if c in {'A'..'Z'}:
c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
h = h +% ord(c)
h = h +% h shl 10
h = h xor (h shr 6)
h = h +% h shl 3
h = h xor (h shr 11)
h = h +% h shl 15
result = THash(h)
proc GetHashCI*(str: cstring): THash =
var
h: THash
c: Char
i: int
h = 0
i = 0
while str[i] != '\0':
c = str[i]
if c in {'A'..'Z'}:
c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
h = h +% ord(c)
h = h +% h shl 10
h = h xor (h shr 6)
Inc(i)
h = h +% h shl 3
h = h xor (h shr 11)
h = h +% h shl 15
result = THash(h)