-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString-*stringhashtests.st
144 lines (120 loc) · 3.12 KB
/
String-*stringhashtests.st
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
139
140
141
142
143
144
'From Cuis 5.0 [latest update: #4763] on 19 August 2021 at 2:24:37 pm'!
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/17/2021 16:10:33'!
hash0
"DJ1B1"
| result |
result := 0.
self do: [ :char |
result := result * 33.
result := result + char asciiValue
].
^ result bitAnd: 16rFFFFFFF! !
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/19/2021 13:54:13'!
hash1
"K&R"
| result |
result := 0.
self do: [ :char |
result := result * 31.
result := result + char asciiValue
].
^ result bitAnd: 16rFFFFFFF! !
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/19/2021 13:59:46'!
hash10
"FNVHash"
| result |
result := 2166136261. "offset32"
self do: [ :char |
result := result * 16777619. "prime32"
result := result bitAnd: char asciiValue
].
^ result bitAnd: 16rFFFFFFF! !
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/17/2021 16:49:14'!
hash2
"STLPort"
| result |
result := 0.
self do: [ :char |
result := (result * 5) + char asciiValue
].
^ result bitAnd: 16rFFFFFFF! !
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/19/2021 13:50:58'!
hash3
"RSHashMod"
| result |
result := 0.
self do: [ :char |
result := (result * 127) + char asciiValue
].
^ result bitAnd: 16rFFFFFFF! !
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/19/2021 13:48:55'!
hash4
"UHashMod"
| result rand1 rand2 |
result := 0.
rand1 := 31415.
rand2 := 27183.
self do: [ :char |
result := result * rand1.
result := (result + char asciiValue).
rand1 := rand1 * rand2.
].
^ result bitAnd: 16rFFFFFFF! !
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/19/2021 13:43:54'!
hash5
"PJWHash1"
| result highBits|
result := 0.
self do: [ :char |
result := result bitShift: 4.
result := (result + char asciiValue).
highBits := result bitAnd: 16rF0000000.
(highBits = 0) ifFalse: [
result := result bitAnd: (highBits bitShift: -24)
].
].
^ result bitAnd: 16rFFFFFFF! !
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/19/2021 13:48:13'!
hash6
"RSHash"
| result |
result := 0.
self do: [ :char |
result := result * 127.
result := (result + char asciiValue) bitAnd: 16rFFFFFFF].
^ result bitAnd: 16rFFFFFFF! !
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/19/2021 13:49:59'!
hash7
"UHash"
| result rand1 rand2 |
result := 0.
rand1 := 31415.
rand2 := 27183.
self do: [ :char |
result := result * rand1.
result := (result + char asciiValue) bitAnd: 16rFFFFFFF.
rand1 := (rand1 * rand2) bitAnd: 16rFFFFFFF.
].
^ result bitAnd: 16rFFFFFFF! !
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/19/2021 13:53:35'!
hash8
"DJ1B2"
| result |
result := 5381.
self do: [ :char |
result := result bitShift: 5.
result := result + result. "?Isn't this just one more left shift?"
result := result + char asciiValue
].
^ result bitAnd: 16rFFFFFFF! !
!String methodsFor: '*stringhashtests' stamp: 'KenD 8/19/2021 13:56:47'!
hash9
"Knuth"
| result |
result := self size.
self do: [ :char |
result := result bitShift: 5.
result := result bitAnd: (result bitShift: -27).
result := result bitAnd: char asciiValue
].
^ result bitAnd: 16rFFFFFFF! !