HashVector

 1 #ifndef hash_vector
 2 #define hash_vector
 3 #include <iostream>
 4 #include <vector>
 5 using namespace std;
 6 template <typename T>
 7
 8 class HashVector{
 9 public:
10     void init(vector<T> sets) {
11         int i;
12         for (i = 0;i < sets.size();i++) {
13             this->m_data.push_back(make_pair(i,sets[i]));
14         }
15         this->m_size = this->m_data.size();
16     }
17     const int size() {
18         return this->size;
19     }
20     void insert(int i,T item) {
21         int p = this->m_data[i].first;
22         this->m_data.push_back(make_pair(p,item));
23         this->m_data[i].first = this->m_data.size()-1;
24     }
25     vector<T> get_items(int i) {
26         vector<T> result;
27         if (i >= this->m_size) {
28             return result;
29         }
30         pair<int, T> tmp = this->m_data[i];
31         while (tmp.first != i) {
32             result.push_back(tmp.second);
33             tmp = this->m_data[tmp.first];
34         }
35         result.push_back(tmp.second);
36         return result;
37     }
38     void clear() {
39         int i;
40         for (i = this->m_data.size() - 1;i >= this->m_size;i--) {
41             this->m_data.erase(this->m_data.begin()+i);
42         }
43         for (;i >= 0;i--){
44             this->m_data[i].first = i;
45         }
46         this->m_size = this->m_data.size();
47     }
48     void destroy() {
49         this->m_data.clear();
50         this->m_size = 0;
51     }
52     void display() {
53         int i,j;
54         for (i = 0;i < this->m_size;i++) {
55             cout << "i = " << i << " : ";
56             vector<T> items = this->get_items(i);
57             for (j = 0;j < items.size();j++) {
58                 cout << items[j] << " ";
59             }
60             cout << endl;
61         }
62     }
63 private:
64     vector<pair<int,T>> m_data;
65     int m_size;
66 };
67
68 #endif
时间: 2024-10-11 20:37:35

HashVector的相关文章