hashfunctions-extension 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +45 -0
- data/LICENSE.txt +234 -0
- data/README.md +28 -0
- data/Rakefile +25 -0
- data/ext/generalhashfunctions_ext/AP_GeneralHashFunctions.c +186 -0
- data/ext/generalhashfunctions_ext/AP_GeneralHashFunctions.h +44 -0
- data/ext/generalhashfunctions_ext/extconf.rb +11 -0
- data/ext/generalhashfunctions_ext/generalhashfunctions_ext.c +93 -0
- data/lib/general-hash-functions/extension.rb +4 -0
- metadata +78 -0
data/.gitignore
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
|
14
|
+
# jeweler generated
|
15
|
+
pkg
|
16
|
+
|
17
|
+
# aptana
|
18
|
+
.project
|
19
|
+
|
20
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
21
|
+
#
|
22
|
+
# * Create a file at ~/.gitignore
|
23
|
+
# * Include files you want ignored
|
24
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
25
|
+
#
|
26
|
+
# After doing this, these files will be ignored in all your git projects,
|
27
|
+
# saving you from having to 'pollute' every project you touch with them
|
28
|
+
#
|
29
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
30
|
+
#
|
31
|
+
# For MacOS:
|
32
|
+
#
|
33
|
+
#.DS_Store
|
34
|
+
#
|
35
|
+
# For TextMate
|
36
|
+
#*.tmproj
|
37
|
+
#tmtags
|
38
|
+
#
|
39
|
+
# For emacs:
|
40
|
+
#*~
|
41
|
+
#\#*
|
42
|
+
#.\#*
|
43
|
+
#
|
44
|
+
# For vim:
|
45
|
+
#*.swp
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,234 @@
|
|
1
|
+
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS COMMON
|
2
|
+
PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION
|
3
|
+
OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
|
4
|
+
|
5
|
+
1. DEFINITIONS
|
6
|
+
|
7
|
+
"Contribution" means:
|
8
|
+
|
9
|
+
a) in the case of the initial Contributor, the initial code and
|
10
|
+
documentation distributed under this Agreement, and
|
11
|
+
|
12
|
+
b) in the case of each subsequent Contributor:
|
13
|
+
|
14
|
+
i) changes to the Program, and
|
15
|
+
|
16
|
+
ii) additions to the Program;
|
17
|
+
|
18
|
+
where such changes and/or additions to the Program originate from
|
19
|
+
and are distributed by that particular Contributor. A Contribution
|
20
|
+
'originates' from a Contributor if it was added to the Program by
|
21
|
+
such Contributor itself or anyone acting on such Contributor's
|
22
|
+
behalf. Contributions do not include additions to the Program which:
|
23
|
+
(i) are separate modules of software distributed in conjunction with
|
24
|
+
the Program under their own license agreement, and (ii) are not
|
25
|
+
derivative works of the Program.
|
26
|
+
|
27
|
+
"Contributor" means any person or entity that distributes the Program.
|
28
|
+
|
29
|
+
"Licensed Patents " mean patent claims licensable by a Contributor
|
30
|
+
which are necessarily infringed by the use or sale of its
|
31
|
+
Contribution alone or when combined with the Program.
|
32
|
+
|
33
|
+
"Program" means the Contributions distributed in accordance with
|
34
|
+
this Agreement.
|
35
|
+
|
36
|
+
"Recipient" means anyone who receives the Program under this
|
37
|
+
Agreement, including all Contributors.
|
38
|
+
|
39
|
+
2. GRANT OF RIGHTS
|
40
|
+
|
41
|
+
a) Subject to the terms of this Agreement, each Contributor hereby
|
42
|
+
grants Recipient a non-exclusive, worldwide, royalty-free copyright
|
43
|
+
license to reproduce, prepare derivative works of, publicly display,
|
44
|
+
publicly perform, distribute and sublicense the Contribution of such
|
45
|
+
Contributor, if any, and such derivative works, in source code and
|
46
|
+
object code form.
|
47
|
+
|
48
|
+
b) Subject to the terms of this Agreement, each Contributor hereby
|
49
|
+
grants Recipient a non-exclusive, worldwide, royalty-free patent
|
50
|
+
license under Licensed Patents to make, use, sell, offer to sell,
|
51
|
+
import and otherwise transfer the Contribution of such Contributor,
|
52
|
+
if any, in source code and object code form. This patent license
|
53
|
+
shall apply to the combination of the Contribution and the Program
|
54
|
+
if, at the time the Contribution is added by the Contributor, such
|
55
|
+
addition of the Contribution causes such combination to be covered
|
56
|
+
by the Licensed Patents. The patent license shall not apply to any
|
57
|
+
other combinations which include the Contribution. No hardware per
|
58
|
+
se is licensed hereunder.
|
59
|
+
|
60
|
+
c) Recipient understands that although each Contributor grants the
|
61
|
+
licenses to its Contributions set forth herein, no assurances are
|
62
|
+
provided by any Contributor that the Program does not infringe the
|
63
|
+
patent or other intellectual property rights of any other entity.
|
64
|
+
Each Contributor disclaims any liability to Recipient for claims
|
65
|
+
brought by any other entity based on infringement of intellectual
|
66
|
+
property rights or otherwise. As a condition to exercising the
|
67
|
+
rights and licenses granted hereunder, each Recipient hereby assumes
|
68
|
+
sole responsibility to secure any other intellectual property rights
|
69
|
+
needed, if any. For example, if a third party patent license is
|
70
|
+
required to allow Recipient to distribute the Program, it is
|
71
|
+
Recipient's responsibility to acquire that license before
|
72
|
+
distributing the Program.
|
73
|
+
|
74
|
+
d) Each Contributor represents that to its knowledge it has
|
75
|
+
sufficient copyright rights in its Contribution, if any, to grant
|
76
|
+
the copyright license set forth in this Agreement.
|
77
|
+
|
78
|
+
3. REQUIREMENTS
|
79
|
+
|
80
|
+
A Contributor may choose to distribute the Program in object code
|
81
|
+
form under its own license agreement, provided that:
|
82
|
+
|
83
|
+
a) it complies with the terms and conditions of this Agreement; and
|
84
|
+
|
85
|
+
b) its license agreement:
|
86
|
+
|
87
|
+
i) effectively disclaims on behalf of all Contributors all
|
88
|
+
warranties and conditions, express and implied, including warranties
|
89
|
+
or conditions of title and non-infringement, and implied warranties
|
90
|
+
or conditions of merchantability and fitness for a particular purpose;
|
91
|
+
|
92
|
+
ii) effectively excludes on behalf of all Contributors all liability
|
93
|
+
for damages, including direct, indirect, special, incidental and
|
94
|
+
consequential damages, such as lost profits;
|
95
|
+
|
96
|
+
iii) states that any provisions which differ from this Agreement are
|
97
|
+
offered by that Contributor alone and not by any other party; and
|
98
|
+
|
99
|
+
iv) states that source code for the Program is available from such
|
100
|
+
Contributor, and informs licensees how to obtain it in a reasonable
|
101
|
+
manner on or through a medium customarily used for software exchange.
|
102
|
+
|
103
|
+
When the Program is made available in source code form:
|
104
|
+
|
105
|
+
a) it must be made available under this Agreement; and
|
106
|
+
|
107
|
+
b) a copy of this Agreement must be included with each copy of the
|
108
|
+
Program.
|
109
|
+
|
110
|
+
Contributors may not remove or alter any copyright notices contained
|
111
|
+
within the Program.
|
112
|
+
|
113
|
+
Each Contributor must identify itself as the originator of its
|
114
|
+
Contribution, if any, in a manner that reasonably allows subsequent
|
115
|
+
Recipients to identify the originator of the Contribution.
|
116
|
+
|
117
|
+
4. COMMERCIAL DISTRIBUTION
|
118
|
+
|
119
|
+
Commercial distributors of software may accept certain
|
120
|
+
responsibilities with respect to end users, business partners and
|
121
|
+
the like. While this license is intended to facilitate the
|
122
|
+
commercial use of the Program, the Contributor who includes the
|
123
|
+
Program in a commercial product offering should do so in a manner
|
124
|
+
which does not create potential liability for other Contributors.
|
125
|
+
Therefore, if a Contributor includes the Program in a commercial
|
126
|
+
product offering, such Contributor ("Commercial Contributor") hereby
|
127
|
+
agrees to defend and indemnify every other Contributor ("Indemnified
|
128
|
+
Contributor") against any losses, damages and costs (collectively
|
129
|
+
"Losses") arising from claims, lawsuits and other legal actions
|
130
|
+
brought by a third party against the Indemnified Contributor to the
|
131
|
+
extent caused by the acts or omissions of such Commercial
|
132
|
+
Contributor in connection with its distribution of the Program in a
|
133
|
+
commercial product offering. The obligations in this section do not
|
134
|
+
apply to any claims or Losses relating to any actual or alleged
|
135
|
+
intellectual property infringement. In order to qualify, an
|
136
|
+
Indemnified Contributor must: a) promptly notify the Commercial
|
137
|
+
Contributor in writing of such claim, and b) allow the Commercial
|
138
|
+
Contributor to control, and cooperate with the Commercial
|
139
|
+
Contributor in, the defense and any related settlement negotiations.
|
140
|
+
The Indemnified Contributor may participate in any such claim at its
|
141
|
+
own expense.
|
142
|
+
|
143
|
+
For example, a Contributor might include the Program in a commercial
|
144
|
+
product offering, Product X. That Contributor is then a Commercial
|
145
|
+
Contributor. If that Commercial Contributor then makes performance
|
146
|
+
claims, or offers warranties related to Product X, those performance
|
147
|
+
claims and warranties are such Commercial Contributor's
|
148
|
+
responsibility alone. Under this section, the Commercial Contributor
|
149
|
+
would have to defend claims against the other Contributors related
|
150
|
+
to those performance claims and warranties, and if a court requires
|
151
|
+
any other Contributor to pay any damages as a result, the Commercial
|
152
|
+
Contributor must pay those damages.
|
153
|
+
|
154
|
+
5. NO WARRANTY
|
155
|
+
|
156
|
+
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS
|
157
|
+
PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
158
|
+
ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION,
|
159
|
+
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
|
160
|
+
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient
|
161
|
+
is solely responsible for determining the appropriateness of using
|
162
|
+
and distributing the Program and assumes all risks associated with
|
163
|
+
its exercise of rights under this Agreement, including but not
|
164
|
+
limited to the risks and costs of program errors, compliance with
|
165
|
+
applicable laws, damage to or loss of data, programs or equipment,
|
166
|
+
and unavailability or interruption of operations.
|
167
|
+
|
168
|
+
6. DISCLAIMER OF LIABILITY
|
169
|
+
|
170
|
+
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT
|
171
|
+
NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT,
|
172
|
+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
173
|
+
(INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON
|
174
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
175
|
+
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
176
|
+
THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS
|
177
|
+
GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
178
|
+
|
179
|
+
7. GENERAL
|
180
|
+
|
181
|
+
If any provision of this Agreement is invalid or unenforceable under
|
182
|
+
applicable law, it shall not affect the validity or enforceability
|
183
|
+
of the remainder of the terms of this Agreement, and without further
|
184
|
+
action by the parties hereto, such provision shall be reformed to
|
185
|
+
the minimum extent necessary to make such provision valid and
|
186
|
+
enforceable.
|
187
|
+
|
188
|
+
If Recipient institutes patent litigation against a Contributor with
|
189
|
+
respect to a patent applicable to software (including a cross-claim
|
190
|
+
or counterclaim in a lawsuit), then any patent licenses granted by
|
191
|
+
that Contributor to such Recipient under this Agreement shall
|
192
|
+
terminate as of the date such litigation is filed. In addition, if
|
193
|
+
Recipient institutes patent litigation against any entity (including
|
194
|
+
a cross-claim or counterclaim in a lawsuit) alleging that the
|
195
|
+
Program itself (excluding combinations of the Program with other
|
196
|
+
software or hardware) infringes such Recipient's patent(s), then
|
197
|
+
such Recipient's rights granted under Section 2(b) shall terminate
|
198
|
+
as of the date such litigation is filed.
|
199
|
+
|
200
|
+
All Recipient's rights under this Agreement shall terminate if it
|
201
|
+
fails to comply with any of the material terms or conditions of this
|
202
|
+
Agreement and does not cure such failure in a reasonable period of
|
203
|
+
time after becoming aware of such noncompliance. If all Recipient's
|
204
|
+
rights under this Agreement terminate, Recipient agrees to cease use
|
205
|
+
and distribution of the Program as soon as reasonably practicable.
|
206
|
+
However, Recipient's obligations under this Agreement and any
|
207
|
+
licenses granted by Recipient relating to the Program shall continue
|
208
|
+
and survive.
|
209
|
+
|
210
|
+
Everyone is permitted to copy and distribute copies of this
|
211
|
+
Agreement, but in order to avoid inconsistency the Agreement is
|
212
|
+
copyrighted and may only be modified in the following manner. The
|
213
|
+
Agreement Steward reserves the right to publish new versions
|
214
|
+
(including revisions) of this Agreement from time to time. No one
|
215
|
+
other than the Agreement Steward has the right to modify this
|
216
|
+
Agreement. IBM is the initial Agreement Steward. IBM may assign the
|
217
|
+
responsibility to serve as the Agreement Steward to a suitable
|
218
|
+
separate entity. Each new version of the Agreement will be given a
|
219
|
+
distinguishing version number. The Program (including Contributions)
|
220
|
+
may always be distributed subject to the version of the Agreement
|
221
|
+
under which it was received. In addition, after a new version of the
|
222
|
+
Agreement is published, Contributor may elect to distribute the
|
223
|
+
Program (including its Contributions) under the new version. Except
|
224
|
+
as expressly stated in Sections 2(a) and 2(b) above, Recipient
|
225
|
+
receives no rights or licenses to the intellectual property of any
|
226
|
+
Contributor under this Agreement, whether expressly, by implication,
|
227
|
+
estoppel or otherwise. All rights in the Program not expressly
|
228
|
+
granted under this Agreement are reserved.
|
229
|
+
|
230
|
+
This Agreement is governed by the laws of the State of New York and
|
231
|
+
the intellectual property laws of the United States of America. No
|
232
|
+
party to this Agreement will bring a legal action under this
|
233
|
+
Agreement more than one year after the cause of action arose. Each
|
234
|
+
party waives its rights to a jury trial in any resulting litigation.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
General Purpose Hash Functions Extension
|
2
|
+
========================================
|
3
|
+
|
4
|
+
**hashfunctions-extension** is a [Ruby][1] C extension which
|
5
|
+
implements 11 fast hash functions for general purpose
|
6
|
+
non-cryptographic use implemented originally by [Arash Partow][2].
|
7
|
+
General pure API is available by the [**hashfunctions**][3] gem.
|
8
|
+
|
9
|
+
Some example:
|
10
|
+
|
11
|
+
require "general-hash-functions/extension"
|
12
|
+
hash = GeneralHashFunctionsExt::djbhash("some string")
|
13
|
+
# will return the 395673328
|
14
|
+
|
15
|
+
See description of all contained functions at Arash Partow's page:
|
16
|
+
http://www.partow.net/programming/hashfunctions/index.html#AvailableHashFunctions
|
17
|
+
|
18
|
+
|
19
|
+
Copyright
|
20
|
+
---------
|
21
|
+
|
22
|
+
Copyright © 2002 [Arash Partow][2]. Copyright © 2012
|
23
|
+
[Martin Kozák][4]. See `LICENSE.txt` for further details.
|
24
|
+
|
25
|
+
[1]: http://www.ruby-lang.org/en/
|
26
|
+
[2]: http://www.partow.net/
|
27
|
+
[3]: http://github.com/martinkozak/hashfunctions
|
28
|
+
[4]: http://www.martinkozak.net/
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/extensiontask'
|
5
|
+
|
6
|
+
spec = Gem::Specification.new do |s|
|
7
|
+
s.name = "hashfunctions-extension"
|
8
|
+
s.email = "martinkozak@martinkozak.net"
|
9
|
+
s.author = "Martin Kozák"
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.summary = "11 fast hash functions for general purpose non-cryptographic use as C extension."
|
12
|
+
s.description = "11 fast hash functions for general purpose non-cryptographic use implemented originally by Arash Partow. The Ruby C extension."
|
13
|
+
s.homepage = "http://github.com/martinkozak/hashfunctions-extension"
|
14
|
+
s.license = "CPL"
|
15
|
+
s.version = "1.0.0"
|
16
|
+
s.extensions = FileList["ext/generalhashfunctions_ext/extconf.rb"]
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.add_development_dependency("rake-compiler", ">= 0")
|
19
|
+
s.add_development_dependency("rake", ">= 0")
|
20
|
+
end
|
21
|
+
|
22
|
+
Gem::PackageTask.new(spec) do |pkg|
|
23
|
+
end
|
24
|
+
|
25
|
+
Rake::ExtensionTask.new('generalhashfunctions_ext', spec)
|
@@ -0,0 +1,186 @@
|
|
1
|
+
#include "AP_GeneralHashFunctions.h"
|
2
|
+
|
3
|
+
unsigned int RSHash(unsigned char* str, unsigned int len)
|
4
|
+
{
|
5
|
+
unsigned int b = 378551;
|
6
|
+
unsigned int a = 63689;
|
7
|
+
unsigned int hash = 0;
|
8
|
+
unsigned int i = 0;
|
9
|
+
|
10
|
+
for(i = 0; i < len; str++, i++)
|
11
|
+
{
|
12
|
+
hash = hash * a + (*str);
|
13
|
+
a = a * b;
|
14
|
+
}
|
15
|
+
|
16
|
+
return hash;
|
17
|
+
}
|
18
|
+
/* End Of RS Hash Function */
|
19
|
+
|
20
|
+
|
21
|
+
unsigned int JSHash(unsigned char* str, unsigned int len)
|
22
|
+
{
|
23
|
+
unsigned int hash = 1315423911;
|
24
|
+
unsigned int i = 0;
|
25
|
+
|
26
|
+
for(i = 0; i < len; str++, i++)
|
27
|
+
{
|
28
|
+
hash ^= ((hash << 5) + (*str) + (hash >> 2));
|
29
|
+
}
|
30
|
+
|
31
|
+
return hash;
|
32
|
+
}
|
33
|
+
/* End Of JS Hash Function */
|
34
|
+
|
35
|
+
|
36
|
+
unsigned int PJWHash(unsigned char* str, unsigned int len)
|
37
|
+
{
|
38
|
+
const unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
|
39
|
+
const unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
|
40
|
+
const unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
|
41
|
+
const unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
|
42
|
+
unsigned int hash = 0;
|
43
|
+
unsigned int test = 0;
|
44
|
+
unsigned int i = 0;
|
45
|
+
|
46
|
+
for(i = 0; i < len; str++, i++)
|
47
|
+
{
|
48
|
+
hash = (hash << OneEighth) + (*str);
|
49
|
+
|
50
|
+
if((test = hash & HighBits) != 0)
|
51
|
+
{
|
52
|
+
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
return hash;
|
57
|
+
}
|
58
|
+
/* End Of P. J. Weinberger Hash Function */
|
59
|
+
|
60
|
+
|
61
|
+
unsigned int ELFHash(unsigned char* str, unsigned int len)
|
62
|
+
{
|
63
|
+
unsigned int hash = 0;
|
64
|
+
unsigned int x = 0;
|
65
|
+
unsigned int i = 0;
|
66
|
+
|
67
|
+
for(i = 0; i < len; str++, i++)
|
68
|
+
{
|
69
|
+
hash = (hash << 4) + (*str);
|
70
|
+
if((x = hash & 0xF0000000L) != 0)
|
71
|
+
{
|
72
|
+
hash ^= (x >> 24);
|
73
|
+
}
|
74
|
+
hash &= ~x;
|
75
|
+
}
|
76
|
+
|
77
|
+
return hash;
|
78
|
+
}
|
79
|
+
/* End Of ELF Hash Function */
|
80
|
+
|
81
|
+
|
82
|
+
unsigned int BKDRHash(unsigned char* str, unsigned int len)
|
83
|
+
{
|
84
|
+
unsigned int seed = 131; /* 31 131 1313 13131 131313 etc.. */
|
85
|
+
unsigned int hash = 0;
|
86
|
+
unsigned int i = 0;
|
87
|
+
|
88
|
+
for(i = 0; i < len; str++, i++)
|
89
|
+
{
|
90
|
+
hash = (hash * seed) + (*str);
|
91
|
+
}
|
92
|
+
|
93
|
+
return hash;
|
94
|
+
}
|
95
|
+
/* End Of BKDR Hash Function */
|
96
|
+
|
97
|
+
|
98
|
+
unsigned int SDBMHash(unsigned char* str, unsigned int len)
|
99
|
+
{
|
100
|
+
unsigned int hash = 0;
|
101
|
+
unsigned int i = 0;
|
102
|
+
|
103
|
+
for(i = 0; i < len; str++, i++)
|
104
|
+
{
|
105
|
+
hash = (*str) + (hash << 6) + (hash << 16) - hash;
|
106
|
+
}
|
107
|
+
|
108
|
+
return hash;
|
109
|
+
}
|
110
|
+
/* End Of SDBM Hash Function */
|
111
|
+
|
112
|
+
|
113
|
+
unsigned int DJBHash(unsigned char* str, unsigned int len)
|
114
|
+
{
|
115
|
+
unsigned int hash = 5381;
|
116
|
+
unsigned int i = 0;
|
117
|
+
|
118
|
+
for(i = 0; i < len; str++, i++)
|
119
|
+
{
|
120
|
+
hash = ((hash << 5) + hash) + (*str);
|
121
|
+
}
|
122
|
+
|
123
|
+
return hash;
|
124
|
+
}
|
125
|
+
/* End Of DJB Hash Function */
|
126
|
+
|
127
|
+
|
128
|
+
unsigned int DEKHash(unsigned char* str, unsigned int len)
|
129
|
+
{
|
130
|
+
unsigned int hash = len;
|
131
|
+
unsigned int i = 0;
|
132
|
+
|
133
|
+
for(i = 0; i < len; str++, i++)
|
134
|
+
{
|
135
|
+
hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
|
136
|
+
}
|
137
|
+
return hash;
|
138
|
+
}
|
139
|
+
/* End Of DEK Hash Function */
|
140
|
+
|
141
|
+
|
142
|
+
unsigned int BPHash(unsigned char* str, unsigned int len)
|
143
|
+
{
|
144
|
+
unsigned int hash = 0;
|
145
|
+
unsigned int i = 0;
|
146
|
+
for(i = 0; i < len; str++, i++)
|
147
|
+
{
|
148
|
+
hash = hash << 7 ^ (*str);
|
149
|
+
}
|
150
|
+
|
151
|
+
return hash;
|
152
|
+
}
|
153
|
+
/* End Of BP Hash Function */
|
154
|
+
|
155
|
+
|
156
|
+
unsigned int FNVHash(unsigned char* str, unsigned int len)
|
157
|
+
{
|
158
|
+
const unsigned int fnv_prime = 0x811C9DC5;
|
159
|
+
unsigned int hash = 0;
|
160
|
+
unsigned int i = 0;
|
161
|
+
|
162
|
+
for(i = 0; i < len; str++, i++)
|
163
|
+
{
|
164
|
+
hash *= fnv_prime;
|
165
|
+
hash ^= (*str);
|
166
|
+
}
|
167
|
+
|
168
|
+
return hash;
|
169
|
+
}
|
170
|
+
/* End Of FNV Hash Function */
|
171
|
+
|
172
|
+
|
173
|
+
unsigned int APHash(unsigned char* str, unsigned int len)
|
174
|
+
{
|
175
|
+
unsigned int hash = 0xAAAAAAAA;
|
176
|
+
unsigned int i = 0;
|
177
|
+
|
178
|
+
for(i = 0; i < len; str++, i++)
|
179
|
+
{
|
180
|
+
hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ (*str) * (hash >> 3)) :
|
181
|
+
(~((hash << 11) + ((*str) ^ (hash >> 5))));
|
182
|
+
}
|
183
|
+
|
184
|
+
return hash;
|
185
|
+
}
|
186
|
+
/* End Of AP Hash Function */
|
@@ -0,0 +1,44 @@
|
|
1
|
+
/*
|
2
|
+
**************************************************************************
|
3
|
+
* *
|
4
|
+
* General Purpose Hash Function Algorithms Library *
|
5
|
+
* *
|
6
|
+
* Author: Arash Partow - 2002 *
|
7
|
+
* URL: http://www.partow.net *
|
8
|
+
* URL: http://www.partow.net/programming/hashfunctions/index.html *
|
9
|
+
* *
|
10
|
+
* Copyright notice: *
|
11
|
+
* Free use of the General Purpose Hash Function Algorithms Library is *
|
12
|
+
* permitted under the guidelines and in accordance with the most current *
|
13
|
+
* version of the Common Public License. *
|
14
|
+
* http://www.opensource.org/licenses/cpl1.0.php *
|
15
|
+
* *
|
16
|
+
**************************************************************************
|
17
|
+
*/
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
#ifndef INCLUDE_GENERALHASHFUNCTION_C_H
|
22
|
+
#define INCLUDE_GENERALHASHFUNCTION_C_H
|
23
|
+
|
24
|
+
|
25
|
+
#include <stdio.h>
|
26
|
+
|
27
|
+
|
28
|
+
typedef unsigned int (*hash_function)(unsigned char*, unsigned int len);
|
29
|
+
|
30
|
+
|
31
|
+
unsigned int RSHash (unsigned char* str, unsigned int len);
|
32
|
+
unsigned int JSHash (unsigned char* str, unsigned int len);
|
33
|
+
unsigned int PJWHash (unsigned char* str, unsigned int len);
|
34
|
+
unsigned int ELFHash (unsigned char* str, unsigned int len);
|
35
|
+
unsigned int BKDRHash(unsigned char* str, unsigned int len);
|
36
|
+
unsigned int SDBMHash(unsigned char* str, unsigned int len);
|
37
|
+
unsigned int DJBHash (unsigned char* str, unsigned int len);
|
38
|
+
unsigned int DEKHash (unsigned char* str, unsigned int len);
|
39
|
+
unsigned int BPHash (unsigned char* str, unsigned int len);
|
40
|
+
unsigned int FNVHash (unsigned char* str, unsigned int len);
|
41
|
+
unsigned int APHash (unsigned char* str, unsigned int len);
|
42
|
+
|
43
|
+
|
44
|
+
#endif
|
@@ -0,0 +1,93 @@
|
|
1
|
+
// Include the Ruby headers and goodies
|
2
|
+
#include "ruby.h"
|
3
|
+
#include "AP_GeneralHashFunctions.h"
|
4
|
+
|
5
|
+
// Defining a space for information and references about the module to be stored internally
|
6
|
+
VALUE GeneralHashFunctionsExt = Qnil;
|
7
|
+
|
8
|
+
// Prototype for the initialization method - Ruby calls this, not you
|
9
|
+
void Init_generalhashfunctions_ext();
|
10
|
+
|
11
|
+
// Prototype for our methods
|
12
|
+
VALUE method_rshash(VALUE self, VALUE str);
|
13
|
+
VALUE method_jshash(VALUE self, VALUE str);
|
14
|
+
VALUE method_pjwhash(VALUE self, VALUE str);
|
15
|
+
VALUE method_elfhash(VALUE self, VALUE str);
|
16
|
+
VALUE method_bkdrhash(VALUE self, VALUE str);
|
17
|
+
VALUE method_sdbmhash(VALUE self, VALUE str);
|
18
|
+
VALUE method_djbhash(VALUE self, VALUE str);
|
19
|
+
VALUE method_dekhash(VALUE self, VALUE str);
|
20
|
+
VALUE method_bphash(VALUE self, VALUE str);
|
21
|
+
VALUE method_fnvhash(VALUE self, VALUE str);
|
22
|
+
VALUE method_aphash(VALUE self, VALUE str);
|
23
|
+
|
24
|
+
// The initialization method for this module
|
25
|
+
void Init_generalhashfunctions_ext() {
|
26
|
+
GeneralHashFunctionsExt = rb_define_module("GeneralHashFunctionsExt");
|
27
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "rshash", method_rshash, 1);
|
28
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "jshash", method_jshash, 1);
|
29
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "pjwhash", method_pjwhash, 1);
|
30
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "elfhash", method_elfhash, 1);
|
31
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "bkdrhash", method_bkdrhash, 1);
|
32
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "sdbmhash", method_sdbmhash, 1);
|
33
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "djbhash", method_djbhash, 1);
|
34
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "dekhash", method_dekhash, 1);
|
35
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "bphash", method_bphash, 1);
|
36
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "fnvhash", method_fnvhash, 1);
|
37
|
+
rb_define_singleton_method(GeneralHashFunctionsExt, "aphash", method_aphash, 1);
|
38
|
+
}
|
39
|
+
|
40
|
+
VALUE method_rshash(VALUE self, VALUE str) {
|
41
|
+
StringValue(str);
|
42
|
+
return UINT2NUM(RSHash(StringValuePtr(str), RSTRING_LEN(str)));
|
43
|
+
}
|
44
|
+
|
45
|
+
VALUE method_jshash(VALUE self, VALUE str) {
|
46
|
+
StringValue(str);
|
47
|
+
return UINT2NUM(JSHash(StringValuePtr(str), RSTRING_LEN(str)));
|
48
|
+
}
|
49
|
+
|
50
|
+
VALUE method_pjwhash(VALUE self, VALUE str) {
|
51
|
+
StringValue(str);
|
52
|
+
return UINT2NUM(PJWHash(StringValuePtr(str), RSTRING_LEN(str)));
|
53
|
+
}
|
54
|
+
|
55
|
+
VALUE method_elfhash(VALUE self, VALUE str) {
|
56
|
+
StringValue(str);
|
57
|
+
return UINT2NUM(ELFHash(StringValuePtr(str), RSTRING_LEN(str)));
|
58
|
+
}
|
59
|
+
|
60
|
+
VALUE method_bkdrhash(VALUE self, VALUE str) {
|
61
|
+
StringValue(str);
|
62
|
+
return UINT2NUM(BKDRHash(StringValuePtr(str), RSTRING_LEN(str)));
|
63
|
+
}
|
64
|
+
|
65
|
+
VALUE method_sdbmhash(VALUE self, VALUE str) {
|
66
|
+
StringValue(str);
|
67
|
+
return UINT2NUM(SDBMHash(StringValuePtr(str), RSTRING_LEN(str)));
|
68
|
+
}
|
69
|
+
|
70
|
+
VALUE method_djbhash(VALUE self, VALUE str) {
|
71
|
+
StringValue(str);
|
72
|
+
return UINT2NUM(DJBHash(StringValuePtr(str), RSTRING_LEN(str)));
|
73
|
+
}
|
74
|
+
|
75
|
+
VALUE method_dekhash(VALUE self, VALUE str) {
|
76
|
+
StringValue(str);
|
77
|
+
return UINT2NUM(DEKHash(StringValuePtr(str), RSTRING_LEN(str)));
|
78
|
+
}
|
79
|
+
|
80
|
+
VALUE method_bphash(VALUE self, VALUE str) {
|
81
|
+
StringValue(str);
|
82
|
+
return UINT2NUM(BPHash(StringValuePtr(str), RSTRING_LEN(str)));
|
83
|
+
}
|
84
|
+
|
85
|
+
VALUE method_fnvhash(VALUE self, VALUE str) {
|
86
|
+
StringValue(str);
|
87
|
+
return UINT2NUM(FNVHash(StringValuePtr(str), RSTRING_LEN(str)));
|
88
|
+
}
|
89
|
+
|
90
|
+
VALUE method_aphash(VALUE self, VALUE str) {
|
91
|
+
StringValue(str);
|
92
|
+
return UINT2NUM(APHash(StringValuePtr(str), RSTRING_LEN(str)));
|
93
|
+
}
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashfunctions-extension
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Martin Kozák
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake-compiler
|
16
|
+
requirement: &20813020 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *20813020
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &20812120 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *20812120
|
36
|
+
description: 11 fast hash functions for general purpose non-cryptographic use implemented
|
37
|
+
originally by Arash Partow. The Ruby C extension.
|
38
|
+
email: martinkozak@martinkozak.net
|
39
|
+
executables: []
|
40
|
+
extensions:
|
41
|
+
- ext/generalhashfunctions_ext/extconf.rb
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- LICENSE.txt
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- ext/generalhashfunctions_ext/AP_GeneralHashFunctions.c
|
49
|
+
- ext/generalhashfunctions_ext/AP_GeneralHashFunctions.h
|
50
|
+
- ext/generalhashfunctions_ext/extconf.rb
|
51
|
+
- ext/generalhashfunctions_ext/generalhashfunctions_ext.c
|
52
|
+
- lib/general-hash-functions/extension.rb
|
53
|
+
homepage: http://github.com/martinkozak/hashfunctions-extension
|
54
|
+
licenses:
|
55
|
+
- CPL
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.15
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: 11 fast hash functions for general purpose non-cryptographic use as C extension.
|
78
|
+
test_files: []
|