<commit_before>/****************************************************************************
**
** Copyright (C) 1992-$THISYEAR$ $TROLLTECH$. All rights reserved.
**
** This file is part of $PRODUCT$.
**
** $CPP_LICENSE$
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "fileout.h"
#include "reporthandler.h"

#include <QFileInfo>
#include <QDir>

bool FileOut::dummy = false;
bool FileOut::diff = false;

FileOut::FileOut(QString n):
    name(n),
    stream(&tmp),
    isDone(false)
{}

static int* lcsLength(QList<QByteArray> a, QList<QByteArray> b) {
    const int height = a.size() + 1;
    const int width = b.size() + 1;

    int *res = new int[width * height];

    for (int row=0; row<height; row++) {
        res[width * row] = 0;
    }
    for (int col=0; col<width; col++) {
        res[col] = 0;
    }

    for (int row=1; row<height; row++) {
        for (int col=1; col<width; col++) {
            
            if (a[row-1] == b[col-1])
                res[width * row + col] = res[width * (row-1) + col-1] + 1;
            else
                res[width * row + col] = qMax(res[width * row     + col-1],
                                              res[width * (row-1) + col]);
        }
    }
    return res;
}

enum Type {Add, Delete, Unchanged};

struct Unit
{
    Unit(Type type, int pos) :
        type(type),
        start(pos),
        end(pos)
    {}

    Type type;
    int start;
    int end;

    void print(QList<QByteArray> a, QList<QByteArray> b){
        for (int i = start; i <= end; i++) {
            if (type == Unchanged) {
                //      printf("  %s\n", a[i].data());
            }
            else if(type == Add) {
                printf("> %s\n", b[i].data());
            } 
            else if (type == Delete) {
                printf("< %s\n", a[i].data());
            }    
        }
    }
};

static QList<Unit*> *unitAppend(QList<Unit*> *res, Type type, int pos)
{
    if (res == 0) {
        res = new QList<Unit*>;
        res->append(new Unit(type, pos));
        return res;
    }

    Unit *last = res->last();
    if (last->type == type) {
        last->end = pos;
    } else {
        res->append(new Unit(type, pos));
    }
    return res;
}

static QList<Unit*> *diffHelper(int *lcs, QList<QByteArray> a, QList<QByteArray> b, int row, int col) {
    if (row>0 && col>0 && (a[row-1] == b[col-1])) {
        return unitAppend(diffHelper(lcs, a, b, row-1, col-1), Unchanged, row-1);
    }
    else {
        int width = b.size()+1;<commit_msg>(split) added some logic to also show parts of code that is not changed, so you have some context.<commit_after>/****************************************************************************
**
** Copyright (C) 1992-$THISYEAR$ $TROLLTECH$. All rights reserved.
**
** This file is part of $PRODUCT$.
**
** $CPP_LICENSE$
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "fileout.h"
#include "reporthandler.h"

#include <QFileInfo>
#include <QDir>

bool FileOut::dummy = false;
bool FileOut::diff = false;

FileOut::FileOut(QString n):
    name(n),
    stream(&tmp),
    isDone(false)
{}

static int* lcsLength(QList<QByteArray> a, QList<QByteArray> b) {
    const int height = a.size() + 1;
    const int width = b.size() + 1;

    int *res = new int[width * height];

    for (int row=0; row<height; row++) {
        res[width * row] = 0;
    }
    for (int col=0; col<width; col++) {
        res[col] = 0;
    }

    for (int row=1; row<height; row++) {
        for (int col=1; col<width; col++) {
            
            if (a[row-1] == b[col-1])
                res[width * row + col] = res[width * (row-1) + col-1] + 1;
            else
                res[width * row + col] = qMax(res[width * row     + col-1],
                                              res[width * (row-1) + col]);
        }
    }
    return res;
}

enum Type {Add, Delete, Unchanged};

struct Unit
{
    Unit(Type type, int pos) :
        type(type),
        start(pos),
        end(pos)
    {}

    Type type;
    int start;
    int end;

    void print(QList<QByteArray> a, QList<QByteArray> b){
        {
            if (type == Unchanged) {
                if ((end - start) > 9) {
                    for (int i = start; i <= start+2; i++)
                        printf("  %s\n", a[i].data());
                    printf("=\n= %d more lines\n=\n", end - start - 6);
                    for (int i = end-2; i <= end; i++) 
                        printf("  %s\n", a[i].data());
                }
                else 
                    for (int i = start; i <= end; i++) 
                        printf("  %s\n", a[i].data());
            }
            else if(type == Add) {
                for (int i = start; i <= end; i++){
#ifdef Q_OS_LINUX
                    printf("\033[32m+ %s\033[0m\n", b[i].data());
#else
                    printf("+ %s\n", b[i].data());
#endif
                }
            } 
            else if (type == Delete) {
                for (int i = start; i <= end; i++) {
#ifdef Q_OS_LINUX
                    printf("\033[31m- %s\033[0m\n", a[i].data());
#else
                    printf("- %s\n", b[i].data());
#endif
                }
            }    
        }
    }
};

static QList<Unit*> *unitAppend(QList<Unit*> *res, Type type, int pos)
{
    if (res == 0) {
        res = new QList<Unit*>;
        res->append(new Unit(type, pos));
        return res;
    }

    Unit *last = res->last();
    if (last->type == type) {
        last->end = pos;
    } else {
        res->append(new Unit(type, pos));
    }
    return res;
}

static QList<Unit*> *diffHelper(int *lcs, QList<QByteArray> a, QList<QByteArray> b, int row, int col) {
    if (row>0 && col>0 && (a[row-1] == b[col-1])) {
        return unitAppend(diffHelper(lcs, a, b, row-1, col-1), Unchanged, row-1);
    }
    else {
        int width = b.size()+1;
{'fraction_non_alphanumeric': 0.20866078588612672, 'fraction_numerical': 0.012510024057738572, 'mean_word_length': 1.88837424733673, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6669573', 'n_tokens_mistral': 2114, 'n_tokens_neox': 2021, 'n_words': 555}