text:
#include "FileDescriptor.h"

#include <faabric/util/bytes.h>
#include <faabric/util/config.h>
#include <faabric/util/logging.h>
#include <faabric/util/macros.h>
#include <faabric/util/timing.h>

#include <conf/FaasmConfig.h>
#include <storage/SharedFiles.h>

#include <WAVM/WASI/WASIABI.h>
#include <boost/filesystem.hpp>
#include <dirent.h>
#include <fcntl.h>
#include <stdexcept>
#include <sys/stat.h>

#define WASI_FD_FLAGS                                                          \
    (__WASI_FDFLAG_RSYNC | __WASI_FDFLAG_APPEND | __WASI_FDFLAG_DSYNC |        \
     __WASI_FDFLAG_SYNC | __WASI_FDFLAG_NONBLOCK)

namespace storage {
std::string prependRuntimeRoot(const std::string& originalPath)
{
    conf::FaasmConfig& conf = conf::getFaasmConfig();
    boost::filesystem::path p(conf.runtimeFilesDir);
    p.append(originalPath);
    return p.string();
}

int32_t wasiFdFlagsToLinux(int32_t fdFlags)
{
    int32_t result = 0;

    // More open flags
    if (fdFlags != 0) {
        if (fdFlags & WASI_FD_FLAGS) {
            if (fdFlags & __WASI_FDFLAG_RSYNC) {
                result |= O_RSYNC;
            }

            if (fdFlags & __WASI_FDFLAG_APPEND) {
                result |= O_APPEND;
            }

            if (fdFlags & __WASI_FDFLAG_DSYNC) {
                result |= O_DSYNC;
            }

            if (fdFlags & __WASI_FDFLAG_SYNC) {
                result |= O_SYNC;
            }

            if (fdFlags & __WASI_FDFLAG_NONBLOCK) {
                result |= O_NONBLOCK;
            }

        } else {
            throw std::runtime_error("Unhandled fd flags");
        }
    }

    return result;
}

OpenMode getOpenMode(uint16_t openFlags)
{
    if (openFlags & __WASI_O_CREAT) {
        return OpenMode::CREATE;
    }
    if (openFlags & __WASI_O_DIRECTORY) {
        return OpenMode::DIRECTORY;
    }
    if (openFlags & __WASI_O_TRUNC) {
        return OpenMode::TRUNC;
    }
    if (openFlags & __WASI_O_EXCL) {
        return OpenMode::EXCL;
    } else if (openFlags == 0) {
        return OpenMode::NONE;
    } else {
        throw std::runtime_error("Unable to detect open mode");
    }
}

ReadWriteType getRwType(uint64_t rights)
{
    // Check for presence of any read/write permissions
    const bool rightsRead = rights & WASI_RIGHTS_READ;
    const bool rightsWrite = rights & WASI_RIGHTS_WRITE;

    if (rightsRead && rightsWrite) {
        return ReadWriteType::READ_WRITE;
    }
    if (rightsRead) {
        return ReadWriteType::READ_ONLY;
    }
    if (!rightsRead && rightsWrite) {
        return ReadWriteType::WRITE_ONLY;
    }
    if (rights == 0) {
        return ReadWriteType::NO_READ_WRITE;
    } else {
        return ReadWriteType::CUSTOM;
    }
}

uint16_t errnoToWasi(int errnoIn)
{
    switch (errnoIn) {
        case EPERM:
            return __WASI_EPERM;
        case ENOENT:
            return __WASI_ENOENT;
        case EIO:
            return __WASI_EIO;
        case EBADF:
            return __WASI_EBADF;
        case ENOMEM:
            return __WASI_ENOMEM;
        case EACCES:
            return __WASI_EACCES;
        case EEXIST:
            return __WASI_EEXIST;
        case ENOTDIR:
            return __WASI_ENOTDIR;
        case EISDIR:
            return __WASI_EISDIR;
        case EINVAL:
            return __WASI_EINVAL;
        case EMFILE:
            return __WASI_EMFILE;
        default:
            throw std::runtime_error("Unsupported WASI errno: " +
                                     std::to_string(errnoIn));
    }
}

std::string FileDescriptor::getPath()
{
    return path;
}

void FileDescriptor::setPath(const std::string& newPath)
{
    path = newPath;
}

std::string FileDescriptor::absPath(const std::string& relativePath)
{
    std::string res;

    if (relativePath.empty()) {
        return path;
    }
    std::string basePath = path == "." ? "" : path;
    boost::filesystem::path joinedPath(basePath);
    joinedPath.append(relativePath);
    return joinedPath.string();
}

FileDescriptor FileDescriptor::stdFdFactory(int stdFd,
                                            const std::string& devPath)
{
    FileDescriptor fdStd;
    fdStd.setPath(devPath);

    uint64_t rights = __WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_FDSTAT_SET_FLAGS |
                      __WASI_RIGHT_FD_WRITE | __WASI_RIGHT_FD_FILESTAT_GET |
                      __WASI_RIGHT_POLL_FD_READWRITE;
    fdStd.setActualRights(rights, rights);
    fdStd.linuxFd = stdFd;
    return fdStd;
}

FileDescriptor FileDescriptor::stdoutFactory()
{
    return FileDescriptor::stdFdFactory(STDOUT_FILENO, "/dev/stdout");
}

FileDescriptor FileDescriptor::stdinFactory()
{
    return FileDescriptor::stdFdFactory(STDIN_FILENO, "/dev/stdin");
}

FileDescriptor FileDescriptor::stderrFactory()
{
    return FileDescriptor::stdFdFactory(STDERR_FILENO, "/dev/stderr");
}

void FileDescriptor::iterReset()
{
    // Reset iterator state
    dirContentsLoaded = false;
    dirContentsIdx = 0;
    dirContents.clear();
}

void FileDescriptor::loadDirContents()
{

    // Work out the local filesystem path
    std::string realPath;
    if (SharedFiles::isPathShared(path)) {
        int pullErr = SharedFiles::syncSharedFile(path);

        if (pullErr != 0) {
            throw std::runtime_error("Failed to open shared dir");
        }

        realPath = SharedFiles::realPathForSharedFile(path);
    } else {
        realPath = prependRuntimeRoot(path);
    }

    // Open the directory
    SPDLOG_DEBUG("Loading dir contents: {}", realPath);
    DIR* dirPtr = ::opendir(realPath.c_str());
    if (dirPtr == nullptr) {
        throw std::runtime_error("Failed to open dir");
    }

    // Load all directory entries
    uint64_t nextIdx = 0;
    struct dirent* direntPtr;
    while ((direntPtr = ::readdir(dirPtr)) != nullptr) {
        nextIdx++;

        DirEnt nextEnt;

        // We have to set "next" here to specify the offset of this
        // directory entry. It seems this is only used to be passed
        // back as the "cookie" value to fd_readdir
        nextEnt.next = nextIdx;

        nextEnt.type = direntPtr->d_type;
        nextEnt.ino = direntPtr->d_ino;
        nextEnt.path = std::string(direntPtr->d_name);

        dirContents.push_back(nextEnt);
    }

    // Close iterator
    closedir(dirPtr);
    SPDLOG_DEBUG("Loaded {} entries for {}", dirContents.size(), realPath);

    // Set flag
    dirContentsLoaded = true;
}

void FileDescriptor::iterBack()
{
    if (!dirContentsLoaded) {
        throw std::runtime_error("Iterator not started, cannot go back");
    }

    if (dirContentsIdx == 0) {
        throw std::runtime_error("Iterator already at zero, cannot go back");
    }

    dirContentsIdx--;
}

bool FileDescriptor::iterStarted() const
{
    return dirContentsLoaded;
}

bool FileDescriptor::iterFinished()
{
    return dirContentsLoaded && (dirContentsIdx >= dirContents.size());
}

DirEnt FileDescriptor::iterNext()
{
    // Check if contents are initialised
    if (!dirContentsLoaded) {
        loadDirContents();
    }

    // Check
    if (iterFinished()) {
        throw std::runtime_error(
          fmt::format("Accessing index {} in directory length {}",
                      dirContentsIdx,
                      dirContents.size()));
    }

    DirEnt nextEntry = dirContents.at(dirContentsIdx);

    // Increment the iterator
    dirContentsIdx++;

    return nextEntry;
}

/**
 * Note that this function conforms to the standard readdir interface:
 * - Copy each dirent struct followed by its path string
 * - If the last path string doesn't fit, return the buffer length
 * - If the last dirent doesn't fit, don't copy it and return the buffer length
 *
 * The external libc wrapper should prevent this third point.
 *
 * If the final dirent or path doesn't fit, the caller will expect the next
 * result to contain that same dirent/ path, so we have to step back one in the
 * iterator if that's the case.
 *
 * The caller knows they've reached the end when the buffer is _not_ filled by
 * this call.
 */
size_t FileDescriptor::copyDirentsToWasiBuffer(uint8_t* buffer,
                                               size_t bufferLen)
{

    // Prepare loop variables
    size_t bytesLeft = bufferLen;
    bool isBufferFull = false;

    size_t wasiDirentSize = sizeof(__wasi_dirent_t);

    while (!iterFinished()) {
        DirEnt d = iterNext();

        // Create WASI dirent
        size_t pathSize = d.path.size();
        __wasi_dirent_t wasmDirEnt{ .d_next = d.next,
                                    .d_ino = d.ino,
                                    .d_namlen = (uint32_t)pathSize,
                                    .d_type = d.type };

        // Drop out if we have no room for the dirent
        if (bytesLeft < wasiDirentSize) {
            isBufferFull = true;
            break;
        }

        auto* direntPtr = BYTES(&wasmDirEnt);
        std::copy(direntPtr, direntPtr + wasiDirentSize, buffer);
        bytesLeft -= wasiDirentSize;
        buffer += wasiDirentSize;

        // Drop out if we have no room for the path
        if (bytesLeft < pathSize) {
            isBufferFull = true;
            break;
        }

        const auto* pathPtr = BYTES_CONST(d.path.c_str());
        std::copy(pathPtr, pathPtr + pathSize, buffer);
        bytesLeft -= pathSize;
        buffer += pathSize;
    }

    if (isBufferFull) {
        // Go back one, we've not been able to finish this entry
        iterBack();
        return bufferLen;
    } // Return the number of bytes copied
    return bufferLen - bytesLeft;
}

bool FileDescriptor::pathOpen(uint32_t lookupFlags,
                              uint32_t openFlags,
                              int32_t fdFlags)
{
    if (!rightsSet) {
        throw std::runtime_error("Opening path with no rights set");
    }

    OpenMode openMode = getOpenMode((uint16_t)openFlags);

    ReadWriteType rwType = getRwType(actualRightsBase);

    int readWrite = 0;
    if (openMode == OpenMode::DIRECTORY) {
        readWrite = O_RDONLY;
    } else if (rwType == ReadWriteType::READ_WRITE) {
        readWrite = O_RDWR;
    } else if (rwType == ReadWriteType::WRITE_ONLY) {
        readWrite = O_WRONLY;
    } else if (rwType == ReadWriteType::READ_ONLY) {
        readWrite = O_RDONLY;
    } else if (rwType == ReadWriteType::NO_READ_WRITE) {
        readWrite = 0;
    } else {
        SPDLOG_ERROR("Unrecognised access mode: {}", rwType);
        throw std::runtime_error("Unrecognised access mode for file");
    }

    // Open flags
    if (openMode == OpenMode::CREATE) {
        linuxFlags = readWrite | O_CREAT;
        linuxMode = S_IRWXU | S_IRGRP | S_IROTH;
    } else if (openMode == OpenMode::DIRECTORY) {
        // Note - we can only pass O_RDONLY with O_DIRECTORY
        linuxFlags = readWrite | O_DIRECTORY;
        linuxMode = 0;
    } else if (openMode == OpenMode::TRUNC) {
        linuxFlags = readWrite | O_TRUNC;
        linuxMode = 0;
    } else if (openMode == OpenMode::EXCL) {
        linuxFlags = readWrite | O_EXCL;
        linuxMode = 0;
    } else if (openMode == OpenMode::NONE) {
        linuxFlags = readWrite;
        linuxMode = 0;
    } else {
        SPDLOG_ERROR("Unrecognised open mode: {}", openMode);
        throw std::runtime_error("Unrecognised open flags");
    }

    // More flags
    linuxFlags |= wasiFdFlagsToLinux(fdFlags);

    bool isShared = SharedFiles::isPathShared(path);
    std::string realPath;
    if (isShared) {
        // Pull the shared file
        linuxErrno = SharedFiles::syncSharedFile(path);
        if (linuxErrno != 0) {
            linuxFd = -1;
            wasiErrno = errnoToWasi(linuxErrno);
            return false;
        }

        realPath = SharedFiles::realPathForSharedFile(path);
    } else {
        realPath = prependRuntimeRoot(path);
    }

    // Attempt to open the local file
    if (realPath == "/dev/urandom") {
        // TODO avoid use of system-wide urandom
        linuxFd = ::open("/dev/urandom", 0, 0);

    } else if (realPath == "/dev/null") {
        linuxFd = ::open("/dev/null", 0, 0);
    } else {
        linuxFd = ::open(realPath.c_str(), linuxFlags, linuxMode);
    }

    if (linuxFd < 0) {
        linuxErrno = errno;
        wasiErrno = errnoToWasi(linuxErrno);
        return false;
    }

    return true;
}

bool FileDescriptor::mkdir(const std::string& dirPath)
{
    std::string fullPath = prependRuntimeRoot(dirPath);
    int res = ::mkdir(fullPath.c_str(), 0755);

    if (res < 0) {
        wasiErrno = errnoToWasi(errno);
        return false;
    }

    return true;
}

bool FileDescriptor::updateFlags(int32_t fdFlags)
{
    // Update underlying file descriptor
    int32_t newFlags = wasiFdFlagsToLinux(fdFlags);

    int res = fcntl(linuxFd, F_SETFL, newFlags);
    if (res < 0) {
        wasiErrno = errnoToWasi(errno);
        return false;
    }

    // Update existing Linux flags
    linuxFlags |= newFlags;
    return true;
}

void FileDescriptor::close() const
{
    if (linuxFd > 0) {
        ::close(linuxFd);
    }
}

bool FileDescriptor::unlink(const std::string& relativePath)
{
    std::string fullPath = absPath(relativePath);
    const std::string maskedPath = prependRuntimeRoot(fullPath);
    int res = ::unlink(maskedPath.c_str());

    if (res != 0) {
        wasiErrno = errnoToWasi(errno);
        return false;
    }

    return true;
}

bool FileDescriptor::rmdir(const std::string& relativePath)
{
    std::string fullPath = absPath(relativePath);
    const std::string maskedPath = prependRuntimeRoot(fullPath);
    int res = ::rmdir(maskedPath.c_str());

    if (res != 0) {
        wasiErrno = errnoToWasi(errno);
        return false;
    }

    return true;
}

bool FileDescriptor::rename(const std::string& newPath,
                            const std::string& relativePath)
{
    std::string fullPath = absPath(relativePath);
    std::string fullOldPath = prependRuntimeRoot(fullPath);
    std::string fullNewPath = prependRuntimeRoot(newPath);

    int res = ::rename(fullOldPath.c_str(), fullNewPath.c_str());

    if (res != 0) {
        wasiErrno = errnoToWasi(-1 * res);
        return false;
    }

    return true;
}

Stat FileDescriptor::stat(const std::string& relativePath)
{
    struct stat nativeStat
    {};

    int statErrno = 0;
    if (linuxFd == STDOUT_FILENO || linuxFd == STDIN_FILENO ||
        linuxFd == STDERR_FILENO) {
        int result = ::fstat(linuxFd, &nativeStat);
        if (result < 0) {
            statErrno = errno;
        }
    } else {
        // Work out whether we're stat-ing a shared path
        std::string statPath = absPath(relativePath);
        std::string realPath;
        if (SharedFiles::isPathShared(statPath)) {
            statErrno = SharedFiles::syncSharedFile(statPath);
            if (statErrno == 0) {
                realPath = SharedFiles::realPathForSharedFile(statPath);
            }
        } else {
            realPath = prependRuntimeRoot(statPath);
        }

        // Do the actual stat
        int result;
        if (!realPath.empty()) {
            result = ::stat(realPath.c_str(), &nativeStat);
            if (result < 0) {
                statErrno = errno;
            }
        }
    }

    Stat statResult;
    if (statErrno != 0) {
        statResult.failed = true;
        statResult.wasiErrno = errnoToWasi(statErrno);
        return statResult;
    }
    statResult.failed = false;
    statResult.wasiErrno = 0;

    // Work out file type
    if (linuxFd == STDOUT_FILENO || linuxFd == STDIN_FILENO ||
        linuxFd == STDERR_FILENO) {
        statResult.wasiFiletype = __WASI_FILETYPE_CHARACTER_DEVICE;
    } else if (S_ISREG(nativeStat.st_mode)) {
        statResult.wasiFiletype = __WASI_FILETYPE_REGULAR_FILE;
    } else if (S_ISBLK(nativeStat.st_mode)) {
        statResult.wasiFiletype = __WASI_FILETYPE_BLOCK_DEVICE;
    } else if (S_ISDIR(nativeStat.st_mode)) {
        statResult.wasiFiletype = __WASI_FILETYPE_DIRECTORY;
    } else if (S_ISLNK(nativeStat.st_mode)) {
        statResult.wasiFiletype = __WASI_FILETYPE_SYMBOLIC_LINK;
    } else if (S_ISCHR(nativeStat.st_mode)) {
        statResult.wasiFiletype = __WASI_FILETYPE_CHARACTER_DEVICE;
    } else {
        throw std::runtime_error("Unrecognised file type");
    }

    // Set up the result
    statResult.st_dev = nativeStat.st_dev;
    statResult.st_ino = nativeStat.st_ino;
    statResult.st_nlink = nativeStat.st_nlink;
    statResult.st_size = nativeStat.st_size;
    statResult.st_mode = nativeStat.st_mode;
    statResult.st_atim = faabric::util::timespecToNanos(&nativeStat.st_atim);
    statResult.st_mtim = faabric::util::timespecToNanos(&nativeStat.st_mtim);
    statResult.st_ctim = faabric::util::timespecToNanos(&nativeStat.st_ctim);

    return statResult;
}

ssize_t FileDescriptor::readLink(const std::string& relativePath,
                                 char* buffer,
                                 size_t bufferLen)
{
    std::string linkPath = prependRuntimeRoot(absPath(relativePath));

    if (SharedFiles::isPathShared(linkPath)) {
        SPDLOG_ERROR("Readlink on shared not yet supported ({})", path);
        throw std::runtime_error("Readlink on shared file not supported");
    }

    ssize_t bytesRead = ::readlink(linkPath.c_str(), buffer, (size_t)bufferLen);
    return bytesRead;
}

uint16_t FileDescriptor::seek(int64_t offset,
                              int wasiWhence,
                              uint64_t* newOffset) const
{
    int linuxWhence;
    if (wasiWhence == __WASI_WHENCE_SET) {
        linuxWhence = SEEK_SET;
    } else if (wasiWhence == __WASI_WHENCE_CUR) {
        linuxWhence = SEEK_CUR;
    } else if (wasiWhence == __WASI_WHENCE_END) {
        linuxWhence = SEEK_END;
    } else {
        throw std::runtime_error("Unsupported whence");
    }

    // Do the seek
    off_t result = ::lseek(linuxFd, offset, linuxWhence);
    if (result < 0) {
        return errnoToWasi(errno);
    }

    *newOffset = (uint64_t)result;

    return __WASI_ESUCCESS;
}

uint64_t FileDescriptor::tell() const
{
    off_t result = ::lseek(linuxFd, 0, SEEK_CUR);
    return result;
}

int FileDescriptor::getLinuxFd() const
{
    return linuxFd;
}

int FileDescriptor::getLinuxFlags() const
{
    return linuxFlags;
}

int FileDescriptor::getLinuxErrno() const
{
    return linuxErrno;
}

uint16_t FileDescriptor::getWasiErrno() const
{
    return wasiErrno;
}

uint64_t FileDescriptor::getActualRightsBase() const
{
    return actualRightsBase;
}

uint64_t FileDescriptor::getActualRightsInheriting() const
{
    return actualRightsInheriting;
}

void FileDescriptor::setActualRights(uint64_t rights, uint64_t inheriting)
{
    actualRightsBase = rights;
    actualRightsInheriting = inheriting;
    rightsSet = true;
}

int FileDescriptor::duplicate(const FileDescriptor& other)
{
    // Duplicate the underlying fd
    linuxFd = ::dup(other.linuxFd);

    linuxMode = other.linuxMode;
    linuxFlags = other.linuxFlags;
    linuxErrno = other.linuxErrno;

    // Set the internal variables
    path = other.path;
    rightsSet = other.rightsSet;
    actualRightsBase = other.actualRightsBase;
    actualRightsInheriting = other.actualRightsInheriting;

    dirContentsLoaded = other.dirContentsLoaded;
    dirContents = other.dirContents;
    dirContentsIdx = other.dirContentsIdx;

    return linuxFd;
}

}

