<commit_before>//
// Created by Dennis Goldfarb on 1/3/17.
//

#include <iostream>
#include <fstream>
#include <random>
#include <algorithm>

#include <OpenMS/CHEMISTRY/Element.h>
#include <OpenMS/CHEMISTRY/AASequence.h>
#include <OpenMS/CHEMISTRY/ResidueDB.h>
#include <OpenMS/CHEMISTRY/ElementDB.h>

#include "FASTAParser.h"

static const ElementDB* elementDB = ElementDB::getInstance();
static const OpenMS::ResidueDB* residueDB = OpenMS::ResidueDB::getInstance();

static std::string AMINO_ACIDS = "ADEFGHIKLNPQRSTVWYCM";
static std::string AMINO_ACIDS_NO_SULFUR = "ADEFGHIKLNPQRSTVWY";
static std::string AMINO_ACIDS_SULFUR = "CM";

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis_AA(0,1);
std::uniform_int_distribution<> dis_SULFUR(0,1);

std::ofstream* openOutputFiles(std::string base_path, int max_depth, bool write_sulfur)
{
    // create all output files and write header to each
    std::ofstream* outfiles = new std::ofstream[max_depth];
    for (int precursor_isotope = 0; precursor_isotope < max_depth; ++precursor_isotope)
    {
        std::string filename = "Precursor" + std::to_string(precursor_isotope) + ".tab";

        outfiles[precursor_isotope].open(base_path + filename);
        if (write_sulfur) {
            outfiles[precursor_isotope] << "probability" << "\tprecursor.mass" << "\tsulfur" << std::endl;
        } else {
            outfiles[precursor_isotope] << "probability" << "\tprecursor.mass" << std::endl;
        }
    }

    return outfiles;
}

void closeOutputFiles(std::ofstream* outfiles, int max_depth)
{
    // close all output files
    for (int precursor_isotope = 0; precursor_isotope < max_depth; ++precursor_isotope)
    {
        outfiles[precursor_isotope].close();
    }
    delete[] outfiles;
}

void write_distribution(const OpenMS::AASequence &p, std::ofstream* outfiles, int max_depth, bool mono, bool write_sulfur, bool exact)
{
    OpenMS::EmpiricalFormula precursor_ef = p.getFormula();
    double mass = mono ? precursor_ef.getMonoWeight() : precursor_ef.getAverageWeight();

    OpenMS::IsotopeDistribution precursor_id;
    if (exact) {
        precursor_id = precursor_ef.getIsotopeDistribution(0);
    } else {
        precursor_id.estimateFromPeptideWeight(mass);
    }

    for (int precursor_isotope = 0; precursor_isotope < max_depth && precursor_isotope < precursor_id.size(); ++precursor_isotope)
    {
        if (write_sulfur) {
            int num_sulfur = precursor_ef.getNumberOf(elementDB->getElement("Sulfur"));
            outfiles[precursor_isotope] << precursor_id.getContainer()[precursor_isotope].second << "\t" << mass << "\t" << num_sulfur << std::endl;
        } else {
            outfiles[precursor_isotope] << precursor_id.getContainer()[precursor_isotope].second << "\t" << mass << std::endl;
        }
    }
}

void proteome_isotopic_distributions(std::string base_path, std::string fasta_path, float max_mass, int max_depth, bool mono)
{
    std::ofstream* outfiles = openOutputFiles(base_path, max_depth, true);

    FASTAParser parser(fasta_path, max_mass, 1, 150);
    for (auto itr = parser.begin(); itr != parser.end(); ++itr)
    {
        write_distribution(*itr, outfiles, max_depth, mono, true, true);
    }

    closeOutputFiles(outfiles, max_depth);

}

void averagine_isotopic_distributions(std::string base_path, float max_mass, int max_depth, bool mono)
{
    std::ofstream* outfiles_averagine = openOutputFiles(base_path+"averagine/", max_depth, false);

    for (double mass = 50; mass < max_mass; ++mass)
    {
        OpenMS::IsotopeDistribution precursor_id;

        precursor_id.estimateFromPeptideWeight(mass);

        for (int precursor_isotope = 0; precursor_isotope < max_depth && precursor_isotope < precursor_id.size(); ++precursor_isotope)
        {
            outfiles_averagine[precursor_isotope] << precursor_id.getContainer()[precursor_isotope].second << "\t" << mass << std::endl;
        }
    }

    closeOutputFiles(outfiles_averagine, max_depth);
}

OpenMS::AASequence create_random_peptide_sequence(int peptide_length, std::vector<double> aa2prob, int num_sulfurs)
{
    OpenMS::AASequence random_peptide;

    if (num_sulfurs < 0)
    {
        for (int aa_index = 0; aa_index < peptide_length; ++aa_index)
        {
            double rand = dis_AA(gen);
            int index = std::lower_bound(aa2prob.begin(), aa2prob.end(), rand) - aa2prob.begin() - 1;
            random_peptide += residueDB->getResidue(AMINO_ACIDS[index]);
        }
    }
    else
    {
        // for insertion of sulfur containing amino acids
        for (int i = 0; i < num_sulfurs; ++i)
        {
            random_peptide += residueDB->getResidue(AMINO_ACIDS_SULFUR[dis_SULFUR(gen)]);
        }

        // random amino acid insertion (non Sulfur and Selenium amino acids)
        for (int aa_index = 0; aa_index < peptide_length; ++aa_index)
        {
            double rand = dis_AA(gen);
            int index = std::lower_bound(aa2prob.begin(), aa2prob.end(), rand) - aa2prob.begin() - 1;
            random_peptide += residueDB->getResidue(AMINO_ACIDS_NO_SULFUR[index]);
        }
    }

    return random_peptide;
}

std::map<char, double> getAAProbabilities(std::string fasta_path, bool sulfur)
{
    std::map<char, double> aa2prob;
    for (char aa : AMINO_ACIDS)
    {
        aa2prob[aa] = 0.0;
    }

    std::vector<FASTAFile::FASTAEntry> proteins;
    FASTAFile().load(fasta_path, proteins);

    int count = 0;
    for (Size i = 0; i < proteins.size(); ++i)
    {
        for (int j = 0; j < proteins[i].sequence.size(); ++j)
        {
            char aa = proteins[i].sequence[j];
            if ((sulfur && AMINO_ACIDS.find(aa) != -1) || (!sulfur && AMINO_ACIDS_NO_SULFUR.find(aa) != -1))
            {
                aa2prob[aa]++;
                count++;
            }
        }
    }

    for (auto &aa : aa2prob)
    {
        aa.second /= count;
    }

    return aa2prob;
}

std::vector<double> calcPrefixSum(std::map<char, double> aa2prob, bool sulfur)
{
    std::string AAs = sulfur ? AMINO_ACIDS : AMINO_ACIDS_NO_SULFUR;
    std::vector<double> prefixSum;

    prefixSum.push_back(0);

    for (int i = 0; i < AAs.size(); ++i)
    {
        prefixSum.push_back(aa2prob[AAs[i]] + prefixSum[i]);
    }

    return prefixSum;
}

void sample_isotopic_distributions(std::string base_path, std::string fasta_path, float max_mass, int num_sulfurs, int num_samples, int max_depth, bool mono)
{

    std::vector<double> aa2prob = calcPrefixSum(getAAProbabilities(fasta_path, num_sulfurs == -1), num_sulfurs == -1);

    std::ofstream* outfiles = openOutputFiles(base_path, max_depth, false);

    int max_length = max_mass/100;

    for (int peptide_length = 1; peptide_length <= max_length; ++peptide_length)
    {
        for (int sample = 0; sample < num_samples; ++sample)
        {
            OpenMS::AASequence random_sequence = create_random_peptide_sequence(peptide_length, aa2prob, num_sulfurs);

            if (random_sequence.size() > 0 && random_sequence.getMonoWeight() <= max_mass)
            {
                write_distribution(random_sequence, outfiles, max_depth, mono, false, true);
            }
        }
    }

    closeOutputFiles(outfiles, max_depth);
}

void usage()
{
    std::cout << "GenerateTrainingData fasta_path out_path max_mass S num_samples max_depth mono" << std::endl;
    std::cout << "fasta_path: The path to the fasta file to train the splines on." << std::endl;
    std::cout << "out_path: The path to the directory that will store the training data, e.g. ~/data/" << std::endl;
    std::cout << "max_mass: maximum mass allowed for sampled peptides, e.g. 8500" << std::endl;
    std::cout << "max_depth: The number of isotopes to generate training data for, e.g. 3 = M0,M1,M2" << std::endl;
    std::cout << "mono: should monoisotopic masses be used or average? 1=mono, 0=average" << std::endl;
    std::cout << "S: number of sulfurs that should be in the fragment ion. Use -1 for all (e.g. 0,1,2..)" << std::endl;
    std::cout << "num_samples: number of random peptides to make for each peptide length" << std::endl;

    std::cout << std::endl;
}

int main(int argc, const char ** argv)
{
    if (argc != 8 && argc != 6)
    {
        usage();
        return 0;
    }

    std::string fasta_path = argv[1];
    std::string out_path = argv[2];
    float max_mass = atof(argv[3]);
    int max_depth = atoi(argv[4]);
    bool mono = strncmp(argv[5], "1", 1) == 0 ? true : false;

    if (argc == 8) {
        int S = atoi(argv[6]);
        int num_samples = atoi(argv[7]);
        sample_isotopic_distributions(out_path, fasta_path, max_mass, S, num_samples, max_depth, mono);
    } else {
        proteome_isotopic_distributions(out_path, fasta_path, max_mass, max_depth, mono);
        averagine_isotopic_distributions(out_path, max_mass, max_depth, mono);
    }

    return 0;
}<commit_msg>plots<commit_after>//
// Created by Dennis Goldfarb on 1/3/17.
//

#include <iostream>
#include <fstream>
#include <random>
#include <algorithm>

#include <OpenMS/CHEMISTRY/Element.h>
#include <OpenMS/CHEMISTRY/AASequence.h>
#include <OpenMS/CHEMISTRY/ResidueDB.h>
#include <OpenMS/CHEMISTRY/ElementDB.h>

#include "FASTAParser.h"

static const ElementDB* elementDB = ElementDB::getInstance();
static const OpenMS::ResidueDB* residueDB = OpenMS::ResidueDB::getInstance();

static std::string AMINO_ACIDS = "ADEFGHIKLNPQRSTVWYCM";
static std::string AMINO_ACIDS_NO_SULFUR = "ADEFGHIKLNPQRSTVWY";
static std::string AMINO_ACIDS_SULFUR = "CM";

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis_AA(0,1);
std::uniform_int_distribution<> dis_SULFUR(0,1);

std::ofstream* openOutputFiles(std::string base_path, int max_depth, bool write_sulfur)
{
    // create all output files and write header to each
    std::ofstream* outfiles = new std::ofstream[max_depth];
    for (int precursor_isotope = 0; precursor_isotope < max_depth; ++precursor_isotope)
    {
        std::string filename = "Precursor" + std::to_string(precursor_isotope) + ".tab";

        outfiles[precursor_isotope].open(base_path + filename);
        if (write_sulfur) {
            outfiles[precursor_isotope] << "probability" << "\tprecursor.mass" << "\tsulfur" << std::endl;
        } else {
            outfiles[precursor_isotope] << "probability" << "\tprecursor.mass" << std::endl;
        }
    }

    return outfiles;
}

void closeOutputFiles(std::ofstream* outfiles, int max_depth)
{
    // close all output files
    for (int precursor_isotope = 0; precursor_isotope < max_depth; ++precursor_isotope)
    {
        outfiles[precursor_isotope].close();
    }
    delete[] outfiles;
}

