11 Commits

Author SHA1 Message Date
a581d9bf9d fix build and test commands 2026-02-26 19:49:42 +03:00
35772fc2d1 plan for pivector2d 2026-02-26 19:43:13 +03:00
6041a72f30 AGENTS.md 2026-02-26 19:42:50 +03:00
98d93865f0 review fixes 2026-02-18 22:00:21 +03:00
d8cadce026 code style 2026-02-18 20:26:38 +03:00
87a29bc8bc addColumn 2026-02-18 20:03:47 +03:00
39c1be6bc0 fix filterElements 2026-02-18 20:03:27 +03:00
52c400915d fix doc 2026-02-18 17:09:26 +03:00
95d3c9c658 add more comments 2026-02-18 14:59:08 +03:00
7f894e77bb add more tests 2026-02-18 13:46:09 +03:00
97c14870c0 Add col and row funcs an tests 2026-02-17 22:27:25 +03:00
6 changed files with 1364 additions and 111 deletions

162
AGENTS.md Normal file
View File

@@ -0,0 +1,162 @@
# AGENTS.md - Agent Guidelines for PIP
This file provides guidance for agentic coding agents working on the PIP (Platform Independent Primitives) codebase.
## Project Overview
PIP is a C++ cross-platform library providing platform-independent abstractions for:
- Core/Types: Strings, variants, containers, datetime, networks
- Threading: Mutexes, semaphores, thread pools, timers
- I/O: Files, serial, CAN, GPIO, SPI, Ethernet
- Math: Vectors, matrices, FFT, quaternions
- Crypto: MD5, SHA, BLAKE2, SipHash
- Compression: zlib support
- HTTP: Client and server support
- Serialization: JSON, binary, XML
## Build Commands
### Basic Build
```bash
cmake -B build
cmake --build build
```
### Build with Tests
```bash
cmake -B build -DTESTS=ON
cmake --build build
```
### Run Tests
```bash
ctest --test-dir build/tests # Run all tests
ctest --test-dir build/tests -R <regex> # Run specific tests matching regex
ctest --test-dir build/tests -V # Verbose output
```
To run a single test:
```bash
# Build the test executable, then run directly:
./build/tests/pip_<test_name>_test
# Or use ctest with specific test name
ctest --test-dir build/tests -R "TestName"
```
### Other Commands
```bash
cmake --build build --target clean # Clean build
cmake --install build_pip # Install
cmake --build build --target doc # Build documentation
```
### Build Options
```bash
-DTESTS=ON # Build tests
-DCOVERAGE=ON # Build with coverage
-DICU=ON # ICU support for codepage conversion
-DSTD_IOSTREAM=ON # std::iostream operators support
-DINTROSPECTION=ON # Build with introspection
-DPIP_BUILD_CRYPT=ON # Crypt module (requires libsodium)
-DPIP_BUILD_FFTW=ON # FFT support
```
## Code Style Guidelines
### File Organization
- Header files: `libs/main/**/*.h`
- Source files: `libs/main/**/*.cpp`
- Private headers: `*_p.h`
- Tests: `tests/<module>/`
- Use Doxygen comments (`/*! ... */`) for documentation with `\brief`, `\param`, `\return`
### Naming Conventions
- **Classes**: PascalCase with `PI` prefix (e.g., `PIString`, `PIByteArray`, `PIVariant`)
- **Functions**: camelCase (e.g., `toAscii()`, `isEmpty()`, `append()`)
- **Member variables**: snake_case, or just lowercase (e.g., `array_size`, `count`)
- **Constants**: PascalCase or UPPER_SNAKE_CASE
- **Enums**: PascalCase for enum names and values
### Code Formatting
- **Indentation**: Use tabs (4 spaces equivalent) or spaces - match existing code
- **Braces**: Opening brace on same line for functions, new line for namespaces/classes
- **Includes**: System includes first, then project headers
- **Use forward declarations** where possible to reduce compile times
- **Use `nullptr`** instead of `NULL`
### C++ Standards
- C++11 standard (enforced in CMakeLists.txt)
- Use `override` keyword for virtual function overrides
- Use `explicit` for single-argument constructors
- Use `const` member functions where applicable
- Use range-based `for` loops when possible
### Error Handling
- Return error codes
- DO NOT USE exceptions
- Use `piCerr` and `piCout` for error messages
- Check for null pointers where appropriate
### Module Structure
Each module typically has:
1. Public header: `pip_<module>.h` or `<classname>.h`
2. Private implementation: `<classname>.cpp` or `<classname>_p.cpp`
3. Use `PIP_EXPORT` macro for symbols that need to be exported from the library
Example class structure:
```cpp
// header.h
#ifndef MODULE_CLASSNAME_H
#define MODULE_CLASSNAME_H
#include "pip_export.h"
class PIP_EXPORT ClassName {
public:
ClassName();
~ClassName();
void doSomething();
bool isValid() const;
private:
void privateMethod();
int data;
};
#endif // MODULE_CLASSNAME_H
// source.cpp
#include "header.h"
#include "piincludes_p.h"
ClassName::ClassName() : data(0) {}
```
### Testing
- Use Google Test framework
- Test files go in `tests/<module>/`
- Use `TEST(TestSuite, TestName)` or `TEST_F(TestFixture, TestName)` for test cases
- Use `ASSERT_*` for fatal failures, `EXPECT_*` for non-fatal
- Test naming: `<ClassName>_<TestDescription>`
Example:
```cpp
#include "pistring.h"
#include "gtest/gtest.h"
TEST(PIString_Tests, constructor_empty) {
PIString str;
ASSERT_TRUE(str.isEmpty());
}
```
### Module Dependencies
- Main library: `libs/main/`
- Third-party: `3rd/`
- Utils: `utils/`
### Key Files
- `CMakeLists.txt` - Main build configuration
- `tests/CMakeLists.txt` - Test configuration

View File

@@ -835,8 +835,8 @@ public:
//! piCout << v.contains({1,4}); // true
//! piCout << v.contains({1,5}); // false
//! \endcode
//! \~\sa \a every(), \a any(), \a entries(), \a forEach()
inline bool contains(const PIVector<T> & v, ssize_t start = 0) const {
//! \~\sa \a every(), \a any(), \a entries(), \a forEach(), \a contains()
inline bool containsAll(const PIVector<T> & v, ssize_t start = 0) const {
if (start < 0) {
start = piv_size + start;
if (start < 0) start = 0;
@@ -854,6 +854,24 @@ public:
return true;
}
//! \~english Tests if any element of `v` exists in the array.
//! \~russian Проверяет наличие хотя бы одного из элементов `v` в массиве.
//! \~\sa \a containsAll(), \a contains()
inline bool containsAny(const PIVector<T> & v, ssize_t start = 0) const {
if (start < 0) {
start = piv_size + start;
if (start < 0) start = 0;
}
for (const T & e: v) {
for (size_t i = start; i < piv_size; ++i) {
if (e == piv_data[i]) {
return true;
}
}
}
return false;
}
//! \~english Count elements equal `e` in the array.
//! \~russian Подсчитывает количество элементов, совпадающих с элементом `e` в массиве.
//! \~\details
@@ -1349,8 +1367,8 @@ public:
alloc(piv_size + v.piv_size);
if (os > 0) {
memmove(reinterpret_cast<void *>(piv_data + index + v.piv_size),
reinterpret_cast<const void *>(piv_data + index),
os * sizeof(T));
reinterpret_cast<const void *>(piv_data + index),
os * sizeof(T));
}
newT(piv_data + index, v.piv_data, v.piv_size);
return *this;
@@ -1372,8 +1390,8 @@ public:
alloc(piv_size + init_list.size());
if (os > 0) {
memmove(reinterpret_cast<void *>(piv_data + index + init_list.size()),
reinterpret_cast<const void *>(piv_data + index),
os * sizeof(T));
reinterpret_cast<const void *>(piv_data + index),
os * sizeof(T));
}
newT(piv_data + index, init_list.begin(), init_list.size());
return *this;

View File

