Skip to content

Commit c575d29

Browse files
committed
Add save, read and == to AdjacencyMatrix class
1 parent de6fd63 commit c575d29

File tree

4 files changed

+212
-150
lines changed

4 files changed

+212
-150
lines changed

src/dsm/dsm.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
static constexpr uint8_t DSM_VERSION_MAJOR = 2;
88
static constexpr uint8_t DSM_VERSION_MINOR = 3;
9-
static constexpr uint8_t DSM_VERSION_PATCH = 21;
9+
static constexpr uint8_t DSM_VERSION_PATCH = 22;
1010

1111
static auto const DSM_VERSION =
1212
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);

src/dsm/headers/AdjacencyMatrix.hpp

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <algorithm>
55
#include <numeric>
6+
#include <string>
67
#include <unordered_map>
78
#include <vector>
89

@@ -38,58 +39,27 @@ namespace dsm {
3839
using reverse_iterator = std::vector<Id>::reverse_iterator;
3940
using const_reverse_iterator = std::vector<Id>::const_reverse_iterator;
4041

41-
AdjacencyMatrix()
42-
: m_rowOffsets{std::vector<Id>(2, 0)},
43-
m_columnIndices{},
44-
m_nRows{1},
45-
m_nCols{0} {}
46-
AdjacencyMatrix(const std::unordered_map<Id, std::unique_ptr<Street>>& streets)
47-
: m_rowOffsets(streets.size()), m_nRows{0}, m_nCols{0} {
48-
auto edgeFirst = streets.begin();
49-
auto edgeLast = streets.end();
50-
const auto size = streets.size();
51-
std::vector<Id> rowSizes(size, 0);
52-
std::vector<Id> colIndexes(size);
53-
std::for_each(edgeFirst,
54-
edgeLast,
55-
[this, &rowSizes, &colIndexes, i = 0](const auto& pair) -> void {
56-
auto row = pair.second->source();
57-
rowSizes[row + 1]++;
58-
if (row >= m_nRows)
59-
m_nRows = row + 1;
60-
});
61-
std::vector<Id> tempOffsets(m_nRows + 1, 0);
62-
tempOffsets[0] = 0;
63-
std::inclusive_scan(
64-
rowSizes.begin(), rowSizes.begin() + m_nRows + 1, tempOffsets.begin());
65-
m_rowOffsets = tempOffsets;
66-
67-
std::for_each(edgeFirst,
68-
edgeLast,
69-
[this, &tempOffsets, &colIndexes, i = 0](const auto& pair) -> void {
70-
auto row = pair.second->source();
71-
auto col = pair.second->target();
72-
if (col >= m_nCols)
73-
m_nCols = col + 1;
74-
colIndexes[tempOffsets[row]] = col;
75-
++tempOffsets[row];
76-
});
77-
/* m_columnIndices = std::move(colIndexes); */
78-
m_columnIndices = std::move(colIndexes);
79-
}
42+
AdjacencyMatrix();
43+
AdjacencyMatrix(std::string const& fileName);
44+
AdjacencyMatrix(const std::unordered_map<Id, std::unique_ptr<Street>>& streets);
45+
46+
bool operator==(const AdjacencyMatrix& other) const;
47+
bool operator()(Id row, Id col);
8048

8149
auto nRows() const { return m_nRows; }
8250
auto nCols() const { return m_nCols; }
8351

8452
void insert(Id row, Id col);
8553

8654
bool contains(Id row, Id col);
87-
bool operator()(Id row, Id col);
8855

8956
std::vector<Id> getRow(Id row);
9057
std::vector<Id> getCol(Id col);
9158

9259
std::vector<int> getInDegreeVector();
9360
std::vector<int> getOutDegreeVector();
61+
62+
void read(std::string const& fileName);
63+
void save(std::string const& fileName) const;
9464
};
9565
} // namespace dsm

src/dsm/sources/AdjacencyMatrix.cpp

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

22
#include "../headers/AdjacencyMatrix.hpp"
3+
#include "../utility/Logger.hpp"
4+
5+
#include <fstream>
36
#include <iostream>
47