max_stars_repo_name: auto-ndp/faasm

max_issues_repo_name: auto-ndp/faasm

max_forks_repo_name: auto-ndp/faasm


text:
#include "core/Parser.h"
#include <boost/algorithm/string/classification.hpp>

Parser::Parser(std::string html)
    : html_(std::move(html)),
      root(gumbo_parse(html_.c_str())->root)
{}

Parser::LinkContainer Parser::getLinks(const ParserSearchParams &params)
{
    LinkContainer container{};
    getLinks(root, params, container);
    return container;
}

void Parser::getLinks(const GumboNode *node, const ParserSearchParams &params, Parser::LinkContainer &container)
{
    if (node->type != GUMBO_NODE_ELEMENT) {
        return;
    }

    GumboAttribute *href;
    if (href = gumbo_get_attribute(&node->v.element.attributes, params.attr.c_str());
        boost::is_any_of(params.tags)(node->v.element.tag) && href) {

        std::string link = href->value;
        if (!link.empty()) {
            container.emplace(std::move(link));
        }
    }

    const GumboVector *children = &node->v.element.children;
    for (size_t i = 0; i < children->length; ++i) {
        getLinks(static_cast<GumboNode *>(children->data[i]),
                 params,
                 container);
    }
}

ext: cpp


text:
// (C) Copyright Jeremy Siek, David Abrahams 2000-2006.
// 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)

#include <boost/concept_check.hpp>
#include "old_concepts.hpp"

// This test verifies that use of the old-style concept checking
// classes still compiles (but not that it detects constraint
// violations).  We check them with the old-style macros just for
// completeness, since those macros stranslate into
// BOOST_CONCEPT_ASSERTs.

struct foo { bool operator()(int) { return true; } };
struct bar { bool operator()(int, char) { return true; } };

class class_requires_test
{
  BOOST_CLASS_REQUIRE(int, old, EqualityComparableConcept);
  typedef int* int_ptr; typedef const int* const_int_ptr;
  BOOST_CLASS_REQUIRE2(int_ptr, const_int_ptr, old, EqualOpConcept);
  BOOST_CLASS_REQUIRE3(foo, bool, int, old, UnaryFunctionConcept);
  BOOST_CLASS_REQUIRE4(bar, bool, int, char, old, BinaryFunctionConcept);
};

int
main()
{
    class_requires_test x;
    boost::ignore_unused_variable_warning(x);
    return 0;
}

max_stars_repo_name: randolphwong/mcsema

max_issues_repo_name: randolphwong/mcsema

max_forks_repo_name: randolphwong/mcsema


text:
// __BEGIN_LICENSE__
//  Copyright (c) 2009-2013, United States Government as represented by the
//  Administrator of the National Aeronautics and Space Administration. All
//  rights reserved.
//
//  The NGT platform is 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.
// __END_LICENSE__

#include <vw/Core/Exception.h>          // for ArgumentErr, vw_throw, etc
#include <vw/Math/Quaternion.h>         // for Quat, Quaternion
#include <vw/Math/Vector.h>             // for Vector, Vector3, Vector4, etc
#include <vw/Cartography/Datum.h>       // for Datum
#include <vw/FileIO/DiskImageResourceGDAL.h>

#include <asp/Core/FileUtils.h>
#include <asp/Camera/XMLBase.h>
#include <asp/Camera/ASTER_XML.h>
#include <asp/Camera/RPCModel.h>
#include <asp/Camera/XMLBase.h>

#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/sax/SAXException.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/dom/DOMException.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/sax/ErrorHandler.hpp>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <iomanip>

using namespace vw;
using namespace vw::cartography;
using namespace xercesc;

using asp::XmlUtils::get_node;
using asp::XmlUtils::cast_xmlch;

