fork download
  1. #include <algorithm>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <memory>
  5.  
  6. using namespace std;
  7.  
  8. template <class X>
  9. class ExArray {
  10. public:
  11. virtual X& operator [](const unsigned int& i) = 0;
  12. virtual X& at(const unsigned int& i) = 0;
  13. virtual unsigned int getSize() const = 0;
  14. virtual bool isEmpty() const = 0;
  15. virtual void push(const X& x) = 0;
  16. virtual void pop() = 0;
  17. virtual void clear() = 0;
  18. virtual void resize(const unsigned int& sz) = 0;
  19. };
  20.  
  21. template <class X>
  22. class PrimitiveExArray : public ExArray<X> {
  23. public:
  24. PrimitiveExArray(const unsigned int& sz = 0);
  25. virtual ~PrimitiveExArray();
  26. virtual X& operator [](const unsigned int& i);
  27. virtual X& at(const unsigned int& i);
  28. virtual unsigned int getSize() const;
  29. virtual bool isEmpty() const;
  30. virtual void push(const X& x);
  31. virtual void pop();
  32. virtual void clear();
  33. virtual void resize(const unsigned int& sz);
  34. protected:
  35. X* buf;
  36. unsigned int buf_sz;
  37. unsigned int sz;
  38. };
  39.  
  40. template <class X>
  41. class ClassObjectExArray : public ExArray<X> {
  42. public:
  43. ClassObjectExArray(const unsigned int& sz = 0);
  44. virtual ~ClassObjectExArray();
  45. virtual X& operator [](const unsigned int& i);
  46. virtual X& at(const unsigned int& i);
  47. virtual unsigned int getSize() const;
  48. virtual bool isEmpty() const;
  49. virtual void push(const X& x);
  50. virtual void pop();
  51. virtual void clear();
  52. virtual void resize(const unsigned int& sz);
  53. protected:
  54. X* buf;
  55. unsigned int buf_sz;
  56. unsigned int sz;
  57. };
  58.  
  59. template <class X>
  60. PrimitiveExArray<X>::PrimitiveExArray(const unsigned int& sz) : buf(nullptr), buf_sz(0), sz(0) {
  61. resize(sz);
  62. }
  63.  
  64. template <class X>
  65. PrimitiveExArray<X>::~PrimitiveExArray() {
  66. if (this->buf) delete[] this->buf;
  67. }
  68.  
  69. template <class X>
  70. X& PrimitiveExArray<X>::operator [](const unsigned int& i) {
  71. return this->buf[i];
  72. }
  73.  
  74. template <class X>
  75. X& PrimitiveExArray<X>::at(const unsigned int& i) {
  76. return this->buf[i];
  77. }
  78.  
  79. template <class X>
  80. unsigned int PrimitiveExArray<X>::getSize() const {
  81. return this->sz;
  82. }
  83.  
  84. template <class X>
  85. bool PrimitiveExArray<X>::isEmpty() const {
  86. return this->sz == 0;
  87. }
  88.  
  89. template <class X>
  90. void PrimitiveExArray<X>::push(const X& x) {
  91. resize(this->sz + 1);
  92. this->buf[this->sz - 1] = x;
  93. }
  94.  
  95. template <class X>
  96. void PrimitiveExArray<X>::pop() {
  97. this->sz--;
  98. }
  99.  
  100. template <class X>
  101. void PrimitiveExArray<X>::clear() {
  102. this->sz = 0;
  103. }
  104.  
  105. template <class X>
  106. void PrimitiveExArray<X>::resize(const unsigned int& sz) {
  107. if (sz > this->buf_sz) {
  108. unsigned int new_buf_sz = sz << 1;
  109. X* new_buf = new X[new_buf_sz];
  110. if (this->buf_sz != 0) {
  111. memcpy(new_buf, this->buf, sizeof(X) * this->sz);
  112. delete[] this->buf;
  113. }
  114. this->buf = new_buf;
  115. this->buf_sz = new_buf_sz;
  116. }
  117. this->sz = sz;
  118. }
  119.  
  120. template <class X>
  121. ClassObjectExArray<X>::ClassObjectExArray(const unsigned int& sz) : buf(nullptr), buf_sz(0), sz(0) {
  122. resize(sz);
  123. }
  124.  
  125. template <class X>
  126. ClassObjectExArray<X>::~ClassObjectExArray() {
  127. if (this->buf) delete[] this->buf;
  128. }
  129.  
  130. template <class X>
  131. X& ClassObjectExArray<X>::operator [](const unsigned int& i) {
  132. return this->buf[i];
  133. }
  134.  
  135. template <class X>
  136. X& ClassObjectExArray<X>::at(const unsigned int& i) {
  137. return this->buf[i];
  138. }
  139.  
  140. template <class X>
  141. unsigned int ClassObjectExArray<X>::getSize() const {
  142. return this->sz;
  143. }
  144.  
  145. template <class X>
  146. bool ClassObjectExArray<X>::isEmpty() const {
  147. return this->sz == 0;
  148. }
  149.  
  150. template <class X>
  151. void ClassObjectExArray<X>::push(const X& x) {
  152. resize(this->sz + 1);
  153. this->buf[this->sz - 1] = x;
  154. }
  155.  
  156. template <class X>
  157. void ClassObjectExArray<X>::pop() {
  158. this->buf[this->sz - 1] = X();
  159. this->sz--;
  160. }
  161.  
  162. template <class X>
  163. void ClassObjectExArray<X>::clear() {
  164. for (unsigned int i = 0; i < this->sz; i++) this->buf[i] = X();
  165. this->sz = 0;
  166. }
  167.  
  168. template <class X>
  169. void ClassObjectExArray<X>::resize(const unsigned int& sz) {
  170. if (sz > this->buf_sz) {
  171. unsigned int new_buf_sz = sz << 1;
  172. X* new_buf = new X[new_buf_sz];
  173. if (this->buf_sz != 0) {
  174. copy(this->buf, this->buf + this->sz, new_buf);
  175. delete[] this->buf;
  176. }
  177. this->buf = new_buf;
  178. this->buf_sz = new_buf_sz;
  179. }
  180. this->sz = sz;
  181. }
  182.  
  183. class Hoge {
  184. public:
  185. int n;
  186. Hoge(const int& n = 0) : n(n) { cout << "Hoge(const int& n = 0) : n=" << this->n << endl; }
  187. Hoge(const Hoge& a) : n(a.n) { cout << "Hoge(const Hoge& a) : n=" << this->n << endl; }
  188. virtual ~Hoge() { cout << "virtual ~Hoge() : n=" << this->n << endl; }
  189. Hoge& operator =(const Hoge& a) { cout << "Hoge& operator =(const Hoge& a) : a.n=" << a.n << ", n=" << this->n << endl; this->n = a.n; return *this; }
  190. operator int() const { return this->n; }
  191. };
  192.  
  193. template <class X> static void print(const shared_ptr<ExArray<X> >& a) {
  194. cout << "size=" << a->getSize() << endl;
  195. for (unsigned int i = 0; i < a->getSize(); i++)
  196. cout << "[" << i << "]=" << (int)a->at(i) << endl;
  197. cout << endl;
  198. }
  199.  
  200. int main() {
  201. {
  202. shared_ptr<ExArray<int> > a(new PrimitiveExArray<int>(1));
  203. print(a);
  204.  
  205. a->push(1);
  206. a->push(23);
  207. a->push(456);
  208. print(a);
  209.  
  210. a->at(1) = 7;
  211. a->pop();
  212. print(a);
  213.  
  214. a->resize(10);
  215. a->at(2) = 8;
  216. a->at(3) = 9;
  217. a->at(4) = 10;
  218. a->resize(7);
  219. print(a);
  220.  
  221. a->clear();
  222. print(a);
  223. }
  224. {
  225. shared_ptr<ExArray<Hoge> > a(new ClassObjectExArray<Hoge>(1));
  226. print(a);
  227.  
  228. a->push(Hoge(1));
  229. a->push(Hoge(23));
  230. a->push(Hoge(456));
  231. print(a);
  232.  
  233. a->at(1) = Hoge(7);
  234. a->pop();
  235. print(a);
  236.  
  237. a->resize(10);
  238. a->at(2) = Hoge(8);
  239. a->at(3) = Hoge(9);
  240. a->at(4) = Hoge(10);
  241. a->resize(7);
  242. print(a);
  243.  
  244. a->clear();
  245. print(a);
  246. }
  247. return 0;
  248. }
  249.  
