text:
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/coruja_object.hpp>
#include <boost/serialization/coruja_unordered_map.hpp>
#include <boost/serialization/string.hpp>

template<typename ocontainer, typename expected_t>
void check_equal(const ocontainer& c, const expected_t& expected)
{
    assert(c.size() == expected.size());
    for(auto& e : c)
        assert(expected.at(e.first) == e.second);
}

#include <fstream>
#include <string>

int main()
{
    const std::string file("/tmp/coruja_object_serialization");
    {
        coruja::unordered_map<int, std::string> obj
        {{0, "abc"}, {1, "def"}, {2, "ghi"}};
        std::ofstream out(file);
        boost::archive::text_oarchive oa(out);
        oa << obj;
    }

    {
        coruja::unordered_map<int, std::string> obj;
        std::ifstream in(file);
        boost::archive::text_iarchive ia(in);
        ia >> obj;
        std::unordered_map<int, std::string> expected
            {{0, "abc"}, {1, "def"}, {2, "ghi"}};
        check_equal(obj, expected);
    }

    {
        coruja::unordered_map<int, coruja::object<std::string>> obj;
        obj.emplace(0, std::string{"abc"});
        obj.emplace(1, std::string{"def"});
        obj.emplace(2, std::string{"ghi"});
        std::ofstream out(file);
        boost::archive::text_oarchive oa(out);
        oa << obj;
    }

    {
        coruja::unordered_map<int, coruja::object<std::string>> obj;
        std::ifstream in(file);
        boost::archive::text_iarchive ia(in);
        ia >> obj;
        coruja::unordered_map<int, coruja::object<std::string>> expected;
        expected.emplace(0, std::string{"abc"});
        expected.emplace(1, std::string{"def"});
        expected.emplace(2, std::string{"ghi"});
        check_equal(obj, expected);
    }
}

ext: cpp

max_stars_repo_name: romariorios/coruja

max_issues_repo_name: romariorios/coruja

max_forks_repo_name: romariorios/coruja


text:
/**
 * @file feedforward_network_test.cpp
 * @author Marcus Edel
 * @author Palash Ahuja
 *
 * Tests the feed forward network.
 *
 * mlpack is free software; you may redistribute it and/or modify it under the
 * terms of the 3-clause BSD license.  You should have received a copy of the
 * 3-clause BSD license along with mlpack.  If not, see
 * http://www.opensource.org/licenses/BSD-3-Clause for more information.
 */
#include <mlpack/core.hpp>

#include <mlpack/methods/ann/layer/layer.hpp>
#include <mlpack/methods/ann/loss_functions/mean_squared_error.hpp>
#include <mlpack/methods/ann/ffn.hpp>

#include <ensmallen.hpp>

#include <boost/test/unit_test.hpp>
#include "test_tools.hpp"
#include "serialization.hpp"
#include "custom_layer.hpp"

using namespace mlpack;
using namespace mlpack::ann;

BOOST_AUTO_TEST_SUITE(FeedForwardNetworkTest);

/**
 * Train and evaluate a vanilla network with the specified structure.
 */
template<typename MatType = arma::mat>
void BuildVanillaNetwork(MatType& trainData,
                         MatType& trainLabels,
                         MatType& testData,
                         MatType& testLabels,
                         const size_t outputSize,
                         const size_t hiddenLayerSize,
                         const size_t maxEpochs,
                         const double classificationErrorThreshold)
{
  /*
   * Construct a feed forward network with trainData.n_rows input nodes,
   * hiddenLayerSize hidden nodes and trainLabels.n_rows output nodes. The
   * network structure looks like:
   *
   *  Input         Hidden        Output
   *  Layer         Layer         Layer
   * +-----+       +-----+       +-----+
   * |     |       |     |       |     |
   * |     +------>|     +------>|     |
   * |     |     +>|     |     +>|     |
   * +-----+     | +--+--+     | +-----+
   *             |             |
   *  Bias       |  Bias       |
   *  Layer      |  Layer      |
   * +-----+     | +-----+     |
   * |     |     | |     |     |
   * |     +-----+ |     +-----+
   * |     |       |     |
   * +-----+       +-----+
   */

  FFN<NegativeLogLikelihood<> > model;
  model.Add<Linear<> >(trainData.n_rows, hiddenLayerSize);
  model.Add<SigmoidLayer<> >();
  model.Add<Linear<> >(hiddenLayerSize, outputSize);
  model.Add<LogSoftMax<> >();

  // RMSProp opt(0.01, 32, 0.88, 1e-8, maxEpochs * trainData.n_cols, -1);
  ens::RMSProp opt(0.01, 32, 0.88, 1e-8, maxEpochs * trainData.n_cols, -1);
  model.Train(trainData, trainLabels, opt);

  MatType predictionTemp;
  model.Predict(testData, predictionTemp);
  MatType prediction = arma::zeros<MatType>(1, predictionTemp.n_cols);

  for (size_t i = 0; i < predictionTemp.n_cols; ++i)
  {
    prediction(i) = arma::as_scalar(arma::find(
        arma::max(predictionTemp.col(i)) == predictionTemp.col(i), 1)) + 1;
  }

  size_t error = 0;
  for (size_t i = 0; i < testData.n_cols; i++)
  {
    if (int(arma::as_scalar(prediction.col(i))) ==
        int(arma::as_scalar(testLabels.col(i))))
    {
      error++;
    }
  }

  double classificationError = 1 - double(error) / testData.n_cols;
  BOOST_REQUIRE_LE(classificationError, classificationErrorThreshold);
}

/**
 * Train the vanilla network on a larger dataset.
 */
BOOST_AUTO_TEST_CASE(VanillaNetworkTest)
{
  // Load the dataset.
  arma::mat trainData;
  data::Load("thyroid_train.csv", trainData, true);

  arma::mat trainLabels = trainData.row(trainData.n_rows - 1);
  trainData.shed_row(trainData.n_rows - 1);

  arma::mat testData;
  data::Load("thyroid_test.csv", testData, true);

  arma::mat testLabels = testData.row(testData.n_rows - 1);
  testData.shed_row(testData.n_rows - 1);

  // Vanilla neural net with logistic activation function.
  // Because 92 percent of the patients are not hyperthyroid the neural
  // network must be significant better than 92%.
  BuildVanillaNetwork<>
      (trainData, trainLabels, testData, testLabels, 3, 8, 10, 0.1);

  arma::mat dataset;
  dataset.load("mnist_first250_training_4s_and_9s.arm");

  // Normalize each point since these are images.
  for (size_t i = 0; i < dataset.n_cols; ++i)
    dataset.col(i) /= norm(dataset.col(i), 2);

  arma::mat labels = arma::zeros(1, dataset.n_cols);
  labels.submat(0, labels.n_cols / 2, 0, labels.n_cols - 1).fill(1);
  labels += 1;

  // Vanilla neural net with logistic activation function.
  BuildVanillaNetwork<>
      (dataset, labels, dataset, labels, 2, 10, 10, 0.2);
}

