42 #ifndef __Teuchos_MatrixMarket_Raw_Graph_Adder_hpp
43 #define __Teuchos_MatrixMarket_Raw_Graph_Adder_hpp
46 #include "Teuchos_ArrayRCP.hpp"
47 #include "Teuchos_CommHelpers.hpp"
49 #include "Teuchos_MatrixMarket_Banner.hpp"
50 #include "Teuchos_MatrixMarket_CoordDataReader.hpp"
61 namespace MatrixMarket {
85 template<
class Ordinal>
96 rowIndex_ (i), colIndex_ (j) {}
100 return rowIndex_ == rhs.rowIndex_ && colIndex_ == rhs.colIndex_;
105 return ! (*
this == rhs);
110 if (rowIndex_ < rhs.rowIndex_)
112 else if (rowIndex_ > rhs.rowIndex_)
115 return colIndex_ < rhs.colIndex_;
126 Ordinal rowIndex_, colIndex_;
133 template<
class Ordinal>
135 operator<< (std::ostream& out, const GraphElement<Ordinal>& elt)
137 out << elt.rowIndex () <<
" " << elt.colIndex ();
160 template<
class Ordinal>
163 typedef Ordinal index_type;
165 typedef typename std::vector<element_type>::size_type size_type;
180 expectedNumEntries_(0),
206 const Ordinal expectedNumCols,
207 const Ordinal expectedNumEntries,
208 const bool tolerant=
false,
209 const bool debug=
false) :
210 expectedNumRows_(expectedNumRows),
211 expectedNumCols_(expectedNumCols),
212 expectedNumEntries_(expectedNumEntries),
216 tolerant_ (tolerant),
243 const bool countAgainstTotal=
true)
246 const bool indexPairOutOfRange = i < 1 || j < 1 ||
247 i > expectedNumRows_ || j > expectedNumCols_;
250 std::invalid_argument,
"Graph is " << expectedNumRows_ <<
" x "
251 << expectedNumCols_ <<
", so entry A(" << i <<
"," << j
252 <<
") is out of range.");
253 if (countAgainstTotal) {
255 std::invalid_argument,
"Cannot add entry A(" << i <<
"," << j
256 <<
") to graph; already have expected number "
257 "of entries " << expectedNumEntries_ <<
".");
268 seenNumRows_ = std::max (seenNumRows_, i);
269 seenNumCols_ = std::max (seenNumCols_, j);
270 if (countAgainstTotal) {
292 print (std::ostream& out,
const bool doMerge,
const bool replace=
false)
296 (replace, std::logic_error,
"replace = true not implemented!");
300 std::sort (elts_.begin(), elts_.end());
303 typedef std::ostream_iterator<element_type> iter_type;
304 std::copy (elts_.begin(), elts_.end(), iter_type (out,
"\n"));
320 std::pair<size_type, size_type>
323 typedef typename std::vector<element_type>::iterator iter_type;
332 std::sort (elts_.begin(), elts_.end());
336 it = std::unique(elts_.begin(), elts_.end());
337 size_type numUnique = std::distance(elts_.begin(),it);
338 const size_type numRemoved = elts_.size() - numUnique;
339 elts_.resize( std::distance(elts_.begin(),it) );
340 elts_.resize (numUnique);
341 return std::make_pair (numUnique, numRemoved);
377 size_type& numRemovedElts,
384 std::pair<size_type, size_type> mergeResult =
merge();
391 const Ordinal nrows = tolerant_ ? seenNumRows_ : expectedNumRows_;
400 typedef typename std::vector<element_type>::const_iterator iter_type;
402 for (iter_type it = elts_.begin(); it != elts_.end(); ++it) {
403 const Ordinal i = it->rowIndex ();
404 const Ordinal j = it->colIndex ();
407 "current graph entry's row index " << i <<
" is less then what "
408 "should be the current row index lower bound " << curRow <<
".");
409 for (Ordinal k = curRow+1; k <= i; ++k) {
415 static_cast<size_t> (curInd) >= elts_.size (),
416 std::logic_error,
"The current index " << curInd <<
" into ind "
417 "is >= the number of matrix entries " << elts_.size ()
422 for (Ordinal k = curRow+1; k <= nrows; ++k) {
431 numUniqueElts = mergeResult.first;
432 numRemovedElts = mergeResult.second;
451 const Ordinal
numRows()
const {
return seenNumRows_; }
456 const Ordinal
numCols()
const {
return seenNumCols_; }
459 Ordinal expectedNumRows_, expectedNumCols_, expectedNumEntries_;
460 Ordinal seenNumRows_, seenNumCols_, seenNumEntries_;
465 std::vector<element_type> elts_;
471 #endif // #ifndef __Teuchos_MatrixMarket_Raw_Graph_Adder_hpp
bool operator==(const GraphElement &rhs)
Compare row and column indices.
To be used with Checker for "raw" sparse matrix input.
GraphAdder()
Default constructor.
bool operator<(const GraphElement &rhs) const
Lexicographic order first by row index, then by column index.
void clear()
Clear all the added graph entries and reset metadata.
bool operator!=(const GraphElement &rhs)
Compare row and column indices.
const std::vector< element_type > & getEntries() const
A temporary const view of the entries of the graph.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
const Ordinal numRows() const
Computed number of rows.
const Ordinal numCols() const
Computed number of columns.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
GraphElement()
Default constructor: an invalid entry of the graph.
Templated Parameter List class.
Ordinal colIndex() const
Column index (zero-based) of this GraphElement.
GraphElement(const Ordinal i, const Ordinal j)
Create a sparse graph entry at (i,j).
void operator()(const Ordinal i, const Ordinal j, const bool countAgainstTotal=true)
Add an entry to the sparse graph.
This structure defines some basic traits for the ordinal field type.
void print(std::ostream &out, const bool doMerge, const bool replace=false)
Print the sparse graph data.
GraphAdder(const Ordinal expectedNumRows, const Ordinal expectedNumCols, const Ordinal expectedNumEntries, const bool tolerant=false, const bool debug=false)
Standard constructor.
Ordinal rowIndex() const
Row index (zero-based) of this GraphElement.
void mergeAndConvertToCSR(size_type &numUniqueElts, size_type &numRemovedElts, Teuchos::ArrayRCP< Ordinal > &rowptr, Teuchos::ArrayRCP< Ordinal > &colind)
Merge duplicate elements and convert to zero-based CSR.
std::pair< size_type, size_type > merge()
Merge duplicate elements.
Stores one entry of a sparse graph.
Reference-counted smart pointer for managing arrays.