Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00037 #ifndef FCL_HASH_H
00038 #define FCL_HASH_H
00039
00040 #include <stdexcept>
00041 #include <set>
00042 #include <vector>
00043 #include <list>
00044 #include <boost/unordered_map.hpp>
00045
00046 namespace fcl
00047 {
00048
00050 template<typename Key, typename Data, typename HashFnc>
00051 class SimpleHashTable
00052 {
00053 protected:
00054 typedef std::list<Data> Bin;
00055
00056 std::vector<Bin> table_;
00057
00058 HashFnc h_;
00059
00060 size_t table_size_;
00061
00062 public:
00063 SimpleHashTable(const HashFnc& h) : h_(h)
00064 {
00065 }
00066
00068 void init(size_t size)
00069 {
00070 if(size == 0)
00071 {
00072 throw std::logic_error("SimpleHashTable must have non-zero size.");
00073 }
00074
00075 table_.resize(size);
00076 table_size_ = size;
00077 }
00078
00080 void insert(Key key, Data value)
00081 {
00082 std::vector<unsigned int> indices = h_(key);
00083 size_t range = table_.size();
00084 for(size_t i = 0; i < indices.size(); ++i)
00085 table_[indices[i] % range].push_back(value);
00086 }
00087
00089 std::vector<Data> query(Key key) const
00090 {
00091 size_t range = table_.size();
00092 std::vector<unsigned int> indices = h_(key);
00093 std::set<Data> result;
00094 for(size_t i = 0; i < indices.size(); ++i)
00095 {
00096 unsigned int index = indices[i] % range;
00097 std::copy(table_[index].begin(), table_[index].end(), std::inserter(result, result.end()));
00098 }
00099
00100 return std::vector<Data>(result.begin(), result.end());
00101 }
00102
00104 void remove(Key key, Data value)
00105 {
00106 size_t range = table_.size();
00107 std::vector<unsigned int> indices = h_(key);
00108 for(size_t i = 0; i < indices.size(); ++i)
00109 {
00110 unsigned int index = indices[i] % range;
00111 table_[index].remove(value);
00112 }
00113 }
00114
00116 void clear()
00117 {
00118 table_.clear();
00119 table_.resize(table_size_);
00120 }
00121 };
00122
00123
00124 template<typename U, typename V>
00125 class unordered_map_hash_table : public boost::unordered_map<U, V> {};
00126
00128 template<typename Key, typename Data, typename HashFnc, template<typename, typename> class TableT = unordered_map_hash_table>
00129 class SparseHashTable
00130 {
00131 protected:
00132 HashFnc h_;
00133 typedef std::list<Data> Bin;
00134 typedef TableT<size_t, Bin> Table;
00135
00136 Table table_;
00137 public:
00138 SparseHashTable(const HashFnc& h) : h_(h) {}
00139
00141 void init(size_t) { table_.clear(); }
00142
00144 void insert(Key key, Data value)
00145 {
00146 std::vector<unsigned int> indices = h_(key);
00147 for(size_t i = 0; i < indices.size(); ++i)
00148 table_[indices[i]].push_back(value);
00149 }
00150
00152 std::vector<Data> query(Key key) const
00153 {
00154 std::vector<unsigned int> indices = h_(key);
00155 std::set<Data> result;
00156 for(size_t i = 0; i < indices.size(); ++i)
00157 {
00158 unsigned int index = indices[i];
00159 typename Table::const_iterator p = table_.find(index);
00160 if(p != table_.end())
00161 std::copy((*p).second.begin(), (*p).second.end(), std::inserter(result, result.end()));
00162 }
00163
00164 return std::vector<Data>(result.begin(), result.end());
00165 }
00166
00168 void remove(Key key, Data value)
00169 {
00170 std::vector<unsigned int> indices = h_(key);
00171 for(size_t i = 0; i < indices.size(); ++i)
00172 {
00173 unsigned int index = indices[i];
00174 table_[index].remove(value);
00175 }
00176 }
00177
00179 void clear()
00180 {
00181 table_.clear();
00182 }
00183 };
00184
00185
00186 }
00187
00188 #endif