Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ef2d12e

Browse files
committed
Exercise 25.14
1 parent 1fd8653 commit ef2d12e

File tree

3 files changed

+303
-0
lines changed

3 files changed

+303
-0
lines changed

‎Chapter_25/C25_Exercise_25.14.cpp‎

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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+
}

‎Chapter_25/C25_Exercise_25.14.h‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Exercise 25.14 */
2+
3+
using namespace std;
4+
5+
const char* sp_2 = " ";
6+
const char* sp_4 = " ";
7+
const char* sp_6 = " ";
8+
const char* sp_8 = " ";
9+
const char* vsp_2 = "\n\n";
10+
const char* vsp_3 = "\n\n\n";
11+
const char* vsp_4 = "\n\n\n\n";
12+
13+
void ClearInput(istream& is);
14+
15+
class PoolException{};
16+
17+
template<typename T> class Pool { // Pool of N objects of type T
18+
public:
19+
Pool(int n = 4096); // make pool of n Ts
20+
~Pool();
21+
T* get(); // get a T from the pool; return 0 if no free Ts
22+
T* get(int n); // get a T[n] from the pool; return nullptr if no free Ts
23+
void free(T* ptr); // return a T given out by get() to the pool
24+
void free(T* ptr, int n); // return a T[n] given out by get() to the pool
25+
int available() const { return nFree; } // number of free Ts
26+
int size() { return n; }
27+
28+
template<typename T> friend ostream& operator<<(ostream& os, const Pool<T>& pool);
29+
private:
30+
// space for T[N] and data to keep track of which Ts are allocated
31+
// and which are not (e.g., a list of free objects)
32+
T* elem;
33+
bool* isUsed;
34+
int nFree; // number of free objects
35+
int n; // number of objects
36+
};
37+
38+
class VectorPoolException {};
39+
40+
template<typename T, int N> class VectorPool {
41+
public:
42+
VectorPool(Pool<T>& pool);
43+
~VectorPool();
44+
int Size() { return sz; }
45+
T& operator[](int i) { return elem[i]; }
46+
const T& operator[](int i) const { return elem[i]; }
47+
private:
48+
T* elem;
49+
int sz;
50+
Pool<T>& pl;
51+
};

‎Chapter_25/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ set (FILE_NAME10 C25_Exercise_25.10)
3131
set (SOURCE_CXX_LIST10 ${FILE_NAME10}.cpp)
3232
set (FILE_NAME11 C25_Exercise_25.11)
3333
set (SOURCE_CXX_LIST11 ${FILE_NAME11}.cpp)
34+
set (FILE_NAME12 C25_Exercise_25.14)
35+
set (SOURCE_CXX_LIST12 ${FILE_NAME12}.cpp)
3436

3537

3638
# Add source to this project's executable.
@@ -45,6 +47,7 @@ add_executable (${FILE_NAME8} ${SOURCE_CXX_LIST8})
4547
add_executable (${FILE_NAME9} ${SOURCE_CXX_LIST9})
4648
add_executable (${FILE_NAME10} ${SOURCE_CXX_LIST10})
4749
add_executable (${FILE_NAME11} ${SOURCE_CXX_LIST11})
50+
add_executable (${FILE_NAME12} ${SOURCE_CXX_LIST12})
4851

4952

5053
# TODO: Add tests and install targets if needed.
@@ -74,3 +77,5 @@ install (TARGETS ${FILE_NAME10} CONFIGURATIONS Debug DESTINATION Build/Debug)
7477
install (TARGETS ${FILE_NAME10} CONFIGURATIONS Release DESTINATION Build)
7578
install (TARGETS ${FILE_NAME11} CONFIGURATIONS Debug DESTINATION Build/Debug)
7679
install (TARGETS ${FILE_NAME11} CONFIGURATIONS Release DESTINATION Build)
80+
install (TARGETS ${FILE_NAME12} CONFIGURATIONS Debug DESTINATION Build/Debug)
81+
install (TARGETS ${FILE_NAME12} CONFIGURATIONS Release DESTINATION Build)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /