1
Fork 0

Better header organization.

This commit is contained in:
Davi de Castro Reis 2011-05-15 20:47:42 -03:00
parent 0761f24182
commit 5a46ad95be
11 changed files with 78 additions and 72 deletions

View File

@ -1,18 +1,18 @@
TESTS = $(check_PROGRAMS)
check_PROGRAMS = cmph_hash_map_test mphtable_test trigraph_test
check_PROGRAMS = mph_map_test mph_table_test trigraph_test
noinst_PROGRAMS = bm_numbers bm_urls
bin_PROGRAMS = cxxmph
lib_LTLIBRARIES = libcxxmph.la
libcxxmph_la_SOURCES = MurmurHash2.h trigragh.h trigraph.cc mphtable.h mphtable.cc cxxmph_hash.h stringpiece.h benchmark.h benchmark.cc
libcxxmph_la_SOURCES = MurmurHash2.h trigragh.h trigraph.cc mph_table.h mph_table.cc seeded_hash.h stringpiece.h benchmark.h benchmark.cc
libcxxmph_la_LDFLAGS = -version-info 0:0:0
cxxmph_includedir = $(includedir)/cxxmph/
cxxmph_include_HEADERS = cmph_hash_map.h mphtable.h MurmurHash2.h trigraph.h cxxmph_hash.h stringpiece.h
cxxmph_include_HEADERS = mph_map.h mph_table.h MurmurHash2.h trigraph.h seeded_hash.h stringpiece.h
cmph_hash_map_test_LDADD = libcxxmph.la
cmph_hash_map_test_SOURCES = cmph_hash_map_test.cc
mph_map_test_LDADD = libcxxmph.la
mph_map_test_SOURCES = mph_map_test.cc
mphtable_test_LDADD = libcxxmph.la
mphtable_test_SOURCES = mphtable_test.cc
mph_table_test_LDADD = libcxxmph.la
mph_table_test_SOURCES = mph_table_test.cc
trigraph_test_LDADD = libcxxmph.la
trigraph_test_SOURCES = trigraph_test.cc

View File

