<commit_before>//=======================================================================
// Copyright (c) 2014-2015 Baptiste Wicht
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#pragma once

/*!
 * \file processor.hpp
 * \brief This file is made to be included by the dllp generated file only.
 */

#include <string>
#include <iostream>
#include <iomanip>

#include "dll/dbn.hpp"

#include "mnist/mnist_reader.hpp"
#include "mnist/mnist_utils.hpp"

namespace dll {

namespace processor {

struct datasource {
    std::string source_file;
    std::string reader;

    bool binarize = false;
    bool normalize = false;

    long limit = -1;

    datasource(){}
    datasource(std::string source_file, std::string reader) : source_file(source_file), reader(reader) {}

    bool empty() const {
        return source_file.empty();
    }
};

struct datasource_pack {
    datasource samples;
    datasource labels;
};

struct pretraining_desc {
    std::size_t epochs = 25;
};

struct training_desc {
    std::size_t epochs = 25;
    double learning_rate = 0.1;
    double momentum = -666.0;
};

struct weights_desc {
    std::string file = "weights.dat";
};

struct task {
    dll::processor::datasource_pack pretraining;
    dll::processor::datasource_pack training;
    dll::processor::datasource_pack testing;

    dll::processor::pretraining_desc pt_desc;
    dll::processor::training_desc ft_desc;
    dll::processor::weights_desc w_desc;
};

template<typename Sample>
bool read_samples(const datasource& ds, std::vector<Sample>& samples){
    if(ds.reader == "mnist"){
        std::size_t limit = 0;

        if(ds.limit > 0){
            limit = ds.limit;
        }

        mnist::read_mnist_image_file<std::vector, Sample>(samples, ds.source_file, limit, []{ return Sample(1 * 28 * 28); });

        if(ds.binarize){
            mnist::binarize_each(samples);
        }

        if(ds.normalize){
            mnist::normalize_each(samples);
        }

        return !samples.empty();
    } else {
        std::cout << "dllp: error: unknown samples reader: " << ds.reader << std::endl;
        return false;
    }
}

template<typename Label>
bool read_labels(const datasource& ds, std::vector<Label>& labels){
    if(ds.reader == "mnist"){
        std::size_t limit = 0;

        if(ds.limit > 0){
            limit = ds.limit;
        }

        mnist::read_mnist_label_file<std::vector, Label>(labels, ds.source_file, limit);

        return !labels.empty();
    } else {
        std::cout << "dllp: error: unknown labels reader: " << ds.reader << std::endl;
        return false;
    }
}

inline void print_title(const std::string& value){
    std::cout << std::string(25, ' ') << std::endl;
    std::cout << std::string(25, '*') << std::endl;
    std::cout << "* " << value << std::string(25 - value.size() - 3, ' ') << "*" << std::endl;
    std::cout << std::string(25, '*') << std::endl;
    std::cout << std::string(25, ' ') << std::endl;
}

template<typename DBN>
void execute(DBN& dbn, task& task, const std::vector<std::string>& actions){
    print_title("Network");
    dbn.display();

    using dbn_t = std::decay_t<DBN>;

    //Execute all the actions sequentially
    for(auto& action : actions){
        if(action == "pretrain"){
            print_title("Pretraining");

            if(task.pretraining.samples.empty()){
                std::cout << "dllp: error: pretrain is not possible without a pretraining input" << std::endl;
                return;
            }

            std::vector<typename dbn_t::input_t> pt_samples;

            //Try to read the samples
            if(!read_samples(task.pretraining.samples, pt_samples)){
                std::cout << "dllp: error: failed to read the pretraining samples" << std::endl;
                return;
            }

            //Pretrain the network
            dbn.pretrain(pt_samples.begin(), pt_samples.end(), task.pt_desc.epochs);
        } else if(action == "train"){
            print_title("Training");

            if(task.training.samples.empty() || task.training.labels.empty()){
                std::cout << "dllp: error: train is not possible without samples and labels" << std::endl;
                return;
            }

            std::vector<typename dbn_t::input_t> ft_samples;
            std::vector<std::size_t> ft_labels;

            //Try to read the samples
            if(!read_samples(task.training.samples, ft_samples)){
                std::cout << "dllp: error: failed to read the training samples" << std::endl;
                return;
            }

            //Try to read the labels
            if(!read_labels(task.training.labels, ft_labels)){
                std::cout << "dllp: error: failed to read the training labels" << std::endl;
                return;
            }

            dbn.learning_rate = task.ft_desc.learning_rate;

            if(task.ft_desc.momentum != -666.0){
                dbn.momentum = task.ft_desc.momentum;
            }

            //Train the network
            dbn.fine_tune(ft_samples, ft_labels, task.ft_desc.epochs);
        } else if(action == "test"){
            print_title("Testing");

            if(task.testing.samples.empty() || task.testing.labels.empty()){
                std::cout << "dllp: error: test is not possible without samples and labels" << std::endl;
                return;
            }

            std::vector<typename dbn_t::input_t> test_samples;
            std::vector<std::size_t> test_labels;

            //Try to read the samples
            if(!read_samples(task.testing.samples, test_samples)){
                std::cout << "dllp: error: failed to read the training samples" << std::endl;
                return;
            }

            //Try to read the labels
            if(!read_labels(task.testing.labels, test_labels)){
                std::cout << "dllp: error: failed to read the training labels" << std::endl;
                return;
            }

            auto classes = dbn_t::output_size();

            etl::dyn_matrix<std::size_t, 2> conf(classes, classes, 0.0);

            std::size_t n = test_samples.size();
            std::size_t tp = 0;

            for(std::size_t i = 0; i < test_samples.size(); ++i){
                auto sample = test_samples[i];
                auto label = test_labels[i];

                auto predicted = dbn.predict(sample);

                if(predicted == label){
                    ++tp;
                }

                ++conf(label, predicted);
            }

            double test_error = (n - tp) / double(n);

            std::cout << "Error rate: " << test_error << std::endl;
            std::cout << "Accuracy: " << (1.0 - test_error) << std::endl << std::endl;

            std::cout << "Error rate per class" << std::endl;

            double overall = 0.0;

            for(std::size_t l = 0; l < classes; ++l){
                std::size_t total = etl::sum(conf(l));
                double acc = (total - conf(l, l)) / double(total);
                std::cout << l << " : " << acc << std::endl;
                overall += acc;
            }

            std::cout << std::endl;

            std::cout << "Overall Error rate: " << overall / classes << std::endl;
            std::cout << "Overall Accuracy: " << 1.0 - (overall / classes) << std::endl << std::endl;

            std::cout << "Confusion Matrix" << std::endl << std::endl;

            std::cout << "  ";
            for(std::size_t l = 0; l < classes; ++l){
                std::cout << std::setw(5) << l << " ";
            }
            std::cout << std::endl;

            for(std::size_t l = 0; l < classes; ++l){
                std::size_t total = etl::sum(conf(l));
                std::cout << l << "|";
                for(std::size_t p = 0; p < classes; ++p){
                    std::cout << std::setw(5) << std::setprecision(2) << 100.0 * (conf(l,p) / double(total)) << "|";
                }
                std::cout << std::endl;
            }
            std::cout << std::endl;
        } else if(action == "save"){
            print_title("Save Weights");

            dbn.store(task.w_desc.file);
            std::cout << "Weights saved" << std::endl;
        } else if(action == "load"){
            print_title("Load Weights");

            dbn.load(task.w_desc.file);
            std::cout << "Weights loaded" << std::endl;
        } else {
            std::cout << "dllp: error: Invalid action: " << action << std::endl;
        }
    }

    //TODO
}

} //end of namespace processor

} //end of namespace dll
<commit_msg>Cleanup output<commit_after>//=======================================================================
// Copyright (c) 2014-2015 Baptiste Wicht
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#pragma once

/*!
 * \file processor.hpp
 * \brief This file is made to be included by the dllp generated file only.
 */

#include <string>
#include <iostream>
#include <iomanip>

#include "dll/dbn.hpp"

