10template<
typename ElementType>
30 if constexpr(std::is_pod_v<ElementType>)
31 _aligned_free(this->m_ptr);
35 this->m_ptr =
nullptr;
47 if constexpr( std::is_pod_v<ElementType>)
49 this->m_ptr = (ElementType*) _aligned_malloc(this->m_size *
sizeof(ElementType), 16);
59 this->m_ptr =
new ElementType[this->m_size];
61 catch(
const std::bad_alloc&)
70 this->m_ptr =
nullptr;
73 inline void copy_memory(ElementType* right_hand_side_m_ptr)
75 if constexpr ( std::is_pod_v<ElementType> )
77 std::memcpy(this->m_ptr, right_hand_side_m_ptr, m_size *
sizeof(ElementType));
82 for(
size_t i = 0; i < this->m_size; ++i)
83 this->m_ptr[i] = right_hand_side_m_ptr[i];
98 return (this->m_ptr + this->m_size);
117 return std::reverse_iterator(
end());
122 return std::reverse_iterator(
begin());
127 return std::reverse_iterator(
cend());
132 return std::reverse_iterator(
cbegin());
135 size_t size()
const {
return this->m_size; }
140 if(
count == this->m_size)
146 this->m_size =
count;
165 m_size{right_hand_side.m_size}
168 copy_memory(right_hand_side.m_ptr);
173 ElementType &
operator[](
size_t index) {
return this->m_ptr[index]; }
175 const ElementType &
operator[](
size_t index)
const {
return this->m_ptr[index]; }
177 ElementType &
at(
size_t index)
179 if (index < this->m_size && this->m_ptr !=
nullptr)
180 return this->m_ptr[index];
185 const ElementType &
at(
size_t index)
const
187 if (index < this->m_size && this->m_ptr !=
nullptr)
188 return this->m_ptr[index];
202 if (
this != std::addressof(right_hand_side))
204 if (this->m_size == right_hand_side.m_size)
210 copy_memory(right_hand_side.m_ptr);
216 this->m_size = right_hand_side.m_size;
219 copy_memory(right_hand_side.m_ptr);
227 : m_size{right_hand_side.m_size},
228 m_ptr{right_hand_side.m_ptr}
233 right_hand_side.invalidate();
238 if (
this != std::addressof(right_hand_side))
242 this->m_size = right_hand_side.m_size;
243 this->m_ptr = right_hand_side.m_ptr;
248 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(),
398 void set(
float x,
float y,
float z)
406 os <<
"< " << v.
m_x <<
", " << v.
m_y <<
", " << v.
m_z <<
" >";
414 virtual void set(
float x,
float y,
float z)
428 os <<
"< " << v.
m_x <<
", " << v.
m_y <<
", " << v.
m_z <<
" >";
443 stream <<
"vector_3d_pod is pod: " << std::is_pod_v<vector_3d_pod> <<
endl;
444 stream <<
"vector_3d_not_pod is pod: " << std::is_pod_v<vector_3d_not_pod> <<
endl;
445 stream <<
"it_is_still_not_pod is pod: " << std::is_pod_v<it_is_still_not_pod> <<
endl;
446 stream <<
"int* is pod: " << std::is_pod_v<int*> <<
endl;
456template<
typename ElementType>
459 stream <<
"Benchmarking with type: "
463 <<
" - Test Count: " << test_count
464 <<
" - Element Count: " << element_count <<
endl;
469 for(
size_t i = 0; i < test_count; ++i)
472 std::vector<ElementType> v(element_count);
474 for(
size_t j = 0; j < element_count; ++j)
475 v[j] = (ElementType)j;
477 if(element_count < 21)
485 <<
" - Test Count: " << test_count
486 <<
" - Element Count: " << element_count <<
endl;
491 for(
size_t i = 0; i < test_count; ++i)
496 for(
size_t j = 0; j < element_count; ++j)
497 v[j] = (ElementType)j;
499 if(element_count < 21)
506template<
typename ElementType>
509 stream <<
"Benchmarking with type: "
513 <<
" - Test Count: " << test_count
514 <<
" - Element Count: " << element_count <<
endl;
519 for(
size_t i = 0; i < test_count; ++i)
522 std::vector<ElementType> v(element_count);
524 for(
size_t j = 0; j < element_count; ++j)
525 v[j].
set((
float)j, (
float)j, (
float)j);
527 if(element_count < 21)
535 <<
" - Test Count: " << test_count
536 <<
" - Element Count: " << element_count <<
endl;
541 for(
size_t i = 0; i < test_count; ++i)
546 for(
size_t j = 0; j < element_count; ++j)
547 v[j].
set((
float)j, (
float)j, (
float)j);
549 if(element_count < 21)
571 benchmarking_with_primitive_type<int>(10, 10'000'000);
573 benchmarking_with_primitive_type<float>(20, 10'000'000);
575 benchmarking_with_primitive_type<double>(5, 10'000'000);
577 benchmarking_with_class_type<vector_3d_pod>(5, 1'000'000);
579 benchmarking_with_class_type<vector_3d_not_pod>(5, 1'000'000);
void test_iterators_dynamic_array()
void examples_for_dynamic_array()
void why_explicit_to_constructor()
std::ostream & operator<<(std::ostream &os, const vector_3d_pod &v)
void please_use_this_method()
void is_still_is_not_perfect()
void benchmarking_with_class_type(size_t test_count, size_t element_count)
void benchmarking_with_primitive_type(size_t test_count, size_t element_count)
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)
std::string elapsed_time(bool bReset=true, TimeUnit dummy_time=TimeUnit{}) const
Implements set operations.
std::unique_ptr< int > ptr
This type is used to manipulate type list.
virtual void set(float x, float y, float z)
void set(float x, float y, float z)
Stream output operators << are implemented.
#define Tpf_ThrowDebugException(debug_message)
Throw a debug_exception with message as argument.