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
|
||||
|
||||
|
|
|
|||
|
|
@ -84,35 +84,39 @@ public:
|
|||
private:
|
||||
void percolateUp (size_t index) {
|
||||
while (index != 0) {
|
||||
size_t parent = this->parent (index);
|
||||
size_t parentIndex = parent (index);
|
||||
|
||||
if (m_data[parent] <= m_data[index]) {
|
||||
if (m_data[parentIndex] > m_data[index]) {
|
||||
cr::swap (m_data[index], m_data[parentIndex]);
|
||||
index = parentIndex;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
cr::swap (m_data[index], m_data[parent]);
|
||||
index = parent;
|
||||
}
|
||||
}
|
||||
|
||||
void percolateDown (size_t index) {
|
||||
while (this->left (index) < m_data.length ()) {
|
||||
size_t best = this->left (index);
|
||||
while (hasLeft (index)) {
|
||||
size_t bestIndex = left (index);
|
||||
|
||||
if (this->right (index) < m_data.length ()) {
|
||||
size_t right_index = this->right (index);
|
||||
if (hasRight (index)) {
|
||||
size_t rightIndex = right (index);
|
||||
|
||||
if (m_data[right_index] < m_data[best]) {
|
||||
best = right_index;
|
||||
if (m_data[rightIndex] < m_data[bestIndex]) {
|
||||
bestIndex = rightIndex;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_data[index] <= m_data[best]) {
|
||||
if (m_data[index] > m_data[bestIndex]) {
|
||||
cr::swap (m_data[index], m_data[bestIndex]);
|
||||
|
||||
index = bestIndex;
|
||||
bestIndex = left (index);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
cr::swap (m_data[index], m_data[best]);
|
||||
|
||||
index = best;
|
||||
best = this->left (index);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -125,16 +129,24 @@ private:
|
|||
}
|
||||
|
||||
private:
|
||||
static constexpr size_t parent (size_t index) {
|
||||
return (index - 1) / 2;
|
||||
}
|
||||
|
||||
static constexpr size_t left (size_t index) {
|
||||
return index << 1 | 1;
|
||||
return (index * 2) + 1;
|
||||
}
|
||||
|
||||
static constexpr size_t right (size_t index) {
|
||||
return ++index << 1;
|
||||
return (index * 2) + 2;
|
||||
}
|
||||
|
||||
static constexpr size_t parent (size_t index) {
|
||||
return --index >> 1;
|
||||
bool hasLeft (size_t index) const {
|
||||
return left (index) < m_data.length ();
|
||||
}
|
||||
|
||||
bool hasRight (size_t index) const {
|
||||
return right (index) < m_data.length ();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -275,7 +275,7 @@ public:
|
|||
|
||||
return false;
|
||||
}
|
||||
Array <uint8, ReservePolicy::PlusOne> buffer (m_chunkSize);
|
||||
SmallArray <uint8> buffer (m_chunkSize);
|
||||
m_code = parseResponseHeader (buffer.data ());
|
||||
|
||||
if (m_code != HttpClientResult::OK) {
|
||||
|
|
@ -379,7 +379,7 @@ public:
|
|||
|
||||
return false;
|
||||
}
|
||||
Array <uint8, ReservePolicy::PlusOne> buffer (m_chunkSize);
|
||||
SmallArray <uint8> buffer (m_chunkSize);
|
||||
int32 length = 0;
|
||||
|
||||
for (;;) {
|
||||
|
|
|
|||
|
|
@ -266,7 +266,7 @@ public:
|
|||
template <typename ...Args> String &assignf (const char *fmt, Args ...args) {
|
||||
const size_t size = snprintf (nullptr, 0, fmt, args...);
|
||||
|
||||
Array <char, ReservePolicy::PlusOne> buffer (size + 1);
|
||||
SmallArray <char> buffer (size + 1);
|
||||
snprintf (buffer.data (), size + 1, fmt, cr::forward <Args> (args)...);
|
||||
|
||||
return assign (buffer.data ());
|
||||
|
|
@ -278,7 +278,7 @@ public:
|
|||
}
|
||||
const size_t size = snprintf (nullptr, 0, fmt, args...) + length ();
|
||||
|
||||
Array <char, ReservePolicy::PlusOne> buffer (size + 1);
|
||||
SmallArray <char> buffer (size + 1);
|
||||
snprintf (buffer.data (), size + 1, fmt, cr::forward <Args> (args)...);
|
||||
|
||||
return append (buffer.data ());
|
||||
|
|
|
|||
|
|
@ -50,17 +50,9 @@ public:
|
|||
return a.second < b.second;
|
||||
}
|
||||
|
||||
friend bool operator <= (const Twin &a, const Twin &b) {
|
||||
return a.second <= b.second;
|
||||
}
|
||||
|
||||
friend bool operator > (const Twin &a, const Twin &b) {
|
||||
return b.second < a.second;
|
||||
}
|
||||
|
||||
friend bool operator >= (const Twin &a, const Twin &b) {
|
||||
return b.second <= a.second;
|
||||
}
|
||||
};
|
||||
|
||||
// creating pairs
|
||||
|
|
|
|||
|
|
@ -37,8 +37,8 @@ private:
|
|||
|
||||
|
||||
private:
|
||||
Array <int32, ReservePolicy::PlusOne> m_hashTable;
|
||||
Array <int32, ReservePolicy::PlusOne> m_prevTable;
|
||||
SmallArray <int32> m_hashTable;
|
||||
SmallArray <int32> m_prevTable;
|
||||
|
||||
public:
|
||||
ULZ () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue