00001
00002
00003 #ifndef AITOOLS_BIGHASHMAP_CORE_CMPH_MAP_HPP
00004 #define AITOOLS_BIGHASHMAP_CORE_CMPH_MAP_HPP
00005
00006 #include <boost/utility.hpp>
00007 #include <boost/type_traits.hpp>
00008 #include <boost/filesystem.hpp>
00009 #include <cmph.h>
00010
00011 #include "util/systemio.hpp"
00012 #include "util/exception.hpp"
00013
00014 namespace bfs = boost::filesystem;
00015
00016 namespace aitools {
00017 namespace bighashmap {
00018 namespace core {
00019
00024 template <typename T>
00025 class cmph_map : public boost::noncopyable {
00026
00027 public:
00028
00029 typedef std::string key_type;
00030 typedef T value_type;
00031 typedef uint32_t checksum_type;
00032
00033 public:
00034
00035 cmph_map(const bfs::path& mph_file)
00036 {
00037
00038 FILE* mph_rfs(util::fopen(mph_file, "rb"));
00039 mphf_ = cmph_load(mph_rfs);
00040 if (mphf_ == NULL)
00041 {
00042 util::throw_runtime_error("Cannot load MPHF from", mph_file);
00043 }
00044 util::fclose(mph_rfs);
00045 }
00046
00047 virtual ~cmph_map()
00048 {
00049 cmph_destroy(mphf_);
00050 }
00051
00052 public:
00053
00054 virtual bool
00055 find(const key_type& key, value_type& value) = 0;
00056
00057 uint32_t
00058 size() const
00059 {
00060 return cmph_size(mphf_);
00061 }
00062
00063 protected:
00064
00065 uint32_t
00066 hash(const std::string& key)
00067 {
00068
00069
00070 return cmph_search(mphf_, key.c_str(), key.size()) % size();
00071 }
00072
00073 private:
00074
00075 cmph_t* mphf_;
00076
00077 };
00078
00079 }
00080 }
00081 }
00082
00083 #endif // AITOOLS_BIGHASHMAP_CORE_CMPH_MAP_HPP