addColumn

This commit is contained in:
2026-02-18 20:03:47 +03:00
parent 39c1be6bc0
commit 87a29bc8bc
2 changed files with 222 additions and 16 deletions

View File

@@ -705,6 +705,11 @@ public:
size_t st_, sz_;
public:
inline RowConst(const PIVector2D<T>::Row & r): p_(r.p_) {
st_ = r.st_;
sz_ = r.sz_;
}
//! \~english Size of the row (number of columns).
//! \~russian Размер строки (количество столбцов).
inline size_t size() const { return sz_; }
@@ -875,6 +880,12 @@ public:
size_t step_, col_, sz_;
public:
inline ColConst(const PIVector2D<T>::Col & c): p_(c.p_) {
step_ = c.step_;
col_ = c.col_;
sz_ = c.sz_;
}
//! \~english Size of the column (number of rows).
//! \~russian Размер столбца (количество строк).
inline size_t size() const { return sz_; }
@@ -1130,18 +1141,6 @@ public:
//! \~russian Если массив был пуст, количество столбцов устанавливается равным размеру исходной строки.
//! В противном случае копируется только `min(cols(), other.size())` элементов; остальные элементы новой строки инициализируются по
//! умолчанию. \sa PIVector::push_back()
inline PIVector2D<T> & addRow(const Row & other) {
if (cols_ == 0) cols_ = other.sz_;
const size_t sz = piMin<size_t>(cols_, other.sz_);
const size_t ps = mat.size();
mat.resize(mat.size() + cols_);
mat._copyRaw(mat.data(ps), other.data(), sz);
rows_++;
return *this;
}
//! \~english Appends a new row to the bottom of the array from a read-only RowConst object.
//! \~russian Добавляет новую строку в конец массива из объекта RowConst только для чтения.
inline PIVector2D<T> & addRow(const RowConst & other) {
if (cols_ == 0) cols_ = other.sz_;
const size_t sz = piMin<size_t>(cols_, other.sz_);
@@ -1164,6 +1163,72 @@ public:
return *this;
}
//! \~english Appends a new column to the right of the array from a \a PIVector.
//! \~russian Добавляет новую строку в конец массива из \a PIVector.
inline PIVector2D<T> & addColumn(const ColConst & other) {
if (other.size() == 0) return *this;
if (size() == 0) {
_resizeRaw(other.size(), 1);
for (size_t r = 0; r < other.size(); ++r) {
element(r, 0) = other[r];
}
return *this;
}
const size_t oldCols = cols_;
const size_t newCols = oldCols + 1;
const size_t newSize = rows_ * newCols;
mat._resizeRaw(newSize);
for (size_t r = rows_ - 1; r > 0; --r) {
T * src = mat.data(r * oldCols);
T * dst = mat.data(r * newCols);
memmove(dst, src, oldCols * sizeof(T));
if (r < other.size()) {
dst[oldCols] = other[r];
} else {
dst[oldCols] = T();
}
}
mat[oldCols] = other[0];
cols_ = newCols;
return *this;
}
inline PIVector2D<T> & addColumn(const PIVector<T> & other) {
if (other.size() == 0) return *this;
if (size() == 0) {
_resizeRaw(other.size(), 1);
for (size_t r = 0; r < other.size(); ++r) {
element(r, 0) = other[r];
}
return *this;
}
const size_t oldCols = cols_;
const size_t newCols = oldCols + 1;
const size_t newSize = rows_ * newCols;
mat._resizeRaw(newSize);
for (size_t r = rows_ - 1; r > 0; --r) {
T * src = mat.data(r * oldCols);
T * dst = mat.data(r * newCols);
memmove(dst, src, oldCols * sizeof(T));
if (r < other.size()) {
dst[oldCols] = other[r];
} else {
dst[oldCols] = T();
}
}
mat[oldCols] = other[0];
cols_ = newCols;
return *this;
}
//! \~english Resizes the 2D array to new dimensions.
//! \~russian Изменяет размер двумерного массива.
//! \param rows New number of rows.