text:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include "StereoMapInit.h"

#include "Utils/cv.h"
#include "Data/Data.h"
#include "Map/InitializationData.h"
#include "Tracking/Keyframebuilder.h"
#include "Tracking/Reprojection.h"
#include "Tracking/FeatureMatcher.h"
#include "Tracking/MapInitialization.h"
#include "Device/CameraCalibration.h"
#include "Bow/BaseBow.h"
#include "Image/ImagePreprocessor.h"
#include "MageUtil.h"
#include "Utils/Logging.h"
#include "Utils/MageConversions.h"
#include <boost/format.hpp>
#include <memory>

#include <opencv2/core/core.hpp>

namespace mage
{
    namespace
    {
        int CreateFilteredDescriptorMask(const int octave, const cv::Rect& crop, const std::shared_ptr<mage::AnalyzedImage> &frame, std::vector<bool>& useDescriptor)
        {
            int trueCount = 0;
            for (unsigned int i = 0; i < frame->GetDescriptorsCount(); i++)
            {
                const cv::KeyPoint& kpt = frame->GetKeyPoint(i);
                if ((octave < 0 || kpt.octave == octave) && IsWithinArea(kpt.pt, crop))
                {
                    useDescriptor[i] = true;
                    trueCount += 1;
                }
                else
                {
                    useDescriptor[i] = false;
                }
            }
            return trueCount;
        }

        void InitializeWithFrames(
            gsl::span<const cv::DMatch> matches,
            const std::shared_ptr<const AnalyzedImage>& referenceFrame, //query
            const std::shared_ptr<const AnalyzedImage>& currentFrame,   //train
            InitializationData& initializationData,
            const Pose& explicitPose,
            float pixelMaxEpipolarDistance,
            float minAcceptanceDistanceRatio,
            thread_memory memory)
        {
            SCOPE_TIMER(MapInitialization::InitializeWithFrames);

            std::vector<std::pair<cv::DMatch, cv::Point3f>> initial3DPoints;

            std::vector<cv::Point2f> frame1_points;
            std::vector<cv::Point2f> frame2_points;
            MapInitialization::CollectMatchPoints(referenceFrame, currentFrame, matches, frame1_points, frame2_points);

            Pose referencePose{};
            {
                MapInitialization::TriangulatePoints(referenceFrame, currentFrame, referencePose, explicitPose, matches, frame1_points, frame2_points, pixelMaxEpipolarDistance, minAcceptanceDistanceRatio, initial3DPoints);
            }

            // create the mappoint proxies to link the keyframe builders to
            initializationData.Clear();

            std::transform(initial3DPoints.cbegin(), initial3DPoints.cend(), std::back_inserter(initializationData.MapPoints),
                [](const auto& point3D)
            {
                // MapInitialization map points are special and get treated as though they have been adjusted once already (which is technically true)
                return MapPointTrackingProxy::CreateNew(point3D.second, 1);
            });

            const std::shared_ptr<KeyframeBuilder> refKeyframeBuilder = initializationData.AddKeyframeBuilder(referenceFrame, referencePose);
            const std::shared_ptr<KeyframeBuilder> currentKeyframeBuilder = initializationData.AddKeyframeBuilder(currentFrame, explicitPose);

            for (size_t iterator = 0; iterator < initial3DPoints.size(); iterator++)
            {
                refKeyframeBuilder->AddAssociation(initializationData.MapPoints[iterator], initial3DPoints[iterator].first.queryIdx);
                currentKeyframeBuilder->AddAssociation(initializationData.MapPoints[iterator], initial3DPoints[iterator].first.trainIdx);
            }
        }

    }

    StereoMapInit::StereoMapInit(const StereoMapInitializationSettings& settings, mira::determinator& determinator)
        : m_settings{ settings }, m_determinator{ determinator }
    {}

    //assumes undistorted input images with null distortion calibrations
    boost::optional<InitializationData> StereoMapInit::Initialize(const std::shared_ptr<AnalyzedImage>& frame0, const std::shared_ptr<AnalyzedImage>& frame1, const cv::Matx44f& frame0ToFrame1, thread_memory memory)
    {
        // get crop region
        cv::Rect frame0CropRect{ ToCVRect(CalculateOverlapCropSourceInTarget(ToMageMat(frame0ToFrame1), frame0->GetUndistortedCalibration().CreateCameraModel(), frame1->GetUndistortedCalibration().CreateCameraModel(), m_settings.MaxDepthMeters)) };
        //TODO: match iteratively across each octave

        // filter frame 0 descriptors to within the crop region for matching
        std::vector<bool> flagsFrame0(frame0->GetDescriptorsCount(), false);
        int numFilteredDescriptorsFrame0 = CreateFilteredDescriptorMask(-1, frame0CropRect, frame0, flagsFrame0);

        std::vector<bool> flagsFrame1(frame1->GetDescriptorsCount(), true);
        int numFilteredDescriptorsFrame1 = gsl::narrow_cast<int>(frame1->GetDescriptorsCount());

        // do feature matching
        std::vector<cv::DMatch> matches;
        unsigned int numMatches = Match(frame0, frame1, flagsFrame0, flagsFrame1,
            numFilteredDescriptorsFrame0, numFilteredDescriptorsFrame1, m_settings.OrbMatcherSettings.MaxHammingDistance,
            m_settings.OrbMatcherSettings.MinHammingDifference, matches);

        const unsigned int minFeatureMatches = m_settings.MinFeatureMatches;

//#define DEBUG_STEREO_MATCHING
#ifdef DEBUG_STEREO_MATCHING
            cv::Mat debugMatches;

            const auto& imgAKeypoints = frame0->GetKeyPoints();
            const auto& imgBKeypoints = frame1->GetKeyPoints();
            cv::drawMatches(frame0->GetDebugImage(), std::vector<cv::KeyPoint>(imgAKeypoints.begin(), imgAKeypoints.end()),
                frame1->GetDebugImage(), std::vector<cv::KeyPoint>(imgBKeypoints.begin(), imgBKeypoints.end()),
                matches, debugMatches);

            static int j = 0;
            j++;
#endif

        LogMessage((boost::wformat(L"StereoInit: found %1% matches out of required %2%") % numMatches % minFeatureMatches).str());

        //normalized the pose
        cv::Matx44f normalizedFrame0ToFrame1(frame0ToFrame1);
        cv::Vec3f worldSpaceDelta = Translation(normalizedFrame0ToFrame1);
        float lenDelta = (float)cv::norm(worldSpaceDelta, cv::NORM_L2);
        const float minPoseDelta = 0.00001f;
        if (lenDelta < minPoseDelta)
        {
            LogMessage(L"StereoInit: [FAILURE] zero displacement between frames");
            return boost::none;
        }

        float oneOverMeters = 1 / lenDelta;
        normalizedFrame0ToFrame1(0, 3) *= oneOverMeters;
        normalizedFrame0ToFrame1(1, 3) *= oneOverMeters;
        normalizedFrame0ToFrame1(2, 3) *= oneOverMeters;

        // triangulate points
        if (numMatches < minFeatureMatches)
        {
            LogMessage(L"StereoInit: [FAILURE] not enough feature matches");
            return boost::none;
        }

        Pose normalizedframe1Pose = { normalizedFrame0ToFrame1.inv() }; //initializer takes a world matrix which is the inverse of wide to narrow
        InitializationData initializationData;
        InitializeWithFrames(matches, frame0, frame1, initializationData, normalizedframe1Pose, m_settings.MaxEpipolarError, m_settings.MinAcceptedDistanceRatio, memory);

        if (initializationData.MapPoints.size() < m_settings.MinInitMapPoints)
        {
            LogMessage((boost::wformat(L"StereoInit: [FAILURE] not enough map points with: %1%") % initializationData.MapPoints.size()).str());

            return boost::none;
        }

        initializationData.Frames.back()->AddExtrinsicTether(initializationData.Frames.front()->GetId(), initializationData.Frames.front()->GetPose(), m_settings.InitializationTetherStrength);

        // perform an initial bundle adjust of the map before creating new map points
        MapInitialization::BundlerSettings bundlerSettings{
            m_settings.BundleAdjustSettings.HuberWidth,
            m_settings.BundleAdjustSettings.MaxOutlierError,
            m_settings.BundleAdjustSettings.MaxOutlierErrorScaleFactor,
            m_settings.BundleAdjustSettings.MinMeanSquareError,
            false,
            m_settings.BundleAdjustSettings.NumStepsPerRun,
            m_settings.BundleAdjustSettings.NumSteps,
            m_settings.BundleAdjustSettings.MinSteps };

        MapInitialization::BundleAdjustInitializationData(initializationData, m_determinator, true, bundlerSettings, memory);

        if (!MapInitialization::ValidateInitializationData(initializationData,
            m_settings.MaxPoseContributionZ,
            m_settings.AmountBACanChangePose,
            m_settings.MinInitMapPoints)
            )
        {
            LogMessage((boost::wformat(L"StereoInit: [FAILURE] init failed validation")).str());
            return boost::none;
        }

        return initializationData;
    }
}

ext: cpp

max_stars_repo_name: syntheticmagus/mageslam

max_issues_repo_name: syntheticmagus/mageslam

max_forks_repo_name: syntheticmagus/mageslam


text:
/***
	MIT License

	Copyright (c) 2018 NUMERARE

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

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

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

	Parts of this file are originally Copyright (c) 2012-2017 The CryptoNote developers, The Bytecoin developers
***/

#include <fstream>

#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>

#include "DaemonCommandsHandler.h"

#include "Common/ScopeExit.h"
#include "Common/SignalHandler.h"
#include "Common/StdOutputStream.h"
#include "Common/StdInputStream.h"
#include "Common/PathTools.h"
#include "Common/Util.h"
#include "crypto/hash.h"
#include "CryptoNoteCore/CryptoNoteTools.h"
#include "CryptoNoteCore/Core.h"
#include "CryptoNoteCore/Currency.h"
#include "CryptoNoteCore/DatabaseBlockchainCache.h"
#include "CryptoNoteCore/DatabaseBlockchainCacheFactory.h"
#include "CryptoNoteCore/MainChainStorage.h"
#include "CryptoNoteCore/MinerConfig.h"
#include "CryptoNoteCore/RocksDBWrapper.h"
#include "CryptoNoteProtocol/CryptoNoteProtocolHandler.h"
#include "P2p/NetNode.h"
#include "P2p/NetNodeConfig.h"
#include "Rpc/RpcServer.h"
#include "Rpc/RpcServerConfig.h"
#include "Serialization/BinaryInputStreamSerializer.h"
#include "Serialization/BinaryOutputStreamSerializer.h"
#include "version.h"

#include <Logging/LoggerManager.h>

#if defined(WIN32)
#include <crtdbg.h>
#endif

using Common::JsonValue;
using namespace CryptoNote;
using namespace Logging;

namespace po = boost::program_options;

namespace
{
  const command_line::arg_descriptor<std::string> arg_config_file = {"config-file", "Specify configuration file", ""};
  const command_line::arg_descriptor<bool>        arg_os_version  = {"os-version", ""};
  const command_line::arg_descriptor<std::string> arg_log_file    = {"log-file", "", ""};
  const command_line::arg_descriptor<int>         arg_log_level   = {"log-level", "", 2}; // info level
  const command_line::arg_descriptor<bool>        arg_console     = {"no-console", "Disable daemon console commands"};
  const command_line::arg_descriptor<std::string> arg_set_fee_address = { "fee-address", "Sets fee address for light wallets to the daemon's RPC responses.", "" };
  const command_line::arg_descriptor<bool>        arg_print_genesis_tx = { "print-genesis-tx", "Prints genesis' block tx hex to insert it to config and exits" };
  const command_line::arg_descriptor<std::vector<std::string>> arg_genesis_block_reward_address = { "genesis-block-reward-address", "" };
  const command_line::arg_descriptor<bool> arg_blockexplorer_on = {"enable-blockexplorer", "Enable blockchain explorer RPC", false};
  const command_line::arg_descriptor<std::vector<std::string>>        arg_enable_cors = { "enable-cors", "Adds header 'Access-Control-Allow-Origin' to the daemon's RPC responses. Uses the value as domain. Use * for all" };
  const command_line::arg_descriptor<std::string> arg_GENESIS_COINBASE_TX_HEX  = {"GENESIS_COINBASE_TX_HEX", "Genesis transaction hex", CryptoNote::parameters::GENESIS_COINBASE_TX_HEX};
  const command_line::arg_descriptor<uint64_t>    arg_CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX  = {"CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX", "uint64_t", CryptoNote::parameters::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX};
  const command_line::arg_descriptor<uint64_t>    arg_MONEY_SUPPLY  = {"MONEY_SUPPLY", "uint64_t", CryptoNote::parameters::MONEY_SUPPLY};
  const command_line::arg_descriptor<unsigned>    arg_EMISSION_SPEED_FACTOR  = {"EMISSION_SPEED_FACTOR", "unsigned", CryptoNote::parameters::EMISSION_SPEED_FACTOR};
  const command_line::arg_descriptor<size_t>      arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE  = {"CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE", "size_t", CryptoNote::parameters::CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE};
  const command_line::arg_descriptor<size_t>      arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1  = {"CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1", "size_t", CryptoNote::parameters::CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1};
  const command_line::arg_descriptor<size_t>      arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2  = {"CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2", "size_t", CryptoNote::parameters::CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2};
  const command_line::arg_descriptor<uint64_t>    arg_CRYPTONOTE_DISPLAY_DECIMAL_POINT  = {"CRYPTONOTE_DISPLAY_DECIMAL_POINT", "uint64_t", CryptoNote::parameters::CRYPTONOTE_DISPLAY_DECIMAL_POINT};
  const command_line::arg_descriptor<uint64_t>    arg_MINIMUM_FEE  = {"MINIMUM_FEE", "uint64_t", CryptoNote::parameters::MINIMUM_FEE};
  const command_line::arg_descriptor<uint64_t>    arg_DEFAULT_DUST_THRESHOLD  = {"DEFAULT_DUST_THRESHOLD", "uint64_t", CryptoNote::parameters::DEFAULT_DUST_THRESHOLD};
  const command_line::arg_descriptor<uint64_t>    arg_DIFFICULTY_TARGET  = {"DIFFICULTY_TARGET", "uint64_t", CryptoNote::parameters::DIFFICULTY_TARGET};
  const command_line::arg_descriptor<uint32_t>    arg_CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW  = {"CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW", "uint32_t", CryptoNote::parameters::CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW};
  const command_line::arg_descriptor<size_t>      arg_MAX_BLOCK_SIZE_INITIAL  = {"MAX_BLOCK_SIZE_INITIAL", "size_t", CryptoNote::parameters::MAX_BLOCK_SIZE_INITIAL};
  const command_line::arg_descriptor<uint64_t>    arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY  = {"EXPECTED_NUMBER_OF_BLOCKS_PER_DAY", "uint64_t"};
  const command_line::arg_descriptor<uint32_t>    arg_UPGRADE_HEIGHT_V2  = {"UPGRADE_HEIGHT_V2", "uint32_t", 0};
  const command_line::arg_descriptor<uint32_t>    arg_UPGRADE_HEIGHT_V3  = {"UPGRADE_HEIGHT_V3", "uint32_t", 0};
  const command_line::arg_descriptor<uint32_t>    arg_KEY_IMAGE_CHECKING_BLOCK_INDEX  = {"KEY_IMAGE_CHECKING_BLOCK_INDEX", "uint32_t", 0};
  const command_line::arg_descriptor<size_t>      arg_DIFFICULTY_WINDOW_V1  = {"DIFFICULTY_WINDOW_V1", "size_t", 0};
  const command_line::arg_descriptor<size_t>      arg_DIFFICULTY_WINDOW_V2  = {"DIFFICULTY_WINDOW_V2", "size_t", 0};
  const command_line::arg_descriptor<size_t>      arg_DIFFICULTY_CUT_V1  = {"DIFFICULTY_CUT_V1", "size_t", CryptoNote::parameters::DIFFICULTY_CUT};
  const command_line::arg_descriptor<size_t>      arg_DIFFICULTY_CUT_V2  = {"DIFFICULTY_CUT_V2", "size_t", CryptoNote::parameters::DIFFICULTY_CUT};
  const command_line::arg_descriptor<size_t>      arg_DIFFICULTY_LAG_V1  = {"DIFFICULTY_LAG_V1", "size_t", CryptoNote::parameters::DIFFICULTY_LAG};
  const command_line::arg_descriptor<size_t>      arg_DIFFICULTY_LAG_V2  = {"DIFFICULTY_LAG_V2", "size_t", CryptoNote::parameters::DIFFICULTY_LAG};
  const command_line::arg_descriptor<size_t>      arg_DIFFICULTY_WINDOW  = {"DIFFICULTY_WINDOW", "size_t", 0};
  const command_line::arg_descriptor<size_t>      arg_DIFFICULTY_CUT  = {"DIFFICULTY_CUT", "size_t", CryptoNote::parameters::DIFFICULTY_CUT};
  const command_line::arg_descriptor<size_t>      arg_DIFFICULTY_LAG  = {"DIFFICULTY_LAG", "size_t", CryptoNote::parameters::DIFFICULTY_LAG};
  const command_line::arg_descriptor<std::string> arg_CRYPTONOTE_NAME  = {"CRYPTONOTE_NAME", "Cryptonote name. Used for storage directory", ""};
  const command_line::arg_descriptor< std::vector<std::string> > arg_CHECKPOINT  = {"CHECKPOINT", "Checkpoints. Format: HEIGHT:HASH"};
  const command_line::arg_descriptor<uint32_t>    arg_BUGGED_ZAWY_DIFFICULTY_BLOCK_INDEX  = {"BUGGED_ZAWY_DIFFICULTY_BLOCK_INDEX", "uint32_t", 0};
  const command_line::arg_descriptor<uint32_t>    arg_ZAWY_LWMA_DIFFICULTY_BLOCK_INDEX = {"ZAWY_LWMA_DIFFICULTY_BLOCK_INDEX", "uint32_t", 0};
  const command_line::arg_descriptor<uint32_t>    arg_ZAWY_LWMA_DIFFICULTY_LAST_BLOCK  = {"ZAWY_LWMA_DIFFICULTY_LAST_BLOCK", "uint32_t", 0};
  const command_line::arg_descriptor<size_t>    arg_ZAWY_LWMA_DIFFICULTY_N  = {"ZAWY_LWMA_DIFFICULTY_N", "size_t", 0};
  const command_line::arg_descriptor<uint32_t>    arg_ZAWY_DIFFICULTY_BLOCK_INDEX  = {"ZAWY_DIFFICULTY_BLOCK_INDEX", "uint32_t", 0};
  const command_line::arg_descriptor<uint32_t>    arg_ZAWY_DIFFICULTY_LAST_BLOCK  = {"ZAWY_DIFFICULTY_LAST_BLOCK", "uint32_t", 0};
  const command_line::arg_descriptor<uint64_t>    arg_GENESIS_BLOCK_REWARD  = {"GENESIS_BLOCK_REWARD", "uint64_t", 0};
  const command_line::arg_descriptor<size_t>    arg_CRYPTONOTE_COIN_VERSION  = {"CRYPTONOTE_COIN_VERSION", "size_t", 0};
  const command_line::arg_descriptor<uint64_t>    arg_TAIL_EMISSION_REWARD  = {"TAIL_EMISSION_REWARD", "uint64_t", 0};
  const command_line::arg_descriptor<uint32_t>    arg_KILL_HEIGHT  = {"KILL_HEIGHT", "uint32_t", 0};
  const command_line::arg_descriptor<uint32_t>    arg_MANDATORY_TRANSACTION  = {"MANDATORY_TRANSACTION", "uint32_t", CryptoNote::parameters::MANDATORY_TRANSACTION};
  const command_line::arg_descriptor<uint32_t>    arg_MIXIN_START_HEIGHT  = {"MIXIN_START_HEIGHT", "uint32_t", 0};
  const command_line::arg_descriptor<uint16_t>    arg_MIN_MIXIN  = {"MIN_MIXIN", "uint16_t", CryptoNote::parameters::MIN_MIXIN};
  const command_line::arg_descriptor<uint8_t>    arg_MANDATORY_MIXIN_BLOCK_VERSION  = {"MANDATORY_MIXIN_BLOCK_VERSION", "uint8_t", CryptoNote::parameters::MANDATORY_MIXIN_BLOCK_VERSION};
  const command_line::arg_descriptor<bool>        arg_testnet_on  = {"testnet", "Used to deploy test nets. Checkpoints and hardcoded seeds are ignored, "
    "network id is changed. Use it with --data-dir flag. The wallet must be launched with --testnet flag.", false};
}

