<commit_before>#include <functional>
#include <future>
#include "kernel.h"
#include "raster.h"
#include "symboltable.h"
#include "ilwisoperation.h"
#include "numericoperation.h"
#include "binarymathraster.h"

using namespace Ilwis;
using namespace BaseOperations;


OperationImplementation *BinaryMathRaster::create(quint64 metaid, const Ilwis::OperationExpression &expr)
{
    return new BinaryMathRaster( metaid, expr);
}

BinaryMathRaster::BinaryMathRaster() : _coveragecoverage(false)
{
}

BinaryMathRaster::BinaryMathRaster(quint64 metaid,const Ilwis::OperationExpression &expr) : NumericOperation(metaid, expr) , _coveragecoverage(false)
{
}

bool BinaryMathRaster::setOutput(ExecutionContext *ctx, SymbolTable& symTable) {
    if ( ctx) {
        QVariant value;
        value.setValue<IRasterCoverage>(_outputGC);
        ctx->addOutput(symTable,value,_outputGC->name(), itRASTER,_outputGC->source() );
    }
    return _outputGC.isValid();
}

bool BinaryMathRaster::executeCoverageNumber(ExecutionContext *ctx, SymbolTable& symTable) {

    auto binaryMath = [&](const Box3D<qint32> box ) -> bool {
        PixelIterator iterIn(_inputGC1, box);
        PixelIterator iterOut(_outputGC, Box3D<qint32>(box.size()));

        for_each(iterOut, iterOut.end(), [&](double& v){
            v = calc(*iterIn, _number1);
            ++iterIn;
        });
        return true;
    };

    ctx->_threaded = false;
    if (!OperationHelperRaster::execute(ctx, binaryMath, _outputGC))
            return false;


    return setOutput(ctx, symTable);

}

bool BinaryMathRaster::executeCoverageCoverage(ExecutionContext *ctx, SymbolTable& symTable) {
    std::function<bool(const Box3D<qint32>)> binaryMath = [&](const Box3D<qint32> box ) -> bool {
        PixelIterator iterIn1(_inputGC1, box);
        PixelIterator iterIn2(_inputGC2, box);
        PixelIterator iterOut(_outputGC, Box3D<qint32>(box.size()));

        for_each(iterOut, iterOut.end(), [&](double& v){
            v = calc(*iterIn1, *iterIn2);
            ++iterIn1;
            ++iterIn2;
        });
        return true;
    };

    if ( !_inputGC1->georeference()->isCompatible(_inputGC2->georeference())) {
        if (!OperationHelperRaster::resample(_inputGC1, _inputGC2, ctx)) {
            return ERROR2(ERR_COULD_NOT_CONVERT_2, TR("georeferences"), TR("common base"));
        }
    }
    // TODO:, research this exception
    // because of the swapping mechanism it is probably detrimental to use multithread here as blocks may continously be swapping
    if ( _inputGC1 == _inputGC2)
        ctx->_threaded = false;

    if (OperationHelperRaster::execute(ctx, binaryMath, _outputGC))
        return setOutput(ctx, symTable);

    return false;
}

bool BinaryMathRaster::execute(ExecutionContext *ctx, SymbolTable& symTable)
{
    if (_prepState == sNOTPREPARED)
        if((_prepState = prepare(ctx, symTable)) != sPREPARED)
            return false;

    if ( _coveragecoverage) {<commit_msg>disabled threading in binarymathraster for baby version<commit_after>#include <functional>
#include <future>
#include "kernel.h"
#include "raster.h"
#include "symboltable.h"
#include "ilwisoperation.h"
#include "numericoperation.h"
#include "binarymathraster.h"

using namespace Ilwis;
using namespace BaseOperations;


OperationImplementation *BinaryMathRaster::create(quint64 metaid, const Ilwis::OperationExpression &expr)
{
    return new BinaryMathRaster( metaid, expr);
}

BinaryMathRaster::BinaryMathRaster() : _coveragecoverage(false)
{
}

BinaryMathRaster::BinaryMathRaster(quint64 metaid,const Ilwis::OperationExpression &expr) : NumericOperation(metaid, expr) , _coveragecoverage(false)
{
}

bool BinaryMathRaster::setOutput(ExecutionContext *ctx, SymbolTable& symTable) {
    if ( ctx) {
        QVariant value;
        value.setValue<IRasterCoverage>(_outputGC);
        ctx->addOutput(symTable,value,_outputGC->name(), itRASTER,_outputGC->source() );
    }
    return _outputGC.isValid();
}

bool BinaryMathRaster::executeCoverageNumber(ExecutionContext *ctx, SymbolTable& symTable) {

    auto binaryMath = [&](const Box3D<qint32> box ) -> bool {
        PixelIterator iterIn(_inputGC1, box);
        PixelIterator iterOut(_outputGC, Box3D<qint32>(box.size()));

        for_each(iterOut, iterOut.end(), [&](double& v){
            v = calc(*iterIn, _number1);
            ++iterIn;
        });
        return true;
    };

    ctx->_threaded = false;
    if (!OperationHelperRaster::execute(ctx, binaryMath, _outputGC))
            return false;


    return setOutput(ctx, symTable);

}

bool BinaryMathRaster::executeCoverageCoverage(ExecutionContext *ctx, SymbolTable& symTable) {
    std::function<bool(const Box3D<qint32>)> binaryMath = [&](const Box3D<qint32> box ) -> bool {
        PixelIterator iterIn1(_inputGC1, box);
        PixelIterator iterIn2(_inputGC2, box);
//        PixelIterator iterOut(_outputGC, Box3D<qint32>(box.size()));
        PixelIterator iterOut(_outputGC, box);

        for_each(iterOut, iterOut.end(), [&](double& v){
            v = calc(*iterIn1, *iterIn2);
            ++iterIn1;
            ++iterIn2;
        });
        return true;
    };

    if ( !_inputGC1->georeference()->isCompatible(_inputGC2->georeference())) {
        if (!OperationHelperRaster::resample(_inputGC1, _inputGC2, ctx)) {
            return ERROR2(ERR_COULD_NOT_CONVERT_2, TR("georeferences"), TR("common base"));
        }
    }
    // TODO:, research this exception
    // because of the swapping mechanism it is probably detrimental to use multithread here as blocks may continously be swapping
//    if ( _inputGC1 == _inputGC2)
        ctx->_threaded = false;

    if (OperationHelperRaster::execute(ctx, binaryMath, _outputGC))
        return setOutput(ctx, symTable);

    return false;
}

bool BinaryMathRaster::execute(ExecutionContext *ctx, SymbolTable& symTable)
{
    if (_prepState == sNOTPREPARED)
        if((_prepState = prepare(ctx, symTable)) != sPREPARED)
            return false;

    if ( _coveragecoverage) {
{'fraction_non_alphanumeric': 0.12439873942610714, 'fraction_numerical': 0.012274008956709239, 'mean_word_length': 3.831730769230769, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1872805', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1866, 'n_words': 389}