8template<
typename ElementType>
26 if (this->m_size != 0 || m_ptr !=
nullptr)
29 this->m_ptr =
nullptr;
36 this->m_ptr =
nullptr;
46 return &this->m_ptr[0];
53 return &this->m_ptr[this->m_size];
72 return std::reverse_iterator(
end());
77 return std::reverse_iterator(
begin());
82 return std::reverse_iterator(
cend());
87 return std::reverse_iterator(
cbegin());
90 size_t size()
const {
return this->m_size; }
96 else if(
count == this->m_size)
105 this->m_size =
count;
106 this->m_ptr =
new ElementType[this->m_size];
121 stream <<
"Default constructor called" <<
endl;
125 this->m_ptr =
size > 0 ?
new ElementType[
size] :
nullptr;
137 m_size{right_hand_side.m_size}
143 this->m_ptr = this->m_size > 0 ?
new ElementType[this->m_size] :
nullptr;
146 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
158 ElementType &
operator[](
size_t index) {
return this->m_ptr[index]; }
160 const ElementType &
operator[](
size_t index)
const {
return this->m_ptr[index]; }
162 ElementType &
at(
size_t index)
164 if (index < this->m_size && this->m_ptr !=
nullptr)
165 return this->m_ptr[index];
170 const ElementType &
at(
size_t index)
const
172 if (index < this->m_size && this->m_ptr !=
nullptr)
173 return this->m_ptr[index];
182 stream <<
"Copy assignment operator() called" <<
endl;
189 if (
this != std::addressof(right_hand_side))
191 if (this->m_size == right_hand_side.m_size)
198 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
204 this->m_size = right_hand_side.m_size;
206 if (this->m_size > 0)
211 this->m_ptr =
new ElementType[this->m_size];
212 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
227 : m_size{right_hand_side.m_size},
228 m_ptr{right_hand_side.m_ptr}
235 right_hand_side.invalidate();
240 stream <<
"Move assignment operator() called" <<
endl;
242 if (
this != std::addressof(right_hand_side))
246 this->m_size = right_hand_side.m_size;
247 this->m_ptr = right_hand_side.m_ptr;
252 right_hand_side.invalidate();
274 for(
size_t i = 0; i <
size; ++i)
277 os << da[
size] <<
" }";
288 for(
size_t i = 0; i < a.size(); ++i)
298 std::vector< dynamic_array<int> > jagged_array(
count);
300 stream <<
"At this point, default constructor of dynamic_array is called "
303 for(
size_t i = 0; i < jagged_array.size(); ++i)
306 stream <<
"In the for loop, default constructor of dynamic_array is called "
307 <<
count <<
" times, and move constructor is called " <<
count <<
" times\n" <<
endl;
314 stream <<
"\nthis is better, but not perfect!!\n" <<
endl;
318 std::vector< dynamic_array<int> > jagged_array;
319 jagged_array.reserve(
count);
321 for(
size_t i = 0; i < jagged_array.capacity(); ++i)
328 stream <<
"\nthis is PEREFECT WAY, YOU SHOULD ALWAYS USE THIS METHOD!!\n" <<
endl;
332 std::vector< dynamic_array<int> > jagged_array;
333 jagged_array.reserve(
count);
335 for(
size_t i = 0; i < jagged_array.capacity(); ++i)
337 jagged_array.emplace_back(i + 1);
344 for(
size_t j = 0; j < jagged_array.back().size(); ++j)
345 jagged_array.back()[j] = (int)j;
350 for(
auto& da: jagged_array)
370 std::for_each(array.rbegin(), array.rend(),
void test_iterators_dynamic_array()
void examples_for_dynamic_array()
void why_explicit_to_constructor()
void please_use_this_method()
void is_still_is_not_perfect()
ElementType * operator&()
dynamic_array(dynamic_array &&right_hand_side) noexcept
ElementType & operator[](size_t index)
const ElementType * const_iterator
dynamic_array(const dynamic_array &right_hand_side)
const ElementType & at(size_t index) const
void resize(size_t count)
dynamic_array & operator=(dynamic_array &&right_hand_side) noexcept
ElementType & at(size_t index)
const ElementType & operator[](size_t index) const
friend std::ostream & operator<<(std::ostream &os, const dynamic_array &da)
dynamic_array(size_t size=1)
dynamic_array & operator=(const dynamic_array &right_hand_side)
Stream output operators << are implemented.
#define Tpf_ThrowDebugException(debug_message)
Throw a debug_exception with message as argument.