text:
/*
 * Node.cpp
 *
 */

#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fstream>
#include <unistd.h>
#include <boost/thread.hpp>
#include "utils.h"
#include "Node.h"

static void throw_bad_map_file() {
	fprintf(stderr,"Node:: ERROR: could not read map file\n");
	throw std::invalid_argument( "bad map file" );
}

static void throw_bad_id(int id) {
	fprintf(stderr,"Node:: ERROR: bad id %d\n",id);
	throw std::invalid_argument( "bad id" );
}

Node::Node(const char* netmap_file, int my_id, NodeUpdatable* updatable, int num_parties)
	:_id(my_id),
	 _connected_to_servers(false),
	 _num_parties_identified(0),
	 _updatable(updatable)
{
	_parse_map(netmap_file, num_parties);
	unsigned int max_message_size = NETWORK_BUFFER_SIZE/2;
	if(_id < 0 || _id > _numparties)
		throw_bad_id(_id);
	_ready_nodes = new bool[_numparties](); //initialized to false
	_clients_connected = new bool[_numparties]();
	_server = new BIU::Server(_port, _numparties-1, this, max_message_size);
	_client = new BIU::Client(_endpoints, _numparties-1, this, max_message_size);
}

Node::~Node() {
	print_waiting();
	delete(_client);
	delete(_server);
	delete[] (_endpoints);
	delete[] (_ready_nodes);
	delete[] (_clients_connected);
}

void Node::Start() {
	_client->Connect();
	boost::thread(&Node::_start, this).join();
	_server->starter->join();
	for (unsigned int i = 0; i < _server->listeners.size(); i++)
		_server->listeners[i]->join();
}

void Node::Stop() {
	_client->Stop();
}

void Node::_start() {
	int interval = 10000;
	int total_wait = 0;
	while(true) {
		bool all_ready = true;
		if (_connected_to_servers) {
			for(int i=0; i<_numparties; i++) {
				if(i!=_id && _ready_nodes[i]==false) {
					all_ready = false;
					break;
				}
			}
		} else {
			all_ready = false;
		}
		if(all_ready)
			break;
		fprintf(stderr,"+");
//		fprintf(stderr,"Node:: waiting for all nodes to get ready ; sleeping for %u usecs\n", START_INTERVAL);
		if (interval < CONNECT_INTERVAL)
			interval *= 2;
		usleep(interval);
		total_wait += interval;
		if (total_wait > 60e6)
			throw runtime_error("waiting for too long");
	}
	printf("All identified\n");
	SendBuffer buffer;
	buffer.serialize(ALL_IDENTIFIED, strlen(ALL_IDENTIFIED));
	_client->Broadcast(buffer);
}

void Node::NewMsg(ReceivedMsg& message, struct sockaddr_in* from) {
	char* msg = message.data();
	size_t len = message.size();
#ifdef DEBUG_COMM
	printf("Node:: got message of length %u at 0x%x\n",len,msg);
	printf("from %s:%d\n", inet_ntoa(from->sin_addr), ntohs(from->sin_port));
	phex(msg, 4);
#endif
	if(len == strlen(ID_HDR)+sizeof(_id) && 0==strncmp(msg, ID_HDR, strlen(ID_HDR))) {
		int *id = (int*)(msg+strlen(ID_HDR));
		printf("Node:: identified as party: %d\n", *id);
		assert(*id >=0 && *id <= _numparties && *id !=_id && !_clients_connected[*id]);
		_clients_connected[*id] = true;
		_clientsmap.insert(std::pair<struct sockaddr_in*,int>( from, *id));
		_ready_nodes[*id] = true;
		printf("Node:: _ready_nodes[%d]=%d\n",*id,_ready_nodes[*id]);
		return;
	} else if (len == strlen(ALL_IDENTIFIED) && 0==strncmp(msg, ALL_IDENTIFIED, strlen(ALL_IDENTIFIED))) {
		printf("Node:: received ALL_IDENTIFIED from %d\n",_clientsmap[from]);
		_num_parties_identified++;
		if(_num_parties_identified == _numparties-1) {
			printf("Node:: received ALL_IDENTIFIED from ALL\n");
			_updatable->NodeReady();
		}
		return;
	}
	_updatable->NewMessage(_clientsmap[from], message);
#ifdef DEBUG_COMM
	printf("finished with %d bytes at 0x%x\n", len, msg);
	phex(msg, 4);
#endif
}

void Node::ClientsConnected() {
	printf("Node:: Clients connected!\n");
}

void Node::NodeAborted(struct sockaddr_in* from)
{
	printf("Node:: party %d has aborted\n",_clientsmap[from]);
	Stop();
}

void Node::ConnectedToServers() {
	printf("Node:: Connected to all servers!\n");
	_connected_to_servers = true;
	_identify();
}

void Node::Send(int to, SendBuffer& msg) {
	int new_recipient = to>_id?to-1:to;
#ifdef DEBUG_COMM
	printf("Node:: new_recipient=%d\n",new_recipient);
	printf("Send %d bytes at 0x%x\n", msg.size(), msg.data());
	phex(msg.data(), 4);
#endif
	_client->Send(new_recipient, msg);
}

void Node::Broadcast(SendBuffer& msg) {
#ifdef DEBUG_COMM
	printf("Broadcast %d bytes at 0x%x\n", msg.size(), msg.data());
	phex(msg.data(), 4);
#endif
	_client->Broadcast(msg);
}
void Node::Broadcast2(SendBuffer& msg) {
#ifdef DEBUG_COMM
	printf("Broadcast2 %d bytes at 0x%x\n", msg.size(), msg.data());
	phex(msg.data(), 4);
#endif
	_client->Broadcast2(msg);
}

void Node::_identify() {
	char msg[strlen(ID_HDR)+sizeof(_id)];
	memcpy(msg, ID_HDR, strlen(ID_HDR));
	memcpy(msg+strlen(ID_HDR), (const char *)&_id, sizeof(_id));
	//printf("Node:: identifying myself:\n");
	SendBuffer buffer;
	buffer.serialize(msg, strlen(ID_HDR)+4);
#ifdef DEBUG_COMM
	cout << "message for identification:";
	phex(buffer.data(), 4);
#endif
	_client->Broadcast(buffer);
}

void Node::_parse_map(const char* netmap_file, int num_parties) {
	if(LOOPBACK == netmap_file) {
		_numparties = num_parties;
		_endpoints = new endpoint_t[_numparties-1];
		int j=0;
		for(int i=0; i<_numparties; i++) {
			if(_id == i) {
				_ip = LOCALHOST_IP;
				_port = PORT_BASE + i;
				//printf("Node:: my address: %s:%d\n", _ip.c_str(),_port);
				continue;
			}
			_endpoints[j].ip = LOCALHOST_IP;
			_endpoints[j].port = PORT_BASE + i;
			j++;
		}
	}
	else {
		std::ifstream netmap(netmap_file);
		if(!netmap.good()) throw_bad_map_file();

		netmap >> _numparties;
		_endpoints = new endpoint_t[_numparties-1];
		int j=0;
		for(int i=0; i<_numparties; i++) {
			if(_id == i) {
				netmap >> _ip >> _port;
#ifdef DEBUG_NETMAP
				printf("Node:: my address: %s:%d\n", _ip.c_str(),_port);
#endif
				continue;
			}
			netmap >> _endpoints[j].ip >> _endpoints[j].port;
#ifdef DEBUG_NETMAP
				printf("Node:: other address (%d): %s:%d\n", j,
						_endpoints[j].ip.c_str(), _endpoints[j].port);
#endif
			j++;
		}
	}
}

void Node::print_waiting()
{
	for (unsigned i = 0; i < _server->timers.size(); i++)
	{
		cout << "Waited " << _server->timers[i].elapsed()
				<< " seconds for client "
				<< _clientsmap[_server->get_client_addr(i)] << endl;
	}
}

max_stars_repo_name: GnarlyMshtep/DORAM

max_issues_repo_name: GnarlyMshtep/DORAM

max_forks_repo_name: GnarlyMshtep/DORAM


text:
/*
    Copyright (c) 2019-2022 Xavier Leclercq
    Released under the MIT License
    See https://github.com/diplodocusdb/tree-db/blob/main/LICENSE.txt
*/

#include "XMLTreeDBTests.h"
#include <boost/filesystem/operations.hpp>
#include <DiplodocusDB/TreeDB/XMLTreeDB/XMLTreeDB.hpp>
#include <sstream>

using namespace boost::gregorian;
using namespace DiplodocusDB;
using namespace Ishiko;

XMLTreeDBTests::XMLTreeDBTests(const TestNumber& number, const TestContext& context)
    : TestSequence(number, "XMLTreeDB tests", context)
{
    append<HeapAllocationErrorsTest>("Creation test 1", CreationTest1);
    append<FileComparisonTest>("create test 1", CreateTest1);
    append<HeapAllocationErrorsTest>("open test 1", OpenTest1);
    append<HeapAllocationErrorsTest>("open test 2", OpenTest2);
    append<HeapAllocationErrorsTest>("open test 3", OpenTest3);
    append<HeapAllocationErrorsTest>("open test 4", OpenTest4);
    append<HeapAllocationErrorsTest>("open test 5", OpenTest5);
    append<HeapAllocationErrorsTest>("open test 6", OpenTest6);
    append<HeapAllocationErrorsTest>("open test 7", OpenTest7);
    append<HeapAllocationErrorsTest>("open test 8", OpenTest8);
    append<HeapAllocationErrorsTest>("open test 9", OpenTest9);
    append<HeapAllocationErrorsTest>("open test 10", OpenTest10);
    append<HeapAllocationErrorsTest>("parent test 1", ParentTest1);
    append<HeapAllocationErrorsTest>("parent test 2", ParentTest2);
    append<HeapAllocationErrorsTest>("childNodes test 1", ChildNodesTest1);
    append<HeapAllocationErrorsTest>("childNodes test 2", ChildNodesTest2);
    append<HeapAllocationErrorsTest>("nextSibling test 1", NextSiblingTest1);
    append<HeapAllocationErrorsTest>("nextSibling test 2", NextSiblingTest2);
    append<HeapAllocationErrorsTest>("nextSibling test 3", NextSiblingTest3);
    append<FileComparisonTest>("insertChildNode test 1", InsertChildNodeTest1);
    append<FileComparisonTest>("appendChildNode test 1", AppendChildNodeTest1);
    append<FileComparisonTest>("appendChildNode test 2", AppendChildNodeTest2);
    append<FileComparisonTest>("appendChildNode test 3", AppendChildNodeTest3);
    append<FileComparisonTest>("appendChildNode test 4", AppendChildNodeTest4);
    append<FileComparisonTest>("appendChildNode test 5", AppendChildNodeTest5);
    append<FileComparisonTest>("appendChildNode test 6", AppendChildNodeTest6);
    append<FileComparisonTest>("appendChildNode test 7", AppendChildNodeTest7);
    append<FileComparisonTest>("appendChildNode test 8", AppendChildNodeTest8);
    append<FileComparisonTest>("appendChildNode test 9", AppendChildNodeTest9);
    append<FileComparisonTest>("appendChildNode test 10", AppendChildNodeTest10);
    append<FileComparisonTest>("setChildNode test 1", SetChildNodeTest1);
    append<FileComparisonTest>("setChildNode test 2", SetChildNodeTest2);
    append<FileComparisonTest>("removeAllChildNodes test 1", RemoveAllChildNodesTest1);
}

