From bbfcdeb5a6c038330d2924cc1985425ea9d5aa3d Mon Sep 17 00:00:00 2001 From: Davi Reis Date: Mon, 23 May 2011 17:18:24 -0700 Subject: [PATCH] Compiles with clang in mac. --- cxxmph/benchmark.h | 4 ++-- cxxmph/bm_map.cc | 14 +++++++++----- cxxmph/mph_index.h | 4 ++-- cxxmph/mph_map.h | 22 +++++++++++++--------- cxxmph/seeded_hash.h | 30 +++++++++++++++--------------- 5 files changed, 41 insertions(+), 33 deletions(-) diff --git a/cxxmph/benchmark.h b/cxxmph/benchmark.h index edd3fb9..cecbc2f 100644 --- a/cxxmph/benchmark.h +++ b/cxxmph/benchmark.h @@ -18,9 +18,9 @@ class Benchmark { static void RunAll(); protected: - virtual bool SetUp() {}; + virtual bool SetUp() { return true; }; virtual void Run() = 0; - virtual bool TearDown() {}; + virtual bool TearDown() { return true; }; private: std::string name_; diff --git a/cxxmph/bm_map.cc b/cxxmph/bm_map.cc index 1d26847..5e79fbc 100644 --- a/cxxmph/bm_map.cc +++ b/cxxmph/bm_map.cc @@ -1,18 +1,19 @@ #include -#include +#include #include "bm_common.h" #include "mph_map.h" using cxxmph::mph_map; using std::string; -using std::unordered_map; +using std::tr1::unordered_map; namespace cxxmph { template class BM_MapCreate : public UrlsBenchmark { public: + BM_MapCreate(const string& urls_file) : UrlsBenchmark(urls_file) { } virtual void Run() { MapType mymap; for (auto it = urls_.begin(); it != urls_.end(); ++it) { @@ -24,17 +25,20 @@ class BM_MapCreate : public UrlsBenchmark { template class BM_MapSearch : public SearchUrlsBenchmark { public: + BM_MapSearch(const std::string& urls_file, int nsearches) + : SearchUrlsBenchmark(urls_file, nsearches) { } virtual void Run() { for (auto it = random_.begin(); it != random_.end(); ++it) { - auto value = mymap[*it]; + auto value = mymap_[it->ToString()]; } } protected: - virtual void SetUp() { + virtual bool SetUp() { for (auto it = urls_.begin(); it != urls_.end(); ++it) { mymap_[*it] = *it; } - mymap_.resize(mymap.size()); + mymap_.rehash(mymap_.bucket_count()); + return true; } MapType mymap_; }; diff --git a/cxxmph/mph_index.h b/cxxmph/mph_index.h index 77b6ea4..5b0f6c6 100644 --- a/cxxmph/mph_index.h +++ b/cxxmph/mph_index.h @@ -7,7 +7,7 @@ #include #include -#include // for std::hash +#include // for std::tr1::hash #include #include @@ -158,7 +158,7 @@ uint32_t MPHIndex::index(const Key& key) const { return Rank(vertex); } -template >::hash_function> +template >::hash_function> class SimpleMPHIndex : public MPHIndex { public: template diff --git a/cxxmph/mph_map.h b/cxxmph/mph_map.h index 1c01b64..d52f617 100644 --- a/cxxmph/mph_map.h +++ b/cxxmph/mph_map.h @@ -1,5 +1,5 @@ #include -#include +#include #include #include // for std::pair @@ -8,12 +8,14 @@ namespace cxxmph { +using std::tr1::unordered_map; + // Save on repetitive typing. #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 > +template , class EqualKey = std::equal_to, class Alloc = std::allocator > class mph_map { public: typedef Key key_type; @@ -52,7 +54,8 @@ class mph_map { const_iterator find(const key_type& k) const; data_type& operator[](const key_type &k); - void pack() { rehash(); } + size_type bucket_count() const { return size(); } + void rehash(size_type nbuckets /*ignored*/) { pack(); } private: template @@ -68,11 +71,11 @@ class mph_map { return iterator_first(it); } - void rehash(); + void pack(); std::vector values_; SimpleMPHIndex::hash_function> index_; // TODO(davi) optimize slack to no hold a copy of the key - typedef typename std::unordered_map slack_type; + typedef unordered_map slack_type; slack_type slack_; }; @@ -82,7 +85,7 @@ bool operator==(const MPH_MAP_CLASS_SPEC& lhs, const MPH_MAP_CLASS_SPEC& rhs) { } MPH_MAP_TMPL_SPEC MPH_MAP_CLASS_SPEC::mph_map() { - rehash(); + pack(); } MPH_MAP_TMPL_SPEC MPH_MAP_CLASS_SPEC::~mph_map() { @@ -95,13 +98,13 @@ MPH_MAP_METHOD_DECL(insert_return_type, insert)(const value_type& x) { slack_.insert(std::make_pair(x.first, values_.size() - 1)); if (slack_.size() == index_.size() || (slack_.size() >= 256 && index_.size() == 0)) { - rehash(); + pack(); } it = find(x.first); return std::make_pair(it, true); } -MPH_MAP_METHOD_DECL(void_type, rehash)() { +MPH_MAP_METHOD_DECL(void_type, pack)() { if (values_.empty()) return; slack_type().swap(slack_); bool success = index_.Reset( @@ -123,6 +126,7 @@ 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(); } +MPH_MAP_METHOD_DECL(size_type, size)() const { return values_.size(); } MPH_MAP_METHOD_DECL(void_type, clear)() { values_.clear(); @@ -132,7 +136,7 @@ MPH_MAP_METHOD_DECL(void_type, clear)() { MPH_MAP_METHOD_DECL(void_type, erase)(iterator pos) { values_.erase(pos); - rehash(); + pack(); } MPH_MAP_METHOD_DECL(void_type, erase)(const key_type& k) { iterator it = find(k); diff --git a/cxxmph/seeded_hash.h b/cxxmph/seeded_hash.h index 7446813..d732d62 100644 --- a/cxxmph/seeded_hash.h +++ b/cxxmph/seeded_hash.h @@ -4,7 +4,7 @@ #include // for uint32_t and friends #include -#include // for std::hash +#include // for std::tr1::hash #include "MurmurHash2.h" #include "stringpiece.h" @@ -52,34 +52,34 @@ struct seeded_hash_function { template struct seeded_hash { typedef seeded_hash_function hash_function; }; -// Use Murmur2 instead for all types defined in std::hash, plus +// Use Murmur2 instead for all types defined in std::tr1::hash, plus // std::string which is commonly extended. -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; -template <> struct seeded_hash > +template <> struct seeded_hash > { typedef seeded_hash_function hash_function; }; } // namespace cxxmph