Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
HashTable.h
Go to the documentation of this file.
1 #ifndef dsa_HashTable_H__
2 #define dsa_HashTable_H__
3 
4 #include <vector>
5 #include <list>
6 
7 namespace meow {
8 
14 template<class Data, class HashFunc>
16 private:
17  std::vector<std::list<Data> > table_;
18  HashFunc func_;
19 public:
24  }
25 
31  HashTableList(size_t size, HashFunc const& func): table_(size), func_(func) {
32  }
33 
38  }
39 
44  table_ = b.table_;
45  func_ = b.func_;
46  return *this;
47  }
48 
52  void clear() {
53  for (size_t i = 0, I = table_.size(); i < I; i++) {
54  table_[i].clear();
55  }
56  }
57 
61  void reset(size_t size, HashFunc const& func) {
62  table_.clear();
63  table_.resize(std::max(size, 1u));
64  func_ = func;
65  }
66 
70  size_t tableSize() const {
71  return table_.size();
72  }
73 
77  size_t size() const {
78  size_t ret = 0;
79  for (size_t i = 0, I = table_.size(); i < I; i++) {
80  ret += table_[i].size();
81  }
82  return ret;
83  }
84 
88  HashFunc const& func() const {
89  return func_;
90  }
91 
95  bool add(Data const& e) {
96  size_t index = func_(e) % size();
97  table_[index].push_back(e);
98  return true;
99  }
100 
104  bool add(HashTableList const& h) {
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) {
108  insert(*it);
109  }
110  }
111  return true;
112  }
113 
117  bool del(Data const& e) {
118  size_t index = func_(e) % size();
119  for (std::list<Data>::const_iterator
120  it = table_[index].begin(); it != table_[index].end(); ++it) {
121  if ((*it) == e) {
122  table_[index].erase(i);
123  return true;
124  }
125  }
126  return false;
127  }
128 
132  bool del(HashTableList const& h) {
133  if (size() > h.size()) {
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) {
137  erase(*it);
138  }
139  }
140  }
141  else {
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(); ) {
145  if (h.exist(*it)) {
146  table_[index].erase(it);
147  }
148  else {
149  ++it;
150  }
151  }
152  }
153  }
154  return true;
155  }
156 
160  bool exist(Data const& e) const {
161  size_t index = func_(e) % size();
162  for (std::list<Data>::const_iterator
163  it = table_[index].begin(); it != table_[index].end(); ++it) {
164  if ((*it) == e)
165  return true;
166  }
167  return false;
168  }
169 
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) {
178  ret.push_back(*it);
179  }
180  }
181  return ret;
182  }
183 
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) {
192  ret.push_back(*it);
193  }
194  return ret;
195  }
196 
199  return copyFrom(h);
200  }
201 
204  add(h);
205  return *this;
206  }
207 
210  del(h);
211  return *this;
212  }
213 };
214 
215 }
216 
217 #endif // dsa_HashTable_H__
std::vector< Data > all(size_t index) const
回傳所有存下來且key為index的資料
Definition: HashTable.h:187
bool del(HashTableList const &h)
刪除有出現在給定的的HashTableList中的element
Definition: HashTable.h:132
HashTableList()
constructor
Definition: HashTable.h:23
HashTableList(size_t size, HashFunc const &func)
constructor
Definition: HashTable.h:31
HashTableList & operator+=(HashTableList const &h)
same as add(h)
Definition: HashTable.h:203
HashTableList & operator-=(HashTableList const &h)
same as del(h)
Definition: HashTable.h:209
void clear()
清除資料
Definition: HashTable.h:52
一個當key相撞時會用list解決的hash_table
Definition: HashTable.h:15
size_t size() const
回傳目前有多少element在其中
Definition: HashTable.h:77
std::vector< Data > all() const
回傳所有存下來的資料
Definition: HashTable.h:173
bool del(Data const &e)
刪除element
Definition: HashTable.h:117
HashFunc const & func() const
回傳hash function
Definition: HashTable.h:88
size_t tableSize() const
回傳table size
Definition: HashTable.h:70
bool exist(Data const &e) const
查看某element是否已經擁有
Definition: HashTable.h:160
bool add(Data const &e)
加入新的element
Definition: HashTable.h:95
void reset(size_t size, HashFunc const &func)
清除資料, 指定新的size與hash function
Definition: HashTable.h:61
bool add(HashTableList const &h)
把給定的HashTableList中所有的element全加進來
Definition: HashTable.h:104
HashTableList & copyFrom(HashTableList const &b)
copy
Definition: HashTable.h:43
HashTableList & operator=(HashTableList const &h)
same as copyFrom(h)
Definition: HashTable.h:198
~HashTableList()
destructor
Definition: HashTable.h:37