diff --git a/libs/main/containers/pivector2d.h b/libs/main/containers/pivector2d.h index 15e38c2c..d6b49e22 100644 --- a/libs/main/containers/pivector2d.h +++ b/libs/main/containers/pivector2d.h @@ -517,8 +517,8 @@ public: //! \~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 В релизной сборке проверка границ не выполняется; используйте с осторожностью. + //! \~english No bounds checking is performed; use with caution. + //! \~russian Проверка границ не выполняется; используйте с осторожностью. //! \sa PIVector::operator[] inline T & operator[](size_t index) { return (*p_)[this->st_ + index]; } @@ -669,10 +669,33 @@ public: //! \~russian Возвращает константную ссылку на элемент по заданной строке и столбцу. 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). - //! \~russian Возвращает константную ссылку на элемент по заданной строке и столбцу (проверка границ только в отладочном режиме). + //! \~english Returns a const reference to the element at the given row and column + //! \~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]; } + //! \~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. //! \~russian Возвращает прокси-объект для строки по заданному индексу для модификации. //! \sa row(), Col diff --git a/tests/math/testpivector2d.cpp b/tests/math/testpivector2d.cpp index c12aa631..d680c989 100644 --- a/tests/math/testpivector2d.cpp +++ b/tests/math/testpivector2d.cpp @@ -1431,3 +1431,61 @@ TEST_F(Vector2DTest, iostream_operator_works) { // No assertion, just ensure it runs } #endif + +// ==================== INDEX STRUCTURE ACCESS TESTS ==================== +TEST_F(Vector2DTest, operator_bracket_with_index_allows_read_access) { + PIVector2D::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::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::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::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::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::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::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(ROWS_COUNT_INIT - 1), static_cast(COLS_COUNT_INIT - 1)}] = 200; + EXPECT_EQ(vec.element(ROWS_COUNT_INIT - 1, COLS_COUNT_INIT - 1), 200); +}