58
namespace dsm {
@@ -10,6 +13,56 @@ namespace dsm {
1013
std::vector<Id> indices(const AdjacencyMatrix& adj) { return adj.m_columnIndices; }
1114
} // namespace test
1215

16+
/*********************************************************************************
17+
* CONSTRUCTORS
18+
**********************************************************************************/
19+
AdjacencyMatrix::AdjacencyMatrix()
20+
: m_rowOffsets{std::vector<Id>(2, 0)}, m_columnIndices{}, m_nRows{1}, m_nCols{0} {}
21+
AdjacencyMatrix::AdjacencyMatrix(std::string const& fileName) { read(fileName); }
22+
AdjacencyMatrix::AdjacencyMatrix(
23+
const std::unordered_map<Id, std::unique_ptr<Street>>& streets)
24+
: m_rowOffsets(streets.size()), m_nRows{0}, m_nCols{0} {
25+
auto edgeFirst = streets.begin();
26+
auto edgeLast = streets.end();
27+
const auto size = streets.size();
28+
std::vector<Id> rowSizes(size, 0);
29+
std::vector<Id> colIndexes(size);
30+
std::for_each(edgeFirst,
31+
edgeLast,
32+
[this, &rowSizes, &colIndexes, i = 0](const auto& pair) -> void {
33+
auto row = pair.second->source();
34+
rowSizes[row + 1]++;
35+
if (row >= m_nRows)
36+
m_nRows = row + 1;
37+
});
38+
std::vector<Id> tempOffsets(m_nRows + 1, 0);
39+
tempOffsets[0] = 0;
40+
std::inclusive_scan(
41+
rowSizes.begin(), rowSizes.begin() + m_nRows + 1, tempOffsets.begin());
42+
m_rowOffsets = tempOffsets;
43+
44+
std::for_each(edgeFirst,
45+
edgeLast,
46+
[this, &tempOffsets, &colIndexes, i = 0](const auto& pair) -> void {
47+
auto row = pair.second->source();
48+
auto col = pair.second->target();
49+
if (col >= m_nCols)
50+
m_nCols = col + 1;
51+
colIndexes[tempOffsets[row]] = col;
52+
++tempOffsets[row];
53+
});
54+
m_columnIndices = std::move(colIndexes);
55+
}
56+
/*********************************************************************************
57+
* OPERATORS
58+
**********************************************************************************/
59+
bool AdjacencyMatrix::operator==(const AdjacencyMatrix& other) const {
60+
return (m_rowOffsets == other.m_rowOffsets) &&
61+
(m_columnIndices == other.m_columnIndices) && (m_nRows == other.m_nRows) &&
62+
(m_nCols == other.m_nCols);
63+
}
64+
bool AdjacencyMatrix::operator()(Id row, Id col) { return contains(row, col); }
65+
1366
void AdjacencyMatrix::insert(Id row, Id col) {
1467
if (row > m_rowOffsets.size() - 2) {
1568
for (auto i = 0ul; i < row - m_rowOffsets.size() + 2; ++i) {
@@ -36,7 +89,6 @@ namespace dsm {
3689
iterator itLast = m_columnIndices.begin() + m_rowOffsets[row + 1];
3790
return std::find(itFirst, itLast, col) != itLast;
3891
}
39-
bool AdjacencyMatrix::operator()(Id row, Id col) { return contains(row, col); }
4092

4193
std::vector<Id> AdjacencyMatrix::getRow(Id row) {
4294
const auto lowerOffset = m_rowOffsets[row];
@@ -77,4 +129,34 @@ namespace dsm {
77129
return degVector;
78130
}
79131

132+
void AdjacencyMatrix::read(std::string const& fileName) {
133+
std::ifstream inStream(fileName, std::ios::binary);
134+
if (!inStream.is_open()) {
135+
Logger::error(std::format("Could not open file {} for reading.", fileName));
136+
}
137+
inStream.read(reinterpret_cast<char*>(&m_nRows), sizeof(size_t));
138+
inStream.read(reinterpret_cast<char*>(&m_nCols), sizeof(size_t));
139+
m_rowOffsets.resize(m_nRows + 1);
140+
m_columnIndices.resize(m_nCols);
141+
inStream.read(reinterpret_cast<char*>(m_rowOffsets.data()),
142+
m_rowOffsets.size() * sizeof(Id));
143+
inStream.read(reinterpret_cast<char*>(m_columnIndices.data()),
144+
m_columnIndices.size() * sizeof(Id));
145+
inStream.close();
146+
}
147+
148+
void AdjacencyMatrix::save(std::string const& fileName) const {
149+
std::ofstream outStream(fileName, std::ios::binary);
150+
if (!outStream.is_open()) {
151+
Logger::error(std::format("Could not open file {} for writing.", fileName));
152+
}
153+
outStream.write(reinterpret_cast<const char*>(&m_nRows), sizeof(size_t));
154+
outStream.write(reinterpret_cast<const char*>(&m_nCols), sizeof(size_t));
155+
outStream.write(reinterpret_cast<const char*>(m_rowOffsets.data()),
156+
m_rowOffsets.size() * sizeof(Id));
157+
outStream.write(reinterpret_cast<const char*>(m_columnIndices.data()),
158+
m_columnIndices.size() * sizeof(Id));
159+
outStream.close();
160+
}
161+
80162
} // namespace dsm

0 commit comments

Comments
 (0)