namespace asp {

DOMElement* ASTERXML::open_xml_file(std::string const& xml_path) {

  // Check if the file actually exists and throw a user helpful file.
  if ( !boost::filesystem::exists( xml_path ) )
    vw_throw( ArgumentErr() << "XML file \"" << xml_path << "\" does not exist." );

  std::string error_prefix = "XML file \"" + xml_path + "\" is invalid.\nException message is: \n";
  std::string err_message  = ""; // Filled in later on error

  try{
    //std::cout << "Set XML parser\n";

    // Set up the XML parser if we have not already done so
    if (!m_parser.get()) {
      m_parser.reset(new XercesDOMParser());
      m_errHandler.reset(new HandlerBase());
      m_parser->setValidationScheme(XercesDOMParser::Val_Always);
      m_parser->setDoNamespaces(true);
      m_parser->setErrorHandler(m_errHandler.get());
    }

    //std::cout << "Load XML\n";

    // Load the XML file
    m_parser->parse( xml_path.c_str() );
    DOMDocument* xmlDoc      = m_parser->getDocument();
    DOMElement * elementRoot = xmlDoc->getDocumentElement();
    return elementRoot;

  } catch (const XMLException& toCatch) {
    char* message = XMLString::transcode(toCatch.getMessage());
    err_message = error_prefix + message;
    XMLString::release(&message);
  } catch (const DOMException& toCatch) {
    char* message = XMLString::transcode(toCatch.msg);
    err_message = error_prefix + message;
    XMLString::release(&message);
  } catch (const SAXException& toCatch) {
    char* message = XMLString::transcode(toCatch.getMessage());
    err_message = error_prefix + message;
    XMLString::release(&message);
  } catch ( const std::exception& e ) {
    err_message = error_prefix + e.what();
  } catch (...) {
    err_message = "Unrecognized error in XML file \"" + xml_path + "\"\n";
  }
  vw_throw( ArgumentErr() << err_message); // Only get here on error
  return 0;
}

void ASTERXML::read_xml(std::string const& xml_path) {

  DOMElement * elementRoot = open_xml_file(xml_path);
  parse_xml(elementRoot);
}

void ASTERXML::parse_xml(xercesc::DOMElement* node) {

  xercesc::DOMElement* lattice_point_node = get_node<DOMElement>(node, "LATTICE_POINT");
  std::string lattice_point_txt( XMLString::transcode(lattice_point_node->getTextContent()) );
  asp::read_matrix_from_string(lattice_point_txt, m_lattice_mat);

  xercesc::DOMElement* sight_vector_node = get_node<DOMElement>(node, "SIGHT_VECTOR");
  std::string sight_vector_txt( XMLString::transcode(sight_vector_node->getTextContent()) );
  asp::read_matrix_from_string(sight_vector_txt, m_sight_mat);
  if (m_lattice_mat.empty() || m_sight_mat.empty()     ||
      m_lattice_mat.size()   != m_sight_mat.size()   ||
      m_lattice_mat[0].size() != m_sight_mat[0].size() ) {
    vw_throw( ArgumentErr() << "Inconsistent lattice point and sight vector information.\n");
  }

  xercesc::DOMElement* world_sight_vector_node = get_node<DOMElement>(node, "WORLD_SIGHT_VECTOR");
  std::string world_sight_vector_txt(XMLString::transcode(world_sight_vector_node->getTextContent()));
  asp::read_matrix_from_string(world_sight_vector_txt, m_world_sight_mat);
  if (m_lattice_mat.empty()   || m_world_sight_mat.empty()  ||
      m_lattice_mat.size()    != m_world_sight_mat.size()   ||
      m_lattice_mat[0].size() != m_world_sight_mat[0].size() ) {
    vw_throw( ArgumentErr() << "Inconsistent lattice point and world sight vector information.\n");
  }

  std::vector< std::vector<vw::Vector3> > sat_pos_mat;
  xercesc::DOMElement* sat_pos_node = get_node<DOMElement>(node, "SAT_POS");
  std::string sat_pos_txt( XMLString::transcode(sat_pos_node->getTextContent()) );
  asp::read_matrix_from_string(sat_pos_txt, sat_pos_mat);
  for (size_t row = 0; row < sat_pos_mat.size(); row++) {
    for (size_t col = 0; col < sat_pos_mat[row].size(); col++) {
      //std::cout << sat_pos_mat[row][col] << " ";
    }
    //std::cout << std::endl;
  }
  m_sat_pos = sat_pos_mat[0]; // Go from matrix with one row to a vector

  if (m_sat_pos.size() != m_sight_mat.size())
    vw_throw( ArgumentErr() << "Inconsistent satellite position and sight vector information.\n");
  if (m_sat_pos.size() != m_world_sight_mat.size())
    vw_throw( ArgumentErr() << "Inconsistent satellite position and world "
	      << "sight vector information.\n");

  xercesc::DOMElement* cols_node = get_node<DOMElement>(node, "IMAGE_COLS");
  std::string cols_txt( XMLString::transcode(cols_node->getTextContent()) );
  int cols = atoi(cols_txt.c_str());

  xercesc::DOMElement* rows_node = get_node<DOMElement>(node, "IMAGE_ROWS");
  std::string rows_txt( XMLString::transcode(rows_node->getTextContent()) );
  int rows = atoi(rows_txt.c_str());

  m_image_size = vw::Vector2(cols, rows);
}

} // end namespace asp

ext: cc

max_stars_repo_name: AndrewAnnex/StereoPipeline

max_issues_repo_name: AndrewAnnex/StereoPipeline

max_forks_repo_name: AndrewAnnex/StereoPipeline


text:
// Header
#include "QuadProgMm.hpp"

// Boost
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH

// QuadProgMm
#include "Translate.hpp"

namespace QuadProgMm {
  Solution minimize(const QuadraticExpression& q, const std::vector<Constraint>& constraints) {
    Translation t = translate(q, constraints);

    quadprogpp::Vector<double> x;
    double cost = quadprogpp::solve_quadprog(t.G, t.g0, t.CE, t.ce0, t.CI, t.ci0, x);

    std::map<Variable, double> values;
    {
      int index = 0;
      foreach(Variable v, t.variables) {
        values[v] = x[index];
        ++index;
      }
    }

    return Solution(cost + t.g00, values);
  }

  Solution maximize(const QuadraticExpression& q, const std::vector<Constraint>& constraints) {
    return minimize(-q, constraints);
  }
}


text:
/**
 * @file eigenray_interpolator.cc
 * Interpolates eigenrays onto a new frequency axis.
 */
#include <usml/waveq3d/eigenray_interpolator.h>
#include <boost/foreach.hpp>

using namespace usml::waveq3d;

/**
 * Construct interpolating data_grid object on original frequency scale.
 */
eigenray_interpolator::eigenray_interpolator(
		const seq_vector* freq, const seq_vector* new_freq)
{
	_new_freq = new_freq;
	const seq_vector* ax[1] = { freq };
	_freq_size = freq->size();
	_intensity_interp = new data_grid<double, 1>(ax);
	_phase_interp = new data_grid<double, 1>(ax);
}

/**
 * Destroy interpolating data_grid objects.
 */
eigenray_interpolator::~eigenray_interpolator() {
	delete _intensity_interp;
	delete _phase_interp;
}

/**
 * Interpolate frequency dependent terms onto a new frequency axis.
 */
void eigenray_interpolator::interpolate(
		const eigenray_list& eigenrays, eigenray_list* new_eigenrays)
{
	// fill the interpolating data_grids with data

    std::list<eigenray>::iterator new_eigenray_list_iter;
    new_eigenray_list_iter = new_eigenrays->begin();

    BOOST_FOREACH (eigenray ray, eigenrays) {
        for (size_t f = 0; f < _freq_size; ++f) {
            _intensity_interp->data(&f, ray.intensity[f]);
            _phase_interp->data(&f, ray.phase[f]);
        }
        // copy terms that are not frequency dependent
        eigenray& new_ray = *new_eigenray_list_iter;

        new_ray.time = ray.time ;
        new_ray.source_de = ray.source_de ;
        new_ray.source_az = ray.source_az ;
        new_ray.target_de = ray.target_de ;
        new_ray.target_az = ray.target_az ;
        new_ray.surface = ray.surface ;
        new_ray.bottom = ray.bottom ;
        new_ray.caustic = ray.caustic ;
        new_ray.upper = ray.upper ;
        new_ray.lower = ray.lower ;

        // interpolate results to new frequency axis
        // assume that calling routine has set new_eigenrays->freq

        for (size_t f = 0; f < _new_freq->size(); ++f) {
            double freq = (*_new_freq)[f] ;
            new_ray.intensity[f] = _intensity_interp->interpolate( &freq );
            new_ray.phase[f] = _phase_interp->interpolate( &freq );
        }
        ++new_eigenray_list_iter;
    }
}

max_stars_repo_name: fraclipe/UnderSeaModelingLibrary

max_issues_repo_name: Wolframy-NUDT/UnderSeaModelingLibrary

max_forks_repo_name: Wolframy-NUDT/UnderSeaModelingLibrary


text:
#include <frovedis.hpp>

#define BOOST_TEST_MODULE FrovedisTest
#include <boost/test/unit_test.hpp>

using namespace frovedis;
using namespace std;

BOOST_AUTO_TEST_CASE( frovedis_test )
{
  int argc = 1;
  char** argv = NULL;
  use_frovedis use(argc, argv);

  // filling sample input map
  auto d1 = frovedis::make_dunordered_map_allocate<std::string,int>();

  d1.put("apple", 10);
  d1.put("orange", 20);

  auto x = d1.get("apple");
  d1.put("apple", 50);
  auto y = d1.get("apple");

  BOOST_CHECK(x == 10 && y == 50);
}

max_stars_repo_name: XpressAI/frovedis

max_issues_repo_name: XpressAI/frovedis

max_forks_repo_name: XpressAI/frovedis


text:
#include <ozo/connection_info.h>
#include <ozo/request.h>
#include <ozo/shortcuts.h>

#include <boost/asio/io_service.hpp>
#include <boost/asio/spawn.hpp>

#include <iostream>

namespace asio = boost::asio;

// adapted from examples/request.cpp
int main(int argc, char **argv) {
    asio::io_context io;
    auto conn_info = ozo::connection_info("!!invalid_connection_string");

    const auto coroutine = [&] (asio::yield_context yield) {
        ozo::rows_of<int> result;
        ozo::error_code ec;
        using namespace ozo::literals;
        using namespace std::chrono_literals;
        const auto connection = ozo::request(conn_info[io], "SELECT 1"_SQL, 1ns, ozo::into(result), yield[ec]);
    };

    asio::spawn(io, coroutine);
    io.run();

    return 0;
}

max_stars_repo_name: rockandsalt/conan-center-index

max_issues_repo_name: rockandsalt/conan-center-index

max_forks_repo_name: rockandsalt/conan-center-index


text: #include <boost/spirit/home/x3/operator/alternative.hpp>

ext: hpp

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#ifndef CDTree_H
#define CDTree_H

#include <iostream>
#include <fstream>  //读取文件内容
#include <map>
#include <vector>
#include <string>
#include <Eigen/Dense>
#include <cmath>

using std::cout; using std::cin; using std::cerr; using std::endl;
using std::string; using std::ifstream; using std::istringstream;
using std::vector; using std::map; using std::ios;

using Eigen::MatrixXf;   //Eigen Matrix float 存储数据  训练数据
using Eigen::MatrixXi;  //Eigen Matrix int 存储数据  训练数据和标签
using Eigen::VectorXi;
using Eigen::VectorXf;

class CDTree
{
public:
    struct TreeNode
    {   //节点信息
        TreeNode* parents;  //当前节点的双亲节点
        int AttributeIndex;   //此节点对应的属性索引
        bool LeafNode; //判断是否为叶子节点
        vector<TreeNode*> children; //孩子节点的地址。
        int label;  //if it is a leaf node then this prameter is useful

    };

    struct IntervalTree
    {
        IntervalTree *leftTree;
        IntervalTree *rightTree;
        bool LeafNode;
        float cutValue;
        IntervalTree *parients;
    };

    vector<int> ColsIndex;  //所有数据的列
    TreeNode* root;
    int deepestTree; // deepest depth of the tree
    int max_bin;   // max number of interval on every  continous parameter's cut
    float thresholdInfoGain; //在计算
    int trainX_Dimension;

	CDTree(int deepestTree, int max_bin, float thresholdInfoGain);
    CDTree();
    int buildTree(const MatrixXf &trainX, const MatrixXi &trainY, const MatrixXf &validateX, const MatrixXi &validateY, string Algorithm);
    int buildTree(const MatrixXf &trainX, const MatrixXi &trainY, string Algorithm);
    MatrixXi predict(const MatrixXf &testX);
    ~CDTree();

private:
    vector<vector<std::pair<float, float>>> intervals;  // 每个属性的区间分割
    vector<vector<std::pair<float, float>>> continous2discrete(const MatrixXf &trainX, const MatrixXi &trainY);
    IntervalTree* MultiwayPartitionGain(IntervalTree *parientsNode, const vector<float> &continuousVec, const MatrixXf &trainX, const MatrixXi &trainY, int index, float thresholdInfoGain);
    int getIntervalsFromTreeStruct(IntervalTree *Intervalroot, vector<float> &intervals);
    int sortVectorXf(const VectorXf &vec, VectorXf &sorted_vec, VectorXi &ind);
    int cutBranches(const MatrixXf &validateX, const MatrixXi &validateY);
    int cutBranches(int deepthTree);
    TreeNode* AlgorithmID3(TreeNode *parients, const MatrixXf &trainX, const MatrixXi &trainY, vector<int> attrrbuteIndexOfCurrentData);
	TreeNode* AlgorithmC4_5(const MatrixXf &trainX, const MatrixXi &trainY);
	TreeNode* AlgorithmCART(const MatrixXf &trainX, const MatrixXi &trainY);
    std::pair<MatrixXf, MatrixXi> splitData(const MatrixXf &trainX, const MatrixXi &trainY, std::pair<float, float> smallInterval, int maxIndex, string flag);
    int FindMaxInformationGain(vector<float> s);
    vector<float> CalculateInfGain(const MatrixXf &trainX, const MatrixXi &trainY, vector<int> attributeIndexOfCurrentData);
    vector<float> getSampleProbability(const MatrixXi &trainY);
    vector<float> removeColRepetitionValue(const MatrixXf &Col);
    MatrixXi findDvlabel(const MatrixXf &trainX, const MatrixXi &trainY, int index, std::pair<float, float> smallInterval, string flag);
    float calculateEntropy(vector<float> probability);
    bool TheSameLabel(const MatrixXi &trainY);
    int chooseMostLabel(const MatrixXi &trainY);
    int findExistandDealwith(int label, vector<std::pair<int, int>> &labelAndNum);
    int destroyTree(TreeNode* root);
    int destroyIntervalTree(IntervalTree *root);

    template<typename Type>
    int KMeans(vector<Type> vec, vector<Type> &newvec, int index);
    int predictTree(TreeNode *node, const MatrixXf testX);

};

#endif

max_stars_repo_path: C++ source Code/Continuous/CDTree.hpp

max_stars_repo_name: PiggyGaGa/MachineLearning-DecisionTree

max_issues_repo_path: C++ source Code/Continuous/CDTree.hpp

max_issues_repo_name: PiggyGaGa/MachineLearning-DecisionTree

max_forks_repo_path: C++ source Code/Continuous/CDTree.hpp

max_forks_repo_name: PiggyGaGa/MachineLearning-DecisionTree


text:
#include "vargraph.h"

#include "modules/io/config.h"
#include "modules/io/file_io.h"
#include "modules/io/zip.h"
#include "modules/bio_base/reference.h"
#include "modules/bio_base/seqset.h"
#include "modules/bio_base/readmap.h"

#include "modules/bio_base/seqset_testutil.h"
#include "modules/bio_base/dna_testutil.h"
#include "modules/bio_base/reference_testutil.h"