bool command_line_preprocessor(const boost::program_options::variables_map& vm, LoggerRef& logger);
void print_genesis_tx_hex(const po::variables_map& vm, LoggerManager& logManager) {
  std::vector<CryptoNote::AccountPublicAddress> targets;
  auto genesis_block_reward_addresses = command_line::get_arg(vm, arg_genesis_block_reward_address);
  CryptoNote::CurrencyBuilder currencyBuilder(logManager);
    currencyBuilder.cryptonoteName(command_line::get_arg(vm, arg_CRYPTONOTE_NAME));
  currencyBuilder.minMixin(command_line::get_arg(vm, arg_MIN_MIXIN));
//uint8_t recognized as char
  if (command_line::get_arg(vm, arg_MANDATORY_MIXIN_BLOCK_VERSION) == 0) {
    currencyBuilder.mandatoryMixinBlockVersion(command_line::get_arg(vm, arg_MANDATORY_MIXIN_BLOCK_VERSION));
  } else {
    currencyBuilder.mandatoryMixinBlockVersion(command_line::get_arg(vm, arg_MANDATORY_MIXIN_BLOCK_VERSION) - '0');
  }
  currencyBuilder.mandatoryTransaction(command_line::get_arg(vm, arg_MANDATORY_TRANSACTION));
    currencyBuilder.genesisCoinbaseTxHex(command_line::get_arg(vm, arg_GENESIS_COINBASE_TX_HEX));
    currencyBuilder.publicAddressBase58Prefix(command_line::get_arg(vm, arg_CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX));
    currencyBuilder.moneySupply(command_line::get_arg(vm, arg_MONEY_SUPPLY));
    currencyBuilder.emissionSpeedFactor(command_line::get_arg(vm, arg_EMISSION_SPEED_FACTOR));
    currencyBuilder.blockGrantedFullRewardZone(command_line::get_arg(vm, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE));
    currencyBuilder.blockGrantedFullRewardZoneV1(command_line::get_arg(vm, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1));
    currencyBuilder.blockGrantedFullRewardZoneV2(command_line::get_arg(vm, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2));
    currencyBuilder.numberOfDecimalPlaces(command_line::get_arg(vm, arg_CRYPTONOTE_DISPLAY_DECIMAL_POINT));
    currencyBuilder.mininumFee(command_line::get_arg(vm, arg_MINIMUM_FEE));
    currencyBuilder.defaultDustThreshold(command_line::get_arg(vm, arg_DEFAULT_DUST_THRESHOLD));
    currencyBuilder.difficultyTarget(command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    currencyBuilder.minedMoneyUnlockWindow(command_line::get_arg(vm, arg_CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW));
    currencyBuilder.maxBlockSizeInitial(command_line::get_arg(vm, arg_MAX_BLOCK_SIZE_INITIAL));
    if (command_line::has_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY) && command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY) != 0)
    {
      currencyBuilder.expectedNumberOfBlocksPerDay(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
      currencyBuilder.difficultyWindow(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
    currencyBuilder.difficultyWindowV1(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
    currencyBuilder.difficultyWindowV2(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
      currencyBuilder.upgradeVotingWindow(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
      currencyBuilder.upgradeWindow(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
    } else {
      currencyBuilder.expectedNumberOfBlocksPerDay(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
      currencyBuilder.difficultyWindow(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    currencyBuilder.difficultyWindowV1(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    currencyBuilder.difficultyWindowV2(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    }
    currencyBuilder.maxBlockSizeGrowthSpeedDenominator(365 * 24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    currencyBuilder.lockedTxAllowedDeltaSeconds(command_line::get_arg(vm, arg_DIFFICULTY_TARGET) * CryptoNote::parameters::CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS);
    if (command_line::has_arg(vm, arg_UPGRADE_HEIGHT_V2) && command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V2) != 0)
    {
      currencyBuilder.upgradeHeightV2(command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V2));
    }
    if (command_line::has_arg(vm, arg_UPGRADE_HEIGHT_V3) && command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V3) != 0)
    {
      currencyBuilder.upgradeHeightV3(command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V3));
    }
    if (command_line::has_arg(vm, arg_KEY_IMAGE_CHECKING_BLOCK_INDEX) && command_line::get_arg(vm, arg_KEY_IMAGE_CHECKING_BLOCK_INDEX) != 0)
    {
      currencyBuilder.keyImageCheckingBlockIndex(command_line::get_arg(vm, arg_KEY_IMAGE_CHECKING_BLOCK_INDEX));
    }
    if (command_line::has_arg(vm, arg_DIFFICULTY_WINDOW) && command_line::get_arg(vm, arg_DIFFICULTY_WINDOW) != 0)
    {
      currencyBuilder.difficultyWindow(command_line::get_arg(vm, arg_DIFFICULTY_WINDOW));
    }
    currencyBuilder.difficultyLag(command_line::get_arg(vm, arg_DIFFICULTY_LAG));
    currencyBuilder.difficultyCut(command_line::get_arg(vm, arg_DIFFICULTY_CUT));
  if (command_line::has_arg(vm, arg_DIFFICULTY_WINDOW_V1) && command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V1) != 0)
  {
    currencyBuilder.difficultyWindowV1(command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V1));
  }
  if (command_line::has_arg(vm, arg_DIFFICULTY_WINDOW_V2) && command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V2) != 0)
  {
    currencyBuilder.difficultyWindowV2(command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V2));
  }
  currencyBuilder.difficultyLagV1(command_line::get_arg(vm, arg_DIFFICULTY_LAG_V1));
  currencyBuilder.difficultyLagV2(command_line::get_arg(vm, arg_DIFFICULTY_LAG_V2));
  currencyBuilder.difficultyCutV1(command_line::get_arg(vm, arg_DIFFICULTY_CUT_V1));
  currencyBuilder.difficultyCutV2(command_line::get_arg(vm, arg_DIFFICULTY_CUT_V2));
bool blockexplorer_mode = command_line::get_arg(vm, arg_blockexplorer_on);
currencyBuilder.isBlockexplorer(blockexplorer_mode);
  CryptoNote::Currency currency = currencyBuilder.currency();
  for (const auto& address_string : genesis_block_reward_addresses) {
     CryptoNote::AccountPublicAddress address;
    if (!currency.parseAccountAddressString(address_string, address)) {
      std::cout << "Failed to parse address: " << address_string << std::endl;
      return;
    }
    targets.emplace_back(std::move(address));
  }
  if (targets.empty()) {
    if (CryptoNote::parameters::GENESIS_BLOCK_REWARD > 0) {
      std::cout << "Error: genesis block reward addresses are not defined" << std::endl;
    } else {
  CryptoNote::Transaction tx = currencyBuilder.generateGenesisTransaction();
  std::string tx_hex = Common::toHex(CryptoNote::toBinaryArray(tx));
  std::cout << "Add this line into your coin configuration file as is: " << std::endl;
  std::cout << "GENESIS_COINBASE_TX_HEX=" << tx_hex << std::endl;
    }
  } else {
  CryptoNote::CurrencyBuilder  currencyBuilder(logManager);
  currencyBuilder.genesisCoinbaseTxHex(command_line::get_arg(vm, arg_GENESIS_COINBASE_TX_HEX));
  currencyBuilder.publicAddressBase58Prefix(command_line::get_arg(vm, arg_CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX));
  currencyBuilder.moneySupply(command_line::get_arg(vm, arg_MONEY_SUPPLY));
  currencyBuilder.emissionSpeedFactor(command_line::get_arg(vm, arg_EMISSION_SPEED_FACTOR));
  currencyBuilder.blockGrantedFullRewardZone(command_line::get_arg(vm, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE));
  currencyBuilder.blockGrantedFullRewardZoneV1(command_line::get_arg(vm, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1));
  currencyBuilder.blockGrantedFullRewardZoneV2(command_line::get_arg(vm, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2));
  currencyBuilder.numberOfDecimalPlaces(command_line::get_arg(vm, arg_CRYPTONOTE_DISPLAY_DECIMAL_POINT));
  currencyBuilder.mininumFee(command_line::get_arg(vm, arg_MINIMUM_FEE));
  currencyBuilder.defaultDustThreshold(command_line::get_arg(vm, arg_DEFAULT_DUST_THRESHOLD));
  currencyBuilder.difficultyTarget(command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
  currencyBuilder.minedMoneyUnlockWindow(command_line::get_arg(vm, arg_CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW));
  currencyBuilder.maxBlockSizeInitial(command_line::get_arg(vm, arg_MAX_BLOCK_SIZE_INITIAL));
  if (command_line::has_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY) && command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY) != 0)
  {
    currencyBuilder.difficultyWindow(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
    currencyBuilder.difficultyWindowV1(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
    currencyBuilder.difficultyWindowV2(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
    currencyBuilder.upgradeVotingWindow(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
    currencyBuilder.upgradeWindow(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
  } else {
    currencyBuilder.difficultyWindow(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    currencyBuilder.difficultyWindowV1(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    currencyBuilder.difficultyWindowV2(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
  }
  currencyBuilder.maxBlockSizeGrowthSpeedDenominator(365 * 24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
  currencyBuilder.lockedTxAllowedDeltaSeconds(command_line::get_arg(vm, arg_DIFFICULTY_TARGET) * CryptoNote::parameters::CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS);
  if (command_line::has_arg(vm, arg_UPGRADE_HEIGHT_V2) && command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V2) != 0)
  {
    currencyBuilder.upgradeHeightV2(command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V2));
  }
  if (command_line::has_arg(vm, arg_UPGRADE_HEIGHT_V3) && command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V3) != 0)
  {
    currencyBuilder.upgradeHeightV3(command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V3));
  }
  currencyBuilder.difficultyLag(command_line::get_arg(vm, arg_DIFFICULTY_LAG));
  currencyBuilder.difficultyCut(command_line::get_arg(vm, arg_DIFFICULTY_CUT));
  if (command_line::has_arg(vm, arg_DIFFICULTY_WINDOW_V1) && command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V1) != 0)
  {
    currencyBuilder.difficultyWindowV1(command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V1));
  }
  if (command_line::has_arg(vm, arg_DIFFICULTY_WINDOW_V2) && command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V2) != 0)
  {
    currencyBuilder.difficultyWindowV2(command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V2));
  }
  currencyBuilder.difficultyLagV1(command_line::get_arg(vm, arg_DIFFICULTY_LAG_V1));
  currencyBuilder.difficultyLagV2(command_line::get_arg(vm, arg_DIFFICULTY_LAG_V2));
  currencyBuilder.difficultyCutV1(command_line::get_arg(vm, arg_DIFFICULTY_CUT_V1));
  currencyBuilder.difficultyCutV2(command_line::get_arg(vm, arg_DIFFICULTY_CUT_V2));
  currencyBuilder.genesisBlockReward(command_line::get_arg(vm, arg_GENESIS_BLOCK_REWARD));
  CryptoNote::Transaction tx = currencyBuilder.generateGenesisTransaction(targets);
    currencyBuilder.cryptonoteName(command_line::get_arg(vm, arg_CRYPTONOTE_NAME));
      std::string tx_hex = Common::toHex(CryptoNote::toBinaryArray(tx));
      std::cout << "Modify this line into your coin configuration file as is: " << std::endl;
  std::cout << "GENESIS_COINBASE_TX_HEX=" << tx_hex << std::endl;
  }
  return;
}

JsonValue buildLoggerConfiguration(Level level, const std::string& logfile) {
  JsonValue loggerConfiguration(JsonValue::OBJECT);
  loggerConfiguration.insert("globalLevel", static_cast<int64_t>(level));

  JsonValue& cfgLoggers = loggerConfiguration.insert("loggers", JsonValue::ARRAY);

  JsonValue& fileLogger = cfgLoggers.pushBack(JsonValue::OBJECT);
  fileLogger.insert("type", "file");
  fileLogger.insert("filename", logfile);
  fileLogger.insert("level", static_cast<int64_t>(TRACE));

  JsonValue& consoleLogger = cfgLoggers.pushBack(JsonValue::OBJECT);
  consoleLogger.insert("type", "console");
  consoleLogger.insert("level", static_cast<int64_t>(TRACE));
  consoleLogger.insert("pattern", "%D %T %L ");

  return loggerConfiguration;
}

int main(int argc, char* argv[])
{

#ifdef WIN32
  _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

  LoggerManager logManager;
  LoggerRef logger(logManager, "daemon");

  try {
    po::options_description desc_cmd_only("Command line options");
    po::options_description desc_cmd_sett("Command line options and settings options");

    command_line::add_arg(desc_cmd_only, command_line::arg_help);
    command_line::add_arg(desc_cmd_only, command_line::arg_version);
    command_line::add_arg(desc_cmd_only, arg_os_version);
    // tools::get_default_data_dir() can't be called during static initialization
    command_line::add_arg(desc_cmd_only, command_line::arg_data_dir, Tools::getDefaultDataDirectory());
    command_line::add_arg(desc_cmd_only, arg_config_file);

    command_line::add_arg(desc_cmd_sett, arg_log_file);
    command_line::add_arg(desc_cmd_sett, arg_log_level);
    command_line::add_arg(desc_cmd_sett, arg_console);
    command_line::add_arg(desc_cmd_sett, arg_set_fee_address);
    command_line::add_arg(desc_cmd_sett, arg_testnet_on);
    command_line::add_arg(desc_cmd_sett, arg_GENESIS_COINBASE_TX_HEX);
    command_line::add_arg(desc_cmd_sett, arg_CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX);
    command_line::add_arg(desc_cmd_sett, arg_MONEY_SUPPLY);
    command_line::add_arg(desc_cmd_sett, arg_EMISSION_SPEED_FACTOR);
    command_line::add_arg(desc_cmd_sett, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE);
    command_line::add_arg(desc_cmd_sett, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1);
    command_line::add_arg(desc_cmd_sett, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2);
    command_line::add_arg(desc_cmd_sett, arg_CRYPTONOTE_DISPLAY_DECIMAL_POINT);
    command_line::add_arg(desc_cmd_sett, arg_MINIMUM_FEE);
    command_line::add_arg(desc_cmd_sett, arg_DEFAULT_DUST_THRESHOLD);
    command_line::add_arg(desc_cmd_sett, arg_DIFFICULTY_TARGET);
    command_line::add_arg(desc_cmd_sett, arg_CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW);
    command_line::add_arg(desc_cmd_sett, arg_MAX_BLOCK_SIZE_INITIAL);
    command_line::add_arg(desc_cmd_sett, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY);
    command_line::add_arg(desc_cmd_sett, arg_UPGRADE_HEIGHT_V2);
    command_line::add_arg(desc_cmd_sett, arg_UPGRADE_HEIGHT_V3);
    command_line::add_arg(desc_cmd_sett, arg_KEY_IMAGE_CHECKING_BLOCK_INDEX);
    command_line::add_arg(desc_cmd_sett, arg_DIFFICULTY_WINDOW_V1);
    command_line::add_arg(desc_cmd_sett, arg_DIFFICULTY_WINDOW_V2);
    command_line::add_arg(desc_cmd_sett, arg_DIFFICULTY_CUT_V1);
    command_line::add_arg(desc_cmd_sett, arg_DIFFICULTY_CUT_V2);
    command_line::add_arg(desc_cmd_sett, arg_DIFFICULTY_LAG_V1);
    command_line::add_arg(desc_cmd_sett, arg_DIFFICULTY_LAG_V2);
    command_line::add_arg(desc_cmd_sett, arg_DIFFICULTY_WINDOW);
    command_line::add_arg(desc_cmd_sett, arg_DIFFICULTY_CUT);
    command_line::add_arg(desc_cmd_sett, arg_DIFFICULTY_LAG);
    command_line::add_arg(desc_cmd_sett, arg_CRYPTONOTE_NAME);
    command_line::add_arg(desc_cmd_sett, arg_CHECKPOINT);
    command_line::add_arg(desc_cmd_sett, arg_BUGGED_ZAWY_DIFFICULTY_BLOCK_INDEX);
    command_line::add_arg(desc_cmd_sett, arg_ZAWY_LWMA_DIFFICULTY_BLOCK_INDEX);
    command_line::add_arg(desc_cmd_sett, arg_ZAWY_LWMA_DIFFICULTY_LAST_BLOCK);
    command_line::add_arg(desc_cmd_sett, arg_ZAWY_LWMA_DIFFICULTY_N);
    command_line::add_arg(desc_cmd_sett, arg_ZAWY_DIFFICULTY_BLOCK_INDEX);
    command_line::add_arg(desc_cmd_sett, arg_ZAWY_DIFFICULTY_LAST_BLOCK);
    command_line::add_arg(desc_cmd_sett, arg_GENESIS_BLOCK_REWARD);
    command_line::add_arg(desc_cmd_sett, arg_CRYPTONOTE_COIN_VERSION);
    command_line::add_arg(desc_cmd_sett, arg_TAIL_EMISSION_REWARD);
    command_line::add_arg(desc_cmd_sett, arg_KILL_HEIGHT);
    command_line::add_arg(desc_cmd_sett, arg_MANDATORY_TRANSACTION);
    command_line::add_arg(desc_cmd_sett, arg_MIXIN_START_HEIGHT);
    command_line::add_arg(desc_cmd_sett, arg_MIN_MIXIN);
    command_line::add_arg(desc_cmd_sett, arg_MANDATORY_MIXIN_BLOCK_VERSION);
command_line::add_arg(desc_cmd_sett, arg_enable_cors);
    command_line::add_arg(desc_cmd_sett, arg_blockexplorer_on);
command_line::add_arg(desc_cmd_sett, arg_print_genesis_tx);
  command_line::add_arg(desc_cmd_sett, arg_genesis_block_reward_address);

    RpcServerConfig::initOptions(desc_cmd_sett);
    NetNodeConfig::initOptions(desc_cmd_sett);
    DataBaseConfig::initOptions(desc_cmd_sett);

    po::options_description desc_options("Allowed options");
    desc_options.add(desc_cmd_only).add(desc_cmd_sett);

    po::variables_map vm;
    boost::filesystem::path data_dir_path;
    bool r = command_line::handle_error_helper(desc_options, [&]()
    {
      po::store(po::parse_command_line(argc, argv, desc_options), vm);

      if (command_line::get_arg(vm, command_line::arg_help))
      {
        std::cout << CryptoNote::CRYPTONOTE_NAME << " v" << PROJECT_VERSION_LONG << ENDL << ENDL;
        std::cout << desc_options << std::endl;
        return false;
      }

      std::string data_dir = command_line::get_arg(vm, command_line::arg_data_dir);

	  std::string config = command_line::get_arg(vm, arg_config_file);

	  if (!config.empty())
      {
		  data_dir_path = data_dir;
		  boost::filesystem::path config_path(config);
		  if (!config_path.has_parent_path()) {
			config_path = data_dir_path / config_path;
		  }

		  boost::system::error_code ec;
		  if (boost::filesystem::exists(config_path, ec)) {
			std::cout << "Success: Configuration file openned: " << config_path << std::endl;
			po::store(po::parse_config_file<char>(config_path.string<std::string>().c_str(), desc_cmd_sett, true), vm);
		  }
		  else
		  {
			std::cout << "Configuration error: Cannot open configuration file" << std::endl;
			std::cout << "" << std::endl;
			std::cout << "Usage:" << std::endl;
			std::cout << "Windows:   forknoted.exe --config-file configs/dashcoin.conf" << std::endl;
			std::cout << "Linux/Mac:   ./forknoted --config-file configs/dashcoin.conf" << std::endl;
			return false;
		  }
	  }

      po::notify(vm);
      if (command_line::get_arg(vm, command_line::arg_data_dir) == Tools::getDefaultDataDirectory() && command_line::has_arg(vm, arg_CRYPTONOTE_NAME) && !command_line::get_arg(vm, arg_CRYPTONOTE_NAME).empty()) {
        boost::replace_all(data_dir, CryptoNote::CRYPTONOTE_NAME, command_line::get_arg(vm, arg_CRYPTONOTE_NAME));
      }

	  data_dir_path = data_dir;

	  if (command_line::get_arg(vm, arg_print_genesis_tx)) {
        print_genesis_tx_hex(vm, logManager);
        return false;
      }

	  return true;
    });

    if (!r)
      return 1;

    auto modulePath = Common::NativePathToGeneric(argv[0]);
    auto cfgLogFile = Common::NativePathToGeneric(command_line::get_arg(vm, arg_log_file));

    if (cfgLogFile.empty()) {
      cfgLogFile = Common::ReplaceExtenstion(modulePath, ".log");
    } else {
      if (!Common::HasParentPath(cfgLogFile)) {
        cfgLogFile = Common::CombinePath(Common::GetPathDirectory(modulePath), cfgLogFile);
      }
    }

    Level cfgLogLevel = static_cast<Level>(static_cast<int>(Logging::ERROR) + command_line::get_arg(vm, arg_log_level));

    // configure logging
    logManager.configure(buildLoggerConfiguration(cfgLogLevel, cfgLogFile));

    logger(INFO) << CryptoNote::CRYPTONOTE_NAME << " v" << PROJECT_VERSION_LONG;

    if (command_line_preprocessor(vm, logger)) {
      return 0;
    }

    logger(INFO) << "Module folder: " << argv[0];

    bool testnet_mode = command_line::get_arg(vm, arg_testnet_on);
    if (testnet_mode) {
      logger(INFO) << "Starting in testnet mode!";
    }

    //create objects and link them
    CryptoNote::CurrencyBuilder currencyBuilder(logManager);
    currencyBuilder.cryptonoteName(command_line::get_arg(vm, arg_CRYPTONOTE_NAME));
  currencyBuilder.minMixin(command_line::get_arg(vm, arg_MIN_MIXIN));
//uint8_t recognized as char
  if (command_line::get_arg(vm, arg_MANDATORY_MIXIN_BLOCK_VERSION) == 0) {
    currencyBuilder.mandatoryMixinBlockVersion(command_line::get_arg(vm, arg_MANDATORY_MIXIN_BLOCK_VERSION));
  } else {
    currencyBuilder.mandatoryMixinBlockVersion(command_line::get_arg(vm, arg_MANDATORY_MIXIN_BLOCK_VERSION) - '0');
  }
  currencyBuilder.mandatoryTransaction(command_line::get_arg(vm, arg_MANDATORY_TRANSACTION));
    currencyBuilder.genesisCoinbaseTxHex(command_line::get_arg(vm, arg_GENESIS_COINBASE_TX_HEX));
    currencyBuilder.publicAddressBase58Prefix(command_line::get_arg(vm, arg_CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX));
    currencyBuilder.moneySupply(command_line::get_arg(vm, arg_MONEY_SUPPLY));
    currencyBuilder.emissionSpeedFactor(command_line::get_arg(vm, arg_EMISSION_SPEED_FACTOR));
    currencyBuilder.blockGrantedFullRewardZone(command_line::get_arg(vm, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE));
    currencyBuilder.blockGrantedFullRewardZoneV1(command_line::get_arg(vm, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1));
    currencyBuilder.blockGrantedFullRewardZoneV2(command_line::get_arg(vm, arg_CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2));
    currencyBuilder.numberOfDecimalPlaces(command_line::get_arg(vm, arg_CRYPTONOTE_DISPLAY_DECIMAL_POINT));
    currencyBuilder.mininumFee(command_line::get_arg(vm, arg_MINIMUM_FEE));
    currencyBuilder.defaultDustThreshold(command_line::get_arg(vm, arg_DEFAULT_DUST_THRESHOLD));
    currencyBuilder.difficultyTarget(command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    currencyBuilder.minedMoneyUnlockWindow(command_line::get_arg(vm, arg_CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW));
    currencyBuilder.maxBlockSizeInitial(command_line::get_arg(vm, arg_MAX_BLOCK_SIZE_INITIAL));
    if (command_line::has_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY) && command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY) != 0)
    {
      currencyBuilder.expectedNumberOfBlocksPerDay(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
      currencyBuilder.difficultyWindow(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
    currencyBuilder.difficultyWindowV1(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
    currencyBuilder.difficultyWindowV2(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
      currencyBuilder.upgradeVotingWindow(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
      currencyBuilder.upgradeWindow(command_line::get_arg(vm, arg_EXPECTED_NUMBER_OF_BLOCKS_PER_DAY));
    } else {
      currencyBuilder.expectedNumberOfBlocksPerDay(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
      currencyBuilder.difficultyWindow(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    currencyBuilder.difficultyWindowV1(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    currencyBuilder.difficultyWindowV2(24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    }
    currencyBuilder.maxBlockSizeGrowthSpeedDenominator(365 * 24 * 60 * 60 / command_line::get_arg(vm, arg_DIFFICULTY_TARGET));
    currencyBuilder.lockedTxAllowedDeltaSeconds(command_line::get_arg(vm, arg_DIFFICULTY_TARGET) * CryptoNote::parameters::CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS);
    if (command_line::has_arg(vm, arg_UPGRADE_HEIGHT_V2) && command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V2) != 0)
    {
      currencyBuilder.upgradeHeightV2(command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V2));
    }
    if (command_line::has_arg(vm, arg_UPGRADE_HEIGHT_V3) && command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V3) != 0)
    {
      currencyBuilder.upgradeHeightV3(command_line::get_arg(vm, arg_UPGRADE_HEIGHT_V3));
    }
    if (command_line::has_arg(vm, arg_KEY_IMAGE_CHECKING_BLOCK_INDEX) && command_line::get_arg(vm, arg_KEY_IMAGE_CHECKING_BLOCK_INDEX) != 0)
    {
      currencyBuilder.keyImageCheckingBlockIndex(command_line::get_arg(vm, arg_KEY_IMAGE_CHECKING_BLOCK_INDEX));
    }
    if (command_line::has_arg(vm, arg_DIFFICULTY_WINDOW) && command_line::get_arg(vm, arg_DIFFICULTY_WINDOW) != 0)
    {
      currencyBuilder.difficultyWindow(command_line::get_arg(vm, arg_DIFFICULTY_WINDOW));
    }
    currencyBuilder.difficultyLag(command_line::get_arg(vm, arg_DIFFICULTY_LAG));
    currencyBuilder.difficultyCut(command_line::get_arg(vm, arg_DIFFICULTY_CUT));
  if (command_line::has_arg(vm, arg_DIFFICULTY_WINDOW_V1) && command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V1) != 0)
  {
    currencyBuilder.difficultyWindowV1(command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V1));
  }
  if (command_line::has_arg(vm, arg_DIFFICULTY_WINDOW_V2) && command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V2) != 0)
  {
    currencyBuilder.difficultyWindowV2(command_line::get_arg(vm, arg_DIFFICULTY_WINDOW_V2));
  }
  currencyBuilder.difficultyLagV1(command_line::get_arg(vm, arg_DIFFICULTY_LAG_V1));
  currencyBuilder.difficultyLagV2(command_line::get_arg(vm, arg_DIFFICULTY_LAG_V2));
  currencyBuilder.difficultyCutV1(command_line::get_arg(vm, arg_DIFFICULTY_CUT_V1));
  currencyBuilder.difficultyCutV2(command_line::get_arg(vm, arg_DIFFICULTY_CUT_V2));
bool blockexplorer_mode = command_line::get_arg(vm, arg_blockexplorer_on);
currencyBuilder.isBlockexplorer(blockexplorer_mode);
    currencyBuilder.mixinStartHeight(command_line::get_arg(vm, arg_MIXIN_START_HEIGHT));
    currencyBuilder.killHeight(command_line::get_arg(vm, arg_KILL_HEIGHT));
    currencyBuilder.tailEmissionReward(command_line::get_arg(vm, arg_TAIL_EMISSION_REWARD));
    currencyBuilder.cryptonoteCoinVersion(command_line::get_arg(vm, arg_CRYPTONOTE_COIN_VERSION));
    currencyBuilder.genesisBlockReward(command_line::get_arg(vm, arg_GENESIS_BLOCK_REWARD));
    currencyBuilder.zawyDifficultyBlockIndex(command_line::get_arg(vm, arg_ZAWY_DIFFICULTY_BLOCK_INDEX));
    currencyBuilder.zawyDifficultyLastBlock(command_line::get_arg(vm, arg_ZAWY_DIFFICULTY_LAST_BLOCK));
    currencyBuilder.zawyLWMADifficultyBlockIndex(command_line::get_arg(vm, arg_ZAWY_LWMA_DIFFICULTY_BLOCK_INDEX));
    currencyBuilder.zawyLWMADifficultyLastBlock(command_line::get_arg(vm, arg_ZAWY_LWMA_DIFFICULTY_LAST_BLOCK));
    currencyBuilder.zawyLWMADifficultyN(command_line::get_arg(vm, arg_ZAWY_LWMA_DIFFICULTY_N));
    currencyBuilder.buggedZawyDifficultyBlockIndex(command_line::get_arg(vm, arg_BUGGED_ZAWY_DIFFICULTY_BLOCK_INDEX));
    currencyBuilder.testnet(testnet_mode);
    try {
      currencyBuilder.currency();
    } catch (std::exception&) {
      std::cout << "GENESIS_COINBASE_TX_HEX constant has an incorrect value. Please launch: " << CryptoNote::CRYPTONOTE_NAME << "d --" << arg_print_genesis_tx.name;
      return 1;
    }
    CryptoNote::Currency currency = currencyBuilder.currency();

    CryptoNote::Checkpoints checkpoints(logManager);
std::vector<CryptoNote::CheckpointData> checkpoint_input;
std::vector<std::string> checkpoint_args = command_line::get_arg(vm, arg_CHECKPOINT);
std::vector<std::string> checkpoint_blockIds;
if (command_line::has_arg(vm, arg_CHECKPOINT) && checkpoint_args.size() != 0)
{
  for(const std::string& str: checkpoint_args) {
    std::string::size_type p = str.find(':');
    if(p != std::string::npos)
    {
      uint32_t checkpoint_height = std::stoull(str.substr(0, p));
      checkpoint_blockIds.push_back(str.substr(p+1, str.size()));
      checkpoint_input.push_back({ checkpoint_height, checkpoint_blockIds.back().c_str() });
    }
  }
}
else
{
  if (command_line::get_arg(vm, arg_CRYPTONOTE_NAME) == "bytecoin") {
      checkpoint_input = CryptoNote::CHECKPOINTS;
  }
}
    if (!testnet_mode) {
for (const auto& cp : checkpoint_input) {
        checkpoints.addCheckpoint(cp.index, cp.blockId);
      }
    }

    NetNodeConfig netNodeConfig;
    netNodeConfig.init(vm);
    netNodeConfig.setTestnet(testnet_mode);
    netNodeConfig.setConfigFolder(data_dir_path.string());

    RpcServerConfig rpcConfig;
    rpcConfig.init(vm);

    DataBaseConfig dbConfig;
    dbConfig.init(vm);

    dbConfig.setDataDir(data_dir_path.string());
    if (dbConfig.isConfigFolderDefaulted()) {
      if (!Tools::create_directories_if_necessary(dbConfig.getDataDir())) {
        throw std::runtime_error("Can't create directory: " + dbConfig.getDataDir());
      }
    } else {
      if (!Tools::directoryExists(dbConfig.getDataDir())) {
        throw std::runtime_error("Directory does not exist: " + dbConfig.getDataDir());
      }
    }

    RocksDBWrapper database(logManager);
    database.init(dbConfig);
    Tools::ScopeExit dbShutdownOnExit([&database] () { database.shutdown(); });

    if (!DatabaseBlockchainCache::checkDBSchemeVersion(database, logManager))
    {
      dbShutdownOnExit.cancel();
      database.shutdown();

      database.destoy(dbConfig);

      database.init(dbConfig);
      dbShutdownOnExit.resume();
    }

    System::Dispatcher dispatcher;
    logger(INFO) << "Initializing core...";
    CryptoNote::Core ccore(
      currency,
      logManager,
      std::move(checkpoints),
      dispatcher,
      std::unique_ptr<IBlockchainCacheFactory>(new DatabaseBlockchainCacheFactory(database, logger.getLogger())),
      createSwappedMainChainStorage(data_dir_path.string(), currency));

    ccore.load();
    logger(INFO) << "Core initialized OK";

    CryptoNote::CryptoNoteProtocolHandler cprotocol(currency, dispatcher, ccore, nullptr, logManager);
    CryptoNote::NodeServer p2psrv(dispatcher, cprotocol, logManager);
    CryptoNote::RpcServer rpcServer(dispatcher, logManager, ccore, p2psrv, cprotocol);

    cprotocol.set_p2p_endpoint(&p2psrv);
    DaemonCommandsHandler dch(ccore, p2psrv, logManager);
    logger(INFO) << "Initializing p2p server...";
    if (!p2psrv.init(netNodeConfig)) {
      logger(ERROR, BRIGHT_RED) << "Failed to initialize p2p server.";
      return 1;
    }

    logger(INFO) << "P2p server initialized OK";

    if (!command_line::has_arg(vm, arg_console)) {
      dch.start_handling();
    }

    logger(INFO) << "Starting core rpc server on address " << rpcConfig.getBindAddress();
    rpcServer.start(rpcConfig.bindIp, rpcConfig.bindPort);
  rpcServer.setFeeAddress(command_line::get_arg(vm, arg_set_fee_address));
rpcServer.enableCors(command_line::get_arg(vm, arg_enable_cors));
    logger(INFO) << "Core rpc server started ok";

    Tools::SignalHandler::install([&dch, &p2psrv] {
      dch.stop_handling();
      p2psrv.sendStopSignal();
    });

    logger(INFO) << "Starting p2p net loop...";
    p2psrv.run();
    logger(INFO) << "p2p net loop stopped";

    dch.stop_handling();

    //stop components
    logger(INFO) << "Stopping core rpc server...";
    rpcServer.stop();

    //deinitialize components
    logger(INFO) << "Deinitializing p2p...";
    p2psrv.deinit();

    cprotocol.set_p2p_endpoint(nullptr);
    ccore.save();

  } catch (const std::exception& e) {
    logger(ERROR, BRIGHT_RED) << "Exception: " << e.what();
    return 1;
  }

  logger(INFO) << "Node stopped.";
  return 0;
}

bool command_line_preprocessor(const boost::program_options::variables_map &vm, LoggerRef &logger) {
  bool exit = false;

  if (command_line::get_arg(vm, command_line::arg_version)) {
    std::cout << CryptoNote::CRYPTONOTE_NAME << " v" << PROJECT_VERSION_LONG << ENDL;
    exit = true;
  }
  if (command_line::get_arg(vm, arg_os_version)) {
    std::cout << "OS: " << Tools::get_os_version_string() << ENDL;
    exit = true;
  }

  if (exit) {
    return true;
  }

  return false;
}

max_stars_repo_name: JacopoDT/numerare-core

max_issues_repo_name: JacopoDT/numerare-core

max_forks_repo_name: JacopoDT/numerare-core


text:
/* Copyright (c) 2016, the Cap authors.
 *
 * This file is subject to the Modified BSD License and may not be distributed
 * without copyright and license information. Please refer to the file LICENSE
 * for the text and further information on this license.
 */

#define BOOST_TEST_MODULE TestSupercapacitorInsepector

#include "main.cc"

#include <cap/energy_storage_device.h>
#include <cap/supercapacitor.h>
#include <boost/test/unit_test.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/filesystem.hpp>
#include <cstdio>
#include <string>

// Check that the inspector can be used to ouput a distributed mesh

namespace cap
{

BOOST_AUTO_TEST_CASE(test_supercapacitor_inspector)
{
  boost::mpi::communicator comm;
  boost::property_tree::ptree device_database;
  boost::property_tree::info_parser::read_info("super_capacitor.info",
                                               device_database);

  std::shared_ptr<cap::EnergyStorageDevice> device =
      cap::EnergyStorageDevice::build(device_database, comm);

  cap::SuperCapacitorInspector<2> supercap_inspector;
  supercap_inspector.inspect(device.get());

  // Check that the files exist
  if (comm.rank() == 0)
  {
    for (int i = 0; i < comm.size(); ++i)
      BOOST_TEST(boost::filesystem::exists("solution-0000.000" +
                                           std::to_string(i) + ".vtu"));
    BOOST_TEST(boost::filesystem::exists("solution-0000.pvtu"));
  }

  // Remove the files
  if (comm.rank() == 0)
  {
    for (int i = 0; i < comm.size(); ++i)
    {
      std::string filename("solution-0000.000" + std::to_string(i) + ".vtu");
      // Delete file and check return value error code.
      BOOST_TEST(std::remove(filename.c_str()) == 0);
    }
    BOOST_TEST(std::remove("solution-0000.pvtu") == 0);
  }
}
}

ext: cc

max_stars_repo_name: iiscsahoo/EnergyData

max_issues_repo_name: iiscsahoo/EnergyData

max_forks_repo_name: iiscsahoo/EnergyData


text:
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

#include "CCDB/CcdbApi.h"
#include "CCDB/CCDBQuery.h"
#include "CCDB/CCDBTimeStampUtils.h"
#include <map>
#include "TFile.h"
#include <iostream>
#include <boost/program_options.hpp>

namespace bpo = boost::program_options;

bool initOptionsAndParse(bpo::options_description& options, int argc, char* argv[], bpo::variables_map& vm)
{
  options.add_options()(
    "host", bpo::value<std::string>()->default_value("ccdb-test.cern.ch:8080"), "CCDB server")(
    "path,p", bpo::value<std::string>(), "CCDB path (identifies the object)")(
    "dest,d", bpo::value<std::string>()->default_value("./"), "destination path")(
    "no-preserve-path", "Do not preserve path structure. If not set, the full path structure -- reflecting the '--path' argument will be put.")(
    "outfile,o", bpo::value<std::string>()->default_value("snapshot.root"), "Name of output file. If set to \"\", the name will be determined from the uploaded content.")(
    "timestamp,t", bpo::value<long>()->default_value(-1), "timestamp in ms - default -1 = now")(
    "help,h", "Produce help message.");

  try {
    bpo::store(parse_command_line(argc, argv, options), vm);

    // help
    if (vm.count("help")) {
      std::cout << options << std::endl;
      return false;
    }

    bpo::notify(vm);
  } catch (const bpo::error& e) {
    std::cerr << e.what() << "\n\n";
    std::cerr << "Error parsing command line arguments; Available options:\n";

    std::cerr << options << std::endl;
    return false;
  }
  return true;
}

// a simple tool to download the CCDB blob and store in a ROOT file
int main(int argc, char* argv[])
{
  bpo::options_description options("Allowed options");
  bpo::variables_map vm;
  if (!initOptionsAndParse(options, argc, argv, vm)) {
    return 1;
  }

  o2::ccdb::CcdbApi api;
  auto host = vm["host"].as<std::string>();
  api.init(host);

  std::map<std::string, std::string> filter;
  long timestamp = vm["timestamp"].as<long>();
  if (timestamp == -1) {
    timestamp = o2::ccdb::getCurrentTimestamp();
  }
  auto path = vm["path"].as<std::string>();
  auto dest = vm["dest"].as<std::string>();

  std::cout << "Querying host " << host << " for path " << path << " and timestamp " << timestamp << "\n";
  bool no_preserve_path = vm.count("no-preserve-path") == 0;
  auto filename = vm["outfile"].as<std::string>();
  auto success = api.retrieveBlob(path, dest, filter, timestamp, no_preserve_path, filename);

  return success ? 0 : 1;
}

ext: cxx


text:
#include "stdafx.h"
#include "ConsoleGeneratorProxy.h"

#include <iostream>
//#include <FerrisLoki/loki/Typelist.h>
#include <boost/lexical_cast.hpp>

ConsoleGeneratorProxy::ConsoleGeneratorProxy(const goal_generator_collection_t& goalGeneratorCollection)
: base_t(L"ConsoleGeneratorProxy_Menu")
, _generators(goalGeneratorCollection)
{
}

ConsoleGeneratorProxy::~ConsoleGeneratorProxy(void)
{
}

Goal::ptr_t ConsoleGeneratorProxy::GenerateGoal()
{
	std::wcout << L"Here is list of available goal generators:" << std::endl;
	std::wcout << L"\t0. Exit" << std::endl;
	size_t generatorsCount = _generators.size();
	size_t i = 0 ;
	for ( ; i < generatorsCount; ++i)
	{
		std::wcout
			<< L'\t' << boost::lexical_cast<std::wstring>(i+1) << L". "
			<< _generators[i]->Name() << std::endl;
	}
	std::wcout << L"choose: ";

	do
	{
		i = boost::lexical_cast<size_t>(std::cin.get()) - '0';
	} while (i && i > generatorsCount);

	if (i==0)
	{
		exit(0);
	}

	return _generators[i-1]->GenerateGoal();
}

max_stars_repo_name: ApusDT/openmind

max_issues_repo_name: SergMariaDB/openmind

max_forks_repo_name: SergMariaDB/openmind


text:
#include <TreeMap.h>

#include <cstdint>
#include <string>
#include <map>

#include <boost/test/unit_test.hpp>

#include <boost/mpl/list.hpp>

using TestedKeyTypes = boost::mpl::list<std::int32_t, std::uint64_t>;

template <typename K>
using Map = aisdi::TreeMap<K, std::string>;

using std::begin;
using std::end;

BOOST_AUTO_TEST_SUITE(TreeMapsTests)

template <typename K>
void thenMapContainsItems(const Map<K>& map,
                          const std::map<K, std::string>& expected)
{
  BOOST_CHECK_EQUAL(map.getSize(), expected.size());

  for (const auto& item : expected)
  {
    const auto it = map.find(item.first);
    BOOST_REQUIRE_MESSAGE(it != end(map), "Missing required item with key: " << item.first);
    BOOST_CHECK_MESSAGE(it->second == item.second,
                        "Wrong value in map for key: " << item.first
                        << " (expected: \"" << item.second
                        << "\" got: \"" << it->second << "\")");
  }
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenMap_WhenCreatedWithDefaultConstructor_ThenItIsEmpty,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map;

  BOOST_CHECK(map.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenAddingItem_ThenItIsNoLongerEmpty,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;

  map[K{}] = std::string{};

  BOOST_CHECK(!map.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenGettingIterators_ThenBeginEqualsEnd,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;

  BOOST_CHECK(begin(map) == end(map));
  BOOST_CHECK(const_cast<const Map<K>&>(map).begin() == map.end());
  BOOST_CHECK(map.cbegin() == map.cend());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNonEmptyMap_WhenGettingIterator_ThenBeginIsNotEnd,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[K{}] = std::string{};

  BOOST_CHECK(begin(map) != end(map));
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenMapWithOnePair_WhenIterating_ThenPairIsReturned,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[753] = "Rome";

  auto it = map.begin();

  BOOST_CHECK_EQUAL(it->first, 753);
  BOOST_CHECK_EQUAL(it->second, "Rome");
  BOOST_CHECK(++it == map.end());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenIterator_WhenPostIncrementing_ThenPreviousPositionIsReturned,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[K{}] = std::string{};

  auto it = map.begin();
  auto postIncrementedIt = it++;

  BOOST_CHECK(postIncrementedIt == map.begin());
  BOOST_CHECK(it == map.end());
  BOOST_CHECK(postIncrementedIt == map.cbegin());
  BOOST_CHECK(it == map.cend());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenIterator_WhenPreIncrementing_ThenNewPositionIsReturned,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[K{}] = std::string{};

  auto it = map.begin();
  auto preIncrementedIt = ++it;

  BOOST_CHECK(preIncrementedIt == it);
  BOOST_CHECK(it == map.end());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEndIterator_WhenIncrementing_ThenOperationThrows,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;

  BOOST_CHECK_THROW(map.end()++, std::out_of_range);
  BOOST_CHECK_THROW(++(map.end()), std::out_of_range);
  BOOST_CHECK_THROW(map.cend()++, std::out_of_range);
  BOOST_CHECK_THROW(++(map.cend()), std::out_of_range);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEndIterator_WhenDecrementing_ThenIteratorPointsToLastItem,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[1] = std::string{};

  auto it = map.end();
  --it;

  BOOST_CHECK(it == begin(map));
  BOOST_CHECK_EQUAL(it->first, 1);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenIterator_WhenPreDecrementing_ThenNewIteratorValueIsReturned,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[1] = std::string{};

  auto it = map.end();
  auto preDecremented = --it;

  BOOST_CHECK(it == preDecremented);
  BOOST_CHECK_EQUAL(it->first, 1);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenIterator_WhenPostDecrementing_ThenOldIteratorValueIsReturned,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[1] = std::string{};

  auto it = map.end();
  auto postDecremented = it--;

  BOOST_CHECK(postDecremented == map.end());
  BOOST_CHECK_EQUAL(it->first, 1);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenBeginIterator_WhenDecrementing_ThenOperationThrows,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;

  BOOST_CHECK_THROW(map.begin()--, std::out_of_range);
  BOOST_CHECK_THROW(--(map.begin()), std::out_of_range);
  BOOST_CHECK_THROW(map.cbegin()--, std::out_of_range);
  BOOST_CHECK_THROW(--(map.cbegin()), std::out_of_range);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEndIterator_WhenDereferencing_ThenOperationThrows,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;

  BOOST_CHECK_THROW(*map.end(), std::out_of_range);
  BOOST_CHECK_THROW(*map.cend(), std::out_of_range);
  BOOST_CHECK_THROW(map.end()->first, std::out_of_range);
  BOOST_CHECK_THROW(map.cend()->second, std::out_of_range);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenConstIterator_WhenDereferencing_ThenItemIsReturned,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[42] = "Answer";

  const auto it = map.cbegin();

  BOOST_CHECK_EQUAL(it->first, 42);
  BOOST_CHECK_EQUAL(it->second, "Answer");
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenSearchingForKey_ThenEndIsReturned,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map;

  const auto it = map.find(123);

  BOOST_CHECK(it == end(map));
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNonEmptyMap_WhenSearchingForMissingKey_ThenEndIsReturned,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[321] = "Not it";

  const auto it = map.find(123);

  BOOST_CHECK(it == end(map));
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNonEmptyMap_WhenSearchingForKey_ThenItemIsReturned,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[321] = "Not it";
  map[123] = "It!";

  const auto it = map.find(123);

  BOOST_CHECK(it != end(map));
  BOOST_CHECK_EQUAL(it->first, 123);
  BOOST_CHECK_EQUAL(it->second, "It!");
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenGettingSize_ThenZeroIsReturnd,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map;

  BOOST_CHECK_EQUAL(map.getSize(), 0);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNonEmptyMap_WhenGettingSize_ThenItemCountIsReturnd,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  map[1] = "1";
  map[2] = "1";

  BOOST_CHECK_EQUAL(map.getSize(), 2);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenMap_WhenInitializingFromListOfPairs_ThenAllItemsAreInMap,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };

  thenMapContainsItems(map, { { 42, "Alice" }, { 27, "Bob" } });
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenIterator_WhenDereferencing_ThenItemCanBeChanged,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 42, "Chuck" }, { 27, "Bob" } };

  auto it = map.find(42);
  it->second = "Alice";

  thenMapContainsItems(map, { { 42, "Alice" }, { 27, "Bob" } });
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenAddingItem_ThenItemIsInMap,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;

  map[42] = "Alice";

  thenMapContainsItems(map, { { 42, "Alice" } });
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNonEmptyMap_WhenChangingItem_ThenNewValueIsInMap,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 42, "Chuck" }, { 27, "Bob" } };

  map[42] = "Alice";

  thenMapContainsItems(map, { { 42, "Alice" }, { 27, "Bob" } });
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenCreatingCopy_ThenBothMapsAreEmpty,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map;
  const Map<K> other(map);

  BOOST_CHECK(other.isEmpty());
  BOOST_CHECK(map.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNonEmptyMap_WhenCreatingCopy_ThenAllItemsAreCopied,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 753, "Rome" }, { 1789, "Paris" } };
  const Map<K> other{map};

  map[1410] = "Grunwald";

  thenMapContainsItems(map, { { 1410, "Grunwald" }, { 753, "Rome" }, { 1789, "Paris" } });
  thenMapContainsItems(other, { { 753, "Rome" }, { 1789, "Paris" } });
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenMovingToOther_ThenBothMapsAreEmpty,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  Map<K> other{std::move(map)};

  BOOST_CHECK(map.isEmpty());
  BOOST_CHECK(other.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNonEmptyMap_WhenMovingToOther_ThenAllItemsAreMoved,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 753, "Rome" }, { 1789, "Paris" } };
  const Map<K> other{std::move(map)};

  thenMapContainsItems(other, { { 753, "Rome" }, { 1789, "Paris" } });
  BOOST_CHECK(map.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenAssigningToOther_ThenOtherMapIsEmpty,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map;
  Map<K> other = { { 42, "Alice" }, { 27, "Bob" } };

  other = map;

  BOOST_CHECK(other.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNonEmptyMap_WhenAssigningToOther_ThenAllElementsAreCopied,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 753, "Rome" }, { 1789, "Paris" } };
  Map<K> other = { { 42, "Alice" }, { 27, "Bob" } };

  other = map;
  map[1410] = "Grunwald";

  thenMapContainsItems(other, { { 753, "Rome" }, { 1789, "Paris" } });
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenSelfAssigning_ThenNothingHappens,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;

  map = map;

  BOOST_CHECK(map.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNotEmptyMap_WhenSelfAssigning_ThenNothingHappens,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };

  map = map;

  thenMapContainsItems(map, { { 42, "Alice" }, { 27, "Bob" } });
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenMoveAssigning_ThenBothMapsAreEmpty,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;
  Map<K> other = { { 42, "Alice" }, { 27, "Bob" } };

  other = std::move(map);

  BOOST_CHECK(other.isEmpty());
  BOOST_CHECK(map.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNonEmptyMap_WhenMoveAssigning_ThenAllElementsAreMoved,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 753, "Rome" }, { 1789, "Paris" } };
  Map<K> other = { { 42, "Alice" }, { 27, "Bob" } };

  other = std::move(map);

  thenMapContainsItems(other, { { 753, "Rome" }, { 1789, "Paris" } });
  BOOST_CHECK(map.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenReadingValueOfAnyKey_ThenExceptionIsThrown,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map;

  BOOST_CHECK_THROW(map.valueOf(1), std::out_of_range);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNotEmptyMap_WhenReadingValueOfMissingKey_ThenExceptionIsThrown,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };

  BOOST_CHECK_THROW(map.valueOf(1), std::out_of_range);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNotEmptyMap_WhenReadingValueOfAKey_ThenValueIsReturned,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };

  BOOST_CHECK_EQUAL(map.valueOf(42), "Alice");
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNotEmptyMap_WhenChangingValueOfAKey_ThenValueIsChanged,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };

  map.valueOf(42) = "Chuck";

  thenMapContainsItems(map, { { 42, "Chuck" }, { 27, "Bob" } });
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenEmptyMap_WhenRemovingValueByKey_ThenExceptionIsThrown,
                              K,
                              TestedKeyTypes)
{
  Map<K> map;

  BOOST_CHECK_THROW(map.remove(1), std::out_of_range);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNotEmptyMap_WhenRemovingValueByWrongKey_ThenExceptionIsThrown,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };

  BOOST_CHECK_THROW(map.remove(1), std::out_of_range);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNotEmptyMap_WhenRemovingValueByKey_ThenItemIsRemoved,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };

  map.remove(27);

  thenMapContainsItems(map, { { 42, "Alice" } });
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenSingleItemMap_WhenRemovingValueByKey_ThenMapBecomesEmpty,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 27, "Bob" } };

  map.remove(27);

  BOOST_CHECK(map.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNotEmptyMap_WhenErasingEnd_ThenExceptionIsThrown,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };

  BOOST_CHECK_THROW(map.remove(end(map)), std::out_of_range);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenNotEmptyMap_WhenRemovingItemByIterator_ThenItemIsRemoved,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };

  map.remove(map.find(42));

  thenMapContainsItems(map, { { 27, "Bob" } });
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenSingleItemMap_WhenRemovingItemByIterator_ThenMapBecomesEmpty,
                              K,
                              TestedKeyTypes)
{
  Map<K> map = { { 42, "Alice" } };

  map.remove(map.find(42));

  BOOST_CHECK(map.isEmpty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenTwoEmptyMaps_WhenComparingThem_ThenTheyAreReportedAsEqual,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map;
  const Map<K> other;

  BOOST_CHECK(map == other);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenTwoEqualMaps_WhenComparingThem_ThenTheyAreReportedAsEqual,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };
  const Map<K> other = { { 42, "Alice" }, { 27, "Bob" } };

  BOOST_CHECK(map == other);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenTwoEquivalentMaps_WhenComparingThem_ThenTheyAreReportedAsEqual,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };
  const Map<K> other = { { 27, "Bob" }, { 42, "Alice" } };

  BOOST_CHECK(map == other);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenTwoMapsWithDifferentValues_WhenComparingThem_ThenTheyAreNotEqual,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map = { { 42, "Alice" }, { 27, "Bob" } };
  const Map<K> other = { { 27, "Alice" }, { 42, "Bob" } };

  BOOST_CHECK(map != other);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(GivenTwoMapsWithDifferentKeys_WhenComparingThem_ThenTheyAreNotEqual,
                              K,
                              TestedKeyTypes)
{
  const Map<K> map = { { 42, "Alice" }, { 27, "Bob" }, { 13, "Chuck" } };
  const Map<K> other = { { 27, "Alice" }, { 42, "Bob" } };

  BOOST_CHECK(map != other);
}