@ -73,7 +73,7 @@ namespace cxxmph {
/* static */ void Benchmark::Register(Benchmark* bm) {
if (bm->name().empty()) {
string name = demangle(typeid(*bm).name());
bm->set_name(name);
bm->set_name(name);
}
g_benchmarks.push_back(bm);
}

View File

@ -2,7 +2,7 @@
#include <vector>
#include "benchmark.h"
#include "mphtable.h"
#include "mph_table.h"
using std::set;
using std::vector;

View File

@ -6,7 +6,7 @@
#include <unordered_map>
#include "benchmark.h"
#include "cmph_hash_map.h"
#include "mph_map.h"
using std::ifstream;
using std::set;
@ -43,7 +43,7 @@ class BM_UrlsCreate : public Benchmark {
urls_.swap(urls);
}
vector<string> urls_;
cxxmph::cmph_hash_map<string, int> table_;
cxxmph::mph_map<string, int> table_;
};
class BM_UrlsFind : public BM_UrlsCreate {

View File

@ -8,7 +8,7 @@
#include <string>
#include <vector>
#include "cmph_hash_map.h"
#include "mph_map.h"
#include "config.h"
using std::cerr;
@ -19,10 +19,10 @@ using std::ifstream;
using std::string;
using std::vector;
using cxxmph::cmph_hash_map;
using cxxmph::mph_map;
void usage(const char* prg) {
cerr << "usage: " << prg << "[-v] [-h] [-V]" << endl;
cerr << "usage: " << prg << " [-v] [-h] [-V] <keys.txt>" << endl;
}
void usage_long(const char* prg) {
usage(prg);
@ -58,11 +58,11 @@ int main(int argc, char** argv) {
string buffer;
while (!getline(f, buffer).eof()) keys.push_back(buffer);
for (int i = 0; i < keys.size(); ++i) string s = keys[i];
cmph_hash_map<string, string> table;
mph_map<string, string> table;
for (int i = 0; i < keys.size(); ++i) table[keys[i]] = keys[i];
cmph_hash_map<string, string>::const_iterator it = table.begin();
cmph_hash_map<string, string>::const_iterator end = table.end();
mph_map<string, string>::const_iterator it = table.begin();
mph_map<string, string>::const_iterator end = table.end();
for (; it != end; ++it) {
cout << (it - table.begin()) << ": " << it->first
<<" -> " << it->second << endl;

View File

@ -4,17 +4,17 @@
#include <utility> // for std::pair
#include "MurmurHash2.h"
#include "mphtable.h"
#include "mph_table.h"
namespace cxxmph {
// Save on repetitive typing.
#define CMPH_TMPL_SPEC template <class Key, class Data, class HashFcn, class EqualKey, class Alloc>
#define CMPH_CLASS_SPEC cmph_hash_map<Key, Data, HashFcn, EqualKey, Alloc>
#define CMPH_METHOD_DECL(r, m) CMPH_TMPL_SPEC typename CMPH_CLASS_SPEC::r CMPH_CLASS_SPEC::m
#define MPH_MAP_TMPL_SPEC template <class Key, class Data, class HashFcn, class EqualKey, class Alloc>
#define MPH_MAP_CLASS_SPEC mph_map<Key, Data, HashFcn, EqualKey, Alloc>
#define MPH_MAP_METHOD_DECL(r, m) MPH_MAP_TMPL_SPEC typename MPH_MAP_CLASS_SPEC::r MPH_MAP_CLASS_SPEC::m
template <class Key, class Data, class HashFcn = std::hash<Key>, class EqualKey = std::equal_to<Key>, class Alloc = std::allocator<Data> >
class cmph_hash_map {
class mph_map {
public:
typedef Key key_type;
typedef Data data_type;
@ -35,8 +35,8 @@ class cmph_hash_map {
typedef bool bool_type;
typedef std::pair<iterator, bool> insert_return_type;
cmph_hash_map();
~cmph_hash_map();
mph_map();
~mph_map();
iterator begin();
iterator end();
@ -70,25 +70,25 @@ class cmph_hash_map {
void rehash();
std::vector<value_type> values_;
SimpleMPHTable<Key, typename cxxmph_hash<HashFcn>::hash_function> table_;
SimpleMPHTable<Key, typename seeded_hash<HashFcn>::hash_function> table_;
// TODO(davi) optimize slack to no hold a copy of the key
typedef typename std::unordered_map<Key, uint32_t, HashFcn, EqualKey, Alloc> slack_type;
slack_type slack_;
};
CMPH_TMPL_SPEC
bool operator==(const CMPH_CLASS_SPEC& lhs, const CMPH_CLASS_SPEC& rhs) {
MPH_MAP_TMPL_SPEC
bool operator==(const MPH_MAP_CLASS_SPEC& lhs, const MPH_MAP_CLASS_SPEC& rhs) {
return lhs.values_ == rhs.values_;
}
CMPH_TMPL_SPEC CMPH_CLASS_SPEC::cmph_hash_map() {
MPH_MAP_TMPL_SPEC MPH_MAP_CLASS_SPEC::mph_map() {
rehash();
}
CMPH_TMPL_SPEC CMPH_CLASS_SPEC::~cmph_hash_map() {
MPH_MAP_TMPL_SPEC MPH_MAP_CLASS_SPEC::~mph_map() {
}
CMPH_METHOD_DECL(insert_return_type, insert)(const value_type& x) {
MPH_MAP_METHOD_DECL(insert_return_type, insert)(const value_type& x) {
iterator it = find(x.first);
if (it != end()) return std::make_pair(it, false);
values_.push_back(x);
@ -101,7 +101,7 @@ CMPH_METHOD_DECL(insert_return_type, insert)(const value_type& x) {
return std::make_pair(it, true);
}
CMPH_METHOD_DECL(void_type, rehash)() {
MPH_MAP_METHOD_DECL(void_type, rehash)() {
if (values_.empty()) return;
slack_type().swap(slack_);
bool success = table_.Reset(
@ -117,29 +117,29 @@ CMPH_METHOD_DECL(void_type, rehash)() {
values_.swap(new_values);
}
CMPH_METHOD_DECL(iterator, begin)() { return values_.begin(); }
CMPH_METHOD_DECL(iterator, end)() { return values_.end(); }
CMPH_METHOD_DECL(const_iterator, begin)() const { return values_.begin(); }
CMPH_METHOD_DECL(const_iterator, end)() const { return values_.end(); }
CMPH_METHOD_DECL(bool_type, empty)() const { return values_.empty(); }
MPH_MAP_METHOD_DECL(iterator, begin)() { return values_.begin(); }
MPH_MAP_METHOD_DECL(iterator, end)() { return values_.end(); }
MPH_MAP_METHOD_DECL(const_iterator, begin)() const { return values_.begin(); }
MPH_MAP_METHOD_DECL(const_iterator, end)() const { return values_.end(); }
MPH_MAP_METHOD_DECL(bool_type, empty)() const { return values_.empty(); }
CMPH_METHOD_DECL(void_type, clear)() {
MPH_MAP_METHOD_DECL(void_type, clear)() {
values_.clear();
slack_.clear();
table_.clear();
}
CMPH_METHOD_DECL(void_type, erase)(iterator pos) {
MPH_MAP_METHOD_DECL(void_type, erase)(iterator pos) {
values_.erase(pos);
rehash();
}
CMPH_METHOD_DECL(void_type, erase)(const key_type& k) {
MPH_MAP_METHOD_DECL(void_type, erase)(const key_type& k) {
iterator it = find(k);
if (it == end()) return;
erase(it);
}
CMPH_METHOD_DECL(const_iterator, find)(const key_type& k) const {
MPH_MAP_METHOD_DECL(const_iterator, find)(const key_type& k) const {
if (!slack_.empty()) {
typename slack_type::const_iterator it = slack_.find(k);
if (it != slack_.end()) return values_.begin() + it->second;
@ -151,7 +151,7 @@ CMPH_METHOD_DECL(const_iterator, find)(const key_type& k) const {
}
return end();
}
CMPH_METHOD_DECL(iterator, find)(const key_type& k) {
MPH_MAP_METHOD_DECL(iterator, find)(const key_type& k) {
if (!slack_.empty()) {
typename slack_type::const_iterator it = slack_.find(k);
if (it != slack_.end()) return values_.begin() + it->second;
@ -164,7 +164,7 @@ CMPH_METHOD_DECL(iterator, find)(const key_type& k) {
return end();
}
CMPH_METHOD_DECL(data_type&, operator[])(const key_type& k) {
MPH_MAP_METHOD_DECL(data_type&, operator[])(const key_type& k) {
return insert(std::make_pair(k, data_type())).first->second;
}

View File

@ -1,16 +1,16 @@
#include "cmph_hash_map.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include "mph_map.h"
using std::make_pair;
using std::string;
using cxxmph::cmph_hash_map;
using cxxmph::mph_map;
int main(int argc, char** argv) {
cmph_hash_map<int64_t, int64_t> b;
mph_map<int64_t, int64_t> b;
for (int i = 0; i < 100*1000; ++i) {
b.insert(make_pair(i, i));
}
@ -18,9 +18,9 @@ int main(int argc, char** argv) {
b.find(i);
}
/*
cmph_hash_map<string, int> h;
mph_map<string, int> h;
h.insert(std::make_pair("-1",-1));
cmph_hash_map<string, int>::const_iterator it;
mph_map<string, int>::const_iterator it;
for (it = h.begin(); it != h.end(); ++it) {
std::cerr << it->first << " -> " << it->second << std::endl;
}

View File

@ -1,10 +1,11 @@
#include <limits>
#include <iostream>
#include <vector>
using std::cerr;
using std::endl;
#include "mphtable.h"
#include "mph_table.h"
using std::vector;

View File

@ -1,5 +1,5 @@
#ifndef __CXXMPH_MPHTABLE_H__
#define __CXXMPH_MPHTABLE_H__
#ifndef __CXXMPH_MPH_TABLE_H__
#define __CXXMPH_MPH_TABLE_H__
// Minimal perfect hash abstraction implementing the BDZ algorithm
@ -15,7 +15,7 @@
using std::cerr;
using std::endl;
#include "cxxmph_hash.h"
#include "seeded_hash.h"
#include "trigraph.h"
namespace cxxmph {
@ -143,7 +143,7 @@ uint32_t MPHTable::index(const Key& key) const {
return Rank(vertex);
}
template <class Key, class HashFcn = typename cxxmph_hash<std::hash<Key> >::hash_function>
template <class Key, class HashFcn = typename seeded_hash<std::hash<Key> >::hash_function>
class SimpleMPHTable : public MPHTable {
public:
template <class ForwardIterator>
@ -155,4 +155,4 @@ class SimpleMPHTable : public MPHTable {
} // namespace cxxmph
#endif // __CXXMPH_MPHTABLE_H__
#endif // __CXXMPH_MPH_TABLE_H__

View File

@ -3,7 +3,7 @@
#include <string>
#include <vector>
#include "mphtable.h"
#include "mph_table.h"
using std::string;
using std::vector;
@ -23,11 +23,11 @@ int main(int argc, char** argv) {
keys.push_back("diogo");
keys.push_back("algume");
SimpleMPHTable<string> mphtable;
assert(mphtable.Reset(keys.begin(), keys.end()));
SimpleMPHTable<string> mph_table;
assert(mph_table.Reset(keys.begin(), keys.end()));
vector<int> ids;
for (vector<int>::size_type i = 0; i < keys.size(); ++i) {
ids.push_back(mphtable.index(keys[i]));
ids.push_back(mph_table.index(keys[i]));
cerr << " " << *(ids.end() - 1);
}
cerr << endl;

View File

@ -1,3 +1,6 @@
#ifndef __CXXMPH_SEEDED_HASH_H__
#define __CXXMPH_SEEDED_HASH_H__
#include <stdint.h> // for uint32_t and friends
#include <cstdlib>
@ -47,36 +50,38 @@ struct seeded_hash_function<Murmur2StringPiece> {
}
};
template <class HashFcn> struct cxxmph_hash
template <class HashFcn> struct seeded_hash
{ typedef seeded_hash_function<HashFcn> hash_function; };
// Use Murmur2 instead for all types defined in std::hash, plus
// std::string which is commonly extended.
template <> struct cxxmph_hash<std::hash<char*> >
template <> struct seeded_hash<std::hash<char*> >
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
template <> struct cxxmph_hash<std::hash<const char*> >
template <> struct seeded_hash<std::hash<const char*> >
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
template <> struct cxxmph_hash<std::hash<std::string> >
template <> struct seeded_hash<std::hash<std::string> >
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
template <> struct cxxmph_hash<std::hash<char> >
template <> struct seeded_hash<std::hash<char> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct cxxmph_hash<std::hash<unsigned char> >
template <> struct seeded_hash<std::hash<unsigned char> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct cxxmph_hash<std::hash<short> >
template <> struct seeded_hash<std::hash<short> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct cxxmph_hash<std::hash<unsigned short> >
template <> struct seeded_hash<std::hash<unsigned short> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct cxxmph_hash<std::hash<int> >
template <> struct seeded_hash<std::hash<int> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct cxxmph_hash<std::hash<unsigned int> >
template <> struct seeded_hash<std::hash<unsigned int> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct cxxmph_hash<std::hash<long> >
template <> struct seeded_hash<std::hash<long> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct cxxmph_hash<std::hash<unsigned long> >
template <> struct seeded_hash<std::hash<unsigned long> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct cxxmph_hash<std::hash<long long> >
template <> struct seeded_hash<std::hash<long long> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct cxxmph_hash<std::hash<unsigned long long> >
template <> struct seeded_hash<std::hash<unsigned long long> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
} // namespace cxxmph
#endif // __CXXMPH_SEEDED_HASH_H__