From eeddadc0968e77c3620c85dfe80acce7280b2af8 Mon Sep 17 00:00:00 2001 From: dehnert Date: Wed, 21 Nov 2012 18:49:46 +0100 Subject: [PATCH] The columns of non-zero elements of a single row in a sparse matrix can now be iterated using an iterator. These iterator functionality is now used by the computation of the backwards transitions. --- src/models/backward_transitions.h | 28 +++++++++++++++++----------- src/sparse/static_sparse_matrix.h | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/models/backward_transitions.h b/src/models/backward_transitions.h index 8e6fd3bd7..8609f1c3a 100644 --- a/src/models/backward_transitions.h +++ b/src/models/backward_transitions.h @@ -47,8 +47,8 @@ public: uint_fast64_t* row_indications = transitionMatrix->getRowIndicationsPointer(); uint_fast64_t* column_indications = transitionMatrix->getColumnIndicationsPointer(); for (uint_fast64_t i = 0; i <= numberOfStates; i++) { - for (uint_fast64_t j = row_indications[i]; j < row_indications[i + 1]; j++) { - this->state_indices_list[column_indications[j] + 1]++; + for (auto rowIt = transitionMatrix->beginConstColumnNoDiagIterator(i); rowIt != transitionMatrix->endConstColumnNoDiagIterator(i); ++rowIt) { + this->state_indices_list[*rowIt + 1]++; } } @@ -70,17 +70,10 @@ public: // Now we are ready to actually fill in the list of predecessors for // every state. Again, we start by considering all but the last row. for (uint_fast64_t i = 0; i <= numberOfStates; i++) { - for (uint_fast64_t j = row_indications[i]; j < row_indications[i + 1]; j++) { - this->predecessor_list[next_state_index_list[column_indications[j]]++] = i; + for (auto rowIt = transitionMatrix->beginConstColumnNoDiagIterator(i); rowIt != transitionMatrix->endConstColumnNoDiagIterator(i); ++rowIt) { + this->predecessor_list[next_state_index_list[*rowIt]++] = i; } } - - for (auto it = beginStatePredecessorIterator(0); it < endStatePredecessorIterator(0); it++) { - std::cout << "state 0 pred " << *it << std::endl; - } - for (auto it = beginStatePredecessorIterator(1); it < endStatePredecessorIterator(1); it++) { - std::cout << "state 1 pred " << *it << std::endl; - } } ~BackwardTransitions() { @@ -92,10 +85,23 @@ public: } } + /*! + * Returns an iterator to the predecessors of the given states. + * @param state The state for which to get the predecessor iterator. + * @return An iterator to the predecessors of the given states. + */ state_predecessor_iterator beginStatePredecessorIterator(uint_fast64_t state) const { return this->predecessor_list + this->state_indices_list[state]; } + + /*! + * Returns an iterator referring to the element after the predecessors of + * the given state. + * @param row The state for which to get the iterator. + * @return An iterator referring to the element after the predecessors of + * the given state. + */ state_predecessor_iterator endStatePredecessorIterator(uint_fast64_t state) const { return this->predecessor_list + this->state_indices_list[state + 1]; } diff --git a/src/sparse/static_sparse_matrix.h b/src/sparse/static_sparse_matrix.h index 19f3457f0..b778fd352 100644 --- a/src/sparse/static_sparse_matrix.h +++ b/src/sparse/static_sparse_matrix.h @@ -30,6 +30,13 @@ namespace sparse { template class StaticSparseMatrix { public: + + /*! + * If we only want to iterate over the columns of the non-zero entries of + * a row, we can simply iterate over the array (part) itself. + */ + typedef const uint_fast64_t * const constIndexIterator; + /*! * An enum representing the internal state of the Matrix. * After creating the Matrix using the Constructor, the Object is in state UnInitialized. After calling initialize(), that state changes to Initialized and after all entries have been entered and finalize() has been called, to ReadReady. @@ -611,6 +618,26 @@ public: return size; } + /*! + * Returns an iterator to the columns of the non-zero entries of the given + * row that are not on the diagonal. + * @param row The row whose columns the iterator will return. + * @return An iterator to the columns of the non-zero entries of the given + * row that are not on the diagonal. + */ + constIndexIterator beginConstColumnNoDiagIterator(uint_fast64_t row) const { + return this->columnIndications + this->rowIndications[row]; + } + + /*! + * Returns an iterator referring to the element after the given row. + * @param row The row for which the iterator should point to the past-the-end + * element. + */ + constIndexIterator endConstColumnNoDiagIterator(uint_fast64_t row) const { + return this->columnIndications + this->rowIndications[row + 1]; + } + private: /*!