// ConstIterator is tested via Iterator methods.
// If Iterator methods are to be changed, then new ConstIterator tests are required.

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: WorkingFen/CppBasics

max_issues_repo_name: WorkingFen/CppBasics

max_forks_repo_name: WorkingFen/CppBasics


text:
// prepare-data2.cc -- converts Michael Collins new file into my training format
//
// Mark Johnson, 8th October 2003

const char usage[] =
"Usage: prepare-data penn-wsj-regex nsentences collins-tree-command collins-score-command\n"
"\n"
"where:\n"
"  penn-wsj-regex is a regular expression that expands to the names of\n"
"      the Penn WSJ treebank files,\n"
"  nsentences is the number of sentences in the data,\n"
"  collins-tree-command is a shell expression that writes Collins trees\n"
"      to stdout, and\n"
"  collins-score-command is a shell expression that writes Collins scores\n"
"      to stdout.\n"
"\n"
"The combined data file is written to stdout.\n"
"\n"
"Format of the output data:\n"
"=========================\n"
"\n"
"The first line of the data is:\n"
"\n"
"	<NSENTENCES>\n"
"\n"
"which is the number of sentences in the data.\n"
"\n"
"The rest of the data consists of a sequence of blocks, one per\n"
"sentence.  Each sentence block begins with a line of the form:\n"
"\n"
"	<NPARSES> <GOLDTREE>\n"
"\n"
"where:\n"
"\n"
"	<NPARSES> is the number of parses,\n"
"	<GOLDTREE> is the treebank tree.\n"
"\n"
"This is followed by <NPARSES> lines, one for each parse, of the form:\n"
"\n"
"	<LOGPROB> <PARSETREE>\n"
"\n"
"where:\n"
"\n"
"	<LOGPROB> is the log probability of the parse, and\n"
"	<PARSETREE> is the parse tree.\n"
"\n"
"The parses are not sorted.\n"
"\n";

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <utility>
#include <vector>