#include "mnist/mnist_reader.hpp"
#include "mnist/mnist_utils.hpp"

namespace dll {

namespace processor {

struct datasource {
    std::string source_file;
    std::string reader;

    bool binarize = false;
    bool normalize = false;

    long limit = -1;

    datasource(){}
    datasource(std::string source_file, std::string reader) : source_file(source_file), reader(reader) {}

    bool empty() const {
        return source_file.empty();
    }
};

struct datasource_pack {
    datasource samples;
    datasource labels;
};

struct pretraining_desc {
    std::size_t epochs = 25;
};

struct training_desc {
    std::size_t epochs = 25;
    double learning_rate = 0.1;
    double momentum = -666.0;
};

struct weights_desc {
    std::string file = "weights.dat";
};

struct task {
    dll::processor::datasource_pack pretraining;
    dll::processor::datasource_pack training;
    dll::processor::datasource_pack testing;

    dll::processor::pretraining_desc pt_desc;
    dll::processor::training_desc ft_desc;
    dll::processor::weights_desc w_desc;
};

template<typename Sample>
bool read_samples(const datasource& ds, std::vector<Sample>& samples){
    if(ds.reader == "mnist"){
        std::size_t limit = 0;

        if(ds.limit > 0){
            limit = ds.limit;
        }

        mnist::read_mnist_image_file<std::vector, Sample>(samples, ds.source_file, limit, []{ return Sample(1 * 28 * 28); });

        if(ds.binarize){
            mnist::binarize_each(samples);
        }

        if(ds.normalize){
            mnist::normalize_each(samples);
        }

        return !samples.empty();
    } else {
        std::cout << "dllp: error: unknown samples reader: " << ds.reader << std::endl;
        return false;
    }
}

template<typename Label>
bool read_labels(const datasource& ds, std::vector<Label>& labels){
    if(ds.reader == "mnist"){
        std::size_t limit = 0;

        if(ds.limit > 0){
            limit = ds.limit;
        }

        mnist::read_mnist_label_file<std::vector, Label>(labels, ds.source_file, limit);

        return !labels.empty();
    } else {
        std::cout << "dllp: error: unknown labels reader: " << ds.reader << std::endl;
        return false;
    }
}

inline void print_title(const std::string& value){
    std::cout << std::string(25, ' ') << std::endl;
    std::cout << std::string(25, '*') << std::endl;
    std::cout << "* " << value << std::string(25 - value.size() - 3, ' ') << "*" << std::endl;
    std::cout << std::string(25, '*') << std::endl;
    std::cout << std::string(25, ' ') << std::endl;
}

template<typename DBN>
void execute(DBN& dbn, task& task, const std::vector<std::string>& actions){
    print_title("Network");
    dbn.display();

    using dbn_t = std::decay_t<DBN>;

    //Execute all the actions sequentially
    for(auto& action : actions){
        if(action == "pretrain"){
            print_title("Pretraining");

            if(task.pretraining.samples.empty()){
                std::cout << "dllp: error: pretrain is not possible without a pretraining input" << std::endl;
                return;
            }

            std::vector<typename dbn_t::input_t> pt_samples;

            //Try to read the samples
            if(!read_samples(task.pretraining.samples, pt_samples)){
                std::cout << "dllp: error: failed to read the pretraining samples" << std::endl;
                return;
            }

            //Pretrain the network
            dbn.pretrain(pt_samples.begin(), pt_samples.end(), task.pt_desc.epochs);
        } else if(action == "train"){
            print_title("Training");

            if(task.training.samples.empty() || task.training.labels.empty()){
                std::cout << "dllp: error: train is not possible without samples and labels" << std::endl;
                return;
            }

            std::vector<typename dbn_t::input_t> ft_samples;
            std::vector<std::size_t> ft_labels;

            //Try to read the samples
            if(!read_samples(task.training.samples, ft_samples)){
                std::cout << "dllp: error: failed to read the training samples" << std::endl;
                return;
            }

            //Try to read the labels
            if(!read_labels(task.training.labels, ft_labels)){
                std::cout << "dllp: error: failed to read the training labels" << std::endl;
                return;
            }

            dbn.learning_rate = task.ft_desc.learning_rate;

            if(task.ft_desc.momentum != -666.0){
                dbn.momentum = task.ft_desc.momentum;
            }

            //Train the network
            dbn.fine_tune(ft_samples, ft_labels, task.ft_desc.epochs);
        } else if(action == "test"){
            print_title("Testing");

            if(task.testing.samples.empty() || task.testing.labels.empty()){
                std::cout << "dllp: error: test is not possible without samples and labels" << std::endl;
                return;
            }

            std::vector<typename dbn_t::input_t> test_samples;
            std::vector<std::size_t> test_labels;

            //Try to read the samples
            if(!read_samples(task.testing.samples, test_samples)){
                std::cout << "dllp: error: failed to read the training samples" << std::endl;
                return;
            }

            //Try to read the labels
            if(!read_labels(task.testing.labels, test_labels)){
                std::cout << "dllp: error: failed to read the training labels" << std::endl;
                return;
            }

            auto classes = dbn_t::output_size();

            etl::dyn_matrix<std::size_t, 2> conf(classes, classes, 0.0);

            std::size_t n = test_samples.size();
            std::size_t tp = 0;

            for(std::size_t i = 0; i < test_samples.size(); ++i){
                auto sample = test_samples[i];
                auto label = test_labels[i];

                auto predicted = dbn.predict(sample);

                if(predicted == label){
                    ++tp;
                }

                ++conf(label, predicted);
            }

            double test_error = (n - tp) / double(n);

            std::cout << "Error rate: " << test_error << std::endl;
            std::cout << "Accuracy: " << (1.0 - test_error) << std::endl << std::endl;

            std::cout << "Results per class" << std::endl;

            double overall = 0.0;

            std::cout << "   | Accuracy | Error rate |" << std::endl;

            for(std::size_t l = 0; l < classes; ++l){
                std::size_t total = etl::sum(conf(l));
                double acc = (total - conf(l, l)) / double(total);
                std::cout << std::setw(3) << l;
                std::cout << "|" << std::setw(10) << (1.0 - acc) << "|" << std::setw(12) << acc << "|" << std::endl;
                overall += acc;
            }

            std::cout << std::endl;

            std::cout << "Overall Error rate: " << overall / classes << std::endl;
            std::cout << "Overall Accuracy: " << 1.0 - (overall / classes) << std::endl << std::endl;

            std::cout << "Confusion Matrix (%)" << std::endl << std::endl;

            std::cout << "    ";
            for(std::size_t l = 0; l < classes; ++l){
                std::cout << std::setw(5) << l << " ";
            }
            std::cout << std::endl;

            for(std::size_t l = 0; l < classes; ++l){
                std::size_t total = etl::sum(conf(l));
                std::cout << std::setw(3) << l << "|";
                for(std::size_t p = 0; p < classes; ++p){
                    std::cout << std::setw(5) << std::setprecision(2) << 100.0 * (conf(l,p) / double(total)) << "|";
                }
                std::cout << std::endl;
            }
            std::cout << std::endl;
        } else if(action == "save"){
            print_title("Save Weights");

            dbn.store(task.w_desc.file);
            std::cout << "Weights saved" << std::endl;
        } else if(action == "load"){
            print_title("Load Weights");

            dbn.load(task.w_desc.file);
            std::cout << "Weights loaded" << std::endl;
        } else {
            std::cout << "dllp: error: Invalid action: " << action << std::endl;
        }
    }

    //TODO
}

} //end of namespace processor

} //end of namespace dll
<|endoftext|>


<commit_before>#include <qrkernel/version.h>

#include "gtest/gtest.h"

using namespace qReal;

