<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "testuriresolver.h"

#include <zorba/zorba.h>
#include <zorbautils/strutil.h>
#include <cassert>

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>

using namespace zorba;

zorba::TestSchemaURIResolverResult::~TestSchemaURIResolverResult()
{}

Item zorba::TestSchemaURIResolverResult::getSchema () const
{
  return theSchema;
}

zorba::TestSchemaURIResolver::TestSchemaURIResolver( const char * file ) :
  map_file ( file )
{}

zorba::TestSchemaURIResolver::~TestSchemaURIResolver() {}

void zorba::TestSchemaURIResolver::initialize ()
{
  std::string path ( map_file );
  for (int i = 0; i < 2; i++) {
    std::string::size_type slash = path.find_last_of ( '/' );
    assert (slash != std::string::npos);
    path.erase ( slash );
  }
  path += "/";
  std::string url ( "file://" );
  url += path;
  std::ifstream urifile ( map_file.c_str() );
  if ( urifile.good () == false ) return;
  std::string uris;
  while (std::getline (urifile, uris)) {
    std::string::size_type eq = uris.find ('=');
    String uri (uris.substr (0, eq));
    String file (url + uris.substr (eq+1).c_str());
    std::cout << "uri_map: " << uri << " -> " << file << std::endl;
    uri_map [uri] = file;
  }

  urifile.close();
}

std::auto_ptr<SchemaURIResolverResult>
zorba::TestSchemaURIResolver::resolve ( const Item & aURI,
				 const std::vector <Item> & aLocationHint,
				 StaticContext * aStaticContext )
  {
    if ( uri_map.empty () ) {
      initialize ();
    }
    std::auto_ptr<TestSchemaURIResolverResult>
      result ( new TestSchemaURIResolverResult () );
    String request = aURI.getStringValue ();
    std::map <String, String >::iterator it = uri_map.find ( request );
    if ( it != uri_map.end () ) {
      const String  target = uri_map [ request ];
      Zorba * zorba = Zorba::getInstance ( 0 );
      ItemFactory * factory = zorba -> getItemFactory ();
      Item item = factory -> createAnyURI ( target );
      result -> theSchema = item;

    } else {
      result -> setError ( URIResolverResult::UR_XQST0059 );
      std::stringstream lErrorStream;
      lErrorStream << "Schema not found " << aURI.getStringValue();
      std::cout << "Schema not found " << aURI.getStringValue() << std::endl;
      result->setErrorDescription(lErrorStream.str());
    }

    std::cout << "Resolved schema " << aURI.getStringValue () << " -> " << result->theSchema.getStringValue () << std::endl;

    return std::auto_ptr<SchemaURIResolverResult> ( result );
  }

std::istream *
zorba::TestModuleURIResolverResult::getModule () const
{
  return theModule;
}

zorba::TestModuleURIResolver::TestModuleURIResolver (const char * file, const std::string &test_)
  : map_file (file), theTest (test_)
{}

void zorba::TestModuleURIResolver::initialize ()
{
  std::string::size_type pos = theTest.find_first_of ('/');
  if (pos != std::string::npos && theTest.substr (0, pos) == "w3c_testsuite")
    pos = theTest.find_first_of ('/', pos + 1);
  else {
    return;
  }
  std::string pfx = theTest.substr (0, pos) + "/";

  std::string path ( map_file );
  std::string::size_type slash = path.find_last_of ( '/' );
  path.erase ( slash );
  slash = path.find_last_of ( '/' );
  path.erase ( slash );
  std::string url ( "file://" );
  std::ifstream urifile ( map_file.c_str() );
  std::string uris;
  while (std::getline ( urifile, uris )) {
    uris = pfx + uris;
    pos = uris.find_first_of (':');
    assert (pos != std::string::npos);
    if (uris.substr (0, pos) != theTest) continue;
    uris = uris.substr (pos + 1);
    std::string::size_type eq = uris.find ('=');
    String uri (uris.substr (0, eq));
    String file ( path );
    file.append ("/");
    file.append (uris.substr (eq+1).c_str());
    file.append (".xqi");
    uri_map [uri] = file;
  }

  urifile.close();
}

zorba::TestModuleURIResolver::~TestModuleURIResolver ()
{}

std::auto_ptr < ModuleURIResolverResult >
zorba::TestModuleURIResolver::
resolve ( const Item & aURI, StaticContext* aStaticContext )
{
  if ( uri_map.empty () ) {
    initialize ();
  }
  std::auto_ptr < TestModuleURIResolverResult >
    lResult ( new TestModuleURIResolverResult () );

  String request = aURI.getStringValue ();
  std::map < String, String > :: iterator it = uri_map.find ( request );
  if ( it != uri_map.end () ) {
    const String target = uri_map [ request ];
    const char * file = target.c_str();
    std::cout << "Resolved module " << aURI.getStringValue() << " -> " << file << std::endl;
    lResult -> theModule = new std::ifstream ( file );
    lResult -> setError ( URIResolverResult::UR_NOERROR );
  } else {
    lResult -> setError ( URIResolverResult::UR_XQST0059 );
    std::stringstream lErrorStream;
    lErrorStream << "Module not found " << aURI.getStringValue();
    std::cout << "Module not found " << aURI.getStringValue() << std::endl;
    lResult->setErrorDescription(lErrorStream.str());
  }
  return std::auto_ptr<ModuleURIResolverResult>(lResult.release());
}

// Collection Resolver
Collection_t
zorba::TestCollectionURIResolverResult::getCollection () const
{
  return theCollection;
}

zorba::TestCollectionURIResolver::TestCollectionURIResolver ( const char * file, const std::string& rbkt_src_dir ) :
  map_file ( file ),
  rbkt_src ( rbkt_src_dir )
{}

void
std_string_tokenize(const std::string& aStr, const std::string& aDelims, std::vector<std::string>& aTokens, const std::string& rbkt_src)
{
  std::string::size_type lastPos = aStr.find_first_not_of(aDelims, 0);
  std::string::size_type pos     = aStr.find_first_of(aDelims, lastPos);

  while (std::string::npos != pos || std::string::npos != lastPos) {
    // Found a token, add it to the vector.
    aTokens.push_back(aStr.substr(lastPos, pos - lastPos));
    zorba::str_replace_all(aTokens.back(), "$RBKT_SRC_DIR", rbkt_src);

    lastPos = aStr.find_first_not_of(aDelims, pos);
    pos = aStr.find_first_of(aDelims, lastPos);
  }
}

void zorba::TestCollectionURIResolver::trim(std::string& str)
{
  std::string::size_type pos = str.find_last_not_of(' ');
  if(pos != std::string::npos) {
    str.erase(pos + 1);
    pos = str.find_first_not_of(' ');
    if(pos != std::string::npos) str.erase(0, pos);
  }
  else str.erase(str.begin(), str.end());
}

void zorba::TestCollectionURIResolver::initialize ()
{
  std::ifstream urifile ( map_file.c_str() );
  if ( urifile.bad() ) return;

  while ( !urifile.eof() ) {
    std::string lCollectionMapping;
    std::getline( urifile, lCollectionMapping );

    if (lCollectionMapping.size() == 0) continue;

    std::string::size_type eq = lCollectionMapping.find ( '=' );
    assert(eq != std::string::npos);

    std::string id ( lCollectionMapping.substr ( 0, eq ) );
    trim(id);

    std::vector<std::string>& lCollections = uri_map[id];

    std_string_tokenize(lCollectionMapping.substr(eq+1), ";", lCollections, rbkt_src);
  }
}

zorba::TestCollectionURIResolver::~TestCollectionURIResolver ()
{}

std::auto_ptr < CollectionURIResolverResult >
zorba::TestCollectionURIResolver::
resolve ( const Item & aURI, StaticContext* aStaticContext, XmlDataManager* aXmlDataManager )
{
  if ( uri_map.empty () ) {
    initialize ();
  }
  std::auto_ptr < TestCollectionURIResolverResult >
    lResult ( new TestCollectionURIResolverResult () );

  std::string request = aURI.getStringValue ().c_str();
  std::string search  = request.substr(request.find_last_of('/')+1);
  std::map < std::string, std::vector<std::string> > :: iterator it = uri_map.find ( search );
  if ( it != uri_map.end () ) {
    const std::vector<std::string>& target = it->second;
    Collection_t lCol = aXmlDataManager->getCollection(request);
    if (lCol.isNull())  {
      lCol = aXmlDataManager->createCollection(request);
      for (std::vector<std::string>::const_iterator lIter = target.begin();
          lIter != target.end(); ++lIter) {
        std::ifstream lIn(lIter->c_str());
        assert(lIn.good());

        lCol->addDocument(lIn);
      }
    }
    lResult -> theCollection = lCol;
    lResult -> setError ( URIResolverResult::UR_NOERROR );
  } else {
    lResult -> setError ( URIResolverResult::UR_FODC0004 );
    std::stringstream lErrorStream;
    lErrorStream << "Collection not found " << aURI.getStringValue();
    lResult->setErrorDescription(lErrorStream.str());
  }
  return std::auto_ptr<CollectionURIResolverResult>(lResult.release());
}

<commit_msg>Removed debugging output, fixed segfault in testdriver resolver<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "testuriresolver.h"

#include <zorba/zorba.h>
#include <zorbautils/strutil.h>
#include <cassert>

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>

using namespace zorba;

zorba::TestSchemaURIResolverResult::~TestSchemaURIResolverResult()
{}

Item zorba::TestSchemaURIResolverResult::getSchema () const
{
  return theSchema;
}

zorba::TestSchemaURIResolver::TestSchemaURIResolver( const char * file ) :
  map_file ( file )
{}

zorba::TestSchemaURIResolver::~TestSchemaURIResolver() {}

void zorba::TestSchemaURIResolver::initialize ()
{
  std::string path ( map_file );
  for (int i = 0; i < 2; i++) {
    std::string::size_type slash = path.find_last_of ( '/' );
    assert (slash != std::string::npos);
    path.erase ( slash );
  }
  path += "/";
  std::string url ( "file://" );
  url += path;
  std::ifstream urifile ( map_file.c_str() );
  if ( urifile.good () == false ) return;
  std::string uris;
  while (std::getline (urifile, uris)) {
    std::string::size_type eq = uris.find ('=');
    String uri (uris.substr (0, eq));
    String file (url + uris.substr (eq+1).c_str());
    uri_map [uri] = file;
  }

  urifile.close();
}

std::auto_ptr<SchemaURIResolverResult>
zorba::TestSchemaURIResolver::resolve ( const Item & aURI,
				 const std::vector <Item> & aLocationHint,
				 StaticContext * aStaticContext )
  {
    if ( uri_map.empty () ) {
      initialize ();
    }
    std::auto_ptr<TestSchemaURIResolverResult>
      result ( new TestSchemaURIResolverResult () );
    String request = aURI.getStringValue ();
    std::map <String, String >::iterator it = uri_map.find ( request );
    if ( it != uri_map.end () ) {
      const String  target = uri_map [ request ];
      Zorba * zorba = Zorba::getInstance ( 0 );
      ItemFactory * factory = zorba -> getItemFactory ();
      Item item = factory -> createAnyURI ( target );
      result -> theSchema = item;
      std::cout << "Resolved schema " << aURI.getStringValue () << " -> " << result->theSchema.getStringValue () << std::endl;
    } else {
      result -> setError ( URIResolverResult::UR_XQST0059 );
      std::stringstream lErrorStream;
      lErrorStream << "Schema not found " << aURI.getStringValue();
      std::cout << "Schema not found " << aURI.getStringValue() << std::endl;
      result->setErrorDescription(lErrorStream.str());
    }

    return std::auto_ptr<SchemaURIResolverResult> ( result );
  }

std::istream *
zorba::TestModuleURIResolverResult::getModule () const
{
  return theModule;
}

zorba::TestModuleURIResolver::TestModuleURIResolver (const char * file, const std::string &test_)
  : map_file (file), theTest (test_)
{}

