Templates -- Meow  1.1.2
不能,也不應該先編譯成obj-file的templates
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__