#include <boost/array.hpp>
#include <boost/lexical_cast.hpp>

#include <ext/hash_map>

#include "symset.h"
#include "tree.h"
#include "utility.h"

typedef ext::hash_map<symbol,size_t> S_C;
typedef std::vector<symbol> Ss;

// tree_terminals() appends the non-empty non-punctuation terminals to terms
//
void tree_terminals(const tree* t, Ss& terms)
{
  if (t->child == NULL)
    terms.push_back(t->label.cat);
  else { // child != NULL
    if (!t->label.is_punctuation() && !t->label.is_none())
      tree_terminals(t->child, terms);
  }
  if (t->next != NULL)
    tree_terminals(t->next, terms);
}  // tree_terminals()

// tree_preterminals() appends the non-empty terminals to terms
//
void tree_preterminals(const tree* t, Ss& preterms)
{
  if (t->is_preterminal()) {
    if (!t->label.is_punctuation() && !t->label.is_none())
      preterms.push_back(t->label.cat);
  }
  else if (t->is_nonterminal())
    tree_preterminals(t->child, preterms);
  if (t->next != NULL)
    tree_preterminals(t->next, preterms);
}  // tree_preterminals()

struct treeinfo {
  tree*  t;
  symbol filename;
  size_t treeno;
  template <typename S>
  treeinfo(tree* t, const S& filename, size_t treeno)
    : t(t), filename(filename), treeno(treeno) { }
}; // treeinfo

