<commit_before>/*
 * File:   BN.cpp
 * Author: knst
 *
 * Created on 27 Май 2010 г., 3:16
 */

#include <cstdio>
#include <cstdlib>
#include <exception>
#include <stdexcept>
#include <iostream>
#include <time.h>

#include "BN.h"
#include "additional.h"

void BN::InitMemory(int type)
{
    switch(type) {
    case 1: {
        bt zero = 0;
        for(size_t i = 0; i < rbc; ++i)
            ba[i] = ~zero;
        break;
    }
    case -1:
        for(size_t i = 0; i < rbc; ++i)
            ba[i] = rand() % bsize;
        Norm();
        break;

    default:
        throw "Unknow type (BN::BN)";
    }
}

int BN::Norm()
{
    for(rbc = ba.size() - 1; rbc > 0 && ba[rbc] == 0; rbc--);
        rbc++;
    return rbc;
}

BN::BN()
{
    rbc = 1;
    ba.resize(2);
}

BN::BN(uint64_t basecount, int type)
{
    ba.resize(basecount + 1);
    if (type == 0 || type == 2) {
        rbc = 1;
    } else if (type == 1 || type == -1) {
        rbc = basecount;
        InitMemory(type);
    } else
        throw std::invalid_argument("BN constructor: invalid type " + to_string(type));
}

BN::BN(uint64_t x)
{
    rbc = (sizeof(uint64_t) + bz - 1) / bz;
    ba.resize(rbc + 1);
    for(int i = 0; i < rbc; i++, x >>= bz8)
        ba[i] = static_cast<bt>(x);
    Norm();
}

BN::BN(const BN& bn)
{
    if(this == &bn)
        throw "constructor copy: Tak ne Byvaet!!! Constructor copii ISTCHO ne sosdannogo classa!!";
    rbc = bn.rbc;
    ba.resize(rbc + 1);
    for (size_t i = 0; i < rbc; ++i)
        ba[i] = bn.ba[i];
}

BN::BN(const BN& bn, int start, int count) {
    if(this == &bn)
        throw "constructor copy with param: Tak ne Byvaet!!! Constructor copii ISTCHO ne sosdannogo classa!!";
    if(count == -1)
        count = bn.rbc;
    if(count <= 0)
        throw "constructor copy with param: count byte is not positive";
    if(start < 0)
        throw "constructor copy with param: start < 0";


    rbc = count;
    ba.resize(rbc + 1);
    for(int i = 0; i < count && i + start < bn.rbc; i++)
        ba[i] = bn.ba[i + start];

    int bbstart = max<size_t>(0, bn.rbc - start);
    for(int i = bbstart; i < ba.size(); i++)
        ba[i] = 0;
    Norm();
}

BN::BN(const string &str,const int &status)
{
        if(status==1)
        {
                BN bn(1,0);
                BN bn10(10);
                for(unsigned int i=0;i<str.size();i++)
                {
                        if(str[i]<'0'||str[i]>'9')
                                continue;
                        BN bnc(str[i]-'0');
                        bn=bn*bn10+bnc;
                }
                rbc=bn.rbc;
                ba.resize(rbc + 1);
                for(int i=0;i<rbc;i++)
                        ba[i]=bn.ba[i];

                return;
        }

        int length = str.size();
        rbc=(length+bz*2-1)/(bz*2);
        ba.resize(rbc + 1);
        InitMemory(0);
        bt z;
        for(int i=0;i<length;i++)
        {
                if(str[i]>='A'&&str[i]<='F')
                {
                        int x=(length-i-1)/(bz*2);
                        ba[x]<<=4;
                        z=str[i]-'A'+10;
                        ba[x]|=z;
                }
                if(str[i]>='a'&&str[i]<='f')
                {
                        int x=(length-i-1)/(bz*2);
                        ba[x]<<=4;
                        z=str[i]-'a'+10;
                        ba[x]|=z;
                }
                if(str[i]>='0'&&str[i]<='9')
                {
                        int x=(length-i-1)/(bz*2);
                        ba[x]<<=4;
                        z=str[i]-'0';
                        ba[x]|=z;
                }
        }
        Norm();
}

BN & BN::operator = (const BN&bn)
{
    if(this == &bn)
        return *this;
    if(ba.size() < bn.rbc) {
        rbc = bn.rbc;
        ba.resize(rbc + 1);
    } else {
        for(int i = bn.rbc; i < rbc; i++)
            ba[i] = 0;
        rbc = bn.rbc;
    }
    for (size_t i = 0; i < rbc; ++i)
        ba[i] = bn.ba[i];
    return *this;
}

