11
22#include " ../headers/AdjacencyMatrix.hpp"
3+ #include " ../utility/Logger.hpp"
4+
5+ #include < fstream>
36#include < iostream>
47
58namespace 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