добавил const для части контейнеров и explicit для конструкторов

This commit is contained in:
2023-07-03 19:10:36 +03:00
parent 9e78546b7e
commit 3a6b3a4064
10 changed files with 217 additions and 176 deletions

View File

@@ -74,9 +74,9 @@
//! if the number of elements is known beforehand.
//!
//! The complexity (efficiency) of common operations on PIVector is as follows:
//! - Random access - constant 𝓞(1)
//! - Insertion or removal of elements at the end - amortized constant 𝓞(1)
//! - Insertion or removal of elements - linear in the distance to the end of the array 𝓞(n)
//! - Random access - constant O(1)
//! - Insertion or removal of elements at the end - amortized constant O(1)
//! - Insertion or removal of elements - linear in the distance to the end of the array O(n)
//!
//! \~russian
//! Элементы хранятся непрерывно, а значит доступны не только через итераторы,
@@ -109,9 +109,9 @@
//! если количество элементов известно заранее.
//!
//! Сложность (эффективность) обычных операций над PIVector следующая:
//! - Произвольный доступ — постоянная 𝓞(1)
//! - Вставка и удаление элементов в конце — амортизированная постоянная 𝓞(1)
//! - Вставка и удаление элементов — линейная по расстоянию до конца массива 𝓞(n)
//! - Произвольный доступ — постоянная O(1)
//! - Вставка и удаление элементов в конце — амортизированная постоянная O(1)
//! - Вставка и удаление элементов — линейная по расстоянию до конца массива O(n)
//!
//! \~\sa \a PIDeque, \a PIMap
template<typename T>
@@ -164,7 +164,7 @@ public:
//! \~english Contructs array with size `size` filled elements `e`.
//! \~russian Создает массив из `size` элементов заполненных `e`.
inline PIVector(size_t size, const T & e = T()) {
inline explicit PIVector(size_t size, const T & e = T()) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
expand(size, e);
}
@@ -248,7 +248,7 @@ public:
return *this;
}
inline iterator operator++(int) {
auto tmp = *this;
const auto tmp = *this;
++*this;
return tmp;
}
@@ -257,7 +257,7 @@ public:
return *this;
}
inline iterator operator--(int) {
auto tmp = *this;
const auto tmp = *this;
--*this;
return tmp;
}
@@ -327,7 +327,7 @@ public:
return *this;
}
inline const_iterator operator++(int) {
auto tmp = *this;
const auto tmp = *this;
++*this;
return tmp;
}
@@ -336,7 +336,7 @@ public:
return *this;
}
inline const_iterator operator--(int) {
auto tmp = *this;
const auto tmp = *this;
--*this;
return tmp;
}
@@ -408,7 +408,7 @@ public:
return *this;
}
inline reverse_iterator operator++(int) {
auto tmp = *this;
const auto tmp = *this;
--*this;
return tmp;
}
@@ -417,7 +417,7 @@ public:
return *this;
}
inline reverse_iterator operator--(int) {
auto tmp = *this;
const auto tmp = *this;
++*this;
return tmp;
}
@@ -486,7 +486,7 @@ public:
return *this;
}
inline const_reverse_iterator operator++(int) {
auto tmp = *this;
const auto tmp = *this;
--*this;
return tmp;
}
@@ -495,7 +495,7 @@ public:
return *this;
}
inline const_reverse_iterator operator--(int) {
auto tmp = *this;
const auto tmp = *this;
++*this;
return tmp;
}
@@ -725,7 +725,7 @@ public:
//! заданному в передаваемой функции `test`, или `def` если такого элемента нет.
//! \~\sa \a indexWhere()
inline const T & atWhere(std::function<bool(const T & e)> test, ssize_t start = 0, const T & def = T()) const {
ssize_t i = indexWhere(test, start);
const ssize_t i = indexWhere(test, start);
if (i < 0)
return def;
else
@@ -739,7 +739,7 @@ public:
//! заданному в передаваемой функции `test`, или `def` если такого элемента нет.
//! \~\sa \a lastIndexWhere()
inline const T & lastAtWhere(std::function<bool(const T & e)> test, ssize_t start = -1, const T & def = T()) const {
ssize_t i = lastIndexWhere(test, start);
const ssize_t i = lastIndexWhere(test, start);
if (i < 0)
return def;
else
@@ -1286,7 +1286,7 @@ public:
//! \~\sa \a size(), \a capacity(), \a resize()
inline PIVector<T> & reserve(size_t new_size) {
if (new_size <= piv_rsize) return *this;
size_t os = piv_size;
const size_t os = piv_size;
alloc(new_size);
piv_size = os;
return *this;
@@ -1306,8 +1306,8 @@ public:
inline PIVector<T> & insert(size_t index, const T & e = T()) {
alloc(piv_size + 1);
if (index < piv_size - 1) {
size_t os = piv_size - index - 1;
memmove((void *)(piv_data + index + 1), (const void *)(piv_data + index), os * sizeof(T));
const size_t os = piv_size - index - 1;
memmove(reinterpret_cast<void *>(piv_data + index + 1), reinterpret_cast<const void *>(piv_data + index), os * sizeof(T));
}
PIINTROSPECTION_CONTAINER_USED(T, 1)
elementNew(piv_data + index, e);
@@ -1323,8 +1323,8 @@ public:
inline PIVector<T> & insert(size_t index, T && e) {
alloc(piv_size + 1);
if (index < piv_size - 1) {
size_t os = piv_size - index - 1;
memmove((void *)(piv_data + index + 1), (const void *)(piv_data + index), os * sizeof(T));
const size_t os = piv_size - index - 1;
memmove(reinterpret_cast<void *>(piv_data + index + 1), reinterpret_cast<const void *>(piv_data + index), os * sizeof(T));
}
PIINTROSPECTION_CONTAINER_USED(T, 1)
elementNew(piv_data + index, std::move(e));
@@ -1345,10 +1345,12 @@ public:
}
#endif
assert(&v != this);
ssize_t os = piv_size - index;
const ssize_t os = piv_size - index;
alloc(piv_size + v.piv_size);
if (os > 0) {
memmove((void *)(piv_data + index + v.piv_size), (const void *)(piv_data + index), os * sizeof(T));
memmove(reinterpret_cast<void *>(piv_data + index + v.piv_size),
reinterpret_cast<const void *>(piv_data + index),
os * sizeof(T));
}
newT(piv_data + index, v.piv_data, v.piv_size);
return *this;
@@ -1366,10 +1368,12 @@ public:
//! \~\sa \a append(), \a prepend(), \a remove()
inline PIVector<T> & insert(size_t index, std::initializer_list<T> init_list) {
if (init_list.size() == 0) return *this;
ssize_t os = piv_size - index;
const ssize_t os = piv_size - index;
alloc(piv_size + init_list.size());
if (os > 0) {
memmove((void *)(piv_data + index + init_list.size()), (const void *)(piv_data + index), os * sizeof(T));
memmove(reinterpret_cast<void *>(piv_data + index + init_list.size()),
reinterpret_cast<const void *>(piv_data + index),
os * sizeof(T));
}
newT(piv_data + index, init_list.begin(), init_list.size());
return *this;
@@ -1392,9 +1396,9 @@ public:
piv_size = index;
}
} else {
size_t os = piv_size - index - count;
const size_t os = piv_size - index - count;
deleteT(piv_data + index, count);
memmove((void *)(piv_data + index), (const void *)(piv_data + index + count), os * sizeof(T));
memmove(reinterpret_cast<void *>(piv_data + index), reinterpret_cast<const void *>(piv_data + index + count), os * sizeof(T));
piv_size -= count;
}
return *this;
@@ -1483,7 +1487,7 @@ public:
//! \endcode
//! \~\sa \a reversed()
inline PIVector<T> & reverse() {
size_t s2 = piv_size / 2;
const size_t s2 = piv_size / 2;
for (size_t i = 0; i < s2; ++i) {
piSwap<T>(piv_data[i], piv_data[piv_size - i - 1]);
}
@@ -1514,7 +1518,7 @@ public:
//! Если `add_size < 0` и в массиве меньше элементов чем указано, то массив становится пустым.
//! \~\sa \a resize()
inline PIVector<T> & enlarge(ssize_t add_size, const T & e = T()) {
ssize_t ns = size_s() + add_size;
const ssize_t ns = size_s() + add_size;
if (ns <= 0)
clear();
else
@@ -1637,7 +1641,7 @@ public:
//! \~\sa \a push_back()
inline PIVector<T> & push_back(std::initializer_list<T> init_list) {
if (init_list.size() == 0) return *this;
size_t ps = piv_size;
const size_t ps = piv_size;
alloc(piv_size + init_list.size());
newT(piv_data + ps, init_list.begin(), init_list.size());
return *this;
@@ -1657,7 +1661,7 @@ public:
}
#endif
assert(&v != this);
size_t ps = piv_size;
const size_t ps = piv_size;
alloc(piv_size + v.piv_size);
newT(piv_data + ps, v.piv_data, v.piv_size);
return *this;
@@ -1909,7 +1913,7 @@ public:
//! \endcode
//! \~\sa \a take_front(), \a pop_back(), \a pop_front()
inline T take_back() {
T e(back());
const T e(back());
pop_back();
return e;
}
@@ -1924,7 +1928,7 @@ public:
//! \endcode
//! \~\sa \a take_front(), \a pop_back(), \a pop_front()
inline T take_front() {
T e(front());
const T e(front());
pop_front();
return e;
}
@@ -2327,8 +2331,8 @@ public:
inline PIVector<C> flatten(ReshapeOrder order = ReshapeByRow) const {
PIVector<C> ret;
if (isEmpty()) return ret;
size_t rows = size();
size_t cols = at(0).size();
const size_t rows = size();
const size_t cols = at(0).size();
ret.reserve(rows * cols);
if (order == ReshapeByRow) {
for (size_t r = 0; r < rows; r++) {
@@ -2399,11 +2403,11 @@ public:
inline PIVector<PIVector<T>> splitBySize(size_t sz) const {
PIVector<PIVector<T>> ret;
if (isEmpty() || sz == 0) return ret;
size_t ch = piv_size / sz;
const size_t ch = piv_size / sz;
for (size_t i = 0; i < ch; ++i) {
ret << PIVector<T>(piv_data + sz * i, sz);
}
size_t t = ch * sz;
const size_t t = ch * sz;
if (t < piv_size) {
ret << PIVector<T>(piv_data + t, piv_size - t);
}
@@ -2431,10 +2435,10 @@ public:
if (index >= piv_size || count == 0) return ret;
if (index + count > piv_size) count = piv_size - index;
ret.alloc(count);
memcpy((void *)ret.piv_data, (const void *)(piv_data + index), count * sizeof(T));
size_t os = piv_size - index - count;
memcpy(reinterpret_cast<void *>(ret.piv_data), reinterpret_cast<const void *>(piv_data + index), count * sizeof(T));
const size_t os = piv_size - index - count;
if (os > 0) {
memmove((void *)(piv_data + index), (const void *)(piv_data + index + count), os * sizeof(T));
memmove(reinterpret_cast<void *>(piv_data + index), reinterpret_cast<const void *>(piv_data + index + count), os * sizeof(T));
piv_size -= count;
} else {
piv_size = index;
@@ -2454,8 +2458,9 @@ private:
if (piv_rsize * 2 >= s && piv_rsize < s) {
return piv_rsize * 2;
}
ssize_t t = _PIContainerConstants<T>::minCountPoT(), s_ = s - 1;
while (s_ >> t)
ssize_t t = _PIContainerConstants<T>::minCountPoT();
s -= 1;
while (s >> t)
++t;
return (1 << t);
}
@@ -2471,7 +2476,7 @@ private:
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0>
inline void newT(T * dst, const T * src, size_t s) {
PIINTROSPECTION_CONTAINER_USED(T, s)
memcpy((void *)(dst), (const void *)(src), s * sizeof(T));
memcpy(reinterpret_cast<void *>(dst), reinterpret_cast<const void *>(src), s * sizeof(T));
}
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable<T1>::value, int>::type = 0>
@@ -2519,13 +2524,13 @@ private:
inline void dealloc() {
if (piv_data != nullptr) {
free((void *)piv_data);
free(reinterpret_cast<void *>(piv_data));
piv_data = nullptr;
}
}
inline void expand(size_t new_size, const T & e = T()) {
size_t os = piv_size;
const size_t os = piv_size;
alloc(new_size);
PIINTROSPECTION_CONTAINER_USED(T, (new_size - os))
for (size_t i = os; i < new_size; ++i) {
@@ -2534,7 +2539,7 @@ private:
}
inline void expand(size_t new_size, std::function<T(size_t i)> f) {
size_t os = piv_size;
const size_t os = piv_size;
alloc(new_size);
PIINTROSPECTION_CONTAINER_USED(T, (new_size - os))
for (size_t i = os; i < new_size; ++i) {
@@ -2547,11 +2552,11 @@ private:
piv_size = new_size;
return;
}
piv_size = new_size;
size_t as = asize(new_size);
piv_size = new_size;
const size_t as = asize(new_size);
if (as == piv_rsize) return;
PIINTROSPECTION_CONTAINER_ALLOC(T, (as - piv_rsize))
T * p_d = (T *)(realloc((void *)(piv_data), as * sizeof(T)));
T * p_d = reinterpret_cast<T *>(realloc(reinterpret_cast<void *>(piv_data), as * sizeof(T)));
#ifndef NDEBUG
if (!p_d) {
printf("error with PIVector<%s>::alloc\n", __PIP_TYPENAME__(T));