Esempio n. 1
0
TFiniteAutomaton& TFiniteAutomaton::Enumerate() {
    vector<size_t> newIndexes(Size());
    size_t newIndex = 0;

    vector<bool> visited(Size());  // TODO: maybe vector<char> ? Compare performance
    visited[StartState] = true;
    std::deque<size_t> que;
    que.push_front(StartState);
    while (!que.empty()) {
        size_t state = que.front();
        que.pop_front();
        newIndexes[state] = newIndex++;
        const TTransitions& tran = States[state].Transitions;
        TTransitions::const_iterator it = tran.find(EPSILON);
        for (TTransitions::const_iterator it = tran.begin(), end = tran.end(); it != end; ++it)
            for (TStateIdSet::const_iterator stIt = it->second.begin(), stEnd = it->second.end(); stIt != stEnd; ++stIt)
                if (!visited[*stIt]) {
                    visited[*stIt] = true;
                    que.push_back(*stIt);
                }
    }

    // and now enumerate not visited states
    for (size_t i = 0, size = newIndexes.size(); i < size; ++i) {
        if (!visited[i])
            newIndexes[i] == newIndex++;
    }

    vector<TState> newStates(Size());
    for (size_t i = 0, size = Size(); i < size; ++i) {
        TState& newState = newStates[newIndexes[i]];
        newState.Swap(States[i]);
        TTransitions& tran = newState.Transitions;
        for (TTransitions::iterator trIt = tran.begin(), trEnd = tran.end(); trIt != trEnd; ++trIt) {
            // TODO: make incrementing iterator
            TStateIdSet newSet;
            for (TStateIdSet::const_iterator stIt = trIt->second.begin(), stEnd = trIt->second.end(); stIt != stEnd; ++stIt)
                newSet.insert(newIndexes[*stIt]);
            trIt->second.swap(newSet);
        }
    }

    States.swap(newStates);

    StartState = newIndexes[StartState];
    TStateIdSet newFinals;
    for (TStateIdSet::const_iterator it = FinalStates.begin(), end = FinalStates.end(); it != end; ++it)
        newFinals.insert(newIndexes[*it]);
    FinalStates.swap(newFinals);

    return *this;
}
void InputMatrix::sortMatrix() {
    std::vector<int> counts(_r2Count);
    for(auto i=0; i<_rowsCount; ++i) {
        counts[_r2Matrix[i]] += 1;
    }

    std::vector<std::tuple<int, int>> sortedCounts(_r2Count);
    for(auto i=0; i<_r2Count; ++i) {
        sortedCounts[i] = std::make_tuple(i, counts[i]);
    }

    std::sort(sortedCounts.begin(), sortedCounts.end(),
              [](std::tuple<int, int> a, std::tuple<int, int> b){ return std::get<1>(a) > std::get<1>(b); });

    std::vector<int> indexes(_r2Count);
    int currentIndex = 0;
    for(auto i=0; i<_r2Count; ++i) {
        indexes[std::get<0>(sortedCounts[i])] = currentIndex;
        currentIndex += std::get<1>(sortedCounts[i]);
    }

    std::vector<int> newIndexes(_rowsCount);
    for(auto i=0; i<_rowsCount; ++i) {
        newIndexes[i] = indexes[_r2Matrix[i]];
        indexes[_r2Matrix[i]] += 1;
    }

    auto oldQMatrix = _qMatrix;
    auto oldRMatrix = _rMatrix;
    auto oldR2Matrix = _r2Matrix;

    _qMatrix = new int[_rowsCount * _qColsCount];
    _rMatrix = new int[_rowsCount * _rColsCount];
    _r2Matrix = new int[_rowsCount];

    for(auto i=0; i<_rowsCount; ++i) {
        for(auto j=0; j<_qColsCount; ++j) {
            _qMatrix[newIndexes[i]*_qColsCount + j] = oldQMatrix[i*_qColsCount + j];
        }
        for(auto j=0; j<_rColsCount; ++j) {
            _rMatrix[newIndexes[i]*_rColsCount + j] = oldRMatrix[i*_rColsCount + j];
        }
        _r2Matrix[newIndexes[i]] = oldR2Matrix[i];
    }

    delete[] oldQMatrix;
    delete[] oldRMatrix;
    delete[] oldR2Matrix;
}