void zorba::TestModuleURIResolver::initialize ()
{
  std::string::size_type pos = theTest.find_first_of ('/');
  if (pos != std::string::npos && theTest.substr (0, pos) == "w3c_testsuite")
    pos = theTest.find_first_of ('/', pos + 1);
  else {
    return;
  }
  std::string pfx = theTest.substr (0, pos) + "/";

  std::string path ( map_file );
  std::string::size_type slash = path.find_last_of ( '/' );
  path.erase ( slash );
  slash = path.find_last_of ( '/' );
  path.erase ( slash );
  std::string url ( "file://" );
  std::ifstream urifile ( map_file.c_str() );
  std::string uris;
  while (std::getline ( urifile, uris )) {
    uris = pfx + uris;
    pos = uris.find_first_of (':');
    assert (pos != std::string::npos);
    if (uris.substr (0, pos) != theTest) continue;
    uris = uris.substr (pos + 1);
    std::string::size_type eq = uris.find ('=');
    String uri (uris.substr (0, eq));
    String file ( path );
    file.append ("/");
    file.append (uris.substr (eq+1).c_str());
    file.append (".xqi");
    uri_map [uri] = file;
  }

  urifile.close();
}

zorba::TestModuleURIResolver::~TestModuleURIResolver ()
{}

std::auto_ptr < ModuleURIResolverResult >
zorba::TestModuleURIResolver::
resolve ( const Item & aURI, StaticContext* aStaticContext )
{
  if ( uri_map.empty () ) {
    initialize ();
  }
  std::auto_ptr < TestModuleURIResolverResult >
    lResult ( new TestModuleURIResolverResult () );

  String request = aURI.getStringValue ();
  std::map < String, String > :: iterator it = uri_map.find ( request );
  if ( it != uri_map.end () ) {
    const String target = uri_map [ request ];
    const char * file = target.c_str();
    std::cout << "Resolved module " << aURI.getStringValue() << " -> " << file << std::endl;
    lResult -> theModule = new std::ifstream ( file );
    lResult -> setError ( URIResolverResult::UR_NOERROR );
  } else {
    lResult -> setError ( URIResolverResult::UR_XQST0059 );
    std::stringstream lErrorStream;
    lErrorStream << "Module not found " << aURI.getStringValue();
    std::cout << "Module not found " << aURI.getStringValue() << std::endl;
    lResult->setErrorDescription(lErrorStream.str());
  }
  return std::auto_ptr<ModuleURIResolverResult>(lResult.release());
}

// Collection Resolver
Collection_t
zorba::TestCollectionURIResolverResult::getCollection () const
{
  return theCollection;
}

zorba::TestCollectionURIResolver::TestCollectionURIResolver ( const char * file, const std::string& rbkt_src_dir ) :
  map_file ( file ),
  rbkt_src ( rbkt_src_dir )
{}

void
std_string_tokenize(const std::string& aStr, const std::string& aDelims, std::vector<std::string>& aTokens, const std::string& rbkt_src)
{
  std::string::size_type lastPos = aStr.find_first_not_of(aDelims, 0);
  std::string::size_type pos     = aStr.find_first_of(aDelims, lastPos);

  while (std::string::npos != pos || std::string::npos != lastPos) {
    // Found a token, add it to the vector.
    aTokens.push_back(aStr.substr(lastPos, pos - lastPos));
    zorba::str_replace_all(aTokens.back(), "$RBKT_SRC_DIR", rbkt_src);

    lastPos = aStr.find_first_not_of(aDelims, pos);
    pos = aStr.find_first_of(aDelims, lastPos);
  }
}

void zorba::TestCollectionURIResolver::trim(std::string& str)
{
  std::string::size_type pos = str.find_last_not_of(' ');
  if(pos != std::string::npos) {
    str.erase(pos + 1);
    pos = str.find_first_not_of(' ');
    if(pos != std::string::npos) str.erase(0, pos);
  }
  else str.erase(str.begin(), str.end());
}

void zorba::TestCollectionURIResolver::initialize ()
{
  std::ifstream urifile ( map_file.c_str() );
  if ( urifile.bad() ) return;

  while ( !urifile.eof() ) {
    std::string lCollectionMapping;
    std::getline( urifile, lCollectionMapping );

    if (lCollectionMapping.size() == 0) continue;

    std::string::size_type eq = lCollectionMapping.find ( '=' );
    assert(eq != std::string::npos);

    std::string id ( lCollectionMapping.substr ( 0, eq ) );
    trim(id);

    std::vector<std::string>& lCollections = uri_map[id];

    std_string_tokenize(lCollectionMapping.substr(eq+1), ";", lCollections, rbkt_src);
  }
}

zorba::TestCollectionURIResolver::~TestCollectionURIResolver ()
{}

std::auto_ptr < CollectionURIResolverResult >
zorba::TestCollectionURIResolver::
resolve ( const Item & aURI, StaticContext* aStaticContext, XmlDataManager* aXmlDataManager )
{
  if ( uri_map.empty () ) {
    initialize ();
  }
  std::auto_ptr < TestCollectionURIResolverResult >
    lResult ( new TestCollectionURIResolverResult () );

  std::string request = aURI.getStringValue ().c_str();
  std::string search  = request.substr(request.find_last_of('/')+1);
  std::map < std::string, std::vector<std::string> > :: iterator it = uri_map.find ( search );
  if ( it != uri_map.end () ) {
    const std::vector<std::string>& target = it->second;
    Collection_t lCol = aXmlDataManager->getCollection(request);
    if (lCol.isNull())  {
      lCol = aXmlDataManager->createCollection(request);
      for (std::vector<std::string>::const_iterator lIter = target.begin();
          lIter != target.end(); ++lIter) {
        std::ifstream lIn(lIter->c_str());
        assert(lIn.good());

        lCol->addDocument(lIn);
      }
    }
    lResult -> theCollection = lCol;
    lResult -> setError ( URIResolverResult::UR_NOERROR );
  } else {
    lResult -> setError ( URIResolverResult::UR_FODC0004 );
    std::stringstream lErrorStream;
    lErrorStream << "Collection not found " << aURI.getStringValue();
    lResult->setErrorDescription(lErrorStream.str());
  }
  return std::auto_ptr<CollectionURIResolverResult>(lResult.release());
}

<|endoftext|>


<commit_before>#include <catch.hpp>
#include <rapidcheck-catch.h>

#include "util/Generators.h"
#include "util/TemplateProps.h"

using namespace rc;
using namespace rc::detail;

TEST_CASE("TestCase") {
    SECTION("operator==/operator!=") {
        propConformsToEquals<TestCase>();
        PROP_REPLACE_MEMBER_INEQUAL(TestCase, size);
        PROP_REPLACE_MEMBER_INEQUAL(TestCase, seed);
    }

    SECTION("operator<<") {
        propConformsToOutputOperator<TestCase>();
    }
}

TEST_CASE("CaseResult") {
    SECTION("operator==/operator!=") {
        propConformsToEquals<CaseResult>();
        PROP_REPLACE_MEMBER_INEQUAL(CaseResult, type);
        PROP_REPLACE_MEMBER_INEQUAL(CaseResult, description);
    }

    SECTION("operator<<") {
        propConformsToOutputOperator<CaseResult>();
    }
}

TEST_CASE("SuccessResult") {
    SECTION("operator==/operator!=") {
        propConformsToEquals<SuccessResult>();
        PROP_REPLACE_MEMBER_INEQUAL(SuccessResult, numSuccess);
    }

    SECTION("operator<<") {
        propConformsToOutputOperator<SuccessResult>();
    }
}

TEST_CASE("FailureResult") {
    SECTION("operator==/operator!=") {
        propConformsToEquals<FailureResult>();
        PROP_REPLACE_MEMBER_INEQUAL(FailureResult, numSuccess);
        PROP_REPLACE_MEMBER_INEQUAL(FailureResult, failingCase);
        PROP_REPLACE_MEMBER_INEQUAL(FailureResult, description);
        PROP_REPLACE_MEMBER_INEQUAL(FailureResult, numShrinks);
        PROP_REPLACE_MEMBER_INEQUAL(FailureResult, counterExample);
    }

    SECTION("operator<<") {
        propConformsToOutputOperator<FailureResult>();
    }
}

TEST_CASE("GaveUpResult") {
    SECTION("operator==/operator!=") {
        propConformsToEquals<GaveUpResult>();
        PROP_REPLACE_MEMBER_INEQUAL(GaveUpResult, numSuccess);
        PROP_REPLACE_MEMBER_INEQUAL(GaveUpResult, description);
    }

    SECTION("operator<<") {
        propConformsToOutputOperator<GaveUpResult>();
    }
}
<commit_msg>Move ResultTests to new framework<commit_after>#include <catch.hpp>
#include <rapidcheck-catch.h>

#include "util/Generators.h"
#include "util/TemplateProps.h"

using namespace rc;
using namespace rc::detail;

TEST_CASE("TestCase") {
    SECTION("operator==/operator!=") {
        newpropConformsToEquals<TestCase>();
        NEWPROP_REPLACE_MEMBER_INEQUAL(TestCase, size);
        NEWPROP_REPLACE_MEMBER_INEQUAL(TestCase, seed);
    }

    SECTION("operator<<") {
        newpropConformsToOutputOperator<TestCase>();
    }
}

TEST_CASE("CaseResult") {
    SECTION("operator==/operator!=") {
        newpropConformsToEquals<CaseResult>();
        NEWPROP_REPLACE_MEMBER_INEQUAL(CaseResult, type);
        NEWPROP_REPLACE_MEMBER_INEQUAL(CaseResult, description);
    }

    SECTION("operator<<") {
        newpropConformsToOutputOperator<CaseResult>();
    }
}

TEST_CASE("SuccessResult") {
    SECTION("operator==/operator!=") {
        newpropConformsToEquals<SuccessResult>();
        NEWPROP_REPLACE_MEMBER_INEQUAL(SuccessResult, numSuccess);
    }

    SECTION("operator<<") {
        newpropConformsToOutputOperator<SuccessResult>();
    }
}

TEST_CASE("FailureResult") {
    SECTION("operator==/operator!=") {
        newpropConformsToEquals<FailureResult>();
        NEWPROP_REPLACE_MEMBER_INEQUAL(FailureResult, numSuccess);
        NEWPROP_REPLACE_MEMBER_INEQUAL(FailureResult, failingCase);
        NEWPROP_REPLACE_MEMBER_INEQUAL(FailureResult, description);
        NEWPROP_REPLACE_MEMBER_INEQUAL(FailureResult, numShrinks);
        NEWPROP_REPLACE_MEMBER_INEQUAL(FailureResult, counterExample);
    }

    SECTION("operator<<") {
        newpropConformsToOutputOperator<FailureResult>();
    }
}

TEST_CASE("GaveUpResult") {
    SECTION("operator==/operator!=") {
        newpropConformsToEquals<GaveUpResult>();
        NEWPROP_REPLACE_MEMBER_INEQUAL(GaveUpResult, numSuccess);
        NEWPROP_REPLACE_MEMBER_INEQUAL(GaveUpResult, description);
    }

    SECTION("operator<<") {
        newpropConformsToOutputOperator<GaveUpResult>();
    }
}
<|endoftext|>


<commit_before>#include <iostream>
#include <utility>
#include <cassert>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "glt/tex_util.h"

