From 0c5f2301df5d1ee25b95e9da6f3c754a4d09e207 Mon Sep 17 00:00:00 2001 From: Davi de Castro Reis Date: Fri, 5 Nov 2010 21:46:53 -0200 Subject: [PATCH] Fixed compilation error and detected iterator problem. --- cxxmph/Makefile.am | 6 +++- cxxmph/cmph_hash_map.h | 24 ++++--------- cxxmph/cmph_hash_map_test.cc | 10 ++++++ cxxmph/cxxmph.cc | 68 ++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 19 deletions(-) create mode 100644 cxxmph/cxxmph.cc diff --git a/cxxmph/Makefile.am b/cxxmph/Makefile.am index 4c8989a..c3a0a2b 100644 --- a/cxxmph/Makefile.am +++ b/cxxmph/Makefile.am @@ -1,4 +1,5 @@ -bin_PROGRAMS = cmph_hash_map_test mphtable_test trigraph_test +noinst_PROGRAMS = cmph_hash_map_test mphtable_test trigraph_test +bin_PROGRAMS = cxxmph lib_LTLIBRARIES = libcxxmph.la include_HEADERS = cmph_hash_map.h mphtable.h MurmurHash2.h trigraph.h cmph_hash_function.h stringpiece.h @@ -13,3 +14,6 @@ mphtable_test_SOURCES = mphtable_test.cc trigraph_test_LDADD = libcxxmph.la trigraph_test_SOURCES = trigraph_test.cc + +cxxmph_LDADD = libcxxmph.la +cxxmph_SOURCES = cxxmph.cc diff --git a/cxxmph/cmph_hash_map.h b/cxxmph/cmph_hash_map.h index 51ddcef..bebb6cd 100644 --- a/cxxmph/cmph_hash_map.h +++ b/cxxmph/cmph_hash_map.h @@ -5,27 +5,14 @@ #include "MurmurHash2.h" #include "mphtable.h" -namespace __gnu_cxx { -template <> struct hash { - std::size_t operator()(std::string const& s) const { - return MurmurHash2(s.c_str(), s.length(), 1 /* seed */); - } -}; -template <> struct hash { - std::size_t operator()(const long long int& s) const { - return MurmurHash2(reinterpret_cast(&s), sizeof(long long int), 1 /* seed */); - } -}; -} // namespace __gnu_cxx - namespace cxxmph { // Save on repetitive typing. -#define CMPH_TMPL_SPEC template +#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 -template , class EqualKey = std::equal_to, class Alloc = std::allocator > +template , class EqualKey = std::equal_to, class Alloc = std::allocator > class cmph_hash_map { public: typedef Key key_type; @@ -132,10 +119,10 @@ 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(); } -CMPH_METHOD_DECL(void_type, clear)() { +CMPH_METHOD_DECL(void_type, clear)() { values_.clear(); slack_.clear(); - table_.clear(); + table_.clear(); } CMPH_METHOD_DECL(void_type, erase)(iterator pos) { @@ -163,6 +150,8 @@ CMPH_METHOD_DECL(const_iterator, find)(const key_type& k) const { CMPH_METHOD_DECL(iterator, find)(const key_type& k) { if (!slack_.empty()) { typename slack_type::const_iterator it = slack_.find(k); + // TODO(davi) this is broken, it->second should be an integer + // otherwise I cannot access values_ iterators. if (it != slack_.end()) return values_.begin() + it->second; } if (table_.size() == 0) return end(); @@ -172,7 +161,6 @@ CMPH_METHOD_DECL(iterator, find)(const key_type& k) { } return end(); } - CMPH_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/cmph_hash_map_test.cc index a3e02f9..6f9eeab 100644 --- a/cxxmph/cmph_hash_map_test.cc +++ b/cxxmph/cmph_hash_map_test.cc @@ -1,5 +1,6 @@ #include "cmph_hash_map.h" +#include #include #include #include @@ -28,4 +29,13 @@ int main(int argc, char** argv) { std::cerr << "Search " << i - 1 << " gives " << h.find(buf)->second << std::endl; } } + for (int j = 0; j < 100; ++j) { + for (int i = 1000; i > 0; --i) { + char buf[10]; + snprintf(buf, 10, "%d", i*100 - 1); + h.find(buf); + std::cerr << "Search " << i*100 - 1 << " gives " << h.find(buf)->second << std::endl; + } + } + } diff --git a/cxxmph/cxxmph.cc b/cxxmph/cxxmph.cc new file mode 100644 index 0000000..9b93450 --- /dev/null +++ b/cxxmph/cxxmph.cc @@ -0,0 +1,68 @@ +// Copyright 2010 Google Inc. All Rights Reserved. +// Author: davi@google.com (Davi Reis) + +#include + +#include +#include +#include +#include + +#include "cmph_hash_map.h" +#include "config.h" + +using std::cerr; +using std::cout; +using std::endl; +using std::getline; +using std::ifstream; +using std::string; +using std::vector; + +using cxxmph::cmph_hash_map; + +void usage(const char* prg) { + cerr << "usage: " << prg << "[-v] [-h] [-V]" << endl; +} +void usage_long(const char* prg) { + usage(prg); + cerr << " -h\t print this help message" << endl; + cerr << " -V\t print version number and exit" << endl; + cerr << " -v\t increase verbosity (may be used multiple times)" << endl; +} + +int main(int argc, char** argv) { + + int verbosity = 0; + while (1) { + char ch = (char)getopt(argc, argv, "hv"); + if (ch == -1) break; + switch (ch) { + case 'h': + usage_long(argv[0]); + return 0; + case 'V': + std::cout << VERSION << std::endl; + return 0; + case 'v': + ++verbosity; + break; + } + } + if (optind != argc - 1) { + usage(argv[0]); + return 1; + } + vector keys; + ifstream f(argv[optind]); + string buffer; + while (!getline(f, buffer).eof()) keys.push_back(buffer); + cmph_hash_map table; + for (int i = 0; i < keys.size(); ++i) table[keys[i].c_str()] = keys[i]; + cmph_hash_map::const_iterator it = table.begin(); + cmph_hash_map::const_iterator end = table.end(); + for (; it != end; ++it) { + cout << (it - table.begin()) << ": " << it->first + <<" -> " << it->second << endl; + } +}