Cod sursa(job #921713)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 21 martie 2013 11:21:22
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 7.06 kb
#include<fstream>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<iostream>
#include<typeinfo>

using namespace std;

class hash_function {

    private:

    int a, b, prime, size;

    public:

    void generate(int, int);
    int calculate(int);
    int calculate(float);
    int calculate(double);
    int calculate(string);
};

void hash_function::generate(int dim, int p) {

    prime = p;
    size = dim;

    a = rand() % size;
    b = rand() % size;
}

int hash_function::calculate(int val) {
    return(((long long)a * (long long)val + (long long)b) % (long long)prime) % (long long)size;
}

int hash_function::calculate(float val) {

    int key;
    float number, fractpart, intpart, mult_magic = 0.618034;

    number = val*mult_magic;
    fractpart = modf(number, &intpart);
    key = (((long long)a * (long long)fractpart + (long long)b * (long long)intpart) % (long long)prime) % (long long)size;

    return key;
}

int hash_function::calculate(double val) {

    int key;
    double number, fractpart, intpart, mult_magic = 0.618034;

    number = val*mult_magic;
    fractpart = modf(number, &intpart);
    key = (((long long)a * (long long)fractpart + (long long)b * (long long)intpart) % (long long)prime) % (long long)size;

    return key;
}

int hash_function::calculate(string str) {

    unsigned InitialFNV = 2166136261U, key;
    int FNVMultiple = 16777619, dim, i;

    dim = str.length();
    key = InitialFNV;
    for(i = 0; i<dim; i++) {
        key = key ^ (str[i]);
        key = key * FNVMultiple;
    }
    key = (((long long)a * (long long)key + (long long)b) % (long long)prime) % (long long)size;

    return key;
}

template<class T>
class cockooHash {

    private:

    T *H1, *H2;
    int prime, size, maxsteps;
    bool *viz1, *viz2;
    string input, output;
    hash_function func1, func2;
    T convert(string);

    ifstream f;
    ofstream g;

    public:

    cockooHash<T>(int, string, string);
    bool remake_hash();
    void parsing(string);

    bool operator[](T);
    void operator+=(T);
    void operator-=(T);
};

int main() {

    cockooHash<int> myHash(1100000, "hashuri.in", "hashuri.out");

    srand(time(0));

    //myHash.parsing("+=3.1[3.1]-=3.1[3.1]");

    bool ok = false;
    while(!ok)
        ok = myHash.remake_hash();

    return 0;
}

template<class T>
T cockooHash<T>::convert(string value) {

    if(typeid(T) == typeid(int())) {
        int i, dim = value.length(), isint = 0;
        for(i=0; i<dim; ++i)
            isint = isint*10 + value[i]-'0';
        return (T)isint;
    }
    if(typeid(T) == typeid(float())) {
        int i, dim = value.length(), nr_decimals = 0, isfloat = 0;
        for(i=0; i<dim && value[i]!='.'; ++i)
            isfloat = isfloat*10 + value[i]-'0';
        while(i<dim) {
            nr_decimals++;
            isfloat = isfloat*10 + value[i]-'0';
            ++i;
        }
        return (T)isfloat / (float)pow(10, nr_decimals);
    }
    if(typeid(T) == typeid(double())) {
        int i, dim = value.length(), nr_decimals = 0, isdouble = 0;
        for(i=0; i<dim && value[i]!='.'; ++i)
            isdouble = isdouble*10 + value[i]-'0';
        while(i<dim) {
            nr_decimals++;
            isdouble = isdouble*10 + value[i]-'0';
            ++i;
        }
        return (T)isdouble / (double)pow(10, nr_decimals);
    }
    if(typeid(T) == typeid(string())) {
            //return (T)value;
    }
}

template<class T>
void cockooHash<T>::parsing(string str) {

    int i, dim;
    string object;
    T value;

    g.open(output.c_str());
    dim = str.length();
    i = 0;
    while(i < dim) {
        object.clear();
        if(str[i] == '+' && str[i+1]=='=') {
            i+=2;
            while(!strchr("+=-[]",str[i]) && i<dim) {
                object.push_back(str[i]);
                ++i;
            }
            value = convert(object);
            (*this)+=value;
            continue;
        }
        if(str[i] == '-' && str[i+1]=='=') {
            i+=2;
            while(!strchr("+=-[]",str[i]) && i<dim) {
                object.push_back(str[i]);
                ++i;
            }
            value = convert(object);
            (*this)-=value;
            continue;
        }
        if(str[i] == '[') {
            i++;
            while(!strchr("+=-[]",str[i]) && i<dim) {
                object.push_back(str[i]);
                ++i;
            }
            value = convert(object);
            g<<(*this)[value]<<"\n";
        }
        else
            ++i;
    }
    g.close();
}

template<class T>
void cockooHash<T>::operator-=(T val) {

    int poz1 = func1.calculate(val);
    int poz2 = func2.calculate(val);

    if(H1[poz1] == val && viz1[poz1] == true) {
        viz1[poz1] = false;
        return;
    }

    if(H2[poz2] == val && viz2[poz2] == true) {
        viz2[poz2] = false;
        return;
    }
}

template<class T>
void cockooHash<T>::operator+=(T val) {

    int i, poz1, poz2;
    T x;

    x = val;
    poz1 = func1.calculate(x);
    poz2 = func2.calculate(x);

    if(viz1[poz1] == false) {
        H1[poz1] = x;
        viz1[poz1] = true;
        return;
    }

    for(i=0; i<maxsteps; i+=2) {
        if(viz2[poz2] == false) {
            H2[poz2] = x;
            viz2[poz2] = true;
            return;
        }

        swap(x, H2[poz2]);
        poz1 = func1.calculate(x);
        poz2 = func2.calculate(x);

        if(viz1[poz1] == false) {
            H1[poz1] = x;
            viz1[poz1] = true;
            return;
        }

        swap(x, H1[poz1]);
        poz1 = func1.calculate(x);
        poz2 = func2.calculate(x);
    }

    remake_hash();
}

template<class T>
bool cockooHash<T>::remake_hash() {

    int Q, op, val;

    f.open(input.c_str());
    g.open(output.c_str());

    f>>Q;
    while(Q--) {
        f>>op>>val;
        if(op == 1) {
            if(!(*this)[val])
                *this += val;
        }
        if(op == 2) {
            if((*this)[val])
                *this -= val;
        }
        if(op == 3)
            g<<(*this)[val]<<"\n";
    }

    f.close();
    g.close();

    return true;
}

template<class T>
cockooHash<T>::cockooHash(int dim, string infile, string outfile) {

    size = dim;
    maxsteps = (int)log2(size);
    prime = 1000000009;
    input = infile;
    output = outfile;

    H1 = new T[size];
    H2 = new T[size];

    viz1 = new bool[size];
    viz2 = new bool[size];
    memset(viz1, 0, sizeof(viz1));
    memset(viz2, 0, sizeof(viz1));

    func1.generate(size, prime);
    func2.generate(size, prime);
}

template<class T>
bool cockooHash<T>::operator[](T val) {

    int poz1 = func1.calculate(val);
    int poz2 = func2.calculate(val);

    if(H1[poz1] == val && viz1[poz1] == true)
        return true;
    if(H2[poz2] == val && viz2[poz2] == true)
        return true;

    return false;
}