//Swap rows of n bytes pointed to by a with those pointed to by b
//for use in doing the y-flip for images so OpenGL has them right-side up
void swap_row(unsigned char *a, unsigned char *b, size_t n){
	for (size_t i = 0; i < n; ++i){
		std::swap(a[i], b[i]);
	}
}
GLint glt::load_texture_2d(const std::string &file, int *width, int *height){
	int x, y, n;
	unsigned char *img = stbi_load(file.c_str(), &x, &y, &n, 0);
	if (!img){
		std::cout << "load_texture_2d error loading " << file
			<< " - " << stbi_failure_reason() << std::endl;
		return -1;
	}
	if (width){
		*width = x;
	}
	if (height){
		*height = y;
	}
	GLenum format;
	switch (n){
		case 1:
			format = GL_RED;
			break;
		case 2:
			format = GL_RG;
			break;
		case 3:
			format = GL_RGB;
			break;
		case 4:
			format = GL_RGBA;
			break;
	}
	for (int i = 0; i < y / 2; ++i){
		swap_row(&img[i * x * n], &img[(y - i - 1) * x * n], x * n);
	}
	GLuint tex;
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, format, x, y, 0, format, GL_UNSIGNED_BYTE, img);
	glGenerateMipmap(GL_TEXTURE_2D);
	stbi_image_free(img);
	return tex;
}
GLint glt::load_texture_2d_array(const std::vector<std::string> &files, int *width, int *height){
	assert(!files.empty());
	int x, y, n;
	std::vector<unsigned char*> images;
	//We need to load the first image to get the dimensions and format we're loading
	images.push_back(stbi_load(files.front().c_str(), &x, &y, &n, 0));
	for (const auto &f : files){
		int ix, iy, in;
		unsigned char *im = stbi_load(f.c_str(), &ix, &iy, &in, 0);
		bool error = false;
		if (!im){
			std::cout << "load_texture_2d_array error loading " << f
				<< " - " << stbi_failure_reason() << std::endl;
			error = true;
		}
		if (x != ix || y != iy || n != in){
			std::cout << "load_texture_2d_array error: incompatible file types found on "
				<< f << std::endl;
			error = true;
		}
		if (error){
			for (auto i : images){
				stbi_image_free(i);
			}
			return -1;
		}
		images.push_back(im);
	}
	if (width){
		*width = x;
	}
	if (height){
		*height = y;
	}
	//Perform y-swap on each loaded image
	for (auto img : images){
		for (int i = 0; i < y / 2; ++i){
			swap_row(&img[i * x * n], &img[(y - i - 1) * x * n], x * n);
		}
	}
	GLenum format;
	switch (n){
		case 1:
			format = GL_RED;
			break;
		case 2:
			format = GL_RG;
			break;
		case 3:
			format = GL_RGB;
			break;
		case 4:
			format = GL_RGBA;
			break;
	}
	GLuint tex;
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
	glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, format, x, y, images.size(), 0, format, GL_UNSIGNED_BYTE, NULL);
	//Upload all the textures in the array
	for (size_t i = 0; i < images.size(); ++i){
		glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, x, y, 1, format, GL_UNSIGNED_BYTE, images.at(i));
	}
	glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
	//Clean up all the image data
	for (auto i : images){
		stbi_image_free(i);
	}
	return tex;
}
GLint glt::load_cubemap(const std::vector<std::string> &files, int *width, int *height){
	assert(files.size() == 6);
	int x, y, n;
	std::vector<unsigned char*> images;
	//We need to load the first image to get the dimensions and format we're loading
	images.push_back(stbi_load(files.front().c_str(), &x, &y, &n, 0));
	for (const auto &f : files){
		int ix, iy, in;
		unsigned char *im = stbi_load(f.c_str(), &ix, &iy, &in, 0);
		bool error = false;
		if (!im){
			std::cout << "load_cubemap error loading " << f
				<< " - " << stbi_failure_reason() << std::endl;
			error = true;
		}
		if (x != ix || y != iy || n != in){
			std::cout << "load_cubemap error: incompatible file types found on "
				<< f << std::endl;
			error = true;
		}
		if (error){
			for (auto i : images){
				stbi_image_free(i);
			}
			return -1;
		}
		images.push_back(im);
	}
	if (width){
		*width = x;
	}
	if (height){
		*height = y;
	}
	//Perform y-swap on each loaded image
	for (auto img : images){
		for (int i = 0; i < y / 2; ++i){
			swap_row(&img[i * x * n], &img[(y - i - 1) * x * n], x * n);
		}
	}
	GLenum format;
	switch (n){
		case 1:
			format = GL_RED;
			break;
		case 2:
			format = GL_RG;
			break;
		case 3:
			format = GL_RGB;
			break;
		case 4:
			format = GL_RGBA;
			break;
	}
	GLuint tex;
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
	//Upload all the textures in the array
	for (size_t i = 0; i < images.size(); ++i){
		glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, x, y, 0, format,
				GL_UNSIGNED_BYTE, images[i]);
	}
	glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
	//Clean up all the image data
	for (auto i : images){
		stbi_image_free(i);
	}
	return tex;
}

<commit_msg>Error checking on loading the first cubemap face too<commit_after>#include <iostream>
#include <utility>
#include <cassert>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "glt/tex_util.h"

//Swap rows of n bytes pointed to by a with those pointed to by b
//for use in doing the y-flip for images so OpenGL has them right-side up
void swap_row(unsigned char *a, unsigned char *b, size_t n){
	for (size_t i = 0; i < n; ++i){
		std::swap(a[i], b[i]);
	}
}
GLint glt::load_texture_2d(const std::string &file, int *width, int *height){
	int x, y, n;
	unsigned char *img = stbi_load(file.c_str(), &x, &y, &n, 0);
	if (!img){
		std::cout << "load_texture_2d error loading " << file
			<< " - " << stbi_failure_reason() << std::endl;
		return -1;
	}
	if (width){
		*width = x;
	}
	if (height){
		*height = y;
	}
	GLenum format;
	switch (n){
		case 1:
			format = GL_RED;
			break;
		case 2:
			format = GL_RG;
			break;
		case 3:
			format = GL_RGB;
			break;
		case 4:
			format = GL_RGBA;
			break;
	}
	for (int i = 0; i < y / 2; ++i){
		swap_row(&img[i * x * n], &img[(y - i - 1) * x * n], x * n);
	}
	GLuint tex;
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, format, x, y, 0, format, GL_UNSIGNED_BYTE, img);
	glGenerateMipmap(GL_TEXTURE_2D);
	stbi_image_free(img);
	return tex;
}
GLint glt::load_texture_2d_array(const std::vector<std::string> &files, int *width, int *height){
	assert(!files.empty());
	int x, y, n;
	std::vector<unsigned char*> images;
	//We need to load the first image to get the dimensions and format we're loading
	images.push_back(stbi_load(files.front().c_str(), &x, &y, &n, 0));
	for (const auto &f : files){
		int ix, iy, in;
		unsigned char *im = stbi_load(f.c_str(), &ix, &iy, &in, 0);
		bool error = false;
		if (!im){
			std::cout << "load_texture_2d_array error loading " << f
				<< " - " << stbi_failure_reason() << std::endl;
			error = true;
		}
		if (x != ix || y != iy || n != in){
			std::cout << "load_texture_2d_array error: incompatible file types found on "
				<< f << std::endl;
			error = true;
		}
		if (error){
			for (auto i : images){
				stbi_image_free(i);
			}
			return -1;
		}
		images.push_back(im);
	}
	if (width){
		*width = x;
	}
	if (height){
		*height = y;
	}
	//Perform y-swap on each loaded image
	for (auto img : images){
		for (int i = 0; i < y / 2; ++i){
			swap_row(&img[i * x * n], &img[(y - i - 1) * x * n], x * n);
		}
	}
	GLenum format;
	switch (n){
		case 1:
			format = GL_RED;
			break;
		case 2:
			format = GL_RG;
			break;
		case 3:
			format = GL_RGB;
			break;
		case 4:
			format = GL_RGBA;
			break;
	}
	GLuint tex;
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
	glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, format, x, y, images.size(), 0, format, GL_UNSIGNED_BYTE, NULL);
	//Upload all the textures in the array
	for (size_t i = 0; i < images.size(); ++i){
		glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, x, y, 1, format, GL_UNSIGNED_BYTE, images.at(i));
	}
	glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
	//Clean up all the image data
	for (auto i : images){
		stbi_image_free(i);
	}
	return tex;
}
GLint glt::load_cubemap(const std::vector<std::string> &files, int *width, int *height){
	assert(files.size() == 6);
	int x, y, n;
	std::vector<unsigned char*> images;
	//We need to load the first image to get the dimensions and format we're loading
	unsigned char *im = stbi_load(files.front().c_str(), &x, &y, &n, 0);
	if (!im){
		std::cout << "load_cubemap error loading " << files.front()
			<< " - " << stbi_failure_reason() << std::endl;
		return -1;
	}
	images.push_back(im);
	for (const auto &f : files){
		int ix, iy, in;
		unsigned char *im = stbi_load(f.c_str(), &ix, &iy, &in, 0);
		bool error = false;
		if (!im){
			std::cout << "load_cubemap error loading " << f
				<< " - " << stbi_failure_reason() << std::endl;
			error = true;
		}
		if (x != ix || y != iy || n != in){
			std::cout << "load_cubemap error: incompatible file types found on "
				<< f << std::endl;
			error = true;
		}
		if (error){
			for (auto i : images){
				stbi_image_free(i);
			}
			return -1;
		}
		images.push_back(im);
	}
	if (width){
		*width = x;
	}
	if (height){
		*height = y;
	}
	//Perform y-swap on each loaded image
	for (auto img : images){
		for (int i = 0; i < y / 2; ++i){
			swap_row(&img[i * x * n], &img[(y - i - 1) * x * n], x * n);
		}
	}
	GLenum format;
	switch (n){
		case 1:
			format = GL_RED;
			break;
		case 2:
			format = GL_RG;
			break;
		case 3:
			format = GL_RGB;
			break;
		case 4:
			format = GL_RGBA;
			break;
	}
	GLuint tex;
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
	//Upload all the textures in the array
	for (size_t i = 0; i < images.size(); ++i){
		glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, x, y, 0, format,
				GL_UNSIGNED_BYTE, images[i]);
	}
	glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
	//Clean up all the image data
	for (auto i : images){
		stbi_image_free(i);
	}
	return tex;
}

<|endoftext|>


<commit_before>#include <archive.h>
#include <archive_entry.h>

#include "serialise.hh"
#include "tarfile.hh"

namespace nix {

static int callback_open(struct archive *, void * self)
{
    return ARCHIVE_OK;
}

static ssize_t callback_read(struct archive * archive, void * _self, const void * * buffer)
{
    auto self = (TarArchive *) _self;
    *buffer = self->buffer.data();

    try {
        return self->source->read((char *) self->buffer.data(), 4096);
    } catch (EndOfFile &) {
        return 0;
    } catch (std::exception & err) {
        archive_set_error(archive, EIO, "Source threw exception: %s", err.what());
        return -1;
    }
}

static int callback_close(struct archive *, void * self)
{
    return ARCHIVE_OK;
}

void TarArchive::check(int err, const std::string & reason)
{
    if (err == ARCHIVE_EOF)
        throw EndOfFile("reached end of archive");
    else if (err != ARCHIVE_OK)
        throw Error(reason, archive_error_string(this->archive));
}

TarArchive::TarArchive(Source & source, bool raw)
    : source(&source), buffer(4096)
{
    init();
    if (!raw)
        archive_read_support_format_all(archive);
    else
        archive_read_support_format_raw(archive);
    check(archive_read_open(archive, (void *)this, callback_open, callback_read, callback_close), "Failed to open archive (%s)");
}

TarArchive::TarArchive(const Path & path)
{
    init();
    archive_read_support_format_all(archive);
    check(archive_read_open_filename(archive, path.c_str(), 16384), "failed to open archive: %s");
}

void TarArchive::init()
{
    archive = archive_read_new();
    archive_read_support_filter_all(archive);
}

void TarArchive::close()
{
    check(archive_read_close(this->archive), "Failed to close archive (%s)");
}

TarArchive::~TarArchive()
{
    if (this->archive) archive_read_free(this->archive);
}

static void extract_archive(TarArchive & archive, const Path & destDir)
{
    int flags = ARCHIVE_EXTRACT_FFLAGS
        | ARCHIVE_EXTRACT_PERM
        | ARCHIVE_EXTRACT_TIME
        | ARCHIVE_EXTRACT_SECURE_SYMLINKS
        | ARCHIVE_EXTRACT_SECURE_NODOTDOT;

    for (;;) {
        struct archive_entry * entry;
        int r = archive_read_next_header(archive.archive, &entry);
        if (r == ARCHIVE_EOF) break;
        auto name = archive_entry_pathname(entry);
        if (!name)
            throw Error("cannot get archive member name: %s", archive_error_string(archive.archive));
        if (r == ARCHIVE_WARN)
            warn(archive_error_string(archive.archive));
        else
            archive.check(r);

        archive_entry_set_pathname(entry,
            (destDir + "/" + name).c_str());

        archive.check(archive_read_extract(archive.archive, entry, flags));
    }

    archive.close();
}

void unpackTarfile(Source & source, const Path & destDir)
{
    auto archive = TarArchive(source);

    createDirs(destDir);
    extract_archive(archive, destDir);
}

void unpackTarfile(const Path & tarFile, const Path & destDir)
{
    auto archive = TarArchive(tarFile);

    createDirs(destDir);
    extract_archive(archive, destDir);
}

}
<commit_msg>extract_archive: use copy_pathname instead of set_pathname.<commit_after>#include <archive.h>
#include <archive_entry.h>