#include <boost/algorithm/string.hpp>
#include <gtest/gtest.h>

using namespace dna_testutil;

typedef std::tuple<uint32_t, uint32_t, bool, dna_sequence, int, int, int, int> node_result;

std::string str_vec(const std::vector<int>& vec) {
  std::string r = "[";
  for(size_t i = 0; i < vec.size(); i++) {
    r += std::to_string(vec[i]);
    if (i != vec.size() - 1) { r += " "; }
  }
  r += "]";
  return r;
}

void dump_cov(const char* name, const vargraph::cov_info_t& ci) {
  SPLOG("  %s:", name);
  SPLOG("    base_cov: %s", str_vec(ci.base_cov).c_str());
  SPLOG("    span_cov: %s", str_vec(ci.span_cov).c_str());
};

void dump_graph(vargraph* vg, bool full_cov = true)
{
  SPLOG("Dump of vargraph with full_cov = %s", full_cov ? "true" : "false");
  for(const auto& kvp : vg->get_nodes()) {
    SPLOG("%s", kvp.second->as_string().c_str());
    if (full_cov) {
      dump_cov("unpaired", kvp.second->unpaired);
      dump_cov("paired", kvp.second->paired);
    }
  }
  for(const auto& e : vg->get_edges()) {
    SPLOG("%s->%s", e->upstream->as_string().c_str(), e->downstream->as_string().c_str());
    SPLOG("  unpaired: %u, paired: %u", e->unpaired, e->paired);
  }
}

void dump_reads(std::vector<std::vector<dna_sequence>> fake_reads) {
  for(auto const& read : fake_reads) {
    if (read.size() == 1) {
      SPLOG("unpr %s", read[0].as_string().c_str());
    } else if (read.size() == 2) {
      SPLOG("pair %s %s", read[0].as_string().c_str(), read[1].as_string().c_str());
    }
  }
}

vargraph::node_t* verify_node(vargraph* vg, uint32_t start, uint32_t end, bool is_ref, dna_sequence seq) {
  auto range = vg->get_nodes().equal_range(start);
  for(auto it = range.first; it != range.second; ++it) {
    vargraph::node_t* n = it->second;
    if (n->start == start &&
        n->end == end &&
        n->is_ref == is_ref &&
        n->seq == seq) {
      return n;
    }
  }
  EXPECT_TRUE(false) << "No matching node with sequence " << seq << " (is_ref=" << is_ref << ")";
  return nullptr;
}

void verify_edge_internal(vargraph* vg, vargraph::node_t* upstream, vargraph::node_t* downstream, uint32_t unpaired, uint32_t paired) {
  for(vargraph::edge_t* e : vg->get_edges()) {
    if (e->upstream == upstream && e->downstream == downstream) {
      EXPECT_EQ(e->unpaired, unpaired) << e->as_string();
      EXPECT_EQ(e->paired, paired) << e->as_string();
      return;
    }
  }
  EXPECT_TRUE(false) << "No matching node with upstream=" << upstream->as_string()
                     << " downstream=" << downstream->as_string();
}

