diff --git a/cxxmph/Makefile.am b/cxxmph/Makefile.am index 087e487..662f488 100644 --- a/cxxmph/Makefile.am +++ b/cxxmph/Makefile.am @@ -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 diff --git a/cxxmph/benchmark.cc b/cxxmph/benchmark.cc index 9a45491..04e5086 100644 --- a/cxxmph/benchmark.cc +++ b/cxxmph/benchmark.cc @@ -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); } diff --git a/cxxmph/bm_numbers.cc b/cxxmph/bm_numbers.cc index 40bef70..85653f5 100644 --- a/cxxmph/bm_numbers.cc +++ b/cxxmph/bm_numbers.cc @@ -2,7 +2,7 @@ #include #include "benchmark.h" -#include "mphtable.h" +#include "mph_table.h" using std::set; using std::vector; diff --git a/cxxmph/bm_urls.cc b/cxxmph/bm_urls.cc index 916c725..6424755 100644 --- a/cxxmph/bm_urls.cc +++ b/cxxmph/bm_urls.cc @@ -6,7 +6,7 @@ #include #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 urls_; - cxxmph::cmph_hash_map table_; + cxxmph::mph_map table_; }; class BM_UrlsFind : public BM_UrlsCreate { diff --git a/cxxmph/cxxmph.cc b/cxxmph/cxxmph.cc index 623ecc4..68bb23e 100644 --- a/cxxmph/cxxmph.cc +++ b/cxxmph/cxxmph.cc @@ -8,7 +8,7 @@ #include #include -#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] " << 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 table; + mph_map table; for (int i = 0; i < keys.size(); ++i) table[keys[i]] = keys[i]; - cmph_hash_map::const_iterator it = table.begin(); - cmph_hash_map::const_iterator end = table.end(); + mph_map::const_iterator it = table.begin(); + mph_map::const_iterator end = table.end(); for (; it != end; ++it) { cout << (it - table.begin()) << ": " << it->first <<" -> " << it->second << endl; diff --git a/cxxmph/cmph_hash_map.h b/cxxmph/mph_map.h similarity index 73% rename from cxxmph/cmph_hash_map.h rename to cxxmph/mph_map.h index 3f1b1b5..538f708 100644 --- a/cxxmph/cmph_hash_map.h +++ b/cxxmph/mph_map.h @@ -4,17 +4,17 @@ #include // for std::pair #include "MurmurHash2.h" -#include "mphtable.h" +#include "mph_table.h" namespace cxxmph { // Save on repetitive typing. -#define CMPH_TMPL_SPEC template -#define CMPH_CLASS_SPEC cmph_hash_map -#define CMPH_METHOD_DECL(r, m) CMPH_TMPL_SPEC typename CMPH_CLASS_SPEC::r CMPH_CLASS_SPEC::m +#define MPH_MAP_TMPL_SPEC template +#define MPH_MAP_CLASS_SPEC mph_map +#define MPH_MAP_METHOD_DECL(r, m) MPH_MAP_TMPL_SPEC typename MPH_MAP_CLASS_SPEC::r MPH_MAP_CLASS_SPEC::m template , class EqualKey = std::equal_to, class Alloc = std::allocator > -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 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 values_; - SimpleMPHTable::hash_function> table_; + SimpleMPHTable::hash_function> table_; // TODO(davi) optimize slack to no hold a copy of the key typedef typename std::unordered_map 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; } diff --git a/cxxmph/cmph_hash_map_test.cc b/cxxmph/mph_map_test.cc similarity index 87% rename from cxxmph/cmph_hash_map_test.cc rename to cxxmph/mph_map_test.cc index 50a7bc5..579e0ca 100644 --- a/cxxmph/cmph_hash_map_test.cc +++ b/cxxmph/mph_map_test.cc @@ -1,16 +1,16 @@ -#include "cmph_hash_map.h" - #include #include #include #include +#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 b; + mph_map 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 h; + mph_map h; h.insert(std::make_pair("-1",-1)); - cmph_hash_map::const_iterator it; + mph_map::const_iterator it; for (it = h.begin(); it != h.end(); ++it) { std::cerr << it->first << " -> " << it->second << std::endl; } diff --git a/cxxmph/mphtable.cc b/cxxmph/mph_table.cc similarity index 99% rename from cxxmph/mphtable.cc rename to cxxmph/mph_table.cc index bbc0c31..03dcfa3 100644 --- a/cxxmph/mphtable.cc +++ b/cxxmph/mph_table.cc @@ -1,10 +1,11 @@ #include #include +#include using std::cerr; using std::endl; -#include "mphtable.h" +#include "mph_table.h" using std::vector; diff --git a/cxxmph/mphtable.h b/cxxmph/mph_table.h similarity index 96% rename from cxxmph/mphtable.h rename to cxxmph/mph_table.h index 340b3db..a2fa236 100644 --- a/cxxmph/mphtable.h +++ b/cxxmph/mph_table.h @@ -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 >::hash_function> +template >::hash_function> class SimpleMPHTable : public MPHTable { public: template @@ -155,4 +155,4 @@ class SimpleMPHTable : public MPHTable { } // namespace cxxmph -#endif // __CXXMPH_MPHTABLE_H__ +#endif // __CXXMPH_MPH_TABLE_H__ diff --git a/cxxmph/mphtable_test.cc b/cxxmph/mph_table_test.cc similarity index 82% rename from cxxmph/mphtable_test.cc rename to cxxmph/mph_table_test.cc index eb6ed3f..d12f901 100644 --- a/cxxmph/mphtable_test.cc +++ b/cxxmph/mph_table_test.cc @@ -3,7 +3,7 @@ #include #include -#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 mphtable; - assert(mphtable.Reset(keys.begin(), keys.end())); + SimpleMPHTable mph_table; + assert(mph_table.Reset(keys.begin(), keys.end())); vector ids; for (vector::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; diff --git a/cxxmph/cxxmph_hash.h b/cxxmph/seeded_hash.h similarity index 71% rename from cxxmph/cxxmph_hash.h rename to cxxmph/seeded_hash.h index 98748a0..7446813 100644 --- a/cxxmph/cxxmph_hash.h +++ b/cxxmph/seeded_hash.h @@ -1,3 +1,6 @@ +#ifndef __CXXMPH_SEEDED_HASH_H__ +#define __CXXMPH_SEEDED_HASH_H__ + #include // for uint32_t and friends #include @@ -47,36 +50,38 @@ struct seeded_hash_function { } }; -template struct cxxmph_hash +template struct seeded_hash { typedef seeded_hash_function hash_function; }; // Use Murmur2 instead for all types defined in std::hash, plus // std::string which is commonly extended. -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct cxxmph_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; } // namespace cxxmph + +#endif // __CXXMPH_SEEDED_HASH_H__