Skip to content

Commit

Permalink
Added support for foreign indices in sparse binary matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
Pseudomanifold committed Jun 2, 2017
1 parent f1acdf6 commit 2187466
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion math/SparseMatrix.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <algorithm>
#include <set>
#include <stdexcept>
#include <vector>

namespace aleph
Expand All @@ -19,9 +20,9 @@ public:
using IndexType = T;
using ColumnType = std::set<IndexType>;
using ColumnsType = std::vector<ColumnType>;
using IndexMap = std::vector<IndexType>;
using size_type = typename ColumnType::size_type;


SparseBinaryMatrix( IndexType columns )
{
_columns.resize( size_type( columns ) );
Expand Down Expand Up @@ -59,6 +60,27 @@ public:
std::copy( c.begin(), c.end(), result );
}

/** Sets row/column indices */
template <class InputIterator> void setIndices( InputIterator begin, InputIterator end )
{
_indexMap.assign( begin, end );
if( _indexMap.size() != this->numColumns() )
throw std::length_error( "Number of indices does not coincide with number of columns" );
}

/**
Gets row/class index of a given column. If no foreign indices have
been set by the client, this function just returns its input.
*/

IndexType getIndex( IndexType column ) const
{
if( _indexMap.empty() )
return column;
else
return _indexMap.at( column );
}

/** Returns number of non-zero entries in a given column */
std::size_t numEntries( IndexType column ) const
{
Expand All @@ -73,6 +95,10 @@ public:

private:

// The index map maps a local row/column index in [0..n-1] to another
// index that has been specified by the client. This is used whenever
// indices occur that may not be contiguous.
IndexMap _indexMap;
ColumnsType _columns;
};

Expand Down

0 comments on commit 2187466

Please sign in to comment.