void XMLTreeDBTests::CreationTest1(Test& test)
{
    DiplodocusDB::XMLTreeDB db;

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::CreateTest1(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_CreateTest1.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_CreateTest1.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::OpenTest1(Test& test)
{
    boost::filesystem::path inputPath("doesnotexist");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.open(inputPath, error);

    ISHIKO_TEST_FAIL_IF_NOT(error);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::OpenTest2(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "EmptyXMLTreeDB.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode& node = db.root();

    ISHIKO_TEST_FAIL_IF_NOT(node.isRoot());

    std::vector<DiplodocusDB::TreeDBNode> children = db.childNodes(node, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(children.size(), 0);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::OpenTest3(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_OpenTest3.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBValue value = db.value(node, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value.type(), DiplodocusDB::PrimitiveDataType::null);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::OpenTest4(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_OpenTest4.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBValue value = db.value(node, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value.type(), DiplodocusDB::PrimitiveDataType::null);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::OpenTest5(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_OpenTest5.xml");

    DiplodocusDB::XMLTreeDB db;

    Error error;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node1 = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBNode node2 = db.child(db.root(), "key2", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBValue value1 = db.value(node1, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value1.type(), DiplodocusDB::PrimitiveDataType::null);

    DiplodocusDB::TreeDBValue value2 = db.value(node2, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value2.type(), DiplodocusDB::PrimitiveDataType::null);

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::OpenTest6(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_OpenTest6.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBValue value = db.value(node, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value.asUTF8String(), "value1");
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::OpenTest7(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_OpenTest7.xml");

    DiplodocusDB::XMLTreeDB db;

    Error error;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node1 = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBNode node2 = db.child(db.root(), "key2", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBValue value1 = db.value(node1, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value1.asUTF8String(), "value1");

    DiplodocusDB::TreeDBValue value2 = db.value(node2, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value2.asUTF8String(), "value2");
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::OpenTest8(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_OpenTest8.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBValue value = db.value(node, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value.asUnsignedInt64(), 123);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::OpenTest9(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_OpenTest9.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBValue value = db.value(node, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value.asDouble(), 123.45);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::OpenTest10(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_OpenTest10.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBValue value = db.value(node, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value.asDate(), date(2021, 12, 25));
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::ParentTest1(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "EmptyXMLTreeDB.xml");

    DiplodocusDB::XMLTreeDB db;

    Error error;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode parent = db.parent(db.root(), error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF(parent);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::ParentTest2(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_ParentTest2.xml");

    DiplodocusDB::XMLTreeDB db;

    Error error;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode childNode = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NOT(childNode);

    DiplodocusDB::TreeDBNode parentNode = db.parent(childNode, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(parentNode, db.root());
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::ChildNodesTest1(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "EmptyXMLTreeDB.xml");

    DiplodocusDB::XMLTreeDB db;

    Error error;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    std::vector<DiplodocusDB::TreeDBNode> children = db.childNodes(db.root(), error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NOT(children.empty());
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::ChildNodesTest2(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_ChildNodesTest2.xml");

    DiplodocusDB::XMLTreeDB db;

    Error error;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF((bool)error);

    std::vector<DiplodocusDB::TreeDBNode> children = db.childNodes(db.root(), error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(children.size(), 1);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::NextSiblingTest1(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "EmptyXMLTreeDB.xml");

    DiplodocusDB::XMLTreeDB db;

    Error error;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode nextSibling = db.nextSibling(db.root(), error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF(nextSibling);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::NextSiblingTest2(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_NextSiblingTest2.xml");

    DiplodocusDB::XMLTreeDB db;

    Error error;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode key1Node = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NOT(key1Node);

    DiplodocusDB::TreeDBNode nextSibling = db.nextSibling(key1Node, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF(nextSibling);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::NextSiblingTest3(Test& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_NextSiblingTest3.xml");

    DiplodocusDB::XMLTreeDB db;

    Error error;
    db.open(inputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode key1Node = db.child(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NOT(key1Node);

    DiplodocusDB::TreeDBNode nextSibling = db.nextSibling(key1Node, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NOT(nextSibling);

    DiplodocusDB::TreeDBValue value = db.value(nextSibling, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF_NEQ(value.type(), DiplodocusDB::PrimitiveDataType::null);

    nextSibling = db.nextSibling(nextSibling, error);

    ISHIKO_TEST_FAIL_IF(error);
    ISHIKO_TEST_FAIL_IF(nextSibling);
    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::InsertChildNodeTest1(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_InsertChildNodeTest1.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.insertChildNode(db.root(), 0, "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_InsertChildNodeTest1.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::AppendChildNodeTest1(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_AppendChildNodeTest1.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.appendChildNode(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_AppendChildNodeTest1.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::AppendChildNodeTest2(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_AppendChildNodeTest2.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node1 = db.appendChildNode(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBNode node2 = db.appendChildNode(db.root(), "key2", error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_AppendChildNodeTest2.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::AppendChildNodeTest3(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_AppendChildNodeTest3.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.appendChildNode(db.root(), "key1",
        DiplodocusDB::TreeDBValue::UTF8String("value1"), error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_AppendChildNodeTest3.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::AppendChildNodeTest4(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_AppendChildNodeTest4.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node1 = db.appendChildNode(db.root(), "key1",
        DiplodocusDB::TreeDBValue::UTF8String("value1"), error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBNode node2 = db.appendChildNode(db.root(), "key2",
        DiplodocusDB::TreeDBValue::UTF8String("value2"), error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_AppendChildNodeTest4.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::AppendChildNodeTest5(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_AppendChildNodeTest5.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    for (size_t i = 0; i < 523; ++i)
    {
        std::stringstream key;
        key << "key" << i;
        DiplodocusDB::TreeDBNode node = db.appendChildNode(db.root(), key.str(), error);
        if (error)
        {
            break;
        }
    }

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_AppendChildNodeTest5.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::AppendChildNodeTest6(FileComparisonTest& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "EmptyXMLTreeDB.xml");
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_AppendChildNodeTest6.xml");

    boost::filesystem::copy_file(inputPath, outputPath, boost::filesystem::copy_option::overwrite_if_exists);

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.open(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node1 = db.appendChildNode(db.root(), "key1", error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node2 = db.appendChildNode(node1, "key2", error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_AppendChildNodeTest6.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::AppendChildNodeTest7(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_AppendChildNodeTest7.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node1 = db.appendChildNode(db.root(), "key1",
        DiplodocusDB::TreeDBValue::UTF8String("value1"), error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBNode node2 = db.appendChildNode(node1, "key2", error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_AppendChildNodeTest7.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::AppendChildNodeTest8(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_AppendChildNodeTest8.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node =
        db.appendChildNode(db.root(), "key1", DiplodocusDB::TreeDBValue::UnsignedInt64(123), error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_AppendChildNodeTest8.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::AppendChildNodeTest9(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_AppendChildNodeTest9.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.appendChildNode(db.root(), "key1", DiplodocusDB::TreeDBValue::Double(123.45),
        error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_AppendChildNodeTest9.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::AppendChildNodeTest10(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_AppendChildNodeTest10.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    TreeDBNode node = db.appendChildNode(db.root(), "key1", TreeDBValue::Date(date(2021, 12, 25)), error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_AppendChildNodeTest10.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::SetChildNodeTest1(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_SetChildNodeTest1.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node = db.setChildNode(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_SetChildNodeTest1.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::SetChildNodeTest2(FileComparisonTest& test)
{
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_SetChildNodeTest2.xml");

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.create(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    DiplodocusDB::TreeDBNode node1 = db.setChildNode(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    DiplodocusDB::TreeDBNode node2 = db.setChildNode(db.root(), "key1", error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_SetChildNodeTest2.xml");

    ISHIKO_TEST_PASS();
}

void XMLTreeDBTests::RemoveAllChildNodesTest1(FileComparisonTest& test)
{
    boost::filesystem::path inputPath(test.context().getTestDataDirectory() / "XMLTreeDBTests_RemoveAllChildNodesTest1.xml");
    boost::filesystem::path outputPath(test.context().getTestOutputDirectory() / "XMLTreeDBTests_RemoveAllChildNodesTest1.xml");

    boost::filesystem::copy_file(inputPath, outputPath, boost::filesystem::copy_option::overwrite_if_exists);

    Error error;

    DiplodocusDB::XMLTreeDB db;
    db.open(outputPath, error);

    ISHIKO_TEST_ABORT_IF(error);

    db.removeAllChildNodes(db.root(), error);

    ISHIKO_TEST_FAIL_IF(error);

    db.close();

    test.setOutputFilePath(outputPath);
    test.setReferenceFilePath(test.context().getReferenceDataDirectory() / "XMLTreeDBTests_RemoveAllChildNodesTest1.xml");

    ISHIKO_TEST_PASS();
}

ext: cpp

max_stars_repo_name: CodeSmithyIDE/TreeDB

max_issues_repo_name: CodeSmithyIDE/TreeDB

max_forks_repo_name: CodeSmithyIDE/TreeDB


text:
// The MIT License (MIT)

// Copyright (c) 2014 Mohammad Dashti
// (www.mdashti.com - mohammad.dashti [at] epfl [dot] ch - mdashti [at] gmail [dot] com)

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

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

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

#ifndef KDOUBLE_H
#define KDOUBLE_H
#include <cmath>
#include <iostream>
// #include <boost/multiprecision/cpp_dec_float.hpp>

// typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<1000, long long> > cpp_dec_float_1000;

#define DOUBLE_TYPE_STD_DOUBLE 1
#define DOUBLE_TYPE_KAHAN_DOUBLE 2
#define DOUBLE_TYPE_STD_LONG_DOUBLE 3
#define DOUBLE_TYPE_BOOST 4

// #ifndef DOUBLE_TYPE

//   //you can change the value of DOUBLE_TYPE_SYM
//   //to change the double type. Choices are:
//   // - DOUBLE_TYPE_STD_DOUBLE
//   // - DOUBLE_TYPE_KAHAN_DOUBLE
//   // - DOUBLE_TYPE_STD_LONG_DOUBLE
//   // - DOUBLE_TYPE_BOOST
//   #define DOUBLE_TYPE_SYM DOUBLE_TYPE_STD_DOUBLE

//   #if DOUBLE_TYPE_SYM == DOUBLE_TYPE_KAHAN_DOUBLE
//     #define DOUBLE_TYPE KDouble
//   #elif DOUBLE_TYPE_SYM == DOUBLE_TYPE_BOOST
//     #define DOUBLE_TYPE cpp_dec_float_1000
//   #elif DOUBLE_TYPE_SYM == DOUBLE_TYPE_STD_LONG_DOUBLE
//     #define DOUBLE_TYPE long double
//   #else
//     #define DOUBLE_TYPE double
//   #endif //DOUBLE_TYPE_SYM == DOUBLE_TYPE_KAHAN_DOUBLE
// #endif //DOUBLE_TYPE

namespace dbtoaster {

class KDouble
{
public:
  static double diff_p;
#if DOUBLE_TYPE_SYM == DOUBLE_TYPE_KAHAN_DOUBLE
private:
  double sum;
  double c;

protected:
  //friends
  friend bool operator==(const double, const KDouble &);
  friend bool operator!=(const double, const KDouble &);
  friend bool operator<(const double, const KDouble &);
  friend bool operator<=(const double, const KDouble &);
  friend bool operator>(const double, const KDouble &);
  friend bool operator>=(const double, const KDouble &);
  friend KDouble acos(const KDouble &);
  friend KDouble atan2(const KDouble &, const KDouble &);
  friend KDouble sqrt(const KDouble &);
  friend KDouble pow(const KDouble &, const KDouble &);
  // friend size_t hash_value(KDouble const &v);
  template <class T>
  friend void hash_combine(std::size_t& seed, const T& v);
  friend KDouble abs(const KDouble &dbl);

public:
  KDouble() : sum(0.0), c(0.0)
  {
  }

  KDouble(const KDouble &init) : sum(init.sum), c(init.c)
  {
  }

  KDouble(const double &init) : sum(init), c(0.0)
  {
  }

  ~KDouble()
  {
  }

  inline KDouble &operator=(const KDouble &other) { sum = other.sum; c = other.c; return *this; }
  inline KDouble &operator=(const double &other) { sum = other; c = 0.0; return *this; }
  inline KDouble &operator-=(const KDouble &other) {
    double y = -other.sum-c;
    double t = sum+y;
    c = static_cast<double>(t-sum)-y;
    sum = t;
    return *this;
  }
  inline KDouble &operator-=(const double &other) {
    double y = -other-c;
    double t = sum+y;
    c = static_cast<double>(t-sum)-y;
    sum = t;
    return *this;
  }
  inline KDouble &operator+=(const KDouble &other) {
    double y = other.sum-c;
    double t = sum+y;
    c = static_cast<double>(t-sum)-y;
    sum = t;
    return *this;
  }
  inline KDouble &operator+=(const double &other) {
    double y = other-c;
    double t = sum+y;
    c = static_cast<double>(t-sum)-y;
    sum = t;
    return *this;
  }

  //TODO Is this the right implementation?
  inline KDouble &operator*=(const KDouble &other) { sum *= other.sum; c *= other.sum; return *this; }
  inline KDouble &operator*=(const double &other) { sum *= other; c *= other; return *this; }
  inline KDouble &operator/=(const KDouble &other) { sum /= other.sum; c /= other.sum; return *this; }
  inline KDouble &operator/=(const double &other) { sum /= other; c /= other; return *this; }

  friend std::ostream &operator<<(std::ostream &out, const KDouble &kd) {
      return out << kd.sum;
  }

  //inline operator double() const { return sum; }
  // operator int() const { return static_cast<int>(sum); }
  // operator long() const { return static_cast<long>(sum); }

  inline bool operator==(const KDouble &other) const
  {
    return abs(sum-other.sum) < diff_p;
  }

  inline bool operator!=(const KDouble &other) const
  {
    return abs(sum-other.sum) >= diff_p;
  }

  // inline bool operator==(const double &other)
  // {
  //   return abs(sum-other) < diff_p;
  // }

  // inline bool operator!=(const double &other)
  // {
  //   return abs(sum-other) >= diff_p;
  // }

  inline bool operator<(const KDouble &other) const
  {
    return sum < other.sum;
  }

  inline bool operator<=(const KDouble &other) const
  {
    return sum <= other.sum;
  }

  inline bool operator>(const KDouble &other) const
  {
    return sum > other.sum;
  }

  inline bool operator>=(const KDouble &other) const
  {
    return sum >= other.sum;
  }
#endif //DOUBLE_TYPE_SYM == DOUBLE_TYPE_KAHAN_DOUBLE
};

#if DOUBLE_TYPE_SYM == DOUBLE_TYPE_KAHAN_DOUBLE

  inline bool operator==(const double sum, const KDouble & other)
  {
    return abs(sum-other.sum) < KDouble::diff_p;
  }
  inline bool operator!=(const double sum, const KDouble & other)
  {
    return abs(sum-other.sum) >= KDouble::diff_p;
  }

  inline bool operator<(const double sum, const KDouble &other)
  {
    return sum < other.sum;
  }

  inline bool operator<=(const double sum, const KDouble &other)
  {
    return sum <= other.sum;
  }

  inline bool operator>(const double sum, const KDouble &other)
  {
    return sum > other.sum;
  }

  inline bool operator>=(const double sum, const KDouble &other)
  {
    return sum >= other.sum;
  }

  inline KDouble operator-(const KDouble &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result -= g2;
      return result;
  }

  inline KDouble operator+(const KDouble &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result += g2;
      return result;
  }

  inline KDouble operator*(const KDouble &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result *= g2;
      return result;
  }

  inline KDouble operator/(const KDouble &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result /= g2;
      return result;
  }

  inline KDouble operator-(const double &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result -= g2;
      return result;
  }

  inline KDouble operator+(const double &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result += g2;
      return result;
  }

  inline KDouble operator/(const double &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result /= g2;
      return result;
  }

  inline KDouble operator-(const long &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result -= g2;
      return result;
  }

  inline KDouble operator+(const long &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result += g2;
      return result;
  }

  inline KDouble operator/(const long &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result /= g2;
      return result;
  }

  inline KDouble operator-(const int &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result -= g2;
      return result;
  }

  inline KDouble operator+(const int &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result += g2;
      return result;
  }

  inline KDouble operator*(const int &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result *= g2;
      return result;
  }

  inline KDouble operator/(const int &g1, const KDouble &g2)
  {
      KDouble result(g1);
      result /= g2;
      return result;
  }

  inline KDouble operator-(const KDouble &g1, const double &g2)
  {
      KDouble result(g1);
      result -= g2;
      return result;
  }

  inline KDouble operator+(const KDouble &g1, const double &g2)
  {
      KDouble result(g1);
      result += g2;
      return result;
  }

  inline KDouble operator*(const KDouble &g1, const double &g2)
  {
      KDouble result(g1);
      result *= g2;
      return result;
  }

  inline KDouble operator/(const KDouble &g1, const double &g2)
  {
      KDouble result(g1);
      result /= g2;
      return result;
  }

  inline KDouble operator-(const KDouble &g1, const long &g2)
  {
      KDouble result(g1);
      result -= g2;
      return result;
  }

  inline KDouble operator+(const KDouble &g1, const long &g2)
  {
      KDouble result(g1);
      result += g2;
      return result;
  }

  inline KDouble operator*(const KDouble &g1, const long &g2)
  {
      KDouble result(g1);
      result *= g2;
      return result;
  }

  inline KDouble operator/(const KDouble &g1, const long &g2)
  {
      KDouble result(g1);
      result /= g2;
      return result;
  }

  inline KDouble operator-(const KDouble &g1, const int &g2)
  {
      KDouble result(g1);
      result -= g2;
      return result;
  }

  inline KDouble operator+(const KDouble &g1, const int &g2)
  {
      KDouble result(g1);
      result += g2;
      return result;
  }

  inline KDouble operator*(const KDouble &g1, const int &g2)
  {
      KDouble result(g1);
      result *= g2;
      return result;
  }

  inline KDouble operator/(const KDouble &g1, const int &g2)
  {
      KDouble result(g1);
      result /= g2;
      return result;
  }

  inline KDouble abs(const KDouble &dbl)
  {
      KDouble result(dbl);
      if(result.sum < 0.0) result.sum = -result.sum;
      return result;
  }

  inline KDouble acos(const KDouble &dbl)
  {
      return KDouble(acos(dbl.sum));
  }

  inline KDouble atan2(const KDouble &g1, const KDouble &g2)
  {
    return KDouble(atan2(g1.sum,g2.sum));
  }

  inline KDouble sqrt(const KDouble &dbl)
  {
    return KDouble(sqrt(dbl.sum));
  }

  inline KDouble pow(const KDouble &g1, const KDouble &g2)
  {
    return KDouble(pow(g1.sum, g2.sum));
  }
#endif //DOUBLE_TYPE_SYM == DOUBLE_TYPE_KAHAN_DOUBLE
}
#endif //KDOUBLE_H

ext: hpp

max_stars_repo_name: erlfilho/dbtoaster-backend

max_issues_repo_name: erlfilho/dbtoaster-backend

max_forks_repo_name: erlfilho/dbtoaster-backend


text:
/*
@copyright Louis Dionne 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
 */

#include <boost/hana/assert.hpp>
#include <boost/hana/core/convert.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/set.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>
namespace hana = boost::hana;

int main() {
    constexpr auto xs = hana::make_tuple(hana::int_c<1>, hana::int_c<3>, hana::type_c<int>, hana::long_c<1>);
    BOOST_HANA_CONSTANT_CHECK(
        hana::to<hana::set_tag>(xs) == hana::make_set(hana::int_c<1>, hana::int_c<3>, hana::type_c<int>)
    );
}

max_stars_repo_name: qicosmos/hana

max_issues_repo_name: qicosmos/hana

max_forks_repo_name: qicosmos/hana


text:
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <math.h>
#include <omp.h>
#include <iostream>

#include "mvnormal.h"
#include "linop.h"
#include "bpmfutils.h"
#include "macauoneprior.h"

using namespace std;
using namespace Eigen;

template<class FType>
void MacauOnePrior<FType>::init(const int nlatent, std::unique_ptr<FType> &Fmat) {
  num_latent = nlatent;

  // parameters of Normal-Gamma distributions
  mu     = VectorXd::Constant(num_latent, 0.0);
  lambda = VectorXd::Constant(num_latent, 10.0);
  // their hyperparameter (lambda_0)
  l0 = 2.0;
  lambda_a0 = 1.0;
  lambda_b0 = 1.0;

  // side information
  F       = std::move(Fmat);
  F_colsq = col_square_sum(*F);

  Uhat = MatrixXd::Constant(num_latent, F->rows(), 0.0);
  beta = MatrixXd::Constant(num_latent, F->cols(), 0.0);

  // initial value (should be determined automatically)
  // Hyper-prior for lambda_beta (mean 1.0):
  lambda_beta     = VectorXd::Constant(num_latent, 5.0);
  lambda_beta_a0 = 0.1;
  lambda_beta_b0 = 0.1;
}

template<class FType>
void MacauOnePrior<FType>::sample_latents(
    Eigen::MatrixXd &U,
    const Eigen::SparseMatrix<double> &Ymat,
    double mean_value,
    const Eigen::MatrixXd &V,
    double alpha,
    const int num_latent)
{
  const int N = U.cols();
  const int D = U.rows();

#pragma omp parallel for schedule(dynamic, 4)
  for (int i = 0; i < N; i++) {

    const int nnz = Ymat.outerIndexPtr()[i + 1] - Ymat.outerIndexPtr()[i];
    VectorXd Yhat(nnz);

    // precalculating Yhat and Qi
    int idx = 0;
    VectorXd Qi = lambda;
    for (SparseMatrix<double>::InnerIterator it(Ymat, i); it; ++it, idx++) {
      Qi.noalias() += alpha * V.col(it.row()).cwiseAbs2();
      Yhat(idx)     = mean_value + U.col(i).dot( V.col(it.row()) );
    }
    VectorXd rnorms(num_latent);
    bmrandn_single(rnorms);

    for (int d = 0; d < D; d++) {
      // computing Lid
      const double uid = U(d, i);
      double Lid = lambda(d) * (mu(d) + Uhat(d, i));

      idx = 0;
      for ( SparseMatrix<double>::InnerIterator it(Ymat, i); it; ++it, idx++) {
        const double vjd = V(d, it.row());
        // L_id += alpha * (Y_ij - k_ijd) * v_jd
        Lid += alpha * (it.value() - (Yhat(idx) - uid*vjd)) * vjd;
      }
      // Now use Lid and Qid to update uid
      double uid_old = U(d, i);
      double uid_var = 1.0 / Qi(d);

      // sampling new u_id ~ Norm(Lid / Qid, 1/Qid)
      U(d, i) = Lid * uid_var + sqrt(uid_var) * rnorms(d);

      // updating Yhat
      double uid_delta = U(d, i) - uid_old;
      idx = 0;
      for (SparseMatrix<double>::InnerIterator it(Ymat, i); it; ++it, idx++) {
        Yhat(idx) += uid_delta * V(d, it.row());
      }
    }
  }
}

template<class FType>
void MacauOnePrior<FType>::sample_latents(
        ProbitNoise& noiseModel,
        TensorData & data,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples,
        int mode,
        const int num_latent)
{
  throw std::runtime_error("Unimplemented: sample_latents");
}

template<class FType>
void MacauOnePrior<FType>::sample_latents(
        double noisePrecision,
        TensorData & data,
        std::vector< std::unique_ptr<Eigen::MatrixXd> > & samples,
        int mode,
        const int num_latent)
{
  auto& sparseMode = (*data.Y)[mode];
  auto& U = samples[mode];
  const int N = U->cols();
  const int D = num_latent;
  VectorView<Eigen::MatrixXd> view(samples, mode);
  const int nmodes1 = view.size();
  const double mean_value = data.mean_value;

  if (U->rows() != num_latent) {
    throw std::runtime_error("U->rows() must be equal to num_latent.");
  }

  Eigen::VectorXi & row_ptr = sparseMode->row_ptr;
  Eigen::MatrixXi & indices = sparseMode->indices;
  Eigen::VectorXd & values  = sparseMode->values;

#pragma omp parallel for schedule(dynamic, 8)
  for (int i = 0; i < N; i++) {
    // looping over all non-zeros for row i of the mode
    // precalculating Yhat and Qi
    const int nnz = row_ptr(i + 1) - row_ptr(i);
    VectorXd Yhat(nnz);
    VectorXd tmpd(nnz);
    VectorXd Qi = lambda;

    for (int idx = 0; idx < nnz; idx++) {
      int j = idx + row_ptr(i);
      VectorXd prod = VectorXd::Ones(D);
      for (int m = 0; m < nmodes1; m++) {
        auto v = view.get(m)->col(indices(j, m));
        prod.noalias() = prod.cwiseProduct(v);
      }
      Qi.noalias() += noisePrecision * prod.cwiseAbs2();
      Yhat(idx) = mean_value + U->col(i).dot(prod);
    }

    // generating random numbers
    VectorXd rnorms(num_latent);
    bmrandn_single(rnorms);

    for (int d = 0; d < D; d++) {
      // computing Lid
      const double uid = (*U)(d, i);
      double Lid = lambda(d) * (mu(d) + Uhat(d, i));

      for (int idx = 0; idx < nnz; idx++) {
        int j = idx + row_ptr(i);

        // computing t = vjd * wkd * ..
        double t = 1.0;
        for (int m = 0; m < nmodes1; m++) {
          t *= (*view.get(m))(d, indices(j, m));
        }
        tmpd(idx) = t;
        // L_id += alpha * (Y_ijk - k_ijkd) * v_jd * wkd
        Lid += noisePrecision * (values(j) - (Yhat(idx) - uid * t)) * t;
      }
      // Now use Lid and Qid to update uid
      double uid_old = uid;
      double uid_var = 1.0 / Qi(d);

      // sampling new u_id ~ Norm(Lid / Qid, 1/Qid)
      (*U)(d, i) = Lid * uid_var + sqrt(uid_var) * rnorms(d);

      // updating Yhat
      double uid_delta = (*U)(d, i) - uid_old;
      for (int idx = 0; idx < nnz; idx++) {
        Yhat(idx) += uid_delta * tmpd(idx);
      }
    }
  }
}

template<class FType>
void MacauOnePrior<FType>::update_prior(const Eigen::MatrixXd &U) {
  sample_mu_lambda(U);
  sample_beta(U);
  compute_uhat(Uhat, *F, beta);
  sample_lambda_beta();
}

template<class FType>
void MacauOnePrior<FType>::sample_mu_lambda(const Eigen::MatrixXd &U) {
  MatrixXd Lambda(num_latent, num_latent);
  MatrixXd WI(num_latent, num_latent);
  WI.setIdentity();
  int N = U.cols();

  MatrixXd Udelta(num_latent, N);
#pragma omp parallel for schedule(static)
  for (int i = 0; i < N; i++) {
    for (int d = 0; d < num_latent; d++) {
      Udelta(d, i) = U(d, i) - Uhat(d, i);
    }
  }
  tie(mu, Lambda) = CondNormalWishart(Udelta, VectorXd::Constant(num_latent, 0.0), 2.0, WI, num_latent);
  lambda = Lambda.diagonal();
}

template<class FType>
void MacauOnePrior<FType>::sample_beta(const Eigen::MatrixXd &U) {
  // updating beta and beta_var
  const int nfeat = beta.cols();
  const int N = U.cols();
  const int blocksize = 4;

  MatrixXd Z;

#pragma omp parallel for private(Z) schedule(static, 1)
  for (int dstart = 0; dstart < num_latent; dstart += blocksize) {
    const int dcount = std::min(blocksize, num_latent - dstart);
    Z.resize(dcount, U.cols());

    for (int i = 0; i < N; i++) {
      for (int d = 0; d < dcount; d++) {
        int dx = d + dstart;
        Z(d, i) = U(dx, i) - mu(dx) - Uhat(dx, i);
      }
    }

    for (int f = 0; f < nfeat; f++) {
      VectorXd zx(dcount), delta_beta(dcount), randvals(dcount);
      // zx = Z[dstart : dstart + dcount, :] * F[:, f]
      At_mul_Bt(zx, *F, f, Z);
      // TODO: check if sampling randvals for whole [nfeat x dcount] matrix works faster
      bmrandn_single( randvals );

      for (int d = 0; d < dcount; d++) {
        int dx = d + dstart;
        double A_df     = lambda_beta(dx) + lambda(dx) * F_colsq(f);
        double B_df     = lambda(dx) * (zx(d) + beta(dx,f) * F_colsq(f));
        double A_inv    = 1.0 / A_df;
        double beta_new = B_df * A_inv + sqrt(A_inv) * randvals(d);
        delta_beta(d)   = beta(dx,f) - beta_new;

        beta(dx, f)     = beta_new;
      }
      // Z[dstart : dstart + dcount, :] += F[:, f] * delta_beta'
      add_Acol_mul_bt(Z, *F, f, delta_beta);
    }
  }
}

template<class FType>
void MacauOnePrior<FType>::sample_latents(ProbitNoise & noise, Eigen::MatrixXd &U, const Eigen::SparseMatrix<double> &mat,
                                          double mean_value, const Eigen::MatrixXd &samples, const int num_latent) {
 //TODO
 throw std::runtime_error("Not implemented!");
}

template<class FType>
void MacauOnePrior<FType>::sample_lambda_beta() {
  double lambda_beta_a = lambda_beta_a0 + beta.cols() / 2.0;
  VectorXd lambda_beta_b = VectorXd::Constant(beta.rows(), lambda_beta_b0);
  const int D = beta.rows();
  const int F = beta.cols();
#pragma omp parallel
  {
    VectorXd tmp(D);
    tmp.setZero();
#pragma omp for schedule(static)
    for (int f = 0; f < F; f++) {
      for (int d = 0; d < D; d++) {
        tmp(d) += square(beta(d, f));
      }
    }
#pragma omp critical
    {
      lambda_beta_b += tmp / 2;
    }
  }
  for (int d = 0; d < D; d++) {
    lambda_beta(d) = rgamma(lambda_beta_a, 1.0 / lambda_beta_b(d));
  }
}

template<class FType>
void MacauOnePrior<FType>::saveModel(std::string prefix) {
  writeToCSVfile(prefix + "-latentmean.csv", mu);
  writeToCSVfile(prefix + "-link.csv", beta);
}

template class MacauOnePrior<SparseFeat>;
template class MacauOnePrior<SparseDoubleFeat>;

max_stars_repo_name: jaak-s/macau

max_issues_repo_name: potatopaul/macau

max_forks_repo_name: potatopaul/macau


text:
// This file is a part of the OpenSurgSim project.
// Copyright 2013-2015, SimQuest Solutions Inc.
//
// 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 "SurgSim/Devices/ReplayPoseDevice/ReplayPoseScaffold.h"

#include <Eigen/Core>

#include <fstream>

#include "SurgSim/DataStructures/DataGroupBuilder.h"
#include "SurgSim/Devices/ReplayPoseDevice/ReplayPoseDevice.h"
#include "SurgSim/Framework/ApplicationData.h"
#include "SurgSim/Framework/Log.h"
#include "SurgSim/Framework/Runtime.h"
#include "SurgSim/Framework/SharedInstance.h"

namespace
{
/// Missing istream "operator >>" for Eigen matrix type
/// http://eigen.tuxfamily.org/bz/show_bug.cgi?id=622
template<typename Derived>
std::istream& operator >>(std::istream& s, Eigen::MatrixBase<Derived>& m)
{
	for (int i = 0; i < m.rows(); ++i)
	{
		for (int j = 0; j < m.cols(); j++)
		{
			s >> m(i, j);
		}
	}
	return s;
}
}

namespace SurgSim
{
namespace Devices
{

ReplayPoseScaffold::ReplayPoseScaffold() :
	Framework::BasicThread("ReplayPoseScaffold"),
	m_logger(Framework::Logger::getLogger("Devices/ReplayPoseScaffold"))
{
	SURGSIM_LOG_DEBUG(m_logger) << "ReplayPose scaffold created.";
}

ReplayPoseScaffold::~ReplayPoseScaffold()
{
	if (m_device != nullptr)
	{
		unregisterDevice();
	}
	SURGSIM_LOG_DEBUG(m_logger) << "ReplayPose scaffold destroyed.";
}

std::shared_ptr<ReplayPoseScaffold> ReplayPoseScaffold::getOrCreateSharedInstance()
{
	static Framework::SharedInstance<ReplayPoseScaffold> sharedInstance;
	return sharedInstance.get();
}

bool ReplayPoseScaffold::doInitialize()
{
	return true;
}

bool ReplayPoseScaffold::doStartUp()
{
	return true;
}

bool ReplayPoseScaffold::doUpdate(double dt)
{
	boost::unique_lock<boost::mutex> scopedLock(m_deviceLock);

	SURGSIM_ASSERT(m_device != nullptr) << "DeviceData not properly allocated";

	return updateDevice(m_device.get());
}

struct ReplayPoseScaffold::DeviceData
{
	/// Constructor
	/// \param device Device to be managed by this scaffold
	explicit DeviceData(ReplayPoseDevice* device) :
		deviceObject(device),
		m_timestamp(0),
		m_index(0),
		m_fileLoaded(false)
	{
		m_fileLoaded = loadFile(deviceObject->getFileName());
		m_timer.start();
	}

	/// Retrieve the pose for the given time stamp "m_timestamp"
	/// \return The requested pose, Identity if none could be loaded
	Math::RigidTransform3d getPose()
	{
		if (m_motion.size() == 0)
		{
			return Math::RigidTransform3d::Identity();
		}

		if (m_motion.size() == 1 || (m_index == 0 && m_timestamp <= m_motion[0].first))
		{
			return m_motion[0].second;
		}

		while (m_motion.size() > m_index && m_timestamp > m_motion[m_index].first)
		{
			m_index++;
		}

		// Requesting a timestamp over the higher limit recorded in the file
		if (m_motion.size() <= m_index)
		{
			return m_motion[m_motion.size() - 1].second;
		}

		// Are we requesting exactly the timestamp we just indexed
		if (m_timestamp == m_motion[m_index].first)
		{
			return m_motion[m_index].second;
		}

		// Otherwise, we need to interpolate between the index and the previous index
		double range = m_motion[m_index].first - m_motion[m_index - 1].first;
		double time = (m_timestamp - m_motion[m_index - 1].first) / range;
		return Math::interpolate(m_motion[m_index - 1].second, m_motion[m_index].second, time);
	}

	/// Device object managed by this scaffold.
	ReplayPoseDevice* const deviceObject;

	/// Timer to keep track of the time stamp and keep the replay in sync with the recording
	Framework::Timer m_timer;

	/// Time stamp for the device (which time stamp are we reproducing?)
	double m_timestamp;

	/// Series of poses through time loaded from the input file
	std::vector<std::pair<double, Math::RigidTransform3d>> m_motion;

	/// Index of the latest pose used
	size_t m_index;

	/// Valid file loaded successfully
	bool m_fileLoaded;

private:
	// Prevent copy construction and copy assignment.  (VS2012 does not support "= delete" yet.)
	DeviceData(const DeviceData&) /*= delete*/;
	DeviceData& operator=(const DeviceData&) /*= delete*/;

	/// \param fileName the file to be open and loaded
	/// \return True if the file was loaded successfully, False otherwise
	bool loadFile(const std::string& fileName)
	{
		auto logger = Framework::Logger::getLogger("Devices/ReplayPoseScaffold");

		std::ifstream inputFile;
		bool result = true;

		inputFile.open(fileName, std::ios::in);

		if (!inputFile.is_open())
		{
			std::string path;
			if (SurgSim::Framework::Runtime::getApplicationData()->tryFindFile(fileName, &path))
			{
				inputFile.open(path, std::ios::in);
			}
		}

		if (inputFile.is_open())
		{
			while (!inputFile.eof())
			{
				double timeStamp;
				Math::RigidTransform3d pose;
				inputFile >> timeStamp >> pose.matrix();
				m_motion.emplace_back(timeStamp, pose);
			}
			SURGSIM_LOG_INFO(logger) << "Loaded " << m_motion.size() << " timestamps";
			if (m_motion.size() >= 1)
			{
				SURGSIM_LOG_INFO(logger) << "The loaded timestamps cover a range of " <<
										 m_motion[m_motion.size() - 1].first - m_motion[0].first << " second(s)";
			}
			SURGSIM_LOG_IF(m_motion.size() == 0, logger, WARNING) <<
					"No poses could be properly loaded, Identity pose will be used";

			inputFile.close();
		}
		else
		{
			SURGSIM_LOG_WARNING(logger) << "Could not find or open the file " << fileName <<
				"\nReplay will use Identity pose.";
			result = false;
		}

		return result;
	}
};

bool ReplayPoseScaffold::registerDevice(ReplayPoseDevice* device)
{
	boost::unique_lock<boost::mutex> scopedLock(m_deviceLock);
	SURGSIM_ASSERT(m_device == nullptr) << "Can't register two ReplayPoseDevice.";

	m_device.reset(new ReplayPoseScaffold::DeviceData(device));
	if (m_device == nullptr)
	{
		SURGSIM_LOG_CRITICAL(m_logger) << "Failed to create a DeviceData";
		return false;
	}
	if (!m_device->m_fileLoaded)
	{
		SURGSIM_LOG_CRITICAL(m_logger) << "Failed to load the file to replay";
		return false;
	}
	SURGSIM_LOG_DEBUG(m_logger) << "Registered device " << device->getName();

	bool success = true;
	if (!isRunning())
	{
		std::shared_ptr<Framework::Barrier> barrier = std::make_shared<Framework::Barrier>(2);
		start(barrier);
		barrier->wait(true); // Wait for initialize
		barrier->wait(true); // Wait for startup
		success = isInitialized();
	}

	if (success)
	{
		setRate(device->getRate());
	}

	return success;
}

bool ReplayPoseScaffold::unregisterDevice()
{

	if (isRunning())
	{
		stop();
	}

	// #threadsafety After unregistering, another thread could be in the process of registering.
	boost::unique_lock<boost::mutex> scopedLock(m_deviceLock);
	m_device.reset();
	SURGSIM_LOG_DEBUG(m_logger) << "Unregistered device";
	return true;
}

bool ReplayPoseScaffold::updateDevice(ReplayPoseScaffold::DeviceData* info)
{
	DataStructures::DataGroup& inputData = m_device->deviceObject->getInputData();

	info->m_timer.markFrame();
	info->m_timestamp += info->m_timer.getLastFramePeriod();

	Math::RigidTransform3d pose = info->getPose();
	inputData.poses().set(DataStructures::Names::POSE, pose);

	m_device->deviceObject->pushInput();

	return true;
}

DataStructures::DataGroup ReplayPoseScaffold::buildDeviceInputData()
{
	DataStructures::DataGroupBuilder builder;
	builder.addPose(DataStructures::Names::POSE);
	return builder.createData();
}

};  // namespace Devices
};  // namespace SurgSim

max_stars_repo_name: dbungert/opensurgsim

max_issues_repo_name: dbungert/opensurgsim

max_forks_repo_name: dbungert/opensurgsim


text:
/**
 * Copyright (C) 2015 Dato, Inc.
 * All rights reserved.
 *
 * This software may be modified and distributed under the terms
 * of the BSD license. See the LICENSE file for details.
 */
/**
 * Copyright (c) 2009 Carnegie Mellon University.
 *     All rights reserved.
 *
 *  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.
 *
 * For more about this software visit:
 *
 *      http://www.graphlab.ml.cmu.edu
 *
 */

/**
 * Also contains code that is Copyright 2011 Yahoo! Inc.  All rights
 * reserved.
 *
 * Contributed under the iCLA for:
 *    Joseph Gonzalez (jegonzal@yahoo-inc.com)
 *
 */

#include <boost/unordered_map.hpp>
#include <rpc/delta_dht.hpp>

#include <graphlab/macros_def.hpp>
namespace graphlab {
  namespace delta_dht_impl {

    typedef boost::unordered_map<const void*, icache*> cache_map_type;

    void destroy_tls_data(void* ptr) {
      cache_map_type* cache_map_ptr = static_cast<cache_map_type*>(ptr);
      if(cache_map_ptr != NULL) {
        cache_map_type& cache_map = *cache_map_ptr;
        typedef cache_map_type::value_type pair_type;
        foreach(pair_type& pair, cache_map) {
          if(pair.second != NULL) {
            delete pair.second;
            pair.second = NULL;
          }
        }
        delete cache_map_ptr;
      }
    }
    struct tls_key_creator {
      pthread_key_t TLS_KEY;
      tls_key_creator() : TLS_KEY(0) {
        pthread_key_create(&TLS_KEY, destroy_tls_data);
      }
    };
    const tls_key_creator key;

    icache*& get_icache_ptr(const void* dht_ptr) {
      cache_map_type* cache_map_ptr = static_cast<cache_map_type*>
        (pthread_getspecific(key.TLS_KEY));
      if(cache_map_ptr == NULL) {
        cache_map_ptr = new cache_map_type();
        pthread_setspecific(key.TLS_KEY, cache_map_ptr);
      }
      ASSERT_NE(cache_map_ptr, NULL);
      ASSERT_NE(dht_ptr, NULL);
      return (*cache_map_ptr)[dht_ptr];
    }

  }; // end of delta dht impl
}; // end of graphlab namespace

max_stars_repo_name: parquette/ParFrame

max_issues_repo_name: parquette/ParFrame

max_forks_repo_name: parquette/ParFrame


text:
/*=============================================================================
    Copyright (c) 2006 Tobias Schwinger
    http://spirit.sourceforge.net/

  Distributed under the Boost Software License, Version 1.0. (See accompanying
  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(BOOST_SPIRIT_DYNAMIC_TYPEOF_HPP)
#define BOOST_SPIRIT_DYNAMIC_TYPEOF_HPP

#include <boost/typeof/typeof.hpp>

#include <boost/spirit/home/classic/namespace.hpp>
#include <boost/spirit/home/classic/core/typeof.hpp>

#include <boost/spirit/home/classic/dynamic/stored_rule_fwd.hpp>

namespace boost { namespace spirit {

BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN

    // if.hpp
    template <class ParsableT, typename CondT> struct if_parser;
    template <class ParsableTrueT, class ParsableFalseT, typename CondT>
    struct if_else_parser;

    // for.hpp
    namespace impl {
    template<typename InitF, typename CondT, typename StepF, class ParsableT>
    struct for_parser;
    }

    // while.hpp
    template<typename ParsableT, typename CondT, bool is_do_parser>
    struct while_parser;

    // lazy.hpp
    template<typename ActorT> struct lazy_parser;

    // rule_alias.hpp
    template <typename ParserT> class rule_alias;

    // switch.hpp
    template <typename CaseT, typename CondT>       struct switch_parser;
    template <int N, class ParserT, bool IsDefault> struct case_parser;

    // select.hpp
    template <typename TupleT, typename BehaviourT, typename T>
    struct select_parser;

BOOST_SPIRIT_CLASSIC_NAMESPACE_END

}} // namespace BOOST_SPIRIT_CLASSIC_NS

#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()

// if.hpp
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::if_parser,2)
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::if_else_parser,3)

// for.hpp
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::impl::for_parser,4)

// while.hpp
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::while_parser,(class)(class)(bool))

// lazy.hpp
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::lazy_parser,1)

// stored_rule.hpp (has forward header)
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::stored_rule,(typename)(typename)(typename)(bool))
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::stored_rule,3)
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::stored_rule,2)
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::stored_rule,1)
BOOST_TYPEOF_REGISTER_TYPE(BOOST_SPIRIT_CLASSIC_NS::stored_rule<>)

// rule_alias.hpp
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::rule_alias,1)

// switch.hpp
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::switch_parser,2)
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::case_parser,(int)(class)(bool))

// select.hpp
BOOST_TYPEOF_REGISTER_TEMPLATE(BOOST_SPIRIT_CLASSIC_NS::select_parser,3)

#endif

max_stars_repo_name: rudylee/expo

max_issues_repo_name: rudylee/expo

max_forks_repo_name: rudylee/expo


text:
// Copyright (c) 2016
// Author: Chrono Law
#include <std.hpp>
using namespace std;

#include <boost/ptr_container/ptr_vector.hpp>
using namespace boost;

///////////////////////////////////////

void case1()
{
    typedef ptr_vector<string> ptr_vec;
    ptr_vec vec;

    vec.push_back(new string("123"));
    vec.push_back(new string("abc"));

    assert(vec.front() == "123");
    assert(vec.back() == "abc");

    ptr_vec vec2;
    vec2.assign(vec.begin(), vec.end());
    assert(vec2.size() == 2);

    assert(*vec2.pop_back() == "abc");
    assert(vec2.size() == 1);

    vec.resize(5);
    assert(vec.back().empty());

    vec.resize(10, new string("xyz"));
    assert(vec.back() == "xyz");

    vec.transfer(vec.end(), vec2.begin(), vec2);
    assert(vec.size() == 11);
    assert(vec2.size() == 0);

    vec2.transfer(vec2.end(), vec.begin(),vec.begin() + 5, vec);
    assert(vec.size() == 6);
    assert(vec2.size() == 5);

    vec.transfer(vec.begin(), vec2);
    assert(vec.size() == 11);
    assert(vec2.size() == 0);

}

///////////////////////////////////////

int main()
{
    std::cout << "hello ptr_sequence_adapter" << std::endl;

    case1();
}


text:
// Boost name_generator.hpp header file  -----------------------------//

// Copyright 2010 Andy Tompkins.
// Copyright 2017 James E. King III

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

#ifndef BOOST_UUID_NAME_GENERATOR_HPP
#define BOOST_UUID_NAME_GENERATOR_HPP

#include <boost/config.hpp>
#include <boost/uuid/name_generator_sha1.hpp>

#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

namespace boost {
namespace uuids {

//! \deprecated
//! \brief this provides backwards compatibility with previous boost
//!        releases however it is now deprecated to ensure that once
//!        a new hashing algorithm is defined for name generation that
//!        there is no confusion - at that time this will be removed.
typedef name_generator_sha1 name_generator;

//! \brief this provides the latest name generator hashing algorithm
//!        regardless of boost release; if you do not need stable
//!        name generation across releases then this will suffice
typedef name_generator_sha1 name_generator_latest;

} // uuids
} // boost

#endif // BOOST_UUID_NAME_GENERATOR_HPP


text:
#include "ten/app.hh"
#include "ten/net.hh"
#include "ten/buffer.hh"
#include <unordered_map>
#include <boost/algorithm/string.hpp>

// poor-man's memcached
// http://www.darkcoding.net/software/in-memory-key-value-store-in-c-go-and-python/

using namespace ten;

struct memg_config : app_config {
    std::string listen_addr;
    bool single;
};

static memg_config conf;

class memg_server : public netsock_server {
public:
    memg_server()
        : netsock_server("memcache")
    {}

private:
    std::unordered_map<std::string, std::string> cache;

    void on_connection(netsock &s) {
        buffer buf{4*1024};
        std::stringstream ss;
        std::string line;
        ssize_t nw;
        std::string key;
        size_t value_length = 0;
        for (;;) {
            buf.reserve(4*1024);
            ssize_t nr = s.recv(buf.back(), buf.available());
            if (nr <= 0) goto done;
            buf.commit(nr);
            char *p = std::find(buf.front(), buf.back(), '\r');
            if (p == buf.back()) continue;
            std::stringstream os;
            std::string line(buf.front(), p - buf.front());
            buf.remove(line.size()+2);
            std::vector<std::string> parts;
            boost::split(parts, line, boost::is_any_of(" "));
            if (parts.empty()) continue;
            if (parts[0] == "get") {
                auto i = cache.find(parts[1]);
                if (i != cache.end()) {
                    os << "VALUE " << i->first << " 0 " << i->second.size() << "\r\n";
                    os << i->second << "\r\n";
                }
                os << "END\r\n";
                nw = s.send(os.str().data(), os.tellp());
            } else if (parts[0] == "set") {
                key = parts[1];
                value_length = boost::lexical_cast<uint32_t>(parts[4]);
                buf.reserve(value_length+2);
                while (buf.size() < value_length+2) {
                    nr = s.recv(buf.back(), buf.available());
                    if (nr <= 0) goto done;
                    buf.commit(nr);
                }
                std::string value{buf.front(), buf.front()+value_length};
                buf.remove(value_length+2);
                cache[key] = value;
                nw = s.send("STORED\r\n", 8);
            }
            (void)nw;
        }
done:
        if (conf.single) {
            kernel::shutdown();
        }
    }
};

struct state {
    std::shared_ptr<application> app;
    std::shared_ptr<memg_server> server;

    state(const std::shared_ptr<application> &app_) : app(app_) {
        server = std::make_shared<memg_server>();
    }

    state(const state &) = delete;
    state &operator =(const state &) = delete;
};

int main(int argc, char *argv[]) {
    return task::main([&] {
        std::shared_ptr<application> app = std::make_shared<application>("0.0.1", conf);
        namespace po = boost::program_options;
        app->opts.configuration.add_options()
            ("listen,L", po::value<std::string>(&conf.listen_addr)->default_value("127.0.0.1:11211"),
            "listen address:port")
            ("single", po::value<bool>(&conf.single)->zero_tokens(), "single connection")
        ;
        app->parse_args(argc, argv);

        std::string addr = conf.listen_addr;
        uint16_t port = 11211;
        parse_host_port(addr, port);
        std::shared_ptr<state> st = std::make_shared<state>(app);
        task::spawn([=] {
            st->server->serve(addr, port);
        });
        app->run();
    });
}

ext: cc

max_stars_repo_name: toffaletti/libten

max_issues_repo_name: toffaletti/libten

max_forks_repo_name: toffaletti/libten


text:
//=======================================================================
// Copyright Baptiste Wicht 2011-2016.
// Distributed under the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#ifndef AST_IF_H
#define AST_IF_H

#include <vector>
#include <memory>

#include <boost/fusion/include/adapt_struct.hpp>

#include "ast/ElseIf.hpp"
#include "ast/Else.hpp"
#include "ast/Value.hpp"

namespace eddic {

class Context;

namespace ast {

/*!
 * \class If
 * \brief The AST node for a if.
 */
struct If {
    std::shared_ptr<Context> context;

    Value condition;
    std::vector<Instruction> instructions;
    std::vector<ElseIf> elseIfs;
    boost::optional<Else> else_;

};

} //end of ast

} //end of eddic

//Adapt the struct for the AST
BOOST_FUSION_ADAPT_STRUCT(
    eddic::ast::If,
    (eddic::ast::Value, condition)
    (std::vector<eddic::ast::Instruction>, instructions)
    (std::vector<eddic::ast::ElseIf>, elseIfs)
    (boost::optional<eddic::ast::Else>, else_)
)

#endif

max_stars_repo_name: wichtounet/eddic

max_issues_repo_name: wichtounet/eddic

max_forks_repo_name: wichtounet/eddic


text:
/// \file   output.hpp
///
/// \brief
///
/// \authors    Maarten P. Scholl
/// \date       2019-03-14
/// \copyright  Copyright 2017-2019 The Institute for New Economic Thinking,
///             Oxford Martin School, University of Oxford
///
///             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.
///
///             You may obtain instructions to fulfill the attribution
///             requirements in CITATION.cff
///
#ifndef ESL_DATA_OUTPUT_HPP
#define ESL_DATA_OUTPUT_HPP

#include <string>
#include <iostream>
#include <vector>

#include <boost/serialization/vector.hpp>

#include <esl/data/output_base.hpp>
#include <esl/simulation/time.hpp>

#include <esl/computation/allocator.hpp>

namespace esl::data {
    ///
    /// \addtogroup simulation
    /// \brief  An "output" is some variable that is strictly written to from
    ///         inside the simulation.
    ///
    /// \details
    ///
    ///
    template<typename... variable_types_>
    struct output
    : public output_base
    {
        friend class boost::serialization::access;

    protected:public:
        ///
        /// \brief The (process-local) observed history of values.
        ///
        std::vector< std::tuple<simulation::time_point, variable_types_...>
                   //, boost::pool_allocator<std::tuple<simulation::time_point, variable_types_...>>
                   > values;

    public:

        ///
        ///
        ///
        output(const std::string &name =
                   ( "observable_" + std::to_string(sizeof...(variable_types_)))
                   , bool buffered = false)
        : output_base(name, buffered)
        , values()
        {

        }

        virtual ~output() = default;

        ///
        /// \param t
        /// \param v
        void put(simulation::time_point t, variable_types_... v)
        {
            values.emplace_back(
                std::tuple<simulation::time_point, variable_types_...>(t, v...));
            if(!buffered){
                (write(std::make_tuple(t, v)), ...);
            }
        }

        template<class archive_t>
        void serialize(archive_t &archive, const unsigned int version)
        {
            (void)version;
            //archive.template register_type<simulation::time_point>();
            archive.template register_type<variable_types_...>();
            archive.template register_type<output<variable_types_...>>();
            archive.template register_type<std::tuple<simulation::time_point, variable_types_...>>();

            archive &boost::serialization::make_nvp(
                "output_base",
                boost::serialization::base_object<output_base>(*this));

            archive &BOOST_SERIALIZATION_NVP(values);
        }
    };
}  // namespace esl::data

#ifdef WITH_MPI
#include <boost/mpi.hpp>
namespace boost::mpi {
    template<typename... variable_types_>
    struct is_mpi_datatype<esl::data::output<variable_types_...>>
        : public mpl::false_
    {

    };
}  // namespace boost::mpi
#endif  // WITH_MPI

#endif  // ESL_DATA_OUTPUT_HPP

max_stars_repo_name: vishalbelsare/ESL

max_issues_repo_name: vishalbelsare/ESL

max_forks_repo_name: vishalbelsare/ESL


text: #include <boost/locale/generic_codecvt.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#include "../include/InvertedIndex.h"
#include <boost/tokenizer.hpp>
#include <caca_conio.h>

using namespace std;
using namespace boost;

InvertedIndex::InvertedIndex() {
    cout<<"We create inverted index for documents!\n";
}

void InvertedIndex::getPath() {
    helpVariable=0;
    int number;
    cout<<"Enter file of path:\n";
    cin>>helpString;
    ifstream input(helpString);
    while(getline(input,pathFile)){
        for(auto dastgire:listOfDocument){
            if(dastgire.first==pathFile){
                helpVariable++;
                number=dastgire.second.second;
                break;
            }
        }
        if(helpVariable==0){
            fileName="";
            for(int i=pathFile.length();i>=0;i--){
                if(pathFile[i]!='/'){
                    fileName+=pathFile[i];
                }
                else{
                    break;
                }
            }
            fileName=inverse(fileName);
            number=listOfDocument.size()+1;
            listOfDocument[pathFile].first=fileName;
            listOfDocument[pathFile].second=number;
        }
        invertedIndex(number,pathFile);
    }
}

void InvertedIndex::invertedIndex(int numberOfFile,string path) {
    ifstream input(path);
    if(input.fail()){
        cout<<"Don't exist this file! please correction of file of path:\n";
        cout<<path;
        //listOfDocument.erase(path);
        // getPath();
        exit(0);
    }
    string text;
    while(getline(input,text)) {
        char_separator<char> sep(" ,|;-!?+-=./()*&^%$#@~><`:0123456789");
        tokenizer<char_separator<char>> tokens(text, sep);
        for(auto &t : tokens) {
            helpVariable=0;
            if(inverted_Index.find(t)!=inverted_Index.end()){//hast
                int length= (int) inverted_Index[t].size();
                for(int i=0;i<length;i++){
                    if(inverted_Index[t][i]==numberOfFile){
                        helpVariable++;
                        break;
                    }
                }
                if(helpVariable==0){//yani az in file in kalameh bodeh
                    inverted_Index[t].push_back(numberOfFile);
                }
            }
            else{
                inverted_Index[t].push_back(numberOfFile);
            }
        }
    }
}

std::string InvertedIndex::inverse(std::string nameOfFile) {
    string local="";
    for(int i=nameOfFile.length();i>=0;i--){
        local+=(nameOfFile[i]);
    }
    return local;
}

void InvertedIndex::printExerciseOne() {

    for(map<string,vector<int> >::iterator it=inverted_Index.begin();it!=inverted_Index.end();++it){
        cout<<it->first<<" : ";
        for(int i=0;i<it->second.size();i++){
            cout<<it->second[i]<<" ";
        }
        cout<<"\n";
    }
}

void InvertedIndex::printExerciseTwo() {
    string incidenceMatrix[inverted_Index.size()+1][listOfDocument.size()+1];
    incidenceMatrix[0][0]="IncidenceMatrix";
    helpVariable=1;
    for(map<string,pair<string,int>>::iterator it=listOfDocument.begin();it!=listOfDocument.end();++it){
        helpString=convertToString(it->second.second);
        incidenceMatrix[0][helpVariable]=((it->second.first)+"/"+helpString);
        helpVariable++;
    }
    helpVariable=1;
    for(map<string,vector<int> >::iterator it=inverted_Index.begin();it!=inverted_Index.end();++it){
        incidenceMatrix[helpVariable][0]=it->first;
        helpVariable++;
    }
    string help2;
    for(int i=1;i<inverted_Index.size()+1;i++){
        for(int j=1;j<listOfDocument.size()+1;j++){
            helpString=incidenceMatrix[0][j];
            help2="";
            for(int k=helpString.length();k>=0;k--){
                if(helpString[k]!='/'){
                    help2+=helpString[k];
                }
                else{
                    break;
                }
            }
            int flag=0;
            helpVariable=convertToInt(help2);//shomare sand ro bedast avordim ta berim to vectoresh begardim
            for(int l=0;l<inverted_Index[incidenceMatrix[i][0]].size();l++){
                if(inverted_Index[incidenceMatrix[i][0]][l]==helpVariable){
                    incidenceMatrix[i][j]="1";
                    flag++;
                }
            }
            if(flag==0){
                incidenceMatrix[i][j]="0";
            }
        }
    }
    for(int i=0;i<inverted_Index.size()+1;i++){
        for(int j=0;j<listOfDocument.size()+1;j++){
            cout<<incidenceMatrix[i][j]<<" ";
        }
        cout<<endl;
    }
}

std::string InvertedIndex::convertToString(int variable) {
    helpString="";
    while(variable!=0){
        helpString+=(char)((variable%10)+48);
        variable=variable/10;
    }
    helpString=inverse(helpString);
    return helpString;
}

int InvertedIndex::convertToInt(std::string variable) {
    int local=1;
    int result=0;
    for(int i=0;i<variable.length();i++){
        int h=(int)(variable[i]-48);
        result+=(h*local);
        local*=10;
    }
    return result;
}


text:
// Transfer into framework complete MARS outputs, including
// per-particle weights.  While weighted particles can not be directly
// used to simulate detector hits, this information is useful when the
// same dataset needs to be re-used repeatedly with additional
// randomization.
//
// Original author: Andrei Gaponenko, 2012

#include "Offline/Sources/inc/ExtMonFNALMARSUtils.hh"

#include <iostream>
#include <fstream>
#include <boost/utility.hpp>
#include <cassert>
#include <set>

#include "CLHEP/Vector/LorentzVector.h"
#include "CLHEP/Vector/ThreeVector.h"

#include "art/Framework/IO/Sources/Source.h"
#include "art/Framework/Core/InputSourceMacros.h"
#include "art/Framework/IO/Sources/SourceHelper.h"
#include "art/Framework/Principal/RunPrincipal.h"
#include "art/Framework/Principal/SubRunPrincipal.h"
#include "art/Framework/Principal/EventPrincipal.h"
#include "art/Framework/IO/Sources/put_product_in_principal.h"
#include "art/Utilities/Globals.h" // FIXME-KJK: should not be necessary to use this
#include "canvas/Persistency/Provenance/Timestamp.h"
#include "canvas/Persistency/Provenance/RunID.h"
#include "canvas/Persistency/Provenance/SubRunID.h"
#include "canvas/Persistency/Provenance/EventID.h"
#include "canvas/Persistency/Provenance/BranchType.h"
#include "canvas/Persistency/Provenance/ProductID.h"
#include "canvas/Persistency/Provenance/canonicalProductName.h"

#include "Offline/MCDataProducts/inc/GenParticle.hh"
#include "Offline/MCDataProducts/inc/GenParticleCollection.hh"
#include "Offline/MCDataProducts/inc/MARSInfo.hh"
#include "Offline/MCDataProducts/inc/MARSInfoCollection.hh"
#include "Offline/MCDataProducts/inc/GenParticleMARSAssns.hh"

namespace mu2e {
  namespace ExtMonFNAL {

    //================================================================
    class EMFMARSWeightedDetail : private boost::noncopyable {
      std::string myModuleLabel_;
      art::SourceHelper const& pm_;
      unsigned runNumber_; // from ParSet
      art::SubRunID currentSubRunID_;
      std::set<art::SubRunID> seenSRIDs_;

      std::string currentFileName_;
      std::ifstream currentFile_;
      unsigned currentSubRunNumber_; // from file name

      // We may need to consume several lines of the input file to produce a single event.
      //
      // This data structure holds data from an input line that
      // was read from the file but not used to make a event yet.
      //
      // The var is pre-filled by readFile() then by readNext(), so
      // it is non-empty at the beginning of readNext() - unless we
      // ran out of data in the current file.
      MARSParticle currentLine_;

      MARSMu2eConverter cnv_;

      unsigned getSubRunNumber(const std::string& filename) const;

      unsigned currentEventNumber_;

      art::ProductID particlesPID_;
      art::ProductID marsInfoPID_;

    public:

      EMFMARSWeightedDetail(const fhicl::ParameterSet&,
                            art::ProductRegistryHelper&,
                            const art::SourceHelper&);

      void readFile(std::string const& filename, art::FileBlock*& fb);

      bool readNext(art::RunPrincipal* const& inR,
                    art::SubRunPrincipal* const& inSR,
                    art::RunPrincipal*& outR,
                    art::SubRunPrincipal*& outSR,
                    art::EventPrincipal*& outE);

      void closeCurrentFile();
    };

    //----------------------------------------------------------------
    EMFMARSWeightedDetail::EMFMARSWeightedDetail(const fhicl::ParameterSet& pset,
                                                 art::ProductRegistryHelper& rh,
                                                 const art::SourceHelper& pm)
      : myModuleLabel_("FromEMFMARSFileWeighted")
      , pm_(pm)
      , runNumber_(pset.get<unsigned>("runNumber"))
      , currentSubRunNumber_(-1U)
      , currentEventNumber_(0)
    {
      if(!art::RunID(runNumber_).isValid()) {
        throw cet::exception("BADCONFIG", " FromEMFMARSFileWeighted: ")
          << " fhicl::ParameterSet specifies an invalid runNumber = "<<runNumber_<<"\n";
      }

      auto const& gpcTypeLabel = rh.reconstitutes<mu2e::GenParticleCollection,art::InEvent>(myModuleLabel_);
      auto const& marsTypeLabel = rh.reconstitutes<mu2e::MARSInfoCollection,art::InEvent>(myModuleLabel_);
      rh.reconstitutes<mu2e::GenParticleMARSAssns,art::InEvent>(myModuleLabel_);

      auto get_ProductID = [](auto const& typeLabel, auto const& processName) {
        if (!typeLabel.hasEmulatedModule()) {
          throw cet::exception("BADCONFIG", " FromEMFMARSFileWeighted: ")
          << " Must provided emulated module name for reconstituted product.\n";
        }
        auto const canonical_product_name = art::canonicalProductName(typeLabel.friendlyClassName(),
                                                                      typeLabel.emulatedModule(),
                                                                      typeLabel.productInstanceName(),
                                                                      processName);
        return art::ProductID{canonical_product_name};
      };
      particlesPID_ = get_ProductID(gpcTypeLabel, art::Globals::instance()->processName());
      marsInfoPID_ = get_ProductID(marsTypeLabel, art::Globals::instance()->processName());
    }

    //----------------------------------------------------------------
    void EMFMARSWeightedDetail::readFile(const std::string& filename, art::FileBlock*& fb) {
      if(currentLine_.protonNumber != -1U) {
        throw cet::exception("UNKNOWN")<<"FromEMFMARSFileWeighted: readFile() called while currentLine_ not empty. Something is wrong.\n";
      }

      currentFileName_ = filename;
      currentSubRunNumber_ = getSubRunNumber(filename);

      if(!seenSRIDs_.insert(art::SubRunID(runNumber_, currentSubRunNumber_)).second) {
        ++runNumber_;
        const bool inserted [[gnu::unused]] = seenSRIDs_.insert(art::SubRunID(runNumber_, currentSubRunNumber_)).second;
        assert(inserted);
      }

      currentEventNumber_ = 0;

      if(!art::SubRunID(runNumber_, currentSubRunNumber_).isValid()) {
        throw cet::exception("BADINPUTS")<<"Got invalid subrun number="<<currentSubRunNumber_<<" from input filename="<<filename<<"\n";
      }
      currentFile_.open(filename.c_str());
      fb = new art::FileBlock(art::FileFormatVersion(1, "EMFMARSWeightedinput"), currentFileName_);

      readMARSLine(currentFile_, currentLine_);
    }

    //----------------------------------------------------------------
    unsigned EMFMARSWeightedDetail::getSubRunNumber(const std::string& filename) const {
      const std::string::size_type islash = filename.rfind('/') ;
      const std::string basename = (islash == std::string::npos) ? filename : filename.substr(islash + 1);

      unsigned sr(-1);
      std::istringstream is(basename);
      if(!(is>>sr)) {
        throw cet::exception("BADINPUTS")<<"Expect an unsigned integer at the beginning of input file name, got "<<basename<<"\n";
      }
      return sr;
    }

    //----------------------------------------------------------------
    void EMFMARSWeightedDetail::closeCurrentFile() {
      currentFileName_ = "";
      currentFile_.close();
    }

    //----------------------------------------------------------------
    bool EMFMARSWeightedDetail::readNext(art::RunPrincipal* const& inR,
                                         art::SubRunPrincipal* const& inSR,
                                         art::RunPrincipal*& outR,
                                         art::SubRunPrincipal*& outSR,
                                         art::EventPrincipal*& outE)
    {
      // currentLine_ pre-filled by readFile() or the previous readNext() call.
      if(currentLine_.protonNumber == -1U) { // no more data in the current file
        return false;
      }
      else { // do have more data

        std::unique_ptr<GenParticleCollection> particles(new GenParticleCollection());
        std::unique_ptr<MARSInfoCollection> info(new MARSInfoCollection());
        std::unique_ptr<GenParticleMARSAssns> assns(new GenParticleMARSAssns());

        art::Timestamp ts;

        art::SubRunID newID(runNumber_, currentSubRunNumber_);
        if(newID.runID() != currentSubRunID_.runID()) {
          outR = pm_.makeRunPrincipal(runNumber_, ts);
        }

        if(newID != currentSubRunID_) {
          outSR = pm_.makeSubRunPrincipal(runNumber_,
                                          currentSubRunNumber_,
                                          ts);
          currentSubRunID_ = newID;
        }

        outE = pm_.makeEventPrincipal(runNumber_, currentSubRunNumber_, ++currentEventNumber_, ts, false);

        const art::EDProductGetter* particlesGetter = outE->productGetter(particlesPID_);
        const art::EDProductGetter* infoGetter = outE->productGetter(marsInfoPID_);

        const unsigned nj = currentLine_.protonNumber;
        particles->push_back(cnv_.marsToMu2eParticle(currentLine_));
        info->push_back(MARSInfo(currentLine_.weight, currentLine_.protonNumber, currentSubRunNumber_, runNumber_));
        assns->addSingle(art::Ptr<GenParticle>(particlesPID_, particles->size()-1, particlesGetter),
                         art::Ptr<MARSInfo>(marsInfoPID_, info->size()-1, infoGetter)
                         );

        while(readMARSLine(currentFile_, currentLine_)) {
          if(currentLine_.protonNumber == nj) {
            particles->push_back(cnv_.marsToMu2eParticle(currentLine_));
            info->push_back(MARSInfo(currentLine_.weight, currentLine_.protonNumber, currentSubRunNumber_, runNumber_));
            assns->addSingle(art::Ptr<GenParticle>(particlesPID_, particles->size()-1, particlesGetter),
                             art::Ptr<MARSInfo>(marsInfoPID_, info->size()-1, infoGetter)
                             );
          }
          else {
            break;
          }
        }

        art::put_product_in_principal(std::move(particles), *outE, myModuleLabel_);
        art::put_product_in_principal(std::move(info), *outE, myModuleLabel_);
        art::put_product_in_principal(std::move(assns), *outE, myModuleLabel_);

        return true;

      } // else-have data

    } // readNext()

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

  } // namespace ExtMonFNAL
} // namespace mu2e

typedef art::Source<mu2e::ExtMonFNAL::EMFMARSWeightedDetail> FromEMFMARSFileWeighted;
DEFINE_ART_INPUT_SOURCE(FromEMFMARSFileWeighted);

max_stars_repo_name: michaelmackenzie/Offline

max_issues_repo_name: michaelmackenzie/Offline

max_forks_repo_name: ryuwd/Offline


text:
/*=============================================================================
  Copyright (c) 2010-2016 Bolero MURAKAMI
  https://github.com/bolero-MURAKAMI/Sprig

  Distributed under the Boost Software License, Version 1.0. (See accompanying
  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef SPRIG_XML_HPP
#define SPRIG_XML_HPP

#include <sprig/config/config.hpp>

#ifdef SPRIG_USING_PRAGMA_ONCE
#	pragma once
#endif	// #ifdef SPRIG_USING_PRAGMA_ONCE

#include <boost/spirit/include/classic.hpp>
#include <boost/spirit/include/classic_ast.hpp>

//
// SPRIG_XML_GRAMMER_RULE_TYPE
//
#define SPRIG_XML_GRAMMER_RULE_TYPE(RULE_INDEX) \
	boost::spirit::classic::rule<Scanner, boost::spirit::classic::parser_tag<RULE_INDEX> >
//
// SPRIG_XML_GRAMMER_RULE_DECLARE
//
#define SPRIG_XML_GRAMMER_RULE_DECLARE(RULE_NAME, RULE_INDEX) \
	SPRIG_XML_GRAMMER_RULE_TYPE(RULE_INDEX) RULE_NAME

namespace sprig {
	//
	// xml_grammar
	//
	class xml_grammar
		: public boost::spirit::classic::grammar<xml_grammar>
	{
	public:
		enum rule_index {
			document = 1,
			S = 3,
			Name = 5,
			AttValue = 10,
			CharData = 14,
			Comment = 15,
			Eq = 25,
			Misc = 27,
			element = 39,
			STag = 40,
			Attribute = 41,
			ETag = 42,
			content = 43,
			EmptyElemTag = 44,
			Reference = 67,
			EntityRef = 68,
		};
	public:
		template<typename Scanner>
		class definition {
		private:
			SPRIG_XML_GRAMMER_RULE_DECLARE(document_, document);
			SPRIG_XML_GRAMMER_RULE_DECLARE(S_, S);
			SPRIG_XML_GRAMMER_RULE_DECLARE(Name_, xml_grammar::Name);
			SPRIG_XML_GRAMMER_RULE_DECLARE(AttValue_, AttValue);
			SPRIG_XML_GRAMMER_RULE_DECLARE(CharData_, CharData);
			SPRIG_XML_GRAMMER_RULE_DECLARE(Comment_, Comment);
			SPRIG_XML_GRAMMER_RULE_DECLARE(Eq_, Eq);
			SPRIG_XML_GRAMMER_RULE_DECLARE(Misc_, Misc);
			SPRIG_XML_GRAMMER_RULE_DECLARE(element_, element);
			SPRIG_XML_GRAMMER_RULE_DECLARE(STag_, STag);
			SPRIG_XML_GRAMMER_RULE_DECLARE(Attribute_, Attribute);
			SPRIG_XML_GRAMMER_RULE_DECLARE(ETag_, ETag);
			SPRIG_XML_GRAMMER_RULE_DECLARE(content_, content);
			SPRIG_XML_GRAMMER_RULE_DECLARE(EmptyElemTag_, EmptyElemTag);
			SPRIG_XML_GRAMMER_RULE_DECLARE(Reference_, Reference);
			SPRIG_XML_GRAMMER_RULE_DECLARE(EntityRef_, EntityRef);
		public:
			definition(xml_grammar const&) {
				using namespace boost::spirit::classic;
				document_ = no_node_d[*Misc_] >> element_ >> no_node_d[*Misc_];
				S_ =+space_p;
				Name_ = leaf_node_d[
					(alpha_p | '_' | ':')
					>> *(alnum_p | '.' | '-' | '_' | ':')
					];
				AttValue_
					= leaf_node_d['\"' >> *((anychar_p - (ch_p('<') | '&' | '\"')) | Reference_) >> '\"']
					| leaf_node_d['\'' >> *((anychar_p - (ch_p('<') | '&' | '\'')) | Reference_) >> '\'']
					;
				CharData_ = leaf_node_d[
					*(anychar_p - (ch_p('<') | '&')) - (*(anychar_p - (ch_p('<') | '&')) >> "]]>" >> *(anychar_p - (ch_p('<') | '&')))
					];
				Comment_ = "<!--" >> *((anychar_p - '-') | ('-' >> (anychar_p - '-'))) >> "-->";
				Eq_ = !S_ >> '=' >> !S_;
				Misc_ = Comment_ | S_;
				element_ = EmptyElemTag_ | (STag_ >> content_ >> no_node_d[ETag_]);
				STag_ = no_node_d[ch_p('<')] >> Name_ >> *(no_node_d[S_] >> Attribute_) >> no_node_d[!S_ >> '>'];
				Attribute_ = Name_ >> no_node_d[Eq_] >> AttValue_;
				ETag_ = no_node_d[str_p("</")] >> Name_ >> no_node_d[!S_ >> '>'];
				content_
					= (no_node_d[!S_] >> !CharData_)
					>> *((element_ | Reference_ | no_node_d[Comment_]) >> (no_node_d[*S_] >> !CharData_))
					>> no_node_d[*S_]
					;
				EmptyElemTag_ = no_node_d[ch_p('<')] >> Name_ >> *(no_node_d[S_] >> Attribute_) >> no_node_d[!S_ >> str_p("/>")];
				Reference_ = EntityRef_;
				EntityRef_ = str_p("&") | "<" | ">" | """;
			}
			SPRIG_XML_GRAMMER_RULE_TYPE(document) const& start() const {
				return document_;
			}
		};
	};
}	// namespace sprig

#endif	// #ifndef SPRIG_XML_HPP

max_stars_repo_name: bolero-MURAKAMI/Sprig

max_issues_repo_name: bolero-MURAKAMI/Sprig

max_forks_repo_name: bolero-MURAKAMI/Sprig