Success #stdin #stdout 0s 3440KB
stdin
Standard input is empty
stdout
size=1
[0]=0

size=4
[0]=0
[1]=1
[2]=23
[3]=456

size=3
[0]=0
[1]=7
[2]=23

size=7
[0]=0
[1]=7
[2]=8
[3]=9
[4]=10
[5]=0
[6]=0

size=0

Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
size=1
[0]=0

Hoge(const int& n = 0) : n=1
Hoge& operator =(const Hoge& a) : a.n=1, n=0
virtual ~Hoge() : n=1
Hoge(const int& n = 0) : n=23
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge& operator =(const Hoge& a) : a.n=0, n=0
Hoge& operator =(const Hoge& a) : a.n=1, n=0
virtual ~Hoge() : n=1
virtual ~Hoge() : n=0
Hoge& operator =(const Hoge& a) : a.n=23, n=0
virtual ~Hoge() : n=23
Hoge(const int& n = 0) : n=456
Hoge& operator =(const Hoge& a) : a.n=456, n=0
virtual ~Hoge() : n=456
size=4
[0]=0
[1]=1
[2]=23
[3]=456

Hoge(const int& n = 0) : n=7
Hoge& operator =(const Hoge& a) : a.n=7, n=1
virtual ~Hoge() : n=7
Hoge(const int& n = 0) : n=0
Hoge& operator =(const Hoge& a) : a.n=0, n=456
virtual ~Hoge() : n=0
size=3
[0]=0
[1]=7
[2]=23

Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge(const int& n = 0) : n=0
Hoge& operator =(const Hoge& a) : a.n=0, n=0
Hoge& operator =(const Hoge& a) : a.n=7, n=0
Hoge& operator =(const Hoge& a) : a.n=23, n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=23
virtual ~Hoge() : n=7
virtual ~Hoge() : n=0
Hoge(const int& n = 0) : n=8
Hoge& operator =(const Hoge& a) : a.n=8, n=23
virtual ~Hoge() : n=8
Hoge(const int& n = 0) : n=9
Hoge& operator =(const Hoge& a) : a.n=9, n=0
virtual ~Hoge() : n=9
Hoge(const int& n = 0) : n=10
Hoge& operator =(const Hoge& a) : a.n=10, n=0
virtual ~Hoge() : n=10
size=7
[0]=0
[1]=7
[2]=8
[3]=9
[4]=10
[5]=0
[6]=0

Hoge(const int& n = 0) : n=0
Hoge& operator =(const Hoge& a) : a.n=0, n=0
virtual ~Hoge() : n=0
Hoge(const int& n = 0) : n=0
Hoge& operator =(const Hoge& a) : a.n=0, n=7
virtual ~Hoge() : n=0
Hoge(const int& n = 0) : n=0
Hoge& operator =(const Hoge& a) : a.n=0, n=8
virtual ~Hoge() : n=0
Hoge(const int& n = 0) : n=0
Hoge& operator =(const Hoge& a) : a.n=0, n=9
virtual ~Hoge() : n=0
Hoge(const int& n = 0) : n=0
Hoge& operator =(const Hoge& a) : a.n=0, n=10
virtual ~Hoge() : n=0
Hoge(const int& n = 0) : n=0
Hoge& operator =(const Hoge& a) : a.n=0, n=0
virtual ~Hoge() : n=0
Hoge(const int& n = 0) : n=0
Hoge& operator =(const Hoge& a) : a.n=0, n=0
virtual ~Hoge() : n=0
size=0

virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0
virtual ~Hoge() : n=0