diff --git a/include/ap/ap_vector.h b/include/ap/ap_vector.h index 06ba4f0..e613392 100644 --- a/include/ap/ap_vector.h +++ b/include/ap/ap_vector.h @@ -351,6 +351,26 @@ namespace ap return 0; } + // Operator[] for non-const objects + real& operator[](size_t index) { + switch (index) { + case 0: return x; + case 1: return y; + case 2: return z; + default: throw std::out_of_range("Index out of range"); + } + } + + // Operator[] for const objects + const real& operator[](size_t index) const { + switch (index) { + case 0: return x; + case 1: return y; + case 2: return z; + default: throw std::out_of_range("Index out of range"); + } + } + private: bool grFlag = 1; // global resize flag }; diff --git a/tests/ap_basicMath_tests/ap_basicMath_tests.cpp b/tests/ap_basicMath_tests/ap_basicMath_tests.cpp index 621b919..25bd7a8 100755 --- a/tests/ap_basicMath_tests/ap_basicMath_tests.cpp +++ b/tests/ap_basicMath_tests/ap_basicMath_tests.cpp @@ -564,6 +564,18 @@ TEST(ap_basicMath_tests, VECTOR_3_revers) { EXPECT_EQ(-a, b); } +TEST(ap_basicMath_tests, VECTOR_3_index) { + VECTOR_3 a; + + a[0] = 1.1; + a[1] = 2.2; + a[2] = 3.3; + + EXPECT_EQ(a[0], 1.1); + EXPECT_EQ(a[1], 2.2); + EXPECT_EQ(a[2], 3.3); +} + TEST(ap_basicMath_tests, MATRIX_3x3_set_from_std_vector) { vector w; w.assign(9, 3.3); @@ -1321,3 +1333,4 @@ TEST(ap_basicMath_tests, MATRIX_3x3_inversion) { EXPECT_DOUBLE_EQ(C.zy, 4); EXPECT_DOUBLE_EQ(C.zz, 1); } + \ No newline at end of file