aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/dsa/HashTable.h
blob: 9171c72d99f61db2cf0d9960103c67472dab1b97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#ifndef   dsa_HashTable_H__
#define   dsa_HashTable_H__

#include <vector>
#include <list>

namespace meow {

/*!
 * @brief 一個當key相撞時會用list解決的hash_table
 *
 * @author cat_leopard
 */
template<class Data, class HashFunc>
class HashTableList {
private:
  std::vector<std::list<Data> > table_;
  HashFunc                          func_;
public:
  /*!
   * @brief constructor
   */
  HashTableList() {
  }

  /*!
   * @brief constructor
   * 
   * 設定table size, hash function
   */
  HashTableList(size_t size, HashFunc const& func): table_(size), func_(func) {
  }

  /*!
   * @brief destructor
   */
  ~HashTableList() {
  }

  /*!
   * @brief copy
   */
  HashTableList& copyFrom(HashTableList const& b) {
    table_ = b.table_;
    func_  = b.func_;
    return *this;
  }

  /*!
   * @brief 清除資料
   */
  void clear() {
    for (size_t i = 0, I = table_.size(); i < I; i++) {
      table_[i].clear();
    }
  }

  /*!
   * @brief 清除資料, 指定新的size與hash function
   */
  void reset(size_t size, HashFunc const& func) {
    table_.clear();
    table_.resize(std::max(size, 1u));
    func_ = func;
  }

  /*!
   * @brief 回傳table size
   */
  size_t tableSize() const {
    return table_.size();
  }

  /*!
   * @brief 回傳目前有多少element在其中
   */
  size_t size() const {
    size_t ret = 0;
    for (size_t i = 0, I = table_.size(); i < I; i++) {
      ret += table_[i].size();
    }
    return ret;
  }

  /*!
   * @brief 回傳hash function
   */
  HashFunc const& func() const {
    return func_;
  }

  /*!
   * @brief 加入新的element
   */
  bool add(Data const& e) {
    size_t index = func_(e) % size();
    table_[index].push_back(e);
    return true;
  }

  /*!
   * @brief 把給定的HashTableList中所有的element全加進來
   */
  bool add(HashTableList const& h) {
    for (size_t i = 0, I = h.table_.size(); i < I; i++) {
      for (std::list<Data>::const_iterator
           it = h.table_[index].begin(); it != h.table_[index].end(); ++it) {
        insert(*it);
      }
    }
    return true;
  }

  /*!
   * @brief 刪除element
   */
  bool del(Data const& e) {
    size_t index = func_(e) % size();
    for (std::list<Data>::const_iterator
         it = table_[index].begin(); it != table_[index].end(); ++it) {
      if ((*it) == e) {
        table_[index].erase(i);
        return true;
      }
    }
    return false;
  }

  /*!
   * @brief 刪除有出現在給定的的HashTableList中的element
   */
  bool del(HashTableList const& h) {
    if (size() > h.size()) {
      for (size_t i = 0, I = h.table_.size(); i < I; i++) {
        for (std::list<Data>::const_iterator
             it = h.table_[index].begin(); it != h.table_[index].end(); ++it) {
          erase(*it);
        }
      }
    }
    else {
      for (size_t i = 0, I = table_.size(); i < I; i++) {
        for (std::list<Data>::const_iterator
             it = table_[index].begin(); it != table_[index].end(); ) {
          if (h.exist(*it)) {
            table_[index].erase(it);
          }
          else {
            ++it;
          }
        }
      }
    }
    return true;
  }

  /*!
   * @brief 查看某element是否已經擁有
   */
  bool exist(Data const& e) const {
    size_t index = func_(e) % size();
    for (std::list<Data>::const_iterator
         it = table_[index].begin(); it != table_[index].end(); ++it) {
      if ((*it) == e)
        return true;
    }
    return false;
  }

  /*!
   * @brief 回傳所有存下來的資料
   */
  std::vector<Data> all() const {
    std::vector<Data> ret;
    for (size_t i = 0, I = table_.size(); i < I; i++) {
      for (std::list<Data>::const_iterator
           it = table_[i].begin(); it != table_[i].end(); ++it) {
        ret.push_back(*it);
      }
    }
    return ret;
  }

  /*!
   * @brief 回傳所有存下來且key為index的資料
   */
  std::vector<Data> all(size_t index) const {
    index %= table_.size();
    std::vector<Data> ret;
    for (std::list<Data>::const_iterator
         it = table_[index].begin(); it != table_[index].end(); ++it) {
      ret.push_back(*it);
    }
    return ret;
  }
  
  //! @brief same as \c copyFrom(h)
  HashTableList& operator=(HashTableList const& h) {
    return copyFrom(h);
  }
  
  //! @brief same as \c add(h)
  HashTableList& operator+=(HashTableList const& h) {
    add(h);
    return *this;
  }
  
  //! @brief same as \c del(h)
  HashTableList& operator-=(HashTableList const& h) {
    del(h);
    return *this;
  }
};

}

#endif // dsa_HashTable_H__