TEST(VersionTest, loadFromStringTest)
{
	Version version = Version::fromString("3");
	ASSERT_EQ(version, Version(3));

	version = Version::fromString("3.14");
	ASSERT_EQ(version, Version(3, 14));

	version = Version::fromString("3.14.15");
	ASSERT_EQ(version, Version(3, 14, 15));

	version = Version::fromString("3 a");
	ASSERT_EQ(version, Version(3, 0, 0, Version::alpha));

	version = Version::fromString("3.1-Alpha1124");
	ASSERT_EQ(version, Version(3, 1, 0, Version::alpha, 1124));

	version = Version::fromString("3.0.0-B1");
	ASSERT_EQ(version, Version(3, 0, 0, Version::beta, 1));

	version = Version::fromString("3.14.0 BETA");
	ASSERT_EQ(version, Version(3, 14, 0, Version::beta));

	version = Version::fromString("3.14.0-RC1");
	ASSERT_EQ(version, Version(3, 14, 0, Version::releaseCandidate, 1));

	version = Version::fromString("");
	ASSERT_EQ(version, Version());

	version = Version::fromString("incorrect");
	ASSERT_EQ(version, Version());

	version = Version::fromString("3.1.4.15");
	ASSERT_EQ(version, Version());

	version = Version::fromString("3.1-wrong1");
	ASSERT_EQ(version, Version());

	version = Version::fromString("3.1-alpha1a");
	ASSERT_EQ(version, Version());
}

TEST(VersionTest, compareTest)
{
	ASSERT_TRUE(Version::fromString("3") == Version::fromString("3.0.0"));
	ASSERT_TRUE(Version::fromString("3.0.0-a") < Version::fromString("3.0.0-rc1"));
	ASSERT_TRUE(Version::fromString("3.0.0-a") < Version::fromString("3.0.0-b1"));
	ASSERT_TRUE(Version::fromString("3.0.0-b1") < Version::fromString("3.0.0-rc1"));
	ASSERT_TRUE(Version::fromString("3.0.0-rc1") < Version::fromString("3.0.0-rc2"));
	ASSERT_TRUE(Version::fromString("3.0.0-rc2") < Version::fromString("3.0.0"));
}
<commit_msg>Added more tests for version class<commit_after>#include <qrkernel/version.h>

#include "gtest/gtest.h"

using namespace qReal;

TEST(VersionTest, loadFromStringTest)
{
	Version version = Version::fromString("3");
	ASSERT_EQ(version, Version(3));

	version = Version::fromString("3.14");
	ASSERT_EQ(version, Version(3, 14));

	version = Version::fromString("3.14.15");
	ASSERT_EQ(version, Version(3, 14, 15));

	version = Version::fromString("3 a");
	ASSERT_EQ(version, Version(3, 0, 0, Version::alpha));

	version = Version::fromString("3.1-Alpha1124");
	ASSERT_EQ(version, Version(3, 1, 0, Version::alpha, 1124));

	version = Version::fromString("3.0.0-B1");
	ASSERT_EQ(version, Version(3, 0, 0, Version::beta, 1));

	version = Version::fromString("3.14.0 BETA");
	ASSERT_EQ(version, Version(3, 14, 0, Version::beta));

	version = Version::fromString("3.14.0-RC1");
	ASSERT_EQ(version, Version(3, 14, 0, Version::releaseCandidate, 1));

	version = Version::fromString("");
	ASSERT_EQ(version, Version());

	version = Version::fromString("incorrect");
	ASSERT_EQ(version, Version());

	version = Version::fromString("3.1.4.15");
	ASSERT_EQ(version, Version());

	version = Version::fromString("3.1-wrong1");
	ASSERT_EQ(version, Version());

	version = Version::fromString("3.1-alpha1a");
	ASSERT_EQ(version, Version());
}

TEST(VersionTest, compareTest)
{
	ASSERT_TRUE(Version::fromString("3") == Version::fromString("3.0.0"));
	ASSERT_TRUE(Version::fromString("3.0.0-a") < Version::fromString("3.0.0-rc1"));
	ASSERT_TRUE(Version::fromString("3.0.0-a") < Version::fromString("3.0.0-b1"));
	ASSERT_TRUE(Version::fromString("3.0.0-b1") < Version::fromString("3.0.0-rc1"));
	ASSERT_TRUE(Version::fromString("3.0.0-rc1") < Version::fromString("3.0.0-rc2"));
	ASSERT_TRUE(Version::fromString("3.0.0-rc2") < Version::fromString("3.0.0"));
}

TEST(VersionTest, toStringTest)
{
	Version version = Version::fromString("3");
	ASSERT_EQ(version, Version::fromString(version.toString()));

	version = Version::fromString("3.0.0");
	ASSERT_EQ(version, Version::fromString(version.toString()));

	version = Version::fromString("3.0.0-a");
	ASSERT_EQ(version, Version::fromString(version.toString()));

	version = Version::fromString("3.0.0-b1");
	ASSERT_EQ(version, Version::fromString(version.toString()));

	version = Version::fromString("3.0.0-rc1");
	ASSERT_EQ(version, Version::fromString(version.toString()));
}
<|endoftext|>


<commit_before>/*
 * Copyright (C) 2009-2011, Gostai S.A.S.
 *
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the
 * implied warranties of fitness for a particular purpose.
 *
 * See the LICENSE file for more information.
 */

#include <libport/cassert>

namespace libport
{
  template<class T, int R>
  inline
  ReservedVector<T, R>::ReservedVector()
  {
    this->reserve(R);
  }

  template<class T, int R>
  inline
  ReservedVector<T, R>::ReservedVector(const self_type& other)
    : super_type()
  {
    this->reserve(R);
    insert(this->end(), other.begin(), other.end());
  }

  template<class T, int R>
  template<typename I1, typename I2>
  inline
  ReservedVector<T, R>::ReservedVector(I1 b, I2 e)
    : super_type()
  {
    this->reserve(R);
    insert(this->end(), b, e);
  }

  template<class T, int R>
  template<int R2>
  inline
  ReservedVector<T, R>::ReservedVector(const ReservedVector<T, R2>& b)
    : super_type(b)
  {
    this->reserve(R);
  }

  template<typename T>
  inline
  void pop_front(T& v)
  {
    T v2(++v.begin(), v.end());
    v = v2;
  }

  template<typename T, typename U>
  inline
  void push_front(T& v, const U& e)
  {
    if (v.empty())
      v.push_back(e);
    else
    {
      T v2;
      v2.push_back(e);
      v2.insert(v2.end(), v.begin(), v.end());
      v = v2;
    }
  }

  template<typename T, int R>
  class ReservedVectorAllocator: public std::allocator<T>
  {
  /*-----------.
  | Typedefs.  |
  `-----------*/
  public :
    typedef ReservedVectorAllocator self_type;
    typedef std::allocator<T> super_type;
    typedef typename super_type::pointer pointer;
    typedef typename super_type::size_type size_type;
    template<typename U>
    struct rebind
    {
      typedef ReservedVectorAllocator<U, R> other;
    };

  /*-------------.
  | Construction |
  `-------------*/
  public:
    ReservedVectorAllocator()
#ifndef NDEBUG
      : local_(false)
#endif
    {

    }

  /*--------------------.
  | Memory allocation.  |
  `--------------------*/
  public:
    inline pointer
    allocate(size_type count, typename std::allocator<void>::const_pointer = 0)
    {
      if (count <= R)
      {
#ifndef NDEBUG
        assert(!local_);
        local_ = true;
#endif
        return reinterpret_cast<pointer>(pool_);
      }
      else
      {
#ifndef NDEBUG
        local_ = false;
#endif
        return reinterpret_cast<pointer>(malloc(count * sizeof(T)));
      }
    }

    inline void
    deallocate(pointer p, size_type count)
    {
      if (count > R)
      {
        assert(reinterpret_cast<pointer>(p) != reinterpret_cast<pointer>(pool_));
        free(p);
      }
      else
        assert(reinterpret_cast<pointer>(p) == reinterpret_cast<pointer>(pool_));
    }

  private:
    typedef long long align_type;
    enum { chunk_size = (sizeof(T) - 1) / sizeof(align_type) + 1 };
    align_type pool_[R * chunk_size];
#ifndef NDEBUG
    bool local_;
#endif
  };
}
<commit_msg>Fix type-punned warning with GCC 4.1.2.<commit_after>/*
 * Copyright (C) 2009-2011, Gostai S.A.S.
 *
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the
 * implied warranties of fitness for a particular purpose.
 *
 * See the LICENSE file for more information.
 */

