Skip to content

Commit

Permalink
Get/set vector value by index
Browse files Browse the repository at this point in the history
  • Loading branch information
jacmie committed Nov 16, 2024
1 parent 8b321b4 commit 77cd3d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions include/ap/ap_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
13 changes: 13 additions & 0 deletions tests/ap_basicMath_tests/ap_basicMath_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,18 @@ TEST(ap_basicMath_tests, VECTOR_3_revers) {
EXPECT_EQ(-a, b);
}

TEST(ap_basicMath_tests, VECTOR_3_index) {
VECTOR_3 <double> 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 <double> w;
w.assign(9, 3.3);
Expand Down Expand Up @@ -1321,3 +1333,4 @@ TEST(ap_basicMath_tests, MATRIX_3x3_inversion) {
EXPECT_DOUBLE_EQ(C.zy, 4);
EXPECT_DOUBLE_EQ(C.zz, 1);
}

0 comments on commit 77cd3d6

Please sign in to comment.