BN BN::operator + (const BN&bn)const {
    int result_len = max(rbc, bn.rbc);
    BN result(result_len + 1, 2);

    bt2 res = 0;
    int pos = 0;
    int m = min(rbc, bn.rbc);
    for(; pos < m; pos++) {
        res += (bt2) ba[pos] + (bt2) bn.ba[pos];
        result.ba[pos] = res;
        res >>= bz8;
    }

    for(; pos < rbc; pos++) {
        res += (bt2) ba[pos];
        result.ba[pos] = res;
        res >>= bz8;<commit_msg>Optimization: replace by bytes coping data to fast copy of vector.<commit_after>/*
 * File:   BN.cpp
 * Author: knst
 *
 * Created on 27 Май 2010 г., 3:16
 */

#include <cstdio>
#include <cstdlib>
#include <exception>
#include <stdexcept>
#include <iostream>
#include <time.h>

#include "BN.h"
#include "additional.h"

void BN::InitMemory(int type)
{
    switch(type) {
    case 1: {
        bt zero = 0;
        for(size_t i = 0; i < rbc; ++i)
            ba[i] = ~zero;
        break;
    }
    case -1:
        for(size_t i = 0; i < rbc; ++i)
            ba[i] = rand() % bsize;
        Norm();
        break;

    default:
        throw "Unknow type (BN::BN)";
    }
}

int BN::Norm()
{
    for(rbc = ba.size() - 1; rbc > 0 && ba[rbc] == 0; rbc--);
        rbc++;
    return rbc;
}

BN::BN()
{
    rbc = 1;
    ba.resize(2);
}

BN::BN(uint64_t basecount, int type)
{
    ba.resize(basecount + 1);
    if (type == 0 || type == 2) {
        rbc = 1;
    } else if (type == 1 || type == -1) {
        rbc = basecount;
        InitMemory(type);
    } else
        throw std::invalid_argument("BN constructor: invalid type " + to_string(type));
}

BN::BN(uint64_t x)
{
    rbc = (sizeof(uint64_t) + bz - 1) / bz;
    ba.resize(rbc + 1);
    for(int i = 0; i < rbc; i++, x >>= bz8)
        ba[i] = static_cast<bt>(x);
    Norm();
}

BN::BN(const BN& bn)
{
    if(this == &bn)
        throw "constructor copy: Tak ne Byvaet!!! Constructor copii ISTCHO ne sosdannogo classa!!";
    rbc = bn.rbc;
    ba = bn.ba;
}

BN::BN(const BN& bn, int start, int count) {
    if(this == &bn)
        throw "constructor copy with param: Tak ne Byvaet!!! Constructor copii ISTCHO ne sosdannogo classa!!";
    if(count == -1)
        count = bn.rbc;
    if(count <= 0)
        throw "constructor copy with param: count byte is not positive";
    if(start < 0)
        throw "constructor copy with param: start < 0";


    rbc = count;
    ba.resize(rbc + 1);
    for(int i = 0; i < count && i + start < bn.rbc; i++)
        ba[i] = bn.ba[i + start];

    int bbstart = max<size_t>(0, bn.rbc - start);
    for(int i = bbstart; i < ba.size(); i++)
        ba[i] = 0;
    Norm();
}

BN::BN(const string &str,const int &status)
{
        if(status==1)
        {
                BN bn(1,0);
                BN bn10(10);
                for(unsigned int i=0;i<str.size();i++)
                {
                        if(str[i]<'0'||str[i]>'9')
                                continue;
                        BN bnc(str[i]-'0');
                        bn=bn*bn10+bnc;
                }
                rbc=bn.rbc;
                ba.resize(rbc + 1);
                for(int i=0;i<rbc;i++)
                        ba[i]=bn.ba[i];

                return;
        }

        int length = str.size();
        rbc=(length+bz*2-1)/(bz*2);
        ba.resize(rbc + 1);
        InitMemory(0);
        bt z;
        for(int i=0;i<length;i++)
        {
                if(str[i]>='A'&&str[i]<='F')
                {
                        int x=(length-i-1)/(bz*2);
                        ba[x]<<=4;
                        z=str[i]-'A'+10;
                        ba[x]|=z;
                }
                if(str[i]>='a'&&str[i]<='f')
                {
                        int x=(length-i-1)/(bz*2);
                        ba[x]<<=4;
                        z=str[i]-'a'+10;
                        ba[x]|=z;
                }
                if(str[i]>='0'&&str[i]<='9')
                {
                        int x=(length-i-1)/(bz*2);
                        ba[x]<<=4;
                        z=str[i]-'0';
                        ba[x]|=z;
                }
        }
        Norm();
}

BN & BN::operator = (const BN&bn)
{
    if (this == &bn)
        return *this;
    ba = bn.ba;
    rbc = bn.rbc;
    return *this;
}

BN BN::operator + (const BN&bn)const {
    int result_len = max(rbc, bn.rbc);
    BN result(result_len + 1, 2);

    bt2 res = 0;
    int pos = 0;
    int m = min(rbc, bn.rbc);
    for(; pos < m; pos++) {
        res += (bt2) ba[pos] + (bt2) bn.ba[pos];
        result.ba[pos] = res;
        res >>= bz8;
    }

    for(; pos < rbc; pos++) {
        res += (bt2) ba[pos];
        result.ba[pos] = res;
        res >>= bz8;
{'fraction_non_alphanumeric': 0.17689032109563815, 'fraction_numerical': 0.021291287835193923, 'mean_word_length': 1.369784565039542, 'pattern_counts': {'":': 0, '<': 71, '<?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': '426525', 'n_tokens_mistral': 3480, 'n_tokens_neox': 3219, 'n_words': 729}