void write_distribution(const OpenMS::AASequence &p, std::ofstream* outfiles, int max_depth, bool mono, bool write_sulfur, bool exact)
{
    OpenMS::EmpiricalFormula precursor_ef = p.getFormula();
    double mass = mono ? precursor_ef.getMonoWeight() : precursor_ef.getAverageWeight();

    OpenMS::IsotopeDistribution precursor_id;
    if (exact) {
        precursor_id = precursor_ef.getIsotopeDistribution(0);
    } else {
        precursor_id.estimateFromPeptideWeight(mass);
    }

    for (int precursor_isotope = 0; precursor_isotope < max_depth && precursor_isotope < precursor_id.size(); ++precursor_isotope)
    {
        if (write_sulfur) {
            int num_sulfur = precursor_ef.getNumberOf(elementDB->getElement("Sulfur"));
            outfiles[precursor_isotope] << precursor_id.getContainer()[precursor_isotope].second << "\t" << mass << "\t" << num_sulfur << std::endl;
        } else {
            outfiles[precursor_isotope] << precursor_id.getContainer()[precursor_isotope].second << "\t" << mass << std::endl;
        }
    }
}

void proteome_isotopic_distributions(std::string base_path, std::string fasta_path, float max_mass, int max_depth, bool mono)
{
    std::ofstream* outfiles = openOutputFiles(base_path, max_depth, true);

    FASTAParser parser(fasta_path, max_mass, 1, 150);
    for (auto itr = parser.begin(); itr != parser.end(); ++itr)
    {
        write_distribution(*itr, outfiles, max_depth, mono, true, true);
    }

    closeOutputFiles(outfiles, max_depth);

}

void averagine_isotopic_distributions(std::string base_path, float max_mass, int max_depth)
{
    std::ofstream* outfiles_averagine = openOutputFiles(base_path+"averagine/", max_depth, false);

    for (double mass = 50; mass < max_mass; mass+=1)
    {
        OpenMS::IsotopeDistribution precursor_id;

        precursor_id.estimateFromPeptideWeight(mass);

        for (int precursor_isotope = 0; precursor_isotope < max_depth && precursor_isotope < precursor_id.size(); ++precursor_isotope)
        {
            outfiles_averagine[precursor_isotope] << precursor_id.getContainer()[precursor_isotope].second << "\t" << mass << std::endl;
        }
    }

    closeOutputFiles(outfiles_averagine, max_depth);
}

OpenMS::AASequence create_random_peptide_sequence(int peptide_length, std::vector<double> aa2prob, int num_sulfurs)
{
    OpenMS::AASequence random_peptide;

    if (num_sulfurs < 0)
    {
        for (int aa_index = 0; aa_index < peptide_length; ++aa_index)
        {
            double rand = dis_AA(gen);
            int index = std::lower_bound(aa2prob.begin(), aa2prob.end(), rand) - aa2prob.begin() - 1;
            random_peptide += residueDB->getResidue(AMINO_ACIDS[index]);
        }
    }
    else
    {
        // for insertion of sulfur containing amino acids
        for (int i = 0; i < num_sulfurs; ++i)
        {
            random_peptide += residueDB->getResidue(AMINO_ACIDS_SULFUR[dis_SULFUR(gen)]);
        }

        // random amino acid insertion (non Sulfur and Selenium amino acids)
        for (int aa_index = 0; aa_index < peptide_length; ++aa_index)
        {
            double rand = dis_AA(gen);
            int index = std::lower_bound(aa2prob.begin(), aa2prob.end(), rand) - aa2prob.begin() - 1;
            random_peptide += residueDB->getResidue(AMINO_ACIDS_NO_SULFUR[index]);
        }
    }

    return random_peptide;
}

std::map<char, double> getAAProbabilities(std::string fasta_path, bool sulfur)
{
    std::map<char, double> aa2prob;
    for (char aa : AMINO_ACIDS)
    {
        aa2prob[aa] = 0.0;
    }

    std::vector<FASTAFile::FASTAEntry> proteins;
    FASTAFile().load(fasta_path, proteins);

    int count = 0;
    for (Size i = 0; i < proteins.size(); ++i)
    {
        for (int j = 0; j < proteins[i].sequence.size(); ++j)
        {
            char aa = proteins[i].sequence[j];
            if ((sulfur && AMINO_ACIDS.find(aa) != -1) || (!sulfur && AMINO_ACIDS_NO_SULFUR.find(aa) != -1))
            {
                aa2prob[aa]++;
                count++;
            }
        }
    }

    for (auto &aa : aa2prob)
    {
        aa.second /= count;
    }

    return aa2prob;
}

std::vector<double> calcPrefixSum(std::map<char, double> aa2prob, bool sulfur)
{
    std::string AAs = sulfur ? AMINO_ACIDS : AMINO_ACIDS_NO_SULFUR;
    std::vector<double> prefixSum;

    prefixSum.push_back(0);

    for (int i = 0; i < AAs.size(); ++i)
    {
        prefixSum.push_back(aa2prob[AAs[i]] + prefixSum[i]);
    }

    return prefixSum;
}

void sample_isotopic_distributions(std::string base_path, std::string fasta_path, float max_mass, int num_sulfurs, int num_samples, int max_depth, bool mono)
{

    std::vector<double> aa2prob = calcPrefixSum(getAAProbabilities(fasta_path, num_sulfurs == -1), num_sulfurs == -1);

    std::ofstream* outfiles = openOutputFiles(base_path, max_depth, false);

    int max_length = max_mass/100;

    for (int peptide_length = 1; peptide_length <= max_length; ++peptide_length)
    {
        for (int sample = 0; sample < num_samples; ++sample)
        {
            OpenMS::AASequence random_sequence = create_random_peptide_sequence(peptide_length, aa2prob, num_sulfurs);

            if (random_sequence.size() > 0 && random_sequence.getMonoWeight() <= max_mass)
            {
                write_distribution(random_sequence, outfiles, max_depth, mono, false, true);
            }
        }
    }

    closeOutputFiles(outfiles, max_depth);
}

void usage()
{
    std::cout << "GenerateTrainingData fasta_path out_path max_mass S num_samples max_depth mono" << std::endl;
    std::cout << "fasta_path: The path to the fasta file to train the splines on." << std::endl;
    std::cout << "out_path: The path to the directory that will store the training data, e.g. ~/data/" << std::endl;
    std::cout << "max_mass: maximum mass allowed for sampled peptides, e.g. 8500" << std::endl;
    std::cout << "max_depth: The number of isotopes to generate training data for, e.g. 3 = M0,M1,M2" << std::endl;
    std::cout << "mono: should monoisotopic masses be used or average? 1=mono, 0=average" << std::endl;
    std::cout << "S: number of sulfurs that should be in the fragment ion. Use -1 for all (e.g. 0,1,2..)" << std::endl;
    std::cout << "num_samples: number of random peptides to make for each peptide length" << std::endl;

    std::cout << std::endl;
}

int main(int argc, const char ** argv)
{
    if (argc != 8 && argc != 6)
    {
        usage();
        return 0;
    }

    std::string fasta_path = argv[1];
    std::string out_path = argv[2];
    float max_mass = atof(argv[3]);
    int max_depth = atoi(argv[4]);
    bool mono = strncmp(argv[5], "1", 1) == 0 ? true : false;

    if (argc == 8) {
        int S = atoi(argv[6]);
        int num_samples = atoi(argv[7]);
        sample_isotopic_distributions(out_path, fasta_path, max_mass, S, num_samples, max_depth, mono);
    } else {
        proteome_isotopic_distributions(out_path, fasta_path, max_mass, max_depth, mono);
        averagine_isotopic_distributions(out_path, max_mass, max_depth);
    }

    return 0;
}<|endoftext|>


<commit_before>#ifndef MOB_HPP
#define MOB_HPP

#include <iostream>

class Mob
{
public:
  mob(std::string, std::string std::string, int, int, int, int);

  void setName(std::string);
  void setArea(std::string);
  void setSkill(std::string);

  void setLevel(int);
  void setEXP(int);
  void setHP(int);
  void setMaxHealth(int);

  void setDamage();
  void setEXP();

  std::string getName;
  std::string getArea;

  int getLevel();
  int getEXP();
  int getHP();
  int getMaxHealth();
  int getDamage();
  int getEXP();

private:
  std::string mobName;
  std::string mobArea;
  int mobLevel;
  int mobHealth;
  int mobMaxHealth;
  int mobDamage;
  int mobEXP;
};

#endif

<commit_msg>Delete mob.hpp<commit_after><|endoftext|>