BOOST_AUTO_TEST_CASE(ForwardBackwardTest)
{
  arma::mat dataset;
  dataset.load("mnist_first250_training_4s_and_9s.arm");

  // Normalize each point since these are images.
  for (size_t i = 0; i < dataset.n_cols; ++i)
    dataset.col(i) /= norm(dataset.col(i), 2);

  arma::mat labels = arma::zeros(1, dataset.n_cols);
  labels.submat(0, labels.n_cols / 2, 0, labels.n_cols - 1).fill(1);
  labels += 1;

  FFN<NegativeLogLikelihood<> > model;
  model.Add<Linear<> >(dataset.n_rows, 50);
  model.Add<SigmoidLayer<> >();
  model.Add<Linear<> >(50, 10);
  model.Add<LogSoftMax<> >();

  ens::VanillaUpdate opt;
  model.ResetParameters();
  opt.Initialize(model.Parameters().n_rows, model.Parameters().n_cols);
  double stepSize = 0.01;
  size_t batchSize = 10;

  size_t iteration = 0;
  bool converged = false;
  while (iteration < 100)
  {
    arma::running_stat<double> error;
    size_t batchStart = 0;
    while (batchStart < dataset.n_cols)
    {
      size_t batchEnd = std::min(batchStart + batchSize,
          (size_t) dataset.n_cols);
      arma::mat currentData = dataset.cols(batchStart, batchEnd - 1);
      arma::mat currentLabels = labels.cols(batchStart, batchEnd - 1);
      arma::mat currentResuls;
      model.Forward(currentData, currentResuls);
      arma::mat gradients;
      model.Backward(currentLabels, gradients);
      opt.Update(model.Parameters(), stepSize, gradients);
      batchStart = batchEnd;

      arma::mat prediction = arma::zeros<arma::mat>(1, currentResuls.n_cols);

      for (size_t i = 0; i < currentResuls.n_cols; ++i)
      {
        prediction(i) = arma::as_scalar(arma::find(
            arma::max(currentResuls.col(i)) == currentResuls.col(i), 1)) + 1;
      }

      size_t correct = 0;
      for (size_t i = 0; i < currentLabels.n_cols; i++)
      {
        if (int(arma::as_scalar(prediction.col(i))) ==
            int(arma::as_scalar(currentLabels.col(i))))
        {
          correct++;
        }
      }

      error(1 - (double) correct / batchSize);
    }
    Log::Debug << "Current training error: " << error.mean() << std::endl;
    iteration++;
    if (error.mean() < 0.05)
    {
      converged = true;
      break;
    }
  }

  BOOST_REQUIRE(converged);
}

/**
 * Train and evaluate a Dropout network with the specified structure.
 */
template<typename MatType = arma::mat>
void BuildDropoutNetwork(MatType& trainData,
                         MatType& trainLabels,
                         MatType& testData,
                         MatType& testLabels,
                         const size_t outputSize,
                         const size_t hiddenLayerSize,
                         const size_t maxEpochs,
                         const double classificationErrorThreshold)
{
  /*
   * Construct a feed forward network with trainData.n_rows input nodes,
   * hiddenLayerSize hidden nodes and trainLabels.n_rows output nodes. The
   * network structure looks like:
   *
   *  Input         Hidden        Dropout      Output
   *  Layer         Layer         Layer        Layer
   * +-----+       +-----+       +-----+       +-----+
   * |     |       |     |       |     |       |     |
   * |     +------>|     +------>|     +------>|     |
   * |     |     +>|     |       |     |       |     |
   * +-----+     | +--+--+       +-----+       +-----+
   *             |
   *  Bias       |
   *  Layer      |
   * +-----+     |
   * |     |     |
   * |     +-----+
   * |     |
   * +-----+
   */

  FFN<NegativeLogLikelihood<> > model;
  model.Add<Linear<> >(trainData.n_rows, hiddenLayerSize);
  model.Add<SigmoidLayer<> >();
  model.Add<Dropout<> >();
  model.Add<Linear<> >(hiddenLayerSize, outputSize);
  model.Add<LogSoftMax<> >();

  ens::RMSProp opt(0.01, 32, 0.88, 1e-8, maxEpochs * trainData.n_cols, -1);

  model.Train(trainData, trainLabels, opt);

  MatType predictionTemp;
  model.Predict(testData, predictionTemp);
  MatType prediction = arma::zeros<MatType>(1, predictionTemp.n_cols);

  for (size_t i = 0; i < predictionTemp.n_cols; ++i)
  {
    prediction(i) = arma::as_scalar(arma::find(
        arma::max(predictionTemp.col(i)) == predictionTemp.col(i), 1)) + 1;
  }

  size_t error = 0;
  for (size_t i = 0; i < testData.n_cols; i++)
  {
    if (int(arma::as_scalar(prediction.col(i))) ==
        int(arma::as_scalar(testLabels.col(i))))
    {
      error++;
    }
  }

  double classificationError = 1 - double(error) / testData.n_cols;
  BOOST_REQUIRE_LE(classificationError, classificationErrorThreshold);
}

/**
 * Train the dropout network on a larger dataset.
 */
BOOST_AUTO_TEST_CASE(DropoutNetworkTest)
{
  // Load the dataset.
  arma::mat trainData;
  data::Load("thyroid_train.csv", trainData, true);

  arma::mat trainLabels = trainData.row(trainData.n_rows - 1);
  trainData.shed_row(trainData.n_rows - 1);

  arma::mat testData;
  data::Load("thyroid_test.csv", testData, true);

  arma::mat testLabels = testData.row(testData.n_rows - 1);
  testData.shed_row(testData.n_rows - 1);

  // Vanilla neural net with logistic activation function.
  // Because 92 percent of the patients are not hyperthyroid the neural
  // network must be significant better than 92%.
  BuildDropoutNetwork<>
      (trainData, trainLabels, testData, testLabels, 3, 8, 10, 0.1);

  arma::mat dataset;
  dataset.load("mnist_first250_training_4s_and_9s.arm");

  // Normalize each point since these are images.
  for (size_t i = 0; i < dataset.n_cols; ++i)
    dataset.col(i) /= norm(dataset.col(i), 2);

  arma::mat labels = arma::zeros(1, dataset.n_cols);
  labels.submat(0, labels.n_cols / 2, 0, labels.n_cols - 1).fill(1);
  labels += 1;

  // Vanilla neural net with logistic activation function.
  BuildDropoutNetwork<>
      (dataset, labels, dataset, labels, 2, 10, 10, 0.2);
}

/**
 * Train and evaluate a DropConnect network(with a baselayer) with the
 * specified structure.
 */
template<typename MatType = arma::mat>
void BuildDropConnectNetwork(MatType& trainData,
                             MatType& trainLabels,
                             MatType& testData,
                             MatType& testLabels,
                             const size_t outputSize,
                             const size_t hiddenLayerSize,
                             const size_t maxEpochs,
                             const double classificationErrorThreshold)
{
 /*
  *  Construct a feed forward network with trainData.n_rows input nodes,
  *  hiddenLayerSize hidden nodes and trainLabels.n_rows output nodes. The
  *  network struct that looks like:
  *
  *  Input         Hidden     DropConnect     Output
  *  Layer         Layer         Layer        Layer
  * +-----+       +-----+       +-----+       +-----+
  * |     |       |     |       |     |       |     |
  * |     +------>|     +------>|     +------>|     |
  * |     |     +>|     |       |     |       |     |
  * +-----+     | +--+--+       +-----+       +-----+
  *             |
  *  Bias       |
  *  Layer      |
  * +-----+     |
  * |     |     |
  * |     +-----+
  * |     |
  * +-----+
  *
  *
  */

  FFN<NegativeLogLikelihood<> > model;
  model.Add<Linear<> >(trainData.n_rows, hiddenLayerSize);
  model.Add<SigmoidLayer<> >();
  model.Add<DropConnect<> >(hiddenLayerSize, outputSize);
  model.Add<LogSoftMax<> >();

  ens::RMSProp opt(0.01, 32, 0.88, 1e-8, maxEpochs * trainData.n_cols, -1);

  model.Train(trainData, trainLabels, opt);

  MatType predictionTemp;
  model.Predict(testData, predictionTemp);
  MatType prediction = arma::zeros<MatType>(1, predictionTemp.n_cols);

  for (size_t i = 0; i < predictionTemp.n_cols; ++i)
  {
    prediction(i) = arma::as_scalar(arma::find(
        arma::max(predictionTemp.col(i)) == predictionTemp.col(i), 1)) + 1;
  }

  size_t error = 0;
  for (size_t i = 0; i < testData.n_cols; i++)
  {
    if (int(arma::as_scalar(prediction.col(i))) ==
        int(arma::as_scalar(testLabels.col(i))))
    {
      error++;
    }
  }

  double classificationError = 1 - double(error) / testData.n_cols;
  BOOST_REQUIRE_LE(classificationError, classificationErrorThreshold);
}

