1 #ifndef dsa_HashTable_H__
2 #define dsa_HashTable_H__
14 template<
class Data,
class HashFunc>
17 std::vector<std::list<Data> > table_;
53 for (
size_t i = 0, I = table_.size(); i < I; i++) {
63 table_.resize(std::max(size, 1u));
79 for (
size_t i = 0, I = table_.size(); i < I; i++) {
80 ret += table_[i].size();
88 HashFunc
const&
func()
const {
95 bool add(Data
const& e) {
96 size_t index = func_(e) %
size();
97 table_[index].push_back(e);
105 for (
size_t i = 0, I = h.table_.size(); i < I; i++) {
106 for (std::list<Data>::const_iterator
107 it = h.table_[index].begin(); it != h.table_[index].end(); ++it) {
118 size_t index = func_(e) %
size();
119 for (std::list<Data>::const_iterator
120 it = table_[index].begin(); it != table_[index].end(); ++it) {
122 table_[index].erase(i);
134 for (
size_t i = 0, I = h.table_.size(); i < I; i++) {
135 for (std::list<Data>::const_iterator
136 it = h.table_[index].begin(); it != h.table_[index].end(); ++it) {
142 for (
size_t i = 0, I = table_.size(); i < I; i++) {
143 for (std::list<Data>::const_iterator
144 it = table_[index].begin(); it != table_[index].end(); ) {
146 table_[index].erase(it);
161 size_t index = func_(e) %
size();
162 for (std::list<Data>::const_iterator
163 it = table_[index].begin(); it != table_[index].end(); ++it) {
173 std::vector<Data>
all()
const {
174 std::vector<Data> ret;
175 for (
size_t i = 0, I = table_.size(); i < I; i++) {
176 for (std::list<Data>::const_iterator
177 it = table_[i].begin(); it != table_[i].end(); ++it) {
187 std::vector<Data>
all(
size_t index)
const {
188 index %= table_.size();
189 std::vector<Data> ret;
190 for (std::list<Data>::const_iterator
191 it = table_[index].begin(); it != table_[index].end(); ++it) {
217 #endif // dsa_HashTable_H__
std::vector< Data > all(size_t index) const
回傳所有存下來且key為index的資料
bool del(HashTableList const &h)
刪除有出現在給定的的HashTableList中的element
HashTableList()
constructor
HashTableList(size_t size, HashFunc const &func)
constructor
HashTableList & operator+=(HashTableList const &h)
same as add(h)
HashTableList & operator-=(HashTableList const &h)
same as del(h)
一個當key相撞時會用list解決的hash_table
size_t size() const
回傳目前有多少element在其中
std::vector< Data > all() const
回傳所有存下來的資料
bool del(Data const &e)
刪除element
HashFunc const & func() const
回傳hash function
size_t tableSize() const
回傳table size
bool exist(Data const &e) const
查看某element是否已經擁有
bool add(Data const &e)
加入新的element
void reset(size_t size, HashFunc const &func)
清除資料, 指定新的size與hash function
bool add(HashTableList const &h)
把給定的HashTableList中所有的element全加進來
HashTableList & copyFrom(HashTableList const &b)
copy
HashTableList & operator=(HashTableList const &h)
same as copyFrom(h)
~HashTableList()
destructor