#define VERIFY_EDGE(vg, upstream, downstream, unpaired, paired)                         \
  do {                                                                                \
    SCOPED_TRACE("verify_edge(" #vg ", upstream=" #upstream ", downstream=" #downstream \
                 ", unpaired=" #unpaired ", paired=" #paired ")");                      \
    verify_edge_internal(vg, upstream, downstream, unpaired, paired);                   \
  } while (false)

TEST(vargraph, unpaired_hom_test) {
  /* Deletion >> abcd     jklm << Insertion
     @[50-60)     bcd-f    klmR   @[120, 120)
                   cd-fg    lmRn
                    d-fgh    mRno
                      fghi    Rnop
                               nopq
                 abcdefghijklmnopqrstuvwxyz << do a repl/snp also?
  */
  std::vector<std::vector<dna_sequence>> fake_reads =
                {
                  //unpaired
                  {tseq("abcd")},
                  {tseq("bcdf")},
                  {tseq("cdfg")},
                  {tseq("dfgh")},
                  {tseq("fghi")},
                  {tseq("jklm")},
                  {tseq("klmR")},
                  {tseq("lmRn")},
                  {tseq("mRno")},
                  {tseq("Rnop")},
                  {tseq("nopq")}
                 };

  //Fake reference & BioGraph
  std::unique_ptr<const reference> ref;
  dna_sequence ref_seq = tseq("abcdefghijklmnopqrstuvwxyz");
  int tsize = ref_seq.size();
  ref = create_reference({ref_seq});

  auto m_biograph = biograph_for_reads(fake_reads);
  std::shared_ptr<const seqset> m_seqset;
  m_seqset = std::move(m_biograph.first);
  auto m_readmap = std::move(m_biograph.second);

  // Build VarGraph
  vargraph *vg = nullptr;
  vg = new vargraph(ref_seq);

  //Fake some variants
  //C insertion -- no coverage
  // When tsize > len - no error is thrown
  //vg->add_variant(24, 24, dna_C);
  //This doesn't work -- need to know what the dna_sequence expands to...
  //10bp deletion of 'c'
  vg->add_variant(40, 50, dna_sequence(""));
  //10bp insertion of R
  vg->add_variant(130, 130, tseq("R"));
  // Do trace
  vg->trace(*m_seqset, *m_readmap.get(), 0, tsize);

  dump_graph(vg, true);

  // Verify nodes
  EXPECT_EQ(vg->get_nodes().size(), 5);
  auto n1 = verify_node(vg, 0, 40, true, tseq("abcd"));
  auto n2 = verify_node(vg, 40, 50, true, tseq("e"));
  auto n3 = verify_node(vg, 50, 130, true, tseq("fghijklm"));
  auto n4 = verify_node(vg, 130, 130, false, tseq("R"));
  auto n5 = verify_node(vg, 130, 260, true, tseq("nopqrstuvwxyz"));
  // Verify edges
  EXPECT_EQ(vg->get_edges().size(), 6);
  VERIFY_EDGE(vg, n1, n2, 0, 0);
  VERIFY_EDGE(vg, n2, n3, 0, 0);
  VERIFY_EDGE(vg, n1, n3, 3, 0); // Deletion ede
  VERIFY_EDGE(vg, n3, n4, 3, 0);
  VERIFY_EDGE(vg, n4, n5, 3, 0);
  VERIFY_EDGE(vg, n3, n5, 0, 0);
  // Verify some interesting coverage points
  // Check that span coverage drops to 0 at the exact point where fghi and ijkl fail to overlap
  EXPECT_EQ(n3->unpaired.span_cov[38], 1);
  EXPECT_EQ(n3->unpaired.span_cov[39], 0);
  EXPECT_EQ(n3->unpaired.span_cov[40], 1);
  // Check base coverage on inserted base (which is only good if reads split across nodes)
  EXPECT_EQ(n4->unpaired.base_cov[5], 4);
  // Check deleted node coverage is 0
  EXPECT_EQ(n2->unpaired.base_cov[5], 0);

  dump_graph(vg);
  dump_reads(fake_reads);
  SPLOG("ref %s", ref_seq.as_string().c_str());

  delete vg;
}

// TEST(vargraph, paired_hom_test) {
//   /* Deletion >> abcd     jklm << Insertion
//      @[50-60)     bcd-f    klmR   @[120, 120)
//                    cd-fg    lmRn
//                     d-fgh    mRno
//                       fghi    Rnop
//                                nopq    wxyz < pair for all
//                                               so a:i don't have
//                  abcdefghijklmnopqrstuvwxyz
//                           | mx 200 insert |
//   */

//   std::vector<std::vector<dna_sequence>> fake_reads =
//                 {
//                   //unpaired -- Need to check directionality, too
//                   //paired - outof distance
//                   {tseq("abcd"), tseq_rc("wxyz")},
//                   {tseq("bcdf"), tseq_rc("wxyz")},
//                   {tseq("cdfg"), tseq_rc("wxyz")},
//                   {tseq("dfgh"), tseq_rc("wxyz")},
//                   {tseq("fghi"), tseq_rc("wxyz")},
//                   //paired - in distance
//                   {tseq("jklm"), tseq_rc("wxyz")},
//                   {tseq("klmR"), tseq_rc("wxyz")},
//                   {tseq("lmRn"), tseq_rc("wxyz")},
//                   {tseq("mRno"), tseq_rc("wxyz")},
//                   {tseq("Rnop"), tseq_rc("wxyz")},
//                   {tseq("nopq"), tseq_rc("wxyz")},
//                  };

//   //Fake reference & BioGraph
//   std::unique_ptr<const reference> ref;
//   dna_sequence ref_seq = tseq("abcdefghijklmnopqrstuvwxyz");
//   int tsize = ref_seq.size();
//   ref = create_reference({ref_seq});

//   auto m_biograph = biograph_for_reads(fake_reads);
//   std::shared_ptr<const seqset> m_seqset;
//   m_seqset = std::move(m_biograph.first);
//   auto m_readmap = std::move(m_biograph.second);

//   // Build VarGraph
//   vargraph *vg = nullptr;
//   vg = new vargraph(ref_seq, 10, 200);

//   //Fake some variants
//   //C insertion -- no coverage
//   // When tsize > len - no error is thrown
//   //vg->add_variant(24, 24, dna_C);
//   //This doesn't work -- need to know what the dna_sequence expands to...
//   //10bp deletion of 'c'
//   vg->add_variant(40, 50, dna_sequence("")); // deletion doesn't make a 'node' anymore
//   //10bp insertion of R
//   vg->add_variant(130, 130, tseq("R"));
//   //Then trace
//   vg->trace(*m_seqset, *m_readmap.get(), 0, tsize);

//   //Verify nodes exist
//   EXPECT_EQ(vg->get_nodes().size(), 5);
//   auto n1 = verify_node(vg, 0, 40, true, tseq("abcd"));
//   auto n2 = verify_node(vg, 40, 50, true, tseq("e"));
//   auto n3 = verify_node(vg, 50, 130, true, tseq("fghijklm"));
//   auto n4 = verify_node(vg, 130, 130, false, tseq("R"));
//   auto n5 = verify_node(vg, 130, 260, true, tseq("nopqrstuvwxyz"));

//   // Verify edges
//   EXPECT_EQ(vg->get_edges().size(), 6);
//   VERIFY_EDGE(vg, n1, n2, 0, 0);
//   VERIFY_EDGE(vg, n2, n3, 0, 0);
//   VERIFY_EDGE(vg, n1, n3, 3, 0); // Deletion edge - all unpaired
//   VERIFY_EDGE(vg, n3, n4, 0, 3); // Insertion edge in - all paired
//   VERIFY_EDGE(vg, n4, n5, 0, 3); // Insertion edge out - all paired
//   VERIFY_EDGE(vg, n3, n5, 0, 0); // Ref - no support

//   //should put interesting point checks
//   dump_graph(vg);
//   EXPECT_EQ(n3->unpaired.span_cov[0], 4);
//   EXPECT_EQ(n3->paired.span_cov[0], 0);
//   EXPECT_EQ(n4->paired.span_cov[0], 4);
//   EXPECT_EQ(n4->paired.span_cov[8], 4);
//   EXPECT_EQ(n4->unpaired.span_cov[9], 4);
//   EXPECT_EQ(n5->paired.base_cov[0], 4);
//   EXPECT_EQ(n5->unpaired.base_cov[0], 0);
//   EXPECT_EQ(n5->unpaired.span_cov[128], 5);
//   EXPECT_EQ(n5->paired.span_cov[128], 6);

//   //Then look at the results
//   dump_graph(vg);
//   dump_reads(fake_reads);
//   SPLOG("ref %s", ref_seq.as_string().c_str());

//   delete vg;
// }

TEST(vargraph, redundant_read_test) {
  /* what happens if we have redundancy in the ref sequence?
     on one copy, we'll have proper pairs, on another we'll have improper pairs
     Does the vargraph 1) only use the read once? if so where?
        2) use the read is used twice, once proper, once improper, are they correctly paired/unpaired

  ref abcdefghijklcdefgmnopqrstuvwxyz
        ^rep1     ^rep2

                 lcde   nopq
                  cdef   opqr
                   defg   pqrs
                    efgm   qrst
        Max Insert 150
        Min Insert 50 should be fine
  */
  //Fake reference & BioGraph
  std::unique_ptr<const reference> ref;
  dna_sequence ref_seq = tseq("abcdefghijklcdefgmnopqrstuvwxy");
  int tsize = ref_seq.size();
  ref = create_reference({ref_seq});

  std::vector<std::vector<dna_sequence>> fake_reads =
                {
                  {tseq("lcde"), tseq_rc("nopq")},
                  {tseq("cdef"), tseq_rc("opqr")}, //r1 repeat
                  {tseq("defg"), tseq_rc("pqrs")}, //r1 repeat
                  {tseq("efgm"), tseq_rc("qrst")}
                };

  auto m_biograph = biograph_for_reads(fake_reads);
  std::shared_ptr<const seqset> m_seqset;
  m_seqset = std::move(m_biograph.first);
  auto m_readmap = std::move(m_biograph.second);

  // Build VarGraph
  vargraph *vg = nullptr;
  vg = new vargraph(ref_seq, 50, 150);
  // No variants
  vg->trace(*m_seqset, *m_readmap.get(), 0, tsize);

  dump_graph(vg);
  EXPECT_EQ(vg->get_nodes().size(), 1);
  auto n1 = verify_node(vg, 0, 300, true, ref_seq);
  //The repeats make the cdef used twice
  EXPECT_EQ(n1->paired.base_cov[30], 0);
  EXPECT_EQ(n1->unpaired.base_cov[30], 2); // the l read's d not counted
  EXPECT_EQ(n1->paired.base_cov[130], 3);
  EXPECT_EQ(n1->unpaired.base_cov[130], 0);
}

TEST(vargraph, het_test) {
  /* Deletion >> abcd     jklm << Insertion
     @[50-60)     bcd-f    klmR   @[120, 120)
                   cd-fg    lmRn
                    d-fgh    mRno
                      fghi    Rnop
                               nopq    wxyz < pair for all up to now
                                              so a:i don't have proper pair
                  bcde      klmn
                   cdef      lmno
                    defg      mnop  < pairs supporting refernce
                     efgh    lmno   <  unpaired supporting reference
                 abcdefghijklmnopqrstuvwxyz
                          | mx 200 insert |

  */

  std::vector<std::vector<dna_sequence>> fake_reads =
                {
                  //paired - out of distance
                  {tseq("abcd"), tseq_rc("wxyz")},
                  {tseq("bcdf"), tseq_rc("wxyz")},
                  {tseq("cdfg"), tseq_rc("wxyz")},
                  {tseq("dfgh"), tseq_rc("wxyz")},
                  {tseq("fghi"), tseq_rc("wxyz")},
                  //paired - in distance
                  {tseq("jklm"), tseq_rc("wxyz")},
                  {tseq("klmR"), tseq_rc("wxyz")},
                  {tseq("lmRn"), tseq_rc("wxyz")},
                  {tseq("mRno"), tseq_rc("wxyz")},
                  {tseq("Rnop"), tseq_rc("wxyz")},
                  {tseq("nopq"), tseq_rc("wxyz")},
                  //paired ref allele reads
                  {tseq("bcde"), tseq_rc("klmn")},
                  {tseq("cdef"), tseq_rc("lmno")},
                  {tseq("defg"), tseq_rc("mnop")},
                  //unpaired ref allele reads
                  {tseq("efgh")},
                  {tseq_rc("lmno")},
                 };

  //Fake reference & BioGraph
  std::unique_ptr<const reference> ref;
  dna_sequence ref_seq = tseq("abcdefghijklmnopqrstuvwxyz");
  int tsize = ref_seq.size();
  ref = create_reference({ref_seq});

  auto m_biograph = biograph_for_reads(fake_reads);
  std::shared_ptr<const seqset> m_seqset;
  m_seqset = std::move(m_biograph.first);
  auto m_readmap = std::move(m_biograph.second);

  // Build VarGraph
  vargraph *vg = nullptr;
  vg = new vargraph(ref_seq, 10, 200);

  //Fake some variants
  //C insertion -- no coverage
  // When tsize > len - no error is thrown
  //vg->add_variant(24, 24, dna_C);
  //This doesn't work -- need to know what the dna_sequence expands to...
  //10bp deletion of 'c'
  vg->add_variant(40, 50, dna_sequence("")); // deletion doesn't make a 'node' anymore
  //10bp insertion of R
  vg->add_variant(130, 130, tseq("R"));
  //Then trace
  vg->trace(*m_seqset, *m_readmap.get(), 0, tsize);

  //Verify nodes exist
  ASSERT_EQ(vg->get_nodes().size(), 5);
  auto n1 = verify_node(vg, 0, 40, true, tseq("abcd"));
  auto n2 = verify_node(vg, 40, 50, true, tseq("e"));
  auto n3 = verify_node(vg, 50, 130, true, tseq("fghijklm"));
  auto n4 = verify_node(vg, 130, 130, false, tseq("R"));
  auto n5 = verify_node(vg, 130, 260, true, tseq("nopqrstuvwxyz"));

  // Verify edges
  ASSERT_EQ(vg->get_edges().size(), 6);
  VERIFY_EDGE(vg, n1, n2, 0, 3);
  VERIFY_EDGE(vg, n2, n3, 1, 2); // Ref allele
  VERIFY_EDGE(vg, n1, n3, 3, 0); // Deletion edge - all unpaired
  VERIFY_EDGE(vg, n3, n4, 0, 3); // Insertion edge in - all paired
  VERIFY_EDGE(vg, n4, n5, 0, 3); // Insertion edge out - all paired
  VERIFY_EDGE(vg, n3, n5, 1, 3); // Ref allele

  //should put interesting point checks
  dump_graph(vg);
  EXPECT_EQ(n3->unpaired.span_cov[0], 5);
  EXPECT_EQ(n3->paired.span_cov[0], 2);
  EXPECT_EQ(n4->paired.span_cov[0], 4);
  EXPECT_EQ(n4->paired.span_cov[8], 4);
  EXPECT_EQ(n5->paired.base_cov[0], 7);
  EXPECT_EQ(n5->unpaired.base_cov[0], 1);
  EXPECT_EQ(n5->unpaired.span_cov[128], 5);
  EXPECT_EQ(n5->paired.span_cov[128], 6);

  //Then look at the results
  dump_graph(vg);
  dump_reads(fake_reads);
  SPLOG("ref %s", ref_seq.as_string().c_str());

  delete vg;
}

TEST(vargraph, multi_pair) {
  /*
  What happens if a read maps twice within a node?

  abcdefghijklmnopqrstjklmnz
   bcd-f   jklm       jklm
   readaln1  read2aln    read2aln2
  */

  std::vector<std::vector<dna_sequence>> fake_reads =
                {
                  //paired - both distance
                  {tseq("bcdf"), tseq_rc("jklm")},
                 };

  //Fake reference & BioGraph
  std::unique_ptr<const reference> ref;
  dna_sequence ref_seq = tseq("abcdefghijklmnopqrstjklmnz");
  int tsize = ref_seq.size();
  ref = create_reference({ref_seq});

  auto m_biograph = biograph_for_reads(fake_reads);
  std::shared_ptr<const seqset> m_seqset;
  m_seqset = std::move(m_biograph.first);
  auto m_readmap = std::move(m_biograph.second);

  // Build VarGraph
  vargraph *vg = nullptr;
  vg = new vargraph(ref_seq, 10, 200);

  //Fake some variants
  //C insertion -- no coverage
  // When tsize > len - no error is thrown
  //vg->add_variant(24, 24, dna_C);
  //This doesn't work -- need to know what the dna_sequence expands to...
  //10bp deletion of 'c'
  vg->add_variant(40, 50, dna_sequence("")); // deletion doesn't make a 'node' anymore

  vg->trace(*m_seqset, *m_readmap.get(), 0, tsize);

  //Verify nodes exist
  ASSERT_EQ(vg->get_nodes().size(), 3);
  auto n1 = verify_node(vg, 0, 40, true, tseq("abcd"));
  auto n2 = verify_node(vg, 40, 50, true, tseq("e"));
  auto n3 = verify_node(vg, 50, 260, true, tseq("fghijklmnopqrstjklmnz"));

  ASSERT_EQ(vg->get_edges().size(), 3);
  VERIFY_EDGE(vg, n1, n2, 0, 0);
  VERIFY_EDGE(vg, n2, n3, 0, 0);
  VERIFY_EDGE(vg, n1, n3, 0, 1);

  //should put interesting point checks
  dump_graph(vg);

  //Then look at the results
  dump_graph(vg);
  dump_reads(fake_reads);
  SPLOG("ref %s", ref_seq.as_string().c_str());

  delete vg;
}

TEST(vargraph, insert_pair_test) {
  /*
  Can I break the insert?
             /G\
  abcdefghijkl  mnopqrstuvwxyz
             \A/
           jklGm       tuvw   : fail - insertsize is 151
           jklGmnopq          : fail - 81
           jklAm   pqrs       : pass 101

   min_pair_dist = 101
   max_pair_dist = 101
   bcde should fail. pqrs should pass. nopq should fail

   readaln1  read2aln    read2aln2
  */

  std::vector<std::vector<dna_sequence>> fake_reads =
                { //paired - both distance
                  {tseq("jkl") + dna_G + tseq("m"), tseq_rc("tuvw"),  },
                  {tseq("jkl") + dna_G + tseq("m"), tseq_rc("nopq"),  },
                  {tseq("jkl") + dna_A + tseq("m"), tseq_rc("pqrs") },
                 };

  //Fake reference & BioGraph
  std::unique_ptr<const reference> ref;
  dna_sequence ref_seq = tseq("abcdefghijkl") + dna_G + tseq("mnopqrstuvwxyz");
  int tsize = ref_seq.size();
  ref = create_reference({ref_seq});

  auto m_biograph = biograph_for_reads(fake_reads);
  std::shared_ptr<const seqset> m_seqset;
  m_seqset = std::move(m_biograph.first);
  auto m_readmap = std::move(m_biograph.second);

  // Build VarGraph
  vargraph *vg = nullptr;
  vg = new vargraph(ref_seq, 101, 101);

  //Fake some variants - not needed since it's not indel. het snp
  vg->add_variant(120, 121, dna_A);
  vg->trace(*m_seqset, *m_readmap.get(), 0, tsize);

  //Verify nodes exist
  ASSERT_EQ(vg->get_nodes().size(), 4);
  auto n1 = verify_node(vg, 0, 120, true, tseq("abcdefghijkl"));
  auto n2 = verify_node(vg, 120, 121, true, dna_G);
  auto n3 = verify_node(vg, 120, 121, false, dna_A);
  auto n4 = verify_node(vg, 121, 261, true, tseq("mnopqrstuvwxyz"));

  ASSERT_EQ(vg->get_edges().size(), 4);
  VERIFY_EDGE(vg, n1, n2, 2, 0);
  VERIFY_EDGE(vg, n1, n3, 0, 1);
  VERIFY_EDGE(vg, n2, n4, 2, 0);
  VERIFY_EDGE(vg, n3, n4, 0, 1);

  //Then look at the results
  dump_graph(vg);
  dump_reads(fake_reads);
  SPLOG("ref %s", ref_seq.as_string().c_str());

  delete vg;
}

TEST(vargraph, insert_pair_test2) {
  /*
  Can I break the insert?
              /G\  /-----\
  abcdefghijkl   mn-opqrs-tuvwxyz
              \A/
 read1     jkl G m         uvwx  : pass - insertsize is 101 because I'll make a 50bp del
 read2     jkl G mn opq          : fail - 81
 read3     jkl A m   pqrs        : pass 101

   min_pair_dist = 101
   max_pair_dist = 101

  OKAY! When I run this with 151 max (insert of read1 going through reference allele over deletion)
        I get correct paired coverage on the G ref allele
  BUT I do not get correct paired coverage on it's up/dn node - so the coverage goes 2-1-2!!

  ALSO! When I run this test with 101 max (insert of read1 going through the deletion allele) it stays 2-2-2.

  SO! this is where the problem is. It's not properly marking pairs on all reads in all nodes!
  I'm suspicious of the reachable
  */

  std::vector<std::vector<dna_sequence>> fake_reads =
                { //paired - both distance
                  {tseq("jkl") + dna_G + tseq("m"), tseq_rc("uvwx"),  },
                  {tseq("jkl") + dna_G + tseq("m"), tseq_rc("nopq"),  },
                  {tseq("jkl") + dna_A + tseq("m"), tseq_rc("pqrs") },
                 };

  //Fake reference & BioGraph
  std::unique_ptr<const reference> ref;
  dna_sequence ref_seq = tseq("abcdefghijkl") + dna_G + tseq("mnopqrstuvwxyz");
  int tsize = ref_seq.size();
  ref = create_reference({ref_seq});

  auto m_biograph = biograph_for_reads(fake_reads);
  std::shared_ptr<const seqset> m_seqset;
  m_seqset = std::move(m_biograph.first);
  auto m_readmap = std::move(m_biograph.second);

  // Build VarGraph
  vargraph *vg = nullptr;
  vg = new vargraph(ref_seq, 101, 200);

  //Fake some variants - not needed since it's not indel. het snp
  vg->add_variant(120, 121, dna_A);
  vg->add_variant(141, 191, dna_sequence(""));
  vg->trace(*m_seqset, *m_readmap.get(), 0, tsize);

  //Verify nodes exist
  ASSERT_EQ(vg->get_nodes().size(), 6);
  auto jkl = verify_node(vg, 0, 120, true, tseq("abcdefghijkl"));
  auto g_insert = verify_node(vg, 120, 121, true, dna_G);
  auto a_insert = verify_node(vg, 120, 121, false, dna_A);
  auto mn = verify_node(vg, 121, 141, true, tseq("mn"));
  auto opqrs = verify_node(vg, 141, 191, true, tseq("opqrs"));
  auto tuv = verify_node(vg, 191, 261, true, tseq("tuvwxyz"));

  ASSERT_EQ(vg->get_edges().size(), 7);
  VERIFY_EDGE(vg, jkl, g_insert, 1, 1);
  VERIFY_EDGE(vg, jkl, a_insert, 0, 1);
  VERIFY_EDGE(vg, g_insert, mn, 1, 1);
  VERIFY_EDGE(vg, a_insert, mn, 0, 1);
  VERIFY_EDGE(vg, mn, opqrs, 1, 0);
  VERIFY_EDGE(vg, opqrs, tuv, 0, 0);

  //Then look at the results
  dump_graph(vg);
  dump_reads(fake_reads);
  SPLOG("ref %s", ref_seq.as_string().c_str());

  delete vg;
}

TEST(vargraph, insert_pair_test3) {
  /*
  This is the same as insert_pair_test2 except I'm going to have read1's dn pair go over the del edge
  It also fails for the same reason. So hitting a new edge doesn't update the info
              /G\  /-----\
  abcdefghijkl   mn-opqrs-tuvwxyz
              \A/
 read1     jkl G m      s tuv    : pass - insertsize is 101 because I'll make a 50bp del
 read2     jkl G mn opq          : fail - 81
 read3     jkl A m   opqrs       : pass 101

   min_pair_dist = 101
   max_pair_dist = 101

  OKAY! When I run this with 151 max (insert of read1 going through reference allele over deletion)
        I get correct paired coverage on the G ref allele
  BUT I do not get correct paired coverage on it's up/dn node - so the coverage goes 2-1-2!!

  ALSO! When I run this test with 101 max (insert of read1 going through the deletion allele) it stays 2-2-2.

  SO! this is where the problem is. It's not properly marking pairs on all reads in all nodes!
  I'm suspicious of the reachable
  */

  std::vector<std::vector<dna_sequence>> fake_reads =
                { //paired - both distance
                  {tseq("jkl") + dna_G + tseq("m"), tseq_rc("stuv"),  },
                  {tseq("jkl") + dna_G + tseq("m"), tseq_rc("nopq"),  },
                  {tseq("jkl") + dna_A + tseq("m"), tseq_rc("pqrs") },
                 };

  //Fake reference & BioGraph
  std::unique_ptr<const reference> ref;
  dna_sequence ref_seq = tseq("abcdefghijkl") + dna_G + tseq("mnopqrstuvwxyz");
  int tsize = ref_seq.size();
  ref = create_reference({ref_seq});

  auto m_biograph = biograph_for_reads(fake_reads);
  std::shared_ptr<const seqset> m_seqset;
  m_seqset = std::move(m_biograph.first);
  auto m_readmap = std::move(m_biograph.second);

  // Build VarGraph
  vargraph *vg = nullptr;
  vg = new vargraph(ref_seq, 101, 200);

  //Fake some variants - not needed since it's not indel. het snp
  vg->add_variant(120, 121, dna_A);
  vg->add_variant(141, 191, dna_sequence(""));
  vg->trace(*m_seqset, *m_readmap.get(), 0, tsize);

  //Verify nodes exist
  ASSERT_EQ(vg->get_nodes().size(), 6);
  auto n1 = verify_node(vg, 0, 120, true, tseq("abcdefghijkl"));
  auto n2 = verify_node(vg, 120, 121, true, dna_G);
  auto n3 = verify_node(vg, 120, 121, false, dna_A);
  auto n4 = verify_node(vg, 121, 141, true, tseq("mn"));
  auto n5 = verify_node(vg, 141, 191, true, tseq("opqrs"));
  auto n6 = verify_node(vg, 191, 261, true, tseq("tuvwxyz"));

  ASSERT_EQ(vg->get_edges().size(), 7);
  VERIFY_EDGE(vg, n1, n2, 1, 1);
  VERIFY_EDGE(vg, n1, n3, 0, 1);
  VERIFY_EDGE(vg, n2, n4, 1, 1);
  VERIFY_EDGE(vg, n3, n4, 0, 1);
  VERIFY_EDGE(vg, n4, n5, 1, 0);
  VERIFY_EDGE(vg, n5, n6, 0, 1);

  //Then look at the results
  dump_graph(vg);
  dump_reads(fake_reads);
  SPLOG("ref %s", ref_seq.as_string().c_str());

  delete vg;
}

TEST(vargraph, insert_culprit) {
  /*
  Just the bad read - I'm going to dump everything from vg to see what it does
  Dude -- this one passes... so now I need to check if I can make the above pass with
  non-identical reads..
              /G\  /-----\
  abcdefghijkl   mn-opqrs-tuvwxyz
              \A/
 read1     jkl G m         uvwx  : pass - insertsize is 151 because I'll make a 50bp del

   min_pair_dist = 101
   max_pair_dist = 101
  */

  std::vector<std::vector<dna_sequence>> fake_reads =
                { //paired - both distance
                  {tseq("jkl") + dna_G + tseq("m"), tseq_rc("uvwx") },
                 };

  //Fake reference & BioGraph
  std::unique_ptr<const reference> ref;
  dna_sequence ref_seq = tseq("abcdefghijkl") + dna_G + tseq("mnopqrstuvwxyz");
  int tsize = ref_seq.size();
  ref = create_reference({ref_seq});

  auto m_biograph = biograph_for_reads(fake_reads);
  std::shared_ptr<const seqset> m_seqset;
  m_seqset = std::move(m_biograph.first);
  auto m_readmap = std::move(m_biograph.second);

  // Build VarGraph
  vargraph *vg = nullptr;
  vg = new vargraph(ref_seq, 101, 200);

  //Fake some variants - not needed since it's not indel. het snp
  vg->add_variant(120, 121, dna_A);
  vg->add_variant(141, 191, dna_sequence(""));
  vg->trace(*m_seqset, *m_readmap.get(), 0, tsize);

  //Verify nodes exist
  ASSERT_EQ(vg->get_nodes().size(), 6);
  auto n1 = verify_node(vg, 0, 120, true, tseq("abcdefghijkl"));
  auto n2 = verify_node(vg, 120, 121, true, dna_G);
  auto n3 = verify_node(vg, 120, 121, false, dna_A);
  auto n4 = verify_node(vg, 121, 141, true, tseq("mn"));
  auto n5 = verify_node(vg, 141, 191, true, tseq("opqrs"));
  auto n6 = verify_node(vg, 191, 261, true, tseq("tuvwxyz"));

  ASSERT_EQ(vg->get_edges().size(), 7);
  VERIFY_EDGE(vg, n1, n2, 0, 1);
  VERIFY_EDGE(vg, n1, n3, 0, 0);
  VERIFY_EDGE(vg, n2, n4, 0, 1);
  VERIFY_EDGE(vg, n3, n4, 0, 0);
  VERIFY_EDGE(vg, n4, n5, 0, 0);
  VERIFY_EDGE(vg, n5, n6, 0, 0);

  //Then look at the results
  dump_graph(vg);
  dump_reads(fake_reads);
  SPLOG("ref %s", ref_seq.as_string().c_str());

  delete vg;
}

TEST(vargraph, insert_culprit_alibi) {
  /*
  This is the same as insert_culprit and insert_test_2, but I'm
  changing the jkl read so that it's mapping to a unique read.
  If this passes, I'm effed
              /G\  /-----\
  abcdefghijkl   mn-opqrs-tuvwxyz
              \A/
 read1     jkl G m         uvwx  : pass - insertsize is 151 or min 101
 read3      kl A mn   qrst       : pass - 101

   min_pair_dist = 101
   max_pair_dist = 200

  OKAY! When I run this with 151 max (insert of read1 going through reference allele over deletion)
        I get correct paired coverage on the G ref allele
  BUT I do not get correct paired coverage on it's up/dn node - so the coverage goes 2-1-2!!

  ALSO! When I run this test with 101 max (insert of read1 going through the deletion allele) it stays 2-2-2.

  SO! this is where the problem is. It's not properly marking pairs on all reads in all nodes!
  I'm suspicious of the reachable
  */

  std::vector<std::vector<dna_sequence>> fake_reads =
                { //paired - both distance
                  {tseq("jkl") + dna_G + tseq("m"), tseq_rc("uvwx"),  },
                  {tseq("kl") + dna_A + tseq("mn"), tseq_rc("qrst") },
                 };

  //Fake reference & BioGraph
  std::unique_ptr<const reference> ref;
  dna_sequence ref_seq = tseq("abcdefghijkl") + dna_G + tseq("mnopqrstuvwxyz");
  int tsize = ref_seq.size();
  ref = create_reference({ref_seq});

  auto m_biograph = biograph_for_reads(fake_reads);
  std::shared_ptr<const seqset> m_seqset;
  m_seqset = std::move(m_biograph.first);
  auto m_readmap = std::move(m_biograph.second);

  // Build VarGraph
  vargraph *vg = nullptr;
  vg = new vargraph(ref_seq, 101, 101);

  //Fake some variants - not needed since it's not indel. het snp
  vg->add_variant(120, 121, dna_A);
  vg->add_variant(141, 191, dna_sequence(""));
  vg->trace(*m_seqset, *m_readmap.get(), 0, tsize);

  //Verify nodes exist
  ASSERT_EQ(vg->get_nodes().size(), 6);
  auto n1 = verify_node(vg, 0, 120, true, tseq("abcdefghijkl"));
  auto n2 = verify_node(vg, 120, 121, true, dna_G);
  auto n3 = verify_node(vg, 120, 121, false, dna_A);
  auto n4 = verify_node(vg, 121, 141, true, tseq("mn"));
  auto n5 = verify_node(vg, 141, 191, true, tseq("opqrs"));
  auto n6 = verify_node(vg, 191, 261, true, tseq("tuvwxyz"));

  ASSERT_EQ(vg->get_edges().size(), 7);
  VERIFY_EDGE(vg, n1, n2, 0, 1);
  VERIFY_EDGE(vg, n1, n3, 0, 1);
  VERIFY_EDGE(vg, n2, n4, 0, 1);
  VERIFY_EDGE(vg, n3, n4, 0, 1);
  VERIFY_EDGE(vg, n4, n5, 0, 0);
  VERIFY_EDGE(vg, n5, n6, 0, 1);

  //Then look at the results
  dump_graph(vg);
  dump_reads(fake_reads);
  SPLOG("ref %s", ref_seq.as_string().c_str());

  delete vg;
}

max_stars_repo_name: spiralgenetics/biograph

max_issues_repo_name: spiralgenetics/biograph

max_forks_repo_name: spiralgenetics/biograph


text:
/**
 * Copyright (c) 2013-2016, Kathrin Rosvall  <krosvall@kth.se>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef __FLEXPERIODICTASKS__
#define __FLEXPERIODICTASKS__

#include <math.h>
#include <iostream>
#include <vector>
#include <bitset>
#include <algorithm>
#include <boost/math/common_factor.hpp>

using namespace std;

class FlexPeriodicTask{
public:
  int phase;
  vector<int> period;
  vector<int> wcec;
  int deadline;
  vector<int> memCons;
  int codeSize;
  bool preemtable;

  FlexPeriodicTask(int _phase, vector<int> _period, vector<int> _wcec, int _deadline, vector<int> _mem, int _cS):
    phase(_phase),
    period(_period),
    wcec(_wcec),
    deadline(_deadline),
    memCons(_mem),
    codeSize(_cS),
    preemtable(true)  {
      sort(period.begin(), period.end());
    };

  FlexPeriodicTask(vector<int> _period, vector<int> _wcec, vector<int> _mem, int _cS):
    phase(0),
    period(_period),
    wcec(_wcec),
    deadline(-1), //TODO: fix this in a meaningful way
    memCons(_mem),
    codeSize(_cS),
    preemtable(true)  {
      sort(period.begin(), period.end());
    };

};

class FlexPeriodicTasks {

protected:
  vector<FlexPeriodicTask> tasks;

public:
  FlexPeriodicTasks();
  FlexPeriodicTasks(vector<FlexPeriodicTask> _tasks);

  int getNumberOfTasks();
  vector<int> getWCEC(int id); //processor dependend
  int getMinWCEC(int id);
  int getMaxWCEC(int id);
  int getWCEC(int id, int node);
  int getPhase(int id);
  vector<int> getPeriod(int id);
  vector<vector<int>> getHyperperiods();
  vector<vector<vector<int>>> getHyperperiodsPerTask();
  int getNumberOfPeriodCombinations();
  int getDeadline(int id);
  vector<int> getMemCons(int id); //processor dependend
  int getCodeSize(int id);
  bool isPreemtable(int id);

  int getMaxCodeSize();
  //int getMaxHyperperiod();
  int getMinWCEC();
  int getMaxWCEC();
  //int getMinPeriod();
  //int getMaxPeriod();
  vector<vector<vector<int>>> getInstancesPerTask();
  vector<vector<int>> getMaxInstancesPerPeriod();
  vector<int> getMaxNumberOfInstances();

  int areHomogeneous(int nodeI, int nodeJ);

};
#endif

max_stars_repo_name: forsyde/DeSyDe

max_issues_repo_name: forsyde/DeSyDe

max_forks_repo_name: forsyde/DeSyDe


text:
#ifndef CONFIG_HPP_
#define CONFIG_HPP_

#define GLDSEL_FUNC_DECL inline

#include <GL/gl.h>
#include <GL/glew.h>
#include <string>
#include <utility>
#include <type_traits>
#include <algorithm>
#include <boost/hana/tuple.hpp>
#include <boost/hana/map.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/type.hpp>
#include <boost/hana/zip_with.hpp>
#include <boost/hana/ext/std/tuple.hpp>
#include <boost/hana/find.hpp>
#include <boost/hana/unpack.hpp>
#include <boost/hana/ext/std/integral_constant.hpp>
#include <boost/hana/ext/std/integer_sequence.hpp>
#include <boost/hana/optional.hpp>
#include <boost/hana/functional/overload.hpp>
#include <boost/hana/transform.hpp>
#include <boost/hana/core/make.hpp>
#include <boost/hana/for_each.hpp>
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/type.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/zip.hpp>
#include <boost/optional.hpp>
#include <boost/optional/optional_io.hpp>
#include <type_traits>
#include <boost/metaparse/string.hpp>
#include <boost/hana/ext/std/tuple.hpp>
#include <boost/hana/integral_constant.hpp>
#include <iostream>
#include <exception>
#include <glm/glm.hpp>
#include <boost/hana/functional/overload.hpp>
#include <boost/hana/if.hpp>
#include <boost/hana/for_each.hpp>
#include <boost/hana/ext/std/vector.hpp>
#include <boost/hana/ext/std/array.hpp>
#include <boost/hana/functional/id.hpp>
#include <utility>
#include <boost/metaparse/alphanum.hpp>
#include <boost/metaparse/transform.hpp>
#include <boost/metaparse/keyword.hpp>
#include <boost/metaparse/token.hpp>
#include <boost/metaparse/sequence.hpp>
#include <boost/metaparse/token.hpp>
#include <boost/metaparse/one_of.hpp>
#include <boost/metaparse/repeated1.hpp>
#include <boost/metaparse/lit_c.hpp>
#include <boost/metaparse/except.hpp>
#include <boost/metaparse/optional.hpp>
#include <boost/metaparse/repeated.hpp>
#include <boost/metaparse/define_error.hpp>
#include <boost/metaparse/int_.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/lambda.hpp>
#include <boost/mpl/at.hpp>

namespace GLDSEL
{
  using indexType = size_t;
  using vaoName = BOOST_METAPARSE_STRING("vaoBuffer");
  using drawTypeName = BOOST_METAPARSE_STRING("drawType");
}

#endif

max_stars_repo_name: nicklausEB/Becket

max_issues_repo_name: nicklausEB/Becket

max_forks_repo_name: nicklausEB/Becket


text:
#include <FishEditor/Serialization/NativeFormatImporter.hpp>
#include <FishEditor/Serialization/YAMLArchive.hpp>
#include <FishEditor/Path.hpp>
#include <FishEngine/Render/Material.hpp>

#include <boost/algorithm/string.hpp>

using namespace FishEngine;

namespace FishEditor
{
	struct MetaData
	{
		int				fileFormatVersion;
		std::string		guid;
		uint64_t		timeCreated;
		std::string		importerName;
		YAML::Node		importerInfo;
	};

	// import ".meta" file
	MetaData ImportMeta(const std::string& path)
	{
		auto node = YAML::LoadAllFromFile(path)[0];
		MetaData meta;
		meta.fileFormatVersion = node["fileFormatVersion"].as<int>();
		meta.guid = node["guid"].as<std::string>();
		meta.timeCreated = node["timeCreated"].as<uint64_t>();
		meta.importerInfo = node;
		return meta;
	}

	void NativeFormatImporter::Import()
	{
		auto path = GetFullPath();
		auto meta = ImportMeta(path + ".meta");

		{
			auto p = fs::path(path);
			auto ext = boost::to_lower_copy(p.extension().string());
			if (ext == ".mat")
			{
				m_MainAsset = Material::GetDefaultMaterial();
				m_FileIDToObject[2100000] = m_MainAsset;
				return;
			}
//			else if (ext != ".prefab")
//			{
//				abort();
//			}
		}

		YAMLInputArchive archive;

		auto fullpath = this->GetFullPath();
		auto str = ReadFileAsString(fullpath);
		auto objects = archive.LoadAllFromString(str);
		int64_t mainObjectFileID = Prefab::ClassID * 100000;
		try {
			mainObjectFileID = meta.importerInfo["NativeFormatImporter"]["mainObjectFileID"].as<int64_t>();
		} catch(const std::exception& e) {
			abort();
		}
		Object* mainObject = nullptr;
		if (mainObjectFileID == 0)		// fileFormatVersion == 2
			mainObject = objects[0];
		else
			mainObject = archive.GetObjectByFileID(mainObjectFileID);
		assert(mainObject != nullptr);
		m_MainAsset = mainObject;

		if (mainObject->GetClassID() == Prefab::ClassID)
		{
			Prefab* prefab = mainObject->As<Prefab>();
			//for (auto &&p : archive.GetFileIDToObject())
			//{
			//	prefab->AddObject(p.first, p.second);
			//}
			prefab->m_FileIDToObject = archive.GetFileIDToObject();
		}
		this->m_FileIDToObject = archive.GetFileIDToObject();
	}
}


text:
//  Copyright (c) 2021 DNV AS
//
//  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

#include <jsScript/jsInterpreter.h>
#include <jsScript/jsInterpreterInternal.h>
#include <jsScript/jsScriptable.h>
#include <jsScript/jsVTBL.h>
#include <jsScript/jsDocumentation.h>
#include <iostream>
#include <boost/algorithm/string/case_conv.hpp>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

using namespace std;

jsInterpreter::jsInterpreter(jsTypeLibrary* typeLibrary)
:  m_inAssignment(false)
{
   m_internal=new jsInterpreterInternal(typeLibrary);
}

jsInterpreter::~jsInterpreter()
{
   delete m_internal;
}

void jsInterpreter::inAssignment(bool inAssignment)
{
   m_inAssignment=inAssignment;
}

bool jsInterpreter::inAssignment() const
{
   return m_inAssignment;
}

void jsInterpreter::document(const std::string& path,const std::string& programName,const std::string& version,const std::string& versionDate)
{
   jsDocumentation(VTBLMap(),path,programName,version,versionDate);
}

jsTypeLibrary& jsInterpreter::VTBLMap()
{
   return m_internal->vtblMap();
}

jsValue* jsInterpreter::prototype(const std::string& typeName)
{
   return VTBLMap().lookup(typeName);
}

void jsInterpreter::setLineNumber(int lineNumber)
{
}

void jsInterpreter::setLastLineNumber(int lineNumber)
{
}

void jsInterpreter::addReservedWord(const std::string& word)
{
    VTBLMap().addReservedWord(word);
}

bool jsInterpreter::isReservedWord(const std::string& word) const
{
    return const_cast<jsInterpreter*>(this)->VTBLMap().isReservedWord(word);
}

void jsInterpreter::validateIdentifier(const std::string& identifier)
{
    if(isReservedWord(identifier)) throw std::runtime_error(identifier + " is reserved, and cannot be used as a named variable.");
}

max_stars_repo_name: dnv-opensource/Reflection

max_issues_repo_name: dnv-opensource/Reflection

max_forks_repo_name: dnv-opensource/Reflection


text:
// Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/describe/members.hpp>
#include <boost/describe/class.hpp>
#include <boost/core/lightweight_test.hpp>
#include <utility>

class X
{
private:

    std::pair<int, int> p_;

public:

    std::pair<int, int>& f() { return p_; }
    std::pair<int, int> const& f() const { return p_; }

    BOOST_DESCRIBE_CLASS(X, (), ((std::pair<int, int>& ()) f, (std::pair<int, int> const& () const) f), (), (p_))
};

#if !defined(BOOST_DESCRIBE_CXX14)

#include <boost/config/pragma_message.hpp>

BOOST_PRAGMA_MESSAGE("Skipping test because C++14 is not available")
int main() {}

#else

#include <boost/mp11.hpp>

int main()
{
    using namespace boost::describe;
    using namespace boost::mp11;

    {
        using L1 = describe_members<X, mod_any_access>;

        BOOST_TEST_EQ( mp_size<L1>::value, 1 );

        using D1 = mp_at_c<L1, 0>;

        BOOST_TEST_CSTR_EQ( D1::name, "p_" );
        BOOST_TEST_EQ( D1::modifiers, mod_private );

        X x;

        auto& p = x.*D1::pointer;

        using L2 = describe_members<X, mod_any_access | mod_function>;

        BOOST_TEST_EQ( mp_size<L2>::value, 2 );

        using D2 = mp_at_c<L2, 0>;
        using D3 = mp_at_c<L2, 1>;

        BOOST_TEST_EQ( &(x.*D2::pointer)(), &p );
        BOOST_TEST_CSTR_EQ( D2::name, "f" );
        BOOST_TEST_EQ( D2::modifiers, mod_public | mod_function );

        BOOST_TEST_EQ( &(x.*D3::pointer)(), &p );
        BOOST_TEST_CSTR_EQ( D3::name, "f" );
        BOOST_TEST_EQ( D3::modifiers, mod_public | mod_function );
    }

    return boost::report_errors();
}

#endif // !defined(BOOST_DESCRIBE_CXX14)


text:
#pragma once
#ifndef TEST_NTL_HPP
#define TEST_NTL_HPP
#include <NTL/GF2X.h>
#include <string>
#include <stdio.h>
#include <inttypes.h>
#include <ctype.h>

static inline void to_hexstring(std::string& str, const NTL::GF2X& poly)
{
    long d = deg(poly);
    long m = d / 64;
    char buff[200];
    str = "";
    for (long c = m; c >= 0; c--) {
        uint64_t mask = 1;
        uint64_t p = 0;
        for(int i = 0; i < 64; i++) {
            if (IsOne(coeff(poly, 64 * c + i))) {
                p |= mask;
            }
            mask <<= 1;
        }
        sprintf(buff, "%016" PRIx64, p);
        str = str + buff;
    }
}

static inline void to_binstring(std::string& str, const NTL::GF2X& poly)
{
    long d = deg(poly);
    long m = d / 64;
    str = "";
    for (int c = 0; c <= m; c++) {
        for(int i = 0; i < 64; i++) {
            if (IsOne(coeff(poly, 64 * c + i))) {
                str += '1';
            } else {
                str += '0';
            }
        }
    }
    reverse(str.begin(), str.end());
}

static inline void hexto_poly(NTL::GF2X& poly, const std::string& str)
{
    NTL::GF2X x1;
    SetCoeff(x1, 1, 1);
    unsigned long len = str.length();
    poly = 0;
    for (unsigned int i = 0; i < len; i++) {
        char c = static_cast<char>(tolower(static_cast<char>(str[i])));
        int d;
        if (isdigit(c)) {
            d = c - '0';
        } else if (isxdigit(c)) {
            d = c - 'a' + 10;
        }
        int mask = 8;
        for (int j = 0; j < 4; j++) {
            poly *= x1;
            if (d & mask) {
                SetCoeff(poly, 0, 1);
            } else {
                SetCoeff(poly, 0, 0);
            }
            mask = mask >> 1;
        }
    }
}

static inline void ntl_print(const char *s, const NTL::GF2X& x)
{
    std::string str;
    to_binstring(str, x);
    long d = deg(x);
    printf("%s %ld,%s\n", s, d, str.c_str());
    fflush(stdout);
}

#if defined(DEBUG)
#define PRT(s, x) ntl_print(s, x)
#define PUTS(s) puts(s)
#else
#define PRT(s, x)
#define PUTS(s)
#endif

#endif // TEST_NTL_HPP


text:
/* rotating_file_logger_test.cc
   Jeremy Barnes, 31 January 2011
   Copyright (c) 2011 Datacratic.  All rights reserved.

   Tests for the endpoints.
*/

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include "soa/logger/file_output.h"
#include <sys/socket.h>
#include "jml/utils/guard.h"
#include "jml/arch/exception_handler.h"
#include "jml/utils/testing/watchdog.h"
#include "jml/utils/testing/fd_exhauster.h"
#include "jml/arch/timers.h"

using namespace std;
using namespace ML;
using namespace Datacratic;

BOOST_AUTO_TEST_CASE( test_rotating_file_logger )
{
    vector<string> filesOpened;

    ML::Call_Guard guard([&] () { for (auto file: filesOpened) unlink(file.c_str());});

    RotatingFileOutput logger;

    int numLinesWritten = 0;

    logger.onPreFileOpen = [&] (string filename)
        {
            cerr << "opening file " << filename << endl;
            filesOpened.push_back(filename);
        };

    logger.onPostFileOpen = [&] (string filename)
        {
            cerr << "closing file " << filename << endl;
        };

    logger.open("tmp/file-logger-%F-%T.log.gz", "2s");

    for (unsigned i = 0;  i < 60;  ++i) {
        logger.logMessage("HELLO", "This is a message " + numLinesWritten);
        ++numLinesWritten;
        ML::sleep(0.1);
    }

    logger.close();

    BOOST_CHECK_GT(filesOpened.size(), 3);
    BOOST_CHECK_LE(filesOpened.size(), 6);

    int totalLines = 0;

    for (auto file: filesOpened) {
        filter_istream stream(file);
        while (stream) {
            string line;
            getline(stream, line);

            if (line != "")
                ++totalLines;
        }
    }

    BOOST_CHECK_EQUAL(totalLines, numLinesWritten);
}

max_stars_repo_name: sharpyfox/soa

max_issues_repo_name: sharpyfox/soa

max_forks_repo_name: sharpyfox/soa


text:
//  Copyright (c) 2001-2008 Hartmut Kaiser
//
//  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_SUPPORT_CONFIX_AUG_19_2008_1103AM)
#define BOOST_SPIRIT_SUPPORT_CONFIX_AUG_19_2008_1103AM

#include <boost/spirit/home/support/placeholders.hpp>

///////////////////////////////////////////////////////////////////////////////
namespace boost { namespace spirit { namespace tag
{
    // This is the tag returned by the confix() function
    template <typename Prefix, typename Suffix>
    struct confix_tag
    {
        Prefix prefix;
        Suffix suffix;
    };

}}}

///////////////////////////////////////////////////////////////////////////////
namespace boost { namespace spirit
{
    ///////////////////////////////////////////////////////////////////////////
    template <typename Prefix, typename Suffix = Prefix>
    struct confix_spec
      : proto::terminal<tag::confix_tag<Prefix, Suffix> >::type
    {
    private:
        typedef typename
            proto::terminal<tag::confix_tag<Prefix, Suffix> >::type
        base_type;

        base_type make_tag(Prefix const& prefix, Suffix const& suffix) const
        {
            base_type xpr = {{prefix, suffix}};
            return xpr;
        }

    public:
        confix_spec(Prefix const& prefix, Suffix const& suffix)
          : base_type(make_tag(prefix, suffix))
        {}
    };

    namespace detail
    {
        struct confix_extractor
        {
            template <typename Prefix, typename Suffix>
            static Prefix const& prefix(tag::confix_tag<Prefix, Suffix> const& c)
            { return c.prefix; }

            template <typename Prefix, typename Suffix>
            static Suffix const& suffix(tag::confix_tag<Prefix, Suffix> const& c)
            { return c.suffix; }
        };
    }

    ///////////////////////////////////////////////////////////////////////////
    // construct a confix component
    ///////////////////////////////////////////////////////////////////////////
    inline confix_spec<char const*>
    confix(char const* prefix, char const* suffix)
    {
        return confix_spec<char const*>(prefix, suffix);
    }

    inline confix_spec<wchar_t const*>
    confix(wchar_t const* prefix, wchar_t const* suffix)
    {
        return confix_spec<wchar_t const*>(prefix, suffix);
    }

    template <typename Prefix, typename Suffix>
    inline confix_spec<Prefix, Suffix>
    confix(Prefix const& prefix, Suffix const& suffix)
    {
        return confix_spec<Prefix, Suffix>(prefix, suffix);
    }

}}

#endif


text:
#include "session.hpp"

#include <string>

#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>

Session::Session(const std::string& p_ip, const std::string& p_port) :
    m_ip(p_ip),
    m_port(p_port),
    m_io_service(),
    m_socket(m_io_service),
    m_deadline(m_io_service)
{
    m_deadline.expires_at(boost::posix_time::pos_infin);
    check_deadline();
}

Session::~Session()
{
    close();
}

bool Session::connect()
{
    try
    {
        boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(m_ip), atoi(m_port.c_str()));

        // set a 2 second deadline for the connection
        boost::system::error_code ec = boost::asio::error::would_block;
        m_deadline.expires_from_now(boost::posix_time::seconds(2));
        m_socket.async_connect(endpoint, boost::lambda::var(ec) = boost::lambda::_1);

        do
        {
            m_io_service.run_one();
        }
        while (ec == boost::asio::error::would_block);

        if (ec || !m_socket.is_open())
        {
           return false;
        }
    }
    catch(const std::exception& e)
    {
        return false;
    }
    return true;
}