/**
 * Train the dropconnect network on a larger dataset.
 */
BOOST_AUTO_TEST_CASE(DropConnectNetworkTest)
{
  // Load the dataset.
  arma::mat trainData;
  data::Load("thyroid_train.csv", trainData, true);

  arma::mat trainLabels = trainData.row(trainData.n_rows - 1);
  trainData.shed_row(trainData.n_rows - 1);

  arma::mat testData;
  data::Load("thyroid_test.csv", testData, true);

  arma::mat testLabels = testData.row(testData.n_rows - 1);
  testData.shed_row(testData.n_rows - 1);

  // Vanilla neural net with logistic activation function.
  // Because 92 percent of the patients are not hyperthyroid the neural
  // network must be significant better than 92%.
  BuildDropConnectNetwork<>
      (trainData, trainLabels, testData, testLabels, 3, 8, 10, 0.1);

  arma::mat dataset;
  dataset.load("mnist_first250_training_4s_and_9s.arm");

  // Normalize each point since these are images.
  for (size_t i = 0; i < dataset.n_cols; ++i)
    dataset.col(i) /= norm(dataset.col(i), 2);

  arma::mat labels = arma::zeros(1, dataset.n_cols);
  labels.submat(0, labels.n_cols / 2, 0, labels.n_cols - 1).fill(1);
  labels += 1;

  // Vanilla neural net with logistic activation function.
  BuildDropConnectNetwork<>
      (dataset, labels, dataset, labels, 2, 10, 10, 0.2);
}

/**
 * Test miscellaneous things of FFN,
 * e.g. copy/move constructor, assignment operator.
 */
BOOST_AUTO_TEST_CASE(FFNMiscTest)
{
  FFN<MeanSquaredError<>> model;
  model.Add<Linear<>>(2, 3);
  model.Add<ReLULayer<>>();

  auto copiedModel(model);
  copiedModel = model;
  auto movedModel(std::move(model));
  movedModel = std::move(copiedModel);
}

/**
 * Test that serialization works ok.
 */
BOOST_AUTO_TEST_CASE(SerializationTest)
{
  // Load the dataset.
  arma::mat trainData;
  data::Load("thyroid_train.csv", trainData, true);

  arma::mat trainLabels = trainData.row(trainData.n_rows - 1);
  trainData.shed_row(trainData.n_rows - 1);

  arma::mat testData;
  data::Load("thyroid_test.csv", testData, true);

  arma::mat testLabels = testData.row(testData.n_rows - 1);
  testData.shed_row(testData.n_rows - 1);

  // Vanilla neural net with logistic activation function.
  // Because 92 percent of the patients are not hyperthyroid the neural
  // network must be significant better than 92%.
  FFN<NegativeLogLikelihood<> > model;
  model.Add<Linear<> >(trainData.n_rows, 8);
  model.Add<SigmoidLayer<> >();
  model.Add<Dropout<> >();
  model.Add<Linear<> >(8, 3);
  model.Add<LogSoftMax<> >();

  ens::RMSProp opt(0.01, 32, 0.88, 1e-8, trainData.n_cols /* 1 epoch */, -1);

  model.Train(trainData, trainLabels, opt);

  FFN<NegativeLogLikelihood<>> xmlModel, textModel, binaryModel;
  xmlModel.Add<Linear<>>(10, 10); // Layer that will get removed.

  // Serialize into other models.
  SerializeObjectAll(model, xmlModel, textModel, binaryModel);

  arma::mat predictions, xmlPredictions, textPredictions, binaryPredictions;
  model.Predict(testData, predictions);
  xmlModel.Predict(testData, xmlPredictions);
  textModel.Predict(testData, textPredictions);
  textModel.Predict(testData, binaryPredictions);

  CheckMatrices(predictions, xmlPredictions, textPredictions,
      binaryPredictions);
}

/**
 * Test if the custom layers work. The target is to see if the code compiles
 * when the Train and Prediction are called.
 */
BOOST_AUTO_TEST_CASE(CustomLayerTest)
{
  // Load the dataset.
  arma::mat trainData;
  data::Load("thyroid_train.csv", trainData, true);

  arma::mat trainLabels = trainData.row(trainData.n_rows - 1);
  trainData.shed_row(trainData.n_rows - 1);

  arma::mat testData;
  data::Load("thyroid_test.csv", testData, true);

  arma::mat testLabels = testData.row(testData.n_rows - 1);
  testData.shed_row(testData.n_rows - 1);

  FFN<NegativeLogLikelihood<>, RandomInitialization, CustomLayer<> > model;
  model.Add<Linear<> >(trainData.n_rows, 8);
  model.Add<CustomLayer<> >();
  model.Add<Linear<> >(8, 3);
  model.Add<LogSoftMax<> >();

  ens::RMSProp opt(0.01, 32, 0.88, 1e-8, 15, -1);
  model.Train(trainData, trainLabels, opt);

  arma::mat predictionTemp;
  model.Predict(testData, predictionTemp);
  arma::mat prediction = arma::zeros<arma::mat>(1, predictionTemp.n_cols);
}

/**
 * Test the overload of Forward function which allows partial forward pass.
 */
BOOST_AUTO_TEST_CASE(PartialForwardTest)
{
  FFN<NegativeLogLikelihood<>, RandomInitialization> model;
  model.Add<Linear<> >(5, 10);

  // Add a new Add<> module which adds a constant term to the input.
  Add<>* addModule = new Add<>(10);
  model.Add(addModule);

  LinearNoBias<>* linearNoBiasModule = new LinearNoBias<>(10, 10);
  model.Add(linearNoBiasModule);

  model.Add<Linear<> >(10, 10);

  model.ResetParameters();
  // Set the parameters of the Add<> module to a matrix of ones.
  addModule->Parameters() = arma::ones(10, 1);
  // Set the parameters of the LinearNoBias<> module to a matrix of ones.
  linearNoBiasModule->Parameters() = arma::ones(10, 10);

  arma::mat input = arma::ones(10, 1);
  arma::mat output;

  // Forward pass only through the Add module.
  model.Forward(input,
                output,
                1 /* Index of the Add module */,
                1 /* Index of the Add module */);

  // As we only forward pass through Add module, input and output should
  // differ by a matrix of ones.
  CheckMatrices(input, output - 1);

  // Forward pass only through the Add module and the LinearNoBias module.
  model.Forward(input,
                output,
                1 /* Index of the Add module */,
                2 /* Index of the LinearNoBias module */);

  // As we only forward pass through Add module followed by the LinearNoBias
  // module, output should be a matrix of 20s.(output = weight * input)
  CheckMatrices(output, arma::ones(10, 1) * 20);
}

/**
 * Test that FFN::Train() returns finite objective value.
 */
BOOST_AUTO_TEST_CASE(FFNTrainReturnObjective)
{
  // Load the dataset.
  arma::mat trainData;
  data::Load("thyroid_train.csv", trainData, true);

  arma::mat trainLabels = trainData.row(trainData.n_rows - 1);
  trainData.shed_row(trainData.n_rows - 1);

  arma::mat testData;
  data::Load("thyroid_test.csv", testData, true);

  arma::mat testLabels = testData.row(testData.n_rows - 1);
  testData.shed_row(testData.n_rows - 1);

  // Vanilla neural net with logistic activation function.
  // Because 92 percent of the patients are not hyperthyroid the neural
  // network must be significantly better than 92%.
  FFN<NegativeLogLikelihood<> > model;
  model.Add<Linear<> >(trainData.n_rows, 8);
  model.Add<SigmoidLayer<> >();
  model.Add<Dropout<> >();
  model.Add<Linear<> >(8, 3);
  model.Add<LogSoftMax<> >();

  ens::RMSProp opt(0.01, 32, 0.88, 1e-8, trainData.n_cols /* 1 epoch */, -1);

  double objVal = model.Train(trainData, trainLabels, opt);

  BOOST_REQUIRE_EQUAL(std::isfinite(objVal), true);
}
BOOST_AUTO_TEST_SUITE_END();

max_stars_repo_name: yashMustak/mlpack

max_issues_repo_name: yashMustak/mlpack

max_forks_repo_name: yashMustak/mlpack


text:
/* Copyright (C) 2014 InfiniDB, Inc.

   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; version 2 of
   the License.

   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., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA. */

/***********************************************************************
 *   $Id: insertdmlpackage.cpp 9210 2013-01-21 14:10:42Z rdempsey $
 *
 *
 ***********************************************************************/
#include <stdexcept>
#include <iostream>
#include <boost/tokenizer.hpp>
#include <string>
using namespace std;
#define INSERTDMLPKG_DLLEXPORT
#include "insertdmlpackage.h"
#undef INSERTDMLPKG_DLLEXPORT

#ifdef _MSC_VER
#define strcasecmp stricmp
#endif

namespace dmlpackage
{

InsertDMLPackage::InsertDMLPackage()
{}

InsertDMLPackage::InsertDMLPackage( std::string schemaName, std::string tableName,
                                    std::string dmlStatement, int sessionID )
    : CalpontDMLPackage( schemaName, tableName, dmlStatement, sessionID )
{}

InsertDMLPackage::~InsertDMLPackage()
{}

int InsertDMLPackage::write(messageqcpp::ByteStream& bytestream)
{
    int retval = 1;

    messageqcpp::ByteStream::byte package_type = DML_INSERT;
    bytestream << package_type;

    messageqcpp::ByteStream::quadbyte session_id = fSessionID;
    bytestream << session_id;

    bytestream << fUuid;

    bytestream << fDMLStatement;
    bytestream << fDMLStatement;
    bytestream << fSchemaName;
    bytestream << fTimeZone;
    bytestream << (uint8_t)fLogging;
    bytestream << (uint8_t)fLogending;

    bytestream << fTableOid;
    bytestream << static_cast<messageqcpp::ByteStream::byte>(fIsInsertSelect);
    bytestream << static_cast<messageqcpp::ByteStream::byte>(fIsBatchInsert);
    bytestream << static_cast<messageqcpp::ByteStream::byte>(fIsCacheInsert);
    bytestream << static_cast<messageqcpp::ByteStream::byte>(fIsAutocommitOn);

    if (fTable != 0)
    {
        retval = fTable->write(bytestream);
    }

    return retval;
}

int InsertDMLPackage::read(messageqcpp::ByteStream& bytestream)
{
    int retval = 1;

    messageqcpp::ByteStream::quadbyte session_id;
    bytestream >> session_id;
    fSessionID = session_id;
    bytestream >> fUuid;

    std::string dmlStatement;
    bytestream >> fDMLStatement;
    bytestream >> fSQLStatement;
    bytestream >> fSchemaName;
    bytestream >> fTimeZone;
    uint8_t logging;
    bytestream >> logging;
    fLogging = (logging != 0);
    uint8_t logending;
    bytestream >> logending;
    fLogending = (logending != 0);

    bytestream >> fTableOid;
    bytestream >> reinterpret_cast<messageqcpp::ByteStream::byte&>(fIsInsertSelect);
    bytestream >> reinterpret_cast<messageqcpp::ByteStream::byte&>(fIsBatchInsert);
    bytestream >> reinterpret_cast<messageqcpp::ByteStream::byte&>(fIsCacheInsert);
    bytestream >> reinterpret_cast<messageqcpp::ByteStream::byte&>(fIsAutocommitOn);

    fTable = new DMLTable();
    retval = fTable->read(bytestream);
    return retval;
}

void InsertDMLPackage::readMetaData(messageqcpp::ByteStream& bytestream)
{
    messageqcpp::ByteStream::quadbyte session_id;
    bytestream >> session_id;
    fSessionID = session_id;
    bytestream >> fUuid;

    std::string dmlStatement;
    bytestream >> fDMLStatement;
    bytestream >> fSQLStatement;
    bytestream >> fSchemaName;
    bytestream >> fTimeZone;
    uint8_t logging;
    bytestream >> logging;
    fLogging = (logging != 0);
    uint8_t logending;
    bytestream >> logending;
    fLogending = (logending != 0);

    bytestream >> fTableOid;
    bytestream >> reinterpret_cast<messageqcpp::ByteStream::byte&>(fIsInsertSelect);
    bytestream >> reinterpret_cast<messageqcpp::ByteStream::byte&>(fIsBatchInsert);
    bytestream >> reinterpret_cast<messageqcpp::ByteStream::byte&>(fIsCacheInsert);
    bytestream >> reinterpret_cast<messageqcpp::ByteStream::byte&>(fIsAutocommitOn);

    fTable = new DMLTable();
    fTable->readMetaData(bytestream);
}

// Has to be called after InsertDMLPackage::readMetaData()
void InsertDMLPackage::readRowData(messageqcpp::ByteStream& bytestream)
{
    fTable->readRowData(bytestream);
}

int InsertDMLPackage::buildFromBuffer(std::string& buffer, int columns, int rows)
{
#ifdef DML_PACKAGE_DEBUG
    // cout << "The data buffer received: " << buffer << endl;
#endif
    int retval = 1;

    initializeTable();

    std::vector<std::string> dataList;
    typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
    boost::char_separator<char> sep(",");
    tokenizer tokens(buffer, sep);

    for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter)
    {
        dataList.push_back(StripLeadingWhitespace(*tok_iter));

    }

    int n = 0;

    for (int i = 0; i < rows; i++)
    {
        //get a new row
        Row* aRowPtr = new Row();
        std::string colName;
        std::string colValue;

        for (int j = 0; j < columns; j++)
        {
            //Build a column list
            colName = dataList[n];
            n++;
            colValue = dataList[n];
            n++;
#ifdef DML_PACKAGE_DEBUG
            //cout << "The column data: " << colName << " " << colValue << endl;
#endif
            DMLColumn* aColumn = new DMLColumn(colName, colValue, false);
            (aRowPtr->get_ColumnList()).push_back(aColumn);
        }

        //build a row list for a table
        fTable->get_RowList().push_back(aRowPtr);
    }

    return retval;
}

int InsertDMLPackage::buildFromMysqlBuffer(ColNameList& colNameList, TableValuesMap& tableValuesMap, int columns, int rows, NullValuesBitset& nullValues )
{
    int retval = 1;

    initializeTable();
    Row* aRowPtr = new Row();
    std::string colName;
    std::vector<std::string> colValList;

    for (int j = 0; j < columns; j++)
    {
        //Build a column list
        colName = colNameList[j];

        colValList = tableValuesMap[j];

        DMLColumn* aColumn = new DMLColumn(colName, colValList, false, 0, nullValues[j]);
        (aRowPtr->get_ColumnList()).push_back(aColumn);
    }

    //build a row list for a table
    fTable->get_RowList().push_back(aRowPtr);
    aRowPtr = NULL;
    delete aRowPtr;
    return retval;
}

int InsertDMLPackage::buildFromSqlStatement(SqlStatement& sqlStatement)
{

    int retval = 1;

    InsertSqlStatement& insertStmt = dynamic_cast<InsertSqlStatement&>(sqlStatement);

    if (!insertStmt.fValuesOrQueryPtr)
        throw runtime_error("insertStmt.fValuesOrQueryPtr == NULL");

    initializeTable();
    bool isNULL = false;

    // only if we don't have a select statement
    if (0 == insertStmt.fValuesOrQueryPtr->fQuerySpecPtr)
    {

        ColumnNameList columnNameList = insertStmt.fColumnList;

        if (columnNameList.size())
        {

            ValuesList valuesList = insertStmt.fValuesOrQueryPtr->fValuesList;

            if (columnNameList.size() != valuesList.size())
            {
                throw logic_error("Column names and values count mismatch!");
            }

            Row* aRow = new Row();

            for (unsigned int i = 0; i < columnNameList.size(); i++)
            {
                DMLColumn* aColumn = new DMLColumn(columnNameList[i], valuesList[i], isNULL);
                (aRow->get_ColumnList()).push_back(aColumn);
            }

            fTable->get_RowList().push_back(aRow);

        }
        else
        {
            ValuesList valuesList = insertStmt.fValuesOrQueryPtr->fValuesList;
            ValuesList::const_iterator iter = valuesList.begin();
            Row* aRow = new Row();
            std::string colName = "";
            std::string colValue;

            while (iter != valuesList.end())
            {
                colValue = *iter;

                if ( strcasecmp(colValue.c_str(), "NULL") == 0)
                {
                    isNULL = true;
                }
                else
                {
                    isNULL = false;
                }

                DMLColumn* aColumn = new DMLColumn(colName, colValue, isNULL);
                (aRow->get_ColumnList()).push_back(aColumn);

                ++iter;
            }

            fTable->get_RowList().push_back(aRow);
        }

    }
    else
    {
        fHasFilter = true;
        fQueryString = insertStmt.getQueryString();
    }

    return retval;
}

}  // namespace dmlpackage

max_stars_repo_name: zettadb/zettalib

max_issues_repo_name: zettadb/zettalib

max_forks_repo_name: zettadb/zettalib


text:
//  (C) Copyright Greg Landrum 2003-2006.
//  Derived from vector_indexing_suite.hpp,
//  which is (C) Copyright Joel de Guzman 2003.
//  Permission to copy, use, modify, sell and distribute this software
//  is granted provided this copyright notice appears in all copies. This
//  software is provided "as is" without express or implied warranty, and
//  with no claim as to its suitability for any purpose.

#ifndef LIST_INDEXING_SUITE_GL20042_HPP
#define LIST_INDEXING_SUITE_GL20042_HPP

#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/indexing_suite.hpp>
#include <boost/python/suite/indexing/container_utils.hpp>
#include <boost/python/iterator.hpp>

#include "pyint_api.h"

namespace boost {
namespace python {

// Forward declaration
template <class Container, bool NoProxy, class DerivedPolicies>
class list_indexing_suite;

namespace detail {
template <class Container, bool NoProxy>
class final_list_derived_policies
    : public list_indexing_suite<
          Container, NoProxy, final_list_derived_policies<Container, NoProxy>> {
};
}  // namespace detail

// The vector_indexing_suite class is a predefined indexing_suite derived
// class for wrapping std::vector (and std::vector like) classes. It provides
// all the policies required by the indexing_suite (see indexing_suite).
// Example usage:
//
//  class X {...};
//
//  ...
//
//      class_<std::vector<X> >("XVec")
//          .def(vector_indexing_suite<std::vector<X> >())
//      ;
//
// By default indexed elements are returned by proxy. This can be
// disabled by supplying *true* in the NoProxy template parameter.
//
template <class Container, bool NoProxy = false,
          class DerivedPolicies =
              detail::final_list_derived_policies<Container, NoProxy>>
class list_indexing_suite
    : public indexing_suite<Container, DerivedPolicies, NoProxy> {
 public:
  typedef typename Container::value_type data_type;
  typedef typename Container::value_type key_type;
  typedef typename Container::size_type index_type;
  typedef typename Container::size_type size_type;
  typedef typename Container::iterator iterator_type;

  static typename mpl::if_<is_class<data_type>, data_type&, data_type>::type
  get_item(Container& container, index_type i) {
    iterator_type pos = moveToPos(container, i);
    return *pos;
  }
  static object get_slice(Container& container, index_type from,
                          index_type to) {
    Container res;
    iterator_type beg = moveToPos(container, from);
    iterator_type end = moveToPos(container, to);
    std::copy(beg, end, res.begin());
    return object(res);
  }
  static void set_item(Container& container, index_type i, data_type const& v) {
    iterator_type pos = moveToPos(container, i);
    *pos = v;
  }

  static void set_slice(Container& container, index_type from, index_type to,
                        data_type const& v) {
    iterator_type beg = moveToPos(container, from);
    iterator_type end = moveToPos(container, to);
    container.erase(beg, end);
    // FIX: didn't we just invalidate this iterator?
    container.insert(beg, v);
  }
  template <class Iter>
  static void set_slice(Container& container, index_type from, index_type to,
                        Iter first, Iter last) {
    iterator_type beg = moveToPos(container, from);
    iterator_type end = moveToPos(container, to);
    container.erase(beg, end);
    // FIX: didn't we just invalidate this iterator?
    container.insert(beg, first, last);
  }

  static void delete_item(Container& container, index_type i) {
    iterator_type pos = moveToPos(container, i);
    container.erase(pos);
  }

  static void delete_slice(Container& container, index_type from,
                           index_type to) {
    iterator_type beg = moveToPos(container, from);
    iterator_type end = moveToPos(container, to);
    container.erase(beg, end);
  }

  static size_t size(Container& container) { return container.size(); }

  static bool contains(Container& container, key_type const& key) {
    return std::find(container.begin(), container.end(), key) !=
           container.end();
  }

  static index_type get_min_index(Container& container) {
    (void)container;
    return 0;
  }

  static index_type get_max_index(Container& container) {
    return container.size();
  }

  static bool compare_index(Container& container, index_type a, index_type b) {
    (void)container;
    return a < b;
  }

  static void append(Container& container, data_type const& v) {
    container.push_back(v);
  }
  template <class Iter>
  static void extend(Container& container, Iter first, Iter last) {
    container.insert(container.end(), first, last);
  }

  static index_type convert_index(Container& container, PyObject* i_) {
    extract<long> i(i_);
    if (i.check()) {
      long index = i();
      if (index < 0) index += DerivedPolicies::size(container);
      if (index >= long(container.size()) || index < 0) {
        PyErr_SetString(PyExc_IndexError, "Index out of range");
        throw_error_already_set();
      }
      return index;
    }

    PyErr_SetString(PyExc_TypeError, "Invalid index type");
    throw_error_already_set();
    return index_type();
  }

 private:
#if 0
        static void
        base_append(Container& container, object v)
        {
            extract<data_type&> elem(v);
            // try if elem is an exact Data
            if (elem.check())
            {
                DerivedPolicies::append(container, elem());
            }
            else
            {
                //  try to convert elem to data_type
                extract<data_type> elem(v);
                if (elem.check())
                {
                    DerivedPolicies::append(container, elem());
                }
                else
                {
                    PyErr_SetString(PyExc_TypeError,
                        "Attempting to append an invalid type");
                    throw_error_already_set();
                }
            }
        }

        static void
        base_extend(Container& container, object v)
        {
            std::vector<data_type> temp;
            container_utils::extend_container(temp, v);
            DerivedPolicies::extend(container, temp.begin(), temp.end());
        }
#endif
  static iterator_type moveToPos(Container& container, index_type i) {
    iterator_type pos;
    index_type idx = 0;
    pos = container.begin();
    while (idx < i && pos != container.end()) {
      pos++;
      idx++;
    }
    if (pos == container.end()) {
      PyErr_SetObject(PyExc_IndexError, PyInt_FromLong(i));
      python::throw_error_already_set();
    }
    return pos;
  }
};

}  // namespace python
}  // namespace boost

#endif

ext: hpp

max_stars_repo_name: kazuyaujihara/rdkit

max_issues_repo_name: kazuyaujihara/rdkit

max_forks_repo_name: bp-kelley/rdkit


text:
// Copyright 2008 by BBN Technologies Corp.
// All Rights Reserved.

#include "Generic/common/leak_detection.h"

#include "Generic/common/SessionLogger.h"
#include "Generic/common/Symbol.h"
#include "Generic/common/SymbolConstants.h"
#include "Generic/common/ParamReader.h"
#include "Generic/common/UnexpectedInputException.h"
#include "Generic/edt/discmodel/DTCorefFeatureType.h"
#include "Generic/edt/AbbrevTable.h"
#include "Generic/edt/NameLinkFunctions.h"
#include "Generic/discTagger/DTFeatureType.h"
#include "Generic/discTagger/DTQuintgramFeature.h"
#include "Generic/discTagger/DTState.h"
#include "Generic/edt/discmodel/DTCorefObservation.h"
#include "Generic/edt/DescLinkFeatureFunctions.h"
#include "Generic/theories/Mention.h"
#include "Generic/theories/Entity.h"
#include "Generic/theories/EntitySet.h"

#include "English/edt/discmodel/featuretypes/en_CountryCapitalFT.h"
#include <boost/scoped_ptr.hpp>

int EnglishEn_CountryCapitalFT::extractFeatures(const DTState &state,
							DTFeature **resultArray) const
{
	DTCorefObservation *o = static_cast<DTCorefObservation*>(
		state.getObservation(0));
	const Mention *mention = o->getMention();
	EntityType linkType = mention->getEntityType();
	if(!linkType.matchesGPE())
		return 0;

	const EntitySet *eset = o->getEntitySet();
	Entity *entity = o->getEntity();

	const MentionSymArrayMap *HWMap = o->getHWMentionMapper();
	const MentionSymArrayMap *abbrevMap = o->getAbbrevMentionMapper();
	SymbolArray *mentionAbbrevArray = *abbrevMap->get(mention->getUID());
	SymbolArray *mentionHWArray = *HWMap->get(mention->getUID());

	Symbol mentionRole = SymbolConstants::nullSymbol;
	Symbol entityMentionLevel = o->getEntityMentionLevel();

	int n_feat = 0;
	bool discard = false;

	SymbolArray *country = NULL;
	Symbol countryWords[MAX_NUM_WORDS_IN_MENTION_HEAD];
	Symbol resolvedWords[MAX_NUM_WORDS_IN_MENTION_HEAD];
	Symbol entityCapitalWords[MAX_NUM_WORDS_IN_MENTION_HEAD];

	bool mentCapitalSameAsCountry = false;
	int nCountryWords, nResolvedWords, nEntityCapitalWords;;//, nEntityCountryWords

	country = lookupPhrase(mentionHWArray, capital2country);
	if(country!=NULL){
		nResolvedWords  = AbbrevTable::resolveSymbols(country->getArray(), country->getLength(), resolvedWords, MAX_NUM_WORDS_IN_MENTION_HEAD);
		nCountryWords = NameLinkFunctions::getLexicalItems(resolvedWords, nResolvedWords, countryWords, MAX_NUM_WORDS_IN_MENTION_HEAD);

		//// check whether the capital and the country are the same
		if(nCountryWords == mentionHWArray->getLength())	{
			bool is_equal = true;
			const Symbol *hws = mentionHWArray->getArray();
			for(int i=0;i<country->getLength(); i++)
				if(countryWords[i] != hws[i]) is_equal=false;
			mentCapitalSameAsCountry = is_equal;
		}

		if (mention->hasRoleType()) // set in the metonymy model
		{
			mentionRole = mention->getRoleType().getName();
		}
	}

	// if the mention is a capital and the capital name is the same as the country name don't fire.
	if(mentCapitalSameAsCountry)
		return 0;

	bool city2countryFound = false;
	bool country2cityFound = false;
	for (int m = 0; m < entity->getNMentions(); m++) {

		if(n_feat>MAX_FEATURES_PER_EXTRACTION-10)
			return n_feat;

		Mention* entMent = o->getEntitySet()->getMention(entity->getMention(m));
		// the mention is a capital city that can be mapped to a country
		if (country!=NULL){ // the mention is a capital city
			SymbolArray *entityAbbrevArray = *abbrevMap->get(entity->getMention(m));
			////check equality
			if (nCountryWords == entityAbbrevArray->getLength()){
				const Symbol * entitySyms = entityAbbrevArray->getArray();
				bool is_equal = true;
				for(int i=0;i<country->getLength(); i++)
					if(countryWords[i] != entitySyms[i]) is_equal=false;
				if(is_equal) {
					city2countryFound = true;
					if (n_feat >= (DTFeatureType::MAX_FEATURES_PER_EXTRACTION-2)){
								discard = true;
								break;
					}
					//std::cerr<<"Debug EnglishEn_CountryCapitalFT slotA adding nfeat "<<n_feat<<"\n";
					resultArray[n_feat++] = _new DTQuintgramFeature(this, state.getTag(), PER_MENTION, CITI_TO_COUNTRY, mentionRole, entityMentionLevel);
				}
			}
		}else { // The mention is not a capital city but may be a country.
				// Check whether there is a match when we map the entity as a capital to a country and compare
			SymbolArray *entityHWArray = *HWMap->get(entity->getMention(m));
			SymbolArray *entityCapital = lookupPhrase(entityHWArray, country2capital);
			if (entityCapital!=NULL && !mentCapitalSameAsCountry){
				nResolvedWords  = AbbrevTable::resolveSymbols(entityCapital->getArray(), entityCapital->getLength(), resolvedWords, MAX_NUM_WORDS_IN_MENTION_HEAD);
				nEntityCapitalWords = NameLinkFunctions::getLexicalItems(resolvedWords, nResolvedWords, entityCapitalWords, MAX_NUM_WORDS_IN_MENTION_HEAD);
				////check equality
				if (mentionAbbrevArray->getLength() == nEntityCapitalWords){
					const Symbol *mentWords = mentionAbbrevArray->getArray();
					bool is_equal = true;
					for(int i=0;i<entityCapital->getLength(); i++)
						if(mentWords[i] != entityCapitalWords[i]) is_equal=false;
					if(is_equal) {
						Symbol entityCapitalRole = (mention->hasRoleType()) ? mention->getRoleType().getName() : SymbolConstants::nullSymbol;
						country2cityFound = true;
						if (n_feat >= (DTFeatureType::MAX_FEATURES_PER_EXTRACTION-2)){
								discard = true;
								break;
						}

						//std::cerr<<"Debug EnglishEn_CountryCapitalFT slotB adding nfeat "<<n_feat<<"\n";

						resultArray[n_feat++] = _new DTQuintgramFeature(this, state.getTag(), PER_MENTION, COUNTRY_TO_CITI, entityCapitalRole, entityMentionLevel);
					}
				}
			}
		}//else
	}//for

	if (city2countryFound){
		if (n_feat >= DTFeatureType::MAX_FEATURES_PER_EXTRACTION){
			discard = true;
		}else{
			resultArray[n_feat++] = _new DTQuintgramFeature(this, state.getTag(), PER_ENTITY, CITI_TO_COUNTRY, mentionRole, entityMentionLevel);
		}
	}
	if (country2cityFound){
		if (n_feat >= DTFeatureType::MAX_FEATURES_PER_EXTRACTION){
			discard = true;
		}else{
			resultArray[n_feat++] = _new DTQuintgramFeature(this, state.getTag(), PER_ENTITY, COUNTRY_TO_CITI, SymbolConstants::nullSymbol, entityMentionLevel);
		}
	}
	if (discard) {
		SessionLogger::warn("DT_feature_limit") <<"EnglishEn_CountryCapitalFT discarding features beyond MAX_FEATURES_PER_EXTRACTION\n";
		//std::cerr<<"Debug EnglishEn_CountryCapitalFT discarded some kept nfeat "<<n_feat<<"\n";
	}
	//if (n_feat > 0) std::cerr<<"Debug EnglishEn_CountryCapitalFT reached nfeat "<<n_feat<<"\n";
	return n_feat;
}

UTF8Token EnglishEn_CountryCapitalFT::FileReader::getToken(int specifier) {
	UTF8Token token;
	if(_nTokenCache > 0)
		token = _tokenCache[--_nTokenCache];
	else _file >> token;

	if(token.symValue() == SymbolConstants::leftParen) {
		if(!(specifier & LPAREN))
			throw UnexpectedInputException("EnglishEn_CountryCapitalFT::FileReader::getToken()", "\'(\' not expected in edt_capital_to_country_file.");
	}
	else if(token.symValue() == SymbolConstants::rightParen) {
		if(!(specifier & RPAREN))
			throw UnexpectedInputException("EnglishEn_CountryCapitalFT::FileReader::getToken()", "\')\' not expected in edt_capital_to_country_file.");
	}
	else if(token.symValue() == Symbol(L"")) {
		if(!(specifier & EOFTOKEN))
			throw UnexpectedInputException("EnglishEn_CountryCapitalFT::FileReader::getToken()", "EOF not expected in edt_capital_to_country_file.");
	}
	else if(!(specifier & WORD))
		throw UnexpectedInputException("EnglishEn_CountryCapitalFT::FileReader::getToken()", "Word not expected in edt_capital_to_country_file.");

	return token;
}

bool EnglishEn_CountryCapitalFT::FileReader::hasMoreTokens()
{
	if(_nTokenCache > 0) return true;
	UTF8Token token;
	_file >> token;
	if(token.symValue() == Symbol(L""))
		return false;
	else {
		if(_nTokenCache < MAX_CACHE)
			_tokenCache[_nTokenCache++] = token;
		return true;
	}
}

int EnglishEn_CountryCapitalFT::FileReader::getSymbolArray(Symbol results[], int max_results) throw(UnexpectedInputException)
{
	UTF8Token token = getToken(LPAREN | WORD);
	int nResults = 0;
	if(token.symValue() == SymbolConstants::leftParen)
		while((token = getToken(RPAREN | WORD)).symValue() != SymbolConstants::rightParen && nResults<max_results)
			results[nResults++] = token.symValue();
	else results[nResults++] = token.symValue();
	return nResults;
}

int EnglishEn_CountryCapitalFT::FileReader::getOptionalSymbolArray(Symbol results[], int max_results) throw(UnexpectedInputException)
{
	UTF8Token token = getToken(LPAREN | WORD | RPAREN);
	int nResults = 0;
	if(token.symValue() == SymbolConstants::rightParen)
		pushBack(token);
	else if(token.symValue() == SymbolConstants::leftParen)
		while((token = getToken(RPAREN | WORD)).symValue() != SymbolConstants::rightParen && nResults<max_results)
			results[nResults++] = token.symValue();
	else results[nResults++] = token.symValue();
	return nResults;
}

void EnglishEn_CountryCapitalFT::FileReader::pushBack(UTF8Token token) {
	if(_nTokenCache < MAX_CACHE)
		_tokenCache[_nTokenCache++] = token;
}

/** while countries we abbreviate and put into the table, capital cities we do not abbreviate
* the reason is that 'washington d.c.' or 'wash.' are usually not metonymic.
* in order to add abbreviation for a capital to country map you can add an entity to the file.
* While this will add an entry for the map from capitals to countries, the country to capital map
* will only hold the last entered value.
*/
void EnglishEn_CountryCapitalFT::initializeTable() {
	std::string filename = ParamReader::getRequiredParam("edt_capital_to_country_file");
	boost::scoped_ptr<UTF8InputStream> instream_scoped_ptr(UTF8InputStream::build());
	UTF8InputStream& instream(*instream_scoped_ptr);
	instream.open(filename.c_str());
	FileReader reader(instream);

	//initialize hash maps
	_table[country2capital] = _new SymbolArraySymbolArrayMap(300);
	_table[capital2country] = _new SymbolArraySymbolArrayMap(300);

	const int MAX_ARRAY_SYMBOLS = 16;
	Symbol array[MAX_ARRAY_SYMBOLS], keyArray[MAX_ARRAY_SYMBOLS], valueArray[MAX_ARRAY_SYMBOLS];
	Symbol resolvedWords[MAX_ARRAY_SYMBOLS];
	int nResolvedWords, nArray = 0, nKeyArray = 0, nValueArray = 0;

	_debugOut.init(Symbol(L"country_capital_debug_file"));
	while(reader.hasMoreTokens()) {
		reader.getLeftParen();

		nArray = reader.getSymbolArray(array, MAX_ARRAY_SYMBOLS);
		nResolvedWords  = AbbrevTable::resolveSymbols(array, nArray, resolvedWords, MAX_ARRAY_SYMBOLS);
		int nValueArray = NameLinkFunctions::getLexicalItems(resolvedWords, nResolvedWords, valueArray, MAX_ARRAY_SYMBOLS);

		_debugOut << "found VALUE: ";
		for(int i=0; i<nValueArray; i++)
			_debugOut << valueArray[i].to_debug_string() << " ";
		_debugOut << "\n";

		nArray = reader.getSymbolArray(array, MAX_SYMBOLS);

		_debugOut << "      KEY: ";
		for(int j=0; j<nArray; j++)
			_debugOut << array[j].to_debug_string() << " ";
		_debugOut << "\n";

		add(array, nArray, valueArray, nValueArray, capital2country);
		add(valueArray, nValueArray, keyArray, nKeyArray, country2capital);

		while((nArray = reader.getOptionalSymbolArray(array, MAX_SYMBOLS))!= 0) {
			_debugOut << "      KEY: ";
			for(int i=0; i<nArray; i++)
				_debugOut << array[i].to_debug_string() << " ";
			_debugOut << "\n";

			add(array, nArray, valueArray, nValueArray, capital2country);
			add(valueArray, nValueArray, keyArray, nKeyArray, country2capital);
		}
		reader.getRightParen();
	}
}

void EnglishEn_CountryCapitalFT::add(Symbol *key, size_t nKey, Symbol *value, size_t nValue, size_t table_index) {

	SymbolArray *keyArray = _new SymbolArray(key, nKey);
	SymbolArray *valueArray = _new SymbolArray(value, nValue);

	if((*_table[table_index]).get(keyArray) == NULL) {
		(*_table[table_index])[keyArray] = valueArray;
	}
	else {
		SymbolArray *oldValue = *(*_table[table_index]).get(keyArray);
		(*_table[table_index])[keyArray] = valueArray;
		delete oldValue;
		delete keyArray;
	}
}

SymbolArray* EnglishEn_CountryCapitalFT::lookupPhrase(SymbolArray *key, size_t index) const{
	SymbolArray **value = (*_table[index]).get(key);
	if(value != NULL) {
		return *value;
	}
	return NULL;
}

max_stars_repo_name: BBN-E/serif

max_issues_repo_name: BBN-E/serif

max_forks_repo_name: BBN-E/serif


text:
/** @file NormalDist.cpp
 * @author Mark J. Olah (mjo\@cs.unm DOT edu)
 * @date 2017-2019
 * @brief NormalDist class definition
 *
 */
#include "PriorHessian/NormalDist.h"
#include "PriorHessian/PriorHessianError.h"

#include <sstream>
#include <cmath>
#include <limits>

#include <boost/math/special_functions/erf.hpp>
#include <boost/math/constants/constants.hpp>