#include "serialise.hh"
#include "tarfile.hh"

namespace nix {

static int callback_open(struct archive *, void * self)
{
    return ARCHIVE_OK;
}

static ssize_t callback_read(struct archive * archive, void * _self, const void * * buffer)
{
    auto self = (TarArchive *) _self;
    *buffer = self->buffer.data();

    try {
        return self->source->read((char *) self->buffer.data(), 4096);
    } catch (EndOfFile &) {
        return 0;
    } catch (std::exception & err) {
        archive_set_error(archive, EIO, "Source threw exception: %s", err.what());
        return -1;
    }
}

static int callback_close(struct archive *, void * self)
{
    return ARCHIVE_OK;
}

void TarArchive::check(int err, const std::string & reason)
{
    if (err == ARCHIVE_EOF)
        throw EndOfFile("reached end of archive");
    else if (err != ARCHIVE_OK)
        throw Error(reason, archive_error_string(this->archive));
}

TarArchive::TarArchive(Source & source, bool raw)
    : source(&source), buffer(4096)
{
    init();
    if (!raw)
        archive_read_support_format_all(archive);
    else
        archive_read_support_format_raw(archive);
    check(archive_read_open(archive, (void *)this, callback_open, callback_read, callback_close), "Failed to open archive (%s)");
}

TarArchive::TarArchive(const Path & path)
{
    init();
    archive_read_support_format_all(archive);
    check(archive_read_open_filename(archive, path.c_str(), 16384), "failed to open archive: %s");
}

void TarArchive::init()
{
    archive = archive_read_new();
    archive_read_support_filter_all(archive);
}

void TarArchive::close()
{
    check(archive_read_close(this->archive), "Failed to close archive (%s)");
}

TarArchive::~TarArchive()
{
    if (this->archive) archive_read_free(this->archive);
}

static void extract_archive(TarArchive & archive, const Path & destDir)
{
    int flags = ARCHIVE_EXTRACT_FFLAGS
        | ARCHIVE_EXTRACT_PERM
        | ARCHIVE_EXTRACT_TIME
        | ARCHIVE_EXTRACT_SECURE_SYMLINKS
        | ARCHIVE_EXTRACT_SECURE_NODOTDOT;

    for (;;) {
        struct archive_entry * entry;
        int r = archive_read_next_header(archive.archive, &entry);
        if (r == ARCHIVE_EOF) break;
        auto name = archive_entry_pathname(entry);
        if (!name)
            throw Error("cannot get archive member name: %s", archive_error_string(archive.archive));
        if (r == ARCHIVE_WARN)
            warn(archive_error_string(archive.archive));
        else
            archive.check(r);

        archive_entry_copy_pathname(entry,
            (destDir + "/" + name).c_str());

        archive.check(archive_read_extract(archive.archive, entry, flags));
    }

    archive.close();
}

void unpackTarfile(Source & source, const Path & destDir)
{
    auto archive = TarArchive(source);

    createDirs(destDir);
    extract_archive(archive, destDir);
}

void unpackTarfile(const Path & tarFile, const Path & destDir)
{
    auto archive = TarArchive(tarFile);

    createDirs(destDir);
    extract_archive(archive, destDir);
}

}
<|endoftext|>


<commit_before>//===----------------------------------------------------------------------===//
//
//                         Peloton
//
// functions_test.cpp
//
// Identification: test/function/functions_test.cpp
//
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "common/harness.h"
#include "sql/testing_sql_util.h"
#include "concurrency/transaction_manager_factory.h"
#include "catalog/catalog.h"
#include "catalog/proc_catalog.h"
#include "catalog/language_catalog.h"
#include "type/ephemeral_pool.h"

namespace peloton {
namespace test {

class FunctionsTests : public PelotonTest {
 public:
  static type::Value TestFunc(UNUSED_ATTRIBUTE const std::vector<type::Value>& args) {
    return type::ValueFactory::GetIntegerValue(0);
  }
};

TEST_F(FunctionsTests, CatalogTest) {
  auto catalog = catalog::Catalog::GetInstance();
  catalog->Bootstrap();
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();

  auto pg_language = catalog::LanguageCatalog::GetInstance();

  // test "internal" language
  auto txn = txn_manager.BeginTransaction();
  auto internal_oid = pg_language->GetLanguageOid("internal", txn);
  EXPECT_NE(INVALID_OID, internal_oid);
  auto name = pg_language->GetLanguageName(internal_oid, txn);
  EXPECT_EQ("internal", name);

  // test add/del language
  type::EphemeralPool pool;
  std::string lanname = "foo_lang";
  pg_language->InsertLanguage(lanname, &pool, txn);
  auto oid = pg_language->GetLanguageOid(lanname, txn);
  EXPECT_NE(INVALID_OID, oid);
  name = pg_language->GetLanguageName(oid, txn);
  EXPECT_EQ(lanname, name);
  pg_language->DeleteLanguage(lanname, txn);
  oid = pg_language->GetLanguageOid(lanname, txn);
  EXPECT_EQ(INVALID_OID, oid);

  txn_manager.CommitTransaction(txn);

  auto pg_proc = catalog::ProcCatalog::GetInstance();

  // test pg_proc
  txn = txn_manager.BeginTransaction();
  std::string func_name = "test_func";
  std::vector<type::TypeId> arg_types{type::TypeId::VARCHAR, type::TypeId::INTEGER};

  catalog->AddFunction(func_name, arg_types, type::TypeId::INTEGER,
                       internal_oid, "TestFunc", TestFunc, txn);

  oid_t prolang = pg_proc->GetProLang(func_name, arg_types, txn);
  EXPECT_EQ(internal_oid, prolang);
  type::TypeId ret_type = pg_proc->GetProRetType(func_name, arg_types, txn);
  EXPECT_EQ(type::TypeId::INTEGER, ret_type);
  std::string func = pg_proc->GetProSrc(func_name, arg_types, txn);
  EXPECT_EQ("TestFunc", func);

  txn_manager.CommitTransaction(txn);

  auto func_data = catalog->GetFunction(func_name, arg_types);
  EXPECT_EQ(ret_type, func_data.return_type_);
  EXPECT_EQ(TestFunc, func_data.func_ptr_);
}

TEST_F(FunctionsTests, FuncCallTest) {
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
  txn_manager.CommitTransaction(txn);

  TestingSQLUtil::ExecuteSQLQuery(
      "CREATE TABLE test(a DECIMAL, s VARCHAR);");

  TestingSQLUtil::ExecuteSQLQuery(
      "INSERT INTO test VALUES (4.0, 'abc');");

  std::vector<StatementResult> result;
  std::vector<FieldInfo> tuple_descriptor;
  std::string error_message;
  int rows_affected;

  TestingSQLUtil::ExecuteSQLQuery(
      "SELECT SQRT(a), SUBSTR(s,1,2) FROM test;", result,
      tuple_descriptor, rows_affected, error_message);
  EXPECT_EQ(1, result[0].second.size());
  EXPECT_EQ('2', result[0].second[0]);
  EXPECT_EQ(2, result[1].second.size());
  EXPECT_EQ(result[1].second[0], 'a');
  EXPECT_EQ(result[1].second[1], 'b');

  TestingSQLUtil::ExecuteSQLQuery(
      "SELECT ASCII(s) FROM test;", result,
      tuple_descriptor, rows_affected, error_message);
  EXPECT_EQ(2, result[0].second.size());
  EXPECT_EQ('9', result[0].second[0]);
  EXPECT_EQ('7', result[0].second[1]);

  // free the database just created
  txn = txn_manager.BeginTransaction();
  catalog::Catalog::GetInstance()->DropDatabaseWithName(DEFAULT_DB_NAME, txn);
  txn_manager.CommitTransaction(txn);
}

}  // namespace test
}  // namespace peloton
<commit_msg>pointer comparision in test<commit_after>//===----------------------------------------------------------------------===//
//
//                         Peloton
//
// functions_test.cpp
//
// Identification: test/function/functions_test.cpp
//
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "common/harness.h"
#include "sql/testing_sql_util.h"
#include "concurrency/transaction_manager_factory.h"
#include "catalog/catalog.h"
#include "catalog/proc_catalog.h"
#include "catalog/language_catalog.h"
#include "type/ephemeral_pool.h"

namespace peloton {
namespace test {

class FunctionsTests : public PelotonTest {
 public:
  static type::Value TestFunc(UNUSED_ATTRIBUTE const std::vector<type::Value>& args) {
    return type::ValueFactory::GetIntegerValue(0);
  }
};

TEST_F(FunctionsTests, CatalogTest) {
  auto catalog = catalog::Catalog::GetInstance();
  catalog->Bootstrap();
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();

  auto pg_language = catalog::LanguageCatalog::GetInstance();

  // test "internal" language
  auto txn = txn_manager.BeginTransaction();
  auto internal_oid = pg_language->GetLanguageOid("internal", txn);
  EXPECT_NE(INVALID_OID, internal_oid);
  auto name = pg_language->GetLanguageName(internal_oid, txn);
  EXPECT_EQ("internal", name);

  // test add/del language
  type::EphemeralPool pool;
  std::string lanname = "foo_lang";
  pg_language->InsertLanguage(lanname, &pool, txn);
  auto oid = pg_language->GetLanguageOid(lanname, txn);
  EXPECT_NE(INVALID_OID, oid);
  name = pg_language->GetLanguageName(oid, txn);
  EXPECT_EQ(lanname, name);
  pg_language->DeleteLanguage(lanname, txn);
  oid = pg_language->GetLanguageOid(lanname, txn);
  EXPECT_EQ(INVALID_OID, oid);

  txn_manager.CommitTransaction(txn);

  auto pg_proc = catalog::ProcCatalog::GetInstance();

  // test pg_proc
  txn = txn_manager.BeginTransaction();
  std::string func_name = "test_func";
  std::vector<type::TypeId> arg_types{type::TypeId::VARCHAR, type::TypeId::INTEGER};

  catalog->AddFunction(func_name, arg_types, type::TypeId::INTEGER,
                       internal_oid, "TestFunc", TestFunc, txn);

  oid_t prolang = pg_proc->GetProLang(func_name, arg_types, txn);
  EXPECT_EQ(internal_oid, prolang);
  type::TypeId ret_type = pg_proc->GetProRetType(func_name, arg_types, txn);
  EXPECT_EQ(type::TypeId::INTEGER, ret_type);
  std::string func = pg_proc->GetProSrc(func_name, arg_types, txn);
  EXPECT_EQ("TestFunc", func);

  txn_manager.CommitTransaction(txn);

  auto func_data = catalog->GetFunction(func_name, arg_types);
  EXPECT_EQ(ret_type, func_data.return_type_);
  EXPECT_EQ((int64_t)TestFunc, (int64_t)func_data.func_ptr_);
}

TEST_F(FunctionsTests, FuncCallTest) {
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
  txn_manager.CommitTransaction(txn);

  TestingSQLUtil::ExecuteSQLQuery(
      "CREATE TABLE test(a DECIMAL, s VARCHAR);");

  TestingSQLUtil::ExecuteSQLQuery(
      "INSERT INTO test VALUES (4.0, 'abc');");

  std::vector<StatementResult> result;
  std::vector<FieldInfo> tuple_descriptor;
  std::string error_message;
  int rows_affected;

  TestingSQLUtil::ExecuteSQLQuery(
      "SELECT SQRT(a), SUBSTR(s,1,2) FROM test;", result,
      tuple_descriptor, rows_affected, error_message);
  EXPECT_EQ(1, result[0].second.size());
  EXPECT_EQ('2', result[0].second[0]);
  EXPECT_EQ(2, result[1].second.size());
  EXPECT_EQ(result[1].second[0], 'a');
  EXPECT_EQ(result[1].second[1], 'b');

  TestingSQLUtil::ExecuteSQLQuery(
      "SELECT ASCII(s) FROM test;", result,
      tuple_descriptor, rows_affected, error_message);
  EXPECT_EQ(2, result[0].second.size());
  EXPECT_EQ('9', result[0].second[0]);
  EXPECT_EQ('7', result[0].second[1]);

  // free the database just created
  txn = txn_manager.BeginTransaction();
  catalog::Catalog::GetInstance()->DropDatabaseWithName(DEFAULT_DB_NAME, txn);
  txn_manager.CommitTransaction(txn);
}

}  // namespace test
}  // namespace peloton
<|endoftext|>