void Session::close()
{
    try
    {
        m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
        m_socket.close();
    }
    catch (...)
    {
    }
}

void Session::check_deadline()
{
    if (m_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now())
    {
      boost::system::error_code ignored_ec;
      m_socket.close(ignored_ec);
      m_deadline.expires_at(boost::posix_time::pos_infin);
    }

    // Put the actor back to sleep.
    m_deadline.async_wait(boost::lambda::bind(&Session::check_deadline, this));
}

max_stars_repo_name: Bram-Wel/routeros

max_issues_repo_name: Bram-Wel/routeros

max_forks_repo_name: Bram-Wel/routeros


text:
// Copyright 2015-2020 Tier IV, Inc. 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.

#include <algorithm>
#include <behavior_tree_plugin/vehicle/behavior_tree.hpp>
#include <behavior_tree_plugin/vehicle/lane_change_action.hpp>
#include <boost/algorithm/clamp.hpp>
#include <memory>
#include <scenario_simulator_exception/exception.hpp>
#include <string>
#include <traffic_simulator/math/catmull_rom_spline.hpp>
#include <traffic_simulator/math/transform.hpp>
#include <vector>

namespace entity_behavior
{
namespace vehicle
{
LaneChangeAction::LaneChangeAction(const std::string & name, const BT::NodeConfiguration & config)
: entity_behavior::VehicleActionNode(name, config), current_s_(0)
{
}

const boost::optional<traffic_simulator_msgs::msg::Obstacle> LaneChangeAction::calculateObstacle(
  const traffic_simulator_msgs::msg::WaypointsArray &)
{
  return boost::none;
}

const traffic_simulator_msgs::msg::WaypointsArray LaneChangeAction::calculateWaypoints()
{
  if (!curve_) {
    THROW_SIMULATION_ERROR("curve is null");
  }
  if (!lane_change_parameters_) {
    THROW_SIMULATION_ERROR("lane change parameter is null");
  }
  if (entity_status.action_status.twist.linear.x >= 0) {
    traffic_simulator_msgs::msg::WaypointsArray waypoints;
    double horizon =
      boost::algorithm::clamp(entity_status.action_status.twist.linear.x * 5, 20, 50);
    auto following_lanelets =
      hdmap_utils->getFollowingLanelets(lane_change_parameters_->target.lanelet_id, 0);
    double l = curve_->getLength();
    double rest_s = current_s_ + horizon - l;
    if (rest_s < 0) {
      const auto curve_waypoints =
        curve_->getTrajectory(current_s_, current_s_ + horizon, 1.0, true);
      waypoints.waypoints = curve_waypoints;
    } else {
      std::vector<geometry_msgs::msg::Point> center_points =
        hdmap_utils->getCenterPoints(following_lanelets);
      // DIFFERENT SPLINE - recalculation needed
      traffic_simulator::math::CatmullRomSpline spline(center_points);
      const auto straight_waypoints = spline.getTrajectory(target_s_, target_s_ + rest_s, 1.0);
      waypoints.waypoints = straight_waypoints;
      const auto curve_waypoints = curve_->getTrajectory(current_s_, l, 1.0, true);
      std::copy(
        straight_waypoints.begin(), straight_waypoints.end(),
        std::back_inserter(waypoints.waypoints));
    }
    return waypoints;
  } else {
    return traffic_simulator_msgs::msg::WaypointsArray();
  }
}

void LaneChangeAction::getBlackBoardValues()
{
  VehicleActionNode::getBlackBoardValues();
  traffic_simulator::lane_change::Parameter lane_change_parameters;
  if (!getInput<traffic_simulator::lane_change::Parameter>(
        "lane_change_parameters", lane_change_parameters)) {
    lane_change_parameters_ = boost::none;
  } else {
    lane_change_parameters_ = lane_change_parameters;
  }
}

BT::NodeStatus LaneChangeAction::tick()
{
  getBlackBoardValues();
  if (request != "lane_change") {
    curve_ = boost::none;
    current_s_ = 0;
    return BT::NodeStatus::FAILURE;
  }
  if (!lane_change_parameters_) {
    curve_ = boost::none;
    current_s_ = 0;
    return BT::NodeStatus::FAILURE;
  }
  if (!curve_) {
    if (request == "lane_change") {
      if (!hdmap_utils->canChangeLane(
            entity_status.lanelet_pose.lanelet_id, lane_change_parameters_->target.lanelet_id)) {
        return BT::NodeStatus::FAILURE;
      }
      boost::optional<std::pair<traffic_simulator::math::HermiteCurve, double>> traj_with_goal;
      traffic_simulator_msgs::msg::LaneletPose along_pose, goal_pose;
      switch (lane_change_parameters_->constraint.type) {
        case traffic_simulator::lane_change::Constraint::Type::NONE:
          traj_with_goal = hdmap_utils->getLaneChangeTrajectory(
            hdmap_utils->toMapPose(entity_status.lanelet_pose).pose, lane_change_parameters_.get(),
            10.0, 20.0, 1.0);
          along_pose = hdmap_utils->getAlongLaneletPose(
            entity_status.lanelet_pose,
            traffic_simulator::lane_change::Parameter::default_lanechange_distance);
          break;
        case traffic_simulator::lane_change::Constraint::Type::LATERAL_VELOCITY:
          traj_with_goal = hdmap_utils->getLaneChangeTrajectory(
            entity_status.lanelet_pose, lane_change_parameters_.get());
          along_pose = hdmap_utils->getAlongLaneletPose(
            entity_status.lanelet_pose,
            traffic_simulator::lane_change::Parameter::default_lanechange_distance);
          break;
        case traffic_simulator::lane_change::Constraint::Type::LONGITUDINAL_DISTANCE:
          traj_with_goal = hdmap_utils->getLaneChangeTrajectory(
            entity_status.lanelet_pose, lane_change_parameters_.get());
          along_pose = hdmap_utils->getAlongLaneletPose(
            entity_status.lanelet_pose, lane_change_parameters_->constraint.value);
          break;
        case traffic_simulator::lane_change::Constraint::Type::TIME:
          traj_with_goal = hdmap_utils->getLaneChangeTrajectory(
            entity_status.lanelet_pose, lane_change_parameters_.get());
          along_pose = hdmap_utils->getAlongLaneletPose(
            entity_status.lanelet_pose, lane_change_parameters_->constraint.value);
          break;
      }
      if (traj_with_goal) {
        curve_ = traj_with_goal->first;
        target_s_ = traj_with_goal->second;
        goal_pose.lanelet_id = lane_change_parameters_->target.lanelet_id;
        goal_pose.s = traj_with_goal->second;
        double offset = std::fabs(
          traffic_simulator::math::getRelativePose(
            hdmap_utils->toMapPose(along_pose).pose, hdmap_utils->toMapPose(goal_pose).pose)
            .position.y);
        switch (lane_change_parameters_->constraint.type) {
          case traffic_simulator::lane_change::Constraint::Type::NONE:
            lane_change_velocity_ = entity_status.action_status.twist.linear.x;
            break;
          case traffic_simulator::lane_change::Constraint::Type::LATERAL_VELOCITY:
            lane_change_velocity_ =
              curve_->getLength() / (offset / lane_change_parameters_->constraint.value);
            break;
          case traffic_simulator::lane_change::Constraint::Type::LONGITUDINAL_DISTANCE:
            lane_change_velocity_ = entity_status.action_status.twist.linear.x;
            break;
          case traffic_simulator::lane_change::Constraint::Type::TIME:
            lane_change_velocity_ = curve_->getLength() / lane_change_parameters_->constraint.value;
            break;
        }
      } else {
        return BT::NodeStatus::FAILURE;
      }
    }
  }
  if (curve_) {
    double target_accel = 0;
    switch (lane_change_parameters_->constraint.policy) {
      /**
       * @brief Force changing speed in order to fullfill constraint.
       */
      case traffic_simulator::lane_change::Constraint::Policy::FORCE:
        entity_status.action_status.twist = geometry_msgs::msg::Twist();
        entity_status.action_status.accel = geometry_msgs::msg::Accel();
        entity_status.action_status.twist.linear.x = lane_change_velocity_;
        current_s_ = current_s_ + entity_status.action_status.twist.linear.x * step_time;
        break;
      /**
       * @brief Changing linear speed and try to fullfill constraint.
       */
      case traffic_simulator::lane_change::Constraint::Policy::BEST_EFFORT:
        target_accel =
          (lane_change_velocity_ - entity_status.action_status.twist.linear.x) / step_time;
        if (entity_status.action_status.twist.linear.x > target_speed) {
          target_accel = boost::algorithm::clamp(target_accel, driver_model.deceleration * -1, 0);
        } else {
          target_accel = boost::algorithm::clamp(target_accel, 0, driver_model.acceleration);
        }
        geometry_msgs::msg::Accel accel_new;
        accel_new.linear.x = target_accel;
        geometry_msgs::msg::Twist twist_new;
        twist_new.linear.x = boost::algorithm::clamp(
          entity_status.action_status.twist.linear.x + accel_new.linear.x * step_time, -10,
          vehicle_parameters.performance.max_speed);
        twist_new.linear.y = 0.0;
        twist_new.linear.z = 0.0;
        twist_new.angular.x = 0.0;
        twist_new.angular.y = 0.0;
        twist_new.angular.z = 0.0;
        entity_status.action_status.twist = twist_new;
        entity_status.action_status.accel = accel_new;
        current_s_ = current_s_ + entity_status.action_status.twist.linear.x * step_time;
        break;
    }
    if (current_s_ < curve_->getLength()) {
      geometry_msgs::msg::Pose pose = curve_->getPose(current_s_, true);
      traffic_simulator_msgs::msg::EntityStatus entity_status_updated;
      entity_status_updated.pose = pose;
      auto lanelet_pose = hdmap_utils->toLaneletPose(pose, entity_status.bounding_box, false);
      if (lanelet_pose) {
        entity_status_updated.lanelet_pose = lanelet_pose.get();
      } else {
        entity_status_updated.lanelet_pose_valid = false;
      }
      entity_status_updated.action_status = entity_status.action_status;
      setOutput("updated_status", entity_status_updated);
      const auto waypoints = calculateWaypoints();
      if (waypoints.waypoints.empty()) {
        return BT::NodeStatus::FAILURE;
      }
      const auto obstacle = calculateObstacle(waypoints);
      setOutput("waypoints", waypoints);
      setOutput("obstacle", obstacle);
      return BT::NodeStatus::RUNNING;
    } else {
      const auto waypoints = calculateWaypoints();
      if (waypoints.waypoints.empty()) {
        return BT::NodeStatus::FAILURE;
      }
      const auto obstacle = calculateObstacle(waypoints);
      setOutput("waypoints", waypoints);
      setOutput("obstacle", obstacle);
      double s = (current_s_ - curve_->getLength()) + target_s_;
      curve_ = boost::none;
      current_s_ = 0;
      lane_change_velocity_ = 0;
      traffic_simulator_msgs::msg::EntityStatus entity_status_updated;
      traffic_simulator_msgs::msg::LaneletPose lanelet_pose;
      lanelet_pose.lanelet_id = lane_change_parameters_->target.lanelet_id;
      lanelet_pose.s = s;
      lanelet_pose.offset = 0;
      entity_status_updated.pose = hdmap_utils->toMapPose(lanelet_pose).pose;
      entity_status_updated.lanelet_pose = lanelet_pose;
      entity_status_updated.action_status = entity_status.action_status;
      setOutput("updated_status", entity_status_updated);
      return BT::NodeStatus::SUCCESS;
    }
  }
  return BT::NodeStatus::FAILURE;
}
}  // namespace vehicle
}  // namespace entity_behavior