add PIVector2D Index

This commit is contained in:
2026-02-27 00:34:56 +03:00
parent b01aecc2fe
commit 56ff28c793
2 changed files with 85 additions and 4 deletions

View File

@@ -517,8 +517,8 @@ public:
//! \~english Accesses the element at the given column index within the row. //! \~english Accesses the element at the given column index within the row.
//! \~russian Доступ к элементу по заданному индексу столбца в строке. //! \~russian Доступ к элементу по заданному индексу столбца в строке.
//! \details //! \details
//! \~english No bounds checking is performed in release builds; use with caution. //! \~english No bounds checking is performed; use with caution.
//! \~russian В релизной сборке проверка границ не выполняется; используйте с осторожностью. //! \~russian Проверка границ не выполняется; используйте с осторожностью.
//! \sa PIVector::operator[] //! \sa PIVector::operator[]
inline T & operator[](size_t index) { return (*p_)[this->st_ + index]; } inline T & operator[](size_t index) { return (*p_)[this->st_ + index]; }
@@ -669,10 +669,33 @@ public:
//! \~russian Возвращает константную ссылку на элемент по заданной строке и столбцу. //! \~russian Возвращает константную ссылку на элемент по заданной строке и столбцу.
inline const T & element(size_t row, size_t col) const { return mat[row * cols_ + col]; } inline const T & element(size_t row, size_t col) const { return mat[row * cols_ + col]; }
//! \~english Returns a const reference to the element at the given row and column (bounds-checked only in debug). //! \~english Returns a const reference to the element at the given row and column
//! \~russian Возвращает константную ссылку на элемент по заданной строке и столбцу (проверка границ только в отладочном режиме). //! \~russian Возвращает константную ссылку на элемент по заданной строке и столбцу
//! \details
//! \~english No bounds checking is performed.
//! \~russian Проверка границ не выполняется.
inline const T & at(size_t row, size_t col) const { return mat[row * cols_ + col]; } inline const T & at(size_t row, size_t col) const { return mat[row * cols_ + col]; }
//! \~english Returns a reference to the element at the given Index.
//! \~russian Возвращает ссылку на элемент по заданному Index.
inline T & operator[](const Index & idx) { return element(idx.row, idx.col); }
//! \~english Returns a const reference to the element at the given Index.
//! \~russian Возвращает константную ссылку на элемент по заданному Index.
inline const T & operator[](const Index & idx) const { return element(idx.row, idx.col); }
//! \~english Returns a reference to the element at the given Index.
//! \~russian Возвращает ссылку на элемент по заданному Index.
inline T & element(const Index & idx) { return element(idx.row, idx.col); }
//! \~english Returns a const reference to the element at the given Index.
//! \~russian Возвращает константную ссылку на элемент по заданному Index.
inline const T & element(const Index & idx) const { return element(idx.row, idx.col); }
//! \~english Returns a const reference to the element at the given Index (bounds-checked only in debug).
//! \~russian Возвращает константную ссылку на элемент по заданному Index (проверка границ только в отладочном режиме).
inline const T & at(const Index & idx) const { return at(idx.row, idx.col); }
//! \~english Returns a proxy object for the row at the given index for modification. //! \~english Returns a proxy object for the row at the given index for modification.
//! \~russian Возвращает прокси-объект для строки по заданному индексу для модификации. //! \~russian Возвращает прокси-объект для строки по заданному индексу для модификации.
//! \sa row(), Col //! \sa row(), Col

View File

@@ -1431,3 +1431,61 @@ TEST_F(Vector2DTest, iostream_operator_works) {
// No assertion, just ensure it runs // No assertion, just ensure it runs
} }
#endif #endif
// ==================== INDEX STRUCTURE ACCESS TESTS ====================
TEST_F(Vector2DTest, operator_bracket_with_index_allows_read_access) {
PIVector2D<int>::Index idx = {5, 7};
int value = vec[idx];
int expected = vec.element(5, 7);
EXPECT_EQ(value, expected);
}
TEST_F(Vector2DTest, operator_bracket_with_index_allows_write_access) {
PIVector2D<int>::Index idx = {10, 15};
vec[idx] = 999;
EXPECT_EQ(vec.element(10, 15), 999);
}
TEST_F(Vector2DTest, operator_bracket_with_const_index_works) {
const auto & constVec = vec;
PIVector2D<int>::Index idx = {3, 12};
int value = constVec[idx];
int expected = vec.element(3, 12);
EXPECT_EQ(value, expected);
}
TEST_F(Vector2DTest, element_function_with_index_works) {
PIVector2D<int>::Index idx = {8, 20};
int value = vec.element(idx);
int expected = vec.element(8, 20);
EXPECT_EQ(value, expected);
}
TEST_F(Vector2DTest, element_function_with_index_modifies_value) {
PIVector2D<int>::Index idx = {12, 8};
vec.element(idx) = 555;
EXPECT_EQ(vec.element(12, 8), 555);
}
TEST_F(Vector2DTest, element_function_with_const_index_works) {
const auto & constVec = vec;
PIVector2D<int>::Index idx = {7, 5};
int value = constVec.element(idx);
int expected = vec.element(7, 5);
EXPECT_EQ(value, expected);
}
TEST_F(Vector2DTest, at_function_with_index_works) {
PIVector2D<int>::Index idx = {4, 11};
int value = vec.at(idx);
int expected = vec.at(4, 11);
EXPECT_EQ(value, expected);
}
TEST_F(Vector2DTest, index_structure_with_brace_initialization_works) {
vec[{0, 0}] = 100;
EXPECT_EQ(vec.element(0, 0), 100);
vec[{static_cast<ssize_t>(ROWS_COUNT_INIT - 1), static_cast<ssize_t>(COLS_COUNT_INIT - 1)}] = 200;
EXPECT_EQ(vec.element(ROWS_COUNT_INIT - 1, COLS_COUNT_INIT - 1), 200);
}