1
Fork 0

Compiles with clang in mac.

This commit is contained in:
Davi Reis 2011-05-23 17:18:24 -07:00
parent bb40a4bb00
commit bbfcdeb5a6
5 changed files with 41 additions and 33 deletions

View File

@ -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_;

View File

@ -1,18 +1,19 @@
#include <string>
#include <unordered_map>
#include <tr1/unordered_map>
#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 MapType>
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 MapType>
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_;
};

View File

@ -7,7 +7,7 @@
#include <cassert>
#include <cmath>
#include <unordered_map> // for std::hash
#include <tr1/unordered_map> // for std::tr1::hash
#include <vector>
#include <iostream>
@ -158,7 +158,7 @@ uint32_t MPHIndex::index(const Key& key) const {
return Rank(vertex);
}
template <class Key, class HashFcn = typename seeded_hash<std::hash<Key> >::hash_function>
template <class Key, class HashFcn = typename seeded_hash<std::tr1::hash<Key> >::hash_function>
class SimpleMPHIndex : public MPHIndex {
public:
template <class ForwardIterator>

View File

@ -1,5 +1,5 @@
#include <algorithm>
#include <unordered_map>
#include <tr1/unordered_map>
#include <vector>
#include <utility> // for std::pair
@ -8,12 +8,14 @@
namespace cxxmph {
using std::tr1::unordered_map;
// Save on repetitive typing.
#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> >
template <class Key, class Data, class HashFcn = std::tr1::hash<Key>, class EqualKey = std::equal_to<Key>, class Alloc = std::allocator<Data> >
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 <typename iterator>
@ -68,11 +71,11 @@ class mph_map {
return iterator_first<iterator>(it);
}
void rehash();
void pack();
std::vector<value_type> values_;
SimpleMPHIndex<Key, typename seeded_hash<HashFcn>::hash_function> index_;
// 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;
typedef unordered_map<Key, uint32_t, HashFcn, EqualKey, Alloc> 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);

View File

@ -4,7 +4,7 @@
#include <stdint.h> // for uint32_t and friends
#include <cstdlib>
#include <unordered_map> // for std::hash
#include <tr1/unordered_map> // for std::tr1::hash
#include "MurmurHash2.h"
#include "stringpiece.h"
@ -52,34 +52,34 @@ struct seeded_hash_function<Murmur2StringPiece> {
template <class HashFcn> struct seeded_hash
{ typedef seeded_hash_function<HashFcn> 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<std::hash<char*> >
template <> struct seeded_hash<std::tr1::hash<char*> >
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
template <> struct seeded_hash<std::hash<const char*> >
template <> struct seeded_hash<std::tr1::hash<const char*> >
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
template <> struct seeded_hash<std::hash<std::string> >
template <> struct seeded_hash<std::tr1::hash<std::string> >
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
template <> struct seeded_hash<std::hash<char> >
template <> struct seeded_hash<std::tr1::hash<char> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct seeded_hash<std::hash<unsigned char> >
template <> struct seeded_hash<std::tr1::hash<unsigned char> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct seeded_hash<std::hash<short> >
template <> struct seeded_hash<std::tr1::hash<short> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct seeded_hash<std::hash<unsigned short> >
template <> struct seeded_hash<std::tr1::hash<unsigned short> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct seeded_hash<std::hash<int> >
template <> struct seeded_hash<std::tr1::hash<int> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct seeded_hash<std::hash<unsigned int> >
template <> struct seeded_hash<std::tr1::hash<unsigned int> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct seeded_hash<std::hash<long> >
template <> struct seeded_hash<std::tr1::hash<long> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct seeded_hash<std::hash<unsigned long> >
template <> struct seeded_hash<std::tr1::hash<unsigned long> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct seeded_hash<std::hash<long long> >
template <> struct seeded_hash<std::tr1::hash<long long> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct seeded_hash<std::hash<unsigned long long> >
template <> struct seeded_hash<std::tr1::hash<unsigned long long> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
} // namespace cxxmph