Created
March 22, 2016 06:13
-
-
Save anonymous/d4da3b91772e7418163e to your computer and use it in GitHub Desktop.
hashtabletest.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "tbb/concurrent_hash_map.h" | |
| #include "tbb/blocked_range.h" | |
| #include "tbb/parallel_for.h" | |
| #include <string> | |
| #include <stdio.h> | |
| using namespace tbb; | |
| using namespace std; | |
| struct MyHashCompare { | |
| static size_t hash( const string& x ) { | |
| size_t h = 0; | |
| for( const char* s = x.c_str(); *s; ++s ) | |
| h = (h*17)^*s; | |
| return h; | |
| } | |
| static bool equal( const string& x, const string& y ) { | |
| return x==y; | |
| } | |
| }; | |
| typedef concurrent_hash_map<string,int,MyHashCompare> StringTable; | |
| struct Tally { | |
| StringTable& table; | |
| Tally( StringTable& table_ ) : table(table_) {} | |
| void operator()( const blocked_range<string*> range ) const { | |
| for( string* p=range.begin(); p!=range.end(); ++p ) { | |
| StringTable::accessor a; | |
| table.insert( a, *p ); | |
| a->second += 1; | |
| } | |
| } | |
| }; | |
| const size_t N = 1000000; | |
| string Data[N]; | |
| void CountOccurrences() { | |
| StringTable table; | |
| parallel_for( blocked_range<string*>( Data, Data+N, 1000 ), | |
| Tally(table) ); | |
| for( StringTable::iterator i=table.begin(); i!=table.end(); ++i ) | |
| printf("%s %d\n",i->first.c_str(),i->second); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment