Minor fixes.
This commit is contained in:
parent
01971c9f0d
commit
829e724a2c
9 changed files with 127 additions and 97 deletions
|
|
@ -14,23 +14,29 @@
|
|||
#include <crlib/cr-movable.h>
|
||||
#include <crlib/cr-random.h>
|
||||
|
||||
#include <initializer_list>
|
||||
|
||||
// policy to reserve memory
|
||||
CR_DECLARE_SCOPED_ENUM (ReservePolicy,
|
||||
MultipleByTwo,
|
||||
PlusOne
|
||||
Mutiple,
|
||||
Single,
|
||||
);
|
||||
|
||||
CR_NAMESPACE_BEGIN
|
||||
|
||||
// simple array class like std::vector
|
||||
template <typename T, ReservePolicy R = ReservePolicy::MultipleByTwo> class Array : public DenyCopying {
|
||||
template <typename T, ReservePolicy R = ReservePolicy::Mutiple, size_t S = 0> class Array : public DenyCopying {
|
||||
public:
|
||||
T *m_data = nullptr;
|
||||
size_t m_capacity = 0;
|
||||
size_t m_length = 0;
|
||||
|
||||
public:
|
||||
explicit Array () = default;
|
||||
explicit Array () {
|
||||
if (fix (S > 0)) {
|
||||
reserve (S);
|
||||
}
|
||||
}
|
||||
|
||||
Array (const size_t amount) {
|
||||
reserve (amount);
|
||||
|
|
@ -44,6 +50,12 @@ public:
|
|||
rhs.reset ();
|
||||
}
|
||||
|
||||
Array (std::initializer_list <T> list) {
|
||||
for (const auto &elem : list) {
|
||||
push (elem);
|
||||
}
|
||||
}
|
||||
|
||||
~Array () {
|
||||
destroy ();
|
||||
}
|
||||
|
|
@ -79,9 +91,9 @@ public:
|
|||
if (m_length + amount < m_capacity) {
|
||||
return true;
|
||||
}
|
||||
auto capacity = m_capacity ? m_capacity : 8;
|
||||
auto capacity = m_capacity ? m_capacity : 12;
|
||||
|
||||
if (cr::fix (R == ReservePolicy::MultipleByTwo)) {
|
||||
if (cr::fix (R == ReservePolicy::Mutiple)) {
|
||||
while (m_length + amount > capacity) {
|
||||
capacity *= 2;
|
||||
}
|
||||
|
|
@ -110,10 +122,10 @@ public:
|
|||
if (!ensure (amount)) {
|
||||
return false;
|
||||
}
|
||||
size_t pushLength = amount - m_length;
|
||||
size_t resizeLength = amount - m_length;
|
||||
|
||||
for (size_t i = 0; i < pushLength; ++i) {
|
||||
push (T ());
|
||||
while (resizeLength--) {
|
||||
emplace ();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
|
@ -193,6 +205,9 @@ public:
|
|||
if (index + count > m_capacity) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = m_length; i < m_length + count; i++) {
|
||||
alloc.destruct (&m_data[i]);
|
||||
}
|
||||
m_length -= count;
|
||||
|
||||
for (size_t i = index; i < m_length; ++i) {
|
||||
|
|
@ -386,5 +401,8 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
// small array (with minimal reserve policy, something like fixed array, but still able to grow, by default allocates 64 elements)
|
||||
template <typename T> using SmallArray = Array <T, ReservePolicy::Single, 64>;
|
||||
|
||||
CR_NAMESPACE_END
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue