<commit_before>///////////////////////////////////////////////////////////
//  ObjectMatrix.cpp
//  Implementation of the Class ObjectMatrix
//  Created on:      07-Lie-2013 20:07:30
//  Original author: Povilas
///////////////////////////////////////////////////////////

/*! \class ObjectMatrix
    \brief A class of methods and attributes for manipulating matrices.
 */
#include "ObjectMatrix.h"
#include "AdditionalMethods.h"
#include "ARFF.h"
#include "DataObject.h"
#include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
//#include <set>

//double ObjectMatrix::_weight = 0;

ObjectMatrix::ObjectMatrix()
{
    DataObjects.reserve(0);   //1
    objectCount = 0;
    isClassPresent = false;
}

ObjectMatrix::~ObjectMatrix()
{
}

ObjectMatrix::ObjectMatrix(std::string file)
{
    fileName.assign(file);
    objectCount = 0;
    isClassPresent = false;
}

ObjectMatrix::ObjectMatrix(int cnt)
{
    DataObjects.reserve(cnt);
    objectCount = 0;
	isClassPresent = false;
}

ObjectMatrix::ObjectMatrix(int n, int m)
{
    DataObjects.reserve(n);
    for (int i = 0; i < n; i++)
        DataObjects[i].setNumOfFeatures(m);
    objectCount = 0;
isClassPresent = false;
}

ObjectMatrix::ObjectMatrix(int m, int n, int k)
{
    std::vector<double> initialFeatures;
    initialFeatures.resize(k, 1.0);
    DataObject initial(initialFeatures);
    DataObjects2D.resize(m, std::vector<DataObject>(n, initial));
    objectCount = 0;
isClassPresent = false;
}

void ObjectMatrix::addObject(DataObject object)
{
    DataObjects.push_back(object);
    objectCount++;
}

void ObjectMatrix::addObject(DataObject object, int cls)
{
    DataObjects.push_back(object);
    objectCount++;
    this->DataObjects.at(objectCount -1).setClassLabel(cls);
}

void ObjectMatrix::addObjectTo(int index, DataObject object)
{
    DataObjects2D[index].push_back(object);
    objectCount++;
}

void ObjectMatrix::updateDataObject(int objectIndex, int featureIndex, double newValue)
{
    this->DataObjects.at(objectIndex).updateFeature(featureIndex, newValue);
}

void ObjectMatrix::updateDataObjectClass(int objectIndex, int newClass)
{
    this->DataObjects.at(objectIndex).setClassLabel(newClass);
  //  this->isClassPresent = true;
}

bool ObjectMatrix::getPrintClass()
{
    return this->isClassPresent;
}

void ObjectMatrix::setPrintClass(std::vector <std::string> printClass)
{
    this->attributeStringClasses = printClass;
    this->isClassPresent = true;
}

void ObjectMatrix::updateDataObject(int rowIndex, int colIndex, int featureIndex, double newValue)
{
    this->DataObjects2D[rowIndex].at(colIndex).updateFeature(featureIndex, newValue);
}

DataObject ObjectMatrix::getObjectAt(int index)
{
    return  DataObjects.at(index);
}

DataObject ObjectMatrix::getObjectAt(int row_index, int col_index)
{
    return  DataObjects2D[row_index].at(col_index);
}

int ObjectMatrix::getObjectCount()
{
    return objectCount;
}

std::vector<std::string> ObjectMatrix::getFeaturesTitle()
{
    return featureTitles;
}

int ObjectMatrix::getClassCount()
{
    int cCount = 0;
    if (!attributeStringClasses.empty())
        cCount = attributeStringClasses.size();
    return cCount;
}

void ObjectMatrix::addAtributes(std::vector<std::string> additionalAttributes)
{
    this->additionalAttributes = additionalAttributes;
}

bool ObjectMatrix::getIsClassPresent()
{
    return this->isClassPresent;
}

void ObjectMatrix::setIsClassPresent()
{
    this->isClassPresent = true;
}

std::vector <std::string> ObjectMatrix::getStringClassAttributes()
{
    return this->attributeStringClasses;
}

void ObjectMatrix::loadDataMatrix()
{
    const char* path = fileName.c_str();
    ARFF file(path);

    ObjectMatrix::_weight = 0;

    if (file.isSuccessfullyRead() == true)   // successful read
    {
        std::vector< std::vector<double>> data = file.getData();
        //featureTitles = file.getAttributes();
        if (file.isClassFound())
           {
                objClasses = file.getIntClass();
                attributeStringClasses = file.getAttributeStringClasses();
                //isClassPresent = true;
               // std::cout << attributeStringClasses.size();
           }

        std::vector< std::vector<double>> ::iterator dataObjectIterator;

        std::vector<double>::iterator featureIterator;
        std::vector<double> dataObjectItems;
        int indx = 0;
        for(dataObjectIterator = data.begin(); dataObjectIterator!=data.end(); ++dataObjectIterator)
        {
            for(featureIterator = (*dataObjectIterator).begin(); featureIterator!=(*dataObjectIterator).end(); ++featureIterator)
                dataObjectItems.push_back(*featureIterator);

            if (file.isClassFound())
                {
                    DataObject tmp(dataObjectItems, file.getObjectClass(indx));
                    DataObjects.push_back(tmp);
                 //   std::cout << DataObjects.at(indx).getClassLabel() <<std::endl;
                }
            else
                {
                    DataObject tmp(dataObjectItems);
                    DataObjects.push_back(tmp);
                    //   std::cout << DataObjects.at(indx).getClassLabel() <<std::endl;
                }
            dataObjectItems.clear();
            indx++;
        }
        objectCount = DataObjects.size();

        //Calculate matrix X distances between vectors

        //ObjectMatrix::_weight = 0;
        int i, j, z;
        int n =  data.at(0).size(); //features
        double diff, sRow, tmpVal;

      //  FILE *distFile;
        AdditionalMethods::distFile = fopen(AdditionalMethods::tempFileSavePath.c_str(), "wb");

        for (i = 0; i < objectCount - 1; i++)
        {
            for (j = i + 1; j < objectCount; j++)
            {
                sRow = 0.0;
                for (z = 0; z < n; z++)
                {
                    diff  = data.at(i).at(z) - data.at(j).at(z);
                    sRow += diff * diff;
                }
                tmpVal = std::sqrt(sRow);
                fwrite(&tmpVal, sizeof(double), 1, AdditionalMethods::distFile);
                ObjectMatrix::_weight += sRow;
            }
        }
       // fclose(distFile);
        // end distance matrix calculation
    }
    else
    {
        //_weight = 0;
        objectCount = 0;
    }
}
void ObjectMatrix::addComment(std::string comnt)
{
    if (this->comments.empty())
        comments.reserve(0);
    comments.push_back(comnt);
}

void ObjectMatrix::saveDataMatrix(const char* path)
{
    ARFF file;
  //  std::vector <std::string> classLabels; classLabels.reserve(0);
   // std::vector <std::string> comment
    //std::vector <int> tmp; // assume v has the elements
    /*if (getPrintClass())
    {
       /*for (int i = 0; i < DataObjects.size(); i++) //pass only unique class labels that will be printed in @Attribute Class section
        {
            bool notFound = true;
            int tmpLabel = DataObjects.at(i).getClassLabel();
            for (int j = 0; j < classLabels.size(); j++)
                if (tmpLabel == classLabels.at(j))
                {
                    notFound = false;
                    break;
                }
            if (notFound)*/
          //  for (int j = 0; j < classLabels.size(); j++)
              //  classLabels.push_back(tmpLabel);
              //  classLabels = attributeStringClasses;
        //}
       // classLabels = attributeClasses;

    std::vector <std::string> attributeLabels; attributeLabels.reserve(0);

    if (!additionalAttributes.empty())
    {
        for (int i = 0; i < getObjectAt(0).getFeatureCount() - additionalAttributes.size(); i++)
            attributeLabels.push_back("atr_" + std::to_string(static_cast<long long>(i+1)) + " REAL");

        for (int i = 0; i < additionalAttributes.size(); i++)
            {
            attributeLabels.push_back(additionalAttributes.at(i) + " REAL");
           // std::cout << attributeStringClasses.at(i);
            }
    }
    else
    {
        for (int i = 0; i < getObjectAt(0).getFeatureCount(); i++)
            attributeLabels.push_back("atr_" + std::to_string(static_cast<long long>(i+1)) + " REAL");
    }
    file.writeData(path, this->comments, this->DataObjects, attributeStringClasses, attributeLabels);
}

void ObjectMatrix::clearDataObjects()
{
    DataObjects.clear();
    objectCount = 0;
}
/*void ObjectMatrix::assignStringClassesAttribute(ObjectMatrix X)
{
    this->attributeStringClasses = X.attributeStringClasses;
}*/

/*double ObjectMatrix::getDistanceBetween(int i, int j)
{
   FILE *distFile;
   distFile = fopen("D:\\dist.txt", "rb");
//   std::ifstream distFile;
//    distFile.open("D:\\dist.txt", std::ios::binary);
   double retVal;
   int noOfBytes = sizeof(double);

   fseek(distFile, (noOfBytes * i) + noOfBytes * (j - i - 1), SEEK_SET);
 //  fseek(distFile, 1, SEEK_SET);
   fread(&retVal, noOfBytes, 1, distFile);
 //  distFile>>retVal;

  // std::cout << retVal << std::endl;
   fclose(distFile);
// distFile.close();
   return retVal;
   //return *(*(distances + i) + j - i - 1);
}*/

double ObjectMatrix::getWeight()
{
    return ObjectMatrix::_weight;
}
<commit_msg>Uknown attributes names changed from attr_ to attr<commit_after>///////////////////////////////////////////////////////////
//  ObjectMatrix.cpp
//  Implementation of the Class ObjectMatrix
//  Created on:      07-Lie-2013 20:07:30
//  Original author: Povilas
///////////////////////////////////////////////////////////

/*! \class ObjectMatrix
    \brief A class of methods and attributes for manipulating matrices.
 */
#include "ObjectMatrix.h"
#include "AdditionalMethods.h"
#include "ARFF.h"
#include "DataObject.h"
#include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
//#include <set>

//double ObjectMatrix::_weight = 0;

ObjectMatrix::ObjectMatrix()
{
    DataObjects.reserve(0);   //1
    objectCount = 0;
    isClassPresent = false;
}

ObjectMatrix::~ObjectMatrix()
{
}

ObjectMatrix::ObjectMatrix(std::string file)
{
    fileName.assign(file);
    objectCount = 0;
    isClassPresent = false;
}

ObjectMatrix::ObjectMatrix(int cnt)
{
    DataObjects.reserve(cnt);
    objectCount = 0;
	isClassPresent = false;
}

ObjectMatrix::ObjectMatrix(int n, int m)
{
    DataObjects.reserve(n);
    for (int i = 0; i < n; i++)
        DataObjects[i].setNumOfFeatures(m);
    objectCount = 0;
isClassPresent = false;
}

ObjectMatrix::ObjectMatrix(int m, int n, int k)
{
    std::vector<double> initialFeatures;
    initialFeatures.resize(k, 1.0);
    DataObject initial(initialFeatures);
    DataObjects2D.resize(m, std::vector<DataObject>(n, initial));
    objectCount = 0;
isClassPresent = false;
}

void ObjectMatrix::addObject(DataObject object)
{
    DataObjects.push_back(object);
    objectCount++;
}

void ObjectMatrix::addObject(DataObject object, int cls)
{
    DataObjects.push_back(object);
    objectCount++;
    this->DataObjects.at(objectCount -1).setClassLabel(cls);
}

void ObjectMatrix::addObjectTo(int index, DataObject object)
{
    DataObjects2D[index].push_back(object);
    objectCount++;
}

void ObjectMatrix::updateDataObject(int objectIndex, int featureIndex, double newValue)
{
    this->DataObjects.at(objectIndex).updateFeature(featureIndex, newValue);
}

void ObjectMatrix::updateDataObjectClass(int objectIndex, int newClass)
{
    this->DataObjects.at(objectIndex).setClassLabel(newClass);
  //  this->isClassPresent = true;
}

bool ObjectMatrix::getPrintClass()
{
    return this->isClassPresent;
}

void ObjectMatrix::setPrintClass(std::vector <std::string> printClass)
{
    this->attributeStringClasses = printClass;
    this->isClassPresent = true;
}

void ObjectMatrix::updateDataObject(int rowIndex, int colIndex, int featureIndex, double newValue)
{
    this->DataObjects2D[rowIndex].at(colIndex).updateFeature(featureIndex, newValue);
}

DataObject ObjectMatrix::getObjectAt(int index)
{
    return  DataObjects.at(index);
}

DataObject ObjectMatrix::getObjectAt(int row_index, int col_index)
{
    return  DataObjects2D[row_index].at(col_index);
}

int ObjectMatrix::getObjectCount()
{
    return objectCount;
}

std::vector<std::string> ObjectMatrix::getFeaturesTitle()
{
    return featureTitles;
}

int ObjectMatrix::getClassCount()
{
    int cCount = 0;
    if (!attributeStringClasses.empty())
        cCount = attributeStringClasses.size();
    return cCount;
}

void ObjectMatrix::addAtributes(std::vector<std::string> additionalAttributes)
{
    this->additionalAttributes = additionalAttributes;
}

bool ObjectMatrix::getIsClassPresent()
{
    return this->isClassPresent;
}

void ObjectMatrix::setIsClassPresent()
{
    this->isClassPresent = true;
}

std::vector <std::string> ObjectMatrix::getStringClassAttributes()
{
    return this->attributeStringClasses;
}

void ObjectMatrix::loadDataMatrix()
{
    const char* path = fileName.c_str();
    ARFF file(path);

    ObjectMatrix::_weight = 0;

    if (file.isSuccessfullyRead() == true)   // successful read
    {
        std::vector< std::vector<double>> data = file.getData();
        //featureTitles = file.getAttributes();
        if (file.isClassFound())
           {
                objClasses = file.getIntClass();
                attributeStringClasses = file.getAttributeStringClasses();
                //isClassPresent = true;
               // std::cout << attributeStringClasses.size();
           }

        std::vector< std::vector<double>> ::iterator dataObjectIterator;

        std::vector<double>::iterator featureIterator;
        std::vector<double> dataObjectItems;
        int indx = 0;
        for(dataObjectIterator = data.begin(); dataObjectIterator!=data.end(); ++dataObjectIterator)
        {
            for(featureIterator = (*dataObjectIterator).begin(); featureIterator!=(*dataObjectIterator).end(); ++featureIterator)
                dataObjectItems.push_back(*featureIterator);

            if (file.isClassFound())
                {
                    DataObject tmp(dataObjectItems, file.getObjectClass(indx));
                    DataObjects.push_back(tmp);
                 //   std::cout << DataObjects.at(indx).getClassLabel() <<std::endl;
                }
            else
                {
                    DataObject tmp(dataObjectItems);
                    DataObjects.push_back(tmp);
                    //   std::cout << DataObjects.at(indx).getClassLabel() <<std::endl;
                }
            dataObjectItems.clear();
            indx++;
        }
        objectCount = DataObjects.size();

        //Calculate matrix X distances between vectors

        //ObjectMatrix::_weight = 0;
        int i, j, z;
        int n =  data.at(0).size(); //features
        double diff, sRow, tmpVal;

      //  FILE *distFile;
        AdditionalMethods::distFile = fopen(AdditionalMethods::tempFileSavePath.c_str(), "wb");

        for (i = 0; i < objectCount - 1; i++)
        {
            for (j = i + 1; j < objectCount; j++)
            {
                sRow = 0.0;
                for (z = 0; z < n; z++)
                {
                    diff  = data.at(i).at(z) - data.at(j).at(z);
                    sRow += diff * diff;
                }
                tmpVal = std::sqrt(sRow);
                fwrite(&tmpVal, sizeof(double), 1, AdditionalMethods::distFile);
                ObjectMatrix::_weight += sRow;
            }
        }
       // fclose(distFile);
        // end distance matrix calculation
    }
    else
    {
        //_weight = 0;
        objectCount = 0;
    }
}
void ObjectMatrix::addComment(std::string comnt)
{
    if (this->comments.empty())
        comments.reserve(0);
    comments.push_back(comnt);
}

void ObjectMatrix::saveDataMatrix(const char* path)
{
    ARFF file;
  //  std::vector <std::string> classLabels; classLabels.reserve(0);
   // std::vector <std::string> comment
    //std::vector <int> tmp; // assume v has the elements
    /*if (getPrintClass())
    {
       /*for (int i = 0; i < DataObjects.size(); i++) //pass only unique class labels that will be printed in @Attribute Class section
        {
            bool notFound = true;
            int tmpLabel = DataObjects.at(i).getClassLabel();
            for (int j = 0; j < classLabels.size(); j++)
                if (tmpLabel == classLabels.at(j))
                {
                    notFound = false;
                    break;
                }
            if (notFound)*/
          //  for (int j = 0; j < classLabels.size(); j++)
              //  classLabels.push_back(tmpLabel);
              //  classLabels = attributeStringClasses;
        //}
       // classLabels = attributeClasses;

    std::vector <std::string> attributeLabels; attributeLabels.reserve(0);

    if (!additionalAttributes.empty())
    {
        for (int i = 0; i < getObjectAt(0).getFeatureCount() - additionalAttributes.size(); i++)
            attributeLabels.push_back("atr" + std::to_string(static_cast<long long>(i+1)) + " REAL");

        for (int i = 0; i < additionalAttributes.size(); i++)
            {
            attributeLabels.push_back(additionalAttributes.at(i) + " REAL");
           // std::cout << attributeStringClasses.at(i);
            }
    }
    else
    {
        for (int i = 0; i < getObjectAt(0).getFeatureCount(); i++)
            attributeLabels.push_back("atr" + std::to_string(static_cast<long long>(i+1)) + " REAL");
    }
    file.writeData(path, this->comments, this->DataObjects, attributeStringClasses, attributeLabels);
}

void ObjectMatrix::clearDataObjects()
{
    DataObjects.clear();
    objectCount = 0;
}
/*void ObjectMatrix::assignStringClassesAttribute(ObjectMatrix X)
{
    this->attributeStringClasses = X.attributeStringClasses;
}*/

/*double ObjectMatrix::getDistanceBetween(int i, int j)
{
   FILE *distFile;
   distFile = fopen("D:\\dist.txt", "rb");
//   std::ifstream distFile;
//    distFile.open("D:\\dist.txt", std::ios::binary);
   double retVal;
   int noOfBytes = sizeof(double);

   fseek(distFile, (noOfBytes * i) + noOfBytes * (j - i - 1), SEEK_SET);
 //  fseek(distFile, 1, SEEK_SET);
   fread(&retVal, noOfBytes, 1, distFile);
 //  distFile>>retVal;

  // std::cout << retVal << std::endl;
   fclose(distFile);
// distFile.close();
   return retVal;
   //return *(*(distances + i) + j - i - 1);
}*/

double ObjectMatrix::getWeight()
{
    return ObjectMatrix::_weight;
}
<|endoftext|>


<commit_before>/*
 *
 * Copyright (C) 2000 Frans Kaashoek (kaashoek@lcs.mit.edu)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#include "chord.h"
#include "dhash.h"
#include "parseopt.h"

EXITFN (cleanup);

#define MAX_VNODES 1024

ptr<chord> chordnode;
static str p2psocket;
int do_cache;
str db_name;
dhash *dh[MAX_VNODES + 1];
int ndhash = 0;
int nreplica;
int ss_mode;

void stats ();
void stop ();

void
client_accept (int fd)
{
  if (fd < 0)
    fatal ("EOF\n");
  ref<axprt_stream> x = axprt_stream::alloc (fd);
  dhashclient *c = New dhashclient (x, nreplica, chordnode);
  if (do_cache) c->set_caching (1);
  else c->set_caching (0);
}

static void
client_accept_socket (int lfd)
{
  sockaddr_un sun;
  bzero (&sun, sizeof (sun));
  socklen_t sunlen = sizeof (sun);
  int fd = accept (lfd, reinterpret_cast<sockaddr *> (&sun), &sunlen);
  if (fd >= 0)
    client_accept (fd);
}

static void
client_listening_cb (int fd, int status)
{
  if (status)
    close (fd);
  else if (listen (fd, 5) < 0) {
    warn ("not listening for sfskey: listen: %m\n");
    close (fd);
  }
  else {
    fdcb (fd, selread, wrap (client_accept_socket, fd));
  }
}

static void
cleanup ()
{
   unlink (p2psocket);
}

static void
startclntd()
{
  sockaddr_un sun;
  if (p2psocket.len () >= sizeof (sun.sun_path)) {
    warn ("not listening on socket: path too long: %s\n", p2psocket.cstr ());
    return;
  }
  int clntfd = socket (AF_UNIX, SOCK_STREAM, 0);
  if (clntfd < 0) {
    warn ("not listening on socket: socket: %m\n");
    return;
  }
  make_async (clntfd);

  bzero (&sun, sizeof (sun));
  sun.sun_family = AF_UNIX;
  strcpy (sun.sun_path, p2psocket);

#if 0
  pid_t pid = afork ();
  if (pid == -1) {
    warn ("not listening on socket: fork: %m\n");
    return;
  }
  else if (!pid) {
#endif
    umask (077);
    if (bind (clntfd, (sockaddr *) &sun, sizeof (sun)) < 0) {
      if (errno == EADDRINUSE)
	unlink (sun.sun_path);
      if (bind (clntfd, (sockaddr *) &sun, sizeof (sun)) < 0) {
	warn ("not listening on socket: %s: %m\n", sun.sun_path);
	err_flush ();
	_exit (1);
      }
    }
#if 0
    err_flush ();
    _exit (0);
  }
  chldcb (pid, wrap (start_listening_cb, clntfd));
#endif
  client_listening_cb (clntfd, 0);
}

static void
newvnode_cb (int nreplica, int n, vnode *my, chordstat stat)
{
  if (stat != CHORD_OK) {
    warnx << "newvnode_cb: status " << stat << "\n";
    fatal ("unable to join\n");
  }
  str db_name_prime = strbuf () << db_name << "-" << n;
  if (ndhash == MAX_VNODES) fatal << "Too many virtual nodes (1024)\n";
  dh[ndhash++] = New dhash (db_name_prime, my, nreplica, 10000, 1000, ss_mode);
  if (n > 0) chordnode->newvnode (wrap (newvnode_cb, nreplica, n-1));
}

static void
initID (str s, chordID *ID)
{
  chordID n (s);
  chordID b (1);
  b = b << NBIT;
  b = b - 1;
  *ID = n & b;
}

static void
parseconfigfile (str cf, int nvnode, int set_rpcdelay)
{
  parseargs pa (cf);
  bool errors = false;
  int line;
  vec<str> av;
  int myport;
  str myhost;
  str wellknownhost;
  int wellknownport;
  chordID wellknownID;
  int ss = 10000;
  int cs = 1000;
  myport = 0;
  int max_connections = 400;
  int max_loccache = 100;

  while (pa.getline (&av, &line)) {
    if (!strcasecmp (av[0], "#")) {
    } else if (!strcasecmp (av[0], "myport")) {
      if (av.size () != 2 || !convertint (av[1], &myport)) {
        errors = true;
        warn << cf << ":" << line << ": usage: myport <number>\n";
      }
    } else if (!strcasecmp (av[0], "wellknownport")) {
      if (av.size () != 2 || !convertint (av[1], &wellknownport)) {
        errors = true;
        warn << cf << ":" << line << ": usage: wellknownport <number>\n";
      }
    } else if (!strcasecmp (av[0], "wellknownID")) {
      if (av.size () != 2) {
        errors = true;
        warn << cf << ":" << line << ": usage: wellknownID <number>\n";
      } else {
	initID (av[1], &wellknownID);
      }
    } else if (!strcasecmp (av[0], "wellknownport")) {
      if (av.size () != 2 || !convertint (av[1], &wellknownport)) {
        errors = true;
        warn << cf << ":" << line << ": usage: wellknownport <number>\n";
      }
    } else if (!strcasecmp (av[0], "nreplica")) {
      if (av.size () != 2 || !convertint (av[1], &nreplica)) {
        errors = true;
        warn << cf << ":" << line << ": usage: nreplica <number>\n";
      }
   } else if (!strcasecmp (av[0], "wellknownhost")) {
      if (av.size () != 2) {
        errors = true;
        warn << cf << ":" << line << ": usage: wellknownhost <hostname>\n";
      }
      else
        wellknownhost = av[1];
   } else if (!strcasecmp (av[0], "storesize")) {
     if (av.size () != 2 || !convertint (av[1], &ss)) {
       errors = true;
       warn << cf << ":" << line << ": usage: storesize <size in elements>\n";
     }
   } else if (!strcasecmp (av[0], "cachesize")) {
     if (av.size () != 2 || !convertint (av[1], &cs)) {
       errors = true;
       warn << cf << ":" << line << ": usage: cachesize <size in elements>\n";
     }
   } else if (!strcasecmp (av[0], "maxopenconnections")) {
     if (av.size () != 2 || !convertint (av[1], &max_connections)) {
       errors = true;
       warn << cf << ":" << line << ": usage: maxopenconnections <number>\n";
     }
   } else if (!strcasecmp (av[0], "maxlocationcache")) {
     if (av.size () != 2 || !convertint (av[1], &max_loccache)) {
       errors = true;
       warn << cf << ":" << line << ": usage: maxlocationcache <number>\n";
     }
   }
  }
  if (errors) {
    fatal ("errors in config file\n");
  }
  if (!wellknownhost) {
    fatal ("specify wellknownhost\n");
  }
  max_loccache = max_loccache * (nvnode + 1);
  chordnode = New refcounted<chord> (wellknownhost, wellknownport,
				     wellknownID, myport, set_rpcdelay,
				     max_loccache, max_connections,
				     ss_mode);
  chordnode->newvnode (wrap (newvnode_cb, nreplica, nvnode-1));
  sigcb(SIGUSR1, wrap (&stats));
  sigcb(SIGUSR2, wrap (&stop));
}

void
stats ()
{
  chordnode->stats ();
  for (int i = 0 ; i < ndhash; i++)
    dh[i]->print_stats ();
  chordnode->print ();
}

void
stop ()
{
  chordnode->stop ();
  for (int i = 0 ; i < ndhash; i++)
    dh[i]->stop ();
}
static void
usage ()
{
  warnx << "Usage: " << progname
	<< "-d <dbfile> -S <sock> -v <nvnode> -c <cache?> -f <conffile>"
	<< "-s <server select mode>\n";
  exit (1);
}

int
main (int argc, char **argv)
{
  int vnode = 1;
  setprogname (argv[0]);
  sfsconst_init ();
  random_init ();
  int ch;
  do_cache = 0;
  int set_name = 0;
  int set_rpcdelay = 0;
  ss_mode = 0;

  while ((ch = getopt (argc, argv, "d:S:v:f:cs:")) != -1)
    switch (ch) {
    case 'S':
      p2psocket = optarg;
      break;
    case 'v':
      vnode = atoi (optarg);
      break;
    case 'r':
      set_rpcdelay = atoi(optarg);
      break;
    case 'f':
      if (!set_name) fatal("must specify db name\n");
      parseconfigfile (optarg, vnode, set_rpcdelay);
      break;
    case 'c':
      do_cache = 1;
      break;
    case 'd':
      db_name = optarg;
      set_name = 1;
      break;
    case 's':
      ss_mode = atoi(optarg);
      break;
    default:
      usage ();
      break;
    }

  if (!chordnode)
    fatal ("Specify config file\n");
  if (p2psocket)
    startclntd();
  amain ();
}

<commit_msg>Report an error if the config file parameter "wellknownhost" is not an IP address, instead of just failing silently.<commit_after>/*
 *
 * Copyright (C) 2000 Frans Kaashoek (kaashoek@lcs.mit.edu)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#include "chord.h"
#include "dhash.h"
#include "parseopt.h"

EXITFN (cleanup);

#define MAX_VNODES 1024

ptr<chord> chordnode;
static str p2psocket;
int do_cache;
str db_name;
dhash *dh[MAX_VNODES + 1];
int ndhash = 0;
int nreplica;
int ss_mode;

void stats ();
void stop ();

void
client_accept (int fd)
{
  if (fd < 0)
    fatal ("EOF\n");
  ref<axprt_stream> x = axprt_stream::alloc (fd);
  dhashclient *c = New dhashclient (x, nreplica, chordnode);
  if (do_cache) c->set_caching (1);
  else c->set_caching (0);
}

static void
client_accept_socket (int lfd)
{
  sockaddr_un sun;
  bzero (&sun, sizeof (sun));
  socklen_t sunlen = sizeof (sun);
  int fd = accept (lfd, reinterpret_cast<sockaddr *> (&sun), &sunlen);
  if (fd >= 0)
    client_accept (fd);
}

static void
client_listening_cb (int fd, int status)
{
  if (status)
    close (fd);
  else if (listen (fd, 5) < 0) {
    warn ("not listening for sfskey: listen: %m\n");
    close (fd);
  }
  else {
    fdcb (fd, selread, wrap (client_accept_socket, fd));
  }
}

static void
cleanup ()
{
   unlink (p2psocket);
}

static void
startclntd()
{
  sockaddr_un sun;
  if (p2psocket.len () >= sizeof (sun.sun_path)) {
    warn ("not listening on socket: path too long: %s\n", p2psocket.cstr ());
    return;
  }
  int clntfd = socket (AF_UNIX, SOCK_STREAM, 0);
  if (clntfd < 0) {
    warn ("not listening on socket: socket: %m\n");
    return;
  }
  make_async (clntfd);

  bzero (&sun, sizeof (sun));
  sun.sun_family = AF_UNIX;
  strcpy (sun.sun_path, p2psocket);

#if 0
  pid_t pid = afork ();
  if (pid == -1) {
    warn ("not listening on socket: fork: %m\n");
    return;
  }
  else if (!pid) {
#endif
    umask (077);
    if (bind (clntfd, (sockaddr *) &sun, sizeof (sun)) < 0) {
      if (errno == EADDRINUSE)
	unlink (sun.sun_path);
      if (bind (clntfd, (sockaddr *) &sun, sizeof (sun)) < 0) {
	warn ("not listening on socket: %s: %m\n", sun.sun_path);
	err_flush ();
	_exit (1);
      }
    }
#if 0
    err_flush ();
    _exit (0);
  }
  chldcb (pid, wrap (start_listening_cb, clntfd));
#endif
  client_listening_cb (clntfd, 0);
}

static void
newvnode_cb (int nreplica, int n, vnode *my, chordstat stat)
{
  if (stat != CHORD_OK) {
    warnx << "newvnode_cb: status " << stat << "\n";
    fatal ("unable to join\n");
  }
  str db_name_prime = strbuf () << db_name << "-" << n;
  if (ndhash == MAX_VNODES) fatal << "Too many virtual nodes (1024)\n";
  dh[ndhash++] = New dhash (db_name_prime, my, nreplica, 10000, 1000, ss_mode);
  if (n > 0) chordnode->newvnode (wrap (newvnode_cb, nreplica, n-1));
}

static void
initID (str s, chordID *ID)
{
  chordID n (s);
  chordID b (1);
  b = b << NBIT;
  b = b - 1;
  *ID = n & b;
}

static void
parseconfigfile (str cf, int nvnode, int set_rpcdelay)
{
  parseargs pa (cf);
  bool errors = false;
  int line;
  vec<str> av;
  int myport;
  str myhost;
  str wellknownhost;
  int wellknownport;
  chordID wellknownID;
  int ss = 10000;
  int cs = 1000;
  myport = 0;
  int max_connections = 400;
  int max_loccache = 100;

  while (pa.getline (&av, &line)) {
    if (!strcasecmp (av[0], "#")) {
    } else if (!strcasecmp (av[0], "myport")) {
      if (av.size () != 2 || !convertint (av[1], &myport)) {
        errors = true;
        warn << cf << ":" << line << ": usage: myport <number>\n";
      }
    } else if (!strcasecmp (av[0], "wellknownport")) {
      if (av.size () != 2 || !convertint (av[1], &wellknownport)) {
        errors = true;
        warn << cf << ":" << line << ": usage: wellknownport <number>\n";
      }
    } else if (!strcasecmp (av[0], "wellknownID")) {
      if (av.size () != 2) {
        errors = true;
        warn << cf << ":" << line << ": usage: wellknownID <number>\n";
      } else {
	initID (av[1], &wellknownID);
      }
    } else if (!strcasecmp (av[0], "wellknownport")) {
      if (av.size () != 2 || !convertint (av[1], &wellknownport)) {
        errors = true;
        warn << cf << ":" << line << ": usage: wellknownport <number>\n";
      }
    } else if (!strcasecmp (av[0], "nreplica")) {
      if (av.size () != 2 || !convertint (av[1], &nreplica)) {
        errors = true;
        warn << cf << ":" << line << ": usage: nreplica <number>\n";
      }
   } else if (!strcasecmp (av[0], "wellknownhost")) {
      if (av.size () != 2 || inet_addr (av[1]) == INADDR_NONE) {
        errors = true;
        warn << cf << ":" << line << ": usage: wellknownhost <ip address>\n";
      }
      else {
        wellknownhost = av[1];
      }
   } else if (!strcasecmp (av[0], "storesize")) {
     if (av.size () != 2 || !convertint (av[1], &ss)) {
       errors = true;
       warn << cf << ":" << line << ": usage: storesize <size in elements>\n";
     }
   } else if (!strcasecmp (av[0], "cachesize")) {
     if (av.size () != 2 || !convertint (av[1], &cs)) {
       errors = true;
       warn << cf << ":" << line << ": usage: cachesize <size in elements>\n";
     }
   } else if (!strcasecmp (av[0], "maxopenconnections")) {
     if (av.size () != 2 || !convertint (av[1], &max_connections)) {
       errors = true;
       warn << cf << ":" << line << ": usage: maxopenconnections <number>\n";
     }
   } else if (!strcasecmp (av[0], "maxlocationcache")) {
     if (av.size () != 2 || !convertint (av[1], &max_loccache)) {
       errors = true;
       warn << cf << ":" << line << ": usage: maxlocationcache <number>\n";
     }
   }
  }
  if (errors) {
    fatal ("errors in config file\n");
  }
  if (!wellknownhost) {
    fatal ("specify wellknownhost\n");
  }
  max_loccache = max_loccache * (nvnode + 1);
  chordnode = New refcounted<chord> (wellknownhost, wellknownport,
				     wellknownID, myport, set_rpcdelay,
				     max_loccache, max_connections,
				     ss_mode);
  chordnode->newvnode (wrap (newvnode_cb, nreplica, nvnode-1));
  sigcb(SIGUSR1, wrap (&stats));
  sigcb(SIGUSR2, wrap (&stop));
}

void
stats ()
{
  chordnode->stats ();
  for (int i = 0 ; i < ndhash; i++)
    dh[i]->print_stats ();
  chordnode->print ();
}

void
stop ()
{
  chordnode->stop ();
  for (int i = 0 ; i < ndhash; i++)
    dh[i]->stop ();
}
static void
usage ()
{
  warnx << "Usage: " << progname
	<< "-d <dbfile> -S <sock> -v <nvnode> -c <cache?> -f <conffile>"
	<< "-s <server select mode>\n";
  exit (1);
}

int
main (int argc, char **argv)
{
  int vnode = 1;
  setprogname (argv[0]);
  sfsconst_init ();
  random_init ();
  int ch;
  do_cache = 0;
  int set_name = 0;
  int set_rpcdelay = 0;
  ss_mode = 0;

  while ((ch = getopt (argc, argv, "d:S:v:f:cs:")) != -1)
    switch (ch) {
    case 'S':
      p2psocket = optarg;
      break;
    case 'v':
      vnode = atoi (optarg);
      break;
    case 'r':
      set_rpcdelay = atoi(optarg);
      break;
    case 'f':
      if (!set_name) fatal("must specify db name\n");
      parseconfigfile (optarg, vnode, set_rpcdelay);
      break;
    case 'c':
      do_cache = 1;
      break;
    case 'd':
      db_name = optarg;
      set_name = 1;
      break;
    case 's':
      ss_mode = atoi(optarg);
      break;
    default:
      usage ();
      break;
    }

  if (!chordnode)
    fatal ("Specify config file\n");
  if (p2psocket)
    startclntd();
  amain ();
}

<|endoftext|>


<commit_before>//=====================================================================//
/*!	@file
	@brief	R8C グループ・A/D 制御 @n
			Copyright 2015 Kunihito Hiramatsu
	@author	平松邦仁 (hira@rvf-rc45.net)
*/
//=====================================================================//
#include "common/adc_io.hpp"

namespace device {

}

<commit_msg>remove file<commit_after><|endoftext|>


<commit_before>
#pragma once

#include "numerics/gradient_descent.hpp"

#include "geometry/grassmann.hpp"
#include "geometry/symmetric_bilinear_form.hpp"
#include "quantities/si.hpp"

namespace principia {
namespace numerics {
namespace internal_gradient_descent {

using geometry::InnerProduct;
using geometry::SymmetricProduct;
using quantities::si::Kilo;
using quantities::si::Metre;

constexpr Length initial_step_size = 1 * Kilo(Metre);
constexpr double ξ = 1;  // BFGS method.

template<typename Scalar, typename Frame>
Position<Frame> GradientDescent(
    Position<Frame> const& start_position,
    Field<Scalar, Frame> const& f,
    Gradient<Scalar, Frame> const& grad_f,
    TerminationCondition const& termination_condition) {
  Position<Frame> const x₀ = start_position;
  auto const grad_f_x₀ = gradient(x₀);
  Position<Frame> const x₁ = x₀ - initial_step_size * grad_f_x₀;
  auto const grad_f_x₁ = gradient(x₁);
  auto const p₀ = x₁ - x₀;
  auto const q₀ = grad_f_x₁ - grad_f_x₀;
  auto const D₀ = SymmetricProduct(p₀, q₀) / q₀.Norm²();
  while (!termination_condition()) {
    auto const grad_f_xₖ = gradient(xₖ);
    auto const xₖ₊₁ = xₖ - αₖ * Dₖ * grad_f_xₖ;
    auto const pₖ = xₖ₊₁ - xₖ;
    auto const grad_f_xₖ₊₁ = gradient(xₖ₊₁);
    auto const qₖ = grad_f_xₖ₊₁ - grad_f_xₖ;
    auto const Dₖqₖ = Dₖ * qₖ;
    auto const τ = InnerProduct(qₖ, Dₖqₖ);
    auto const pₖqₖ = InnerProduct(pₖ, qₖ);
    auto const v = pₖ / pₖqₖ - Dₖqₖ / τ;
    auto const Dₖ₊₁ = Dₖ + SymmetricProduct(pₖ, pₖ) / pₖqₖ -
                      Dₖ * SymmetricProduct(qₖ, qₖ) * Dₖ / τ +
                      ξ * τ * v.Norm²();
  }
}

}  // namespace internal_gradient_descent
}  // namespace numerics
}  // namespace principia
<commit_msg>Armijo rule.<commit_after>
#pragma once

#include "numerics/gradient_descent.hpp"

#include "geometry/grassmann.hpp"
#include "geometry/symmetric_bilinear_form.hpp"
#include "quantities/si.hpp"

namespace principia {
namespace numerics {
namespace internal_gradient_descent {

using geometry::Displacement;
using geometry::InnerProduct;
using geometry::SymmetricProduct;
using quantities::si::Kilo;
using quantities::si::Metre;

// Parameters for the Armijo rule.
constexpr Length s = 1 * Kilo(Metre);
constexpr double β = 0.5;
constexpr double σ = 0.5;

// We use the BFGS method.
constexpr double ξ = 1;

template<typename Scalar, typename Frame>
Length ArmijoRule(Position<Frame> const& xₖ,
                  Displacement<Frame> const& dₖ,
                  Vector<Derivative<Scalar, Length>, Frame> const& grad_f_xₖ,
                  Field<Scalar, Frame> const& f) {
  Scalar const f_xₖ = f(xₖ);
  for (double βᵐ = 1;; βᵐ *= β) {
    Length const step = βᵐ * s;
    if (f_xₖ - f(xₖ + step * dₖ) >= -σ * step * grad_f_xₖ * dₖ) {
      return step;
    }
  }
}

template<typename Scalar, typename Frame>
Position<Frame> GradientDescent(
    Position<Frame> const& start_position,
    Field<Scalar, Frame> const& f,
    Gradient<Scalar, Frame> const& grad_f,
    TerminationCondition const& termination_condition) {
  Position<Frame> const x₀ = start_position;
  auto const grad_f_x₀ = gradient(x₀);
  Position<Frame> const x₁ = x₀ - initial_step_size * grad_f_x₀;
  auto const grad_f_x₁ = gradient(x₁);
  auto const p₀ = x₁ - x₀;
  auto const q₀ = grad_f_x₁ - grad_f_x₀;
  auto const D₀ = SymmetricProduct(p₀, q₀) / q₀.Norm²();

  auto xₖ = x₁;
  auto grad_f_xₖ = grad_f_x₁;
  auto Dₖ = D₀;
  while (!termination_condition()) {
    Displacement<Frame> const dₖ = -Dₖ * grad_f_xₖ;
    double const αₖ = ArmijoRule(xₖ, dₖ, grad_f_xₖ, f);
    auto const xₖ₊₁ = xₖ + αₖ * dₖ;
    auto const pₖ = xₖ₊₁ - xₖ;
    auto const grad_f_xₖ₊₁ = gradient(xₖ₊₁);
    auto const qₖ = grad_f_xₖ₊₁ - grad_f_xₖ;
    auto const Dₖqₖ = Dₖ * qₖ;
    auto const τ = InnerProduct(qₖ, Dₖqₖ);
    auto const pₖqₖ = InnerProduct(pₖ, qₖ);
    auto const v = pₖ / pₖqₖ - Dₖqₖ / τ;
    auto const Dₖ₊₁ = Dₖ + SymmetricProduct(pₖ, pₖ) / pₖqₖ -
                      Dₖ * SymmetricProduct(qₖ, qₖ) * Dₖ / τ +
                      ξ * τ * v.Norm²();

    xₖ = xₖ₊₁;
    grad_f_xₖ = grad_f_xₖ₊₁;
    Dₖ = Dₖ₊₁;
  }
}

}  // namespace internal_gradient_descent
}  // namespace numerics
}  // namespace principia
<|endoftext|>


<commit_before>#include <cppunit/extensions/HelperMacros.h>
#include "BVect.hpp"

class BVectTest : public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(BVectTest);
  CPPUNIT_TEST(testDominance);
  CPPUNIT_TEST_SUITE_END();
 private:
  BVect* a;
  BVect* b;
  BVect* c;
  BVect* d;
 public:
  void setUp() {
    std::vector<double> x;
    a = new BVect(1, 2, x);
    b = new BVect(2, 1, x);
    c = new BVect(3, 3, x);
    d = new BVect(2, 2, x);
  }
  void tearDown() {
    delete a;
    delete b;
    delete c;
    delete d;
  }
  void testDominance() {
    CPPUNIT_ASSERT(!a->dominates(*a));
    CPPUNIT_ASSERT(!a->dominates(*b));
    CPPUNIT_ASSERT(a->dominates(*c));
    CPPUNIT_ASSERT(a->dominates(*d));
    CPPUNIT_ASSERT(!b->dominates(*a));
    CPPUNIT_ASSERT(!b->dominates(*b));
    CPPUNIT_ASSERT(b->dominates(*c));
    CPPUNIT_ASSERT(b->dominates(*d));
    CPPUNIT_ASSERT(!c->dominates(*a));
    CPPUNIT_ASSERT(!c->dominates(*b));
    CPPUNIT_ASSERT(!c->dominates(*c));
    CPPUNIT_ASSERT(!c->dominates(*d));
    CPPUNIT_ASSERT(!d->dominates(*a));
    CPPUNIT_ASSERT(!d->dominates(*b));
    CPPUNIT_ASSERT(d->dominates(*c));
    CPPUNIT_ASSERT(!d->dominates(*d));
  }
};

CPPUNIT_TEST_SUITE_REGISTRATION(BVectTest);
<commit_msg>BVect: Add weak dominance tests<commit_after>#include <cppunit/extensions/HelperMacros.h>
#include "BVect.hpp"

class BVectTest : public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(BVectTest);
  CPPUNIT_TEST(testDominance);
  CPPUNIT_TEST(testWeakDominance);
  CPPUNIT_TEST_SUITE_END();
 private:
  BVect* a;
  BVect* b;
  BVect* c;
  BVect* d;
 public:
  void setUp() {
    std::vector<double> x;
    a = new BVect(1, 2, x);
    b = new BVect(2, 1, x);
    c = new BVect(3, 3, x);
    d = new BVect(2, 2, x);
  }
  void tearDown() {
    delete a;
    delete b;
    delete c;
    delete d;
  }
  void testDominance() {
    CPPUNIT_ASSERT(!a->dominates(*a));
    CPPUNIT_ASSERT(!a->dominates(*b));
    CPPUNIT_ASSERT(a->dominates(*c));
    CPPUNIT_ASSERT(a->dominates(*d));
    CPPUNIT_ASSERT(!b->dominates(*a));
    CPPUNIT_ASSERT(!b->dominates(*b));
    CPPUNIT_ASSERT(b->dominates(*c));
    CPPUNIT_ASSERT(b->dominates(*d));
    CPPUNIT_ASSERT(!c->dominates(*a));
    CPPUNIT_ASSERT(!c->dominates(*b));
    CPPUNIT_ASSERT(!c->dominates(*c));
    CPPUNIT_ASSERT(!c->dominates(*d));
    CPPUNIT_ASSERT(!d->dominates(*a));
    CPPUNIT_ASSERT(!d->dominates(*b));
    CPPUNIT_ASSERT(d->dominates(*c));
    CPPUNIT_ASSERT(!d->dominates(*d));
  }
  void testWeakDominance() {
    CPPUNIT_ASSERT(a->weaklyDominates(*a));
    CPPUNIT_ASSERT(!a->weaklyDominates(*b));
    CPPUNIT_ASSERT(a->weaklyDominates(*c));
    CPPUNIT_ASSERT(a->weaklyDominates(*d));
    CPPUNIT_ASSERT(!b->weaklyDominates(*a));
    CPPUNIT_ASSERT(b->weaklyDominates(*b));
    CPPUNIT_ASSERT(b->weaklyDominates(*c));
    CPPUNIT_ASSERT(b->weaklyDominates(*d));
    CPPUNIT_ASSERT(!c->weaklyDominates(*a));
    CPPUNIT_ASSERT(!c->weaklyDominates(*b));
    CPPUNIT_ASSERT(c->weaklyDominates(*c));
    CPPUNIT_ASSERT(!c->weaklyDominates(*d));
    CPPUNIT_ASSERT(!d->weaklyDominates(*a));
    CPPUNIT_ASSERT(!d->weaklyDominates(*b));
    CPPUNIT_ASSERT(d->weaklyDominates(*c));
    CPPUNIT_ASSERT(d->weaklyDominates(*d));
  }
};

CPPUNIT_TEST_SUITE_REGISTRATION(BVectTest);
<|endoftext|>


<commit_before>/*
 * Copyright 2018 Google LLC.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. Neither the name of the copyright holder 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 HOLDER 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.
 *
 */

// Implementation of vcf_writer.h
#include "third_party/nucleus/io/vcf_writer.h"

#include <cmath>
#include <utility>
#include <vector>

#include "google/protobuf/map.h"
#include "google/protobuf/repeated_field.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/substitute.h"
#include "htslib/hts.h"
#include "htslib/sam.h"
#include "third_party/nucleus/io/hts_path.h"
#include "third_party/nucleus/io/vcf_conversion.h"
#include "third_party/nucleus/protos/reference.pb.h"
#include "third_party/nucleus/protos/variants.pb.h"
#include "third_party/nucleus/util/utils.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/platform/logging.h"

namespace nucleus {

namespace tf = tensorflow;

using absl::StrCat;
using nucleus::genomics::v1::Variant;

namespace {

constexpr char kOpenModeCompressed[] = "wz";
constexpr char kOpenModeUncompressed[] = "w";

constexpr char kFilterHeaderFmt[] = "##FILTER=<ID=$0,Description=\"$1\">";
constexpr char kInfoHeaderFmt[] =
    "##INFO=<ID=$0,Number=$1,Type=$2,Description=\"$3\"$4>";
constexpr char kFormatHeaderFmt[] =
    "##FORMAT=<ID=$0,Number=$1,Type=$2,Description=\"$3\">";
constexpr char kContigHeaderFmt[] = "##contig=<ID=$0$1>";
constexpr char kStructuredExtraHeaderFmt[] = "##$0=<$1>";
constexpr char kExtraHeaderFmt[] = "##$0=\"$1\"";

// Adds a FILTER field to the bcf_hdr_t header based on the VcfFilterInfo
// object.
void AddFilterToHeader(const nucleus::genomics::v1::VcfFilterInfo& filter,
                       bcf_hdr_t* header) {
  string filterStr = absl::Substitute(
      kFilterHeaderFmt, filter.id().c_str(), filter.description().c_str());
  bcf_hdr_append(header, filterStr.c_str());
}

// Adds an INFO field to the bcf_hdr_t header based on the VcfInfo object.
void AddInfoToHeader(const nucleus::genomics::v1::VcfInfo& info,
                     bcf_hdr_t* header) {
  string extra = string("");
  if (!info.source().empty()) {
    extra = StrCat(",Source=\"", info.source(), "\"");
  }
  if (!info.version().empty()) {
    extra = StrCat(extra, ",Version=\"", info.version(), "\"");
  }
  string infoStr = absl::Substitute(
      kInfoHeaderFmt, info.id().c_str(), info.number().c_str(),
      info.type().c_str(), info.description().c_str(), extra.c_str());
  bcf_hdr_append(header, infoStr.c_str());
}

// Adds a FORMAT field to the bcf_hdr_t header based on the VcfFormatInfo
// object.
void AddFormatToHeader(const nucleus::genomics::v1::VcfFormatInfo& format,
                       bcf_hdr_t* header) {
  string formatStr = absl::Substitute(
      kFormatHeaderFmt, format.id().c_str(), format.number().c_str(),
      format.type().c_str(), format.description().c_str());
  bcf_hdr_append(header, formatStr.c_str());
}

// Adds a structured extra field to the bcf_hdr_t header based on the
// VcfStructuredExtra object.
void AddStructuredExtraToHeader(
    const nucleus::genomics::v1::VcfStructuredExtra& sExtra,
    bcf_hdr_t* header) {
  string fieldStr = string("");
  for (auto const& kv : sExtra.fields()) {
    fieldStr = StrCat(fieldStr, kv.key(), "=\"", kv.value(), "\",");
  }
  if (!fieldStr.empty()) {
    // Cut off the dangling comma.
    fieldStr.pop_back();
  }
  string result = absl::Substitute(
      kStructuredExtraHeaderFmt, sExtra.key().c_str(), fieldStr.c_str());
  bcf_hdr_append(header, result.c_str());
}

// Adds an unstructured extra field to the bcf_hdr_t header based on the
// VcfExtra object.
void AddExtraToHeader(const nucleus::genomics::v1::VcfExtra& extra,
                      bcf_hdr_t* header) {
  string result = absl::Substitute(
      kExtraHeaderFmt, extra.key().c_str(), extra.value().c_str());
  bcf_hdr_append(header, result.c_str());
}

// Adds a contig field to the bcf_hdr_t header based on the ContigInfo object.
void AddContigToHeader(const nucleus::genomics::v1::ContigInfo& contig,
                       bcf_hdr_t* header) {
  string extra =
      contig.n_bases() ? StrCat(",length=", contig.n_bases()) : string("");
  if (!contig.description().empty()) {
    extra = StrCat(extra, ",description=\"", contig.description(), "\"");
  }
  for (auto const& kv : contig.extra()) {
    extra = StrCat(extra, ",", kv.first, "=\"", kv.second, "\"");
  }
  string contigStr = absl::Substitute(
      kContigHeaderFmt, contig.name().c_str(), extra.c_str());
  bcf_hdr_append(header, contigStr.c_str());
}

}  // namespace

StatusOr<std::unique_ptr<VcfWriter>> VcfWriter::ToFile(
    const string& variants_path, const nucleus::genomics::v1::VcfHeader& header,
    const nucleus::genomics::v1::VcfWriterOptions& options) {
  const char* const openMode = EndsWith(variants_path, ".gz")
                                   ? kOpenModeCompressed
                                   : kOpenModeUncompressed;
  htsFile* fp = hts_open_x(variants_path.c_str(), openMode);
  if (fp == nullptr)
    return tf::errors::Unknown(
        StrCat("Could not open variants_path ", variants_path));

  auto writer = absl::WrapUnique(new VcfWriter(header, options, fp));
  TF_RETURN_IF_ERROR(writer->WriteHeader());
  return std::move(writer);
}

VcfWriter::VcfWriter(const nucleus::genomics::v1::VcfHeader& header,
                     const nucleus::genomics::v1::VcfWriterOptions& options,
                     htsFile* fp)
    : fp_(fp),
      options_(options),
      vcf_header_(header),
      record_converter_(
          vcf_header_,
          std::vector<string>(options_.excluded_info_fields().begin(),
                              options_.excluded_info_fields().end()),
          std::vector<string>(options_.excluded_format_fields().begin(),
                              options_.excluded_format_fields().end()),
          options_.retrieve_gl_and_pl_from_info_map()) {
  CHECK(fp != nullptr);

  // Note: bcf_hdr_init writes the fileformat= and the FILTER=<ID=PASS,...>
  // filter automatically.
  header_ = bcf_hdr_init("w");
  for (const nucleus::genomics::v1::VcfFilterInfo& filter :
       vcf_header_.filters()) {
    if (filter.id() != "PASS") {
      AddFilterToHeader(filter, header_);
    }
  }
  for (const nucleus::genomics::v1::VcfInfo& info : vcf_header_.infos()) {
    AddInfoToHeader(info, header_);
  }
  for (const nucleus::genomics::v1::VcfFormatInfo& format :
       vcf_header_.formats()) {
    AddFormatToHeader(format, header_);
  }
  for (const nucleus::genomics::v1::VcfStructuredExtra& sExtra :
       vcf_header_.structured_extras()) {
    AddStructuredExtraToHeader(sExtra, header_);
  }
  for (const nucleus::genomics::v1::VcfExtra& extra : vcf_header_.extras()) {
    AddExtraToHeader(extra, header_);
  }
  for (const nucleus::genomics::v1::ContigInfo& contig :
       vcf_header_.contigs()) {
    AddContigToHeader(contig, header_);
  }

  for (const string& sampleName : vcf_header_.sample_names()) {
    bcf_hdr_add_sample(header_, sampleName.c_str());
  }
  bcf_hdr_add_sample(header_, nullptr);
}

tf::Status VcfWriter::WriteHeader() {
  if (bcf_hdr_write(fp_, header_) < 0)
    return tf::errors::Unknown("Failed to write header");
  else
    return tf::Status::OK();
}

VcfWriter::~VcfWriter() {
  if (fp_) {
    // There's nothing we can do but assert fail if there's an error during
    // the Close() call here.
    TF_CHECK_OK(Close());
  }
}

tf::Status VcfWriter::Write(const Variant& variant_message) {
  if (fp_ == nullptr)
    return tf::errors::FailedPrecondition("Cannot write to closed VCF stream.");
  bcf1_t* v = bcf_init();
  if (v == nullptr)
    return tf::errors::Unknown("bcf_init call failed");
  TF_RETURN_IF_ERROR(
      RecordConverter().ConvertFromPb(variant_message, *header_, v));
  if (options_.round_qual_values() && !bcf_float_is_missing(v->qual)) {
    // Round quality value printed out to one digit past the decimal point.
    double rounded_quality = floor(variant_message.quality() * 10 + 0.5) / 10;
    v->qual = rounded_quality;
  }
  if (bcf_write(fp_, header_, v) != 0)
    return tf::errors::Unknown("bcf_write call failed");
  bcf_destroy(v);
  return tf::Status::OK();
}

tf::Status VcfWriter::Close() {
  if (fp_ == nullptr)
    return tf::errors::FailedPrecondition(
        "Cannot close an already closed VcfWriter");
  if (hts_close(fp_) < 0)
    return tf::errors::Unknown("hts_close call failed");
  fp_ = nullptr;
  bcf_hdr_destroy(header_);
  header_ = nullptr;
  return tf::Status::OK();
}

}  // namespace nucleus
<commit_msg>There are two places where VcfWriter::Write exited on error but failed to clean up bcf1_t*.<commit_after>/*
 * Copyright 2018 Google LLC.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. Neither the name of the copyright holder 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 HOLDER 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.
 *
 */

// Implementation of vcf_writer.h
#include "third_party/nucleus/io/vcf_writer.h"

#include <cmath>
#include <utility>
#include <vector>

#include "google/protobuf/map.h"
#include "google/protobuf/repeated_field.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/substitute.h"
#include "htslib/hts.h"
#include "htslib/sam.h"
#include "third_party/nucleus/io/hts_path.h"
#include "third_party/nucleus/io/vcf_conversion.h"
#include "third_party/nucleus/protos/reference.pb.h"
#include "third_party/nucleus/protos/variants.pb.h"
#include "third_party/nucleus/util/utils.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/platform/logging.h"

namespace nucleus {

namespace tf = tensorflow;

using absl::StrCat;
using nucleus::genomics::v1::Variant;

namespace {

constexpr char kOpenModeCompressed[] = "wz";
constexpr char kOpenModeUncompressed[] = "w";

constexpr char kFilterHeaderFmt[] = "##FILTER=<ID=$0,Description=\"$1\">";
constexpr char kInfoHeaderFmt[] =
    "##INFO=<ID=$0,Number=$1,Type=$2,Description=\"$3\"$4>";
constexpr char kFormatHeaderFmt[] =
    "##FORMAT=<ID=$0,Number=$1,Type=$2,Description=\"$3\">";
constexpr char kContigHeaderFmt[] = "##contig=<ID=$0$1>";
constexpr char kStructuredExtraHeaderFmt[] = "##$0=<$1>";
constexpr char kExtraHeaderFmt[] = "##$0=\"$1\"";

// Adds a FILTER field to the bcf_hdr_t header based on the VcfFilterInfo
// object.
void AddFilterToHeader(const nucleus::genomics::v1::VcfFilterInfo& filter,
                       bcf_hdr_t* header) {
  string filterStr = absl::Substitute(
      kFilterHeaderFmt, filter.id().c_str(), filter.description().c_str());
  bcf_hdr_append(header, filterStr.c_str());
}

// Adds an INFO field to the bcf_hdr_t header based on the VcfInfo object.
void AddInfoToHeader(const nucleus::genomics::v1::VcfInfo& info,
                     bcf_hdr_t* header) {
  string extra = string("");
  if (!info.source().empty()) {
    extra = StrCat(",Source=\"", info.source(), "\"");
  }
  if (!info.version().empty()) {
    extra = StrCat(extra, ",Version=\"", info.version(), "\"");
  }
  string infoStr = absl::Substitute(
      kInfoHeaderFmt, info.id().c_str(), info.number().c_str(),
      info.type().c_str(), info.description().c_str(), extra.c_str());
  bcf_hdr_append(header, infoStr.c_str());
}

// Adds a FORMAT field to the bcf_hdr_t header based on the VcfFormatInfo
// object.
void AddFormatToHeader(const nucleus::genomics::v1::VcfFormatInfo& format,
                       bcf_hdr_t* header) {
  string formatStr = absl::Substitute(
      kFormatHeaderFmt, format.id().c_str(), format.number().c_str(),
      format.type().c_str(), format.description().c_str());
  bcf_hdr_append(header, formatStr.c_str());
}

// Adds a structured extra field to the bcf_hdr_t header based on the
// VcfStructuredExtra object.
void AddStructuredExtraToHeader(
    const nucleus::genomics::v1::VcfStructuredExtra& sExtra,
    bcf_hdr_t* header) {
  string fieldStr = string("");
  for (auto const& kv : sExtra.fields()) {
    fieldStr = StrCat(fieldStr, kv.key(), "=\"", kv.value(), "\",");
  }
  if (!fieldStr.empty()) {
    // Cut off the dangling comma.
    fieldStr.pop_back();
  }
  string result = absl::Substitute(
      kStructuredExtraHeaderFmt, sExtra.key().c_str(), fieldStr.c_str());
  bcf_hdr_append(header, result.c_str());
}

// Adds an unstructured extra field to the bcf_hdr_t header based on the
// VcfExtra object.
void AddExtraToHeader(const nucleus::genomics::v1::VcfExtra& extra,
                      bcf_hdr_t* header) {
  string result = absl::Substitute(
      kExtraHeaderFmt, extra.key().c_str(), extra.value().c_str());
  bcf_hdr_append(header, result.c_str());
}

// Adds a contig field to the bcf_hdr_t header based on the ContigInfo object.
void AddContigToHeader(const nucleus::genomics::v1::ContigInfo& contig,
                       bcf_hdr_t* header) {
  string extra =
      contig.n_bases() ? StrCat(",length=", contig.n_bases()) : string("");
  if (!contig.description().empty()) {
    extra = StrCat(extra, ",description=\"", contig.description(), "\"");
  }
  for (auto const& kv : contig.extra()) {
    extra = StrCat(extra, ",", kv.first, "=\"", kv.second, "\"");
  }
  string contigStr = absl::Substitute(
      kContigHeaderFmt, contig.name().c_str(), extra.c_str());
  bcf_hdr_append(header, contigStr.c_str());
}

// RAII wrapper on top of bcf1_t* to always perform cleanup.
class BCFRecord {
 public:
  BCFRecord() : bcf1_(bcf_init()) {}
  ~BCFRecord() {
    if (bcf1_) {
      bcf_destroy(bcf1_);
    }
  }

  // Disable copy or assignment
  BCFRecord(const BCFRecord& other) = delete;
  BCFRecord& operator=(const BCFRecord&) = delete;

  bcf1_t* get_bcf1() { return bcf1_; }

 private:
  bcf1_t* bcf1_;
};

}  // namespace

StatusOr<std::unique_ptr<VcfWriter>> VcfWriter::ToFile(
    const string& variants_path, const nucleus::genomics::v1::VcfHeader& header,
    const nucleus::genomics::v1::VcfWriterOptions& options) {
  const char* const openMode = EndsWith(variants_path, ".gz")
                                   ? kOpenModeCompressed
                                   : kOpenModeUncompressed;
  htsFile* fp = hts_open_x(variants_path.c_str(), openMode);
  if (fp == nullptr)
    return tf::errors::Unknown(
        StrCat("Could not open variants_path ", variants_path));

  auto writer = absl::WrapUnique(new VcfWriter(header, options, fp));
  TF_RETURN_IF_ERROR(writer->WriteHeader());
  return std::move(writer);
}

VcfWriter::VcfWriter(const nucleus::genomics::v1::VcfHeader& header,
                     const nucleus::genomics::v1::VcfWriterOptions& options,
                     htsFile* fp)
    : fp_(fp),
      options_(options),
      vcf_header_(header),
      record_converter_(
          vcf_header_,
          std::vector<string>(options_.excluded_info_fields().begin(),
                              options_.excluded_info_fields().end()),
          std::vector<string>(options_.excluded_format_fields().begin(),
                              options_.excluded_format_fields().end()),
          options_.retrieve_gl_and_pl_from_info_map()) {
  CHECK(fp != nullptr);

  // Note: bcf_hdr_init writes the fileformat= and the FILTER=<ID=PASS,...>
  // filter automatically.
  header_ = bcf_hdr_init("w");
  for (const nucleus::genomics::v1::VcfFilterInfo& filter :
       vcf_header_.filters()) {
    if (filter.id() != "PASS") {
      AddFilterToHeader(filter, header_);
    }
  }
  for (const nucleus::genomics::v1::VcfInfo& info : vcf_header_.infos()) {
    AddInfoToHeader(info, header_);
  }
  for (const nucleus::genomics::v1::VcfFormatInfo& format :
       vcf_header_.formats()) {
    AddFormatToHeader(format, header_);
  }
  for (const nucleus::genomics::v1::VcfStructuredExtra& sExtra :
       vcf_header_.structured_extras()) {
    AddStructuredExtraToHeader(sExtra, header_);
  }
  for (const nucleus::genomics::v1::VcfExtra& extra : vcf_header_.extras()) {
    AddExtraToHeader(extra, header_);
  }
  for (const nucleus::genomics::v1::ContigInfo& contig :
       vcf_header_.contigs()) {
    AddContigToHeader(contig, header_);
  }

  for (const string& sampleName : vcf_header_.sample_names()) {
    bcf_hdr_add_sample(header_, sampleName.c_str());
  }
  bcf_hdr_add_sample(header_, nullptr);
}

tf::Status VcfWriter::WriteHeader() {
  if (bcf_hdr_write(fp_, header_) < 0)
    return tf::errors::Unknown("Failed to write header");
  else
    return tf::Status::OK();
}

VcfWriter::~VcfWriter() {
  if (fp_) {
    // There's nothing we can do but assert fail if there's an error during
    // the Close() call here.
    TF_CHECK_OK(Close());
  }
}

tf::Status VcfWriter::Write(const Variant& variant_message) {
  if (fp_ == nullptr)
    return tf::errors::FailedPrecondition("Cannot write to closed VCF stream.");
  BCFRecord v;
  if (v.get_bcf1() == nullptr) {
    return tf::errors::Unknown("bcf_init call failed");
  }
  TF_RETURN_IF_ERROR(
      RecordConverter().ConvertFromPb(variant_message, *header_, v.get_bcf1()));
  if (options_.round_qual_values() &&
      !bcf_float_is_missing(v.get_bcf1()->qual)) {
    // Round quality value printed out to one digit past the decimal point.
    double rounded_quality = floor(variant_message.quality() * 10 + 0.5) / 10;
    v.get_bcf1()->qual = rounded_quality;
  }
  if (bcf_write(fp_, header_, v.get_bcf1()) != 0) {
    return tf::errors::Unknown("bcf_write call failed");
  }
  return tf::Status::OK();
}

tf::Status VcfWriter::Close() {
  if (fp_ == nullptr)
    return tf::errors::FailedPrecondition(
        "Cannot close an already closed VcfWriter");
  if (hts_close(fp_) < 0)
    return tf::errors::Unknown("hts_close call failed");
  fp_ = nullptr;
  bcf_hdr_destroy(header_);
  header_ = nullptr;
  return tf::Status::OK();
}

}  // namespace nucleus
<|endoftext|>


<commit_before>/*
 * TI Voxel Lib component.
 *
 * Copyright (c) 2014 Texas Instruments Inc.
 *
 * Derived from http://paulbourke.net/miscellaneous/dft/
 * with major modifications to avoid too many memory allocations.
 *
 *
 * Reference: DFT (Discrete Fourier Transform) and FFT (Fast Fourier Transform)
 * Written by Paul Bourke, June 1993
 *
 * This is not meant to be an efficient implementation.
 *
 */

#include <malloc.h>
#include "DFT.h"

#include "Logger.h"

namespace Voxel
{

// bool FFT2D(Complex2D &c, int dir)
// {
//   if(c.size() == 0)
//     return false;
//
//   int i, j;
//   int m, twopm;
//   double *real, *imag;
//
//   int nx = c[0].size();
//   int ny = c.size();
//
//   /* Transform the rows */
//   real = (double *)malloc(nx * sizeof(double));
//   imag = (double *)malloc(nx * sizeof(double));
//
//   if(real == NULL || imag == NULL)
//   {
//     logger(LOG_ERROR) << "FFT2D: Failed to allocate memory" << std::endl;
//     return false;
//   }
//
//   if(!Powerof2(nx, &m, &twopm) || twopm != nx)
//   {
//     logger(LOG_ERROR) << "FFT2D: Failed to get nearest power of 2 of '" << nx << "'" << std::endl;
//     return false;
//   }
//
//   for(j = 0; j < ny; j++)
//   {
//     for(i = 0; i < nx; i++)
//     {
//       real[i] = c[i][j].real();
//       imag[i] = c[i][j].imag();
//     }
//
//     FFT(dir, m, real, imag);
//
//     for(i = 0; i < nx; i++)
//     {
//       c[i][j].real(real[i]);
//       c[i][j].imag(imag[i]);
//     }
//   }
//
//   free(real);
//   free(imag);
//
//   /* Transform the columns */
//   real = (double *)malloc(ny * sizeof(double));
//   imag = (double *)malloc(ny * sizeof(double));
//
//   if(real == NULL || imag == NULL)
//   {
//     logger(LOG_ERROR) << "FFT2D: Failed to allocate memory" << std::endl;
//     return false;
//   }
//
//   if(!Powerof2(ny, &m, &twopm) || twopm != ny)
//   {
//     logger(LOG_ERROR) << "FFT2D: Failed to get nearest power of 2 of '" << nx << "'" << std::endl;
//     return false;
//   }
//
//   for(i = 0; i < nx; i++)
//   {
//     for(j = 0; j < ny; j++)
//     {
//       real[j] = c[i][j].real();
//       imag[j] = c[i][j].imag();
//     }
//
//     FFT(dir, m, real, imag);
//
//     for(j = 0; j < ny; j++)
//     {
//       c[i][j].real(real[j]);
//       c[i][j].imag(imag[j]);
//     }
//   }
//
//   free(real);
//   free(imag);
//
//   return true;
// }
//
// bool FFT(int dir, int m, double *x, double *y)
// {
//   long nn, i, i1, j, k, i2, l, l1, l2;
//   double c1, c2, tx, ty, t1, t2, u1, u2, z;
//
//   /* Calculate the number of points */
//   nn = 1;
//
//   for(i = 0; i < m; i++)
//     nn *= 2;
//
//   /* Do the bit reversal */
//   i2 = nn >> 1;
//   j = 0;
//
//   for(i = 0; i < nn - 1; i++)
//   {
//     if(i < j)
//     {
//       tx = x[i];
//       ty = y[i];
//       x[i] = x[j];
//       y[i] = y[j];
//       x[j] = tx;
//       y[j] = ty;
//     }
//
//     k = i2;
//
//     while(k <= j)
//     {
//       j -= k;
//       k >>= 1;
//     }
//
//     j += k;
//   }
//
//   /* Compute the FFT */
//   c1 = -1.0;
//   c2 = 0.0;
//   l2 = 1;
//
//   for(l = 0; l < m; l++)
//   {
//     l1 = l2;
//     l2 <<= 1;
//     u1 = 1.0;
//     u2 = 0.0;
//
//     for(j = 0; j < l1; j++)
//     {
//       for(i = j; i < nn; i += l2)
//       {
//         i1 = i + l1;
//         t1 = u1 * x[i1] - u2 * y[i1];
//         t2 = u1 * y[i1] + u2 * x[i1];
//         x[i1] = x[i] - t1;
//         y[i1] = y[i] - t2;
//         x[i] += t1;
//         y[i] += t2;
//       }
//
//       z =  u1 * c1 - u2 * c2;
//       u2 = u1 * c2 + u2 * c1;
//       u1 = z;
//     }
//
//     c2 = sqrt((1.0 - c1) / 2.0);
//
//     if(dir == 1)
//       c2 = -c2;
//
//     c1 = sqrt((1.0 + c1) / 2.0);
//   }
//
//   /* Scaling for forward transform */
//   if(dir == 1)
//   {
//     for(i = 0; i < nn; i++)
//     {
//       x[i] /= (double)nn;
//       y[i] /= (double)nn;
//     }
//   }
//
//   return true;
// }
//
// int Powerof2(int n, int *m, int *twopm)
// {
//   if(n <= 1)
//   {
//     *m = 0;
//     *twopm = 1;
//     return false;
//   }
//
//   *m = 1;
//   *twopm = 2;
//
//   do
//   {
//     (*m)++;
//     (*twopm) *= 2;
//   }
//   while(2 * (*twopm) <= n);
//
//   if(*twopm != n)
//     return false;
//   else
//     return true;
// }

bool DFT::DFT2D(Complex2D &data, Direction dir)
{
  if(data.size() == 0 || data[0].size() == 0)
  {
    logger(LOG_ERROR) << "DFT: Number of rows is zero in input data?" << std::endl;
    return false;
  }

  _expTable.clear();

  SizeType rows = data.size(), columns = data[0].size();

  _internal.resize(rows);

  for(auto i = 0; i < rows; i++)
  {
    _internal[i].resize(columns);

    if(!DFT1D(data[i], _internal[i], dir))
    {
      logger(LOG_ERROR) << "Failed to perform 1D DFT for row '" << i << "'" << std::endl;
      return false;
    }
  }

  Complex1D in, out;
  in.resize(rows);
  out.resize(rows);

  for(auto i = 0; i < columns; i++)
  {
    for(auto j = 0; j < rows; j++)
      in[j] = _internal[j][i];

    if(!DFT1D(in, out, dir))
    {
      logger(LOG_ERROR) << "Failed to perform 1D DFT for row '" << i << "'" << std::endl;
      return false;
    }

    for(auto j = 0; j < rows; j++)
      data[j][i] = out[j];
  }

  return true;
}

bool DFT::DFT1D(const Complex1D &in, Complex1D &out, Direction dir)
{
  if(out.size() != in.size())
    out.resize(in.size());

  if(_expTable.find(in.size()) == _expTable.end())
  {
    _computeExpTable(in.size(), dir);
  }

  Complex2D &expTable = _expTable[in.size()];

  int i, k;

  int m = in.size();

  for(i = 0; i < m; i++)
  {
    out[i] = 0;
    for(k = 0; k < m; k++)
      out[i] += in[k]*expTable[i][k];
//     {
//       cosarg = cos(k*arg);
//       sinarg = sin(k*arg);
//       out[i].real(out[i].real() + (in[k].real() * cosarg - in[k].imag() * sinarg));
//       out[i].imag(out[i].imag() + (in[k].real() * sinarg + in[k].imag() * cosarg));
//     }
  }

  double msq = sqrt(m);
  for(i = 0; i < m; i++)
    out[i] /= msq;
  return true;
}

void DFT::_computeExpTable(SizeType size, Direction dir)
{
  Complex2D &expTable = _expTable[size];

  expTable.resize(size);
  for(int i = 0; i < size; i++)
  {
    expTable[i].resize(size);
    ComplexDouble arg = ComplexDouble(0, -((dir*2.0*M_PI*i)/size));

    for(int k = 0; k < size; k++)
      expTable[i][k] = exp(((double)k)*arg);
  }
}

}
<commit_msg>Fix build for DFT.cpp<commit_after>/*
 * TI Voxel Lib component.
 *
 * Copyright (c) 2014 Texas Instruments Inc.
 *
 * Derived from http://paulbourke.net/miscellaneous/dft/
 * with major modifications to avoid too many memory allocations.
 *
 *
 * Reference: DFT (Discrete Fourier Transform) and FFT (Fast Fourier Transform)
 * Written by Paul Bourke, June 1993
 *
 * This is not meant to be an efficient implementation.
 *
 */

#include <malloc.h>
#include "DFT.h"

#include "Logger.h"
#define _USE_MATH_DEFINES
#include <math.h>