<commit_before>#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"

#include "GraphDirected.h"

#include <map>
#include <utility>
using namespace std;

TEST_CASE( "GraphDirected", "[GraphDirected]" ) {

  GraphDirected<int> _Optimizer;

  map< pair< int, int >, int > _Connections;
  _Connections.emplace( std::pair<int,int>(0,1), 1 );
  _Connections.emplace( std::pair<int,int>(1,2), 1 );
  _Connections.emplace( std::pair<int,int>(2,0), 1 );
  _Connections.emplace( std::pair<int,int>(0,3), 1 );
  _Connections.emplace( std::pair<int,int>(3,4), 1 );
  _Connections.emplace( std::pair<int,int>(4,1), 1 );
  _Connections.emplace( std::pair<int,int>(1,0), 1 );
  _Connections.emplace( std::pair<int,int>(1,0), 1 );
  _Connections.emplace( std::pair<int,int>(2,6), 1 );
  _Connections.emplace( std::pair<int,int>(1,6), 1 );
  _Connections.emplace( std::pair<int,int>(6,6), 0 );
  _Connections.emplace( std::pair<int,int>(7,4), 0 );

  _Optimizer.GenerateGraphFromWeightMap( _Connections );

  SECTION( "Shortest Path" ) {
    vector< int > _Path;
    int _Dist;
    _Optimizer.GetShortestPath( 0, 6, _Path, _Dist  );
    REQUIRE( 3 == _Path.size() );
    CHECK( 2 == _Dist );
    CHECK( 0 == _Path[0] );
    CHECK( 1 == _Path[1] );
    CHECK( 6 == _Path[2] );
  }

  SECTION( "Cycle" ) {
    vector< int > _Path;
    int _Dist;
    bool bRet = _Optimizer.GetShortestPath( 4, 4, _Path, _Dist  );
    REQUIRE( true == bRet );
    REQUIRE( 5 == _Path.size() );
    CHECK( 4 == _Dist );
    CHECK( 4 == _Path[0] );
    CHECK( 1 == _Path[1] );
    CHECK( 0 == _Path[2] );
    CHECK( 3 == _Path[3] );
    CHECK( 4 == _Path[4] );
  }

  SECTION( "Self" ) {
    vector< int > _Path;
    int _Dist;
    bool bRet = _Optimizer.GetShortestPath( 6, 6, _Path, _Dist  );
    REQUIRE( true == bRet );
    REQUIRE( 1 == _Path.size() );
    CHECK( 0 == _Dist );
    CHECK( 6 == _Path[0] );
  }

}
<commit_msg>replace emplace with insert<commit_after>#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"

#include "GraphDirected.h"

#include <map>
#include <utility>
using namespace std;

TEST_CASE( "GraphDirected", "[GraphDirected]" ) {

  GraphDirected<int> _Optimizer;

  map< pair< int, int >, int > _Connections;
  _Connections.insert( make_pair( std::pair<int,int>(0,1), 1 ) );
  _Connections.insert( make_pair( std::pair<int,int>(1,2), 1 ) );
  _Connections.insert( make_pair( std::pair<int,int>(2,0), 1 ) );
  _Connections.insert( make_pair( std::pair<int,int>(0,3), 1 ) );
  _Connections.insert( make_pair( std::pair<int,int>(3,4), 1 ) );
  _Connections.insert( make_pair( std::pair<int,int>(4,1), 1 ) );
  _Connections.insert( make_pair( std::pair<int,int>(1,0), 1 ) );
  _Connections.insert( make_pair( std::pair<int,int>(1,0), 1 ) );
  _Connections.insert( make_pair( std::pair<int,int>(2,6), 1 ) );
  _Connections.insert( make_pair( std::pair<int,int>(1,6), 1 ) );
  _Connections.insert( make_pair( std::pair<int,int>(6,6), 0 ) );
  _Connections.insert( make_pair( std::pair<int,int>(7,4), 0 ) );

  _Optimizer.GenerateGraphFromWeightMap( _Connections );

  SECTION( "Shortest Path" ) {
    vector< int > _Path;
    int _Dist;
    _Optimizer.GetShortestPath( 0, 6, _Path, _Dist  );
    REQUIRE( 3 == _Path.size() );
    CHECK( 2 == _Dist );
    CHECK( 0 == _Path[0] );
    CHECK( 1 == _Path[1] );
    CHECK( 6 == _Path[2] );
  }

  SECTION( "Cycle" ) {
    vector< int > _Path;
    int _Dist;
    bool bRet = _Optimizer.GetShortestPath( 4, 4, _Path, _Dist  );
    REQUIRE( true == bRet );
    REQUIRE( 5 == _Path.size() );
    CHECK( 4 == _Dist );
    CHECK( 4 == _Path[0] );
    CHECK( 1 == _Path[1] );
    CHECK( 0 == _Path[2] );
    CHECK( 3 == _Path[3] );
    CHECK( 4 == _Path[4] );
  }

  SECTION( "Self" ) {
    vector< int > _Path;
    int _Dist;
    bool bRet = _Optimizer.GetShortestPath( 6, 6, _Path, _Dist  );
    REQUIRE( true == bRet );
    REQUIRE( 1 == _Path.size() );
    CHECK( 0 == _Dist );
    CHECK( 6 == _Path[0] );
  }

}
<|endoftext|>


<commit_before>#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <thread>
#include <iomanip>
#include <chrono>

#include <vision/nxtcam.h>
#include "stereo_config.h"
#include "delta_robot_args.h"

using namespace ev3dev;
using namespace std;

static cl::arg<vector3i> pid(
  vector3i(5000, 5000, 100),
  cl::name("pid"),
  cl::desc("PID parameters Kp, Ki, Kd."));

static delta_robot_args delta_geometry("", "Delta robot geometry");

static stereo_config stereo;

static cl::arg<float> sample_rate(
  30.0f,
  cl::name("sample-rate"),
  cl::desc("Frequency of camera observation samples, in Hz."));

static cl::arg<float> scale(
  1.0f,
  cl::name("scale"),
  cl::desc("Ratio of robot movement to object movement."));

int main(int argc, const char **argv) {
  cl::parse(argv[0], argc - 1, argv + 1);

  // Reduce clutter of insignificant digits.
  cout << fixed << showpoint << setprecision(3);
  cerr << fixed << showpoint << setprecision(3);

  nxtcam nxtcam0(port_to_i2c_path(stereo.cam0.port));
  nxtcam nxtcam1(port_to_i2c_path(stereo.cam1.port));
  cout << "Cameras:" << endl;
  cout << nxtcam0.device_id() << " " << nxtcam0.version() << " (" << nxtcam0.vendor_id() << ")" << endl;
  cout << nxtcam1.device_id() << " " << nxtcam1.version() << " (" << nxtcam1.vendor_id() << ")" << endl;

  thread nxtcam_init_thread([&] () {
    nxtcam0.track_objects();
    nxtcam1.track_objects();
    cout << "Tracking objects..." << endl;
  });

  // Initialize the delta robot.
  delta_robot delta(delta_geometry.geometry());
  if (scale > 0.0f) {
    // Set the motor parameters.
    delta.set_pid(pid->x, pid->y, pid->z);
    delta.init();

    // Bask in the glory of the calibration result for a moment.
    this_thread::sleep_for(chrono::milliseconds(500));
  }

  nxtcam_init_thread.join();

  pair<vector3f, float> volume = delta.get_volume();

  cameraf cam0, cam1;
  tie(cam0, cam1) = stereo.cameras();

  float baseline = abs(cam1.x - cam0.x);
  if (baseline < 1e-6f)
    throw runtime_error("camera baseline is zero");
  vector3f b = unit(cam1.x - cam0.x);

  // t will increment in regular intervals of T.
  typedef chrono::high_resolution_clock clock;
  auto t = clock::now();
  chrono::microseconds T(static_cast<int>(1e6f/sample_rate + 0.5f));

  vector3f origin(0.0f, 0.0f, 0.0f);

  string eraser;
  while (true) {
    nxtcam::blob_list blobs0 = nxtcam0.blobs();
    nxtcam::blob_list blobs1 = nxtcam1.blobs();

    if (blobs0.size() == 1 && blobs1.size() == 1) {
      const nxtcam::blob &b0 = blobs0.front();
      const nxtcam::blob &b1 = blobs1.front();

      vector3f x0 = cam0.sensor_to_projection(b0.center(), 1.0f) - cam0.x;
      vector3f x1 = cam1.sensor_to_projection(b1.center(), 1.0f) - cam1.x;

      // z is determined by the stereo disparity.
      float z = baseline/(dot(x0, b) - dot(x1, b));

      // Move the points from the focal plane to the (parallel) plane containing z and add the camera origins.
      x0 = x0*z + cam0.x;
      x1 = x1*z + cam1.x;
      vector3f x = (x0 + x1)/2;

      stringstream ss;
      ss << fixed << showpoint << setprecision(3);
      ss << "x=" << x << ", ||x0 - x1||=" << abs(x0 - x1);
      string msg = ss.str();
      if (msg.length() > eraser.length())
        eraser = string(msg.length(), ' ');
      cout << msg << string(eraser.size() - msg.size(), ' ');

      if (dot(origin, origin) == 0.0f)
        origin = x*scale - volume.first;

      x = x*scale - origin;

      if (scale > 0.0f) {
        try {
          // Move to the position.
          delta.set_position_setpoint(x);
        } catch(runtime_error &) {

        }
      }
    } else {
      cout << eraser;
    }
    cout << "\r";
    cout.flush();

    t += T;
    this_thread::sleep_until(t);
  }

  return 0;
}<commit_msg>More precise delta robot volume<commit_after>#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <thread>
#include <iomanip>
#include <chrono>

#include <vision/nxtcam.h>
#include "stereo_config.h"
#include "delta_robot_args.h"

using namespace ev3dev;
using namespace std;

static cl::arg<vector3i> pid(
  vector3i(5000, 5000, 100),
  cl::name("pid"),
  cl::desc("PID parameters Kp, Ki, Kd."));

static delta_robot_args delta_geometry("", "Delta robot geometry");

static stereo_config stereo;

static cl::arg<float> sample_rate(
  30.0f,
  cl::name("sample-rate"),
  cl::desc("Frequency of camera observation samples, in Hz."));

static cl::arg<float> scale(
  1.0f,
  cl::name("scale"),
  cl::desc("Ratio of robot movement to object movement."));