typedef ext::hash_multimap<Ss,treeinfo> Ss_TIs;
typedef Ss_TIs::const_iterator STIit;
typedef std::pair<STIit,STIit> STIrange;

typedef std::vector<treeinfo*> TIPtrs;

struct penn {
  Ss_TIs yield_trees;
  TIPtrs treeno_treeinfo;

  size_t treeno;

  penn() : treeno() { treeno_treeinfo.reserve(40000); }

  void operator()(const tree* tp0) {
    tree* tp = tp0->copy_without_empties();
    Ss yield;
    tree_terminals(tp, yield);
    const char* fn = rindex(readtree_filename, '/') + 1;
    if (fn == NULL)
      fn = readtree_filename;
    STIit it = yield_trees.insert(Ss_TIs::value_type(yield, treeinfo(tp,fn,treeno++)));
    treeno_treeinfo.push_back(&it->second);
    assert(treeno == treeno_treeinfo.size());
  }  // penn::operator()

};  // penn

struct collins_info {
  double logprob;
  tree*  t;
};

typedef ext::hash_map<std::string, collins_info> S_CI;

// npostagcorrect() returns the number of POS tags in the two sentences
// that are the same.
//
size_t npostagcorrect(const tree* t1, const tree* t2) {
  Ss pt1s, pt2s;
  tree_preterminals(t1, pt1s);
  tree_preterminals(t2, pt2s);
  if (pt1s.size() != pt2s.size())
    std::cerr << "## Error: pt1s.size() = " << pt1s.size()
	      << ", pt2s.size() = " << pt2s.size() << '\n'
	      << "## t1 = " << t1 << '\n'
	      << "## t2 = " << t2 << std::endl;
  size_t ncorrect = 0;
  for (size_t i = 0; i < pt1s.size(); ++i)
    if (pt1s[i] == pt2s[i])
      ++ncorrect;
  return ncorrect;
}  // npostagcorrect()