#include <libport/cassert>

namespace libport
{
  template<class T, int R>
  inline
  ReservedVector<T, R>::ReservedVector()
  {
    this->reserve(R);
  }

  template<class T, int R>
  inline
  ReservedVector<T, R>::ReservedVector(const self_type& other)
    : super_type()
  {
    this->reserve(R);
    insert(this->end(), other.begin(), other.end());
  }

  template<class T, int R>
  template<typename I1, typename I2>
  inline
  ReservedVector<T, R>::ReservedVector(I1 b, I2 e)
    : super_type()
  {
    this->reserve(R);
    insert(this->end(), b, e);
  }

  template<class T, int R>
  template<int R2>
  inline
  ReservedVector<T, R>::ReservedVector(const ReservedVector<T, R2>& b)
    : super_type(b)
  {
    this->reserve(R);
  }

  template<typename T>
  inline
  void pop_front(T& v)
  {
    T v2(++v.begin(), v.end());
    v = v2;
  }

  template<typename T, typename U>
  inline
  void push_front(T& v, const U& e)
  {
    if (v.empty())
      v.push_back(e);
    else
    {
      T v2;
      v2.push_back(e);
      v2.insert(v2.end(), v.begin(), v.end());
      v = v2;
    }
  }

  template<typename T, int R>
  class ReservedVectorAllocator: public std::allocator<T>
  {
  /*-----------.
  | Typedefs.  |
  `-----------*/
  public :
    typedef ReservedVectorAllocator self_type;
    typedef std::allocator<T> super_type;
    typedef typename super_type::pointer pointer;
    typedef typename super_type::size_type size_type;
    template<typename U>
    struct rebind
    {
      typedef ReservedVectorAllocator<U, R> other;
    };

  /*-------------.
  | Construction |
  `-------------*/
  public:
    ReservedVectorAllocator()
#ifndef NDEBUG
      : local_(false)
#endif
    {

    }

  /*--------------------.
  | Memory allocation.  |
  `--------------------*/
  public:
    inline pointer
    allocate(size_type count, typename std::allocator<void>::const_pointer = 0)
    {
      if (count <= R)
      {
#ifndef NDEBUG
        assert(!local_);
        local_ = true;
#endif
        // Union hack to cast to a different pointer type;
        // reinterpret_cast<pointer>(pool_) gives a type-punned
        // warning with GCC 4.1.2.
        union
        {
          align_type* in;
          pointer out;
        } conv;
        conv.in = pool_;
        return conv.out;
      }
      else
      {
#ifndef NDEBUG
        local_ = false;
#endif
        return reinterpret_cast<pointer>(malloc(count * sizeof(T)));
      }
    }

    inline void
    deallocate(pointer p, size_type count)
    {
      if (count > R)
      {
        assert(reinterpret_cast<pointer>(p) != reinterpret_cast<pointer>(pool_));
        free(p);
      }
      else
        assert(reinterpret_cast<pointer>(p) == reinterpret_cast<pointer>(pool_));
    }

  private:
    typedef long long align_type;
    enum { chunk_size = (sizeof(T) - 1) / sizeof(align_type) + 1 };
    align_type pool_[R * chunk_size];
#ifndef NDEBUG
    bool local_;
#endif
  };
}
<|endoftext|>


