|
| 1 | +/* Exercise 25.14 */ |
| 2 | + |
| 3 | +#include<iostream> |
| 4 | +#include<sstream> |
| 5 | +#include<iomanip> |
| 6 | +#include"C25_Exercise_25.14.h" |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +inline void error(string s) { throw runtime_error(s); } |
| 11 | +inline void error(const string& s, const string& s2) { error(s + s2); } |
| 12 | +inline void error(const string& s, int i) { ostringstream os; os << s << ": " << i; error(os.str()); } |
| 13 | +inline void keep_window_open() { char ch; cin >> ch; } |
| 14 | + |
| 15 | +int main() |
| 16 | +{ |
| 17 | + enum Action { |
| 18 | + EXIT = -1, PRINTACTIONLIST, |
| 19 | + CASE1, CASE2, CASE3, CASE4, CASE5, CASE6, CASE7, CASE8, CASE9, CASE10 |
| 20 | + }; |
| 21 | + const string actionList = "\tList of actions:\n" |
| 22 | + " (1) VectorPool\n" |
| 23 | + " (-1) Exit, (0) Print the list of actions\n"; |
| 24 | + cout << actionList; |
| 25 | + int action; |
| 26 | + bool cond{ true }; |
| 27 | + while (cond) try { |
| 28 | + cout << "\nPlease enter the action: "; |
| 29 | + if (!(cin >> action)) { ClearInput(cin); error("Error. Incorrect input"); } |
| 30 | + char ch; |
| 31 | + cin.get(ch); |
| 32 | + switch (action) { |
| 33 | + case CASE1: { |
| 34 | + cout << endl; |
| 35 | + |
| 36 | + Pool<int> poolInt; |
| 37 | + |
| 38 | + int* ptrInt01 = poolInt.get(); |
| 39 | + if (ptrInt01 != nullptr) { |
| 40 | + *ptrInt01 = 111111; |
| 41 | + cout << sp_2 << "*ptrInt01 = " << *ptrInt01 << endl; |
| 42 | + } |
| 43 | + int* ptrInt02 = poolInt.get(); |
| 44 | + if (ptrInt02 != nullptr) { |
| 45 | + *ptrInt02 = 222222; |
| 46 | + cout << sp_2 << "*ptrInt02 = " << *ptrInt02 << endl; |
| 47 | + } |
| 48 | + cout << endl; |
| 49 | + |
| 50 | + VectorPool<int, 1024> vpi1{ poolInt }; |
| 51 | + for (int i = 0; i < vpi1.Size(); ++i) { |
| 52 | + vpi1[i] = i; |
| 53 | + } |
| 54 | + cout << sp_2 << "vpi1:" << endl; |
| 55 | + for (int i = 0; i < vpi1.Size(); ++i) { |
| 56 | + cout << sp_4 << "vpi1[" << i << "] = " << vpi1[i] << endl; |
| 57 | + } |
| 58 | + cout << endl; |
| 59 | + |
| 60 | + int* ptrInt03 = poolInt.get(); |
| 61 | + if (ptrInt03 != nullptr) { |
| 62 | + *ptrInt03 = 333333; |
| 63 | + cout << sp_2 << "*ptrInt03 = " << *ptrInt03 << endl; |
| 64 | + } |
| 65 | + int* ptrInt04 = poolInt.get(); |
| 66 | + if (ptrInt04 != nullptr) { |
| 67 | + *ptrInt04 = 444444; |
| 68 | + cout << sp_2 << "*ptrInt04 = " << *ptrInt04 << endl; |
| 69 | + } |
| 70 | + cout << endl; |
| 71 | + |
| 72 | + VectorPool<int, 512> vpi2{ poolInt }; |
| 73 | + for (int i = 0; i < vpi2.Size(); ++i) { |
| 74 | + vpi2[i] = i + 1000; |
| 75 | + } |
| 76 | + cout << sp_2 << "vpi2:" << endl; |
| 77 | + for (int i = 0; i < vpi2.Size(); ++i) { |
| 78 | + cout << sp_4 << "vpi2[" << i << "] = " << vpi2[i] << endl; |
| 79 | + } |
| 80 | + cout << endl; |
| 81 | + |
| 82 | + int* ptrInt05 = poolInt.get(); |
| 83 | + if (ptrInt05 != nullptr) { |
| 84 | + *ptrInt05 = 555555; |
| 85 | + cout << sp_2 << "*ptrInt05 = " << *ptrInt05 << endl; |
| 86 | + } |
| 87 | + int* ptrInt06 = poolInt.get(); |
| 88 | + if (ptrInt06 != nullptr) { |
| 89 | + *ptrInt06 = 666666; |
| 90 | + cout << sp_2 << "*ptrInt06 = " << *ptrInt06 << endl; |
| 91 | + } |
| 92 | + cout << endl; |
| 93 | + poolInt.free(ptrInt05); |
| 94 | + poolInt.free(ptrInt06); |
| 95 | + |
| 96 | + VectorPool<int, 64> vpi3{ poolInt }; |
| 97 | + for (int i = 0; i < vpi3.Size(); ++i) { |
| 98 | + vpi3[i] = i + 2000; |
| 99 | + } |
| 100 | + cout << sp_2 << "vpi3:" << endl; |
| 101 | + for (int i = 0; i < vpi3.Size(); ++i) { |
| 102 | + cout << sp_4 << "vpi3[" << i << "] = " << vpi3[i] << endl; |
| 103 | + } |
| 104 | + cout << endl; |
| 105 | + |
| 106 | + int* ptrInt07 = poolInt.get(); |
| 107 | + if (ptrInt07 != nullptr) { |
| 108 | + *ptrInt07 = 777777; |
| 109 | + cout << sp_2 << "*ptrInt07 = " << *ptrInt07 << endl; |
| 110 | + } |
| 111 | + int* ptrInt08 = poolInt.get(); |
| 112 | + if (ptrInt08 != nullptr) { |
| 113 | + *ptrInt08 = 888888; |
| 114 | + cout << sp_2 << "*ptrInt08 = " << *ptrInt08 << endl; |
| 115 | + } |
| 116 | + cout << endl; |
| 117 | + poolInt.free(ptrInt07); |
| 118 | + poolInt.free(ptrInt08); |
| 119 | + |
| 120 | + // print all pool |
| 121 | + cout << "Pool<int> poolInt:" << endl; |
| 122 | + cout << poolInt; |
| 123 | + |
| 124 | + cout << vsp_2; |
| 125 | + break; |
| 126 | + } |
| 127 | + case PRINTACTIONLIST: |
| 128 | + cout << actionList; |
| 129 | + break; |
| 130 | + case EXIT: |
| 131 | + cond = false; |
| 132 | + break; |
| 133 | + default: |
| 134 | + error("Error. Incorrect action number"); |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + catch (runtime_error& e) { |
| 139 | + cerr << e.what() << endl; |
| 140 | + } |
| 141 | + catch (...) { |
| 142 | + cerr << "Error. Exception\n"; |
| 143 | + return 1; |
| 144 | + } |
| 145 | + return 0; |
| 146 | +} |
| 147 | + |
| 148 | +void ClearInput(istream& is) |
| 149 | +{ |
| 150 | + is.clear(); |
| 151 | + is.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 152 | +} |
| 153 | + |
| 154 | +template<typename T> Pool<T>::Pool(int n) |
| 155 | + : nFree{ n }, n{ n } |
| 156 | +{ |
| 157 | + elem = new T[n]; |
| 158 | + if (elem == nullptr) throw PoolException{}; |
| 159 | + isUsed = new bool[n]; |
| 160 | + if (isUsed == nullptr) throw PoolException{}; |
| 161 | + for (int i = 0; i < n; ++i) isUsed[i] = false; |
| 162 | +} |
| 163 | + |
| 164 | +template<typename T> Pool<T>::~Pool() |
| 165 | +{ |
| 166 | + delete[] elem; |
| 167 | + delete[] isUsed; |
| 168 | +} |
| 169 | + |
| 170 | +template<typename T> T* Pool<T>::get() |
| 171 | +{ |
| 172 | + if (nFree == 0) return nullptr; |
| 173 | + for (int i = 0; i < n; ++i) { |
| 174 | + if (isUsed[i] == false) { |
| 175 | + isUsed[i] = true; |
| 176 | + --nFree; |
| 177 | + return &elem[i]; |
| 178 | + } |
| 179 | + } |
| 180 | + return nullptr; |
| 181 | +} |
| 182 | + |
| 183 | +template<typename T> T* Pool<T>::get(int n) |
| 184 | +{ |
| 185 | + if (nFree == 0) return nullptr; |
| 186 | + int i = 0; |
| 187 | + while (i < this->n) { |
| 188 | + if (isUsed[i] == false) { |
| 189 | + int j; |
| 190 | + for (j = 0; j < n; ++j) { |
| 191 | + if (i >= this->n) return nullptr; |
| 192 | + if (isUsed[i] == true) break; |
| 193 | + ++i; |
| 194 | + } |
| 195 | + if (j == n) { |
| 196 | + for (int k = i - n; k < i; ++k) isUsed[k] = true; |
| 197 | + nFree -= n; |
| 198 | + return &elem[i - n]; |
| 199 | + } |
| 200 | + } |
| 201 | + ++i; |
| 202 | + } |
| 203 | + return nullptr; |
| 204 | +} |
| 205 | + |
| 206 | +template<typename T> void Pool<T>::free(T* ptr) |
| 207 | +{ |
| 208 | + if (ptr >= elem && ptr < &elem[n]) { // range [elem[0]:elem[N]) |
| 209 | + isUsed[ptr - elem] = false; |
| 210 | + ++nFree; |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +template<typename T> void Pool<T>::free(T* ptr, int n) |
| 215 | +{ |
| 216 | + if (ptr >= elem && ptr + n - 1 < &elem[this->n]) { // range [elem[0]:elem[N]) |
| 217 | + int i = ptr - elem; |
| 218 | + for (int j = 0; j < n; ++j) { |
| 219 | + isUsed[i] = false; |
| 220 | + ++i; |
| 221 | + } |
| 222 | + nFree += n; |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +template<typename T> ostream& operator<<(ostream& os, const Pool<T>& pool) |
| 227 | +{ |
| 228 | + os << boolalpha; |
| 229 | + for (int i = 0; i < pool.n; ++i) { |
| 230 | + os << sp_4 << "elem[" << i << "] = " << pool.elem[i] |
| 231 | + << ", isUsed[" << i << "] = " << pool.isUsed[i] << endl; |
| 232 | + } |
| 233 | + os << noboolalpha; |
| 234 | + return os; |
| 235 | +} |
| 236 | + |
| 237 | +template<typename T, int N> VectorPool<T, N>::VectorPool(Pool<T>& pool) |
| 238 | + : sz{ N }, pl{ pool } |
| 239 | +{ |
| 240 | + elem = pool.get(N); |
| 241 | + if (elem == nullptr) throw VectorPoolException{}; |
| 242 | +} |
| 243 | + |
| 244 | +template<typename T, int N> VectorPool<T, N>::~VectorPool() |
| 245 | +{ |
| 246 | + pl.free(elem, sz); |
| 247 | +} |
0 commit comments