// write_collins_trees() returns the tree
size_t write_collins_trees(const penn& p, S_CI& parse_stats, FILE* of,
			   int& treeno, bool writeflag = false)
{
  assert(!parse_stats.empty());
  treeinfo* gold_tip = NULL;

  if (treeno < 0) {
    // first tree; look it up
    Ss yield;
    tree* parse = readtree_root(parse_stats.begin()->first.c_str());
    parse->terminals(yield);
    STIrange r = p.yield_trees.equal_range(yield); // r is the set of gold trees
    if (r.first == r.second) {
      std::cerr << "## Error: Can't find first parse's yield " << yield
		<< "\n## parse = " << best_parse << std::endl;
      exit(EXIT_FAILURE);
    }
    else if (r.second - r.first != 1) {
      std::cerr << "## Error: First parse's yield matches " << r.second - r.first << " Penn trees\n"
		<< "## yield = " << yield << std::endl;
      exit(EXIT_FAILURE);
    }
    else {
      // exactly one gold tree with this yield -- great!!
      gold_tip = &r.first->second;
      treeno = tip->treeno;
    }
  }
  else {
    // not first tree; look up treeinfo based on treeno
    ++treeno;
    assert(treeno < p.treeno_treeinfo.size());
    gold_tip = p.treeno_treeinfo[treeno];
  }

  if (writeflag)
    std::cerr << "# " << gold_tip->filename << ':' << gold_tip->treeno << std::endl;

  // look up yield in p
  Ss gold_yield;
  gold_tip->t->terminals(gold_yield);

  size_t nparses = 0;

  cforeach (S_CI, it, parse_stats) {
    const tree* parse = readtree_root(it->first.c_str());
    if (parse == NULL) {
      std::cerr << "## Couldn't read parse tree from string " << it->first
		<< "\n## " << gold_tip->filename << ':' << gold_tip->treeno
		<< "\n## " << gold_yield << std::endl;
      it->second.t = NULL;
    }
    else {
      Ss yield;
      parse->terminals(yield);
      if (yield != gold_yield) {
	std::cerr << "## Parse's yield differs from gold tree's yield "
		  << gold_tip->filename << ':' << gold_tip->treeno
		  << "\n## parse yield " << yield
		  << "\n## gold yield " << gold_yield
		  << std::endl;
	delete parse;
	it->second.t = NULL;
      }
      else {
	// Got a matching parse
	++nparses;
	it->second.t = parse;
      }
    }
  }

  // write out gold parse info

  fprintf(of, "%d %s\n", nparses,
	  boost::lexical_cast<std::string>(gold_tip->t).c_str());

  cforeach (S_CI, it, parse_stats)
    if (it->second.t != NULL) {
      fprintf(of, "%g %s\n", it->second.logprob,
	      boost::lexical_cast<std::string>(it->second.t).c_str());
      delete it->second.t;
    }
}  // write_collins_trees()

int transform_collins(const penn& p, const char tree_command[], FILE* of)
{
  FILE* tp = popen(tree_command, "r");
  assert(tp != NULL);

  S_CI parse_stats;

  int old_parse_id = -1;
  int lineno = 0;
  int nsentences = 0;
  int treeno = -1;  // indicates no tree found yet

  while(true) {
    int parse_id, rank, edge, score_id, parsenum, score_rank, best;
    double logprob, score, normscore, score_logprob, condprob;
    int nread_parse = fscanf(tp, " ID %d PROB %lf RANK %d EDGE %d ",
			     &parse_id, &logprob, &rank, &edge);
    ++lineno;
    if (nread_parse == EOF) {
      ++nsentences;
      write_collins_trees(p, parse_stats, of, treeno, true);
      std::cerr << "# Oracle " << pr << std::endl;
      break;
    }
    assert(nread_parse == 4);

    if (old_parse_id != parse_id) {
      if (old_parse_id != -1) {
	++nsentences;
	write_collins_trees(p, parse_stats, of, treeno, nsentences==1);
	parse_stats.clear();
      }
      old_parse_id = parse_id;
    }

    {
      char cparse[1024];
      char* cp = fgets(cparse, 1024, tp);
      assert(cp != NULL);
      assert(cparse[0] != '\0');
      for (int i = strlen(cparse)-1; i >= 0 && isspace(cparse[i]); --i)
	cparse[i] = '\0';  // replace final space with '\0'
      assert(strncmp(cparse, "(TOP ", 5) == 0);
      cparse[4] = '(';
      std::string parse(cparse+4);
      if (parse_stats.count(parse) == 0
	  || parse_stats[parse].logprob < logprob) {
	collins_info& ci = parse_stats[parse];
	ci.logprob = logprob;
      }
    }
  }
  pclose(tp);
  pclose(sp);
  return nsentences;
}  // transform_collins()

int main(int argc, char* argv[])
{
  std::ios::sync_with_stdio(false);

  if (argc != 4) {
    std::cerr << usage << std::endl;
    exit(EXIT_FAILURE);
  }

  penn p;
  map_regex_trees(argv[1], p);

  std::cerr << "# Read " << p.yield_trees.size()
	    << " penn trees." << std::endl;

  char* endptr = NULL;
  int nsentences0 = strtol(argv[2], &endptr, 10);
  if (endptr == NULL || *endptr != '\0') {
    std::cerr << "## Error: expected an integer as second argument, but got "
	      << argv[2] << std::endl;
    exit(EXIT_FAILURE);
  }

  fprintf(stdout, "%d\n", nsentences0);
  int nsentences = transform_collins(p, argv[3], stdout);
  if (nsentences != nsentences0) {
    std::cerr << "## Error: number of sentences from second argument (" << nsentences0
	      << ") is incorrect.\n## The correct number of sentences is " << nsentences
	      << ".\n## Please rerun " << argv[0] << " with second argument = "
	      << nsentences << std::endl;
    exit(EXIT_FAILURE);
  }

}

max_stars_repo_name: AmmarQaseem/CPI-Pipeline-test

max_stars_repo_licenses: PostgreSQL ISC Intel

max_issues_repo_name: AmmarQaseem/CPI-Pipeline-test

max_issues_repo_licenses: PostgreSQL ISC Intel

max_forks_repo_name: AmmarQaseem/CPI-Pipeline-test

max_forks_repo_licenses: PostgreSQL ISC Intel


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

#ifndef DATA_RUNNING__DATA_RUNNER_BASE_HPP_
#define DATA_RUNNING__DATA_RUNNER_BASE_HPP_

#include <boost/core/noncopyable.hpp>
#include <string>

#ifdef PERFORMANCE_TEST_MEMORYTOOLS_ENABLED
#include <osrf_testing_tools_cpp/memory_tools/memory_tools.hpp>
#include <osrf_testing_tools_cpp/scope_exit.hpp>
#endif

#include "../utilities/statistics_tracker.hpp"
#include "../experiment_configuration/experiment_configuration.hpp"

