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; }
95 if(
count == this->m_size)
104 this->m_size =
count;
105 this->m_ptr =
new ElementType[this->m_size];
120 stream <<
"Default constructor called" <<
endl;
124 this->m_ptr =
new ElementType[
size];
136 m_size{right_hand_side.m_size}
142 this->m_ptr =
new ElementType[this->m_size];
143 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
155 ElementType &
operator[](
size_t index) {
return this->m_ptr[index]; }
157 const ElementType &
operator[](
size_t index)
const {
return this->m_ptr[index]; }
159 ElementType &
at(
size_t index)
161 if (index < this->m_size && this->m_ptr !=
nullptr)
162 return this->m_ptr[index];
167 const ElementType &
at(
size_t index)
const
169 if (index < this->m_size && this->m_ptr !=
nullptr)
170 return this->m_ptr[index];
179 stream <<
"Copy assignment operator() called" <<
endl;
186 if (
this != std::addressof(right_hand_side))
188 if (this->m_size == right_hand_side.m_size)
194 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
200 this->m_size = right_hand_side.m_size;
205 this->m_ptr =
new ElementType[this->m_size];
206 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
221 : m_size{right_hand_side.m_size},
222 m_ptr{right_hand_side.m_ptr}
229 right_hand_side.invalidate();
234 stream <<
"Move assignment operator() called" <<
endl;
236 if (
this != std::addressof(right_hand_side))
240 this->m_size = right_hand_side.m_size;
241 this->m_ptr = right_hand_side.m_ptr;
246 right_hand_side.invalidate();
264 for(
size_t i = 0; i <
size; ++i)
267 os << da[
size] <<
" }";
277 for(
size_t i = 0; i < a.size(); ++i)
287 std::vector< dynamic_array<int> > jagged_array(
count);
289 stream <<
"At this point, default constructor of dynamic_array is called "
292 for(
size_t i = 0; i < jagged_array.size(); ++i)
295 stream <<
"In the for loop, default constructor of dynamic_array is called "
296 <<
count <<
" times, and move constructor is called " <<
count <<
" times\n" <<
endl;
303 stream <<
"\nthis is better, but not perfect!!\n" <<
endl;
307 std::vector< dynamic_array<int> > jagged_array;
308 jagged_array.reserve(
count);
310 for(
size_t i = 0; i < jagged_array.capacity(); ++i)
317 stream <<
"\nthis is PEREFECT WAY, YOU SHOULD ALWAYS USE THIS METHOD!!\n" <<
endl;
321 std::vector< dynamic_array<int> > jagged_array;
322 jagged_array.reserve(
count);
324 for(
size_t i = 0; i < jagged_array.capacity(); ++i)
326 jagged_array.emplace_back(i + 1);
333 for(
size_t j = 0; j < jagged_array.back().size(); ++j)
334 jagged_array.back()[j] = (int)j;
339 for(
auto& da: jagged_array)
359 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.