<commit_before>/*

Copyright (c) 2010-2012, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef TORRENT_BUILD_CONFIG_HPP_INCLUDED
#define TORRENT_BUILD_CONFIG_HPP_INCLUDED

#include "libtorrent/config.hpp"
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/stringize.hpp>

#ifdef TORRENT_DEBUG
#define TORRENT_CFG_DEBUG dbg_
#else
#define TORRENT_CFG_DEBUG rel_
#endif

#if TORRENT_USE_BOOST_DATE_TIME
#define TORRENT_CFG_TIME boosttime_
#elif TORRENT_USE_ABSOLUTE_TIME
#define TORRENT_CFG_TIME absolutetime_
#elif TORRENT_USE_QUERY_PERFORMANCE_TIMER
#define TORRENT_CFG_TIME performancetimer_
#elif TORRENT_USE_CLOCK_GETTIME
#define TORRENT_CFG_TIME clocktime_
#elif TORRENT_USE_SYSTEM_TIME
#define TORRENT_CFG_TIME systime_
#else
#error what timer is used?
#endif

#if TORRENT_USE_IPV6
#define TORRENT_CFG_IPV6 ipv6_
#else
#define TORRENT_CFG_IPV6 noipv_-
#endif

#ifdef TORRENT_DISABLE_POOL_ALLOCATORS
#define TORRENT_CFG_POOL nopools_
#else
#define TORRENT_CFG_POOL pools_
#endif

#ifdef _UNICODE
#define TORRENT_CFG_UNICODE unicode_
#else
#define TORRENT_CFG_UNICODE ansi_
#endif

#ifdef TORRENT_NO_DEPRECATE
#define TORRENT_CFG_DEPR nodeprecate_
#else
#define TORRENT_CFG_DEPR deprecated_
#endif

#ifdef TORRENT_DISABLE_FULL_STATS
#define TORRENT_CFG_STATS partialstats_
#else
#define TORRENT_CFG_STATS fullstats_
#endif

#ifdef TORRENT_DISABLE_EXTENSIONS
#define TORRENT_CFG_EXT noext_
#else
#define TORRENT_CFG_EXT ext_
#endif

#define TORRENT_CFG \
	BOOST_PP_CAT(TORRENT_CFG_DEBUG, \
	BOOST_PP_CAT(TORRENT_CFG_TIME, \
	BOOST_PP_CAT(TORRENT_CFG_POOL, \
	BOOST_PP_CAT(TORRENT_CFG_LOG, \
	BOOST_PP_CAT(TORRENT_CFG_DEPR, \
	TORRENT_CFG_EXT)))))

#define TORRENT_CFG_STRING BOOST_PP_STRINGIZE(TORRENT_CFG)

#endif

<commit_msg>disable pool allocators does not affect libtorrent API<commit_after>/*

Copyright (c) 2010-2012, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef TORRENT_BUILD_CONFIG_HPP_INCLUDED
#define TORRENT_BUILD_CONFIG_HPP_INCLUDED

#include "libtorrent/config.hpp"
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/stringize.hpp>

#ifdef TORRENT_DEBUG
#define TORRENT_CFG_DEBUG dbg_
#else
#define TORRENT_CFG_DEBUG rel_
#endif

#if TORRENT_USE_BOOST_DATE_TIME
#define TORRENT_CFG_TIME boosttime_
#elif TORRENT_USE_ABSOLUTE_TIME
#define TORRENT_CFG_TIME absolutetime_
#elif TORRENT_USE_QUERY_PERFORMANCE_TIMER
#define TORRENT_CFG_TIME performancetimer_
#elif TORRENT_USE_CLOCK_GETTIME
#define TORRENT_CFG_TIME clocktime_
#elif TORRENT_USE_SYSTEM_TIME
#define TORRENT_CFG_TIME systime_
#else
#error what timer is used?
#endif

#if TORRENT_USE_IPV6
#define TORRENT_CFG_IPV6 ipv6_
#else
#define TORRENT_CFG_IPV6 noipv_-
#endif

#ifdef _UNICODE
#define TORRENT_CFG_UNICODE unicode_
#else
#define TORRENT_CFG_UNICODE ansi_
#endif

#ifdef TORRENT_NO_DEPRECATE
#define TORRENT_CFG_DEPR nodeprecate_
#else
#define TORRENT_CFG_DEPR deprecated_
#endif

#ifdef TORRENT_DISABLE_FULL_STATS
#define TORRENT_CFG_STATS partialstats_
#else
#define TORRENT_CFG_STATS fullstats_
#endif

#ifdef TORRENT_DISABLE_EXTENSIONS
#define TORRENT_CFG_EXT noext_
#else
#define TORRENT_CFG_EXT ext_
#endif

#define TORRENT_CFG \
	BOOST_PP_CAT(TORRENT_CFG_DEBUG, \
	BOOST_PP_CAT(TORRENT_CFG_TIME, \
	BOOST_PP_CAT(TORRENT_CFG_LOG, \
	BOOST_PP_CAT(TORRENT_CFG_DEPR, \
	TORRENT_CFG_EXT))))

#define TORRENT_CFG_STRING BOOST_PP_STRINGIZE(TORRENT_CFG)

#endif

<|endoftext|>


<commit_before>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/test_timeouts.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "chrome/test/test_switches.h"

namespace {

void InitializeTimeout(const char* switch_name, int* value) {
  DCHECK(value);
  if (CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
    std::string string_value(
        CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switch_name));
    int timeout;
    base::StringToInt(string_value, &timeout);
    *value = std::max(*value, timeout);
  }
}

}  // namespace

// static
bool TestTimeouts::initialized_ = false;

// The timeout values should increase in the order they appear in this block.
// static
int TestTimeouts::action_timeout_ms_ = 2000;
int TestTimeouts::action_max_timeout_ms_ = 15000;
int TestTimeouts::medium_test_timeout_ms_ = 30 * 1000;
int TestTimeouts::large_test_timeout_ms_ = 10 * 60 * 1000;

// static
int TestTimeouts::sleep_timeout_ms_ = 2000;

// static
int TestTimeouts::command_execution_timeout_ms_ = 25000;

// static
int TestTimeouts::wait_for_terminate_timeout_ms_ = 15000;

// static
void TestTimeouts::Initialize() {
  if (initialized_) {
    NOTREACHED();
    return;
  }
  initialized_ = true;

  InitializeTimeout(switches::kUiTestActionTimeout, &action_timeout_ms_);
  InitializeTimeout(switches::kUiTestActionMaxTimeout,
                    &action_max_timeout_ms_);
  InitializeTimeout(switches::kMediumTestTimeout, &medium_test_timeout_ms_);
  InitializeTimeout(switches::kUiTestTimeout, &large_test_timeout_ms_);

  // The timeout values should be increasing in the right order.
  CHECK(action_timeout_ms_ <= action_max_timeout_ms_);
  CHECK(action_max_timeout_ms_ <= medium_test_timeout_ms_);
  CHECK(medium_test_timeout_ms_ <= large_test_timeout_ms_);

  InitializeTimeout(switches::kUiTestSleepTimeout, &sleep_timeout_ms_);
  InitializeTimeout(switches::kUiTestCommandExecutionTimeout,
                    &command_execution_timeout_ms_);
  InitializeTimeout(switches::kUiTestTerminateTimeout,
                    &wait_for_terminate_timeout_ms_);
}
<commit_msg>GTTF: Increase the timeout for medium tests to one minute.<commit_after>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/test_timeouts.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "chrome/test/test_switches.h"

namespace {

void InitializeTimeout(const char* switch_name, int* value) {
  DCHECK(value);
  if (CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
    std::string string_value(
        CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switch_name));
    int timeout;
    base::StringToInt(string_value, &timeout);
    *value = std::max(*value, timeout);
  }
}

}  // namespace

// static
bool TestTimeouts::initialized_ = false;

// The timeout values should increase in the order they appear in this block.
// static
int TestTimeouts::action_timeout_ms_ = 2000;
int TestTimeouts::action_max_timeout_ms_ = 15000;
int TestTimeouts::medium_test_timeout_ms_ = 60 * 1000;
int TestTimeouts::large_test_timeout_ms_ = 10 * 60 * 1000;

// static
int TestTimeouts::sleep_timeout_ms_ = 2000;

// static
int TestTimeouts::command_execution_timeout_ms_ = 25000;

// static
int TestTimeouts::wait_for_terminate_timeout_ms_ = 15000;

// static
void TestTimeouts::Initialize() {
  if (initialized_) {
    NOTREACHED();
    return;
  }
  initialized_ = true;

  InitializeTimeout(switches::kUiTestActionTimeout, &action_timeout_ms_);
  InitializeTimeout(switches::kUiTestActionMaxTimeout,
                    &action_max_timeout_ms_);
  InitializeTimeout(switches::kMediumTestTimeout, &medium_test_timeout_ms_);
  InitializeTimeout(switches::kUiTestTimeout, &large_test_timeout_ms_);

  // The timeout values should be increasing in the right order.
  CHECK(action_timeout_ms_ <= action_max_timeout_ms_);
  CHECK(action_max_timeout_ms_ <= medium_test_timeout_ms_);
  CHECK(medium_test_timeout_ms_ <= large_test_timeout_ms_);

  InitializeTimeout(switches::kUiTestSleepTimeout, &sleep_timeout_ms_);
  InitializeTimeout(switches::kUiTestCommandExecutionTimeout,
                    &command_execution_timeout_ms_);
  InitializeTimeout(switches::kUiTestTerminateTimeout,
                    &wait_for_terminate_timeout_ms_);
}
<|endoftext|>


<commit_before>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "build/build_config.h"
#include "chrome/test/ui/ui_test.h"
#include "net/base/net_util.h"

class GPUTest : public UITest {
 protected:
  GPUTest() {
  }

  virtual void SetUp() {
    UITest::SetUp();
    gpu_test_dir_ = test_data_directory_.AppendASCII("gpu");
  }

  FilePath gpu_test_dir_;
};

// TODO(apatrick): Other pending changes will fix this for mac.
#if defined(OS_MACOSX)
#define MAYBE_UITestLaunchedWithOSMesa DISABLED_UITestLaunchedWithOSMesa
#else
#define MAYBE_UITestLaunchedWithOSMesa UITestLaunchedWithOSMesa
#endif

TEST_F(GPUTest, MAYBE_UITestLaunchedWithOSMesa) {
  // Check the webgl test reports success and that the renderer was OSMesa.
  // We use OSMesa for tests in order to get consistent results across a
  // variety of boxes.
  NavigateToURL(
      net::FilePathToFileURL(gpu_test_dir_.AppendASCII("webgl.html")));

  EXPECT_EQ(std::wstring(L"SUCCESS: Mesa OffScreen"), GetActiveTabTitle());
}
<commit_msg>Marked GPUUITest.UITestLaunchedWithOSMesa disabled on Windows.<commit_after>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "build/build_config.h"
#include "chrome/test/ui/ui_test.h"
#include "net/base/net_util.h"

class GPUUITest : public UITest {
 protected:
  GPUUITest() {
  }

  virtual void SetUp() {
    UITest::SetUp();
    gpu_test_dir_ = test_data_directory_.AppendASCII("gpu");
  }

  FilePath gpu_test_dir_;
};

#if defined(OS_WIN) || defined(OS_MACOSX)
#define MAYBE_UITestLaunchedWithOSMesa DISABLED_UITestLaunchedWithOSMesa
#else
#define MAYBE_UITestLaunchedWithOSMesa UITestLaunchedWithOSMesa
#endif

TEST_F(GPUUITest, MAYBE_UITestLaunchedWithOSMesa) {
  // Check the webgl test reports success and that the renderer was OSMesa.
  // We use OSMesa for tests in order to get consistent results across a
  // variety of boxes.
  NavigateToURL(
      net::FilePathToFileURL(gpu_test_dir_.AppendASCII("webgl.html")));

  EXPECT_EQ(std::wstring(L"SUCCESS: Mesa OffScreen"), GetActiveTabTitle());
}
<|endoftext|>


<commit_before>/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2011 Artem Pavlenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#ifndef MAPNIK_POINT_SYMBOLIZER_HPP
#define MAPNIK_POINT_SYMBOLIZER_HPP

// mapnik
#include <mapnik/symbolizer.hpp>
#include <mapnik/enumeration.hpp>

namespace mapnik
{

enum point_placement_enum {
    CENTROID_POINT_PLACEMENT,
    INTERIOR_POINT_PLACEMENT,
    point_placement_enum_MAX
};

DEFINE_ENUM( point_placement_e, point_placement_enum );

struct MAPNIK_DECL point_symbolizer :
        public symbolizer_with_image, public symbolizer_base
{
    explicit point_symbolizer();
    point_symbolizer(path_expression_ptr file);
    point_symbolizer(point_symbolizer const& rhs);
    void set_allow_overlap(bool overlap);
    bool get_allow_overlap() const;
    void set_point_placement(point_placement_e point_p);
    point_placement_e get_point_placement() const;
    void set_ignore_placement(bool ignore_placement);
    bool get_ignore_placement() const;

private:
    bool overlap_;
    point_placement_e point_p_;
    bool ignore_placement_;
};
}

#endif // MAPNIK_POINT_SYMBOLIZER_HPP
<commit_msg>+ explicit keyword only makes sense for 1 arg ctor's<commit_after>/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2011 Artem Pavlenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#ifndef MAPNIK_POINT_SYMBOLIZER_HPP
#define MAPNIK_POINT_SYMBOLIZER_HPP

// mapnik
#include <mapnik/symbolizer.hpp>
#include <mapnik/enumeration.hpp>

namespace mapnik
{

enum point_placement_enum {
    CENTROID_POINT_PLACEMENT,
    INTERIOR_POINT_PLACEMENT,
    point_placement_enum_MAX
};

DEFINE_ENUM( point_placement_e, point_placement_enum );

struct MAPNIK_DECL point_symbolizer :
        public symbolizer_with_image, public symbolizer_base
{
    point_symbolizer();
    point_symbolizer(path_expression_ptr file);
    point_symbolizer(point_symbolizer const& rhs);
    void set_allow_overlap(bool overlap);
    bool get_allow_overlap() const;
    void set_point_placement(point_placement_e point_p);
    point_placement_e get_point_placement() const;
    void set_ignore_placement(bool ignore_placement);
    bool get_ignore_placement() const;

private:
    bool overlap_;
    point_placement_e point_p_;
    bool ignore_placement_;
};
}

#endif // MAPNIK_POINT_SYMBOLIZER_HPP
<|endoftext|>


<commit_before>/*
  Copyright (c) 2017 Genome Research Ltd.

  Author: Jouni Siren <jouni.siren@iki.fi>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

#include "new_relative_lcp.h"

using namespace relative;

//------------------------------------------------------------------------------

int
main(int argc, char** argv)
{
  if(argc < 3)
  {
    std::cerr << "Usage: build_rlcp reference target1 [target2 ...]" << std::endl;
    std::cerr << std::endl;
    std::exit(EXIT_SUCCESS);
  }

  std::cout << "Building the relative LCP array" << std::endl;
  std::cout << std::endl;

  std::string ref_name = std::string(argv[1]) + LCP_EXTENSION;
  printHeader("Reference"); std::cout << ref_name << std::endl;
  SLArray reference;
  sdsl::load_from_file(reference, ref_name);
  printHeader("Length"); std::cout << reference.size() << std::endl;
  std::cout << std::endl;

  for(int i = 2; i < argc; i++)
  {
    std::string target_name = std::string(argv[i]) + LCP_EXTENSION;
    printHeader("Target"); std::cout << target_name << std::endl;
    SLArray target;
    sdsl::load_from_file(target, target_name);
    printHeader("Length"); std::cout << target.size() << std::endl;

    double start = readTimer();
    NewRelativeLCP rlcp(reference, target);
    double seconds = readTimer() - start;
    printHeader("Construction"); std::cout << seconds << " seconds" << std::endl;
    double phrase_length = rlcp.size() / (double)(rlcp.phrases());
    printHeader("Phrases"); std::cout << rlcp.phrases() << " (average length " << phrase_length << ")" << std::endl;
    rlcp.reportSize(true);
    rlcp.writeTo(argv[i]);

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

  std::cout << "Memory usage: " << inGigabytes(memoryUsage()) << " GB" << std::endl;
  std::cout << std::endl;

  return 0;
}

//------------------------------------------------------------------------------
<commit_msg>Output Reference LCP size<commit_after>/*
  Copyright (c) 2017 Genome Research Ltd.

  Author: Jouni Siren <jouni.siren@iki.fi>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

#include "new_relative_lcp.h"

using namespace relative;

//------------------------------------------------------------------------------

int
main(int argc, char** argv)
{
  if(argc < 3)
  {
    std::cerr << "Usage: build_rlcp reference target1 [target2 ...]" << std::endl;
    std::cerr << std::endl;
    std::exit(EXIT_SUCCESS);
  }

  std::cout << "Building the relative LCP array" << std::endl;
  std::cout << std::endl;

  std::string ref_name = std::string(argv[1]) + LCP_EXTENSION;
  printHeader("Reference"); std::cout << ref_name << std::endl;
  SLArray reference;
  sdsl::load_from_file(reference, ref_name);
  printHeader("Length"); std::cout << reference.size() << std::endl;
  printSize("LCP array", sdsl::size_in_bytes(reference), reference.size());
  std::cout << std::endl;

  for(int i = 2; i < argc; i++)
  {
    std::string target_name = std::string(argv[i]) + LCP_EXTENSION;
    printHeader("Target"); std::cout << target_name << std::endl;
    SLArray target;
    sdsl::load_from_file(target, target_name);
    printHeader("Length"); std::cout << target.size() << std::endl;

    double start = readTimer();
    NewRelativeLCP rlcp(reference, target);
    double seconds = readTimer() - start;
    printHeader("Construction"); std::cout << seconds << " seconds" << std::endl;
    double phrase_length = rlcp.size() / (double)(rlcp.phrases());
    printHeader("Phrases"); std::cout << rlcp.phrases() << " (average length " << phrase_length << ")" << std::endl;
    rlcp.reportSize(true);
    rlcp.writeTo(argv[i]);

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

  std::cout << "Memory usage: " << inGigabytes(memoryUsage()) << " GB" << std::endl;
  std::cout << std::endl;

  return 0;
}

//------------------------------------------------------------------------------
<|endoftext|>


<commit_before>// Copyright (c) 2016-2019 Daniel Frey and Dr. Colin Hirsch
// Please see LICENSE for license or visit https://github.com/taocpp/taopq/

#ifndef TAO_PQ_PARAMETER_TRAITS_HPP
#define TAO_PQ_PARAMETER_TRAITS_HPP

#include <cmath>
#include <optional>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>

#include <byteswap.h>
#include <cstring>

#include <tao/pq/internal/printf.hpp>
#include <tao/pq/null.hpp>

namespace tao::pq
{
   inline int ftoi( const float v )
   {
      static_assert( sizeof( float ) == 4 );
      static_assert( sizeof( int ) == 4 );

      int r;
      std::memcpy( (char*)&r, (char*)&v, 4 );
      return r;
   }

   inline long dtol( const double v )
   {
      static_assert( sizeof( double ) == 8 );
      static_assert( sizeof( long ) == 8 );

      long r;
      std::memcpy( (char*)&r, (char*)&v, 8 );
      return r;
   }

   template< typename, typename = void >
   struct parameter_traits;

   namespace internal
   {
      class char_pointer_helper
      {
      private:
         const char* m_p;

      protected:
         explicit char_pointer_helper( const char* p ) noexcept
            : m_p( p )
         {
         }

      public:
         static constexpr std::size_t columns = 1;

         template< std::size_t I >
         const char* c_str() const noexcept
         {
            static_assert( I < columns );
            return m_p;
         }

         template< std::size_t I >
         constexpr int size() const noexcept
         {
            static_assert( I < columns );
            return 0;
         }

         template< std::size_t I >
         constexpr int format() const noexcept
         {
            static_assert( I < columns );
            return 0;
         }
      };

      class string_helper
      {
      private:
         std::string m_s;

      protected:
         template< typename... Ts >
         explicit string_helper( Ts&&... ts ) noexcept( noexcept( std::string( std::forward< Ts >( ts )... ) ) )
            : m_s( std::forward< Ts >( ts )... )
         {
         }

      public:
         static constexpr std::size_t columns = 1;

         template< std::size_t I >
         const char* c_str() const noexcept
         {
            static_assert( I < columns );
            return m_s.c_str();
         }

         template< std::size_t I >
         constexpr int size() const noexcept
         {
            static_assert( I < columns );
            return 0;
         }

         template< std::size_t I >
         constexpr int format() const noexcept
         {
            static_assert( I < columns );
            return 0;
         }
      };

      template< typename T >
      [[nodiscard]] std::string printf_helper( const char* format, const T v )
      {
         if( std::isfinite( v ) ) {
            return internal::printf( format, v );
         }
         if( std::isnan( v ) ) {
            return "NAN";
         }
         return ( v < 0 ) ? "-INF" : "INF";
      }

   }  // namespace internal

   template<>
   struct parameter_traits< null_t >
   {
      parameter_traits( const null_t& ) noexcept
      {
      }

      static constexpr std::size_t columns = 1;

      template< std::size_t I >
      constexpr const char* c_str() const noexcept
      {
         static_assert( I < columns );
         return nullptr;
      }

      template< std::size_t I >
      constexpr int size() const noexcept
      {
         static_assert( I < columns );
         return 0;
      }

      template< std::size_t I >
      constexpr int format() const noexcept
      {
         static_assert( I < columns );
         return 0;
      }
   };

   template<>
   struct parameter_traits< const char* >
      : internal::char_pointer_helper
   {
      parameter_traits( const char* p ) noexcept
         : char_pointer_helper( p )
      {
      }
   };

   template<>
   struct parameter_traits< std::string >
      : internal::char_pointer_helper
   {
      parameter_traits( const std::string& v ) noexcept
         : char_pointer_helper( v.c_str() )
      {
      }
   };

   template<>
   struct parameter_traits< bool >
      : internal::char_pointer_helper
   {
      parameter_traits( const bool v ) noexcept
         : char_pointer_helper( v ? "TRUE" : "FALSE" )
      {
      }
   };

   template<>
   struct parameter_traits< char >
      : internal::string_helper
   {
      parameter_traits( const char v )
         : string_helper( 1, v )
      {
      }
   };

   template<>
   struct parameter_traits< signed char >
      : internal::string_helper
   {
      parameter_traits( const signed char v )
         : string_helper( internal::printf( "%hhd", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< unsigned char >
      : internal::string_helper
   {
      parameter_traits( const unsigned char v )
         : string_helper( internal::printf( "%hhu", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< short >
   {
      const short m_v;

      static_assert( sizeof( short ) == 2 );

      parameter_traits( const short v ) noexcept
         : m_v( bswap_16( v ) )
      {
      }

      static constexpr std::size_t columns = 1;

      template< std::size_t I >
      const char* c_str() const noexcept
      {
         static_assert( I < columns );
         return reinterpret_cast< const char* >( &m_v );
      }

      template< std::size_t I >
      constexpr int size() const noexcept
      {
         static_assert( I < columns );
         return sizeof( short );
      }

      template< std::size_t I >
      constexpr int format() const noexcept
      {
         static_assert( I < columns );
         return 1;
      }
   };

   template<>
   struct parameter_traits< unsigned short >
      : internal::string_helper
   {
      parameter_traits( const unsigned short v )
         : string_helper( internal::printf( "%hu", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< int >
   {
      const int m_v;

      static_assert( sizeof( int ) == 4 );

      parameter_traits( const int v ) noexcept
         : m_v( bswap_32( v ) )
      {
      }

      static constexpr std::size_t columns = 1;

      template< std::size_t I >
      const char* c_str() const noexcept
      {
         static_assert( I < columns );
         return reinterpret_cast< const char* >( &m_v );
      }

      template< std::size_t I >
      constexpr int size() const noexcept
      {
         static_assert( I < columns );
         return sizeof( int );
      }

      template< std::size_t I >
      constexpr int format() const noexcept
      {
         static_assert( I < columns );
         return 1;
      }
   };

   template<>
   struct parameter_traits< unsigned >
      : internal::string_helper
   {
      parameter_traits( const unsigned v )
         : string_helper( internal::printf( "%u", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< long >
   {
      const long m_v;

      static_assert( sizeof( long ) == 8 );

      parameter_traits( const long v ) noexcept
         : m_v( bswap_64( v ) )
      {
      }

      static constexpr std::size_t columns = 1;

      template< std::size_t I >
      const char* c_str() const noexcept
      {
         static_assert( I < columns );
         return reinterpret_cast< const char* >( &m_v );
      }

      template< std::size_t I >
      constexpr int size() const noexcept
      {
         static_assert( I < columns );
         return sizeof( long );
      }

      template< std::size_t I >
      constexpr int format() const noexcept
      {
         static_assert( I < columns );
         return 1;
      }
   };

   template<>
   struct parameter_traits< unsigned long >
      : internal::string_helper
   {
      parameter_traits( const unsigned long v )
         : string_helper( internal::printf( "%lu", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< long long >
      : internal::string_helper
   {
      parameter_traits( const long long v )
         : string_helper( internal::printf( "%lld", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< unsigned long long >
      : internal::string_helper
   {
      parameter_traits( const unsigned long long v )
         : string_helper( internal::printf( "%llu", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< float >
      : parameter_traits< int >
   {
      parameter_traits( const float v ) noexcept
         : parameter_traits< int >( ftoi( v ) )
      {
      }
   };

   template<>
   struct parameter_traits< double >
      : parameter_traits< long >
   {
      parameter_traits( const double v )
         : parameter_traits< long >( dtol( v ) )
      {
      }
   };

   template<>
   struct parameter_traits< long double >
      : internal::string_helper
   {
      parameter_traits( const long double v )
         : string_helper( internal::printf_helper( "%.21Lg", v ) )
      {
      }
   };

   template< typename T >
   struct parameter_traits< std::optional< T > >
   {
   private:
      using U = parameter_traits< std::decay_t< T > >;
      std::optional< U > m_forwarder;

   public:
      parameter_traits( const std::optional< T >& v )
      {
         if( v ) {
            m_forwarder.emplace( *v );
         }
      }

      parameter_traits( std::optional< T >&& v )
      {
         if( v ) {
            m_forwarder.emplace( std::move( *v ) );
         }
      }

      static constexpr std::size_t columns = 1;
      static_assert( U::columns == 1 );

      template< std::size_t I >
      constexpr const char* c_str() const noexcept
      {
         static_assert( I < columns );
         return m_forwarder ? m_forwarder->template c_str< I >() : nullptr;
      }

      template< std::size_t I >
      constexpr int size() const noexcept
      {
         static_assert( I < columns );
         return m_forwarder ? m_forwarder->template size< I >() : 0;
      }

      template< std::size_t I >
      constexpr int format() const noexcept
      {
         static_assert( I < columns );
         return m_forwarder ? m_forwarder->template format< I >() : 0;
      }
   };

}  // namespace tao::pq

#endif
<commit_msg>Use bit_cast<commit_after>// Copyright (c) 2016-2019 Daniel Frey and Dr. Colin Hirsch
// Please see LICENSE for license or visit https://github.com/taocpp/taopq/

#ifndef TAO_PQ_PARAMETER_TRAITS_HPP
#define TAO_PQ_PARAMETER_TRAITS_HPP

#include <cmath>
#include <optional>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>

#include <byteswap.h>
#include <cstring>

#include <tao/pq/internal/printf.hpp>
#include <tao/pq/null.hpp>

namespace tao::pq
{
   template< typename, typename = void >
   struct parameter_traits;

   namespace internal
   {
      template< typename To, typename From >
      std::enable_if_t< ( sizeof( To ) == sizeof( From ) ) && std::is_trivially_copyable< From >::value && std::is_trivial< To >::value, To >
      bit_cast( const From& src ) noexcept
      {
         To dst;
         std::memcpy( &dst, &src, sizeof( To ) );
         return dst;
      }

      class char_pointer_helper
      {
      private:
         const char* m_p;

      protected:
         explicit char_pointer_helper( const char* p ) noexcept
            : m_p( p )
         {
         }

      public:
         static constexpr std::size_t columns = 1;

         template< std::size_t I >
         const char* c_str() const noexcept
         {
            static_assert( I < columns );
            return m_p;
         }

         template< std::size_t I >
         constexpr int size() const noexcept
         {
            static_assert( I < columns );
            return 0;
         }

         template< std::size_t I >
         constexpr int format() const noexcept
         {
            static_assert( I < columns );
            return 0;
         }
      };

      class string_helper
      {
      private:
         std::string m_s;

      protected:
         template< typename... Ts >
         explicit string_helper( Ts&&... ts ) noexcept( noexcept( std::string( std::forward< Ts >( ts )... ) ) )
            : m_s( std::forward< Ts >( ts )... )
         {
         }

      public:
         static constexpr std::size_t columns = 1;

         template< std::size_t I >
         const char* c_str() const noexcept
         {
            static_assert( I < columns );
            return m_s.c_str();
         }

         template< std::size_t I >
         constexpr int size() const noexcept
         {
            static_assert( I < columns );
            return 0;
         }

         template< std::size_t I >
         constexpr int format() const noexcept
         {
            static_assert( I < columns );
            return 0;
         }
      };

      template< typename T >
      [[nodiscard]] std::string printf_helper( const char* format, const T v )
      {
         if( std::isfinite( v ) ) {
            return internal::printf( format, v );
         }
         if( std::isnan( v ) ) {
            return "NAN";
         }
         return ( v < 0 ) ? "-INF" : "INF";
      }

   }  // namespace internal

   template<>
   struct parameter_traits< null_t >
   {
      explicit parameter_traits( const null_t& ) noexcept
      {
      }

      static constexpr std::size_t columns = 1;

      template< std::size_t I >
      constexpr const char* c_str() const noexcept
      {
         static_assert( I < columns );
         return nullptr;
      }

      template< std::size_t I >
      constexpr int size() const noexcept
      {
         static_assert( I < columns );
         return 0;
      }

      template< std::size_t I >
      constexpr int format() const noexcept
      {
         static_assert( I < columns );
         return 0;
      }
   };

   template<>
   struct parameter_traits< const char* >
      : internal::char_pointer_helper
   {
      explicit parameter_traits( const char* p ) noexcept
         : char_pointer_helper( p )
      {
      }
   };

   template<>
   struct parameter_traits< std::string >
      : internal::char_pointer_helper
   {
      explicit parameter_traits( const std::string& v ) noexcept
         : char_pointer_helper( v.c_str() )
      {
      }
   };

   template<>
   struct parameter_traits< bool >
      : internal::char_pointer_helper
   {
      explicit parameter_traits( const bool v ) noexcept
         : char_pointer_helper( v ? "TRUE" : "FALSE" )
      {
      }
   };

   template<>
   struct parameter_traits< char >
      : internal::string_helper
   {
      explicit parameter_traits( const char v )
         : string_helper( 1, v )
      {
      }
   };

   template<>
   struct parameter_traits< signed char >
      : internal::string_helper
   {
      explicit parameter_traits( const signed char v )
         : string_helper( internal::printf( "%hhd", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< unsigned char >
      : internal::string_helper
   {
      explicit parameter_traits( const unsigned char v )
         : string_helper( internal::printf( "%hhu", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< short >
   {
      const short m_v;

      static_assert( sizeof( short ) == 2 );

      explicit parameter_traits( const short v ) noexcept
         : m_v( bswap_16( v ) )
      {
      }

      static constexpr std::size_t columns = 1;

      template< std::size_t I >
      const char* c_str() const noexcept
      {
         static_assert( I < columns );
         return reinterpret_cast< const char* >( &m_v );
      }

      template< std::size_t I >
      constexpr int size() const noexcept
      {
         static_assert( I < columns );
         return sizeof( short );
      }

      template< std::size_t I >
      constexpr int format() const noexcept
      {
         static_assert( I < columns );
         return 1;
      }
   };

   template<>
   struct parameter_traits< unsigned short >
      : internal::string_helper
   {
      explicit parameter_traits( const unsigned short v )
         : string_helper( internal::printf( "%hu", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< int >
   {
      const int m_v;

      static_assert( sizeof( int ) == 4 );

      explicit parameter_traits( const int v ) noexcept
         : m_v( bswap_32( v ) )
      {
      }

      static constexpr std::size_t columns = 1;

      template< std::size_t I >
      const char* c_str() const noexcept
      {
         static_assert( I < columns );
         return reinterpret_cast< const char* >( &m_v );
      }

      template< std::size_t I >
      constexpr int size() const noexcept
      {
         static_assert( I < columns );
         return sizeof( int );
      }

      template< std::size_t I >
      constexpr int format() const noexcept
      {
         static_assert( I < columns );
         return 1;
      }
   };

   template<>
   struct parameter_traits< unsigned >
      : internal::string_helper
   {
      explicit parameter_traits( const unsigned v )
         : string_helper( internal::printf( "%u", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< long >
   {
      const long m_v;

      static_assert( sizeof( long ) == 8 );

      explicit parameter_traits( const long v ) noexcept
         : m_v( bswap_64( v ) )
      {
      }

      static constexpr std::size_t columns = 1;

      template< std::size_t I >
      const char* c_str() const noexcept
      {
         static_assert( I < columns );
         return reinterpret_cast< const char* >( &m_v );
      }

      template< std::size_t I >
      constexpr int size() const noexcept
      {
         static_assert( I < columns );
         return sizeof( long );
      }

      template< std::size_t I >
      constexpr int format() const noexcept
      {
         static_assert( I < columns );
         return 1;
      }
   };

   template<>
   struct parameter_traits< unsigned long >
      : internal::string_helper
   {
      explicit parameter_traits( const unsigned long v )
         : string_helper( internal::printf( "%lu", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< long long >
      : internal::string_helper
   {
      explicit parameter_traits( const long long v )
         : string_helper( internal::printf( "%lld", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< unsigned long long >
      : internal::string_helper
   {
      explicit parameter_traits( const unsigned long long v )
         : string_helper( internal::printf( "%llu", v ) )
      {
      }
   };

   template<>
   struct parameter_traits< float >
      : parameter_traits< int >
   {
      explicit parameter_traits( const float v ) noexcept
         : parameter_traits< int >( internal::bit_cast< int >( v ) )
      {
      }
   };

   template<>
   struct parameter_traits< double >
      : parameter_traits< long >
   {
      explicit parameter_traits( const double v )
         : parameter_traits< long >( internal::bit_cast< long >( v ) )
      {
      }
   };

   template<>
   struct parameter_traits< long double >
      : internal::string_helper
   {
      explicit parameter_traits( const long double v )
         : string_helper( internal::printf_helper( "%.21Lg", v ) )
      {
      }
   };

   template< typename T >
   struct parameter_traits< std::optional< T > >
   {
   private:
      using U = parameter_traits< std::decay_t< T > >;
      std::optional< U > m_forwarder;

   public:
      explicit parameter_traits( const std::optional< T >& v )
      {
         if( v ) {
            m_forwarder.emplace( *v );
         }
      }

      explicit parameter_traits( std::optional< T >&& v )
      {
         if( v ) {
            m_forwarder.emplace( std::move( *v ) );
         }
      }

      static constexpr std::size_t columns = 1;
      static_assert( U::columns == 1 );

      template< std::size_t I >
      constexpr const char* c_str() const noexcept
      {
         static_assert( I < columns );
         return m_forwarder ? m_forwarder->template c_str< I >() : nullptr;
      }

      template< std::size_t I >
      constexpr int size() const noexcept
      {
         static_assert( I < columns );
         return m_forwarder ? m_forwarder->template size< I >() : 0;
      }

      template< std::size_t I >
      constexpr int format() const noexcept
      {
         static_assert( I < columns );
         return m_forwarder ? m_forwarder->template format< I >() : 0;
      }
   };

}  // namespace tao::pq

#endif
<|endoftext|>