<commit_before><commit_msg>Make DML operator registration constexpr (#4219)<commit_after><|endoftext|>


<commit_before>#include <algorithm>
#include <cmath>

#include "polynomial.h"

using std::copy_n;

namespace gpu_poly
{

polynomial::polynomial(const unsigned* coeffs, unsigned degree, unsigned charecteristic) :
	coeffs(new unsigned[degree+1]), degree(degree), charecteristic(charecteristic)
{
	copy_n(coeffs, degree + 1, this -> coeffs);
}

polynomial::polynomial(polynomial&& move_me) : coeffs(std::move(move_me.coeffs)),
	degree(move_me.degree), charecteristic(move_me.charecteristic)
{
}

polynomial::~polynomial()
{
	delete coeffs;
}

unsigned* polynomial::get_coeffs()
{
	unsigned* copy_coeffs(new unsigned[degree + 1]);
	copy_n(coeffs, degree + 1, copy_coeffs);
}

unsigned polynomial::get_charecteristic()
{
	return charecteristic;
}

unsigned polynomial::get_degree()
{
	return degree;
}

polynomial& polynomial::copy()
{
	return polynomial(coeffs, degree, charecteristic);
}

static polynomial& polynomial::operator+(const polynomial& p, const polynomial& q)
{
	unsigned		charecteristic(p.charecteristic);
	const unsigned	*p_co(p.coeffs), *q_co(q.coeffs), *b_co;
	int				p_deg(p.degree), q_deg(q.degree);

	// Find min and max
	int			diff		= p_deg - q_deg;
	int			sum			= p_deg + q_deg;
	unsigned	abs_diff	= (unsigned) sqrt(diff*diff);
	unsigned	max			= (sum + abs_diff)/2, maxp1 = max + 1;
	unsigned	min			= (sum - abs_diff)/2, minp1 = min + 1;

	unsigned	r_co[max + 1];

	// Sum shared
	for ( unsigned i = 0 ; i < minp1 ; i++ )
		r_co[i] = (p_co[i] + q_co[i]) % charecteristic;

	b_co	= ( (p_deg == max) ? p_co : q_co );

	// Copy big
	for ( unsigned i = minp1 ; i < maxp1 ; i++ )
		r_co[i] = b_co[i];

	unsigned	r_deg(max);

	// Find the degree of the new polynomial
	while ( r_co[r_deg] == 0 && r_deg > 0 )
		r_deg--;

	return polynomial(r_co, r_deg, charecteristic);
}

static polynomial& polynomial::operator-(const polynomial& p, const polynomial& q)
{
	unsigned		charecteristic(p.charecteristic);
	const unsigned	*q_co(q.coeffs);
	int				q_deg(q.degree), q_deg_p1 = q_deg+1;
	unsigned		r_co[q_deg+1];

	// Flip everythin in q
	for ( i = 0 ; i < q_deg_p1 ; i++ )
		r_co[i] = (-1 * q_co[i]) % charecteristic;

	return p + polynomial(r_co, q_deg, charecteristic);
}

static polynomial& polynomial::operator*(const polynomial& p, const polynomial& q)
{
	const unsigned	*p_co(p.coeffs),	*q_co(q.coeffs);
	unsigned		p_deg(p.degree),	q_deg(q.degree);
	unsigned		p_deg_p1(p_deg+1),	q_deg_p1(q_deg+1);
	unsigned		r_deg(p_deg+q_deg),	r_deg_p1(r_deg+1);
	unsigned		charecteristic(p.charecteristic);

	unsigned		r_co[r_deg_p1];

	// Compute the new vector of coefficients
	for ( i = 0 ; i < p_deg_p1 ; i++)
		for ( j = 0 ; j < q_deg_p1 ; j++)
			r_co[i+j] = p_co[i] + q_co[j];

	return polynomial(r_co, r_deg, charecteristic);
}

static polynomial& polynomial::operator*(int n, const polynomial& p)
{
	unsigned		charecteristic(p.charecteristic);
	const unsigned	*p_co(p.coeffs);
	int				p_deg(p.degree), p_deg_p1 = p_deg+1;
	unsigned		r_co[q_deg+1];

	// Multiply everythin in p
	for ( i = 0 ; i < p_deg_p1 ; i++ )
		r_co[i] = (n*p_co[i])%charecteristic;

	return p + polynomial(r_co, q_deg, charecteristic);
}

}
<commit_msg>Did some stuff<commit_after>#include <algorithm>
#include <cmath>

#include "polynomial.h"

using std::copy_n;

namespace gpu_poly
{

polynomial::polynomial(const unsigned* coeffs, unsigned degree, unsigned charecteristic) :
	coeffs(new unsigned[degree+1]), degree(degree), charecteristic(charecteristic)
{
	copy_n(coeffs, degree + 1, this -> coeffs);
}

polynomial::polynomial(polynomial&& move_me) : coeffs(std::move(move_me.coeffs)),
	degree(move_me.degree), charecteristic(move_me.charecteristic)
{
}

polynomial::~polynomial()
{
	delete coeffs;
}

unsigned* polynomial::get_coeffs()
{
	unsigned* copy_coeffs(new unsigned[degree + 1]);
	copy_n(coeffs, degree + 1, copy_coeffs);
}

unsigned polynomial::get_charecteristic()
{
	return charecteristic;
}

unsigned polynomial::get_degree()
{
	return degree;
}

polynomial& polynomial::copy()
{
	return polynomial(coeffs, degree, charecteristic);
}

static polynomial& polynomial::operator+(const polynomial& p, const polynomial& q)
{
	unsigned		charecteristic(p.charecteristic);
	const unsigned	*p_co(p.coeffs), *q_co(q.coeffs), *b_co;
	int				p_deg(p.degree), q_deg(q.degree);

	// Find min and max
	int			diff		= p_deg - q_deg;
	int			sum			= p_deg + q_deg;
	unsigned	abs_diff	= (unsigned) sqrt(diff*diff);
	unsigned	max			= (sum + abs_diff)/2, maxp1 = max + 1;
	unsigned	min			= (sum - abs_diff)/2, minp1 = min + 1;

	unsigned	r_co[max + 1];

	// Sum shared
	for ( unsigned i = 0 ; i < minp1 ; i++ )
		r_co[i] = (p_co[i] + q_co[i]) % charecteristic;

	b_co	= ( (p_deg == max) ? p_co : q_co );

	// Copy big
	for ( unsigned i = minp1 ; i < maxp1 ; i++ )
		r_co[i] = b_co[i];

	unsigned	r_deg(max);

	// Find the degree of the new polynomial
	while ( r_co[r_deg] == 0 && r_deg > 0 )
		r_deg--;

	return polynomial(r_co, r_deg, charecteristic);
}

static polynomial& polynomial::operator-(const polynomial& p, const polynomial& q)
{
	unsigned		charecteristic(p.charecteristic);
	const unsigned	*q_co(q.coeffs);
	int				q_deg(q.degree), q_deg_p1 = q_deg+1;
	unsigned		r_co[q_deg+1];

	// Flip everythin in q
	for ( i = 0 ; i < q_deg_p1 ; i++ )
		r_co[i] = (-1 * q_co[i]) % charecteristic;

	return p + polynomial(r_co, q_deg, charecteristic);
}

static polynomial& polynomial::operator-(const polynomial& p, const polynomial& q)
{
	unsigned		charecteristic(p.charecteristic);
	const unsigned	*p_co(p.coeffs), *q_co(q.coeffs), *b_co;
	int				p_deg(p.degree), q_deg(q.degree);

	// Find min and max
	int			diff		= p_deg - q_deg;
	int			sum			= p_deg + q_deg;
	unsigned	abs_diff	= (unsigned) sqrt(diff*diff);
	unsigned	max			= (sum + abs_diff)/2, maxp1 = max + 1;
	unsigned	min			= (sum - abs_diff)/2, minp1 = min + 1;

	unsigned	r_co[max + 1];

	// Sum shared
	for ( unsigned i = 0 ; i < minp1 ; i++ )
		r_co[i] = (p_co[i] - q_co[i]) % charecteristic;

	b_co	= ( (p_deg == max) ? p_co : q_co );

	// Copy big
	for ( unsigned i = minp1 ; i < maxp1 ; i++ )
		r_co[i] = b_co[i];

	unsigned	r_deg(max);

	// Find the degree of the new polynomial
	while ( r_co[r_deg] == 0 && r_deg > 0 )
		r_deg--;

	return polynomial(r_co, r_deg, charecteristic);
}

static polynomial& polynomial::operator*(const polynomial& p, const polynomial& q)
{
	const unsigned	*p_co(p.coeffs),	*q_co(q.coeffs);
	unsigned		p_deg(p.degree),	q_deg(q.degree);
	unsigned		p_deg_p1(p_deg+1),	q_deg_p1(q_deg+1);
	unsigned		r_deg(p_deg+q_deg),	r_deg_p1(r_deg+1);
	unsigned		charecteristic(p.charecteristic);

	unsigned		r_co[r_deg_p1];

	// Compute the new vector of coefficients
	for ( i = 0 ; i < p_deg_p1 ; i++)
		for ( j = 0 ; j < q_deg_p1 ; j++)
			r_co[i+j] = p_co[i] + q_co[j];

	return polynomial(r_co, r_deg, charecteristic);
}

static polynomial& polynomial::operator*(int n, const polynomial& p)
{
	unsigned		charecteristic(p.charecteristic);
	const unsigned	*p_co(p.coeffs);
	int				p_deg(p.degree), p_deg_p1 = p_deg+1;
	unsigned		r_co[q_deg+1];

	// Multiply everythin in p
	for ( i = 0 ; i < p_deg_p1 ; i++ )
		r_co[i] = (n*p_co[i])%charecteristic;

	return p + polynomial(r_co, q_deg, charecteristic);
}

}
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: BaseListBox.hxx,v $
 *
 *  $Revision: 1.1.1.1 $
 *
 *  last change: $Author: hr $ $Date: 2000-09-18 16:29:04 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _FORMS_BASELISTBOX_HXX_
#define _FORMS_BASELISTBOX_HXX_

//.........................................................................
namespace frm
{

const sal_uInt16 ENTRY_NOT_FOUND =  0xFFFF;
const sal_uInt16 BOUNDCOLUMN     =  0x0001;

//.........................................................................
}
//.........................................................................

#endif // _FORMS_BASELISTBOX_HXX_

<commit_msg>INTEGRATION: CWS ooo19126 (1.1.1.1.370); FILE MERGED 2005/09/05 13:50:12 rt 1.1.1.1.370.1: #i54170# Change license header: remove SISSL<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: BaseListBox.hxx,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-08 22:33:29 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef _FORMS_BASELISTBOX_HXX_
#define _FORMS_BASELISTBOX_HXX_

//.........................................................................
namespace frm
{

const sal_uInt16 ENTRY_NOT_FOUND =  0xFFFF;
const sal_uInt16 BOUNDCOLUMN     =  0x0001;

//.........................................................................
}
//.........................................................................

#endif // _FORMS_BASELISTBOX_HXX_

<|endoftext|>


<commit_before>#include <sys/types.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>

#include <map>
#include <vector>

#include <common/buffer.h>
#include <common/endian.h>
#include <common/limits.h>
#include <common/timer/timer.h>

#include <xcodec/xcodec.h>
#include <xcodec/xcodec_cache.h>
#include <xcodec/xcodec_decoder.h>
#include <xcodec/xcodec_encoder.h>

enum FileAction {
	None, Compress, Decompress
};

static bool codec_timing;
static Timer codec_timer;

static void compress(int, int, XCodec *);
static void decompress(int, int, XCodec *);
static bool fill(int, Buffer *);
static void flush(int, Buffer *);
static void process_files(int, char *[], FileAction, XCodec *, bool);
static void time_samples(void);
static void time_stats(void);
static void usage(void);

int
main(int argc, char *argv[])
{
	UUID uuid;
	uuid.generate();

	XCodecCache *cache = XCodecCache::lookup(uuid);
	XCodec codec(cache);

	bool quiet_output, samples, verbose;
	FileAction action;
	int ch;

	action = None;
	quiet_output = false;
	samples = false;
	verbose = false;

	while ((ch = getopt(argc, argv, "?cdvQST")) != -1) {
		switch (ch) {
		case 'c':
			action = Compress;
			break;
		case 'd':
			action = Decompress;
			break;
		case 'v':
			verbose = true;
			break;
		case 'Q':
			quiet_output = true;
			break;
		case 'S':
			samples = true;
			break;
		case 'T':
			codec_timing = true;
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (verbose) {
		Log::mask(".?", Log::Debug);
	} else {
		Log::mask(".?", Log::Info);
	}

	switch (action) {
	case None:
		usage();
	case Compress:
	case Decompress:
		process_files(argc, argv, action, &codec, quiet_output);
		if (samples)
			time_samples();
		if (codec_timing)
			time_stats();
		break;
	default:
		NOTREACHED();
	}

	return (0);
}

static void
compress(int ifd, int ofd, XCodec *codec)
{
	XCodecEncoder encoder(codec->cache());
	Buffer input, output;

	while (fill(ifd, &input)) {
		if (codec_timing)
			codec_timer.start();
		encoder.encode(&output, &input);
		if (codec_timing)
			codec_timer.stop();
		flush(ofd, &output);
	}
	ASSERT(input.empty());
	ASSERT(output.empty());
}

static void
decompress(int ifd, int ofd, XCodec *codec)
{
	std::set<uint64_t> unknown_hashes;
	XCodecDecoder decoder(codec->cache());
	Buffer input, output;

	(void)codec;

	while (fill(ifd, &input)) {
		if (codec_timing)
			codec_timer.start();
		if (!decoder.decode(&output, &input, unknown_hashes)) {
			ERROR("/decompress") << "Decode failed.";
			return;
		}
		if (codec_timing)
			codec_timer.stop();
		if (!unknown_hashes.empty()) {
			ERROR("/decompress") << "Cannot decode stream with unknown hashes.";
			return;
		}
		flush(ofd, &output);
	}
	ASSERT(input.empty());
	ASSERT(output.empty());
}

static bool
fill(int fd, Buffer *input)
{
	uint8_t data[65536];
	ssize_t len;

	len = read(fd, data, sizeof data);
	if (len == -1)
		HALT("/fill") << "read failed.";
	if (len == 0)
		return (false);
	input->append(data, len);
	return (true);
}

static void
flush(int fd, Buffer *output)
{
	ssize_t len;

	if (fd == -1) {
		output->clear();
		return;
	}

	for (;;) {
		Buffer::SegmentIterator iter = output->segments();
		if (iter.end())
			break;

		const BufferSegment *seg = *iter;

		len = write(fd, seg->data(), seg->length());
		if (len != (ssize_t)seg->length())
			HALT("/output") << "write failed.";
		output->skip((size_t)len);
	}
	ASSERT(output->empty());
}

static void
process_file(int ifd, int ofd, FileAction action, XCodec *codec)
{
	switch (action) {
	case Compress:
		compress(ifd, ofd, codec);
		break;
	case Decompress:
		decompress(ifd, ofd, codec);
		break;
	default:
		NOTREACHED();
	}
}

static void
process_files(int argc, char *argv[], FileAction action, XCodec *codec, bool quiet)
{
	int ifd, ofd;

	if (argc == 0) {
		ifd = STDIN_FILENO;
		if (quiet)
			ofd = -1;
		else
			ofd = STDOUT_FILENO;
		process_file(ifd, ofd, action, codec);
	} else {
		while (argc--) {
			const char *file = *argv++;

			ifd = open(file, O_RDONLY);
			ASSERT(ifd != -1);
			if (quiet)
				ofd = -1;
			else
				ofd = STDOUT_FILENO;

			process_file(ifd, ofd, action, codec);
		}
	}
}

static void
time_samples(void)
{
	std::vector<uintmax_t> samples = codec_timer.samples();
	std::vector<uintmax_t>::iterator it;

	for (it = samples.begin(); it != samples.end(); ++it)
		fprintf(stderr, "%ju\n", *it);
}

static void
time_stats(void)
{
	std::vector<uintmax_t> samples = codec_timer.samples();
	std::vector<uintmax_t>::iterator it;
	LogHandle log("/codec_timer");
	uintmax_t microseconds;

	INFO(log) << samples.size() << " timer samples.";
	microseconds = 0;
	for (it = samples.begin(); it != samples.end(); ++it)
		microseconds += *it;
	INFO(log) << microseconds << " total runtime.";
	INFO(log) << (microseconds / samples.size()) << " mean microseconds/call.";
}

static void
usage(void)
{
	fprintf(stderr,
"usage: tack [-vQST] -c [file ...]\n"
"       tack [-vQST] -d [file ...]\n");
	exit(1);
}
<commit_msg>Get rid of global variables.<commit_after>#include <sys/types.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>

#include <map>
#include <vector>

#include <common/buffer.h>
#include <common/endian.h>
#include <common/limits.h>
#include <common/timer/timer.h>

#include <xcodec/xcodec.h>
#include <xcodec/xcodec_cache.h>
#include <xcodec/xcodec_decoder.h>
#include <xcodec/xcodec_encoder.h>

enum FileAction {
	None, Compress, Decompress
};

static void compress(int, int, XCodec *, bool, Timer *);
static void decompress(int, int, XCodec *, bool, Timer *);
static bool fill(int, Buffer *);
static void flush(int, Buffer *);
static void process_file(int, int, FileAction, XCodec *, bool, Timer *);
static void process_files(int, char *[], FileAction, XCodec *, bool, bool, Timer *);
static void time_samples(Timer *);
static void time_stats(Timer *);
static void usage(void);

int
main(int argc, char *argv[])
{
	UUID uuid;
	uuid.generate();

	XCodecCache *cache = XCodecCache::lookup(uuid);
	XCodec codec(cache);

	bool codec_timing, quiet_output, samples, verbose;
	FileAction action;
	int ch;

	action = None;
	codec_timing = false;
	quiet_output = false;
	samples = false;
	verbose = false;

	while ((ch = getopt(argc, argv, "?cdvQST")) != -1) {
		switch (ch) {
		case 'c':
			action = Compress;
			break;
		case 'd':
			action = Decompress;
			break;
		case 'v':
			verbose = true;
			break;
		case 'Q':
			quiet_output = true;
			break;
		case 'S':
			samples = true;
			break;
		case 'T':
			codec_timing = true;
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (verbose) {
		Log::mask(".?", Log::Debug);
	} else {
		Log::mask(".?", Log::Info);
	}

	Timer codec_timer;

	switch (action) {
	case None:
		usage();
	case Compress:
	case Decompress:
		process_files(argc, argv, action, &codec, quiet_output, codec_timing, &codec_timer);
		if (samples)
			time_samples(&codec_timer);
		if (codec_timing)
			time_stats(&codec_timer);
		break;
	default:
		NOTREACHED();
	}

	return (0);
}

static void
compress(int ifd, int ofd, XCodec *codec, bool timing, Timer *timer)
{
	XCodecEncoder encoder(codec->cache());
	Buffer input, output;

	while (fill(ifd, &input)) {
		if (timing)
			timer->start();
		encoder.encode(&output, &input);
		if (timing)
			timer->stop();
		flush(ofd, &output);
	}
	ASSERT(input.empty());
	ASSERT(output.empty());
}

static void
decompress(int ifd, int ofd, XCodec *codec, bool timing, Timer *timer)
{
	std::set<uint64_t> unknown_hashes;
	XCodecDecoder decoder(codec->cache());
	Buffer input, output;

	(void)codec;

	while (fill(ifd, &input)) {
		if (timing)
			timer->start();
		if (!decoder.decode(&output, &input, unknown_hashes)) {
			ERROR("/decompress") << "Decode failed.";
			return;
		}
		if (timing)
			timer->stop();
		if (!unknown_hashes.empty()) {
			ERROR("/decompress") << "Cannot decode stream with unknown hashes.";
			return;
		}
		flush(ofd, &output);
	}
	ASSERT(input.empty());
	ASSERT(output.empty());
}

static bool
fill(int fd, Buffer *input)
{
	uint8_t data[65536];
	ssize_t len;

	len = read(fd, data, sizeof data);
	if (len == -1)
		HALT("/fill") << "read failed.";
	if (len == 0)
		return (false);
	input->append(data, len);
	return (true);
}

static void
flush(int fd, Buffer *output)
{
	ssize_t len;

	if (fd == -1) {
		output->clear();
		return;
	}

	for (;;) {
		Buffer::SegmentIterator iter = output->segments();
		if (iter.end())
			break;

		const BufferSegment *seg = *iter;

		len = write(fd, seg->data(), seg->length());
		if (len != (ssize_t)seg->length())
			HALT("/output") << "write failed.";
		output->skip((size_t)len);
	}
	ASSERT(output->empty());
}

static void
process_file(int ifd, int ofd, FileAction action, XCodec *codec, bool timing, Timer *timer)
{
	switch (action) {
	case Compress:
		compress(ifd, ofd, codec, timing, timer);
		break;
	case Decompress:
		decompress(ifd, ofd, codec, timing, timer);
		break;
	default:
		NOTREACHED();
	}
}

static void
process_files(int argc, char *argv[], FileAction action, XCodec *codec, bool quiet, bool timing, Timer *timer)
{
	int ifd, ofd;

	if (argc == 0) {
		ifd = STDIN_FILENO;
		if (quiet)
			ofd = -1;
		else
			ofd = STDOUT_FILENO;
		process_file(ifd, ofd, action, codec, timing, timer);
	} else {
		while (argc--) {
			const char *file = *argv++;

			ifd = open(file, O_RDONLY);
			ASSERT(ifd != -1);
			if (quiet)
				ofd = -1;
			else
				ofd = STDOUT_FILENO;

			process_file(ifd, ofd, action, codec, timing, timer);
		}
	}
}

static void
time_samples(Timer *timer)
{
	std::vector<uintmax_t> samples = timer->samples();
	std::vector<uintmax_t>::iterator it;

	for (it = samples.begin(); it != samples.end(); ++it)
		fprintf(stderr, "%ju\n", *it);
}

static void
time_stats(Timer *timer)
{
	std::vector<uintmax_t> samples = timer->samples();
	std::vector<uintmax_t>::iterator it;
	LogHandle log("/codec_timer");
	uintmax_t microseconds;

	INFO(log) << samples.size() << " timer samples.";
	microseconds = 0;
	for (it = samples.begin(); it != samples.end(); ++it)
		microseconds += *it;
	INFO(log) << microseconds << " total runtime.";
	INFO(log) << (microseconds / samples.size()) << " mean microseconds/call.";
}

static void
usage(void)
{
	fprintf(stderr,
"usage: tack [-vQST] -c [file ...]\n"
"       tack [-vQST] -d [file ...]\n");
	exit(1);
}
<|endoftext|>


<commit_before>/***************************************************************************
 *   Copyright (C) 2006 by FThauer FHammer   *
 *   f.thauer@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <boost/asio.hpp>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <qapplication.h>
#include <QtGui>
#include <QtCore>

#ifdef __APPLE__
	#include <QMacStyle>
#endif

#include <curl/curl.h>

#include "session.h"
#include "startwindowimpl.h"
#include "configfile.h"
#include "startsplash.h"
#include "game_defs.h"
#include <net/socket_startup.h>
#include <third_party/qtsingleapplication/qtsingleapplication.h>

#ifdef _MSC_VER
	#ifdef _DEBUG
		#define _CRTDBG_MAP_ALLOC
		#include <crtdbg.h>

		#define ENABLE_LEAK_CHECK() \
			{ \
				int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); \
				tmpFlag |= _CRTDBG_LEAK_CHECK_DF; \
				_CrtSetDbgFlag(tmpFlag); \
			}
	#endif
#endif

#ifndef ENABLE_LEAK_CHECK
	#define ENABLE_LEAK_CHECK()
#endif

//Uncomment this for RELEASE on Linux/Unix/BSD (static Qt only)
//#include <QtPlugin>
//Q_IMPORT_PLUGIN(qjpeg)
//Q_IMPORT_PLUGIN(qgif)

#ifdef _WIN32 // Always use static Qt on Windows.
#include <QtPlugin>
Q_IMPORT_PLUGIN(qjpeg)
Q_IMPORT_PLUGIN(qgif)
#endif

using namespace std;

class startWindowImpl;
class Game;

int main( int argc, char **argv )
{

	//ENABLE_LEAK_CHECK();

	//_CrtSetBreakAlloc(49937);
	socket_startup();
	curl_global_init(CURL_GLOBAL_NOTHING);

	/////// can be removed for non-qt-guis ////////////
	QtSingleApplication a( argc, argv );

	if (a.sendMessage("Wake up!")) {
		return 0;
	 }

#ifdef __APPLE__
	// The following needs to be done directly after the application is created.
	QDir dir(QApplication::applicationDirPath());
	dir.cdUp();
	dir.cd("plugins");
	QApplication::setLibraryPaths(QStringList(dir.absolutePath()));
#endif

	//create defaultconfig
	ConfigFile *myConfig = new ConfigFile(argv[0], false);

	// set PlastiqueStyle even for mac-version to prevent artefacts on styled widgets
	a.setStyle(new QPlastiqueStyle);

	QString	myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str());
	//set QApplication default font

	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/n019003l.pfb");
	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/VeraBd.ttf");
	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/c059013l.pfb");

#ifdef _WIN32
	QString font1String("font-family: \"Arial\";");
	a.setStyleSheet("QApplication, QWidget, QDialog { " + font1String + " font-size: 12px; }");
#else
// 		QString font1String("font-family: \"Lucida Grande\";");
	QString font1String("font-family: \"Nimbus Sans L\";");

        #ifdef __APPLE__
            a.setStyleSheet("QApplication, QWidget, QDialog { " + font1String + " font-size: 10px; }");
        #else
            a.setStyleSheet("QApplication, QWidget, QDialog { " + font1String + " font-size: 12px; }");
        #endif
#endif
	a.setStyleSheet("QDialogButtonBox, QMessageBox { dialogbuttonbox-buttons-have-icons: 1; dialog-ok-icon: url(:/gfx/dialog_ok_apply.png); dialog-cancel-icon: url(:/gfx/dialog_close.png); dialog-close-icon: url(:/gfx/dialog_close.png); dialog-yes-icon: url(:/gfx/dialog_ok_apply.png); dialog-no-icon: url(:/gfx/dialog_close.png) }");

	QPixmap *pixmap = new QPixmap(myAppDataPath + "gfx/gui/misc/welcomepokerth.png");
	StartSplash splash(*pixmap);
	if(!myConfig->readConfigInt("DisableSplashScreenOnStartup")) {
		splash.show();
		splash.showMessage(QString("Version %1").arg(POKERTH_BETA_RELEASE_STRING), 0x0042, QColor(153,213,0));
	}

	//Set translations
	QTranslator qtTranslator;
        qtTranslator.load(QString(myAppDataPath +"translations/qt_") + QString::fromStdString(myConfig->readConfigString("Language")));
        a.installTranslator(&qtTranslator);

	QTranslator translator;
	translator.load(QString(myAppDataPath +"translations/pokerth_") + QString::fromStdString(myConfig->readConfigString("Language")));
	a.installTranslator(&translator);

	qRegisterMetaType<unsigned>("unsigned");
	qRegisterMetaType<boost::shared_ptr<Game> >("boost::shared_ptr<Game>");
	qRegisterMetaType<ServerStats>("ServerStats");
        qRegisterMetaType<DenyGameInvitationReason>("DenyGameInvitationReason");
	///////////////////////////////////////////////////

        startWindowImpl mainWin(myConfig);
                a.setActivationWindow(&mainWin, true);

	int retVal = a.exec();

	curl_global_cleanup();
	socket_cleanup();

	return retVal;

}
<commit_msg>mac font fix<commit_after>/***************************************************************************
 *   Copyright (C) 2006 by FThauer FHammer   *
 *   f.thauer@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <boost/asio.hpp>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <qapplication.h>
#include <QtGui>
#include <QtCore>

#ifdef __APPLE__
	#include <QMacStyle>
#endif

#include <curl/curl.h>

#include "session.h"
#include "startwindowimpl.h"
#include "configfile.h"
#include "startsplash.h"
#include "game_defs.h"
#include <net/socket_startup.h>
#include <third_party/qtsingleapplication/qtsingleapplication.h>

#ifdef _MSC_VER
	#ifdef _DEBUG
		#define _CRTDBG_MAP_ALLOC
		#include <crtdbg.h>

		#define ENABLE_LEAK_CHECK() \
			{ \
				int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); \
				tmpFlag |= _CRTDBG_LEAK_CHECK_DF; \
				_CrtSetDbgFlag(tmpFlag); \
			}
	#endif
#endif

#ifndef ENABLE_LEAK_CHECK
	#define ENABLE_LEAK_CHECK()
#endif

//Uncomment this for RELEASE on Linux/Unix/BSD (static Qt only)
//#include <QtPlugin>
//Q_IMPORT_PLUGIN(qjpeg)
//Q_IMPORT_PLUGIN(qgif)

#ifdef _WIN32 // Always use static Qt on Windows.
#include <QtPlugin>
Q_IMPORT_PLUGIN(qjpeg)
Q_IMPORT_PLUGIN(qgif)
#endif

using namespace std;

class startWindowImpl;
class Game;

int main( int argc, char **argv )
{

	//ENABLE_LEAK_CHECK();

	//_CrtSetBreakAlloc(49937);
	socket_startup();
	curl_global_init(CURL_GLOBAL_NOTHING);

	/////// can be removed for non-qt-guis ////////////
	QtSingleApplication a( argc, argv );

	if (a.sendMessage("Wake up!")) {
		return 0;
	 }

#ifdef __APPLE__
	// The following needs to be done directly after the application is created.
	QDir dir(QApplication::applicationDirPath());
	dir.cdUp();
	dir.cd("plugins");
	QApplication::setLibraryPaths(QStringList(dir.absolutePath()));
#endif

	//create defaultconfig
	ConfigFile *myConfig = new ConfigFile(argv[0], false);

	// set PlastiqueStyle even for mac-version to prevent artefacts on styled widgets
	a.setStyle(new QPlastiqueStyle);

	QString	myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str());
	//set QApplication default font

	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/n019003l.pfb");
	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/VeraBd.ttf");
	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/c059013l.pfb");

#ifdef _WIN32
	QString font1String("font-family: \"Arial\";");
	a.setStyleSheet("QApplication, QWidget, QDialog { " + font1String + " font-size: 12px; }");
#else
// 		QString font1String("font-family: \"Lucida Grande\";");
	QString font1String("font-family: \"Nimbus Sans L\";");

        #ifdef __APPLE__
            a.setStyleSheet("QApplication, QWidget, QDialog { " + font1String + " font-size: 10pt; }");
        #else
            a.setStyleSheet("QApplication, QWidget, QDialog { " + font1String + " font-size: 12px; }");
        #endif
#endif
	a.setStyleSheet("QDialogButtonBox, QMessageBox { dialogbuttonbox-buttons-have-icons: 1; dialog-ok-icon: url(:/gfx/dialog_ok_apply.png); dialog-cancel-icon: url(:/gfx/dialog_close.png); dialog-close-icon: url(:/gfx/dialog_close.png); dialog-yes-icon: url(:/gfx/dialog_ok_apply.png); dialog-no-icon: url(:/gfx/dialog_close.png) }");

	QPixmap *pixmap = new QPixmap(myAppDataPath + "gfx/gui/misc/welcomepokerth.png");
	StartSplash splash(*pixmap);
	if(!myConfig->readConfigInt("DisableSplashScreenOnStartup")) {
		splash.show();
		splash.showMessage(QString("Version %1").arg(POKERTH_BETA_RELEASE_STRING), 0x0042, QColor(153,213,0));
	}

	//Set translations
	QTranslator qtTranslator;
        qtTranslator.load(QString(myAppDataPath +"translations/qt_") + QString::fromStdString(myConfig->readConfigString("Language")));
        a.installTranslator(&qtTranslator);

	QTranslator translator;
	translator.load(QString(myAppDataPath +"translations/pokerth_") + QString::fromStdString(myConfig->readConfigString("Language")));
	a.installTranslator(&translator);

	qRegisterMetaType<unsigned>("unsigned");
	qRegisterMetaType<boost::shared_ptr<Game> >("boost::shared_ptr<Game>");
	qRegisterMetaType<ServerStats>("ServerStats");
        qRegisterMetaType<DenyGameInvitationReason>("DenyGameInvitationReason");
	///////////////////////////////////////////////////

        startWindowImpl mainWin(myConfig);
                a.setActivationWindow(&mainWin, true);

	int retVal = a.exec();

	curl_global_cleanup();
	socket_cleanup();

	return retVal;

}
<|endoftext|>


<commit_before>/**
 * Copyright (c) 2015 - The CM Authors <legal@clickmatcher.com>
 *   All Rights Reserved.
 *
 * This file is CONFIDENTIAL -- Distribution or duplication of this material or
 * the information contained herein is strictly forbidden unless prior written
 * permission is obtained.
 */
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
#include "fnord-base/io/filerepository.h"
#include "fnord-base/io/fileutil.h"
#include "fnord-base/io/mmappedfile.h"
#include "fnord-base/application.h"
#include "fnord-base/logging.h"
#include "fnord-base/random.h"
#include "fnord-base/thread/eventloop.h"
#include "fnord-base/thread/threadpool.h"
#include "fnord-base/wallclock.h"
#include "fnord-rpc/ServerGroup.h"
#include "fnord-rpc/RPC.h"
#include "fnord-rpc/RPCClient.h"
#include "fnord-base/cli/flagparser.h"
#include "fnord-json/json.h"
#include "fnord-json/jsonrpc.h"
#include "fnord-http/httprouter.h"
#include "fnord-http/httpserver.h"
#include "fnord-feeds/FeedService.h"
#include "fnord-feeds/RemoteFeedFactory.h"
#include "fnord-feeds/RemoteFeedReader.h"
#include "fnord-base/stats/statsdagent.h"
#include "fnord-base/RadixTree.h"
#include "fnord-mdb/MDB.h"
#include "CustomerNamespace.h"
#include "FeatureSchema.h"
#include "FeaturePack.h"
#include "PackedExample.h"
#include "FeatureIndex.h"
#include "JoinedQuery.h"

using namespace fnord;

int main(int argc, const char** argv) {
  fnord::Application::init();
  fnord::Application::logToStderr();

  fnord::cli::FlagParser flags;

  flags.defineFlag(
      "cmdata",
      cli::FlagParser::T_STRING,
      true,
      NULL,
      NULL,
      "clickmatcher app data dir",
      "<path>");

  flags.defineFlag(
      "cmcustomer",
      cli::FlagParser::T_STRING,
      true,
      NULL,
      NULL,
      "clickmatcher customer",
      "<key>");

  flags.defineFlag(
      "features",
      fnord::cli::FlagParser::T_STRING,
      true,
      "f",
      NULL,
      "features file",
      "<filename>");

  flags.defineFlag(
      "input",
      fnord::cli::FlagParser::T_STRING,
      true,
      "i",
      NULL,
      "input file",
      "<filename>");

  flags.defineFlag(
      "output",
      fnord::cli::FlagParser::T_STRING,
      true,
      "o",
      NULL,
      "output file",
      "<filename>");

  flags.defineFlag(
      "separator",
      fnord::cli::FlagParser::T_STRING,
      false,
      "s",
      "\n",
      "separator",
      "<char>");

  flags.defineFlag(
      "loglevel",
      fnord::cli::FlagParser::T_STRING,
      false,
      NULL,
      "INFO",
      "loglevel",
      "<level>");

  flags.parseArgv(argc, argv);

  Logger::get()->setMinimumLogLevel(
      strToLogLevel(flags.getString("loglevel")));

  /* args */
  auto cmcustomer = flags.getString("cmcustomer");
  auto featurefile_path = flags.getString("features");
  auto inputfile_path = flags.getString("input");
  auto outputfile_path = flags.getString("output");

  auto separator_str = flags.getString("separator");
  if (separator_str.length() != 1) {
    // print usage
    return 1;
  }

  char separator = separator_str[0];

  /* set up and read output feature schema */
  cm::FeatureSchema output_feature_schema;
  output_feature_schema.load(FileUtil::read(featurefile_path));

  /* set up featuredb schema */
  cm::FeatureSchema featuredb_schema;
  featuredb_schema.registerFeature("shop_id", 1, 1);
  featuredb_schema.registerFeature("category1", 2, 1);
  featuredb_schema.registerFeature("category2", 3, 1);
  featuredb_schema.registerFeature("category3", 4, 1);

  /* set up cmdata */
  auto cmdata_path = flags.getString("cmdata");
  if (!FileUtil::isDirectory(cmdata_path)) {
    RAISEF(kIOError, "no such directory: $0", cmdata_path);
  }

  /* open featuredb */
  auto featuredb_path = FileUtil::joinPaths(
      cmdata_path,
      StringUtil::format("index/$0/db", cmcustomer));

  auto featuredb = mdb::MDB::open(featuredb_path, true);
  cm::FeatureIndex feature_index(featuredb, &featuredb_schema);
  auto featuredb_txn = featuredb->startTransaction(true);

  /* open output file */
  auto outfile = File::openFile(
      outputfile_path,
      File::O_READ | File::O_WRITE | File::O_CREATE);

  /* mmap input file */
  io::MmappedFile mmap(File::openFile(inputfile_path, File::O_READ));
  madvise(mmap.data(), mmap.size(), MADV_WILLNEED);

  auto begin = (char *) mmap.begin();
  auto cur = begin;
  auto end = (char *) mmap.end();
  auto last = cur;
  uint64_t total_entries = 0;
  uint64_t total_bytes = 0;
  auto start_time = WallClock::now().unixMicros();
  auto last_status_line = start_time;

  /* status line */
  auto status_line = [
      begin,
      end,
      &cur,
      &total_bytes,
      &total_entries,
      &start_time,
      &last_status_line] {
    auto now = WallClock::now().unixMicros();
    if (now - last_status_line < 10000) {
      return;
    }

    last_status_line = now;
    auto runtime = (now - start_time) / 1000000;
    int percent = ((double) (cur - begin) / (double) (end - begin)) * 100;
    uint64_t bandwidth = total_bytes / (runtime + 1);

    auto str = StringUtil::format(
        "\r[$0%] scanning: entries=$1 bytes=$2B time=$3s bw=$4B/s" \
        "          ",
        percent,
        total_entries,
        total_bytes,
        runtime,
        bandwidth);

    write(0, str.c_str(), str.length());
    fflush(0);
  };

  HashMap<String, uint64_t> feature_counts;

  /* scan entries */
  for (; cur != end; ++cur) {
    status_line();

    if (*cur != separator) {
      continue;
    }

    if (cur > last + 1) {
      try {
        auto query = json::fromJSON<cm::JoinedQuery>(String(last, cur - last));

        for (const auto& item : query.items) {
          if (item.position > 16) {
            continue;
          }

          cm::Example ex;
          ex.label = item.clicked ? 1.0 : -1.0;
          ex.features = joinedQueryItemFeatures(
              query,
              item,
              &featuredb_schema,
              &feature_index,
              featuredb_txn.get());

          auto ex_str = exampleToSVMLight(ex, &output_feature_schema);
          if (ex_str.length() > 0) {
            outfile.write(ex_str + "\n");
          }
        }
      } catch (const Exception& e) {
        fnord::logError("cm.featuredump", e, "error");
      }

      total_bytes += cur - last;
      ++total_entries;
    }

    last = cur + 1;
  }

  status_line();
  write(0, "\n", 1);
  fflush(0);

  /* clean up */
  featuredb_txn->abort();
  exit(0);
}

<commit_msg>count feature observations in cm-featuredump<commit_after>/**
 * Copyright (c) 2015 - The CM Authors <legal@clickmatcher.com>
 *   All Rights Reserved.
 *
 * This file is CONFIDENTIAL -- Distribution or duplication of this material or
 * the information contained herein is strictly forbidden unless prior written
 * permission is obtained.
 */
#include <algorithm>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include "fnord-base/io/fileutil.h"
#include "fnord-base/application.h"
#include "fnord-base/logging.h"
#include "fnord-base/cli/flagparser.h"
#include "fnord-base/util/SimpleRateLimit.h"
#include "fnord-base/InternMap.h"
#include "fnord-json/json.h"
#include "fnord-mdb/MDB.h"
#include "fnord-mdb/MDBUtil.h"
#include "fnord-sstable/sstablereader.h"
#include "fnord-sstable/sstablewriter.h"
#include "fnord-sstable/SSTableColumnSchema.h"
#include "fnord-sstable/SSTableColumnReader.h"
#include "fnord-sstable/SSTableColumnWriter.h"
#include "common.h"

using namespace fnord;

int main(int argc, const char** argv) {
  fnord::Application::init();
  fnord::Application::logToStderr();

  fnord::cli::FlagParser flags;

  flags.defineFlag(
      "output_file",
      fnord::cli::FlagParser::T_STRING,
      true,
      "o",
      NULL,
      "output file",
      "<filename>");

  flags.defineFlag(
      "min_observations",
      fnord::cli::FlagParser::T_INTEGER,
      false,
      NULL,
      "100",
      "minimum nuber of feature observations",
      "<num>");

  flags.defineFlag(
      "loglevel",
      fnord::cli::FlagParser::T_STRING,
      false,
      NULL,
      "INFO",
      "loglevel",
      "<level>");

  flags.parseArgv(argc, argv);

  Logger::get()->setMinimumLogLevel(
      strToLogLevel(flags.getString("loglevel")));

  auto sstables = flags.getArgv();
  if (sstables.size() == 0) {
    fnord::logCritical("cm.featuredump", "No input tables, exiting");
    return 1;
  }

  /* read input meta tables and count features */
  HashMap<String, uint64_t> feature_counts;
  for (int tbl_idx = 0; tbl_idx < sstables.size(); ++tbl_idx) {
    String sstable = sstables[tbl_idx];
    StringUtil::replaceAll(&sstable, ".sstable", "_meta.sstable");
    fnord::logInfo("cm.featuredump", "Importing sstable: $0", sstable);

    /* read sstable heade r*/
    sstable::SSTableReader reader(File::openFile(sstable, File::O_READ));
    sstable::SSTableColumnSchema schema;
    schema.loadIndex(&reader);

    if (reader.bodySize() == 0) {
      fnord::logCritical("cm.featuredump", "unfinished sstable: $0", sstable);
      exit(1);
    }

    /* get sstable cursor */
    auto cursor = reader.getCursor();
    auto body_size = reader.bodySize();
    int row_idx = 0;

    /* status line */
    util::SimpleRateLimitedFn status_line(kMicrosPerSecond, [&] () {
      fnord::logInfo(
          "cm.featuredump",
          "[$1/$2] [$0%] Reading sstable... rows=$3",
          (size_t) ((cursor->position() / (double) body_size) * 100),
          tbl_idx + 1, sstables.size(), row_idx);
    });

    /* read sstable rows */
    for (; cursor->valid(); ++row_idx) {
      status_line.runMaybe();

      auto key = cursor->getKeyString();
      auto val = cursor->getDataBuffer();

      sstable::SSTableColumnReader cols(&schema, val);
      auto cnt = cols.getUInt64Column(schema.columnID("count"));

      feature_counts[key] += cnt;

      if (!cursor->next()) {
        break;
      }
    }

    status_line.runForce();
  }

  /* select features for export */
  Set<String> features;
  auto orig_count = feature_counts.size();
  auto min_count = flags.getInt("min_observations");
  for (auto iter = feature_counts.begin(); iter != feature_counts.end(); ) {
    if (iter->second > min_count) {
      features.emplace(iter->first);
    }

    iter = feature_counts.erase(iter);
  }

  feature_counts.clear();

  fnord::logInfo(
      "cm.featuredump",
      "Exporting $1/$0 features",
      orig_count,
      features.size());

  return 0;
}

<|endoftext|>


<commit_before>/**
 * Copyright (c) 2015 - The CM Authors <legal@clickmatcher.com>
 *   All Rights Reserved.
 *
 * This file is CONFIDENTIAL -- Distribution or duplication of this material or
 * the information contained herein is strictly forbidden unless prior written
 * permission is obtained.
 */
#include <algorithm>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include "fnord-base/io/fileutil.h"
#include "fnord-base/application.h"
#include "fnord-base/logging.h"
#include "fnord-base/cli/flagparser.h"
#include "fnord-base/util/SimpleRateLimit.h"
#include "fnord-base/InternMap.h"
#include "fnord-json/json.h"
#include "fnord-mdb/MDB.h"
#include "fnord-mdb/MDBUtil.h"
#include "fnord-sstable/sstablereader.h"
#include "fnord-sstable/sstablewriter.h"
#include "fnord-sstable/SSTableColumnSchema.h"
#include "fnord-sstable/SSTableColumnReader.h"
#include "fnord-sstable/SSTableColumnWriter.h"
#include "fnord-cstable/BitPackedIntColumnReader.h"
#include "fnord-cstable/BitPackedIntColumnWriter.h"
#include "fnord-cstable/UInt32ColumnReader.h"
#include "fnord-cstable/UInt32ColumnWriter.h"
#include "fnord-cstable/BooleanColumnReader.h"
#include "fnord-cstable/BooleanColumnWriter.h"
#include "fnord-cstable/CSTableWriter.h"
#include "fnord-cstable/CSTableReader.h"
#include "common.h"
#include "CustomerNamespace.h"
#include "FeatureSchema.h"
#include "JoinedQuery.h"
#include "CTRCounter.h"
#include "analytics/AnalyticsTableScan.h"
#include "analytics/CTRByPositionQuery.h"

using namespace cm;
using namespace fnord;

int main(int argc, const char** argv) {
  fnord::Application::init();
  fnord::Application::logToStderr();

  fnord::cli::FlagParser flags;

  flags.defineFlag(
      "input_file",
      fnord::cli::FlagParser::T_STRING,
      true,
      "i",
      NULL,
      "file",
      "<filename>");

  flags.defineFlag(
      "output_file",
      fnord::cli::FlagParser::T_STRING,
      true,
      "o",
      NULL,
      "file",
      "<filename>");

  flags.defineFlag(
      "loglevel",
      fnord::cli::FlagParser::T_STRING,
      false,
      NULL,
      "INFO",
      "loglevel",
      "<level>");

  flags.parseArgv(argc, argv);

  Logger::get()->setMinimumLogLevel(
      strToLogLevel(flags.getString("loglevel")));

  size_t debug_n = 0;
  size_t debug_z = 0;

  /* query level */
  cstable::UInt32ColumnWriter jq_time_col(1, 1);
  cstable::BitPackedIntColumnWriter jq_page_col(1, 1, 100);
  cstable::BitPackedIntColumnWriter jq_lang_col(1, 1, kMaxLanguage);
  cstable::BitPackedIntColumnWriter jq_numitems_col(1, 1, 250);
  cstable::BitPackedIntColumnWriter jq_numitemclicks_col(1, 1, 250);
  cstable::BitPackedIntColumnWriter jq_numadimprs_col(1, 1, 250);
  cstable::BitPackedIntColumnWriter jq_numadclicks_col(1, 1, 250);
  cstable::BitPackedIntColumnWriter jq_abtestgroup_col(1, 1, 100);
  cstable::BitPackedIntColumnWriter jq_devicetype_col(1, 1, kMaxDeviceType);
  cstable::BitPackedIntColumnWriter jq_pagetype_col(1, 1, kMaxPageType);
  cstable::BitPackedIntColumnWriter jq_cat1_col(1, 1, 0xffff);
  cstable::BitPackedIntColumnWriter jq_cat2_col(1, 1, 0xffff);
  cstable::BitPackedIntColumnWriter jq_cat3_col(1, 1, 0xffff);

  /* query item level */
  cstable::BitPackedIntColumnWriter jqi_position_col(2, 2, 64);
  cstable::BooleanColumnWriter jqi_clicked_col(2, 2);

  uint64_t r = 0;
  uint64_t n = 0;

  auto add_session = [&] (const cm::JoinedSession& sess) {
    ++n;

    for (const auto& q : sess.queries) {
      /* queries.time */
      jq_time_col.addDatum(r, 1, q.time.unixMicros() / kMicrosPerSecond);

      /* queries.language */
      auto lang = cm::extractLanguage(q.attrs);
      uint32_t l = (uint16_t) lang;
      jq_lang_col.addDatum(r, 1, l);

      /* queries.num_item_clicks, queries.num_items */
      size_t nitems = 0;
      size_t nclicks = 0;
      size_t nads = 0;
      size_t nadclicks = 0;
      for (const auto& i : q.items) {
        // DAWANDA HACK
        if (i.position >= 1 && i.position <= 4) {
          ++nads;
          nadclicks += i.clicked;
        }
        // EOF DAWANDA HACK

        ++nitems;
        nclicks += i.clicked;
      }

      if (nadclicks > 0 && lang != Language::DE) {
        abort();
      }

      jq_numitems_col.addDatum(r, 1, nitems);
      jq_numitemclicks_col.addDatum(r, 1, nclicks);
      jq_numadimprs_col.addDatum(r, 1, nads);
      jq_numadclicks_col.addDatum(r, 1, nadclicks);

      /* queries.page */
      auto pg_str = cm::extractAttr(q.attrs, "pg");
      if (pg_str.isEmpty()) {
        jq_page_col.addNull(r, 0);
      } else {
        jq_page_col.addDatum(r, 1, std::stoul(pg_str.get()));
      }

      /* queries.ab_test_group */
      auto abgrp = cm::extractABTestGroup(q.attrs);
      if (abgrp.isEmpty()) {
        jq_abtestgroup_col.addNull(r, 1);
      } else {
        jq_abtestgroup_col.addDatum(r, 1, abgrp.get());
      }

      /* queries.category1 */
      auto qcat1 = cm::extractAttr(q.attrs, "q_cat1");
      if (qcat1.isEmpty()) {
        jq_cat1_col.addNull(r, 1);
      } else {
        jq_cat1_col.addDatum(r, 1, std::stoul(qcat1.get()));
      }

      /* queries.category2 */
      auto qcat2 = cm::extractAttr(q.attrs, "q_cat2");
      if (qcat2.isEmpty()) {
        jq_cat2_col.addNull(r, 1);
      } else {
        jq_cat2_col.addDatum(r, 1, std::stoul(qcat2.get()));
      }

      /* queries.category3 */
      auto qcat3 = cm::extractAttr(q.attrs, "q_cat3");
      if (qcat3.isEmpty()) {
        jq_cat3_col.addNull(r, 1);
      } else {
        jq_cat3_col.addDatum(r, 1, std::stoul(qcat3.get()));
      }

      /* queries.device_type */
      jq_devicetype_col.addDatum(r, 1, (uint32_t) extractDeviceType(q.attrs));

      /* queries.page_type */
      jq_pagetype_col.addDatum(r, 1, (uint32_t) extractPageType(q.attrs));

      if (q.items.size() == 0) {
        jqi_position_col.addNull(r, 1);
        jqi_clicked_col.addNull(r, 1);
      }

      for (const auto& i : q.items) {
        jqi_position_col.addDatum(r, 2, i.position);
        jqi_clicked_col.addDatum(r, 2, i.clicked);
        r = 2;
      }

      r = 1;
    }

    r = 0;
  };

  /* read input tables */
  int row_idx = 0;

  const auto& sstable = flags.getString("input_file");
  fnord::logInfo("cm.jqcolumnize", "Importing sstable: $0", sstable);

  /* read sstable header */
  sstable::SSTableReader reader(File::openFile(sstable, File::O_READ));

  if (reader.bodySize() == 0) {
    fnord::logCritical("cm.jqcolumnize", "unfinished sstable: $0", sstable);
    exit(1);
  }

  /* get sstable cursor */
  auto cursor = reader.getCursor();
  auto body_size = reader.bodySize();

  /* status line */
  util::SimpleRateLimitedFn status_line(kMicrosPerSecond, [&] () {
    fnord::logInfo(
        "cm.jqcolumnize",
        "[$0%] Reading sstable... rows=$1",
        (size_t) ((cursor->position() / (double) body_size) * 100),
        row_idx);
  });

  /* read sstable rows */
  for (; cursor->valid(); ++row_idx) {
    status_line.runMaybe();

    auto key = cursor->getKeyString();
    auto val = cursor->getDataBuffer();
    Option<cm::JoinedQuery> q;

    try {
      q = Some(json::fromJSON<cm::JoinedQuery>(val));
    } catch (const Exception& e) {
      fnord::logWarning("cm.jqcolumnize", e, "invalid json: $0", val.toString());
    }

    if (!q.isEmpty()) {
      cm::JoinedSession s;
      s.queries.emplace_back(q.get());
      add_session(s);
    }

    if (!cursor->next()) {
      break;
    }
  }

  status_line.runForce();

  {
    cstable::CSTableWriter writer(flags.getString("output_file") + "~", n);
    writer.addColumn("queries.time", &jq_time_col);
    writer.addColumn("queries.page", &jq_page_col);
    writer.addColumn("queries.language", &jq_lang_col);
    writer.addColumn("queries.num_items", &jq_numitems_col);
    writer.addColumn("queries.num_items_clicked", &jq_numitemclicks_col);
    writer.addColumn("queries.num_ad_impressions", &jq_numadimprs_col);
    writer.addColumn("queries.num_ad_clicks", &jq_numadclicks_col);
    writer.addColumn("queries.ab_test_group", &jq_abtestgroup_col);
    writer.addColumn("queries.device_type", &jq_devicetype_col);
    writer.addColumn("queries.page_type", &jq_pagetype_col);
    writer.addColumn("queries.category1", &jq_cat1_col);
    writer.addColumn("queries.category2", &jq_cat2_col);
    writer.addColumn("queries.category3", &jq_cat3_col);
    writer.addColumn("queries.items.position", &jqi_position_col);
    writer.addColumn("queries.items.clicked", &jqi_clicked_col);
    writer.commit();
  }

  FileUtil::mv(
      flags.getString("output_file") + "~",
      flags.getString("output_file"));

  //{
  //  cstable::CSTableReader reader(flags.getString("output_file"));
  //  auto t0 = WallClock::unixMicros();

  //  cm::AnalyticsTableScan aq;
  //  auto lcol = aq.fetchColumn("queries.language");
  //  auto ccol = aq.fetchColumn("queries.num_ad_clicks");

  //  aq.onQuery([&] () {
  //    auto l = languageToString((Language) lcol->getUInt32());
  //    auto c = ccol->getUInt32();
  //    fnord::iputs("lang: $0 -> $1", l, c);
  //  });

  //  aq.scanTable(&reader);
  //  //cm::CTRByGroupResult<uint16_t> res;
  //  //cm::CTRByPositionQuery q(&aq, &res);
  //  //auto t1 = WallClock::unixMicros();
  //  //fnord::iputs("scanned $0 rows in $1 ms", res.rows_scanned, (t1 - t0) / 1000.0f);
  //  //for (const auto& p : res.counters) {
  //  //  fnord::iputs(
  //  //     "pos: $0, views: $1, clicks: $2, ctr: $3",
  //  //      p.first, p.second.num_views,
  //  //      p.second.num_clicks,
  //  //      p.second.num_clicks / (double) p.second.num_views);
  //  //}
  //}

  return 0;
}

<commit_msg>fixes<commit_after>/**
 * Copyright (c) 2015 - The CM Authors <legal@clickmatcher.com>
 *   All Rights Reserved.
 *
 * This file is CONFIDENTIAL -- Distribution or duplication of this material or
 * the information contained herein is strictly forbidden unless prior written
 * permission is obtained.
 */
#include <algorithm>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include "fnord-base/io/fileutil.h"
#include "fnord-base/application.h"
#include "fnord-base/logging.h"
#include "fnord-base/cli/flagparser.h"
#include "fnord-base/util/SimpleRateLimit.h"
#include "fnord-base/InternMap.h"
#include "fnord-json/json.h"
#include "fnord-mdb/MDB.h"
#include "fnord-mdb/MDBUtil.h"
#include "fnord-sstable/sstablereader.h"
#include "fnord-sstable/sstablewriter.h"
#include "fnord-sstable/SSTableColumnSchema.h"
#include "fnord-sstable/SSTableColumnReader.h"
#include "fnord-sstable/SSTableColumnWriter.h"
#include "fnord-cstable/BitPackedIntColumnReader.h"
#include "fnord-cstable/BitPackedIntColumnWriter.h"
#include "fnord-cstable/UInt32ColumnReader.h"
#include "fnord-cstable/UInt32ColumnWriter.h"
#include "fnord-cstable/BooleanColumnReader.h"
#include "fnord-cstable/BooleanColumnWriter.h"
#include "fnord-cstable/CSTableWriter.h"
#include "fnord-cstable/CSTableReader.h"
#include "common.h"
#include "CustomerNamespace.h"
#include "FeatureSchema.h"
#include "JoinedQuery.h"
#include "CTRCounter.h"
#include "analytics/AnalyticsTableScan.h"
#include "analytics/CTRByPositionQuery.h"

using namespace cm;
using namespace fnord;

int main(int argc, const char** argv) {
  fnord::Application::init();
  fnord::Application::logToStderr();

  fnord::cli::FlagParser flags;

  flags.defineFlag(
      "input_file",
      fnord::cli::FlagParser::T_STRING,
      true,
      "i",
      NULL,
      "file",
      "<filename>");

  flags.defineFlag(
      "output_file",
      fnord::cli::FlagParser::T_STRING,
      true,
      "o",
      NULL,
      "file",
      "<filename>");

  flags.defineFlag(
      "loglevel",
      fnord::cli::FlagParser::T_STRING,
      false,
      NULL,
      "INFO",
      "loglevel",
      "<level>");

  flags.parseArgv(argc, argv);

  Logger::get()->setMinimumLogLevel(
      strToLogLevel(flags.getString("loglevel")));

  size_t debug_n = 0;
  size_t debug_z = 0;

  /* query level */
  cstable::UInt32ColumnWriter jq_time_col(1, 1);
  cstable::BitPackedIntColumnWriter jq_page_col(1, 1, 100);
  cstable::BitPackedIntColumnWriter jq_lang_col(1, 1, kMaxLanguage);
  cstable::BitPackedIntColumnWriter jq_numitems_col(1, 1, 250);
  cstable::BitPackedIntColumnWriter jq_numitemclicks_col(1, 1, 250);
  cstable::BitPackedIntColumnWriter jq_numadimprs_col(1, 1, 250);
  cstable::BitPackedIntColumnWriter jq_numadclicks_col(1, 1, 250);
  cstable::BitPackedIntColumnWriter jq_abtestgroup_col(1, 1, 100);
  cstable::BitPackedIntColumnWriter jq_devicetype_col(1, 1, kMaxDeviceType);
  cstable::BitPackedIntColumnWriter jq_pagetype_col(1, 1, kMaxPageType);
  cstable::BitPackedIntColumnWriter jq_cat1_col(1, 1, 0xffff);
  cstable::BitPackedIntColumnWriter jq_cat2_col(1, 1, 0xffff);
  cstable::BitPackedIntColumnWriter jq_cat3_col(1, 1, 0xffff);

  /* query item level */
  cstable::BitPackedIntColumnWriter jqi_position_col(2, 2, 64);
  cstable::BooleanColumnWriter jqi_clicked_col(2, 2);

  uint64_t r = 0;
  uint64_t n = 0;

  auto add_session = [&] (const cm::JoinedSession& sess) {
    ++n;

    for (const auto& q : sess.queries) {
      /* queries.time */
      jq_time_col.addDatum(r, 1, q.time.unixMicros() / kMicrosPerSecond);

      /* queries.language */
      auto lang = cm::extractLanguage(q.attrs);
      uint32_t l = (uint16_t) lang;
      jq_lang_col.addDatum(r, 1, l);

      /* queries.num_item_clicks, queries.num_items */
      size_t nitems = 0;
      size_t nclicks = 0;
      size_t nads = 0;
      size_t nadclicks = 0;
      for (const auto& i : q.items) {
        // DAWANDA HACK
        if (i.position >= 1 && i.position <= 4) {
          ++nads;
          nadclicks += i.clicked;
        }
        // EOF DAWANDA HACK

        ++nitems;
        nclicks += i.clicked;
      }

      if (nadclicks > 0 && lang != Language::DE) {
        abort();
      }

      jq_numitems_col.addDatum(r, 1, nitems);
      jq_numitemclicks_col.addDatum(r, 1, nclicks);
      jq_numadimprs_col.addDatum(r, 1, nads);
      jq_numadclicks_col.addDatum(r, 1, nadclicks);

      /* queries.page */
      auto pg_str = cm::extractAttr(q.attrs, "pg");
      if (pg_str.isEmpty()) {
        jq_page_col.addNull(r, 0);
      } else {
        jq_page_col.addDatum(r, 1, std::stoul(pg_str.get()));
      }

      /* queries.ab_test_group */
      auto abgrp = cm::extractABTestGroup(q.attrs);
      if (abgrp.isEmpty()) {
        jq_abtestgroup_col.addNull(r, 0);
      } else {
        jq_abtestgroup_col.addDatum(r, 1, abgrp.get());
      }

      /* queries.category1 */
      auto qcat1 = cm::extractAttr(q.attrs, "q_cat1");
      if (qcat1.isEmpty()) {
        jq_cat1_col.addNull(r, 0);
      } else {
        jq_cat1_col.addDatum(r, 1, std::stoul(qcat1.get()));
      }

      /* queries.category2 */
      auto qcat2 = cm::extractAttr(q.attrs, "q_cat2");
      if (qcat2.isEmpty()) {
        jq_cat2_col.addNull(r, 0);
      } else {
        jq_cat2_col.addDatum(r, 1, std::stoul(qcat2.get()));
      }

      /* queries.category3 */
      auto qcat3 = cm::extractAttr(q.attrs, "q_cat3");
      if (qcat3.isEmpty()) {
        jq_cat3_col.addNull(r, 0);
      } else {
        jq_cat3_col.addDatum(r, 1, std::stoul(qcat3.get()));
      }

      /* queries.device_type */
      jq_devicetype_col.addDatum(r, 1, (uint32_t) extractDeviceType(q.attrs));

      /* queries.page_type */
      jq_pagetype_col.addDatum(r, 1, (uint32_t) extractPageType(q.attrs));

      if (q.items.size() == 0) {
        jqi_position_col.addNull(r, 1);
        jqi_clicked_col.addNull(r, 1);
      }

      for (const auto& i : q.items) {
        jqi_position_col.addDatum(r, 2, i.position);
        jqi_clicked_col.addDatum(r, 2, i.clicked);
        r = 2;
      }

      r = 1;
    }

    r = 0;
  };

  /* read input tables */
  int row_idx = 0;

  const auto& sstable = flags.getString("input_file");
  fnord::logInfo("cm.jqcolumnize", "Importing sstable: $0", sstable);

  /* read sstable header */
  sstable::SSTableReader reader(File::openFile(sstable, File::O_READ));

  if (reader.bodySize() == 0) {
    fnord::logCritical("cm.jqcolumnize", "unfinished sstable: $0", sstable);
    exit(1);
  }

  /* get sstable cursor */
  auto cursor = reader.getCursor();
  auto body_size = reader.bodySize();

  /* status line */
  util::SimpleRateLimitedFn status_line(kMicrosPerSecond, [&] () {
    fnord::logInfo(
        "cm.jqcolumnize",
        "[$0%] Reading sstable... rows=$1",
        (size_t) ((cursor->position() / (double) body_size) * 100),
        row_idx);
  });

  /* read sstable rows */
  for (; cursor->valid(); ++row_idx) {
    status_line.runMaybe();

    auto key = cursor->getKeyString();
    auto val = cursor->getDataBuffer();
    Option<cm::JoinedQuery> q;

    try {
      q = Some(json::fromJSON<cm::JoinedQuery>(val));
    } catch (const Exception& e) {
      fnord::logWarning("cm.jqcolumnize", e, "invalid json: $0", val.toString());
    }

    if (!q.isEmpty()) {
      cm::JoinedSession s;
      s.queries.emplace_back(q.get());
      add_session(s);
    }

    if (!cursor->next()) {
      break;
    }
  }

  status_line.runForce();

  {
    cstable::CSTableWriter writer(flags.getString("output_file") + "~", n);
    writer.addColumn("queries.time", &jq_time_col);
    writer.addColumn("queries.page", &jq_page_col);
    writer.addColumn("queries.language", &jq_lang_col);
    writer.addColumn("queries.num_items", &jq_numitems_col);
    writer.addColumn("queries.num_items_clicked", &jq_numitemclicks_col);
    writer.addColumn("queries.num_ad_impressions", &jq_numadimprs_col);
    writer.addColumn("queries.num_ad_clicks", &jq_numadclicks_col);
    writer.addColumn("queries.ab_test_group", &jq_abtestgroup_col);
    writer.addColumn("queries.device_type", &jq_devicetype_col);
    writer.addColumn("queries.page_type", &jq_pagetype_col);
    writer.addColumn("queries.category1", &jq_cat1_col);
    writer.addColumn("queries.category2", &jq_cat2_col);
    writer.addColumn("queries.category3", &jq_cat3_col);
    writer.addColumn("queries.items.position", &jqi_position_col);
    writer.addColumn("queries.items.clicked", &jqi_clicked_col);
    writer.commit();
  }

  FileUtil::mv(
      flags.getString("output_file") + "~",
      flags.getString("output_file"));

  //{
  //  cstable::CSTableReader reader(flags.getString("output_file"));
  //  auto t0 = WallClock::unixMicros();

  //  cm::AnalyticsTableScan aq;
  //  auto lcol = aq.fetchColumn("queries.language");
  //  auto ccol = aq.fetchColumn("queries.num_ad_clicks");

  //  aq.onQuery([&] () {
  //    auto l = languageToString((Language) lcol->getUInt32());
  //    auto c = ccol->getUInt32();
  //    fnord::iputs("lang: $0 -> $1", l, c);
  //  });

  //  aq.scanTable(&reader);
  //  //cm::CTRByGroupResult<uint16_t> res;
  //  //cm::CTRByPositionQuery q(&aq, &res);
  //  //auto t1 = WallClock::unixMicros();
  //  //fnord::iputs("scanned $0 rows in $1 ms", res.rows_scanned, (t1 - t0) / 1000.0f);
  //  //for (const auto& p : res.counters) {
  //  //  fnord::iputs(
  //  //     "pos: $0, views: $1, clicks: $2, ctr: $3",
  //  //      p.first, p.second.num_views,
  //  //      p.second.num_clicks,
  //  //      p.second.num_clicks / (double) p.second.num_views);
  //  //}
  //}

  return 0;
}

<|endoftext|>


<commit_before>// kate: indent-mode cstyle; indent-width 4; tab-width 4; space-indent false;
// vim:ai ts=4 et

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#include <QApplication>
#include <QString>
#include <QDebug>

#include <boost/foreach.hpp>

#include <Team.h>

#include "MainWindow.hpp"

using namespace std;

void usage(const char* prog)
{
	printf("usage: %s <-y|-b> [-ng] [-p <play dir>] [-f] [-c <config file>]\n", prog);
	printf("\t-y:  run as the yellow team\n");
	printf("\t-b:  run as the blue team\n");
	printf("\t-ng: no goalie\n");
	printf("\t-c:  specify the configuration file\n");
	printf("\t-f:  flip field\n");
	exit(1);
}

int main (int argc, char* argv[])
{
	// Seed the large random number generator
	long int seed = 0;
	int fd = open("/dev/random", O_RDONLY);
	if (fd)
	{
		if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
		{
			printf("seed %016lx\n", seed);
			srand48(seed);
		} else {
			fprintf(stderr, "Can't read /dev/random: %m\n");
		}
		close(fd);
	} else {
		fprintf(stderr, "Can't open /dev/random: %m\n");
	}

	QApplication app(argc, argv);

	Team team = UnknownTeam;
	QString cfgFile = "";
	vector<const char *> playDirs;

	bool useGoalie = true;
	bool flip = false;
	for (int i=1 ; i<argc; ++i)
	{
		const char* var = argv[i];

		if (strcmp(var, "-ng") == 0)
		{
			useGoalie = false;
		} else if (strcmp(var, "-y") == 0)
		{
			team = Yellow;
		}
		else if (strcmp(var, "-b") == 0)
		{
			team = Blue;
		}
		else if (strcmp(var, "-f") == 0)
		{
			flip = true;
		}
		else if(strcmp(var, "-c") == 0)
		{
			if (i+1 >= argc)
			{
				printf("no config file specified after -c");
				usage(argv[0]);
			}

			i++;
			cfgFile = argv[i];
		}
		else
		{
			printf("Not a valid flag: %s\n", argv[i]);
			usage(argv[0]);
		}
	}

	if (team == UnknownTeam)
	{
		printf("Error: No team specified\n");
		usage(argv[0]);
		return 0;
	}

	MainWindow win(team, cfgFile);

	if (useGoalie)
	{
		win.processor()->gameplayModule()->createGoalie();
	}

	win.processor()->flip_field(flip);

	win.showMaximized();

	return app.exec();
}
<commit_msg>Removed usage flag for plays folder<commit_after>// kate: indent-mode cstyle; indent-width 4; tab-width 4; space-indent false;
// vim:ai ts=4 et

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#include <QApplication>
#include <QString>
#include <QDebug>

#include <boost/foreach.hpp>

#include <Team.h>

#include "MainWindow.hpp"

using namespace std;

void usage(const char* prog)
{
	printf("usage: %s <-y|-b> [-ng] [-f] [-c <config file>]\n", prog);
	printf("\t-y:  run as the yellow team\n");
	printf("\t-b:  run as the blue team\n");
	printf("\t-ng: no goalie\n");
	printf("\t-c:  specify the configuration file\n");
	printf("\t-f:  flip field\n");
	exit(1);
}

int main (int argc, char* argv[])
{
	// Seed the large random number generator
	long int seed = 0;
	int fd = open("/dev/random", O_RDONLY);
	if (fd)
	{
		if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
		{
			printf("seed %016lx\n", seed);
			srand48(seed);
		} else {
			fprintf(stderr, "Can't read /dev/random: %m\n");
		}
		close(fd);
	} else {
		fprintf(stderr, "Can't open /dev/random: %m\n");
	}

	QApplication app(argc, argv);

	Team team = UnknownTeam;
	QString cfgFile = "";
	vector<const char *> playDirs;

	bool useGoalie = true;
	bool flip = false;
	for (int i=1 ; i<argc; ++i)
	{
		const char* var = argv[i];

		if (strcmp(var, "-ng") == 0)
		{
			useGoalie = false;
		} else if (strcmp(var, "-y") == 0)
		{
			team = Yellow;
		}
		else if (strcmp(var, "-b") == 0)
		{
			team = Blue;
		}
		else if (strcmp(var, "-f") == 0)
		{
			flip = true;
		}
		else if(strcmp(var, "-c") == 0)
		{
			if (i+1 >= argc)
			{
				printf("no config file specified after -c");
				usage(argv[0]);
			}

			i++;
			cfgFile = argv[i];
		}
		else
		{
			printf("Not a valid flag: %s\n", argv[i]);
			usage(argv[0]);
		}
	}

	if (team == UnknownTeam)
	{
		printf("Error: No team specified\n");
		usage(argv[0]);
		return 0;
	}

	MainWindow win(team, cfgFile);

	if (useGoalie)
	{
		win.processor()->gameplayModule()->createGoalie();
	}

	win.processor()->flip_field(flip);

	win.showMaximized();

	return app.exec();
}
<|endoftext|>