<commit_before>// Copyright (C) 2016 xaizek <<EMAIL>>
//
// This file is part of uncov.
//
// uncov is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// uncov is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with uncov.  If not, see <http://www.gnu.org/licenses/>.

#include "FileComparator.hpp"

#include <boost/multi_array.hpp>

#include <cstddef>

#include <deque>
#include <string>
#include <vector>

FileComparator::FileComparator(const std::vector<std::string> &o,
                               const std::vector<int> &oCov,
                               const std::vector<std::string> &n,
                               const std::vector<int> &nCov,
                               bool considerHits)
{
    valid = (o.size() == oCov.size() && n.size() == nCov.size());
    if (!valid) {
        equal = false;
        return;
    }

    boost::multi_array<int, 2> d(boost::extents[o.size() + 1U][n.size() + 1U]);

    // Modified edit distance finding.
    using size_type = std::vector<std::string>::size_type;
    for (size_type i = 0U, nf = o.size(); i <= nf; ++i) {
        for (size_type j = 0U, ns = n.size(); j <= ns; ++j) {
            if (i == 0U) {
                d[i][j] = j;
            } else if (j == 0U) {
                d[i][j] = i;
            } else {
                d[i][j] = std::min(d[i - 1U][j] + 1, d[i][j - 1U] + 1);
                if (o[i - 1U] == n[j - 1U]) {
                    d[i][j] = std::min(d[i - 1U][j - 1U], d[i][j]);
                }
            }
        }
    }

    std::size_t identicalLines = 0;

    auto foldIdentical = [this, &identicalLines](bool last) {
        if (identicalLines > 4) {
            int startContext = last ? 0 : 1;
            int endContext = identicalLines == diffSequence.size() ? 0 : 1;
            int context = startContext + endContext;

            diffSequence.erase(diffSequence.cbegin() + startContext,
                               diffSequence.cbegin() +
                               (identicalLines - endContext));
            diffSequence.emplace(diffSequence.cbegin() + startContext,
                                 DiffLineType::Note,
                                 std::to_string(identicalLines - context) +
                                 " identical lines folded", -1, -1);
        }
        identicalLines = 0;<commit_msg>Don't say that we fold "identical lines"<commit_after>// Copyright (C) 2016 xaizek <<EMAIL>>
//
// This file is part of uncov.
//
// uncov is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// uncov is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with uncov.  If not, see <http://www.gnu.org/licenses/>.

#include "FileComparator.hpp"

#include <boost/multi_array.hpp>

#include <cstddef>

#include <deque>
#include <string>
#include <vector>

FileComparator::FileComparator(const std::vector<std::string> &o,
                               const std::vector<int> &oCov,
                               const std::vector<std::string> &n,
                               const std::vector<int> &nCov,
                               bool considerHits)
{
    valid = (o.size() == oCov.size() && n.size() == nCov.size());
    if (!valid) {
        equal = false;
        return;
    }

    boost::multi_array<int, 2> d(boost::extents[o.size() + 1U][n.size() + 1U]);

    // Modified edit distance finding.
    using size_type = std::vector<std::string>::size_type;
    for (size_type i = 0U, nf = o.size(); i <= nf; ++i) {
        for (size_type j = 0U, ns = n.size(); j <= ns; ++j) {
            if (i == 0U) {
                d[i][j] = j;
            } else if (j == 0U) {
                d[i][j] = i;
            } else {
                d[i][j] = std::min(d[i - 1U][j] + 1, d[i][j - 1U] + 1);
                if (o[i - 1U] == n[j - 1U]) {
                    d[i][j] = std::min(d[i - 1U][j - 1U], d[i][j]);
                }
            }
        }
    }

    std::size_t identicalLines = 0;

    auto foldIdentical = [this, &identicalLines](bool last) {
        if (identicalLines > 4) {
            int startContext = last ? 0 : 1;
            int endContext = identicalLines == diffSequence.size() ? 0 : 1;
            int context = startContext + endContext;

            diffSequence.erase(diffSequence.cbegin() + startContext,
                               diffSequence.cbegin() +
                               (identicalLines - endContext));
            diffSequence.emplace(diffSequence.cbegin() + startContext,
                                 DiffLineType::Note,
                                 std::to_string(identicalLines - context) +
                                 " lines folded", -1, -1);
        }
        identicalLines = 0;
{'fraction_non_alphanumeric': 0.13334520206515935, 'fraction_numerical': 0.010325796688623821, 'mean_word_length': 1.7811881188118812, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '623998', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1658, 'n_words': 502}