namespace performance_test
{

/// Indicates what functionality an object should exhibit.
enum class RunType
{
  /// Indicates to sending/publish data.
  PUBLISHER,
  /// Indicates to receive/subscribe to data.
  SUBSCRIBER
};

/// Interface for generic object which effectively executes the experiment and collects the
/// experiment results.
class DataRunnerBase : boost::noncopyable
{
public:
  DataRunnerBase()
  : m_ec(ExperimentConfiguration::get())
  {
    if (m_ec.check_memory()) {
#ifdef PERFORMANCE_TEST_MEMORYTOOLS_ENABLED
      osrf_testing_tools_cpp::memory_tools::initialize();
      osrf_testing_tools_cpp::memory_tools::enable_monitoring_in_all_threads();
      assert_memory_tools_is_working();
      const auto on_unexpected_memory =
        [](osrf_testing_tools_cpp::memory_tools::MemoryToolsService & service) {
          // this will cause a backtrace to be printed for each unexpected memory operations
          service.print_backtrace();
        };
      osrf_testing_tools_cpp::memory_tools::on_unexpected_calloc(on_unexpected_memory);
      osrf_testing_tools_cpp::memory_tools::on_unexpected_free(on_unexpected_memory);
      osrf_testing_tools_cpp::memory_tools::on_unexpected_malloc(on_unexpected_memory);
      osrf_testing_tools_cpp::memory_tools::on_unexpected_realloc(on_unexpected_memory);

#else
      throw std::runtime_error(
              "OSRF memory tools is not installed. Memory check must be disabled.");
#endif
    }
  }
  virtual ~DataRunnerBase() = default;
  /// Sum of the received samples per second.
  virtual uint64_t sum_received_samples() const = 0;
  /// Sum of the lost samples per second.
  virtual uint64_t sum_lost_samples() const = 0;
  /// Sum of the data received in bytes per second.
  virtual std::size_t sum_data_received() const = 0;
  /// Sum of the samples sent per second.
  virtual uint64_t sum_sent_samples() const = 0;

  /// Statistics about the latency of received samples.
  virtual StatisticsTracker latency_statistics() const = 0;
  /// Statistics about how much time every loop iteration had left over.
  virtual StatisticsTracker loop_time_reserve_statistics() const = 0;

  /// Resets all the stored metrics and replaces them with current ones from the running threads.
  virtual void sync_reset() = 0;

protected:
  /// A reference to the experiment configuration.
  const ExperimentConfiguration & m_ec;

  void malloc_test_function(const std::string & str)
  {
    void * some_memory = std::malloc(1024);
    // We need to do something with the malloc'ed memory to make sure this
    // function doesn't get optimized away.  memset isn't enough, so we do a
    // memcpy from a passed in string, which is enough to keep the optimizer away.
    // see: https://github.com/osrf/osrf_testing_tools_cpp/pull/8
    memcpy(some_memory, str.c_str(), str.length());
    std::free(some_memory);
  }

#ifdef PERFORMANCE_TEST_MEMORYTOOLS_ENABLED
  void assert_memory_tools_is_working()
  {
    bool saw_malloc = false;
    auto on_malloc_cb = [&saw_malloc]() {
        saw_malloc = true;
      };
    osrf_testing_tools_cpp::memory_tools::on_malloc(on_malloc_cb);
    OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(osrf_testing_tools_cpp::memory_tools::on_malloc(nullptr));
    {
      const std::string test_str = "test message";
      malloc_test_function(test_str);
    }
    if (!saw_malloc) {
      throw std::runtime_error(
              "Memory checking does not work properly. Please consult the documentation on how to "
              "properly set it up.");
    }
  }
#endif
};

}  // namespace performance_test
#endif  // DATA_RUNNING__DATA_RUNNER_BASE_HPP_

ext: hpp


text:
/*
 * This file is part of the DUDS project. It is subject to the BSD-style
 * license terms in the LICENSE file found in the top-level directory of this
 * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
 * No part of DUDS, including this file, may be copied, modified, propagated,
 * or distributed except according to the terms contained in the LICENSE file.
 *
 * Copyright (C) 2017  Jeff Jackowski
 */
/**
 * @file
 * Serialization support for Interstellar Time data types.
 */
#include <duds/time/interstellar/Interstellar.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/nvp.hpp>

namespace boost { namespace serialization {

/**
 * Makes serialization functions. I tried with just templates, but it didn't
 * go well, so I used macros to avoid the compiler errors I was getting.
 */
#define TIMESER(IST, INT, GET, SETOP, FLD) \
template <class Archive> \
void save(Archive &ar, const duds::time::interstellar::IST &t, const unsigned int) { \
	INT val(t.GET()); \
	ar & boost::serialization::make_nvp(#FLD, val); \
} \
template <class Archive> \
void load(Archive &ar, duds::time::interstellar::IST &t, const unsigned int) { \
	INT val; \
	ar & boost::serialization::make_nvp(#FLD, val); \
	t = duds::time::interstellar::IST(SET ## SETOP(val)); \
} \
template <class Archive> \
void serialize(Archive &a, duds::time::interstellar::IST &t, const unsigned int v) { \
	split_free(a, t, v); \
}

#define SETDUR(val)  val.value
#define SETPTR(val)  duds::time::interstellar::Femtoseconds(val.value)
#define SETSELF(val) val
#define SETMILLI(val) duds::time::interstellar::Milliseconds(val)
#define SETNANO(val) duds::time::interstellar::Nanoseconds(val)
#define SETSEC(val) duds::time::interstellar::Seconds(val)

TIMESER(Femtoseconds, duds::data::int128_w, count, DUR, duration)
TIMESER(Milliseconds, std::int64_t, count, SELF, duration)
TIMESER(Nanoseconds, std::uint64_t, count, SELF, duration)
TIMESER(Seconds, std::int64_t, count, SELF, duration)

TIMESER(FemtoTime, duds::data::int128_w, time_since_epoch().count, PTR, time)
TIMESER(MilliTime, std::int64_t, time_since_epoch().count, MILLI, time)
TIMESER(NanoTime, std::uint64_t, time_since_epoch().count, NANO, time)
TIMESER(SecondTime, std::int64_t, time_since_epoch().count, SEC, time)

#undef TIMESER
#undef SETDUR
#undef SETPTR
#undef SETSELF
#undef SETMILLI
#undef SETNANO
#undef SETSEC

} }

max_stars_repo_name: jjackowski/duds

max_issues_repo_name: jjackowski/duds

max_forks_repo_name: jjackowski/duds


text:
#include <iostream>
#include <fstream>
#include <string>
#include <boost/tokenizer.hpp>
#include <Eigen/Dense>

#include <omplapp/config.h>
#include <omplapp/apps/SE3RigidBodyPlanning.h>
#include <ompl/base/samplers/UniformValidStateSampler.h>
#include <ompl/tools/benchmark/Benchmark.h>

#include <ompl/geometric/planners/GSE/GSE.h>
#include <ompl/geometric/planners/est/EST.h>
#include <ompl/geometric/planners/kpiece/BKPIECE1.h>
#include <ompl/geometric/planners/kpiece/KPIECE1.h>
#include <ompl/geometric/planners/kpiece/LBKPIECE1.h>
#include <ompl/geometric/planners/prm/PRM.h>
#include <ompl/geometric/planners/rrt/RRT.h>
#include <ompl/geometric/planners/informedtrees/BITstar.h>
#include <ompl/geometric/planners/rrt/RRTstar.h>
#include <ompl/geometric/planners/rrt/RRTConnect.h>
#include <ompl/geometric/planners/sbl/SBL.h>

using namespace ompl;

base::PlannerPtr myConfiguredGSE(const base::SpaceInformationPtr &si);
void genSphere(Eigen::MatrixXd &sphere);

int main(int,char**){

    app::SE3RigidBodyPlanning setup;
    // load the robot and the environment
    std::string robot_fname = std::string(OMPLAPP_RESOURCE_DIR) + "/GSE/point.dae";
    std::string env_fname = std::string(OMPLAPP_RESOURCE_DIR) + "/GSE/lattice.dae";
    setup.setRobotMesh(robot_fname);
    setup.setEnvironmentMesh(env_fname);

    // define start state
    base::ScopedState<base::SE3StateSpace> start(setup.getSpaceInformation());
    start->setX(-2);
    start->setY(-2);
    start->setZ(-2);
    start->rotation().setIdentity();

    // define goal state
    base::ScopedState<base::SE3StateSpace> goal(start);
    goal->setX(10);
    goal->setY(10);
    goal->setZ(10);
    goal->rotation().setIdentity();

    base::RealVectorBounds bounds(3);
    bounds.setHigh(0, 12.1);
    bounds.setHigh(1, 12.1);
    bounds.setHigh(2, 12.1);
    bounds.setLow(0, -2.1);
    bounds.setLow(1, -2.1);
    bounds.setLow(2, -2.1);
    setup.getStateSpace()->as<base::SE3StateSpace>()->setBounds(bounds);

    // set the start & goal states
    setup.setStartAndGoalStates(start, goal);
    // setting collision checking resolution to 1% of the space extent
    setup.getSpaceInformation()->setStateValidityCheckingResolution(0.01);

    // run all planners with a uniform valid state sampler on the benchmark problem
    setup.getSpaceInformation()->setValidStateSamplerAllocator(
        [](const base::SpaceInformation *si) -> base::ValidStateSamplerPtr {
            return std::make_shared<base::UniformValidStateSampler>(si);
        });

    // set high because memory usage is not always estimated correctly
    double runtime_limit = 0.06, memory_limit = 100.0, run_count = 500;

    // create the benchmark object and add all the planners we'd like to run
    tools::Benchmark::Request request(runtime_limit, memory_limit, run_count);
    tools::Benchmark b(setup, "GSE Benchmark");

    b.addPlannerAllocator(std::bind(&myConfiguredGSE, std::placeholders::_1));
    b.addPlanner(std::make_shared<geometric::RRTConnect>(setup.getSpaceInformation()));
    b.addPlanner(std::make_shared<geometric::RRT>(setup.getSpaceInformation()));
    b.addPlanner(std::make_shared<geometric::BKPIECE1>(setup.getSpaceInformation()));
    b.addPlanner(std::make_shared<geometric::LBKPIECE1>(setup.getSpaceInformation()));
    b.addPlanner(std::make_shared<geometric::KPIECE1>(setup.getSpaceInformation()));
    b.addPlanner(std::make_shared<geometric::SBL>(setup.getSpaceInformation()));
    b.addPlanner(std::make_shared<geometric::EST>(setup.getSpaceInformation()));
    b.addPlanner(std::make_shared<geometric::PRM>(setup.getSpaceInformation()));

    b.benchmark(request);
    b.saveResultsToFile();

    return 0;
}

template<typename M>
M load_csv(const std::string path){
    std::ifstream fin(path.c_str());
    std::string line;
    std::vector<double> values;
    uint cols =0;
    while(getline(fin,line)){
        boost::tokenizer<boost::escaped_list_separator<char>> tok(line);
        for (auto str: tok)
            values.push_back(std::stod(str));
        ++cols;
    }
    return Eigen::Map<M>(values.data(),values.size()/cols,cols);
}

base::PlannerPtr myConfiguredGSE(const base::SpaceInformationPtr &si)
{
    geometric::GSE *planner = new geometric::GSE(si,3);

    Eigen::MatrixXd sphere(1000,3);
    genSphere(sphere);

    std::vector<std::vector<double>> obs{{0,0,0},
                                        {0,-2,0},
                                        {0,2,0},
                                        {-2,0,0},
                                        {2,0,0},
                                        {0,0,-2},
                                        {0,0,2},
                                        {1,1,1}};

    for(auto row:obs){
        Eigen::VectorXd centre = Eigen::Map<Eigen::VectorXd>(row.data(),row.size());
        Eigen::MatrixXd obsi = (0.5*sphere).colwise()+centre;
        planner->addObstacle(obsi);
    }

    si->setup();
    auto projector = si->getStateSpace()->getDefaultProjection();
    planner->setProjector(projector);

    return base::PlannerPtr(planner);
}

// generate Uniform randomn Points on a sphere of raidius 1
// http://corysimon.github.io/articles/uniformdistn-on-sphere/
void genSphere(Eigen::MatrixXd &sphere){
    int N = sphere.rows();
    auto theta = 2*M_PI*(Eigen::ArrayXd::Random(N)*0.5+0.5);
    auto phi = Eigen::ArrayXd::Random(N).acos();

    Eigen::VectorXd x = phi.sin()*theta.cos();
    Eigen::VectorXd y = phi.sin()*theta.sin();
    Eigen::VectorXd z = phi.cos();

    sphere<<x,y,z;
}