summary refs log tree commit diff stats
path: root/compiler/nhashes.nim
blob: 181f46d55a369db788aeb92fdfdc8feb736bab4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#
#
#           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)