namespace Voxel
{

// bool FFT2D(Complex2D &c, int dir)
// {
//   if(c.size() == 0)
//     return false;
//
//   int i, j;
//   int m, twopm;
//   double *real, *imag;
//
//   int nx = c[0].size();
//   int ny = c.size();
//
//   /* Transform the rows */
//   real = (double *)malloc(nx * sizeof(double));
//   imag = (double *)malloc(nx * sizeof(double));
//
//   if(real == NULL || imag == NULL)
//   {
//     logger(LOG_ERROR) << "FFT2D: Failed to allocate memory" << std::endl;
//     return false;
//   }
//
//   if(!Powerof2(nx, &m, &twopm) || twopm != nx)
//   {
//     logger(LOG_ERROR) << "FFT2D: Failed to get nearest power of 2 of '" << nx << "'" << std::endl;
//     return false;
//   }
//
//   for(j = 0; j < ny; j++)
//   {
//     for(i = 0; i < nx; i++)
//     {
//       real[i] = c[i][j].real();
//       imag[i] = c[i][j].imag();
//     }
//
//     FFT(dir, m, real, imag);
//
//     for(i = 0; i < nx; i++)
//     {
//       c[i][j].real(real[i]);
//       c[i][j].imag(imag[i]);
//     }
//   }
//
//   free(real);
//   free(imag);
//
//   /* Transform the columns */
//   real = (double *)malloc(ny * sizeof(double));
//   imag = (double *)malloc(ny * sizeof(double));
//
//   if(real == NULL || imag == NULL)
//   {
//     logger(LOG_ERROR) << "FFT2D: Failed to allocate memory" << std::endl;
//     return false;
//   }
//
//   if(!Powerof2(ny, &m, &twopm) || twopm != ny)
//   {
//     logger(LOG_ERROR) << "FFT2D: Failed to get nearest power of 2 of '" << nx << "'" << std::endl;
//     return false;
//   }
//
//   for(i = 0; i < nx; i++)
//   {
//     for(j = 0; j < ny; j++)
//     {
//       real[j] = c[i][j].real();
//       imag[j] = c[i][j].imag();
//     }
//
//     FFT(dir, m, real, imag);
//
//     for(j = 0; j < ny; j++)
//     {
//       c[i][j].real(real[j]);
//       c[i][j].imag(imag[j]);
//     }
//   }
//
//   free(real);
//   free(imag);
//
//   return true;
// }
//
// bool FFT(int dir, int m, double *x, double *y)
// {
//   long nn, i, i1, j, k, i2, l, l1, l2;
//   double c1, c2, tx, ty, t1, t2, u1, u2, z;
//
//   /* Calculate the number of points */
//   nn = 1;
//
//   for(i = 0; i < m; i++)
//     nn *= 2;
//
//   /* Do the bit reversal */
//   i2 = nn >> 1;
//   j = 0;
//
//   for(i = 0; i < nn - 1; i++)
//   {
//     if(i < j)
//     {
//       tx = x[i];
//       ty = y[i];
//       x[i] = x[j];
//       y[i] = y[j];
//       x[j] = tx;
//       y[j] = ty;
//     }
//
//     k = i2;
//
//     while(k <= j)
//     {
//       j -= k;
//       k >>= 1;
//     }
//
//     j += k;
//   }
//
//   /* Compute the FFT */
//   c1 = -1.0;
//   c2 = 0.0;
//   l2 = 1;
//
//   for(l = 0; l < m; l++)
//   {
//     l1 = l2;
//     l2 <<= 1;
//     u1 = 1.0;
//     u2 = 0.0;
//
//     for(j = 0; j < l1; j++)
//     {
//       for(i = j; i < nn; i += l2)
//       {
//         i1 = i + l1;
//         t1 = u1 * x[i1] - u2 * y[i1];
//         t2 = u1 * y[i1] + u2 * x[i1];
//         x[i1] = x[i] - t1;
//         y[i1] = y[i] - t2;
//         x[i] += t1;
//         y[i] += t2;
//       }
//
//       z =  u1 * c1 - u2 * c2;
//       u2 = u1 * c2 + u2 * c1;
//       u1 = z;
//     }
//
//     c2 = sqrt((1.0 - c1) / 2.0);
//
//     if(dir == 1)
//       c2 = -c2;
//
//     c1 = sqrt((1.0 + c1) / 2.0);
//   }
//
//   /* Scaling for forward transform */
//   if(dir == 1)
//   {
//     for(i = 0; i < nn; i++)
//     {
//       x[i] /= (double)nn;
//       y[i] /= (double)nn;
//     }
//   }
//
//   return true;
// }
//
// int Powerof2(int n, int *m, int *twopm)
// {
//   if(n <= 1)
//   {
//     *m = 0;
//     *twopm = 1;
//     return false;
//   }
//
//   *m = 1;
//   *twopm = 2;
//
//   do
//   {
//     (*m)++;
//     (*twopm) *= 2;
//   }
//   while(2 * (*twopm) <= n);
//
//   if(*twopm != n)
//     return false;
//   else
//     return true;
// }

bool DFT::DFT2D(Complex2D &data, Direction dir)
{
  if(data.size() == 0 || data[0].size() == 0)
  {
    logger(LOG_ERROR) << "DFT: Number of rows is zero in input data?" << std::endl;
    return false;
  }

  _expTable.clear();

  SizeType rows = data.size(), columns = data[0].size();

  _internal.resize(rows);

  for(auto i = 0; i < rows; i++)
  {
    _internal[i].resize(columns);

    if(!DFT1D(data[i], _internal[i], dir))
    {
      logger(LOG_ERROR) << "Failed to perform 1D DFT for row '" << i << "'" << std::endl;
      return false;
    }
  }

  Complex1D in, out;
  in.resize(rows);
  out.resize(rows);

  for(auto i = 0; i < columns; i++)
  {
    for(auto j = 0; j < rows; j++)
      in[j] = _internal[j][i];

    if(!DFT1D(in, out, dir))
    {
      logger(LOG_ERROR) << "Failed to perform 1D DFT for row '" << i << "'" << std::endl;
      return false;
    }

    for(auto j = 0; j < rows; j++)
      data[j][i] = out[j];
  }

  return true;
}

bool DFT::DFT1D(const Complex1D &in, Complex1D &out, Direction dir)
{
  if(out.size() != in.size())
    out.resize(in.size());

  if(_expTable.find(in.size()) == _expTable.end())
  {
    _computeExpTable(in.size(), dir);
  }

  Complex2D &expTable = _expTable[in.size()];

  int i, k;

  int m = in.size();

  for(i = 0; i < m; i++)
  {
    out[i] = 0;
    for(k = 0; k < m; k++)
      out[i] += in[k]*expTable[i][k];
//     {
//       cosarg = cos(k*arg);
//       sinarg = sin(k*arg);
//       out[i].real(out[i].real() + (in[k].real() * cosarg - in[k].imag() * sinarg));
//       out[i].imag(out[i].imag() + (in[k].real() * sinarg + in[k].imag() * cosarg));
//     }
  }

  double msq = sqrt(m);
  for(i = 0; i < m; i++)
    out[i] /= msq;
  return true;
}

void DFT::_computeExpTable(SizeType size, Direction dir)
{
  Complex2D &expTable = _expTable[size];

  expTable.resize(size);
  for(int i = 0; i < size; i++)
  {
    expTable[i].resize(size);
    ComplexDouble arg = ComplexDouble(0, -((dir*2.0*M_PI*i)/size));

    for(int k = 0; k < size; k++)
      expTable[i][k] = exp(((double)k)*arg);
  }
}

}
<|endoftext|>


<commit_before>
#include "firebasepush.hh"
#include <iostream>
#include <string.h>
#include "log/logmanager.hh"

using namespace std;

FirebasePushNotificationRequest::FirebasePushNotificationRequest(const PushInfo &pinfo)
: PushNotificationRequest(pinfo.mAppId, "firebase") {
	const string &deviceToken = pinfo.mDeviceToken;
	const string &apiKey = pinfo.mApiKey;
	ostringstream httpBody;
	time_t t = time(NULL);
	struct tm *tm = localtime(&t);
	char date[20];
	strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", tm);

	httpBody << "{\"to\":\"" << deviceToken << "\", \"priority\":\"high\"" << ", \"uuid\":\"" << pinfo.mUid
		<< "\", \"send-time\":\"" << date << "\"}";
	mHttpBody = httpBody.str();
	LOGD("Push notification https post body is %s", mHttpBody.c_str());

	ostringstream httpHeader;
	httpHeader << "POST /fcm/send "
	"HTTP/1.1\r\nHost:fcm.googleapis.com\r\nContent-Type:application/json\r\nAuthorization:key="
	<< apiKey << "\r\nContent-Length:" << httpBody.str().size() << "\r\n\r\n";
	mHttpHeader = httpHeader.str();
	SLOGD << "PNR " << this << " https post header is " << mHttpHeader;
}

void FirebasePushNotificationRequest::createPushNotification() {
	int headerLength = mHttpHeader.length();
	int bodyLength = mHttpBody.length();

	mBuffer.clear();
	mBuffer.resize(headerLength + bodyLength);

	char *binaryMessageBuff = &mBuffer[0];
	char *binaryMessagePt = binaryMessageBuff;

	memcpy(binaryMessagePt, &mHttpHeader[0], headerLength);
	binaryMessagePt += headerLength;

	memcpy(binaryMessagePt, &mHttpBody[0], bodyLength);
	binaryMessagePt += bodyLength;
}

const vector<char> &FirebasePushNotificationRequest::getData() {
	createPushNotification();
	return mBuffer;
}

string FirebasePushNotificationRequest::isValidResponse(const string &str) {
	static const char expected[] = "HTTP/1.1 200";
	return strncmp(expected, str.c_str(), sizeof(expected) - 1) == 0 ? "" : "Unexpected HTTP response value (not 200 OK)";
}
<commit_msg>Revert "Quote uuid in json body sent to Firebase."<commit_after>
#include "firebasepush.hh"
#include <iostream>
#include <string.h>
#include "log/logmanager.hh"

using namespace std;

FirebasePushNotificationRequest::FirebasePushNotificationRequest(const PushInfo &pinfo)
: PushNotificationRequest(pinfo.mAppId, "firebase") {
	const string &deviceToken = pinfo.mDeviceToken;
	const string &apiKey = pinfo.mApiKey;
	ostringstream httpBody;
	time_t t = time(NULL);
	struct tm *tm = localtime(&t);
	char date[20];
	strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", tm);

	httpBody << "{\"to\":\"" << deviceToken << "\", \"priority\":\"high\"" << ", \"uuid\":" << pinfo.mUid
		<< ", \"send-time\":\"" << date << "\"}";
	mHttpBody = httpBody.str();
	LOGD("Push notification https post body is %s", mHttpBody.c_str());

	ostringstream httpHeader;
	httpHeader << "POST /fcm/send "
	"HTTP/1.1\r\nHost:fcm.googleapis.com\r\nContent-Type:application/json\r\nAuthorization:key="
	<< apiKey << "\r\nContent-Length:" << httpBody.str().size() << "\r\n\r\n";
	mHttpHeader = httpHeader.str();
	SLOGD << "PNR " << this << " https post header is " << mHttpHeader;
}

void FirebasePushNotificationRequest::createPushNotification() {
	int headerLength = mHttpHeader.length();
	int bodyLength = mHttpBody.length();

	mBuffer.clear();
	mBuffer.resize(headerLength + bodyLength);

	char *binaryMessageBuff = &mBuffer[0];
	char *binaryMessagePt = binaryMessageBuff;

	memcpy(binaryMessagePt, &mHttpHeader[0], headerLength);
	binaryMessagePt += headerLength;

	memcpy(binaryMessagePt, &mHttpBody[0], bodyLength);
	binaryMessagePt += bodyLength;
}

const vector<char> &FirebasePushNotificationRequest::getData() {
	createPushNotification();
	return mBuffer;
}

string FirebasePushNotificationRequest::isValidResponse(const string &str) {
	static const char expected[] = "HTTP/1.1 200";
	return strncmp(expected, str.c_str(), sizeof(expected) - 1) == 0 ? "" : "Unexpected HTTP response value (not 200 OK)";
}
<|endoftext|>