Skip to content

Commit 2a43c69

Browse files
authored
Moved symmetric matrix to datastructures (cda-tum#621)
## Description Moved the datastructure "Symmtric Matrix" from qmap to mqt-core as it might be useful also in other situations. ## Checklist: - [x] The pull request only contains commits that are related to it. - [x] I have added appropriate tests and documentation. - [x] I have made sure that all CI jobs on GitHub pass. - [x] The pull request introduces no new warnings and follows the project's style guidelines.
1 parent 4db8776 commit 2a43c69

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
#include <vector>
5+
/**
6+
* @brief Symmetric matrix class with same number of rows and columns that
7+
* allows access by row and column but uses less memory than a full matrix
8+
*/
9+
template <typename T> class SymmetricMatrix {
10+
private:
11+
std::vector<std::vector<T>> data;
12+
13+
public:
14+
// Constructors
15+
SymmetricMatrix() = default;
16+
explicit SymmetricMatrix(const size_t size) {
17+
data.resize(size);
18+
for (size_t i = 0; i < size; ++i) {
19+
data[i].resize(i + 1);
20+
}
21+
}
22+
23+
SymmetricMatrix(const size_t size, const T& value) {
24+
data.resize(size);
25+
for (size_t i = 0; i < size; ++i) {
26+
data[i].resize(i + 1, value);
27+
}
28+
}
29+
30+
[[nodiscard]] const T& operator()(const size_t row, const size_t col) const {
31+
if (row < col) {
32+
return data[col][row];
33+
}
34+
return data[row][col];
35+
}
36+
37+
[[nodiscard]] T& operator()(const size_t row, const size_t col) {
38+
if (row < col) {
39+
return data[col][row];
40+
}
41+
return data[row][col];
42+
}
43+
44+
[[nodiscard]] size_t size() const { return data.size(); }
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "datastructures/SymmetricMatrix.hpp"
2+
3+
#include <cstddef>
4+
#include <gtest/gtest.h>
5+
#include <string>
6+
7+
namespace qc {
8+
9+
TEST(SymmetricMatrix, Constructors) {
10+
SymmetricMatrix<int> const m(3);
11+
EXPECT_EQ(m.size(), 3);
12+
SymmetricMatrix<int> m2(3, 1);
13+
EXPECT_EQ(m2.size(), 3);
14+
for (size_t i = 0; i < m2.size(); ++i) {
15+
for (size_t j = 0; j <= i; ++j) {
16+
EXPECT_EQ(m2(i, j), 1);
17+
EXPECT_EQ(m2(j, i), 1);
18+
}
19+
}
20+
}
21+
22+
TEST(SymmetricMatrix, DifferentDataTypes) {
23+
SymmetricMatrix<double> const m(3);
24+
EXPECT_EQ(m.size(), 3);
25+
SymmetricMatrix<std::string> const m2(3, "1");
26+
EXPECT_EQ(m2.size(), 3);
27+
EXPECT_EQ(m2(0, 2), m2(2, 0));
28+
EXPECT_EQ(m2(0, 2), "1");
29+
SymmetricMatrix<char> const m3(3, '1');
30+
EXPECT_EQ(m3.size(), 3);
31+
EXPECT_EQ(m3(0, 1), m3(1, 0));
32+
EXPECT_EQ(m3(0, 1), '1');
33+
}
34+
35+
TEST(SymmetricMatrix, Assignment) {
36+
SymmetricMatrix<int> m(3);
37+
m(0, 1) = 1;
38+
m(1, 2) = 2;
39+
m(0, 2) = 3;
40+
EXPECT_EQ(m(0, 1), 1);
41+
EXPECT_EQ(m(1, 0), 1);
42+
EXPECT_EQ(m(1, 2), 2);
43+
EXPECT_EQ(m(2, 1), 2);
44+
EXPECT_EQ(m(0, 2), 3);
45+
EXPECT_EQ(m(2, 0), 3);
46+
}
47+
48+
} // namespace qc

0 commit comments

Comments
 (0)