int main(int argc, const char **argv) {
  cl::parse(argv[0], argc - 1, argv + 1);

  // Reduce clutter of insignificant digits.
  cout << fixed << showpoint << setprecision(3);
  cerr << fixed << showpoint << setprecision(3);

  nxtcam nxtcam0(port_to_i2c_path(stereo.cam0.port));
  nxtcam nxtcam1(port_to_i2c_path(stereo.cam1.port));
  cout << "Cameras:" << endl;
  cout << nxtcam0.device_id() << " " << nxtcam0.version() << " (" << nxtcam0.vendor_id() << ")" << endl;
  cout << nxtcam1.device_id() << " " << nxtcam1.version() << " (" << nxtcam1.vendor_id() << ")" << endl;

  thread nxtcam_init_thread([&] () {
    nxtcam0.track_objects();
    nxtcam1.track_objects();
    cout << "Tracking objects..." << endl;
  });

  // Initialize the delta robot.
  delta_robot delta(delta_geometry.geometry());
  if (scale > 0.0f) {
    // Set the motor parameters.
    delta.set_pid(pid->x, pid->y, pid->z);
    delta.init();

    // Bask in the glory of the calibration result for a moment.
    this_thread::sleep_for(chrono::milliseconds(500));
  }

  nxtcam_init_thread.join();

  delta_robot::ellipse volume = delta.volume();

  cameraf cam0, cam1;
  tie(cam0, cam1) = stereo.cameras();

  float baseline = abs(cam1.x - cam0.x);
  if (baseline < 1e-6f)
    throw runtime_error("camera baseline is zero");
  vector3f b = unit(cam1.x - cam0.x);

  // t will increment in regular intervals of T.
  typedef chrono::high_resolution_clock clock;
  auto t = clock::now();
  chrono::microseconds T(static_cast<int>(1e6f/sample_rate + 0.5f));

  vector3f origin(0.0f, 0.0f, 0.0f);

  string eraser;
  while (true) {
    nxtcam::blob_list blobs0 = nxtcam0.blobs();
    nxtcam::blob_list blobs1 = nxtcam1.blobs();

    if (blobs0.size() == 1 && blobs1.size() == 1) {
      const nxtcam::blob &b0 = blobs0.front();
      const nxtcam::blob &b1 = blobs1.front();

      vector3f x0 = cam0.sensor_to_projection(b0.center(), 1.0f) - cam0.x;
      vector3f x1 = cam1.sensor_to_projection(b1.center(), 1.0f) - cam1.x;

      // z is determined by the stereo disparity.
      float z = baseline/(dot(x0, b) - dot(x1, b));

      // Move the points from the focal plane to the (parallel) plane containing z and add the camera origins.
      x0 = x0*z + cam0.x;
      x1 = x1*z + cam1.x;
      vector3f x = (x0 + x1)/2;

      stringstream ss;
      ss << fixed << showpoint << setprecision(3);
      ss << "x=" << x << ", ||x0 - x1||=" << abs(x0 - x1) << ", z=" << z;
      string msg = ss.str();
      if (msg.length() > eraser.length())
        eraser = string(msg.length(), ' ');
      cout << msg << string(eraser.size() - msg.size(), ' ');

      if (dot(origin, origin) == 0.0f)
        origin = x*scale - volume.origin;

      x = x*scale - origin;

      if (scale > 0.0f) {
        try {
          // Move to the position.
          delta.set_position_setpoint(x);
        } catch(runtime_error &) {

        }
      }
    } else {
      cout << eraser;
    }
    cout << "\r";
    cout.flush();

    t += T;
    this_thread::sleep_until(t);
  }

  return 0;
}<|endoftext|>


<commit_before>/* MergeSort.c

	Este programa realiza el algoritmo paralelizado del MergeSort para ordenar una lista desordenada, el cual consiste en:
		Dividir la lista recursivamente en sublistas de aproximadamente la mitad del tamano hasta que su tamano sea 1
		Ordenar cada sublista recursivamente usando este algoritmo
		Mezclar cada sublista hasta que se forme una sola

	El programa ordena con p procesos una lista de n valores entre 0 y m, donde
		p es el numero de procesos, lo define el usuario en "mpiexcec". Debe de ser potencia de 2
		n es el tamaÃ±o de la lista a ordenas, se los pide al usuario el proceso raiz. Debe de se divisible por p
		m es el rango de posibles valores que pueden aparecer en la lista, se los pide al usuario el proceso raiz. Debe ser <=60

	Al final se despliega
		Lista ordenada, si el usuario lo desea
		Tiempo que tardo el ordenamieto
		Valores de p, n y m
		Numero de veces que aparecia en la lista cada uno de los posibles valores del 0 a m
*/

#include <stdio.h>
#include "mpi.h"
#include <stdlib.h>

#include<iostream>
#include <chrono>
#include <ctime>

using namespace std;

int main( int argc, char *argv[] )
{
    int* lista;        /* Lista que debe ser ordenada*/
    int* lista_cantidadMenor;
    int* lista_local;
    int  n;            /*  Dimension de los 2 vectores dada por el usuario */
    int m;	       /*  Limite de numeros aleatorios de la lista */
    int  p;            /*  Numero de procesos que corren */
    int  my_rank;      /*  Identificacion de cada proceso*/
    int r;
    int t;

    int cantidadMenorTag = 11011;

    void Genera_vector(int lista[], int n, int m);   /* Genera valores aleatorios para n vector de enteros*/

    MPI_Init(&argc, &argv); /* Inicializacion del ambiente para MPI. */

    MPI_Comm_size(MPI_COMM_WORLD, &p); //Cantidad de procesos inicializados.

    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); //El rango del proceso "local".

    MPI_Comm balancer_comm; //Este comunicador sirve entre procesos que tengan el mísmo número de items.
    MPI_Group original_group;

    MPI_Comm_group(MPI_COMM_WORLD, &original_group); //

    if (my_rank == 0) {   /* LO QUE SIGUE LO REALIZA UNICAMENTE EL PROCESO 0 (PROCESO RAIZ o PRINCIPAL) */
      std::cout << "Digite el tamano de la lista\n" << endl;
      std::cin >> n;

      std::cout << "Digite el limite de valores aleatorios en la lista" << endl;
	    std::cin >> m;

      lista = (int *)malloc(n*sizeof(int));
      if(lista==NULL){
		    std::cout << "Error al asignar memoria a la lista" << endl;
		    return 0;
      }
      Genera_vector(lista, n, m);

    	for(int i=0; i < n; i++) {
        std::cout <<"parent WORLD: " << lista[i] << " ";
      }

      r = n % p;  //Restante de la division de items en la lista entre los procesos.
                  //También indica los r primero procesos a recibir t+1 items.
      t = (n - r)/p;  //Cuantos items por proceso para que tengan el mismo número de procesos (no contando los restantes).

      lista_cantidadMenor = lista+r;

      if(r != 0){
      MPI_Send (lista_cantidadMenor,n-r,MPI_INT,r,cantidadMenorTag, MPI_COMM_WORLD);  //Mandamos la lista de cantidades menores al pocesador que se convertira
                                                                                //en la raiz de su comunicador que se crea al hacer el split después.
      std::cout << "0 is done sending lista_cantidadMenor" << std::endl;
      }
      free(lista_cantidadMenor);
    }

    MPI_Bcast(&t, 1, MPI_INT, 0, MPI_COMM_WORLD);
   	MPI_Bcast(&r, 1, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);

    if(my_rank == r-1)//Soy el proceso raíz del segundo subgrupo de comm.
    {
      MPI_Recv(lista,n-r,MPI_INT,r-1,cantidadMenorTag,MPI_COMM_WORLD,MPI_STATUS_IGNORE); //Recibir la lista de cantidades menores
    }

    //Vamos a dividir los procesos en dos sub comunicadores. El de color 1 que recibe t+1 items en sus listas.
    //El de color 2 que recibe t items. Esto para garantizar una manera de recibir cualquier cantidad de n de items.

    int color = my_rank < r ? 1 : 0; //Esto es para calcular el color (identificar procesos de un comunicador) del proceso.

    //Aquí es donde se reparte los procesos según color y se identifican con el COMM handler balancer_comm.
    MPI_Comm_split(MPI_COMM_WORLD, color, my_rank, &balancer_comm);

    //Ahora, dependiendo del color, la lista será del tamaño t+1, o t.
    lista_local = (int *)malloc((t+color)*sizeof(int));

    if(r > 0){
      //Vamos a repartir la lista. Para los procesos en COMM_WORLD con rank menor a r, esto reciben t+1, para los otro t.
      MPI_Scatter(lista, t + 1, MPI_INT, lista_local, t + 1, MPI_INT, 0, balancer_comm);
      MPI_Scatter(lista, t, MPI_INT, lista_local, t, MPI_INT, r-1, balancer_comm);
    }else{
      lista_local = lista;
    }

  std::cout << "Yo soy el proceso " << my_rank << " del comunicador: "<< balancer_comm << " first: " << lista_local[0] << std::endl;

  if(my_rank == 0 || my_rank == r-1){
	   free(lista);
  }

  free(lista_local);

  MPI_Finalize();

   return 0;
}

void Genera_vector(int lista[], int n,  int m)
{
      int i;
      for (i = 0; i < n; i++) {
        //lista[i]= 0 + rand()%(m+1-0);
	         lista[i]= i;
      }
}
<commit_msg>added necessary barriers<commit_after>/* MergeSort.c

	Este programa realiza el algoritmo paralelizado del MergeSort para ordenar una lista desordenada, el cual consiste en:
		Dividir la lista recursivamente en sublistas de aproximadamente la mitad del tamano hasta que su tamano sea 1
		Ordenar cada sublista recursivamente usando este algoritmo
		Mezclar cada sublista hasta que se forme una sola

	El programa ordena con p procesos una lista de n valores entre 0 y m, donde
		p es el numero de procesos, lo define el usuario en "mpiexcec". Debe de ser potencia de 2
		n es el tamaÃ±o de la lista a ordenas, se los pide al usuario el proceso raiz. Debe de se divisible por p
		m es el rango de posibles valores que pueden aparecer en la lista, se los pide al usuario el proceso raiz. Debe ser <=60

	Al final se despliega
		Lista ordenada, si el usuario lo desea
		Tiempo que tardo el ordenamieto
		Valores de p, n y m
		Numero de veces que aparecia en la lista cada uno de los posibles valores del 0 a m
*/

#include <stdio.h>
#include "mpi.h"
#include <stdlib.h>

#include<iostream>
#include <chrono>
#include <ctime>

using namespace std;

int main( int argc, char *argv[] )
{
    int* lista;        /* Lista que debe ser ordenada*/
    int* lista_cantidadMenor;
    int* lista_local;
    int  n;            /*  Dimension de los 2 vectores dada por el usuario */
    int m;	       /*  Limite de numeros aleatorios de la lista */
    int  p;            /*  Numero de procesos que corren */
    int  my_rank;      /*  Identificacion de cada proceso*/
    int r;
    int t;

    int cantidadMenorTag = 11011;

    void Genera_vector(int lista[], int n, int m);   /* Genera valores aleatorios para n vector de enteros*/

    MPI_Init(&argc, &argv); /* Inicializacion del ambiente para MPI. */

    MPI_Comm_size(MPI_COMM_WORLD, &p); //Cantidad de procesos inicializados.

    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); //El rango del proceso "local".

    MPI_Comm balancer_comm; //Este comunicador sirve entre procesos que tengan el mísmo número de items.
    MPI_Group original_group;

    MPI_Comm_group(MPI_COMM_WORLD, &original_group); //

    if (my_rank == 0) {   /* LO QUE SIGUE LO REALIZA UNICAMENTE EL PROCESO 0 (PROCESO RAIZ o PRINCIPAL) */
      std::cout << "Digite el tamano de la lista\n" << endl;
      std::cin >> n;

      std::cout << "Digite el limite de valores aleatorios en la lista" << endl;
	    std::cin >> m;

      lista = (int *)malloc(n*sizeof(int));
      if(lista==NULL){
		    std::cout << "Error al asignar memoria a la lista" << endl;
		    return 0;
      }
      Genera_vector(lista, n, m);

    	for(int i=0; i < n; i++) {
        std::cout <<"parent WORLD: " << lista[i] << " ";
      }

      r = n % p;  //Restante de la division de items en la lista entre los procesos.
                  //También indica los r primero procesos a recibir t+1 items.
      t = (n - r)/p;  //Cuantos items por proceso para que tengan el mismo número de procesos (no contando los restantes).
      int num_elementos_to_send = n - (2*r);

      lista_cantidadMenor = lista+2*r;

      for(int i = 0; i < num_elementos_to_send; i++){
        std::cout <<"tesnting lista cantidad menor: " << lista_cantidadMenor[i] << std::endl;
      }

      if(r != 0){
      std::cout << "About to send lista_cantidadMenor to process " << r << std::endl;
      //MPI_Send (&buf,count,datatype,dest,tag,comm)
      MPI_Send (lista_cantidadMenor,num_elementos_to_send,MPI_INT,r,cantidadMenorTag, MPI_COMM_WORLD);  //Mandamos la lista de cantidades menores al pocesador que se convertira
                                                                                //en la raiz de su comunicador que se crea al hacer el split después.
      std::cout << "0 is done sending lista_cantidadMenor" << std::endl;
      }

      //free(lista_cantidadMenor);
      lista_cantidadMenor = NULL;
    }

    MPI_Bcast(&t, 1, MPI_INT, 0, MPI_COMM_WORLD);
   	MPI_Bcast(&r, 1, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Barrier(MPI_COMM_WORLD);
    if(my_rank == 0 ){std::cout << "Donde broadcasting with r = "<< r << std::endl; }

    if(my_rank == r)//Soy el proceso raíz del segundo subgrupo de comm.
    {
      std::cout << "preparing list with n = " << n << std::endl;
      int num_elementos_to_rcv = n - 2*r;

      lista = (int *)malloc(num_elementos_to_rcv*sizeof(int));
          if(lista==NULL){
            std::cout << "Error al asignar memoria a la lista" << endl;
            return 0;
          }
      std::cout << my_rank <<  " is bout to receive " << std::endl;
      //MPI_Recv (&buf,count,datatype,source,tag,comm,&status)
      MPI_Recv(lista,n-2*r,MPI_INT,0,cantidadMenorTag,MPI_COMM_WORLD,MPI_STATUS_IGNORE); //Recibir la lista de cantidades menores
      std::cout << "Done receiving, starting testing: ";
      for(int i = 0; i < n - 2*r; i++){
        std::cout << "--" << lista[i] << std::endl;
      }
      std::cout << "la lista está terminada" <<std::endl;
    }

    //Vamos a dividir los procesos en dos sub comunicadores. El de color 1 que recibe t+1 items en sus listas.
    //El de color 2 que recibe t items. Esto para garantizar una manera de recibir cualquier cantidad de n de items.

   // std::cout << "Starting to calculate color for "<< my_rank <<std::endl;

    int color = my_rank < r ? 1 : 0; //Esto es para calcular el color (identificar procesos de un comunicador) del proceso.
    std::cout << " pro "<< my_rank << "with color "<< color << std::endl;
    MPI_Barrier(MPI_COMM_WORLD);

    //Aquí es donde se reparte los procesos según color y se identifican con el COMM handler balancer_comm.
    MPI_Comm_split(MPI_COMM_WORLD, color, my_rank, &balancer_comm);
    std::cout << "After Splitiing about to allocate "<< my_rank << " with balancer: "<< balancer_comm <<std::endl;

    //Ahora, dependiendo del color, la lista será del tamaño t+1, o t.
    lista_local = (int *)malloc((t+color)*sizeof(int));
     std::cout << "Done allocating "<< my_rank <<std::endl;

     MPI_Barrier(MPI_COMM_WORLD);

    if(r > 0){
      //Vamos a repartir la lista. Para los procesos en COMM_WORLD con rank menor a r, esto reciben t+1, para los otro t.
       // MPI_Scatter (&sendbuf,sendcnt,sendtype,&recvbuf,recvcnt,recvtype,root,comm)

        MPI_Scatter(lista, t + 1, MPI_INT, lista_local, t + 1, MPI_INT, 0, balancer_comm);

      MPI_Scatter(lista, t, MPI_INT, lista_local, t, MPI_INT, 0, balancer_comm);

    }else{
      lista_local = lista;
    }

MPI_Barrier(MPI_COMM_WORLD);
  std::cout << "Yo soy el proceso " << my_rank <<  " first: " << lista_local[0] << std::endl;

  if(my_rank == 0 || my_rank == r){
	   free(lista);
  }

  free(lista_local);

  MPI_Finalize();

   return 0;
}

void Genera_vector(int lista[], int n,  int m)
{
      int i;
      for (i = 0; i < n; i++) {
        //lista[i]= 0 + rand()%(m+1-0);
	         lista[i]= i;
      }
}
<|endoftext|>


<commit_before>// Copyright 2014 BVLC and contributors.

#ifndef CAFFE_LAYER_H_
#define CAFFE_LAYER_H_

#include <vector>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"

using std::vector;

namespace caffe {

template <typename Dtype>
class Layer {
 public:
  // You should not implement your own constructor. Any set up code should go
  // to SetUp(), where the dimensions of the bottom blobs are provided to the
  // layer.
  explicit Layer(const LayerParameter& param)
    : layer_param_(param) {
      // The only thing we do is to copy blobs if there are any.
      if (layer_param_.blobs_size() > 0) {
        blobs_.resize(layer_param_.blobs_size());
        for (int i = 0; i < layer_param_.blobs_size(); ++i) {
          blobs_[i].reset(new Blob<Dtype>());
          blobs_[i]->FromProto(layer_param_.blobs(i));
        }
      }
    }
  virtual ~Layer() {}
  // SetUp: your function should implement this.
  virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) = 0;

  // Forward and backward wrappers. You should implement the cpu and
  // gpu specific implementations instead, and should not change these
  // functions.
  inline Dtype Forward(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top);
  inline void Backward(const vector<Blob<Dtype>*>& top,
      const bool propagate_down,
      vector<Blob<Dtype>*>* bottom);

  // Returns the vector of blobs.
  vector<shared_ptr<Blob<Dtype> > >& blobs() {
    return blobs_;
  }

  // Returns the layer parameter
  const LayerParameter& layer_param() { return layer_param_; }
  // Writes the layer parameter to a protocol buffer
  virtual void ToProto(LayerParameter* param, bool write_diff = false);

 protected:
  // The protobuf that stores the layer parameters
  LayerParameter layer_param_;
  // The vector that stores the parameters as a set of blobs.
  vector<shared_ptr<Blob<Dtype> > > blobs_;

  // Forward functions
  virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) = 0;
  // If no gpu code is provided, we will simply use cpu code.
  virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) {
    // LOG(WARNING) << "Using CPU code as backup.";
    return Forward_cpu(bottom, top);
  }

  // Backward functions: the backward function will compute the gradients for
  // any parameters and also for the bottom blobs if propagate_down is true.
  // It will return the loss produced from this layer.
  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const bool propagate_down,
      vector<Blob<Dtype>*>* bottom) = 0;
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const bool propagate_down,
      vector<Blob<Dtype>*>* bottom) {
    // LOG(WARNING) << "Using CPU code as backup.";
    Backward_cpu(top, propagate_down, bottom);
  }

  DISABLE_COPY_AND_ASSIGN(Layer);
};  // class Layer

// Forward and backward wrappers. You should implement the cpu and
// gpu specific implementations instead, and should not change these
// functions.
template <typename Dtype>
inline Dtype Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom,
    vector<Blob<Dtype>*>* top) {
  switch (Caffe::mode()) {
  case Caffe::CPU:
    return Forward_cpu(bottom, top);
  case Caffe::GPU:
    return Forward_gpu(bottom, top);
  default:
    LOG(FATAL) << "Unknown caffe mode.";
    return Dtype(0);
  }
}

template <typename Dtype>
inline void Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
    const bool propagate_down,
    vector<Blob<Dtype>*>* bottom) {
  switch (Caffe::mode()) {
  case Caffe::CPU:
    Backward_cpu(top, propagate_down, bottom);
    break;
  case Caffe::GPU:
    Backward_gpu(top, propagate_down, bottom);
    break;
  default:
    LOG(FATAL) << "Unknown caffe mode.";
  }
}

template <typename Dtype>
void Layer<Dtype>::ToProto(LayerParameter* param, bool write_diff) {
  param->Clear();
  param->CopyFrom(layer_param_);
  param->clear_blobs();
  for (int i = 0; i < blobs_.size(); ++i) {
    blobs_[i]->ToProto(param->add_blobs(), write_diff);
  }
}

// The layer factory function
template <typename Dtype>
Layer<Dtype>* GetLayer(const LayerParameter& param);

}  // namespace caffe

#endif  // CAFFE_LAYER_H_
<commit_msg>Comment current forward/backward responsibilities<commit_after>// Copyright 2014 BVLC and contributors.

#ifndef CAFFE_LAYER_H_
#define CAFFE_LAYER_H_

#include <vector>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"

using std::vector;

namespace caffe {

template <typename Dtype>
class Layer {
 public:
  // You should not implement your own constructor. Any set up code should go
  // to SetUp(), where the dimensions of the bottom blobs are provided to the
  // layer.
  explicit Layer(const LayerParameter& param)
    : layer_param_(param) {
      // The only thing we do is to copy blobs if there are any.
      if (layer_param_.blobs_size() > 0) {
        blobs_.resize(layer_param_.blobs_size());
        for (int i = 0; i < layer_param_.blobs_size(); ++i) {
          blobs_[i].reset(new Blob<Dtype>());
          blobs_[i]->FromProto(layer_param_.blobs(i));
        }
      }
    }
  virtual ~Layer() {}
  // SetUp: your function should implement this.
  virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) = 0;

  // Forward and backward wrappers. You should implement the cpu and
  // gpu specific implementations instead, and should not change these
  // functions.
  inline Dtype Forward(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top);
  inline void Backward(const vector<Blob<Dtype>*>& top,
      const bool propagate_down,
      vector<Blob<Dtype>*>* bottom);

  // Returns the vector of blobs.
  vector<shared_ptr<Blob<Dtype> > >& blobs() {
    return blobs_;
  }

  // Returns the layer parameter
  const LayerParameter& layer_param() { return layer_param_; }
  // Writes the layer parameter to a protocol buffer
  virtual void ToProto(LayerParameter* param, bool write_diff = false);

 protected:
  // The protobuf that stores the layer parameters
  LayerParameter layer_param_;
  // The vector that stores the parameters as a set of blobs.
  vector<shared_ptr<Blob<Dtype> > > blobs_;

  // Forward functions: compute the layer output
  // (and loss layers return the loss; other layers return the dummy value 0.)
  virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) = 0;
  // If no gpu code is provided, we will simply use cpu code.
  virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) {
    // LOG(WARNING) << "Using CPU code as backup.";
    return Forward_cpu(bottom, top);
  }

  // Backward functions: compute the gradients for any parameters and
  // for the bottom blobs if propagate_down is true.
  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const bool propagate_down,
      vector<Blob<Dtype>*>* bottom) = 0;
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const bool propagate_down,
      vector<Blob<Dtype>*>* bottom) {
    // LOG(WARNING) << "Using CPU code as backup.";
    Backward_cpu(top, propagate_down, bottom);
  }

  DISABLE_COPY_AND_ASSIGN(Layer);
};  // class Layer

// Forward and backward wrappers. You should implement the cpu and
// gpu specific implementations instead, and should not change these
// functions.
template <typename Dtype>
inline Dtype Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom,
    vector<Blob<Dtype>*>* top) {
  switch (Caffe::mode()) {
  case Caffe::CPU:
    return Forward_cpu(bottom, top);
  case Caffe::GPU:
    return Forward_gpu(bottom, top);
  default:
    LOG(FATAL) << "Unknown caffe mode.";
    return Dtype(0);
  }
}

template <typename Dtype>
inline void Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
    const bool propagate_down,
    vector<Blob<Dtype>*>* bottom) {
  switch (Caffe::mode()) {
  case Caffe::CPU:
    Backward_cpu(top, propagate_down, bottom);
    break;
  case Caffe::GPU:
    Backward_gpu(top, propagate_down, bottom);
    break;
  default:
    LOG(FATAL) << "Unknown caffe mode.";
  }
}

// Serialize LayerParameter to protocol buffer
template <typename Dtype>
void Layer<Dtype>::ToProto(LayerParameter* param, bool write_diff) {
  param->Clear();
  param->CopyFrom(layer_param_);
  param->clear_blobs();
  for (int i = 0; i < blobs_.size(); ++i) {
    blobs_[i]->ToProto(param->add_blobs(), write_diff);
  }
}

// The layer factory function
template <typename Dtype>
Layer<Dtype>* GetLayer(const LayerParameter& param);

}  // namespace caffe

#endif  // CAFFE_LAYER_H_
<|endoftext|>


<commit_before>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "config.h"
#include <cassert>
#include "stored-value.hh"

#ifndef DEFAULT_HT_SIZE
#define DEFAULT_HT_SIZE 12582917
#endif

size_t HashTable::defaultNumBuckets = DEFAULT_HT_SIZE;
size_t HashTable::defaultNumLocks = 193;
enum stored_value_type HashTable::defaultStoredValueType = featured;

static inline size_t getDefault(size_t x, size_t d) {
    return x == 0 ? d : x;
}

/**
 * Get the number of buckets for a hash table.
 *
 * @param n the desired number of buckets, if 0, use the default
 *
 * @return the number of buckets to create
 */
size_t HashTable::getNumBuckets(size_t n = 0) {
    return getDefault(n, defaultNumBuckets);
}

/**
 * Get the number of locks for a hash table.
 *
 * @param n the desired number of locks, if 0, use the default
 *
 * @return the number of locks to create
 */
size_t HashTable::getNumLocks(size_t n = 0) {
    return getDefault(n, defaultNumLocks);
}

/**
 * Set the default number of hashtable buckets.
 */
void HashTable::setDefaultNumBuckets(size_t to) {
    if (to != 0) {
        defaultNumBuckets = to;
    }
}

/**
 * Set the default number of hashtable locks.
 */
void HashTable::setDefaultNumLocks(size_t to) {
    if (to != 0) {
        defaultNumLocks = to;
    }
}

size_t HashTable::clear(bool deactivate) {
    size_t rv = 0;

    if (!deactivate) {
        // If not deactivating, assert we're already active.
        assert(active());
    }
    MultiLockHolder(mutexes, n_locks);
    if (deactivate) {
        active(false);
    }
    for (int i = 0; i < (int)size; i++) {
        while (values[i]) {
            ++rv;
            StoredValue *v = values[i];
            values[i] = v->next;
            delete v;
        }
    }

    numItems.set(0);

    return rv;
}

void HashTable::visit(HashTableVisitor &visitor) {
    if (!active()) {
        return;
    }
    VisitorTracker vt(&visitors);
    bool aborted = !visitor.shouldContinue();
    size_t visited = 0;
    for (int l = 0; active() && !aborted && l < static_cast<int>(n_locks); l++) {
        LockHolder lh(getMutex(l));
        for (int i = l; i < static_cast<int>(size); i+= n_locks) {
            assert(l == mutexForBucket(i));
            StoredValue *v = values[i];
            while (v) {
                visitor.visit(v);
                v = v->next;
            }
            ++visited;
        }
        lh.unlock();
        aborted = !visitor.shouldContinue();
    }
    assert(aborted || visited == size);
}

void HashTable::visitDepth(HashTableDepthVisitor &visitor) {
    if (!active()) {
        return;
    }
    size_t visited = 0;
    VisitorTracker vt(&visitors);

    for (int l = 0; l < static_cast<int>(n_locks); l++) {
        LockHolder lh(getMutex(l));
        for (int i = l; i < static_cast<int>(size); i+= n_locks) {
            size_t depth = 0;
            StoredValue *p = values[i];
            while (p) {
                depth++;
                p = p->next;
            }
            visitor.visit(i, depth);
            ++visited;
        }
    }

    assert(visited == size);
}

bool HashTable::setDefaultStorageValueType(const char *t) {
    bool rv = false;
    if (t && strcmp(t, "featured") == 0) {
        setDefaultStorageValueType(featured);
        rv = true;
    } else if (t && strcmp(t, "small") == 0) {
        setDefaultStorageValueType(small);
        rv = true;
    }
    return rv;
}

void HashTable::setDefaultStorageValueType(enum stored_value_type t) {
    defaultStoredValueType = t;
}

enum stored_value_type HashTable::getDefaultStorageValueType() {
    return defaultStoredValueType;
}

const char* HashTable::getDefaultStorageValueTypeStr() {
    const char *rv = "unknown";
    switch(getDefaultStorageValueType()) {
    case small: rv = "small"; break;
    case featured: rv = "featured"; break;
    default: abort();
    }
    return rv;
}

/**
 * Get the maximum amount of memory available for storing data.
 *
 * @return the memory ceiling
 */
size_t StoredValue::getMaxDataSize(EPStats& st) {
    return st.maxDataSize;
}

/**
 * Set the default number of bytes available for stored values.
 */
void StoredValue::setMaxDataSize(EPStats &st, size_t to) {
    if (to != 0) {
        st.maxDataSize = to;
    }
}

/**
 * What's the total size of allocations?
 */
size_t StoredValue::getCurrentSize(EPStats &st) {
    return st.currentSize.get() + st.memOverhead.get();
}

size_t StoredValue::getTotalCacheSize(EPStats &st) {
    return st.totalCacheSize.get();
}

void StoredValue::increaseCurrentSize(EPStats &st, size_t by, bool residentOnly) {
    if (!residentOnly) {
        st.totalCacheSize.incr(by);
    }
    st.currentSize.incr(by);
    assert(st.currentSize.get() < GIGANTOR);
}

void StoredValue::reduceCurrentSize(EPStats &st, size_t by, bool residentOnly) {
    size_t val;

    do {
        val = st.currentSize.get();
        assert(val >= by);
    } while (!st.currentSize.cas(val, val - by));;

    if (!residentOnly) {
        st.totalCacheSize.decr(by);
    }
}

/**
 * Is there enough space for this thing?
 */
bool StoredValue::hasAvailableSpace(EPStats &st, const Item &item) {
    return getCurrentSize(st) + sizeof(StoredValue) + item.getNKey() + item.getNBytes()
        <= getMaxDataSize(st);
}
<commit_msg>Don't visit empty hash tables.<commit_after>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "config.h"
#include <cassert>
#include "stored-value.hh"

#ifndef DEFAULT_HT_SIZE
#define DEFAULT_HT_SIZE 12582917
#endif

size_t HashTable::defaultNumBuckets = DEFAULT_HT_SIZE;
size_t HashTable::defaultNumLocks = 193;
enum stored_value_type HashTable::defaultStoredValueType = featured;

static inline size_t getDefault(size_t x, size_t d) {
    return x == 0 ? d : x;
}

/**
 * Get the number of buckets for a hash table.
 *
 * @param n the desired number of buckets, if 0, use the default
 *
 * @return the number of buckets to create
 */
size_t HashTable::getNumBuckets(size_t n = 0) {
    return getDefault(n, defaultNumBuckets);
}

/**
 * Get the number of locks for a hash table.
 *
 * @param n the desired number of locks, if 0, use the default
 *
 * @return the number of locks to create
 */
size_t HashTable::getNumLocks(size_t n = 0) {
    return getDefault(n, defaultNumLocks);
}

/**
 * Set the default number of hashtable buckets.
 */
void HashTable::setDefaultNumBuckets(size_t to) {
    if (to != 0) {
        defaultNumBuckets = to;
    }
}

/**
 * Set the default number of hashtable locks.
 */
void HashTable::setDefaultNumLocks(size_t to) {
    if (to != 0) {
        defaultNumLocks = to;
    }
}

size_t HashTable::clear(bool deactivate) {
    size_t rv = 0;

    if (!deactivate) {
        // If not deactivating, assert we're already active.
        assert(active());
    }
    MultiLockHolder(mutexes, n_locks);
    if (deactivate) {
        active(false);
    }
    for (int i = 0; i < (int)size; i++) {
        while (values[i]) {
            ++rv;
            StoredValue *v = values[i];
            values[i] = v->next;
            delete v;
        }
    }

    numItems.set(0);

    return rv;
}

void HashTable::visit(HashTableVisitor &visitor) {
    if (numItems.get() == 0 || !active()) {
        return;
    }
    VisitorTracker vt(&visitors);
    bool aborted = !visitor.shouldContinue();
    size_t visited = 0;
    for (int l = 0; active() && !aborted && l < static_cast<int>(n_locks); l++) {
        LockHolder lh(getMutex(l));
        for (int i = l; i < static_cast<int>(size); i+= n_locks) {
            assert(l == mutexForBucket(i));
            StoredValue *v = values[i];
            while (v) {
                visitor.visit(v);
                v = v->next;
            }
            ++visited;
        }
        lh.unlock();
        aborted = !visitor.shouldContinue();
    }
    assert(aborted || visited == size);
}

void HashTable::visitDepth(HashTableDepthVisitor &visitor) {
    if (numItems.get() == 0 || !active()) {
        return;
    }
    size_t visited = 0;
    VisitorTracker vt(&visitors);

    for (int l = 0; l < static_cast<int>(n_locks); l++) {
        LockHolder lh(getMutex(l));
        for (int i = l; i < static_cast<int>(size); i+= n_locks) {
            size_t depth = 0;
            StoredValue *p = values[i];
            while (p) {
                depth++;
                p = p->next;
            }
            visitor.visit(i, depth);
            ++visited;
        }
    }

    assert(visited == size);
}

bool HashTable::setDefaultStorageValueType(const char *t) {
    bool rv = false;
    if (t && strcmp(t, "featured") == 0) {
        setDefaultStorageValueType(featured);
        rv = true;
    } else if (t && strcmp(t, "small") == 0) {
        setDefaultStorageValueType(small);
        rv = true;
    }
    return rv;
}

void HashTable::setDefaultStorageValueType(enum stored_value_type t) {
    defaultStoredValueType = t;
}

enum stored_value_type HashTable::getDefaultStorageValueType() {
    return defaultStoredValueType;
}

const char* HashTable::getDefaultStorageValueTypeStr() {
    const char *rv = "unknown";
    switch(getDefaultStorageValueType()) {
    case small: rv = "small"; break;
    case featured: rv = "featured"; break;
    default: abort();
    }
    return rv;
}

/**
 * Get the maximum amount of memory available for storing data.
 *
 * @return the memory ceiling
 */
size_t StoredValue::getMaxDataSize(EPStats& st) {
    return st.maxDataSize;
}

/**
 * Set the default number of bytes available for stored values.
 */
void StoredValue::setMaxDataSize(EPStats &st, size_t to) {
    if (to != 0) {
        st.maxDataSize = to;
    }
}

/**
 * What's the total size of allocations?
 */
size_t StoredValue::getCurrentSize(EPStats &st) {
    return st.currentSize.get() + st.memOverhead.get();
}

size_t StoredValue::getTotalCacheSize(EPStats &st) {
    return st.totalCacheSize.get();
}

void StoredValue::increaseCurrentSize(EPStats &st, size_t by, bool residentOnly) {
    if (!residentOnly) {
        st.totalCacheSize.incr(by);
    }
    st.currentSize.incr(by);
    assert(st.currentSize.get() < GIGANTOR);
}

void StoredValue::reduceCurrentSize(EPStats &st, size_t by, bool residentOnly) {
    size_t val;

    do {
        val = st.currentSize.get();
        assert(val >= by);
    } while (!st.currentSize.cas(val, val - by));;

    if (!residentOnly) {
        st.totalCacheSize.decr(by);
    }
}

/**
 * Is there enough space for this thing?
 */
bool StoredValue::hasAvailableSpace(EPStats &st, const Item &item) {
    return getCurrentSize(st) + sizeof(StoredValue) + item.getNKey() + item.getNBytes()
        <= getMaxDataSize(st);
}
<|endoftext|>


<commit_before>#ifndef CLUE_MEMORY__
#define CLUE_MEMORY__

#include <clue/common.hpp>

namespace clue {

#if (defined(_WIN32) || defined(_WIN64)) && defined(_MSC_VER)

inline void* aligned_alloc(size_t nbytes, unsigned int alignment)
{
    void* p = ::_aligned_malloc(nbytes, alignment));
    if (!p)
    {
        throw std::bad_alloc();
    }
    return p;
}

inline void aligned_free(void *p)
{
    ::_aligned_free(p);
}

#else

inline void* aligned_alloc(size_t nbytes, unsigned int alignment)
{
    char* p = 0;
    if (::posix_memalign((void**)(&p), alignment, nbytes) != 0)
    {
        throw std::bad_alloc();
    }
    return p;
}

inline void aligned_free(void *p)
{
    ::free(p);
}

#endif

}

#endif
<commit_msg>add relevant header in memory.hpp<commit_after>#ifndef CLUE_MEMORY__
#define CLUE_MEMORY__

#include <clue/common.hpp>

#if (defined(_WIN32) || defined(_WIN64)) && defined(_MSC_VER)
#include <malloc.h>
#else
#include <stdlib.h>
#endif

namespace clue {

#if (defined(_WIN32) || defined(_WIN64)) && defined(_MSC_VER)

inline void* aligned_alloc(size_t nbytes, unsigned int alignment)
{
    void* p = ::_aligned_malloc(nbytes, alignment));
    if (!p)
    {
        throw std::bad_alloc();
    }
    return p;
}

inline void aligned_free(void *p)
{
    ::_aligned_free(p);
}

#else

inline void* aligned_alloc(size_t nbytes, unsigned int alignment)
{
    char* p = 0;
    if (::posix_memalign((void**)(&p), alignment, nbytes) != 0)
    {
        throw std::bad_alloc();
    }
    return p;
}

inline void aligned_free(void *p)
{
    ::free(p);
}

#endif

}

#endif
<|endoftext|>