Skip to content

Commit

Permalink
add CityHashCrc256 binding function.
Browse files Browse the repository at this point in the history
  • Loading branch information
fbzhong committed May 8, 2011
1 parent 1819db3 commit 636e98f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
32 changes: 32 additions & 0 deletions binding.cc
Expand Up @@ -182,6 +182,18 @@ objectify_hash(const String::AsciiValue &hash) {
}
}

Local<Object>
objectify_hashs(const uint64 hashs[], size_t size) {
Local<Object> ret = Array::New(size);

for(int i=0; i<size; i++) {
uint64 hash = hashs[i];
ret->Set(i, objectify_hash(hash));
}

return ret;
}

Handle<Value>
node_Stringify(const Arguments& args) {
HandleScope scope;
Expand Down Expand Up @@ -292,6 +304,25 @@ node_CityHashCrc128(const Arguments& args) {
return scope.Close(objectify_hash(hash));
}

Handle<Value>
node_CityHashCrc256(const Arguments& args) {
HandleScope scope;

printf("handle");
int args_len = args.Length();
if(args_len != 1) {
return ThrowException(String::New("Invalid arguments."));
}

String::Utf8Value data(args[0]->ToString());
const char* str = *data;
size_t len = data.length();

uint64 hashs[4];
CityHashCrc256(str, len, hashs);
return scope.Close(objectify_hashs(hashs, 4));
}

extern "C" void
init (Handle<Object> target) {
HandleScope scope;
Expand All @@ -300,4 +331,5 @@ init (Handle<Object> target) {
target->Set(String::New("hash64"), FunctionTemplate::New(node_CityHash64)->GetFunction());
target->Set(String::New("hash128"), FunctionTemplate::New(node_CityHash128)->GetFunction());
target->Set(String::New("crc128"), FunctionTemplate::New(node_CityHashCrc128)->GetFunction());
target->Set(String::New("crc256"), FunctionTemplate::New(node_CityHashCrc256)->GetFunction());
}
7 changes: 7 additions & 0 deletions test-sample/main.cc
Expand Up @@ -30,6 +30,13 @@ int main (int argc, const char * argv[])
hash = CityHashCrc128WithSeed("Hello", strlen("Hello"), seed);
std::cout << "CityHashCrc128WithSeed(\"Hello\") : " << hash.first << "," << hash.second << std::endl;

char *longString = "CityHash provides hash functions for strings. The latest stable version is cityhash-1.0.1.tar.gz. The functions mix the input bits thoroughly but are not suitable for cryptography. We provide reference implementations in C++, with a friendly MIT license. To download the code with svn use these instructions. The README contains a good explanation of the various CityHash functions. However, here is a short summary: CityHash64() and similar return a 64-bit hash. Inside Google, where CityHash was developed starting in 2010, we use variants of CityHash64() mainly in hash tables such as hash_map<string, int>. CityHash128() and similar return a 128-bit hash and are tuned for strings of at least a few hundred bytes. Depending on your compiler and hardware, it may be faster than CityHash64() on sufficiently long strings. It is known to be slower than necessary on shorter strings, but we expect that case to be relatively unimportant. Inside Google we use variants of CityHash128() mainly for code that wants to minimize collisions. CityHashCrc128() and CityHashCrc256() and similar are additional variants, specially tuned for CPUs with SSE4.2.";
hash = CityHashCrc128(longString, strlen(longString));
std::cout << "CityHashCrc128(longString) : " << hash.first << "," << hash.second << std::endl;

uint64 crc256_results[4];
CityHashCrc256("Hello", strlen("Hello"), crc256_results);

return 0;
}

4 changes: 4 additions & 0 deletions test.js
Expand Up @@ -219,4 +219,8 @@ assertEqual({
"uint128": true
}, cityhash.crc128('Hello', cityhash.objectify('12343,30293')), 'Crc128 for "Hello" with objectify seed 12343,30293');

// since my computer does not support SSE4.2, can not calculate the CRC by CityHashCrc256 algorithm.
// assertEqual({
// }, cityhash.crc256('Hello'), 'Crc256 for "Hello"');

end();

0 comments on commit 636e98f

Please sign in to comment.