9template<
typename ElementType>
21 inline ElementType* new_alloc(
size_t size)
22 {
return static_cast<ElementType*
>(malloc(
sizeof(ElementType)*
size));}
26 if (this->m_size != 0 || m_ptr !=
nullptr)
29 this->m_ptr =
nullptr;
36 this->m_ptr =
nullptr;
49 {
return static_cast<const_iterator>(&this->m_ptr[this->m_size]); }
51 auto rbegin() {
return std::reverse_iterator(
end()); }
52 auto rend() {
return std::reverse_iterator(
begin()); }
58 size_t size()
const {
return this->m_size; }
64 else if(
count == this->m_size)
74 this->m_ptr = new_alloc(this->m_size);
89 this->m_ptr = new_alloc(
size);
98 m_size{right_hand_side.m_size}
104 this->m_ptr = this->m_size > 0 ? new_alloc(this->m_size) :
nullptr;
107 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
119 ElementType &
operator[](
size_t index) {
return this->m_ptr[index]; }
121 const ElementType &
operator[](
size_t index)
const {
return this->m_ptr[index]; }
123 ElementType &
at(
size_t index)
125 if (index < this->m_size && this->m_ptr !=
nullptr)
126 return this->m_ptr[index];
131 const ElementType &
at(
size_t index)
const
133 if (index < this->m_size && this->m_ptr !=
nullptr)
134 return this->m_ptr[index];
143 stream <<
"Copy assignment operator() called" <<
endl;
150 if (
this != std::addressof(right_hand_side))
152 if (this->m_size == right_hand_side.m_size)
159 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
165 this->m_size = right_hand_side.m_size;
167 if (this->m_size > 0)
172 this->m_ptr = new_alloc(this->m_size);
173 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
188 : m_size{right_hand_side.m_size},
189 m_ptr{right_hand_side.m_ptr}
196 right_hand_side.invalidate();
201 stream <<
"Move assignment operator() called" <<
endl;
203 if (
this != std::addressof(right_hand_side))
207 this->m_size = right_hand_side.m_size;
208 this->m_ptr = right_hand_side.m_ptr;
213 right_hand_side.invalidate();
233 for(
size_t i = 0; i <
size; ++i)
236 os << da[
size] <<
" }";
247 for(
size_t i = 0; i < a.size(); ++i)
257 std::vector< dynamic_array_malloc<int> > jagged_array(
count);
259 stream <<
"At this point, default constructor of dynamic_array_malloc is called "
262 for(
size_t i = 0; i < jagged_array.size(); ++i)
265 stream <<
"In the for loop, default constructor of dynamic_array_malloc is called "
266 <<
count <<
" times, and move constructor is called " <<
count <<
" times\n" <<
endl;
273 stream <<
"\nthis is better, but not perfect!!\n" <<
endl;
277 std::vector< dynamic_array_malloc<int> > jagged_array;
278 jagged_array.reserve(
count);
280 for(
size_t i = 0; i < jagged_array.capacity(); ++i)
287 stream <<
"\nthis is PEREFECT WAY, YOU SHOULD ALWAYS USE THIS METHOD!!\n" <<
endl;
291 std::vector< dynamic_array_malloc<int> > jagged_array;
292 jagged_array.reserve(
count);
294 for(
size_t i = 0; i < jagged_array.capacity(); ++i)
296 jagged_array.emplace_back(i + 1);
303 for(
size_t j = 0; j < jagged_array.back().size(); ++j)
304 jagged_array.back()[j] = (int)j;
309 for(
auto& da: jagged_array)
325 for_each(array.crbegin(), array.crend(), [](
auto e)
327 std::cout << e << std::endl;
333 size_t test_count = 5;
341 v.emplace_back((
int)i);
void please_use_this_method()
void is_still_is_not_perfect()
void examples_for_dynamic_array_malloc()
const ElementType & operator[](size_t index) const
void resize(size_t count)
ElementType & operator[](size_t index)
friend std::ostream & operator<<(std::ostream &os, const dynamic_array_malloc &da)
dynamic_array_malloc & operator=(const dynamic_array_malloc &right_hand_side)
dynamic_array_malloc(const dynamic_array_malloc &right_hand_side)
dynamic_array_malloc(dynamic_array_malloc &&right_hand_side) noexcept
ElementType & at(size_t index)
ElementType * operator&()
const ElementType * const_iterator
dynamic_array_malloc & operator=(dynamic_array_malloc &&right_hand_side) noexcept
dynamic_array_malloc(size_t size=1)
const ElementType & at(size_t index) const
std::string elapsed_time(bool bReset=true, TimeUnit dummy_time=TimeUnit{}) const
constexpr size_t array_size(ElementType(&array)[ElementSize]) noexcept
Stream output operators << are implemented.
#define Tpf_ThrowDebugException(debug_message)
Throw a debug_exception with message as argument.