namespace prior_hessian {

/* Static member variables */
const StringVecT NormalDist::_param_names = { "mu", "sigma" };
const NormalDist::NparamsVecT NormalDist::_param_lbound = {-INFINITY, 0}; //Lower bound on valid parameter values
const NormalDist::NparamsVecT NormalDist::_param_ubound = {INFINITY, INFINITY}; //Upper bound on valid parameter values

/* Constructors */
NormalDist::NormalDist(double mu, double sigma)
    : UnivariateDist()
{
    set_params(mu,sigma);
}

/* Non-static member functions */

void NormalDist::set_sigma(double val)
{
    _sigma = checked_sigma(val);
    _sigma_inv = 1./_sigma;
    llh_const_initialized = false;
}

double NormalDist::cdf(double x) const
{
    return .5*(1 + boost::math::erf((x - _mu)*_sigma_inv*constants::sqrt2_inv));
}

double NormalDist::icdf(double u) const
{
    return mu() + sigma()*constants::sqrt2*boost::math::erf_inv(2*u-1);
}

double NormalDist::checked_mu(double val)
{
    if(!std::isfinite(val)) {
        std::ostringstream msg;
        msg<<"NormalDist: got bad mu value:"<<val;
        throw ParameterValueError(msg.str());
    }
    return val;
}

double NormalDist::checked_sigma(double val)
{
    if(val<=0 || !std::isfinite(val)) {
        std::ostringstream msg;
        msg<<"NormalDist: got bad sigma value:"<<val;
        throw ParameterValueError(msg.str());
    }
    return val;
}

double NormalDist::llh(double x) const
{
    if(!llh_const_initialized) initialize_llh_const(); //Lazy computation of llh_const.
    return rllh(x) + llh_const;
}

void NormalDist::initialize_llh_const() const
{
    llh_const = compute_llh_const(sigma());
    llh_const_initialized = true;
}

double NormalDist::compute_llh_const(double sigma)
{
    return -log(sigma) - .5*constants::log2pi;
}

} /* namespace prior_hessian */

max_stars_repo_name: markjolah/PriorHessianLib

max_issues_repo_name: markjolah/PriorHessianLib

max_forks_repo_name: markjolah/PriorHessianLib


text:
#ifndef CUKE_CUKECOMMANDS_HPP_
#define CUKE_CUKECOMMANDS_HPP_

#include "ContextManager.hpp"
#include "Scenario.hpp"
#include "Table.hpp"
#include "step/StepManager.hpp"
#include "hook/HookRegistrar.hpp"

#include <map>
#include <string>
#include <sstream>

#include <boost/shared_ptr.hpp>

namespace cucumber {
namespace internal {

using boost::shared_ptr;

/**
 * Legacy class to be removed when feature #31 is complete, substituted by CukeEngineImpl.
 */
class CukeCommands {
public:
	CukeCommands();
	virtual ~CukeCommands();

    void beginScenario(const TagExpression::tag_list *tags);
    void endScenario();
    const std::string snippetText(const std::string stepKeyword, const std::string stepName) const;
    MatchResult stepMatches(const std::string description) const;
    InvokeResult invoke(step_id_type id, const InvokeArgs * pArgs);

private:
    StepManager stepManager;
    HookRegistrar hookRegistrar;
    ContextManager contextManager;
    bool hasStarted;

private:
    static shared_ptr<Scenario> currentScenario;
};

}
}

#endif /* CUKE_CUKECOMMANDS_HPP_ */

max_stars_repo_name: AndreasAugustin/Gherkin-Demos-cpp

max_issues_repo_name: AndreasAugustin/Gherkin-Demos-cpp

max_forks_repo_name: AndreasAugustin/Gherkin-Demos-cpp


text:
//
//  Copyright (c) 2021 Kannav Mehta
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef LA_MATRIX_TRAIT_H
#define LA_MATRIX_TRAIT_H

#include <boost/numeric/ublas/matrix/engine_traits.hpp>

namespace boost::numeric::ublas::experimental {
template<typename T>
class matrix;
} // namespace boost::numeric::ublas::experimental

namespace boost::numeric::ublas::experimental::detail {

/// @brief checks if type is a matrix
template<typename T>
struct is_matrix : std::false_type {
};

template<typename T>
struct is_matrix<matrix<T>> : std::true_type {
};

template<typename T>
using is_matrix_t = typename is_matrix<std::decay_t<T>>::type;

template<typename T>
inline constexpr bool is_matrix_v = is_matrix_t<T>::value;

template<typename T>
struct is_matrix<T &> : is_matrix<T> {
};

/// @brief used for type space reduction in expression templates
template<typename T>
using enable_if_matrix = std::enable_if_t<is_matrix_v<T>>;

} // namespace boost::numeric::ublas::experimental::detail

namespace boost::numeric::ublas::experimental {

template <typename E>
struct matrix_traits;

template<typename E>
struct matrix_traits<matrix<E>>;

template<typename E, detail::enable_if_dynamic<E>>
struct matrix_traits<matrix<E>> {

};
}

#endif //LA_MATRIX_TRAIT_H

max_stars_repo_name: abakfja/linear-algebra

max_issues_repo_name: abakfja/linear-algebra

max_forks_repo_name: abakfja/linear-algebra


text:
/*!
 * @file
 * Forward declares the @ref Orderable typeclass.
 *
 *
 * @copyright Louis Dionne 2014
 * Distributed under the Boost Software License, Version 1.0.
 *         (See accompanying file LICENSE.md or copy at
 *             http://www.boost.org/LICENSE_1_0.txt)
 */

#ifndef BOOST_MPL11_FWD_ORDERABLE_HPP
#define BOOST_MPL11_FWD_ORDERABLE_HPP

#include <boost/mpl11/fwd/bool.hpp>

namespace boost { namespace mpl11 {
    /*!
     * @ingroup typeclasses
     * @defgroup Orderable Orderable
     *
     * The `Orderable` typeclass is used for totally ordered datatypes.
     *
     *
     * ### Methods
     * `less`, `less_equal`, `greater`, `greater_equal`, `min` and `max`.
     *
     * The `less`, `less_equal`, `greater` and `greater_equal` methods can
     * all be invoked with more than two arguments, in which case the method's
     * behavior conforms to the usual mathematical usage. Specifically,
     * `less<x1, x2, xs...>` is equivalent to
     * `and_<less<x1, x2>, less<x2, xs...>>`.
     *
     * The `min` and `max` methods can also be invoked with more than two
     * arguments. Specifically, `min<x1, x2, xs...>` is equivalent to
     * `min<min<x1, x2>, xs...>`.
     *
     *
     * ### Minimal complete definition
     * `less`
     *
     * @{
     */
    template <typename Left, typename Right = Left, typename = true_>
    struct Orderable;

    /*!
     * Returns whether the given objects are ordered according to the `<`
     * ordering.
     */
    template <typename x1, typename x2, typename ...xs>
    struct less;

    /*!
     * Returns whether the given objects are ordered according to the `<=`
     * ordering.
     */
    template <typename x1, typename x2, typename ...xs>
    struct less_equal;

    /*!
     * Returns whether the given objects are ordered according to the `>`
     * ordering.
     */
    template <typename x1, typename x2, typename ...xs>
    struct greater;

    /*!
     * Returns whether the given objects are ordered according to the `>=`
     * ordering.
     */
    template <typename x1, typename x2, typename ...xs>
    struct greater_equal;

    /*!
     * Returns the smallest of its arguments according to the ordering
     * induced by `less`.
     */
    template <typename x1, typename x2, typename ...xs>
    struct min;

    /*!
     * Returns the largest of its arguments according to the ordering
     * induced by `less`.
     */
    template <typename x1, typename x2, typename ...xs>
    struct max;
    //! @}
}} // end namespace boost::mpl11

#endif // !BOOST_MPL11_FWD_ORDERABLE_HPP