@@ -43,7 +43,7 @@
//! (use \a resize(), \a addRow(), \a removeRow(), \a removeColumn() instead), but you can modify the values of existing elements.
//! \~russian
//! Этот класс используется для хранения двумерного массива элементов любого типа в виде единого непрерывного блока памяти (обычного
//! PIVector). Доступ к элементам осуществляется с помощью операторов `[][]`, где первый индекс — это строка, а второй — столбец. Со
//! \a PIVector). Доступ к элементам осуществляется с помощью операторов `[][]`, где первый индекс — это строка, а второй — столбец. Со
//! строками можно работать как с объектами \a PIVector, что позволяет изменять отдельные элементы или присваивать целые строки. Нельзя
//! напрямую добавлять или удалять элементы, чтобы изменить размеры массива после создания (используйте \a resize(), \a addRow(), \a
//! removeRow(), \a removeColumn() для этого), но можно изменять значения существующих элементов.
@@ -52,21 +52,22 @@
template<typename T>
class PIVector2D {
public:
//! \~english Constructs an empty 2D array.
//! \~russian Создает пустой двумерный массив.
//! \~english Constructs an empty 2D array. No memory is allocated.
//! \~russian Создаёт пустой двумерный массив. Память не выделяется.
//! \details
//! \~english After this constructor, \a rows() and \a cols() return 0, and \a isEmpty() returns true.
//! \~russian После этого конструктора \a rows() и \a cols() возвращают 0, а \a isEmpty() возвращает true.
//! \sa PIVector::PIVector()
inline PIVector2D() { rows_ = cols_ = 0; }
//! \~english Constructs a 2D array with the given dimensions, filled with copies of `f`.
//! \~russian Создает двумерный массив заданного размера, заполненный копиями `f`.
//! \param rows Number of rows.
//! \param cols Number of columns.
//! \param f Value to fill the array with.
//! \~english \param rows Количество строк.
//! \~russian \param rows Количество строк.
//! \~english \param cols Количество столбцов.
//! \~russian \param cols Количество столбцов.
//! \~english \param f Значение для заполнения массива.
//! \~russian \param f Значение для заполнения массива.
//! \~russian Создаёт двумерный массив заданного размера, заполненный копиями `f`.
//! \details
//! \~english The underlying storage is a single contiguous block of memory of size `rows * cols`.
//! All elements are initialized with the value `f`.
//! \~russian Внутреннее хранилище представляет собой единый непрерывный блок памяти размером `rows * cols`.
//! Все элементы инициализируются значением `f`.
//! \sa PIVector::PIVector(size_t, const T&)
inline PIVector2D(size_t rows, size_t cols, const T & f = T()) {
rows_ = rows;
cols_ = cols;
@@ -74,22 +75,35 @@ public:
}
//! \~english Constructs a 2D array from an existing 1D vector, reshaping it.
//! \~russian Создает двумерный массив из существующего одномерного вектора, изменяя его форму.
//! \param rows Number of rows.
//! \param cols Number of columns.
//! \param v The source 1D vector. Its size must be at least `rows * cols`.
//! \~russian Создаёт двумерный массив из существующего одномерного вектора, изменяя его форму.
//! \details
//! \~english The constructor copies the data from `v` into the internal flat vector.
//! If `v` is larger than `rows * cols`, the excess elements are ignored (the vector is truncated).
//! If `v` is smaller, other values filled whith default cunstructor T()
//! \~russian Конструктор копирует данные из `v` во внутренний плоский вектор.
//! Если `v` больше, чем `rows * cols`, лишние элементы игнорируются (вектор обрезается).
//! Если `v` меньше, остальные значения будут заполнены из конструктора по умолчанию T()
//! \sa PIVector::PIVector(const PIVector&), reshape()
inline PIVector2D(size_t rows, size_t cols, const PIVector<T> & v): rows_(rows), cols_(cols), mat(v) { mat.resize(rows * cols); }
//! \~english Move constructs a 2D array from an existing 1D vector, reshaping it.
//! \~russian Конструктор перемещения из существующего одномерного вектора, изменяя его форму.
//! \param rows Number of rows.
//! \param cols Number of columns.
//! \param v The source 1D vector (rvalue reference). Its size must be at least `rows * cols`.
//! \details
//! \~english The data is moved from `v` into the internal flat vector, avoiding a copy.
//! After construction, `v` is left in a valid but unspecified state.
//! \~russian Данные перемещаются из `v` во внутренний плоский вектор, что позволяет избежать копирования.
//! После завершения конструктора `v` остаётся в корректном, но неопределённом состоянии.
//! \sa PIVector::PIVector(PIVector&&)
inline PIVector2D(size_t rows, size_t cols, PIVector<T> && v): rows_(rows), cols_(cols), mat(std::move(v)) { mat.resize(rows * cols); }
//! \~english Constructs a 2D array from a vector of vectors (jagged array). Assumes all inner vectors have the same size.
//! \~russian Создает двумерный массив из вектора векторов (рваного массива). Предполагается, что все внутренние векторы имеют
//! одинаковый размер. \param v The source vector of vectors.
//! \~russian Создаёт двумерный массив из вектора векторов (рваного массива). Предполагается, что все внутренние векторы имеют
//! одинаковый размер.
//! \details
//! \~english If the input is empty, the constructed array is also empty. Otherwise, the number of columns is taken from the size of the
//! first inner vector. All inner vectors are concatenated in the internal flat storage.
//! \~russian Если входной массив пуст, создаётся пустой двумерный массив. В противном случае количество столбцов берётся из размера
//! первого внутреннего вектора. Все внутренние векторы конкатенируются во внутреннем плоском хранилище. \sa PIVector::append()
inline PIVector2D(const PIVector<PIVector<T>> & v) {
rows_ = v.size();
if (rows_) {
@@ -103,36 +117,67 @@ public:
if (mat.isEmpty()) rows_ = cols_ = 0;
}
//! \~english Number of rows.
//! \~russian Количество строк.
//! \~english Returns the number of rows in the 2D array.
//! \~russian Возвращает количество строк в двумерном массиве.
//! \return Number of rows.
//! \details
//! \~english The result is always non-negative. If the array is empty, returns 0.
//! \~russian Результат всегда неотрицательный. Если массив пуст, возвращает 0.
//! \sa cols(), size(), PIVector::size()
inline size_t rows() const { return rows_; }
//! \~english Number of columns.
//! \~russian Количество столбцов.
//! \~english Returns the number of columns in the 2D array.
//! \~russian Возвращает количество столбцов в двумерном массиве.
//! \return Number of columns.
//! \details
//! \~english The result is always non-negative. If the array is empty, returns 0.
//! \~russian Результат всегда неотрицательный. Если массив пуст, возвращает 0.
//! \sa rows(), size(), PIVector::size()
inline size_t cols() const { return cols_; }
//! \~english Total number of elements (`rows * cols`).
//! \~russian Общее количество элементов (`строки * столбцы`).
//! \~english Returns the total number of elements (`rows * cols`).
//! \~russian Возвращает общее количество элементов (`строки * столбцы`).
//! \return Total number of elements.
//! \details
//! \~english This is equivalent to the size of the underlying flat vector.
//! \~russian Это эквивалентно размеру внутреннего плоского вектора.
//! \sa rows(), cols(), PIVector::size()
inline size_t size() const { return mat.size(); }
//! \~english Total number of elements as signed value.
//! \~russian Общее количество элементов в виде знакового числа.
//! \~english Returns the total number of elements as a signed value.
//! \~russian Возвращает общее количество элементов в виде знакового числа.
//! \return Signed size.
//! \sa size(), PIVector::size_s()
inline ssize_t size_s() const { return mat.size_s(); }
//! \~english Total number of elements.
//! \~russian Общее количество элементов.
//! \~english Returns the total number of elements (same as \a size()).
//! \~russian Возвращает общее количество элементов (то же, что и \a size()).
//! \return Total number of elements.
//! \sa size(), PIVector::length()
inline size_t length() const { return mat.length(); }
//! \~english Number of elements that the underlying container has currently allocated space for.
//! \~russian Количество элементов, для которого сейчас выделена память во внутреннем контейнере.
//! \~english Returns the number of elements that the underlying container has currently allocated space for.
//! \~russian Возвращает количество элементов, для которого сейчас выделена память во внутреннем контейнере.
//! \return Capacity of the flat vector.
//! \details
//! \~english This value may be larger than \a size(). It indicates how many elements can be added before a reallocation is needed.
//! \~russian Это значение может быть больше, чем \a size(). Оно показывает, сколько элементов можно добавить до того, как потребуется
//! перераспределение памяти. \sa reserve(), PIVector::capacity()
inline size_t capacity() const { return mat.capacity(); }
//! \~english Checks if the array has no elements.
//! \~russian Проверяет, пуст ли массив.
//! \return \c true if the array is empty, \c false otherwise.
//! \details
//! \~english An empty array has both rows and columns equal to 0.
//! \~russian Пустой массив имеет и строки, и столбцы равные 0.
//! \sa isNotEmpty(), PIVector::isEmpty()
inline bool isEmpty() const { return mat.isEmpty(); }
//! \~english Checks if the array has elements.
//! \~english Checks if the array has at least one element.
//! \~russian Проверяет, не пуст ли массив.
//! \return \c true if the array is not empty, \c false otherwise.
//! \sa isEmpty(), PIVector::isNotEmpty()
inline bool isNotEmpty() const { return mat.isNotEmpty(); }
@@ -140,6 +185,13 @@ public:
//! \brief
//! \~english Proxy class representing a single row in a \a PIVector2D for modification.
//! \~russian Прокси-класс, представляющий одну строку в \a PIVector2D для модификации.
//! \details
//! \~english Objects of this class are returned by non-const \a operator[] or \a row().
//! They provide array-like access to the elements of a specific row and allow operations such as assignment from another row or a \a
//! PIVector, searching, filling, and iteration.
//! \~russian Объекты этого класса возвращаются неконстантными операторами \a operator[] или методом \a row().
//! Они предоставляют доступ к элементам конкретной строки, подобный массиву, и позволяют выполнять такие операции, как присваивание из
//! другой строки или \a PIVector, поиск, заполнение и итерацию. \sa Col, RowConst
class Row {
friend class PIVector2D<T>;
@@ -152,28 +204,45 @@ public:
size_t st_, sz_;
public:
//! \~english Size of the row (number of columns).
//! \~russian Размер строки (количество столбцов).
//! \~english Returns the number of columns in this row.
//! \~russian Возвращает количество столбцов в этой строке.
//! \return Row size (number of columns).
//! \sa PIVector::size()
inline size_t size() const { return sz_; }
//! \~english Accesses the element at the given column index within the row.
//! \~russian Доступ к элементу по заданному индексу столбца в строке.
//! \details
//! \~english No bounds checking is performed in release builds; use with caution.
//! \~russian В релизной сборке проверка границ не выполняется; используйте с осторожностью.
//! \sa PIVector::operator[]
inline T & operator[](size_t index) { return (*p_)[st_ + index]; }
//! \~english Const access to the element at the given column index within the row.
//! \~russian Константный доступ к элементу по заданному индексу столбца в строке.
//! \sa operator[] (non-const)
inline const T & operator[](size_t index) const { return (*p_)[st_ + index]; }
//! \~english Returns a pointer to the row data starting at an optional offset.
//! \~russian Возвращает указатель на данные строки, начиная с опционального смещения.
//! \details
//! \~english The pointer can be used for direct memory operations. It remains valid as long as the underlying 2D array is not
//! reallocated.
//! \~russian Указатель можно использовать для прямых операций с памятью. Он остаётся действительным, пока не произойдёт
//! перераспределение памяти внутреннего двумерного массива. \sa PIVector::data()
inline T * data(size_t index = 0) { return p_->data(st_ + index); }
//! \~english Returns a const pointer to the row data starting at an optional offset.
//! \~russian Возвращает константный указатель на данные строки, начиная с опционального смещения.
//! \sa data() (non-const)
inline const T * data(size_t index = 0) const { return p_->data(st_ + index); }
//! \~english Assigns the contents of another Row to this row.
//! \~russian Присваивает этой строке содержимое другой строки.
//! \details
//! \~english Only the minimum of the two row sizes is copied; if this row is shorter, excess elements in `other` are ignored.
//! \~russian Копируется только минимум из размеров двух строк; если эта строка короче, лишние элементы из `other` игнорируются.
//! \sa PIVector::operator=
inline Row & operator=(const Row & other) {
if (p_ == other.p_ && st_ == other.st_) return *this;
const size_t sz = piMin<size_t>(sz_, other.sz_);
@@ -183,6 +252,10 @@ public:
//! \~english Assigns the contents of a \a PIVector to this row.
//! \~russian Присваивает этой строке содержимое \a PIVector.
//! \details
//! \~english Only the minimum of the row size and vector size is copied.
//! \~russian Копируется только минимум из размера строки и размера вектора.
//! \sa PIVector::operator=
inline Row & operator=(const PIVector<T> & other) {
const size_t sz = piMin<size_t>(sz_, other.size());
p_->_copyRaw(p_->data(st_), other.data(), sz);
@@ -191,9 +264,15 @@ public:
//! \~english Converts the row to a \a PIVector.
//! \~russian Преобразует строку в \a PIVector.
//! \sa PIVector::PIVector(const T*, size_t)
inline PIVector<T> toVector() const { return PIVector<T>(p_->data(st_), sz_); }
// --- Поиск в строке ---
//! \~english Returns the first index of element `e` in the row, starting from `start`.
//! \~russian Возвращает первый индекс элемента `e` в строке, начиная с позиции `start`.
//! \details
//! \~english See \a PIVector::indexOf() for details on negative start handling.
//! \~russian Подробнее об обработке отрицательного `start` см. \a PIVector::indexOf().
//! \sa PIVector::indexOf()
inline ssize_t indexOf(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
@@ -201,6 +280,10 @@ public:
}
return -1;
}
//! \~english Returns the last index of element `e` in the row, searching backwards from `start`.
//! \~russian Возвращает последний индекс элемента `e` в строке, выполняя поиск в обратном направлении от `start`.
//! \sa PIVector::lastIndexOf()
inline ssize_t lastIndexOf(const T & e, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
@@ -208,6 +291,10 @@ public:
}
return -1;
}
//! \~english Returns the first index where the predicate `test` returns true, starting from `start`.
//! \~russian Возвращает первый индекс, для которого предикат `test` возвращает true, начиная с `start`.
//! \sa PIVector::indexWhere()
inline ssize_t indexWhere(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
@@ -215,6 +302,11 @@ public:
}
return -1;
}
//! \~english Returns the last index where the predicate `test` returns true, searching backwards from `start`.
//! \~russian Возвращает последний индекс, для которого предикат `test` возвращает true,
//! выполняя поиск в обратном направлении от `start`.
//! \sa PIVector::lastIndexWhere()
inline ssize_t lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
@@ -222,12 +314,100 @@ public:
}
return -1;
}
//! \~english Applies a function to each element of the row (modifiable).
//! \~russian Применяет функцию к каждому элементу строки (с возможностью изменения).
//! \param func Function that takes a reference to T.
//! \details
//! \~english The function can modify the elements.
//! \~russian Функция может изменять элементы.
//! \sa PIVector::forEach()
inline void forEach(std::function<void(T &)> func) {
for (size_t i = 0; i < sz_; ++i) {
func((*p_)[st_ + i]);
}
}
//! \~english Applies a function to each element of the row (read-only).
//! \~russian Применяет функцию к каждому элементу строки (только чтение).
//! \details
//! \~english The function can't modify the elements.
//! \~russian Функция не может изменять элементы.
//! \sa forEach (modifiable)
inline void forEach(std::function<void(const T &)> func) const {
for (size_t i = 0; i < sz_; ++i) {
func((*p_)[st_ + i]);
}
}
//! \~english Fills the row with copies of `value`.
//! \~russian Заполняет строку копиями `value`.
//! \sa PIVector::fill()
inline void fill(const T & value) {
for (size_t i = 0; i < sz_; ++i) {
(*p_)[st_ + i] = value;
}
}
//! \~english Checks if the row contains the element `e`.
//! \~russian Проверяет, содержит ли строка элемент `e`.
//! \sa PIVector::contains()
inline bool contains(const T & e, ssize_t start = 0) const { return indexOf(e, start) != -1; }
//! \~english Counts occurrences of `e` in the row.
//! \~russian Подсчитывает количество вхождений `e` в строке.
//! \sa PIVector::entries()
inline int entries(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if ((*p_)[st_ + i] == e) ++count;
}
return count;
}
//! \~english Counts elements in the row that pass the `test`.
//! \~russian Подсчитывает элементы в строке, проходящие `test`.
//! \sa PIVector::entries(std::function)
inline int entries(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if (test((*p_)[st_ + i])) ++count;
}
return count;
}
//! \~english Tests if any element in the row passes the `test`.
//! \~russian Проверяет, проходит ли какой-либо элемент в строке `test`.
//! \sa PIVector::any()
inline bool any(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (test((*p_)[st_ + i])) return true;
}
return false;
}
//! \~english Tests if all elements in the row pass the `test`.
//! \~russian Проверяет, проходят ли все элементы в строке `test`.
//! \sa PIVector::every()
inline bool every(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (!test((*p_)[st_ + i])) return false;
}
return true;
}
};
//! \class Col
//! \brief
//! \~english Proxy class representing a single column in a \a PIVector2D for modification.
//! \~russian Прокси-класс, представляющий один столбец в \a PIVector2D для модификации.
//! \details
//! \~english Objects of this class are returned by non-const \a col(). They provide column-wise access and operations similar to \a
//! Row.
//! \~russian Объекты этого класса возвращаются неконстантным методом \a col(). Они предоставляют доступ к столбцам и операции,
//! аналогичные \a Row. \sa Row, ColConst
class Col {
friend class PIVector2D<T>;
@@ -241,12 +421,13 @@ public:
size_t step_, col_, sz_;
public:
//! \~english Size of the column (number of rows).
//! \~russian Размер столбца (количество строк).
//! \~english Returns the size of the column (number of rows).
//! \~russian Возвращает размер столбца (количество строк).
inline size_t size() const { return sz_; }
//! \~english Accesses the element at the given row index within the column.
//! \~russian Доступ к элементу по заданному индексу строки в столбце.
//! \return Reference to the element.
inline T & operator[](size_t index) { return (*p_)[index * step_ + col_]; }
//! \~english Const access to the element at the given row index within the column.
@@ -255,6 +436,11 @@ public:
//! \~english Returns a pointer to the column data starting at an optional row offset.
//! \~russian Возвращает указатель на данные столбца, начиная с опционального смещения по строкам.
//! \details
//! \~english Note that column elements are not stored contiguously in memory, so this pointer cannot be used to iterate over the
//! whole column.
//! \~russian Обратите внимание, что элементы столбца не хранятся в памяти непрерывно, поэтому этот указатель нельзя использовать
//! для итерации по всему столбцу.
inline T * data(size_t index = 0) { return p_->data(index * step_ + col_); }
//! \~english Returns a const pointer to the column data starting at an optional row offset.
@@ -290,7 +476,12 @@ public:
return ret;
}
// --- Поиск в столбце ---
//! \~english Returns the first index of element `e` in the row, starting from `start`.
//! \~russian Возвращает первый индекс элемента `e` в строке, начиная с позиции `start`.
//! \details
//! \~english See \a PIVector::indexOf() for details on negative start handling.
//! \~russian Подробнее об обработке отрицательного `start` см. \a PIVector::indexOf().
//! \sa PIVector::indexOf()
inline ssize_t indexOf(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
@@ -298,6 +489,10 @@ public:
}
return -1;
}
//! \~english Returns the last index of element `e` in the row, searching backwards from `start`.
//! \~russian Возвращает последний индекс элемента `e` в строке, выполняя поиск в обратном направлении от `start`.
//! \sa PIVector::lastIndexOf()
inline ssize_t lastIndexOf(const T & e, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
@@ -305,6 +500,10 @@ public:
}
return -1;
}
//! \~english Returns the first index where the predicate `test` returns true, starting from `start`.
//! \~russian Возвращает первый индекс, для которого предикат `test` возвращает true, начиная с `start`.
//! \sa PIVector::indexWhere()
inline ssize_t indexWhere(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
@@ -312,6 +511,11 @@ public:
}
return -1;
}
//! \~english Returns the last index where the predicate `test` returns true, searching backwards from `start`.
//! \~russian Возвращает последний индекс, для которого предикат `test` возвращает true,
//! выполняя поиск в обратном направлении от `start`.
//! \sa PIVector::lastIndexWhere()
inline ssize_t lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
@@ -319,12 +523,98 @@ public:
}
return -1;
}
//! \~english Applies a function to each element of the column (modifiable).
//! \~russian Применяет функцию к каждому элементу столбца (с возможностью изменения).
//! \details
//! \~english The function can modify the elements.
//! \~russian Функция может изменять элементы.
//! \sa PIVector::forEach()
inline void forEach(std::function<void(T &)> func) {
for (size_t i = 0; i < sz_; ++i) {
func((*p_)[i * step_ + col_]);
}
}
//! \~english Applies a function to each element of the column (read-only).
//! \~russian Применяет функцию к каждому элементу столбца (только чтение).
//! \details
//! \~english The function can't modify the elements.
//! \~russian Функция не может изменять элементы.
//! \sa forEach (modifiable)
inline void forEach(std::function<void(const T &)> func) const {
for (size_t i = 0; i < sz_; ++i) {
func((*p_)[i * step_ + col_]);
}
}
//! \~english Fills the column with copies of `value`.
//! \~russian Заполняет столбец копиями `value`.
//! \sa PIVector::fill()
inline void fill(const T & value) {
for (size_t i = 0; i < sz_; ++i) {
(*p_)[i * step_ + col_] = value;
}
}
//! \~english Checks if the column contains the element `e`.
//! \~russian Проверяет, содержит ли столбец элемент `e`.
//! \sa PIVector::contains()
inline bool contains(const T & e, ssize_t start = 0) const { return indexOf(e, start) != -1; }
//! \~english Counts occurrences of `e` in the column.
//! \~russian Подсчитывает количество вхождений `e` в столбце.
//! \sa PIVector::entries()
inline int entries(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if ((*p_)[i * step_ + col_] == e) ++count;
}
return count;
}
//! \~english Counts elements in the column that pass the `test`.
//! \~russian Подсчитывает элементы в столбце, проходящие `test`.
//! \sa PIVector::entries(std::function)
inline int entries(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if (test((*p_)[i * step_ + col_])) ++count;
}
return count;
}
//! \~english Tests if any element in the column passes the `test`.
//! \~russian Проверяет, проходит ли какой-либо элемент в столбце `test`.
//! \sa PIVector::any()
inline bool any(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (test((*p_)[i * step_ + col_])) return true;
}
return false;
}
//! \~english Tests if all elements in the column pass the `test`.
//! \~russian Проверяет, проходят ли все элементы в столбце `test`.
//! \sa PIVector::every()
inline bool every(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (!test((*p_)[i * step_ + col_])) return false;
}
return true;
}
};
//! \class RowConst
//! \brief
//! \~english Proxy class representing a single read-only row in a \a PIVector2D.
//! \~russian Прокси-класс, представляющий одну строку в \a PIVector2D только для чтения.
//! \details
//! \~english Returned by const \a operator[] or \a row(). Provides const access to row elements.
//! \~russian Возвращается константными версиями \a operator[] или \a row(). Предоставляет константный доступ к элементам строки.
//! \sa Row, ColConst
class RowConst {
friend class PIVector2D<T>;
@@ -337,6 +627,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_; }
@@ -353,7 +648,12 @@ public:
//! \~russian Преобразует строку в \a PIVector.
inline PIVector<T> toVector() const { return PIVector<T>(p_->data(st_), sz_); }
// --- Поиск в строке (только чтение) ---
//! \~english Returns the first index of element `e` in the row, starting from `start`.
//! \~russian Возвращает первый индекс элемента `e` в строке, начиная с позиции `start`.
//! \details
//! \~english See \a PIVector::indexOf() for details on negative start handling.
//! \~russian Подробнее об обработке отрицательного `start` см. \a PIVector::indexOf().
//! \sa PIVector::indexOf()
inline ssize_t indexOf(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
@@ -361,6 +661,11 @@ public:
}
return -1;
}
//! \~english Returns the last index of element `e` in the row, searching backwards from `start`.
//! \~russian Возвращает последний индекс элемента `e` в строке, выполняя поиск в обратном направлении от `start`.
//! \return Index if found, -1 otherwise.
//! \sa PIVector::lastIndexOf()
inline ssize_t lastIndexOf(const T & e, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
@@ -368,6 +673,10 @@ public:
}
return -1;
}
//! \~english Returns the first index where the predicate `test` returns true, starting from `start`.
//! \~russian Возвращает первый индекс, для которого предикат `test` возвращает true, начиная с `start`.
//! \sa PIVector::indexWhere()
inline ssize_t indexWhere(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
@@ -375,6 +684,11 @@ public:
}
return -1;
}
//! \~english Returns the last index where the predicate `test` returns true, searching backwards from `start`.
//! \~russian Возвращает последний индекс, для которого предикат `test` возвращает true,
//! выполняя поиск в обратном направлении от `start`.
//! \sa PIVector::lastIndexWhere()
inline ssize_t lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
@@ -382,12 +696,77 @@ public:
}
return -1;
}
//! \~english Applies a function to each element of the row (read-only).
//! \~russian Применяет функцию к каждому элементу строки (только чтение).
//! \details
//! \~english The function can't modify the elements.
//! \~russian Функция не может изменять элементы.
//! \sa forEach (modifiable)
inline void forEach(std::function<void(const T &)> func) const {
for (size_t i = 0; i < sz_; ++i) {
func((*p_)[st_ + i]);
}
}
//! \~english Checks if the row contains the element `e`.
//! \~russian Проверяет, содержит ли строка элемент `e`.
//! \sa PIVector::contains()
inline bool contains(const T & e, ssize_t start = 0) const { return indexOf(e, start) != -1; }
//! \~english Counts occurrences of `e` in the row.
//! \~russian Подсчитывает количество вхождений `e` в строке.
//! \sa PIVector::entries()
inline int entries(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if ((*p_)[st_ + i] == e) ++count;
}
return count;
}
//! \~english Counts elements in the row that pass the `test`.
//! \~russian Подсчитывает элементы в строке, проходящие `test`.
//! \sa PIVector::entries(std::function)
inline int entries(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if (test((*p_)[st_ + i])) ++count;
}
return count;
}
//! \~english Tests if any element in the row passes the `test`.
//! \~russian Проверяет, проходит ли какой-либо элемент в строке `test`.
//! \sa PIVector::any()
inline bool any(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (test((*p_)[st_ + i])) return true;
}
return false;
}
//! \~english Tests if all elements in the row pass the `test`.
//! \~russian Проверяет, проходят ли все элементы в строке `test`.
//! \sa PIVector::every()
inline bool every(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (!test((*p_)[st_ + i])) return false;
}
return true;
}
};
//! \class ColConst
//! \brief
//! \~english Proxy class representing a single read-only column in a \a PIVector2D.
//! \~russian Прокси-класс, представляющий один столбец в \a PIVector2D только для чтения.
//! \details
//! \~english Returned by const \a col(). Provides const access to column elements.
//! \~russian Возвращается константной версией \a col(). Предоставляет константный доступ к элементам столбца.
//! \sa Col, RowConst
class ColConst {
friend class PIVector2D<T>;
@@ -401,6 +780,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_; }
@@ -423,7 +808,12 @@ public:
return ret;
}
// --- Поиск в столбце (только чтение) ---
//! \~english Returns the first index of element `e` in the row, starting from `start`.
//! \~russian Возвращает первый индекс элемента `e` в строке, начиная с позиции `start`.
//! \details
//! \~english See \a PIVector::indexOf() for details on negative start handling.
//! \~russian Подробнее об обработке отрицательного `start` см. \a PIVector::indexOf().
//! \sa PIVector::indexOf()
inline ssize_t indexOf(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
@@ -431,6 +821,10 @@ public:
}
return -1;
}
//! \~english Returns the last index of element `e` in the row, searching backwards from `start`.
//! \~russian Возвращает последний индекс элемента `e` в строке, выполняя поиск в обратном направлении от `start`.
//! \sa PIVector::lastIndexOf()
inline ssize_t lastIndexOf(const T & e, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
@@ -438,6 +832,10 @@ public:
}
return -1;
}
//! \~english Returns the first index where the predicate `test` returns true, starting from `start`.
//! \~russian Возвращает первый индекс, для которого предикат `test` возвращает true, начиная с `start`.
//! \sa PIVector::indexWhere()
inline ssize_t indexWhere(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
@@ -445,6 +843,11 @@ public:
}
return -1;
}
//! \~english Returns the last index where the predicate `test` returns true, searching backwards from `start`.
//! \~russian Возвращает последний индекс, для которого предикат `test` возвращает true,
//! выполняя поиск в обратном направлении от `start`.
//! \sa PIVector::lastIndexWhere()
inline ssize_t lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
@@ -452,10 +855,75 @@ public:
}
return -1;
}
//! \~english Applies a function to each element of the column (read-only).
//! \~russian Применяет функцию к каждому элементу столбца (только чтение).
//! \details
//! \~english The function can't modify the elements.
//! \~russian Функция не может изменять элементы.
//! \sa forEach (modifiable)
inline void forEach(std::function<void(const T &)> func) const {
for (size_t i = 0; i < sz_; ++i) {
func((*p_)[i * step_ + col_]);
}
}
//! \~english Checks if the column contains the element `e`.
//! \~russian Проверяет, содержит ли столбец элемент `e`.
//! \sa PIVector::contains()
inline bool contains(const T & e, ssize_t start = 0) const { return indexOf(e, start) != -1; }
//! \~english Counts occurrences of `e` in the column.
//! \~russian Подсчитывает количество вхождений `e` в столбце.
//! \sa PIVector::entries()
inline int entries(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if ((*p_)[i * step_ + col_] == e) ++count;
}
return count;
}
//! \~english Counts elements in the column that pass the `test`.
//! \~russian Подсчитывает элементы в столбце, проходящие `test`.
//! \sa PIVector::entries(std::function)
inline int entries(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if (test((*p_)[i * step_ + col_])) ++count;
}
return count;
}
//! \~english Tests if any element in the column passes the `test`.
//! \~russian Проверяет, проходит ли какой-либо элемент в столбце `test`.
//! \sa PIVector::any()
inline bool any(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (test((*p_)[i * step_ + col_])) return true;
}
return false;
}
//! \~english Tests if all elements in the column pass the `test`.
//! \~russian Проверяет, проходят ли все элементы в столбце `test`.
//! \sa PIVector::every()
inline bool every(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (!test((*p_)[i * step_ + col_])) return false;
}
return true;
}
};
//! \~english Returns a reference to the element at the given row and column.
//! \~russian Возвращает ссылку на элемент по заданной строке и столбцу.
//! \details
//! \~english No bounds checking is performed.
//! \~russian Проверка границ не выполняется.
//! \sa at() (const version), PIVector::operator[]
inline T & element(size_t row, size_t col) { return mat[row * cols_ + col]; }
//! \~english Returns a const reference to the element at the given row and column.
@@ -468,6 +936,7 @@ public:
//! \~english Returns a proxy object for the row at the given index for modification.
//! \~russian Возвращает прокси-объект для строки по заданному индексу для модификации.
//! \sa row(), Col
inline Row operator[](size_t index) { return Row(this, index); }
//! \~english Returns a proxy object for the row at the given index for read-only access.
@@ -476,6 +945,7 @@ public:
//! \~english Returns a pointer to the underlying flat data starting at an optional offset.
//! \~russian Возвращает указатель на внутренние плоские данные, начиная с опционального смещения.
//! \sa PIVector::data()
inline T * data(size_t index = 0) { return mat.data(index); }
//! \~english Returns a const pointer to the underlying flat data starting at an optional offset.
@@ -485,6 +955,7 @@ public:
//! \~english Returns a proxy object for the row at the given index for modification.
//! \~russian Возвращает прокси-объект для строки по заданному индексу для модификации.
//! \sa operator[]
inline Row row(size_t index) { return Row(this, index); }
//! \~english Returns a proxy object for the row at the given index for read-only access.
@@ -493,21 +964,13 @@ public:
//! \~english Returns a proxy object for the column at the given index for modification.
//! \~russian Возвращает прокси-объект для столбца по заданному индексу для модификации.
//! \sa col() const
inline Col col(size_t index) { return Col(this, index); }
//! \~english Returns a proxy object for the column at the given index for read-only access.
//! \~russian Возвращает прокси-объект для столбца по заданному индексу только для чтения.
inline ColConst col(size_t index) const { return ColConst(this, index); }
//! \~english Replaces a row with the contents of another Row object.
//! \~russian Заменяет строку содержимым другого объекта Row.
inline PIVector2D<T> & setRow(size_t row, const Row & other) {
const size_t sz = piMin<size_t>(cols_, other.sz_);
mat._copyRaw(mat.data(cols_ * row), other.data(), sz);
return *this;
}
//! \~english Replaces a row with the contents of a read-only RowConst object.
//! \~russian Заменяет строку содержимым объекта RowConst только для чтения.
inline PIVector2D<T> & setRow(size_t row, const RowConst & other) {
@@ -526,18 +989,12 @@ public:
//! \~english Appends a new row to the bottom of the array from another Row object.
//! \~russian Добавляет новую строку в конец массива из другого объекта Row.
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 только для чтения.
//! \details
//! \~english If the array was empty, its column count is set to the size of the source row.
//! Otherwise, only `min(cols(), other.size())` elements are copied; the rest of the new row is default-initialized.
//! \~russian Если массив был пуст, количество столбцов устанавливается равным размеру исходной строки.
//! В противном случае копируется только `min(cols(), other.size())` элементов; остальные элементы новой строки инициализируются по
//! умолчанию. \sa PIVector::push_back()
inline PIVector2D<T> & addRow(const RowConst & other) {
if (cols_ == 0) cols_ = other.sz_;
const size_t sz = piMin<size_t>(cols_, other.sz_);
@@ -560,13 +1017,88 @@ public:
return *this;
}
//! \~english Appends a new column to the right of the array from a \a ColConst.
//! \~russian Добавляет новую строку в конец массива из \a ColConst.
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()) {
mat._copyRaw(&(dst[oldCols]), &(other[r]), 1);
} else {
const T tmp = T();
mat._copyRaw(&(dst[oldCols]), &tmp, 1);
}
}
mat._copyRaw(mat.data(oldCols), &(other[0]), 1);
cols_ = newCols;
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 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()) {
mat._copyRaw(&(dst[oldCols]), &(other[r]), 1);
} else {
const T tmp = T();
mat._copyRaw(&(dst[oldCols]), &tmp, 1);
}
}
mat._copyRaw(mat.data(oldCols), &(other[0]), 1);
cols_ = newCols;
return *this;
}
//! \~english Resizes the 2D array to new dimensions.
//! \~russian Изменяет размер двумерного массива.
//! \details
//! \~english If the new dimensions are larger, new elements are filled with `f`.
//! If they are smaller, the array is truncated.
//! \~russian Если новые размеры больше, новые элементы заполняются `f`.
//! Если они меньше, массив обрезается.
//! \~english If the new dimensions are larger, new elements are appended and filled with copies of `f`.
//! If they are smaller, the array is truncated (excess elements are destroyed). The underlying memory may be reallocated.
//! \~russian Если новые размеры больше текущих, новые элементы добавляются в конец и заполняются копиями `f`.
//! Если новые размеры меньше, массив усекается (лишние элементы уничтожаются). Внутренняя память может быть перераспределена.
//! \code
//! PIVector2D<int> mat(2, 3, 0); // 2x3 matrix filled with 0
//! mat.resize(3, 4, 1); // becomes 3x4, new elements filled with 1
//! \endcode
//! \sa PIVector::resize()
inline PIVector2D<T> & resize(size_t rows, size_t cols, const T & f = T()) {
if (rows == rows_ && cols == cols_) return *this;
PIVector2D<T> tmp(rows, cols, f);
@@ -583,6 +1115,7 @@ public:
//! \~english Equality operator.
//! \~russian Оператор равенства.
//! \sa PIVector::operator==
inline bool operator==(const PIVector2D<T> & t) const {
if (cols_ != t.cols_ || rows_ != t.rows_) return false;
return mat == t.mat;
@@ -594,6 +1127,10 @@ public:
//! \~english Converts the 2D array to a vector of vectors (PIVector<PIVector<T>>).
//! \~russian Преобразует двумерный массив в вектор векторов (PIVector<PIVector<T>>).
//! \details
//! \~english Each row vector is a copy of the corresponding row.
//! \~russian Каждый вектор-строка является копией соответствующей строки.
//! \sa fromVectors(), PIVector::PIVector(const T*, size_t)
inline PIVector<PIVector<T>> toVectors() const {
PIVector<PIVector<T>> ret;
ret.reserve(rows_);
@@ -616,6 +1153,10 @@ public:
//! \~english Swaps this 2D array with another.
//! \~russian Меняет местами этот двумерный массив с другим.
//! \details
//! \~english Swaps the flat vectors and the dimension members. Very fast, no memory allocation.
//! \~russian Обменивает внутренние плоские векторы и члены, хранящие размеры. Очень быстро, без выделения памяти.
//! \sa PIVector::swap()
inline void swap(PIVector2D<T> & other) {
mat.swap(other.mat);
piSwap<size_t>(rows_, other.rows_);
@@ -633,6 +1174,10 @@ public:
//! \~english Clears the array, removing all elements and setting dimensions to 0.
//! \~russian Очищает массив, удаляя все элементы и устанавливая размеры в 0.
//! \details
//! \~english The capacity of the underlying flat vector may remain unchanged.
//! \~russian Ёмкость внутреннего плоского вектора может остаться неизменной.
//! \sa PIVector::clear()
inline void clear() {
rows_ = cols_ = 0;
mat.clear();
@@ -641,31 +1186,32 @@ public:
//! \~english Checks if the underlying flat vector contains the element `e`.
//! \~russian Проверяет, содержит ли внутренний плоский вектор элемент `e`.
inline bool contains(const T & e, ssize_t start = 0) const { return mat.contains(e, start); }
//! \~english Checks if the underlying flat vector contains all elements of `v`.
//! \~russian Проверяет, содержит ли внутренний плоский вектор все элементы `v`.
inline bool contains(const PIVector<T> & v, ssize_t start = 0) const { return mat.contains(v, start); }
//! \sa PIVector::contains()
inline bool contains(const T & e) const { return mat.contains(e); }
//! \~english Counts occurrences of `e` in the underlying flat vector.
//! \~russian Подсчитывает количество вхождений `e` во внутреннем плоском векторе.
inline int entries(const T & e, ssize_t start = 0) const { return mat.entries(e, start); }
//! \sa PIVector::entries()
inline int entries(const T & e) const { return mat.entries(e); }
//! \~english Counts elements in the flat vector that pass the `test`.
//! \~russian Подсчитывает элементы в плоском векторе, проходящие `test`.
inline int entries(std::function<bool(const T & e)> test, ssize_t start = 0) const { return mat.entries(test, start); }
//! \sa PIVector::entries(std::function)
inline int entries(std::function<bool(const T & e)> test) const { return mat.entries(test); }
//! \~english Returns the first index (row, col) of `e` in the 2D array.
//! \~russian Возвращает первый индекс (строка, столбец) элемента `e` в двумерном массиве.
inline PIPair<ssize_t, ssize_t> indexOf(const T & e, ssize_t start = 0) const {
ssize_t flat = mat.indexOf(e, start);
//! \sa PIVector::indexOf()
inline PIPair<ssize_t, ssize_t> indexOf(const T & e) const {
ssize_t flat = mat.indexOf(e);
if (flat < 0 || cols_ == 0) return PIPair<ssize_t, ssize_t>(-1, -1);
return PIPair<ssize_t, ssize_t>(flat / cols_, flat % cols_);
}
//! \~english Returns the first index (row, col) in the 2D array that passes the `test`.
//! \~russian Возвращает первый индекс (строка, столбец) в двумерном массиве, проходящий `test`.
//! \sa PIVector::indexWhere()
inline PIPair<ssize_t, ssize_t> indexWhere(std::function<bool(const T & e)> test, ssize_t start = 0) const {
ssize_t flat = mat.indexWhere(test, start);
if (flat < 0 || cols_ == 0) return PIPair<ssize_t, ssize_t>(-1, -1);
@@ -674,6 +1220,7 @@ public:
//! \~english Returns the last index (row, col) of `e` in the 2D array.
//! \~russian Возвращает последний индекс (строка, столбец) элемента `e` в двумерном массиве.
//! \sa PIVector::lastIndexOf()
inline PIPair<ssize_t, ssize_t> lastIndexOf(const T & e, ssize_t start = -1) const {
ssize_t flat = mat.lastIndexOf(e, start);
if (flat < 0 || cols_ == 0) return PIPair<ssize_t, ssize_t>(-1, -1);
@@ -682,6 +1229,7 @@ public:
//! \~english Returns the last index (row, col) in the 2D array that passes the `test`.
//! \~russian Возвращает последний индекс (строка, столбец) в двумерном массиве, проходящий `test`.
//! \sa PIVector::lastIndexWhere()
inline PIPair<ssize_t, ssize_t> lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const {
ssize_t flat = mat.lastIndexWhere(test, start);
if (flat < 0 || cols_ == 0) return PIPair<ssize_t, ssize_t>(-1, -1);
@@ -691,14 +1239,17 @@ public:
//! \~english Tests if any element in the flat vector passes the `test`.
//! \~russian Проверяет, проходит ли какой-либо элемент в плоском векторе `test`.
//! \sa PIVector::any()
inline bool any(std::function<bool(const T & e)> test) const { return mat.any(test); }
//! \~english Tests if all elements in the flat vector pass the `test`.
//! \~russian Проверяет, проходят ли все элементы в плоском векторе `test`.
//! \sa PIVector::every()
inline bool every(std::function<bool(const T & e)> test) const { return mat.every(test); }
//! \~english Fills the entire 2D array with copies of `e`.
//! \~russian Заполняет весь двумерный массив копиями `e`.
//! \sa PIVector::fill()
inline PIVector2D<T> & fill(const T & e = T()) {
mat.fill(e);
return *this;
@@ -706,6 +1257,7 @@ public:
//! \~english Fills the entire 2D array using a generator function `f` based on flat index.
//! \~russian Заполняет весь двумерный массив, используя функцию-генератор `f` на основе плоского индекса.
//! \sa PIVector::fill(std::function)
inline PIVector2D<T> & fill(std::function<T(size_t i)> f) {
mat.fill(f);
return *this;
@@ -717,6 +1269,7 @@ public:
//! \~english Assigns new size and fills with value.
//! \~russian Задаёт новый размер и заполняет значением.
//! \sa PIVector::assign(size_t, const T&)
inline PIVector2D<T> & assign(size_t rows, size_t cols, const T & f = T()) {
mat.assign(rows * cols, f);
rows_ = rows;
@@ -725,9 +1278,15 @@ public:
}
// TODO: исправить - при транспонировании количество строк становится количеством столбцов и наоборот
//! \~english Returns a transposed 2D array (rows become columns and vice versa).
//! \~russian Возвращает транспонированный двумерный массив (строки становятся столбцами и наоборот).
//! \details
//! \~english The element at (r, c) in the original becomes at (c, r) in the result.
//! \~russian Элемент (r, c) исходного массива становится элементом (c, r) в результате.
//! \code
//! PIVector2D<int> mat(2, 3, ...);
//! auto t = mat.transposed(); // now 3x2
//! \endcode
inline PIVector2D<T> transposed() const {
if (isEmpty()) return PIVector2D<T>();
PIVector2D<T> result(cols_, rows_);
@@ -741,6 +1300,7 @@ public:
//! \~english Reverses the order of rows in place.
//! \~russian Изменяет порядок строк на обратный на месте.
//! \sa reverseColumns(), PIVector::reverse()
inline PIVector2D<T> & reverseRows() {
const size_t half = rows_ / 2;
for (size_t i = 0; i < half; ++i) {
@@ -755,6 +1315,7 @@ public:
//! \~english Reverses the order of columns in each row in place.
//! \~russian Изменяет порядок столбцов в каждой строке на обратный на месте.
//! \sa reverseRows(), PIVector::reverse()
inline PIVector2D<T> & reverseColumns() {
for (size_t r = 0; r < rows_; ++r) {
Row currentRow = row(r);
@@ -768,10 +1329,14 @@ public:
//! \~english Returns a sub-2D array (a range of rows and columns).
//! \~russian Возвращает подмассив (диапазон строк и столбцов).
//! \details
//! \~english If the range exceeds the array boundaries, it is clipped. If rowCount or colCount is 0, an empty array is returned.
//! \~russian Если диапазон выходит за границы массива, он обрезается. Если rowCount или colCount равны 0, возвращается пустой массив.
//! \sa PIVector::getRange()
inline PIVector2D<T> getRange(size_t rowStart, size_t rowCount, size_t colStart, size_t colCount) const {
if (rowStart >= rows_ || colStart >= cols_ || rowCount == 0 || colCount == 0) return PIVector2D<T>();
size_t actualRowCount = piMin(rowCount, rows_ - rowStart);
size_t actualColCount = piMin(colCount, cols_ - colStart);
size_t actualRowCount = piMin<size_t>(rowCount, rows_ - rowStart);
size_t actualColCount = piMin<size_t>(colCount, cols_ - colStart);
PIVector2D<T> result(actualRowCount, actualColCount);
for (size_t r = 0; r < actualRowCount; ++r) {
@@ -784,6 +1349,10 @@ public:
//! \~english Applies a function to each element and returns a new 2D array of a different type.
//! \~russian Применяет функцию к каждому элементу и возвращает новый двумерный массив другого типа.
//! \details
//! \~english The original array is not modified.
//! \~russian Исходный массив не изменяется.
//! \sa PIVector::map()
template<typename ST>
inline PIVector2D<ST> map(std::function<ST(const T & e)> f) const {
return PIVector2D<ST>(rows_, cols_, mat.template map<ST>(f));
@@ -791,6 +1360,7 @@ public:
//! \~english Applies a function (with row and col indices) to each element and returns a new 2D array.
//! \~russian Применяет функцию (с индексами строки и столбца) к каждому элементу и возвращает новый двумерный массив.
//! \sa PIVector::mapIndexed()
template<typename ST>
inline PIVector2D<ST> mapIndexed(std::function<ST(size_t row, size_t col, const T & e)> f) const {
PIVector<ST> mappedMat;
@@ -803,20 +1373,22 @@ public:
return PIVector2D<ST>(rows_, cols_, std::move(mappedMat));
}
// --- Итерация по строкам и столбцам ---
//! \~english Applies a function to each row (modifiable).
//! \~russian Применяет функцию к каждой строке (с возможностью изменения).
//! \sa forEachRow() const, PIVector::forEach()
inline PIVector2D<T> & forEachRow(std::function<void(Row)> f) {
for (size_t r = 0; r < rows_; ++r)
f(row(r));
return *this;
}
//! \~english Applies a function to each row (read-only).
//! \~russian Применяет функцию к каждой строке (только чтение).
inline void forEachRow(std::function<void(RowConst)> f) const {
for (size_t r = 0; r < rows_; ++r)
f(row(r));
}
//! \~english Applies a function to each column (modifiable).
//! \~russian Применяет функцию к каждому столбцу (с возможностью изменения).
inline PIVector2D<T> & forEachColumn(std::function<void(Col)> f) {
@@ -824,8 +1396,10 @@ public:
f(col(c));
return *this;
}
//! \~english Applies a function to each column (read-only).
//! \~russian Применяет функцию к каждому столбцу (только чтение).
//! \param f Function taking a \a ColConst.
inline void forEachColumn(std::function<void(ColConst)> f) const {
for (size_t c = 0; c < cols_; ++c)
f(col(c));
@@ -833,6 +1407,7 @@ public:
//! \~english Accumulates a value across all elements.
//! \~russian Аккумулирует значение по всем элементам.
//! \sa PIVector::reduce()
template<typename ST>
inline ST reduce(std::function<ST(const T & e, const ST & acc)> f, const ST & initial = ST()) const {
return mat.template reduce<ST>(f, initial);
@@ -840,6 +1415,7 @@ public:
//! \~english Accumulates a value across all elements with indices.
//! \~russian Аккумулирует значение по всем элементам с индексами.
//! \sa PIVector::reduceIndexed()
template<typename ST>
inline ST reduceIndexed(std::function<ST(size_t row, size_t col, const T & e, const ST & acc)> f, const ST & initial = ST()) const {
ST ret(initial);
@@ -853,10 +1429,15 @@ public:
//! \~english Removes a row from the 2D array.
//! \~russian Удаляет строку из двумерного массива.
//! \details
//! \~english The elements of the specified row are destroyed, and all subsequent rows are shifted up.
//! If the last row is removed and the array becomes empty, \a cols() is set to 0.
//! \~russian Элементы указанной строки уничтожаются, а все последующие строки сдвигаются вверх.
//! Если удаляется последняя строка и массив становится пустым, \a cols() устанавливается в 0.
//! \sa removeColumn(), PIVector::remove()
inline PIVector2D<T> & removeRow(size_t row) {
if (row >= rows_) return *this;
size_t startIdx = row * cols_;
mat.remove(startIdx, cols_);
mat.remove(row * cols_, cols_);
rows_--;
if (rows_ == 0) cols_ = 0;
return *this;
@@ -864,6 +1445,10 @@ public:
//! \~english Removes a column from the 2D array.
//! \~russian Удаляет столбец из двумерного массива.
//! \details
//! \~english This operation is more expensive than removing a row because elements must be moved.
//! \~russian Эта операция дороже, чем удаление строки, поскольку требуется перемещение элементов.
//! \sa removeRow(), PIVector::remove()
inline PIVector2D<T> & removeColumn(size_t col) {
if (col >= cols_ || rows_ == 0) return *this;
PIVector2D<T> result(rows_, cols_ - 1);
@@ -879,26 +1464,29 @@ public:
//! \~english Removes all rows that satisfy a condition.
//! \~russian Удаляет все строки, удовлетворяющие условию.
//! \details
//! \~english Rows are removed from the bottom to avoid index shifting issues.
//! \~russian Строки удаляются снизу вверх, чтобы избежать проблем со смещением индексов.
//! \sa removeColumnsWhere(), PIVector::removeWhere()
inline PIVector2D<T> & removeRowsWhere(std::function<bool(const RowConst &)> test) {
ssize_t r = rows_ - 1;
while (r >= 0) {
ssize_t r = rows_;
while (--r >= 0) {
if (test(RowConst(this, r))) {
removeRow(r);
}
--r;
}
return *this;
}
//! \~english Removes all columns that satisfy a condition.
//! \~russian Удаляет все столбцы, удовлетворяющие условию.
//! \sa removeRowsWhere()
inline PIVector2D<T> & removeColumnsWhere(std::function<bool(const ColConst &)> test) {
ssize_t c = cols_ - 1;
while (c >= 0) {
ssize_t c = cols_;
while (--c >= 0) {
if (test(ColConst(this, c))) {
removeColumn(c);
}
--c;
}
return *this;
}
@@ -906,6 +1494,7 @@ public:
//! \~english Returns a new 2D array containing only the rows that pass the test.
//! \~russian Возвращает новый двумерный массив, содержащий только строки, прошедшие проверку.
//! \sa filterColumns(), PIVector::filter()
inline PIVector2D<T> filterRows(std::function<bool(const RowConst &)> test) const {
PIVector2D<T> result;
for (size_t r = 0; r < rows_; ++r) {
@@ -919,6 +1508,7 @@ public:
//! \~english Returns a new 2D array containing only the columns that pass the test.
//! \~russian Возвращает новый двумерный массив, содержащий только столбцы, прошедшие проверку.
//! \sa filterRows()
inline PIVector2D<T> filterColumns(std::function<bool(const ColConst &)> test) const {
if (isEmpty()) return PIVector2D<T>();
PIVector<size_t> goodCols;
@@ -936,14 +1526,6 @@ public:
return result;
}
//! \~english Returns a new 2D array (as a single row) containing only the elements that pass the test.
//! \~russian Возвращает новый двумерный массив (в виде одной строки), содержащий только элементы, прошедшие проверку.
inline PIVector2D<T> filterElements(std::function<bool(const T &)> test) const {
PIVector<T> filtered = mat.filter(test);
if (filtered.isEmpty()) return PIVector2D<T>();
return PIVector2D<T>(1, filtered.size(), filtered);
}
protected:
size_t rows_, cols_;
PIVector<T> mat;

View File

@@ -342,12 +342,12 @@ PIVector<PIHIDeviceInfo> PIHIDevice::allDevices(bool try_open) {
PIDir hid_dir("/sys/bus/hid/devices"_a);
auto hid_devs = hid_dir.entries();
for (auto hd: hid_devs) {
for (const auto & hd: hid_devs) {
// piCout << d.path;
if (!isDir(hd)) continue;
PIDir dir_input(hd.path + "/input"_a);
auto hid_inputs = dir_input.entries();
for (auto hd_i: hid_inputs) {
for (const auto & hd_i: hid_inputs) {
if (!isDir(hd_i)) continue;
// now in /sys/bus/hid/devices/<dev>/input/input<N>
// piCout << hd_i.path;
@@ -364,7 +364,7 @@ PIVector<PIHIDeviceInfo> PIHIDevice::allDevices(bool try_open) {
PIDir dir_e(hd_i.path);
PIStringList devs;
auto dl_e = dir_e.entries();
for (auto d_e: dl_e) {
for (const auto & d_e: dl_e) {
if (!d_e.isDir() || d_e.flags[PIFile::FileInfo::Dot] || d_e.flags[PIFile::FileInfo::DotDot]) continue;
devs << d_e.name();
}
@@ -392,7 +392,7 @@ PIVector<PIHIDeviceInfo> PIHIDevice::allDevices(bool try_open) {
if (test_f.isClosed()) continue;
}
ullong ev = readFile(hd_i.path + "/capabilities/ev"_a).toULLong(16);
// ullong ev = readFile(hd_i.path + "/capabilities/ev"_a).toULLong(16);
auto readAxes = [readFile, checkBit, &hd_i, &dev](const PIString & file, bool is_relative) {
PIVector<PIHIDeviceInfo::AxisInfo> ret;

70
plans/pivector2d.md Normal file
View File

@@ -0,0 +1,70 @@
# План рефакторинга PIVector2D
## Этап 1: Выполнить наследование Row от RowConst, Col от ColConst
### 1.1 Переместить RowConst перед Row
- Найти местоположение RowConst (текущая позиция ~строка 610)
- Переместить определение RowConst перед Row (до строки ~184)
### 1.2 Переместить ColConst перед Col
- Найти местоположение ColConst (текущая позиция ~строка 770)
- Переместить определение ColConst перед Col (до строки ~402)
### 1.3 Изменить класс Row
- Наследовать от RowConst: `class Row : public RowConst`
- Убрать дублирующиеся методы (они унаследованы от RowConst):
- size()
- toVector()
- operator[] (const версия)
- data() (const версия)
- indexOf()
- lastIndexOf()
- indexWhere()
- lastIndexWhere()
- forEach() (const версия)
- contains()
- entries()
- any()
- every()
- Сохранить неконстантные методы:
- operator[] (неконстантный)
- data() (неконстантный)
- operator=()
- forEach() (неконстантный)
- fill()
### 1.4 Изменить класс Col
- Наследовать от ColConst: `class Col : public ColConst`
- Аналогично убрать дублирующиеся методы
### 1.5 Проверить тесты
- Запустить: `./pip_math_test --gtest_filter="*Vector2D*"`
---
## Этап 2: Заменить PIPair<ssize_t, ssize_t> на PIVector2DIndex
### 2.1 Создать структуру PIVector2DIndex
```cpp
struct PIVector2DIndex {
ssize_t row;
ssize_t col;
};
```
### 2.2 Обновить return types
Методы для изменения:
- indexOf() -> возвращает PIVector2DIndex вместо PIPair<ssize_t, ssize_t>
- lastIndexOf()
- indexWhere()
- lastIndexWhere()
### 2.3 Обновить тесты и документацию
---
## Этап 3: Дополнительные улучшения (опционально)
- Добавить методы для работы с диапазонами
- Оптимизировать методы удаления строк/столбцов
- Добавить проверку границ в debug-режиме

View File

@@ -379,6 +379,146 @@ TEST_F(Vector2DTest, addRow_with_shorter_vector_uses_min) {
}
}
// ==================== ADD COLUMN TESTS ====================
TEST_F(Vector2DTest, addColumn_appends_column_to_empty) {
PIVector2D<int> empty;
PIVector<int> newCol(5);
for (size_t i = 0; i < 5; ++i)
newCol[i] = static_cast<int>(100 + i);
empty.addColumn(newCol);
EXPECT_EQ(empty.rows(), 5);
EXPECT_EQ(empty.cols(), 1);
for (size_t r = 0; r < 5; ++r) {
EXPECT_EQ(empty.element(r, 0), static_cast<int>(100 + r));
}
}
TEST_F(Vector2DTest, addColumn_appends_column_to_existing) {
size_t oldRows = vec.rows();
size_t oldCols = vec.cols();
PIVector<int> newCol(oldRows, 999);
vec.addColumn(newCol);
EXPECT_EQ(vec.rows(), oldRows);
EXPECT_EQ(vec.cols(), oldCols + 1);
// Check that old data is preserved
for (size_t r = 0; r < oldRows; ++r) {
for (size_t c = 0; c < oldCols; ++c) {
EXPECT_EQ(vec.element(r, c), static_cast<int>(r * COLS_COUNT_INIT + c));
}
}
// Check new column
for (size_t r = 0; r < oldRows; ++r) {
EXPECT_EQ(vec.element(r, oldCols), 999);
}
}
TEST_F(Vector2DTest, addColumn_with_shorter_vector_uses_min) {
size_t oldRows = vec.rows();
size_t oldCols = vec.cols();
size_t shortLen = oldRows - 10;
PIVector<int> shortCol(shortLen, 777);
vec.addColumn(shortCol);
EXPECT_EQ(vec.cols(), oldCols + 1);
// First shortLen rows should be 777
for (size_t r = 0; r < shortLen; ++r) {
EXPECT_EQ(vec.element(r, oldCols), 777);
}
// Remaining rows should be default-initialized (0)
for (size_t r = shortLen; r < oldRows; ++r) {
EXPECT_EQ(vec.element(r, oldCols), 0);
}
}
TEST_F(Vector2DTest, addColumn_with_longer_vector_truncates) {
size_t oldRows = vec.rows();
size_t oldCols = vec.cols();
size_t longLen = oldRows + 10;
PIVector<int> longCol(longLen, 555);
vec.addColumn(longCol);
EXPECT_EQ(vec.cols(), oldCols + 1);
// All rows should be 555 (only first oldRows elements are used)
for (size_t r = 0; r < oldRows; ++r) {
EXPECT_EQ(vec.element(r, oldCols), 555);
}
}
TEST_F(Vector2DTest, addColumn_with_empty_source_does_nothing_on_empty) {
PIVector2D<int> empty;
PIVector<int> emptyCol;
empty.addColumn(emptyCol);
EXPECT_TRUE(empty.isEmpty());
EXPECT_EQ(empty.rows(), 0);
EXPECT_EQ(empty.cols(), 0);
}
TEST_F(Vector2DTest, addColumn_with_empty_source_adds_default_column) {
auto oldVec = vec;
vec.addColumn({});
EXPECT_EQ(vec.cols(), oldVec.cols());
EXPECT_EQ(vec.rows(), oldVec.rows());
for (size_t r = 0; r < oldVec.rows(); ++r) {
for (size_t c = 0; c < oldVec.cols(); ++c) {
EXPECT_EQ(vec.element(r, c), oldVec.element(r, c));
}
}
}
TEST_F(Vector2DTest, addColumn_with_Col_proxy_works) {
auto oldVec = vec;
const size_t colIndex = 5;
auto srcCol = oldVec.col(colIndex);
vec.addColumn(srcCol);
EXPECT_EQ(vec.cols(), oldVec.cols() + 1);
EXPECT_EQ(vec.rows(), oldVec.rows());
for (size_t r = 0; r < oldVec.rows(); ++r) {
for (size_t c = 0; c < oldVec.cols(); ++c) {
EXPECT_EQ(vec.element(r, c), oldVec.element(r, c));
}
}
for (size_t r = 0; r < vec.rows(); ++r) {
// EXPECT_EQ(vec.element(r, oldVec.cols()), int());
piCout << r << vec.cols() << oldVec.cols() << colIndex;
EXPECT_EQ(vec.element(r, oldVec.cols()), oldVec.element(r, colIndex));
}
}
TEST_F(Vector2DTest, addColumn_with_ColConst_proxy_works) {
size_t oldRows = vec.rows();
size_t oldCols = vec.cols();
const auto & constVec = vec;
auto srcCol = constVec.col(7);
// Need a non-const array to add to
PIVector2D<int> mutableVec = vec; // copy
mutableVec.addColumn(srcCol);
EXPECT_EQ(mutableVec.cols(), oldCols + 1);
for (size_t r = 0; r < oldRows; ++r) {
EXPECT_EQ(mutableVec.element(r, oldCols), vec.element(r, 7));
}
}
// ==================== RESIZE TESTS ====================
class Vector2DResizeTest: public Vector2DTest {
protected:
@@ -470,17 +610,15 @@ TEST_F(Vector2DTest, contains_finds_element_in_flat_vector) {
TEST_F(Vector2DTest, contains_with_start_parameter_works) {
int target = 10 * COLS_COUNT_INIT + 15;
EXPECT_TRUE(vec.contains(target));
EXPECT_TRUE(vec.contains(target, target)); // start exactly at target (inclusive)
EXPECT_FALSE(vec.contains(target, target + 1)); // start after target
}
TEST_F(Vector2DTest, contains_vector_of_elements_works) {
PIVector<int> searchFor;
searchFor << 100 << 200 << 300;
EXPECT_TRUE(vec.contains(searchFor));
EXPECT_TRUE(vec.asPlainVector().containsAll(searchFor));
searchFor << -999;
EXPECT_FALSE(vec.contains(searchFor));
EXPECT_FALSE(vec.asPlainVector().containsAll(searchFor));
}
TEST_F(Vector2DTest, entries_counts_occurrences) {
@@ -652,6 +790,53 @@ TEST_F(Vector2DTest, transposed_returns_correct_dimensions) {
}
}
TEST(Vector2DTransposeTest, emptyMatrix_returnsEmpty) {
PIVector2D<int> empty;
auto transposed = empty.transposed();
EXPECT_TRUE(transposed.isEmpty());
EXPECT_EQ(transposed.rows(), 0);
EXPECT_EQ(transposed.cols(), 0);
}
TEST(Vector2DTransposeTest, singleElement_returnsSame) {
PIVector2D<int> single(1, 1, 42);
auto transposed = single.transposed();
EXPECT_EQ(transposed.rows(), 1);
EXPECT_EQ(transposed.cols(), 1);
EXPECT_EQ(transposed.element(0, 0), 42);
}
TEST(Vector2DTransposeTest, oneRow_becomesOneColumn) {
PIVector2D<int> rowVec(1, 5);
for (size_t c = 0; c < 5; ++c)
rowVec.element(0, c) = static_cast<int>(c);
auto transposed = rowVec.transposed();
EXPECT_EQ(transposed.rows(), 5);
EXPECT_EQ(transposed.cols(), 1);
for (size_t r = 0; r < 5; ++r) {
EXPECT_EQ(transposed.element(r, 0), static_cast<int>(r));
}
}
TEST(Vector2DTransposeTest, oneColumn_becomesOneRow) {
PIVector2D<int> colVec(5, 1);
for (size_t r = 0; r < 5; ++r)
colVec.element(r, 0) = static_cast<int>(r);
auto transposed = colVec.transposed();
EXPECT_EQ(transposed.rows(), 1);
EXPECT_EQ(transposed.cols(), 5);
for (size_t c = 0; c < 5; ++c) {
EXPECT_EQ(transposed.element(0, c), static_cast<int>(c));
}
}
TEST_F(Vector2DTest, transposed_doesNotModifyOriginal) {
auto original = vec; // копия для сравнения
auto transposed = vec.transposed();
// Проверяем, что исходный массив не изменился
EXPECT_EQ(vec, original);
}
TEST_F(Vector2DTest, reverseRows_reverses_row_order) {
auto original = vec;
vec.reverseRows();
@@ -993,6 +1178,242 @@ TEST(Vector2DEdgeTest, single_element_vector) {
EXPECT_EQ(single.element(0, 0), 42);
}
// ==================== PROXY ADDITIONAL OPERATIONS TESTS ====================
TEST_F(Vector2DTest, row_proxy_forEach_modifies_elements) {
auto row = vec[5];
row.forEach([](int & e) { e += 100; });
for (size_t c = 0; c < COLS_COUNT_INIT; ++c) {
EXPECT_EQ(vec.element(5, c), 5 * COLS_COUNT_INIT + c + 100);
}
}
TEST_F(Vector2DTest, row_proxy_forEach_const_iterates) {
const auto & constVec = vec;
auto row = constVec[5];
size_t count = 0;
row.forEach([&count](const int &) { ++count; });
EXPECT_EQ(count, COLS_COUNT_INIT);
}
TEST_F(Vector2DTest, row_proxy_fill_sets_all_elements) {
auto row = vec[12];
row.fill(999);
for (size_t c = 0; c < COLS_COUNT_INIT; ++c) {
EXPECT_EQ(vec.element(12, c), 999);
}
}
TEST_F(Vector2DTest, row_proxy_contains_finds_element) {
auto row = vec[8];
EXPECT_TRUE(row.contains(vec.element(8, 10)));
EXPECT_FALSE(row.contains(-999));
}
TEST_F(Vector2DTest, row_proxy_entries_counts_occurrences) {
auto row = vec[15];
// Add a duplicate
int val = vec.element(15, 5);
vec.element(15, 20) = val;
EXPECT_EQ(row.entries(val), 2);
EXPECT_EQ(row.entries(-999), 0);
}
TEST_F(Vector2DTest, row_proxy_entries_with_predicate_counts_matches) {
auto row = vec[20];
auto isEven = [](const int & e) { return e % 2 == 0; };
int expected = 0;
for (size_t c = 0; c < COLS_COUNT_INIT; ++c) {
if (row[c] % 2 == 0) ++expected;
}
EXPECT_EQ(row.entries(isEven), expected);
}
TEST_F(Vector2DTest, row_proxy_any_returns_true_if_any_match) {
auto row = vec[25];
auto isNegative = [](const int & e) { return e < 0; };
EXPECT_FALSE(row.any(isNegative));
auto isPositive = [](const int & e) { return e >= 0; };
EXPECT_TRUE(row.any(isPositive));
}
TEST_F(Vector2DTest, row_proxy_every_returns_true_if_all_match) {
auto row = vec[30];
auto isLessThanMax = [&](const int & e) { return e < static_cast<int>(vec.size()); };
EXPECT_TRUE(row.every(isLessThanMax));
auto isEven = [](const int & e) { return e % 2 == 0; };
EXPECT_FALSE(row.every(isEven));
}
// ----------------------------------------------------------------------------
TEST_F(Vector2DTest, col_proxy_forEach_modifies_elements) {
auto col = vec.col(7);
col.forEach([](int & e) { e += 50; });
for (size_t r = 0; r < ROWS_COUNT_INIT; ++r) {
EXPECT_EQ(vec.element(r, 7), r * COLS_COUNT_INIT + 7 + 50);
}
}
TEST_F(Vector2DTest, col_proxy_forEach_const_iterates) {
const auto & constVec = vec;
auto col = constVec.col(9);
size_t count = 0;
col.forEach([&count](const int &) { ++count; });
EXPECT_EQ(count, ROWS_COUNT_INIT);
}
TEST_F(Vector2DTest, col_proxy_fill_sets_all_elements) {
auto col = vec.col(11);
col.fill(777);
for (size_t r = 0; r < ROWS_COUNT_INIT; ++r) {
EXPECT_EQ(vec.element(r, 11), 777);
}
}
TEST_F(Vector2DTest, col_proxy_contains_finds_element) {
auto col = vec.col(13);
EXPECT_TRUE(col.contains(vec.element(5, 13)));
EXPECT_FALSE(col.contains(-999));
}
TEST_F(Vector2DTest, col_proxy_entries_counts_occurrences) {
auto col = vec.col(17);
int val = vec.element(3, 17);
vec.element(22, 17) = val; // duplicate
EXPECT_EQ(col.entries(val), 2);
EXPECT_EQ(col.entries(-999), 0);
}
TEST_F(Vector2DTest, col_proxy_entries_with_predicate_counts_matches) {
auto col = vec.col(19);
auto isOdd = [](const int & e) { return e % 2 != 0; };
int expected = 0;
for (size_t r = 0; r < ROWS_COUNT_INIT; ++r) {
if (col[r] % 2 != 0) ++expected;
}
EXPECT_EQ(col.entries(isOdd), expected);
}
TEST_F(Vector2DTest, col_proxy_any_returns_true_if_any_match) {
auto col = vec.col(21);
auto isNegative = [](const int & e) { return e < 0; };
EXPECT_FALSE(col.any(isNegative));
auto isPositive = [](const int & e) { return e >= 0; };
EXPECT_TRUE(col.any(isPositive));
}
TEST_F(Vector2DTest, col_proxy_every_returns_true_if_all_match) {
auto col = vec.col(23);
auto isLessThanMax = [&](const int & e) { return e < static_cast<int>(vec.size()); };
EXPECT_TRUE(col.every(isLessThanMax));
auto isEven = [](const int & e) { return e % 2 == 0; };
EXPECT_FALSE(col.every(isEven));
}
// ----------------------------------------------------------------------------
TEST_F(Vector2DTest, rowconst_proxy_forEach_iterates) {
const auto & constVec = vec;
auto row = constVec[5];
size_t count = 0;
row.forEach([&count](const int &) { ++count; });
EXPECT_EQ(count, COLS_COUNT_INIT);
}
TEST_F(Vector2DTest, rowconst_proxy_contains_finds_element) {
const auto & constVec = vec;
auto row = constVec[6];
EXPECT_TRUE(row.contains(vec.element(6, 10)));
EXPECT_FALSE(row.contains(-999));
}
TEST_F(Vector2DTest, rowconst_proxy_entries_counts_occurrences) {
const auto & constVec = vec;
auto row = constVec[7];
int val = vec.element(7, 5);
// We can't modify through const proxy, but duplicates already exist from previous tests
EXPECT_GE(row.entries(val), 1); // at least one
}
TEST_F(Vector2DTest, rowconst_proxy_entries_with_predicate_counts_matches) {
const auto & constVec = vec;
auto row = constVec[9];
auto isEven = [](const int & e) { return e % 2 == 0; };
int expected = 0;
for (size_t c = 0; c < COLS_COUNT_INIT; ++c) {
if (vec.element(9, c) % 2 == 0) ++expected;
}
EXPECT_EQ(row.entries(isEven), expected);
}
TEST_F(Vector2DTest, rowconst_proxy_any_returns_true_if_any_match) {
const auto & constVec = vec;
auto row = constVec[10];
auto isNegative = [](const int & e) { return e < 0; };
EXPECT_FALSE(row.any(isNegative));
auto isPositive = [](const int & e) { return e >= 0; };
EXPECT_TRUE(row.any(isPositive));
}
TEST_F(Vector2DTest, rowconst_proxy_every_returns_true_if_all_match) {
const auto & constVec = vec;
auto row = constVec[11];
auto isLessThanMax = [&](const int & e) { return e < static_cast<int>(vec.size()); };
EXPECT_TRUE(row.every(isLessThanMax));
auto isEven = [](const int & e) { return e % 2 == 0; };
EXPECT_FALSE(row.every(isEven));
}
// ----------------------------------------------------------------------------
TEST_F(Vector2DTest, colconst_proxy_forEach_iterates) {
const auto & constVec = vec;
auto col = constVec.col(25);
size_t count = 0;
col.forEach([&count](const int &) { ++count; });
EXPECT_EQ(count, ROWS_COUNT_INIT);
}
TEST_F(Vector2DTest, colconst_proxy_contains_finds_element) {
const auto & constVec = vec;
auto col = constVec.col(27);
EXPECT_TRUE(col.contains(vec.element(4, 27)));
EXPECT_FALSE(col.contains(-999));
}
TEST_F(Vector2DTest, colconst_proxy_entries_counts_occurrences) {
const auto & constVec = vec;
auto col = constVec.col(29);
int val = vec.element(8, 29);
EXPECT_GE(col.entries(val), 1);
}
TEST_F(Vector2DTest, colconst_proxy_entries_with_predicate_counts_matches) {
const auto & constVec = vec;
auto col = constVec.col(31);
auto isOdd = [](const int & e) { return e % 2 != 0; };
int expected = 0;
for (size_t r = 0; r < ROWS_COUNT_INIT; ++r) {
if (vec.element(r, 31) % 2 != 0) ++expected;
}
EXPECT_EQ(col.entries(isOdd), expected);
}
TEST_F(Vector2DTest, colconst_proxy_any_returns_true_if_any_match) {
const auto & constVec = vec;
auto col = constVec.col(33);
auto isNegative = [](const int & e) { return e < 0; };
EXPECT_FALSE(col.any(isNegative));
auto isPositive = [](const int & e) { return e >= 0; };
EXPECT_TRUE(col.any(isPositive));
}
TEST_F(Vector2DTest, colconst_proxy_every_returns_true_if_all_match) {
const auto & constVec = vec;
auto col = constVec.col(0);
auto isLessThanMax = [&](const int & e) { return e < static_cast<int>(vec.size()); };
EXPECT_TRUE(col.every(isLessThanMax));
auto isNotEven = [](const int & e) { return e % 2 != 0; };
EXPECT_FALSE(col.every(isNotEven));
}
// ==================== OUTPUT TESTS ====================
TEST_F(Vector2DTest, picout_operator_works) {
// Just test that it compiles and doesn't crash