<commit_before>// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/foreach.hpp>

#include "checkpoints.h"

#include "main.h"
#include "uint256.h"

namespace Checkpoints
{
    typedef std::map<int, uint256> MapCheckpoints;

    // How many times we expect transactions after the last checkpoint to
    // be slower. This number is a compromise, as it can't be accurate for
    // every system. When reindexing from a fast disk with a slow CPU, it
    // can be up to 20, while when downloading from a slow network with a
    // fast multicore CPU, it won't be much higher than 1.
    static const double fSigcheckVerificationFactor = 5.0;

    struct CCheckpointData {
        const MapCheckpoints *mapCheckpoints;
        int64 nTimeLastCheckpoint;
        int64 nTransactionsLastCheckpoint;
        double fTransactionsPerDay;
    };

    // What makes a good checkpoint block?
    // + Is surrounded by blocks with reasonable timestamps
    //   (no blocks before with a timestamp after, none after with
    //    timestamp before)
    // + Contains no strange transactions
    static MapCheckpoints mapCheckpoints =
        boost::assign::map_list_of
        (      0, uint256("0x4d96a915f49d40b1e5c2844d1ee2dccb90013a990ccea12c492d22110489f0c4"))
        (  24200, uint256("0xd7ed819858011474c8b0cae4ad0b9bdbb745becc4c386bc22d1220cc5a4d1787"))
        ;
    static const CCheckpointData data = {
        &mapCheckpoints,
        1390701333, // * UNIX timestamp of last checkpoint block
        55013,    // * total number of transactions between genesis and last checkpoint
                    //   (the tx=... number in the SetBestChain debug.log lines)
        8000.0     // * estimated number of transactions per day after checkpoint
    };

    static MapCheckpoints mapCheckpointsTestnet =
        boost::assign::map_list_of
        (   0, uint256("0x"))
        ;
    static const CCheckpointData dataTestnet = {
        &mapCheckpointsTestnet,
        1389241455,
        0,
        300
    };

    const CCheckpointData &Checkpoints() {
        if (fTestNet)
            return dataTestnet;
        else
            return data;
    }

    bool CheckBlock(int nHeight, const uint256& hash)
    {
        if (fTestNet) return true; // Testnet has no checkpoints
        if (!GetBoolArg("-checkpoints", true))
            return true;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
        if (i == checkpoints.end()) return true;
        return hash == i->second;
    }

    // Guess how far we are in the verification process at the given block index
    double GuessVerificationProgress(CBlockIndex *pindex) {
        if (pindex==NULL)
            return 0.0;

        int64 nNow = time(NULL);

        double fWorkBefore = 0.0; // Amount of work done before pindex
        double fWorkAfter = 0.0;  // Amount of work left after pindex (estimated)
        // Work is defined as: 1.0 per transaction before the last checkoint, and
        // fSigcheckVerificationFactor per transaction after.

        const CCheckpointData &data = Checkpoints();

        if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
            double nCheapBefore = pindex->nChainTx;
            double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
            double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
            fWorkBefore = nCheapBefore;
            fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
        } else {
            double nCheapBefore = data.nTransactionsLastCheckpoint;
            double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
            double nExpensiveAfter = (nNow - pindex->nTime)/86400.0*data.fTransactionsPerDay;
            fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
            fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
        }

        return fWorkBefore / (fWorkBefore + fWorkAfter);
    }

    int GetTotalBlocksEstimate()
    {
        if (fTestNet) return 0; // Testnet has no checkpoints
        if (!GetBoolArg("-checkpoints", true))
            return 0;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        return checkpoints.rbegin()->first;
    }

    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
    {
        if (fTestNet) return NULL; // Testnet has no checkpoints
        if (!GetBoolArg("-checkpoints", true))
            return NULL;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
        {
            const uint256& hash = i.second;
            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
            if (t != mapBlockIndex.end())
                return t->second;
        }
        return NULL;
    }
}
<commit_msg>Checkpoint mainnet at block 65000<commit_after>// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/foreach.hpp>

#include "checkpoints.h"

#include "main.h"
#include "uint256.h"

namespace Checkpoints
{
    typedef std::map<int, uint256> MapCheckpoints;

    // How many times we expect transactions after the last checkpoint to
    // be slower. This number is a compromise, as it can't be accurate for
    // every system. When reindexing from a fast disk with a slow CPU, it
    // can be up to 20, while when downloading from a slow network with a
    // fast multicore CPU, it won't be much higher than 1.
    static const double fSigcheckVerificationFactor = 5.0;

    struct CCheckpointData {
        const MapCheckpoints *mapCheckpoints;
        int64 nTimeLastCheckpoint;
        int64 nTransactionsLastCheckpoint;
        double fTransactionsPerDay;
    };

    // What makes a good checkpoint block?
    // + Is surrounded by blocks with reasonable timestamps
    //   (no blocks before with a timestamp after, none after with
    //    timestamp before)
    // + Contains no strange transactions
    static MapCheckpoints mapCheckpoints =
        boost::assign::map_list_of
        (      0, uint256("0x4d96a915f49d40b1e5c2844d1ee2dccb90013a990ccea12c492d22110489f0c4"))
        (  24200, uint256("0xd7ed819858011474c8b0cae4ad0b9bdbb745becc4c386bc22d1220cc5a4d1787"))
        (  65000, uint256("0x9e673a69c35a423f736ab66f9a195d7c42f979847a729c0f3cef2c0b8b9d0289"))
        ;
    static const CCheckpointData data = {
        &mapCheckpoints,
        1396856613, // * UNIX timestamp of last checkpoint block
        511112,    // * total number of transactions between genesis and last checkpoint
                    //   (the tx=... number in the SetBestChain debug.log lines)
        8000.0     // * estimated number of transactions per day after checkpoint
    };

    static MapCheckpoints mapCheckpointsTestnet =
        boost::assign::map_list_of
        (   0, uint256("0x"))
        ;
    static const CCheckpointData dataTestnet = {
        &mapCheckpointsTestnet,
        1389241455,
        0,
        300
    };

    const CCheckpointData &Checkpoints() {
        if (fTestNet)
            return dataTestnet;
        else
            return data;
    }

    bool CheckBlock(int nHeight, const uint256& hash)
    {
        if (fTestNet) return true; // Testnet has no checkpoints
        if (!GetBoolArg("-checkpoints", true))
            return true;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
        if (i == checkpoints.end()) return true;
        return hash == i->second;
    }

    // Guess how far we are in the verification process at the given block index
    double GuessVerificationProgress(CBlockIndex *pindex) {
        if (pindex==NULL)
            return 0.0;

        int64 nNow = time(NULL);

        double fWorkBefore = 0.0; // Amount of work done before pindex
        double fWorkAfter = 0.0;  // Amount of work left after pindex (estimated)
        // Work is defined as: 1.0 per transaction before the last checkoint, and
        // fSigcheckVerificationFactor per transaction after.

        const CCheckpointData &data = Checkpoints();

        if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
            double nCheapBefore = pindex->nChainTx;
            double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
            double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
            fWorkBefore = nCheapBefore;
            fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
        } else {
            double nCheapBefore = data.nTransactionsLastCheckpoint;
            double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
            double nExpensiveAfter = (nNow - pindex->nTime)/86400.0*data.fTransactionsPerDay;
            fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
            fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
        }

        return fWorkBefore / (fWorkBefore + fWorkAfter);
    }

    int GetTotalBlocksEstimate()
    {
        if (fTestNet) return 0; // Testnet has no checkpoints
        if (!GetBoolArg("-checkpoints", true))
            return 0;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        return checkpoints.rbegin()->first;
    }

    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
    {
        if (fTestNet) return NULL; // Testnet has no checkpoints
        if (!GetBoolArg("-checkpoints", true))
            return NULL;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
        {
            const uint256& hash = i.second;
            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
            if (t != mapBlockIndex.end())
                return t->second;
        }
        return NULL;
    }
}
<|endoftext|>


<commit_before>// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/foreach.hpp>

#include "checkpoints.h"

#include "txdb.h"
#include "main.h"
#include "uint256.h"

static const int nCheckpointSpan = 500;

namespace Checkpoints
{
    typedef std::map<int, uint256> MapCheckpoints;

    //
    // What makes a good checkpoint block?
    // + Is surrounded by blocks with reasonable timestamps
    //   (no blocks before with a timestamp after, none after with
    //    timestamp before)
    // + Contains no strange transactions
    //
    static MapCheckpoints mapCheckpoints =
        boost::assign::map_list_of
        ( 0,   uint256("0x0000095667f3c1fdbf0b9b4937be57c6401162fcfe72be373df27393f0c69d93") ) // genesis
        ( 50000,   uint256("0x38de11fd0ac1ef41a3fab6d5faac3e597c509244ad028dba20a12832f9b38e92") )
        ( 100000,   uint256("0x4d32d90e958c1060a8346dbcd72b96459bcc9ded1241d51f7b09be3eaf405901") )
        ( 150000,   uint256("0x3efea69afbdfaab1a9e2d1240a014aa7c88c66cf246107febbe0d2db147e7350") )
        ( 200000,   uint256("0xf0bb302a774b841e03d3818f980ecb0c1354f7ff048ff2dc004c3003178c3d56") )
        ( 250000,   uint256("0xa7325d458200a636d20f3be182d2121acb725de486adf1f855cb72ea9203f56d") )
        ( 300000,   uint256("0x3c536e6c98d1eb54610a7133d671f560627d731aaef8a46db35335255217d2eb") )
        ( 330000,   uint256("0xbc45b022e71c2c83765009426a5a54db7e6fa4f94d3ee53e1418a54388529b95") )
        ( 360000,   uint256("0x103c1ee3cf53529a1bee0bfdff21a32bec361c668df6f7bfc3665d84cbad394d") )
        ( 423000,   uint256("0x00000000000000940d7a26bfaf12ac74bcefb22ff231560b805deac6728917fc") )
        ( 451000,   uint256("0x66e23ed10acad88fe2b81c3e31d510dd37ee351c217c5c1a2146bcab754e89cc") )
        ( 472737,   uint256("0x2b306a5c48b16fd7fd84017ef6a9df4da86bb9871bf8a015a74044d41ec84aaa") )
        ( 482666,   uint256("0x9291b42d4b06da001a471a306888c0a58329b812ed26509129e0231545d7a93a") )
        ( 499888,   uint256("0xe9c977ab38ce0d47dc222e078ad71a548c67425d563d79d8f25247eaf6457e1c") )
    ;

    // TestNet has no checkpoints
    static MapCheckpoints mapCheckpointsTestnet;

    bool CheckHardened(int nHeight, const uint256& hash)
    {
        MapCheckpoints& checkpoints = (TestNet() ? mapCheckpointsTestnet : mapCheckpoints);

        MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
        if (i == checkpoints.end()) return true;
        return hash == i->second;
    }

    int GetTotalBlocksEstimate()
    {
        MapCheckpoints& checkpoints = (TestNet() ? mapCheckpointsTestnet : mapCheckpoints);

        if (checkpoints.empty())
            return 0;
        return checkpoints.rbegin()->first;
    }

    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
    {
        MapCheckpoints& checkpoints = (TestNet() ? mapCheckpointsTestnet : mapCheckpoints);

        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
        {
            const uint256& hash = i.second;
            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
            if (t != mapBlockIndex.end())
                return t->second;
        }
        return NULL;
    }

    // Automatically select a suitable sync-checkpoint
    const CBlockIndex* AutoSelectSyncCheckpoint()
    {
        const CBlockIndex *pindex = pindexBest;
        // Search backward for a block within max span and maturity window
        while (pindex->pprev && pindex->nHeight + nCheckpointSpan > pindexBest->nHeight)
            pindex = pindex->pprev;
        return pindex;
    }

    // Check against synchronized checkpoint
    bool CheckSync(int nHeight)
    {
        const CBlockIndex* pindexSync = AutoSelectSyncCheckpoint();

        if (nHeight <= pindexSync->nHeight)
            return false;
        return true;
    }
}
<commit_msg>Add CP at 511111<commit_after>// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/foreach.hpp>

#include "checkpoints.h"

#include "txdb.h"
#include "main.h"
#include "uint256.h"

static const int nCheckpointSpan = 500;

namespace Checkpoints
{
    typedef std::map<int, uint256> MapCheckpoints;

    //
    // What makes a good checkpoint block?
    // + Is surrounded by blocks with reasonable timestamps
    //   (no blocks before with a timestamp after, none after with
    //    timestamp before)
    // + Contains no strange transactions
    //
    static MapCheckpoints mapCheckpoints =
        boost::assign::map_list_of
        ( 0,   uint256("0x0000095667f3c1fdbf0b9b4937be57c6401162fcfe72be373df27393f0c69d93") ) // genesis
        ( 50000,   uint256("0x38de11fd0ac1ef41a3fab6d5faac3e597c509244ad028dba20a12832f9b38e92") )
        ( 100000,   uint256("0x4d32d90e958c1060a8346dbcd72b96459bcc9ded1241d51f7b09be3eaf405901") )
        ( 150000,   uint256("0x3efea69afbdfaab1a9e2d1240a014aa7c88c66cf246107febbe0d2db147e7350") )
        ( 200000,   uint256("0xf0bb302a774b841e03d3818f980ecb0c1354f7ff048ff2dc004c3003178c3d56") )
        ( 250000,   uint256("0xa7325d458200a636d20f3be182d2121acb725de486adf1f855cb72ea9203f56d") )
        ( 300000,   uint256("0x3c536e6c98d1eb54610a7133d671f560627d731aaef8a46db35335255217d2eb") )
        ( 330000,   uint256("0xbc45b022e71c2c83765009426a5a54db7e6fa4f94d3ee53e1418a54388529b95") )
        ( 360000,   uint256("0x103c1ee3cf53529a1bee0bfdff21a32bec361c668df6f7bfc3665d84cbad394d") )
        ( 423000,   uint256("0x00000000000000940d7a26bfaf12ac74bcefb22ff231560b805deac6728917fc") )
        ( 451000,   uint256("0x66e23ed10acad88fe2b81c3e31d510dd37ee351c217c5c1a2146bcab754e89cc") )
        ( 472737,   uint256("0x2b306a5c48b16fd7fd84017ef6a9df4da86bb9871bf8a015a74044d41ec84aaa") )
        ( 482666,   uint256("0x9291b42d4b06da001a471a306888c0a58329b812ed26509129e0231545d7a93a") )
        ( 499888,   uint256("0xe9c977ab38ce0d47dc222e078ad71a548c67425d563d79d8f25247eaf6457e1c") )
        ( 511111,   uint256("0xf2e9623e0efe8fe23687126cb5cb7588584407e89121fa238d7729af38366de7") )
    ;

    // TestNet has no checkpoints
    static MapCheckpoints mapCheckpointsTestnet;

    bool CheckHardened(int nHeight, const uint256& hash)
    {
        MapCheckpoints& checkpoints = (TestNet() ? mapCheckpointsTestnet : mapCheckpoints);

        MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
        if (i == checkpoints.end()) return true;
        return hash == i->second;
    }

    int GetTotalBlocksEstimate()
    {
        MapCheckpoints& checkpoints = (TestNet() ? mapCheckpointsTestnet : mapCheckpoints);

        if (checkpoints.empty())
            return 0;
        return checkpoints.rbegin()->first;
    }

    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
    {
        MapCheckpoints& checkpoints = (TestNet() ? mapCheckpointsTestnet : mapCheckpoints);

        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
        {
            const uint256& hash = i.second;
            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
            if (t != mapBlockIndex.end())
                return t->second;
        }
        return NULL;
    }

    // Automatically select a suitable sync-checkpoint
    const CBlockIndex* AutoSelectSyncCheckpoint()
    {
        const CBlockIndex *pindex = pindexBest;
        // Search backward for a block within max span and maturity window
        while (pindex->pprev && pindex->nHeight + nCheckpointSpan > pindexBest->nHeight)
            pindex = pindex->pprev;
        return pindex;
    }

    // Check against synchronized checkpoint
    bool CheckSync(int nHeight)
    {
        const CBlockIndex* pindexSync = AutoSelectSyncCheckpoint();

        if (nHeight <= pindexSync->nHeight)
            return false;
        return true;
    }
}
<|endoftext|>


<commit_before>// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/foreach.hpp>

#include "checkpoints.h"

#include "main.h"
#include "uint256.h"

namespace Checkpoints
{
    typedef std::map<int, uint256> MapCheckpoints;

    // How many times we expect transactions after the last checkpoint to
    // be slower. This number is a compromise, as it can't be accurate for
    // every system. When reindexing from a fast disk with a slow CPU, it
    // can be up to 20, while when downloading from a slow network with a
    // fast multicore CPU, it won't be much higher than 1.
    static const double fSigcheckVerificationFactor = 5.0;

    struct CCheckpointData {
        const MapCheckpoints *mapCheckpoints;
        int64 nTimeLastCheckpoint;
        int64 nTransactionsLastCheckpoint;
        double fTransactionsPerDay;
    };

    // What makes a good checkpoint block?
    // + Is surrounded by blocks with reasonable timestamps
    //   (no blocks before with a timestamp after, none after with
    //    timestamp before)
    // + Contains no strange transactions
    static MapCheckpoints mapCheckpoints =
        boost::assign::map_list_of
        (     1, uint256("0xe613b5a195556386f422875d8df68e3e154f37b4981dec641fe041211672870f"))
        (  2016, uint256("0x94a9ab39b9e1e7dafdda1a5823c11556750157aa16e93ae2a59de426e185de58"))
        (  4032, uint256("0xa29010ca0ec82b0d2d937f5d82d2e30918467b1f4b3bb51026d13782b7aac73a"))
        (  8064, uint256("0x76ef2061b587842f424aae28fcab25fb8434612b58125258292799aaaed0f833"))
        ( 12096, uint256("0x5abe39584788a018296d97a6f00ca80c81603ebbe8ad84af5778e7d6fcdbed70"))
        ( 13422, uint256("0xc70a40fe1c53e5525636644e625d4c38c8493b180a0ca81c9d7c9801748e6b52"))
        ( 14496, uint256("0xd50a2656a6ac593ebb034c1d5ca5eac93220455727f677e7506629f61d566239"))
        ;
    static const CCheckpointData data = {
        &mapCheckpoints,
        1398890889, // * UNIX timestamp of last checkpoint block
        16135,   // * total number of transactions between genesis and last checkpoint
                    //   (the tx=... number in the SetBestChain debug.log lines)
        600.0     // * estimated number of transactions per day after checkpoint
    };

    static MapCheckpoints mapCheckpointsTestnet =
        boost::assign::map_list_of
        ( 0, uint256("0x0d44ae9be7f13a64253c9b61dbbddc37304e1c359fd593565caf356e4fd597c7"))
        ;
    static const CCheckpointData dataTestnet = {
        &mapCheckpointsTestnet,
        1393990003,
        0,
        300
    };

    const CCheckpointData &Checkpoints() {
        if (fTestNet)
            return dataTestnet;
        else
            return data;
    }

    bool CheckBlock(int nHeight, const uint256& hash)
    {
        if (!GetBoolArg("-checkpoints", true))
            return true;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
        if (i == checkpoints.end()) return true;
        return hash == i->second;
    }

    // Guess how far we are in the verification process at the given block index
    double GuessVerificationProgress(CBlockIndex *pindex) {
        if (pindex==NULL)
            return 0.0;

        int64 nNow = time(NULL);

        double fWorkBefore = 0.0; // Amount of work done before pindex
        double fWorkAfter = 0.0;  // Amount of work left after pindex (estimated)
        // Work is defined as: 1.0 per transaction before the last checkoint, and
        // fSigcheckVerificationFactor per transaction after.

        const CCheckpointData &data = Checkpoints();

        if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
            double nCheapBefore = pindex->nChainTx;
            double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
            double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
            fWorkBefore = nCheapBefore;
            fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
        } else {
            double nCheapBefore = data.nTransactionsLastCheckpoint;
            double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
            double nExpensiveAfter = (nNow - pindex->nTime)/86400.0*data.fTransactionsPerDay;
            fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
            fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
        }

        return fWorkBefore / (fWorkBefore + fWorkAfter);
    }

    int GetTotalBlocksEstimate()
    {
        if (!GetBoolArg("-checkpoints", true))
            return 0;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        return checkpoints.rbegin()->first;
    }

    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
    {
        if (!GetBoolArg("-checkpoints", true))
            return NULL;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
        {
            const uint256& hash = i.second;
            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
            if (t != mapBlockIndex.end())
                return t->second;
        }
        return NULL;
    }
}
<commit_msg>Checkpoint @ Block 15809<commit_after>// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/foreach.hpp>

#include "checkpoints.h"

#include "main.h"
#include "uint256.h"

namespace Checkpoints
{
    typedef std::map<int, uint256> MapCheckpoints;

    // How many times we expect transactions after the last checkpoint to
    // be slower. This number is a compromise, as it can't be accurate for
    // every system. When reindexing from a fast disk with a slow CPU, it
    // can be up to 20, while when downloading from a slow network with a
    // fast multicore CPU, it won't be much higher than 1.
    static const double fSigcheckVerificationFactor = 5.0;

    struct CCheckpointData {
        const MapCheckpoints *mapCheckpoints;
        int64 nTimeLastCheckpoint;
        int64 nTransactionsLastCheckpoint;
        double fTransactionsPerDay;
    };

    // What makes a good checkpoint block?
    // + Is surrounded by blocks with reasonable timestamps
    //   (no blocks before with a timestamp after, none after with
    //    timestamp before)
    // + Contains no strange transactions
    static MapCheckpoints mapCheckpoints =
        boost::assign::map_list_of
        (     1, uint256("0xe613b5a195556386f422875d8df68e3e154f37b4981dec641fe041211672870f"))
        (  2016, uint256("0x94a9ab39b9e1e7dafdda1a5823c11556750157aa16e93ae2a59de426e185de58"))
        (  4032, uint256("0xa29010ca0ec82b0d2d937f5d82d2e30918467b1f4b3bb51026d13782b7aac73a"))
        (  8064, uint256("0x76ef2061b587842f424aae28fcab25fb8434612b58125258292799aaaed0f833"))
        ( 12096, uint256("0x5abe39584788a018296d97a6f00ca80c81603ebbe8ad84af5778e7d6fcdbed70"))
        ( 13422, uint256("0xc70a40fe1c53e5525636644e625d4c38c8493b180a0ca81c9d7c9801748e6b52"))
        ( 14496, uint256("0xd50a2656a6ac593ebb034c1d5ca5eac93220455727f677e7506629f61d566239"))
        ( 15809, uint256("0x31cdc96eb5510e69bf2635b76b0266c0065839685bb30d9b94224d9a5c972b78"))
        ;
    static const CCheckpointData data = {
        &mapCheckpoints,
        1399736336, // * UNIX timestamp of last checkpoint block
        18226,   // * total number of transactions between genesis and last checkpoint
                    //   (the tx=... number in the SetBestChain debug.log lines)
        600.0     // * estimated number of transactions per day after checkpoint
    };

    static MapCheckpoints mapCheckpointsTestnet =
        boost::assign::map_list_of
        ( 0, uint256("0x0d44ae9be7f13a64253c9b61dbbddc37304e1c359fd593565caf356e4fd597c7"))
        ;
    static const CCheckpointData dataTestnet = {
        &mapCheckpointsTestnet,
        1393990003,
        0,
        300
    };

    const CCheckpointData &Checkpoints() {
        if (fTestNet)
            return dataTestnet;
        else
            return data;
    }

    bool CheckBlock(int nHeight, const uint256& hash)
    {
        if (!GetBoolArg("-checkpoints", true))
            return true;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
        if (i == checkpoints.end()) return true;
        return hash == i->second;
    }

    // Guess how far we are in the verification process at the given block index
    double GuessVerificationProgress(CBlockIndex *pindex) {
        if (pindex==NULL)
            return 0.0;

        int64 nNow = time(NULL);

        double fWorkBefore = 0.0; // Amount of work done before pindex
        double fWorkAfter = 0.0;  // Amount of work left after pindex (estimated)
        // Work is defined as: 1.0 per transaction before the last checkoint, and
        // fSigcheckVerificationFactor per transaction after.

        const CCheckpointData &data = Checkpoints();

        if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
            double nCheapBefore = pindex->nChainTx;
            double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
            double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
            fWorkBefore = nCheapBefore;
            fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
        } else {
            double nCheapBefore = data.nTransactionsLastCheckpoint;
            double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
            double nExpensiveAfter = (nNow - pindex->nTime)/86400.0*data.fTransactionsPerDay;
            fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
            fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
        }

        return fWorkBefore / (fWorkBefore + fWorkAfter);
    }

    int GetTotalBlocksEstimate()
    {
        if (!GetBoolArg("-checkpoints", true))
            return 0;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        return checkpoints.rbegin()->first;
    }

    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
    {
        if (!GetBoolArg("-checkpoints", true))
            return NULL;

        const MapCheckpoints& checkpoints = *Checkpoints().mapCheckpoints;

        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
        {
            const uint256& hash = i.second;
            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
            if (t != mapBlockIndex.end())
                return t->second;
        }
        return NULL;
    }
}
<|endoftext|>


<commit_before>// Copyright (c) 2009-2012 The Bitcoin developers
// Copyright (c) 2011-2012 Litecoin Developers
// Copyright (c) 2014 BlitzCoin Developers

// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/foreach.hpp>

#include "checkpoints.h"

#include "main.h"
#include "uint256.h"

namespace Checkpoints
{
    typedef std::map<int, uint256> MapCheckpoints;

    //
    // What makes a good checkpoint block?
    // + Is surrounded by blocks with reasonable timestamps
    //   (no blocks before with a timestamp after, none after with
    //    timestamp before)
    // + Contains no strange transactions
    //
    static MapCheckpoints mapCheckpoints =
            boost::assign::map_list_of
            (     0, uint256("0xafaf057ef023cb6799f554c9faef896d20eda2594b8ee02c32f165486d4751ec"))
            (   100, uint256("0xa0d7b106ae20ef57386998369826bfd87ea1d1c8af53984820f4e7c85a1c6f1c"))
            (   150, uint256("0xbe14b2084e69121890fd2dea6a1e6644e7b0632471436adaac0fd9200bd43dbc"))
            (   303, uint256("0x879902ffd867f4843bbba2c25ce43e32cba896b856c620afc5391ea6a4a9da79"))
            (   500, uint256("0xeff58a380b74b9d5b0ecd1090fbc4732a7fac7ede4b078f23e1d7cf0aba6f84e"))
            (   700, uint256("0x53e0f3005e6429ab162fcf73b398dda0afe71eb5e5bb8a9dd0d9ab6a47b5b318"))
            (   999, uint256("0xedb846f5fd67278b47b160cf00a4f4a1d8a5ee83a4b01fccf43bbfc37a09ebfa"))
            (  1500, uint256("0xed3a473ad4b9cdc66f345af53d130e3b5f0cd824fb129b713158001d76d87c1d"))
            (  1901, uint256("0xeedee54336e3d372d2d17efa02fce71c10a49d0172daa781259b5b9d919fa380"))
            (  2203, uint256("0xfca331d8f613bdcb924570b8f993cfee409e1f78ec2f2afd8347b33efad2d33a"))
            (  2701, uint256("0x85618f98239b0f120914c17f7f37795aa32887e9b17e026f0c00323e05a77b06"))
            (  3000, uint256("0xef6c66058e32c8d0d23161ef301988c04e9298157311b64c6fea1fe45e9b4bed"))
            (  3150, uint256("0x22cedee94491c18bbdcb0a4da50cdcb0692c46c3bfccfe8a9d7b543e43c2337d"))
            ;

    bool CheckBlock(int nHeight, const uint256& hash)
    {
        if (fTestNet) return true; // Testnet has no checkpoints

        MapCheckpoints::const_iterator i = mapCheckpoints.find(nHeight);
        if (i == mapCheckpoints.end()) return true;
        return hash == i->second;
    }

    int GetTotalBlocksEstimate()
    {
        if (fTestNet) return 0;
        return mapCheckpoints.rbegin()->first;
    }

    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
    {
        if (fTestNet) return NULL;

        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
        {
            const uint256& hash = i.second;
            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
            if (t != mapBlockIndex.end())
                return t->second;
        }
        return NULL;
    }
}
<commit_msg>adding checkpoints<commit_after>// Copyright (c) 2009-2012 The Bitcoin developers
// Copyright (c) 2011-2012 Litecoin Developers
// Copyright (c) 2014 BlitzCoin Developers

// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/foreach.hpp>

#include "checkpoints.h"

#include "main.h"
#include "uint256.h"

namespace Checkpoints
{
    typedef std::map<int, uint256> MapCheckpoints;

    //
    // What makes a good checkpoint block?
    // + Is surrounded by blocks with reasonable timestamps
    //   (no blocks before with a timestamp after, none after with
    //    timestamp before)
    // + Contains no strange transactions
    //
    static MapCheckpoints mapCheckpoints =
            boost::assign::map_list_of
            (     0, uint256("0xafaf057ef023cb6799f554c9faef896d20eda2594b8ee02c32f165486d4751ec"))
            (   100, uint256("0xa0d7b106ae20ef57386998369826bfd87ea1d1c8af53984820f4e7c85a1c6f1c"))
            (   150, uint256("0xbe14b2084e69121890fd2dea6a1e6644e7b0632471436adaac0fd9200bd43dbc"))
            (   303, uint256("0x879902ffd867f4843bbba2c25ce43e32cba896b856c620afc5391ea6a4a9da79"))
            (   500, uint256("0xeff58a380b74b9d5b0ecd1090fbc4732a7fac7ede4b078f23e1d7cf0aba6f84e"))
            (   700, uint256("0x53e0f3005e6429ab162fcf73b398dda0afe71eb5e5bb8a9dd0d9ab6a47b5b318"))
            (   999, uint256("0xedb846f5fd67278b47b160cf00a4f4a1d8a5ee83a4b01fccf43bbfc37a09ebfa"))
            (  1500, uint256("0xed3a473ad4b9cdc66f345af53d130e3b5f0cd824fb129b713158001d76d87c1d"))
            (  1901, uint256("0xeedee54336e3d372d2d17efa02fce71c10a49d0172daa781259b5b9d919fa380"))
            (  2203, uint256("0xfca331d8f613bdcb924570b8f993cfee409e1f78ec2f2afd8347b33efad2d33a"))
            (  2701, uint256("0x85618f98239b0f120914c17f7f37795aa32887e9b17e026f0c00323e05a77b06"))
            (  3000, uint256("0xef6c66058e32c8d0d23161ef301988c04e9298157311b64c6fea1fe45e9b4bed"))
            (  3150, uint256("0x22cedee94491c18bbdcb0a4da50cdcb0692c46c3bfccfe8a9d7b543e43c2337d"))
            (  3375, uint256("0x89bc95c8f702110c9352ec8052a44e2cb318a26fef8c355036fe8f954043063e"))
            ;

    bool CheckBlock(int nHeight, const uint256& hash)
    {
        if (fTestNet) return true; // Testnet has no checkpoints

        MapCheckpoints::const_iterator i = mapCheckpoints.find(nHeight);
        if (i == mapCheckpoints.end()) return true;
        return hash == i->second;
    }

    int GetTotalBlocksEstimate()
    {
        if (fTestNet) return 0;
        return mapCheckpoints.rbegin()->first;
    }

    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
    {
        if (fTestNet) return NULL;

        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
        {
            const uint256& hash = i.second;
            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
            if (t != mapBlockIndex.end())
                return t->second;
        }
        return NULL;
    }
}
<|endoftext|>


<commit_before>// Copyright (c) 2009-2015 The Bitcoin developers
// Copyright (c) 2011-2015 Litecoin Developers
// Copyright (c) 2013-2015 Globalcoin Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/foreach.hpp>

#include "checkpoints.h"

#include "main.h"
#include "uint256.h"

namespace Checkpoints
{
    typedef std::map<int, uint256> MapCheckpoints;

    //
    // What makes a good checkpoint block?
    // + Is surrounded by blocks with reasonable timestamps
    //   (no blocks before with a timestamp after, none after with
    //    timestamp before)
    // + Contains no strange transactions
    //
    static MapCheckpoints mapCheckpoints =
        boost::assign::map_list_of //Gensis and Checkpoints
        (         0, uint256("0xa28dcb138cef4fa6ea6e1a49fafb352c72c4fffa6674f2bb7e506817e27bdfcc"))
        (         2230, uint256("0x71a9d486b349c840ffdf29927414aa903c1443f945b7b2618d807ea8f3e15699"))
        (         7000, uint256("0xf8aa8224404caeada717008f6df70db2a2b6d04e44c29ee1fa7abcee7ae2aa5a"))
        (         12000, uint256("0x0f44a3b36b9d501f59a4e6b218a1f311b5342de436b134148cd902be7c3b4b05"))
        (         18000, uint256("0x21fb15db01439362a5113ea3c1cac04163673798974fd43f0c0e6532fab5f26d"))
        (         22300, uint256("0x063e89eae28119aa94cf44c9d7e155aec9ae4229dba37bd6456431e49868a039"))
        (         22644, uint256("0x9aec5d2cb0f43cc8089878daf85d5c441e9ee9b0e030263f13c23befef11a0cd"))
        (         26051, uint256("0x9541a7030ae49c99660ccf3242ccee6959153877fbfc1d53afe9cc9c04d85f6e"))
        (         39058, uint256("0x1ff3b6e143861c01fd952c16501c32b3723c8c14b8f1c62ca63d1daa22f5b0dd"))
        (         45053, uint256("0x403536342585bcfc3b299f919dff220c4a9facd2d4bb83f77aa8ab3ba3f0171c"))
        (         52062, uint256("0xccfe2de478a51a69cccd6ee765caee4f78d8ee4655ca708555b806aac911feca"))
        (         63390, uint256("0x9176fa0f727374ad7eda5206912cd81eefeb0a2ebb8878dab84f8cb1b65ee43f"))
        (         63399, uint256("0xae14b26cab4753cd6f0d15c3223ac7046f944d7510988851a48a6af5fd556b51"))
        (         63417, uint256("0xcc0a685b344f4fd1503974ef95d63f545edfe831fa30e1770cc676181d674fec"))
        (         63424, uint256("0x8ebe2f5acebe0bce1819645ceca3337c3c312ab8b2c298a5d5cf542531117a4b"))
        (         75000, uint256("0x2ace5643f4501d542bd6c047605c619c4854585050b677f97a6be7880eb02766"))
        (         85000, uint256("0x18d9c82e34157fb48f7722d1557bb505f7794e3465bbf0dbbde7b22b8dd98ebd"))
        (         90000, uint256("0xbd27920f0035a682e5807ed4539ee7e338538abca2915ce21b8e17725a161a7f"))
        (         97000, uint256("0x90c415d63cf43621a28e9962d31c07ffeea01184bbd1f5f7ffc71dc7cc33e1a8"))
        (         97200, uint256("0x5c561e62f3618555b8ace2345fd789fe2ba339da0882b28df46c372579eacf0b"))
        (         130000, uint256("0x695ca714ab42b13425b59dcc4c861bcb78070a8bfd47250b42cd87fb1b1c9981"))
        (         133000, uint256("0x16c8b8b8328c2236f221bdb1310fc73adc17367ec2c175af35ff2577d5a480d3"))
        (         179849, uint256("0xffd88e4420b67f8f92fe793e9dd19bba3a929747d491afe923a170d07a98299d"))
        (         185000, uint256("0x603af05e5e6964e5c2718658a4dbdc8f22d0826c266a3427468ae7bb7c129158"))
        (         195000, uint256("0x2e64ac4387c738935800ea8462c61c28002205040f55c49755d8ed4be5740f0b"))
        (         200000, uint256("0x693b6b5dab8cc79269da596598b245577dd0ea8bf0fc6b2cc09db38920012171"))
        (         220000, uint256("0x4d5052d3ec3c989237ac66ab16026a13819d515836302beefd6b8d19c2979ed9"))
        (         240000, uint256("0x715e10e343468c2383197c7c5ce463cb71c816fa35e6433bfff2f48b72d80142"))
        (         260000, uint256("0x5f04c7584f6d93d3cb96a91c71b2003340e3fcb15d5be327ca70506acd8dff9d"))
        (         300000, uint256("0x070e8e07c70bb1a3d25edfee44b08aed27291a92d4d6abe1966f16e90e97c5c6"))
        (         320000, uint256("0x76e68781f7f6c54c9704b80350bb4059d42cd3af492bb7d848cce64c30af6004"))
        (         340000, uint256("0xddc3e4e9ed5d39eaed20cc141148e7996f34dc0f0c8b263306279077d6d756a6"))
        (         360000, uint256("0xfc9ed488ca4ef32cd2d04e7d55237087d3d2c8e313447c6883fb31eae8d09be8"))
        (         380000, uint256("0x1e4dca66760910b9b14b58c1daf7b012603b9d6208e23ef3af1a5a0900d5aec4"))
        (         400000, uint256("0xe6a0ec05a27a40ec3b8c2ddb414df9d5fba64c5761e3599f96c050da389d4a09"))
        (         420000, uint256("0x1c8a82a688555e36ba8e376a770d6910dafadf9dff01d2d65fe08174fbe5bbf0"))
        (         440000, uint256("0x324170f6f6a3d3fa172121b5d221c7f0602ce603040ceb9af7ebfc3c8f2cfa2c"))
        (         460000, uint256("0xe8c5582aad569778ab9d610a501100f987d8bf05345d90b16284211c48b4d159"))
        (         480000, uint256("0x03f2e5815706d70d2dd88c25851086ad51173b7677f555b585d9abbdf65677c0"))
        (         500000, uint256("0xff93802621e2e90824c73ec6d672406a1beaee9610582cd28613e4abc0c1e855"))
        (         520000, uint256("0x8b65b813bf3e3d796cae87c3ceac9a3f129008aa678a557dd7727cb18a6b1625"))
        (         540000, uint256("0xc1ef13c7ebd3f946598096671da5df8d145aaab44a78f70702632824ec4ac6cf"))
        (         560000, uint256("0xca37279c1255f018dd4e989bcf5edacd098cbac67dc67ef9b8898fae0aa58013"))
        (         580000, uint256("0x57eb0935278e79883a5523b2a2a8d72a16430494a0e60b20dea40068a62ed846"))
        (         600000, uint256("0xdfc25b486ee340967320ebb8439f6bc238ee3e90781515bc21e1588b50d54eb4"))
        (         620000, uint256("0x3125d821fb513aaf682211d9d128b8854bd6aa4aa206fe2a9e317500516f5447"))
        (         640000, uint256("0x87d58fc3185cc85c4cdbc49505c66a3e64cc5087e4b6604686f80be16e4a2714"))
        (         660000, uint256("0x6ae70c28f3b942d9c0b6ce1d44c98f58e61aa8e4ace0573718cf9faed33438e0"))
        ;

    bool CheckBlock(int nHeight, const uint256& hash)
    {
        if (fTestNet) return true; // Testnet has no checkpoints

        MapCheckpoints::const_iterator i = mapCheckpoints.find(nHeight);
        if (i == mapCheckpoints.end()) return true;
        return hash == i->second;
    }

    int GetTotalBlocksEstimate()
    {
        if (fTestNet) return 0;
        return mapCheckpoints.rbegin()->first;
    }

    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
    {
        if (fTestNet) return NULL;

        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
        {
            const uint256& hash = i.second;
            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
            if (t != mapBlockIndex.end())
                return t->second;
        }
        return NULL;
    }
}
<commit_msg>Clean Up + add Checkpoints<commit_after>// Copyright (c) 2009-2015 The Bitcoin developers
// Copyright (c) 2011-2015 Litecoin Developers
// Copyright (c) 2013-2015 Globalcoin Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/foreach.hpp>

#include "checkpoints.h"

#include "main.h"
#include "uint256.h"

namespace Checkpoints
{
    typedef std::map<int, uint256> MapCheckpoints;

    //
    // What makes a good checkpoint block?
    // + Is surrounded by blocks with reasonable timestamps
    //   (no blocks before with a timestamp after, none after with
    //    timestamp before)
    // + Contains no strange transactions
    //
    static MapCheckpoints mapCheckpoints =
        boost::assign::map_list_of //Gensis and Checkpoints
        (         0, uint256("0xa28dcb138cef4fa6ea6e1a49fafb352c72c4fffa6674f2bb7e506817e27bdfcc"))
        (         2230, uint256("0x71a9d486b349c840ffdf29927414aa903c1443f945b7b2618d807ea8f3e15699"))
        (         7000, uint256("0xf8aa8224404caeada717008f6df70db2a2b6d04e44c29ee1fa7abcee7ae2aa5a"))
        (         12000, uint256("0x0f44a3b36b9d501f59a4e6b218a1f311b5342de436b134148cd902be7c3b4b05"))
        (         85000, uint256("0x18d9c82e34157fb48f7722d1557bb505f7794e3465bbf0dbbde7b22b8dd98ebd"))
        (         185000, uint256("0x603af05e5e6964e5c2718658a4dbdc8f22d0826c266a3427468ae7bb7c129158"))
        (         240000, uint256("0x715e10e343468c2383197c7c5ce463cb71c816fa35e6433bfff2f48b72d80142"))
        (         260000, uint256("0x5f04c7584f6d93d3cb96a91c71b2003340e3fcb15d5be327ca70506acd8dff9d"))
        (         380000, uint256("0x1e4dca66760910b9b14b58c1daf7b012603b9d6208e23ef3af1a5a0900d5aec4"))
        (         480000, uint256("0x03f2e5815706d70d2dd88c25851086ad51173b7677f555b585d9abbdf65677c0"))
        (         500000, uint256("0xff93802621e2e90824c73ec6d672406a1beaee9610582cd28613e4abc0c1e855"))
        (         580000, uint256("0x57eb0935278e79883a5523b2a2a8d72a16430494a0e60b20dea40068a62ed846"))
        (         600000, uint256("0xdfc25b486ee340967320ebb8439f6bc238ee3e90781515bc21e1588b50d54eb4"))
        (         660000, uint256("0x6ae70c28f3b942d9c0b6ce1d44c98f58e61aa8e4ace0573718cf9faed33438e0"))
        (         800000, uint256("0xe907d0d8963409581d65507434c90c3ce3719f058a1865282e28fc492e42c3dc"))
        (         900000, uint256("0xa5e3ed21f3e14764987f2ac9445c7ab0a601fd56cfe62187ece99e685ebf124e"))
        (         1000000, uint256("0xfea5852b22a9f82d9f90b3a1430b43926292b124b7ed5fb10de1353594f8e572"))
        (         1193100, uint256("0x124c12e426c1acf0c868fccec009bff2dc84ca91cff77cc599b91505fa4bdccd"))
        ;

    bool CheckBlock(int nHeight, const uint256& hash)
    {
        if (fTestNet) return true; // Testnet has no checkpoints

        MapCheckpoints::const_iterator i = mapCheckpoints.find(nHeight);
        if (i == mapCheckpoints.end()) return true;
        return hash == i->second;
    }

    int GetTotalBlocksEstimate()
    {
        if (fTestNet) return 0;
        return mapCheckpoints.rbegin()->first;
    }

    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
    {
        if (fTestNet) return NULL;

        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
        {
            const uint256& hash = i.second;
            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
            if (t != mapBlockIndex.end())
                return t->second;
        }
        return NULL;
    }
}
<|endoftext|>


<commit_before>#include "Arith256.h"
#include "Runtime.h"
#include "Type.h"

#include <llvm/IR/Function.h>

namespace dev
{
namespace eth
{
namespace jit
{

Arith256::Arith256(llvm::IRBuilder<>& _builder) :
	CompilerHelper(_builder)
{
	using namespace llvm;

	m_result = m_builder.CreateAlloca(Type::Word, nullptr, "arith.result");
	m_arg1 = m_builder.CreateAlloca(Type::Word, nullptr, "arith.arg1");
	m_arg2 = m_builder.CreateAlloca(Type::Word, nullptr, "arith.arg2");
	m_arg3 = m_builder.CreateAlloca(Type::Word, nullptr, "arith.arg3");

	using Linkage = GlobalValue::LinkageTypes;

	llvm::Type* arg2Types[] = {Type::WordPtr, Type::WordPtr, Type::WordPtr};
	llvm::Type* arg3Types[] = {Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr};

	m_mul = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_mul", getModule());
	m_div = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_div", getModule());
	m_mod = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_mod", getModule());
	m_sdiv = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_sdiv", getModule());
	m_smod = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_smod", getModule());
	m_exp = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_exp", getModule());
	m_addmod = Function::Create(FunctionType::get(Type::Void, arg3Types, false), Linkage::ExternalLinkage, "arith_addmod", getModule());
	m_mulmod = Function::Create(FunctionType::get(Type::Void, arg3Types, false), Linkage::ExternalLinkage, "arith_mulmod", getModule());
}

Arith256::~Arith256()
{}

llvm::Value* Arith256::binaryOp(llvm::Function* _op, llvm::Value* _arg1, llvm::Value* _arg2)
{
	m_builder.CreateStore(_arg1, m_arg1);
	m_builder.CreateStore(_arg2, m_arg2);
	m_builder.CreateCall3(_op, m_arg1, m_arg2, m_result);
	return m_builder.CreateLoad(m_result);
}

llvm::Value* Arith256::ternaryOp(llvm::Function* _op, llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3)
{
	m_builder.CreateStore(_arg1, m_arg1);
	m_builder.CreateStore(_arg2, m_arg2);
	m_builder.CreateStore(_arg3, m_arg3);
	m_builder.CreateCall4(_op, m_arg1, m_arg2, m_arg3, m_result);
	return m_builder.CreateLoad(m_result);
}

llvm::Value* Arith256::mul(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_mul, _arg1, _arg2);
}

llvm::Value* Arith256::div(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_div, _arg1, _arg2);
}

llvm::Value* Arith256::mod(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_mod, _arg1, _arg2);
}

llvm::Value* Arith256::sdiv(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_sdiv, _arg1, _arg2);
}

llvm::Value* Arith256::smod(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_smod, _arg1, _arg2);
}

llvm::Value* Arith256::exp(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_exp, _arg1, _arg2);
}

llvm::Value* Arith256::addmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3)
{
	return ternaryOp(m_addmod, _arg1, _arg2, _arg3);
}

llvm::Value* Arith256::mulmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3)
{
	return ternaryOp(m_mulmod, _arg1, _arg2, _arg3);
}

namespace
{
	using s256 = boost::multiprecision::int256_t;

	inline s256 u2s(u256 _u)
	{
		static const bigint c_end = (bigint)1 << 256;
		static const u256 c_send = (u256)1 << 255;
		if (_u < c_send)
			return (s256)_u;
		else
			return (s256)-(c_end - _u);
	}

	inline u256 s2u(s256 _u)
	{
		static const bigint c_end = (bigint)1 << 256;
		if (_u >= 0)
			return (u256)_u;
		else
			return (u256)(c_end + _u);
	}
}

}
}
}

extern "C"
{

	using namespace dev::eth::jit;

	EXPORT void arith_mul(i256* _arg1, i256* _arg2, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		*o_result = eth2llvm(arg1 * arg2);
	}

	EXPORT void arith_div(i256* _arg1, i256* _arg2, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		*o_result = eth2llvm(arg2 == 0 ? arg2 : arg1 / arg2);
	}

	EXPORT void arith_mod(i256* _arg1, i256* _arg2, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		*o_result = eth2llvm(arg2 == 0 ? arg2 : arg1 % arg2);
	}

	EXPORT void arith_sdiv(i256* _arg1, i256* _arg2, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		*o_result = eth2llvm(arg2 == 0 ? arg2 : s2u(u2s(arg1) / u2s(arg2)));
	}

	EXPORT void arith_smod(i256* _arg1, i256* _arg2, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		*o_result = eth2llvm(arg2 == 0 ? arg2 : s2u(u2s(arg1) % u2s(arg2)));
	}

	EXPORT void arith_exp(i256* _arg1, i256* _arg2, i256* o_result)
	{
		bigint left = llvm2eth(*_arg1);
		bigint right = llvm2eth(*_arg2);
		auto ret = static_cast<u256>(boost::multiprecision::powm(left, right, bigint(2) << 256));
		*o_result = eth2llvm(ret);
	}

	EXPORT void arith_mulmod(i256* _arg1, i256* _arg2, i256* _arg3, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		auto arg3 = llvm2eth(*_arg3);
		if (arg3 != 0)
			*o_result = eth2llvm(u256((bigint(arg1) * bigint(arg2)) % arg3));
		else
			*o_result = {};
	}

	EXPORT void arith_addmod(i256* _arg1, i256* _arg2, i256* _arg3, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		auto arg3 = llvm2eth(*_arg3);
		if (arg3 != 0)
			*o_result = eth2llvm(u256((bigint(arg1) + bigint(arg2)) % arg3));
		else
			*o_result = {};
	}

}

<commit_msg>Internal mul256 implementation<commit_after>#include "Arith256.h"
#include "Runtime.h"
#include "Type.h"

#include <llvm/IR/Function.h>

namespace dev
{
namespace eth
{
namespace jit
{

Arith256::Arith256(llvm::IRBuilder<>& _builder) :
	CompilerHelper(_builder)
{
	using namespace llvm;

	m_result = m_builder.CreateAlloca(Type::Word, nullptr, "arith.result");
	m_arg1 = m_builder.CreateAlloca(Type::Word, nullptr, "arith.arg1");
	m_arg2 = m_builder.CreateAlloca(Type::Word, nullptr, "arith.arg2");
	m_arg3 = m_builder.CreateAlloca(Type::Word, nullptr, "arith.arg3");

	using Linkage = GlobalValue::LinkageTypes;

	llvm::Type* arg2Types[] = {Type::WordPtr, Type::WordPtr, Type::WordPtr};
	llvm::Type* arg3Types[] = {Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr};

	m_mul = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_mul", getModule());
	m_div = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_div", getModule());
	m_mod = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_mod", getModule());
	m_sdiv = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_sdiv", getModule());
	m_smod = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_smod", getModule());
	m_exp = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_exp", getModule());
	m_addmod = Function::Create(FunctionType::get(Type::Void, arg3Types, false), Linkage::ExternalLinkage, "arith_addmod", getModule());
	m_mulmod = Function::Create(FunctionType::get(Type::Void, arg3Types, false), Linkage::ExternalLinkage, "arith_mulmod", getModule());
}

Arith256::~Arith256()
{}

llvm::Value* Arith256::binaryOp(llvm::Function* _op, llvm::Value* _arg1, llvm::Value* _arg2)
{
	m_builder.CreateStore(_arg1, m_arg1);
	m_builder.CreateStore(_arg2, m_arg2);
	m_builder.CreateCall3(_op, m_arg1, m_arg2, m_result);
	return m_builder.CreateLoad(m_result);
}

llvm::Value* Arith256::ternaryOp(llvm::Function* _op, llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3)
{
	m_builder.CreateStore(_arg1, m_arg1);
	m_builder.CreateStore(_arg2, m_arg2);
	m_builder.CreateStore(_arg3, m_arg3);
	m_builder.CreateCall4(_op, m_arg1, m_arg2, m_arg3, m_result);
	return m_builder.CreateLoad(m_result);
}

llvm::Value* Arith256::mul(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_mul, _arg1, _arg2);
}

llvm::Value* Arith256::div(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_div, _arg1, _arg2);
}

llvm::Value* Arith256::mod(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_mod, _arg1, _arg2);
}

llvm::Value* Arith256::sdiv(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_sdiv, _arg1, _arg2);
}

llvm::Value* Arith256::smod(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_smod, _arg1, _arg2);
}

llvm::Value* Arith256::exp(llvm::Value* _arg1, llvm::Value* _arg2)
{
	return binaryOp(m_exp, _arg1, _arg2);
}

llvm::Value* Arith256::addmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3)
{
	return ternaryOp(m_addmod, _arg1, _arg2, _arg3);
}

llvm::Value* Arith256::mulmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3)
{
	return ternaryOp(m_mulmod, _arg1, _arg2, _arg3);
}

namespace
{
	using s256 = boost::multiprecision::int256_t;

	inline s256 u2s(u256 _u)
	{
		static const bigint c_end = (bigint)1 << 256;
		static const u256 c_send = (u256)1 << 255;
		if (_u < c_send)
			return (s256)_u;
		else
			return (s256)-(c_end - _u);
	}

	inline u256 s2u(s256 _u)
	{
		static const bigint c_end = (bigint)1 << 256;
		if (_u >= 0)
			return (u256)_u;
		else
			return (u256)(c_end + _u);
	}

	using uint128 = __uint128_t;

//	uint128 add(uint128 a, uint128 b) { return a + b; }
//	uint128 mul(uint128 a, uint128 b) { return a * b; }
//
//	uint128 mulq(uint64_t x, uint64_t y)
//	{
//		return (uint128)x * (uint128)y;
//	}
//
//	uint128 addc(uint64_t x, uint64_t y)
//	{
//		return (uint128)x * (uint128)y;
//	}

	struct uint256
	{
		uint64_t lo;
		uint64_t mid;
		uint128 hi;
	};

//	uint256 add(uint256 x, uint256 y)
//	{
//		auto lo = (uint128) x.lo + y.lo;
//		auto mid = (uint128) x.mid + y.mid + (lo >> 64);
//		return {lo, mid, x.hi + y.hi + (mid >> 64)};
//	}

	uint256 mul(uint256 x, uint256 y)
	{
		auto t1 = (uint128) x.lo * y.lo;
		auto t2 = (uint128) x.lo * y.mid;
		auto t3 = x.lo * y.hi;
		auto t4 = (uint128) x.mid * y.lo;
		auto t5 = (uint128) x.mid * y.mid;
		auto t6 = x.mid * y.hi;
		auto t7 = x.hi * y.lo;
		auto t8 = x.hi * y.mid;

		auto lo = (uint64_t) t1;
		auto m1 = (t1 >> 64) + (uint64_t) t2;
		auto m2 = (uint64_t) m1;
		auto mid = (uint128) m2 + (uint64_t) t4;
		auto hi = (t2 >> 64) + t3 + (t4 >> 64) + t5 + (t6 << 64) + t7
			 + (t8 << 64) + (m1 >> 64) + (mid >> 64);

		return {lo, (uint64_t)mid, hi};
	}
}

}
}
}

extern "C"
{

	using namespace dev::eth::jit;

	EXPORT void arith_mul(uint256* _arg1, uint256* _arg2, uint256* o_result)
	{
		*o_result = mul(*_arg1, *_arg2);
	}

	EXPORT void arith_div(i256* _arg1, i256* _arg2, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		*o_result = eth2llvm(arg2 == 0 ? arg2 : arg1 / arg2);
	}

	EXPORT void arith_mod(i256* _arg1, i256* _arg2, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		*o_result = eth2llvm(arg2 == 0 ? arg2 : arg1 % arg2);
	}

	EXPORT void arith_sdiv(i256* _arg1, i256* _arg2, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		*o_result = eth2llvm(arg2 == 0 ? arg2 : s2u(u2s(arg1) / u2s(arg2)));
	}

	EXPORT void arith_smod(i256* _arg1, i256* _arg2, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		*o_result = eth2llvm(arg2 == 0 ? arg2 : s2u(u2s(arg1) % u2s(arg2)));
	}

	EXPORT void arith_exp(i256* _arg1, i256* _arg2, i256* o_result)
	{
		bigint left = llvm2eth(*_arg1);
		bigint right = llvm2eth(*_arg2);
		auto ret = static_cast<u256>(boost::multiprecision::powm(left, right, bigint(2) << 256));
		*o_result = eth2llvm(ret);
	}

	EXPORT void arith_mulmod(i256* _arg1, i256* _arg2, i256* _arg3, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		auto arg3 = llvm2eth(*_arg3);
		if (arg3 != 0)
			*o_result = eth2llvm(u256((bigint(arg1) * bigint(arg2)) % arg3));
		else
			*o_result = {};
	}

	EXPORT void arith_addmod(i256* _arg1, i256* _arg2, i256* _arg3, i256* o_result)
	{
		auto arg1 = llvm2eth(*_arg1);
		auto arg2 = llvm2eth(*_arg2);
		auto arg3 = llvm2eth(*_arg3);
		if (arg3 != 0)
			*o_result = eth2llvm(u256((bigint(arg1) + bigint(arg2)) % arg3));
		else
			*o_result = {};
	}

}

<|endoftext|>


<commit_before>#include <search-manager/NumericPropertyTable.h>
#include "NumericRangeGroupLabel.h"

NS_FACETED_BEGIN

NumericRangeGroupLabel::NumericRangeGroupLabel(const NumericPropertyTable *propertyTable, const float &targetValue)
    : propertyTable_(propertyTable)
    , targetValue_(targetValue)
    , test_(&NumericRangeGroupLabel::test1)
{}

NumericRangeGroupLabel::NumericRangeGroupLabel(const NumericPropertyTable *propertyTable, const int64_t &lowerBound, const int64_t &upperBound)
    : propertyTable_(propertyTable)
    , lowerBound_(lowerBound)
    , upperBound_(upperBound)
    , test_(&NumericRangeGroupLabel::test2)
{}

NumericRangeGroupLabel::~NumericRangeGroupLabel()
{
    delete propertyTable_;
}

bool NumericRangeGroupLabel::test(docid_t doc) const
{
    return (this->*test_)(doc);
}

bool NumericRangeGroupLabel::test1(docid_t doc) const
{
    float value;
    propertyTable_->convertPropertyValue(doc, value);
    return (value == targetValue_);
}

bool NumericRangeGroupLabel::test2(docid_t doc) const
{
    int64_t value;
    propertyTable_->convertPropertyValue(doc, value);
    return (value >= lowerBound_ && value <= upperBound_);
}

NS_FACETED_END
<commit_msg>make NumericRangeGroupLabel::test() more robust by checking conversion result.<commit_after>#include <search-manager/NumericPropertyTable.h>
#include "NumericRangeGroupLabel.h"

NS_FACETED_BEGIN

NumericRangeGroupLabel::NumericRangeGroupLabel(const NumericPropertyTable *propertyTable, const float &targetValue)
    : propertyTable_(propertyTable)
    , targetValue_(targetValue)
    , test_(&NumericRangeGroupLabel::test1)
{}

NumericRangeGroupLabel::NumericRangeGroupLabel(const NumericPropertyTable *propertyTable, const int64_t &lowerBound, const int64_t &upperBound)
    : propertyTable_(propertyTable)
    , lowerBound_(lowerBound)
    , upperBound_(upperBound)
    , test_(&NumericRangeGroupLabel::test2)
{}

NumericRangeGroupLabel::~NumericRangeGroupLabel()
{
    delete propertyTable_;
}

bool NumericRangeGroupLabel::test(docid_t doc) const
{
    return (this->*test_)(doc);
}

bool NumericRangeGroupLabel::test1(docid_t doc) const
{
    float value = 0;
    if (propertyTable_->convertPropertyValue(doc, value))
        return (value == targetValue_);

    return false;
}

bool NumericRangeGroupLabel::test2(docid_t doc) const
{
    int64_t value = 0;
    if (propertyTable_->convertPropertyValue(doc, value))
        return (value >= lowerBound_ && value <= upperBound_);

    return false;
}

NS_FACETED_END
<|endoftext|>


<commit_before>#include "dtex2/DrawTexture.h"
#include "dtex2/Texture.h"
#include "dtex2/typedef.h"
#include "dtex2/RenderAPI.h"
#include "dtex2/ResCache.h"
#include "dtex2/Target.h"

namespace dtex
{

CU_SINGLETON_DEFINITION(DrawTexture);

DrawTexture::DrawTexture()
	: m_curr(nullptr)
{
}

bool DrawTexture::Draw(int src_tex_id, int src_w, int src_h, const Rect& src_r, Texture* dst, const Rect& dst_r, bool rotate)
{
	Bind(dst);

	if (!RenderAPI::CheckTargetStatus()) {
		return false;
	}

	float vertices[8];
	float w_inv = m_curr->GetWidthInv(),
		  h_inv = m_curr->GetHeightInv();
	float dst_xmin = dst_r.xmin * w_inv * 2 - 1,
		  dst_xmax = dst_r.xmax * w_inv * 2 - 1,
		  dst_ymin = dst_r.ymin * h_inv * 2 - 1,
		  dst_ymax = dst_r.ymax * h_inv * 2 - 1;
	vertices[0] = dst_xmin; vertices[1] = dst_ymin;
	vertices[2] = dst_xmax; vertices[3] = dst_ymin;
	vertices[4] = dst_xmax; vertices[5] = dst_ymax;
	vertices[6] = dst_xmin; vertices[7] = dst_ymax;
	if (rotate)
	{
		float x, y;
		x = vertices[6]; y = vertices[7];
		vertices[6] = vertices[4]; vertices[7] = vertices[5];
		vertices[4] = vertices[2]; vertices[5] = vertices[3];
		vertices[2] = vertices[0]; vertices[3] = vertices[1];
		vertices[0] = x;           vertices[1] = y;
	}

	float texcoords[8];
	float src_w_inv = 1.0f / src_w,
		  src_h_inv = 1.0f / src_h;
	float src_xmin = src_r.xmin * src_w_inv,
		  src_xmax = src_r.xmax * src_w_inv,
		  src_ymin = src_r.ymin * src_h_inv,
		  src_ymax = src_r.ymax * src_h_inv;
	texcoords[0] = src_xmin; texcoords[1] = src_ymin;
	texcoords[2] = src_xmax; texcoords[3] = src_ymin;
	texcoords[4] = src_xmax; texcoords[5] = src_ymax;
	texcoords[6] = src_xmin; texcoords[7] = src_ymax;

	RenderAPI::SetProgram();
	RenderAPI::Draw(vertices, texcoords, src_tex_id);

	return true;
}

void DrawTexture::ClearTex(Texture* tex, float xmin, float ymin, float xmax, float ymax)
{
	Bind(tex);

	int w = m_curr->GetWidth(),
		h = m_curr->GetHeight();
	RenderAPI::ScissorPush(
		static_cast<int>(w * xmin),
		static_cast<int>(h * ymin),
		static_cast<int>(w * (xmax - xmin)),
		static_cast<int>(h * (ymax - ymin)));
	RenderAPI::ClearColor(0, 0, 0, 0);
	RenderAPI::ScissorPop();
}

void DrawTexture::Flush()
{
	if (m_curr) {
		DrawAfter(m_ctx);
		m_curr = nullptr;
	}
}

void DrawTexture::Clear()
{
	Flush();

	m_curr = nullptr;

	if (m_ctx.target) {
		ResCache::Instance()->ReturnTarget(m_ctx.target);
		m_ctx.target = nullptr;
	}
}

void DrawTexture::ClearAllTex(Texture* tex)
{
	Target* target = ResCache::Instance()->FetchTarget();

	target->Bind();
	target->BindTexture(tex->GetID());

	RenderAPI::ClearColor(0, 0, 0, 0);

	target->UnbindTexture();
	target->Unbind();

	ResCache::Instance()->ReturnTarget(target);
}

void DrawTexture::DrawBefore(Context& ctx)
{
	RenderAPI::DrawBegin();
	RenderAPI::GetViewport(ctx.vx, ctx.vy, ctx.vw, ctx.vh);
	RenderAPI::SetViewport(0, 0, m_curr->GetWidth(), m_curr->GetHeight());

	Target* target = ResCache::Instance()->FetchTarget();
	target->Bind();
	target->BindTexture(m_curr->GetID());

	ctx.target = target;
}

void DrawTexture::DrawAfter(Context& ctx)
{
	ctx.target->UnbindTexture();
	ctx.target->Unbind();
	ResCache::Instance()->ReturnTarget(ctx.target);
	ctx.target = nullptr;

	RenderAPI::SetViewport(ctx.vx, ctx.vy, ctx.vw, ctx.vh);
	RenderAPI::DrawEnd();
}

void DrawTexture::Bind(Texture* tex)
{
	if (!tex || m_curr == tex) {
		return;
	}
	if (m_curr) {
		DrawAfter(m_ctx);
	}
	m_curr = tex;
	DrawBefore(m_ctx);
}

}<commit_msg>[ADDED] check rt status<commit_after>#include "dtex2/DrawTexture.h"
#include "dtex2/Texture.h"
#include "dtex2/typedef.h"
#include "dtex2/RenderAPI.h"
#include "dtex2/ResCache.h"
#include "dtex2/Target.h"

namespace dtex
{

CU_SINGLETON_DEFINITION(DrawTexture);

DrawTexture::DrawTexture()
	: m_curr(nullptr)
{
}

bool DrawTexture::Draw(int src_tex_id, int src_w, int src_h, const Rect& src_r, Texture* dst, const Rect& dst_r, bool rotate)
{
	Bind(dst);
	if (!RenderAPI::CheckTargetStatus()) {
		return false;
	}

	float vertices[8];
	float w_inv = m_curr->GetWidthInv(),
		  h_inv = m_curr->GetHeightInv();
	float dst_xmin = dst_r.xmin * w_inv * 2 - 1,
		  dst_xmax = dst_r.xmax * w_inv * 2 - 1,
		  dst_ymin = dst_r.ymin * h_inv * 2 - 1,
		  dst_ymax = dst_r.ymax * h_inv * 2 - 1;
	vertices[0] = dst_xmin; vertices[1] = dst_ymin;
	vertices[2] = dst_xmax; vertices[3] = dst_ymin;
	vertices[4] = dst_xmax; vertices[5] = dst_ymax;
	vertices[6] = dst_xmin; vertices[7] = dst_ymax;
	if (rotate)
	{
		float x, y;
		x = vertices[6]; y = vertices[7];
		vertices[6] = vertices[4]; vertices[7] = vertices[5];
		vertices[4] = vertices[2]; vertices[5] = vertices[3];
		vertices[2] = vertices[0]; vertices[3] = vertices[1];
		vertices[0] = x;           vertices[1] = y;
	}

	float texcoords[8];
	float src_w_inv = 1.0f / src_w,
		  src_h_inv = 1.0f / src_h;
	float src_xmin = src_r.xmin * src_w_inv,
		  src_xmax = src_r.xmax * src_w_inv,
		  src_ymin = src_r.ymin * src_h_inv,
		  src_ymax = src_r.ymax * src_h_inv;
	texcoords[0] = src_xmin; texcoords[1] = src_ymin;
	texcoords[2] = src_xmax; texcoords[3] = src_ymin;
	texcoords[4] = src_xmax; texcoords[5] = src_ymax;
	texcoords[6] = src_xmin; texcoords[7] = src_ymax;

	RenderAPI::SetProgram();
	RenderAPI::Draw(vertices, texcoords, src_tex_id);

	return true;
}

void DrawTexture::ClearTex(Texture* tex, float xmin, float ymin, float xmax, float ymax)
{
	Bind(tex);
	if (!RenderAPI::CheckTargetStatus()) {
		return;
	}

	int w = m_curr->GetWidth(),
		h = m_curr->GetHeight();
	RenderAPI::ScissorPush(
		static_cast<int>(w * xmin),
		static_cast<int>(h * ymin),
		static_cast<int>(w * (xmax - xmin)),
		static_cast<int>(h * (ymax - ymin)));
	RenderAPI::ClearColor(0, 0, 0, 0);
	RenderAPI::ScissorPop();
}

void DrawTexture::Flush()
{
	if (m_curr) {
		DrawAfter(m_ctx);
		m_curr = nullptr;
	}
}

void DrawTexture::Clear()
{
	Flush();

	m_curr = nullptr;

	if (m_ctx.target) {
		ResCache::Instance()->ReturnTarget(m_ctx.target);
		m_ctx.target = nullptr;
	}
}

void DrawTexture::ClearAllTex(Texture* tex)
{
	Target* target = ResCache::Instance()->FetchTarget();

	target->Bind();
	target->BindTexture(tex->GetID());

	if (RenderAPI::CheckTargetStatus() != 0) {
		RenderAPI::ClearColor(0, 0, 0, 0);
	}

	target->UnbindTexture();
	target->Unbind();

	ResCache::Instance()->ReturnTarget(target);
}

void DrawTexture::DrawBefore(Context& ctx)
{
	RenderAPI::DrawBegin();
	RenderAPI::GetViewport(ctx.vx, ctx.vy, ctx.vw, ctx.vh);
	RenderAPI::SetViewport(0, 0, m_curr->GetWidth(), m_curr->GetHeight());

	Target* target = ResCache::Instance()->FetchTarget();
	target->Bind();
	target->BindTexture(m_curr->GetID());

	ctx.target = target;
}

void DrawTexture::DrawAfter(Context& ctx)
{
	ctx.target->UnbindTexture();
	ctx.target->Unbind();
	ResCache::Instance()->ReturnTarget(ctx.target);
	ctx.target = nullptr;

	RenderAPI::SetViewport(ctx.vx, ctx.vy, ctx.vw, ctx.vh);
	RenderAPI::DrawEnd();
}

void DrawTexture::Bind(Texture* tex)
{
	if (!tex || m_curr == tex) {
		return;
	}
	if (m_curr) {
		DrawAfter(m_ctx);
	}
	m_curr = tex;
	DrawBefore(m_ctx);
}

}<|endoftext|>


<commit_before>#ifndef DUNE_STUFF_CONFIGCONTAINER_HH_INCLUDED
#define DUNE_STUFF_CONFIGCONTAINER_HH_INCLUDED

#ifdef HAVE_CMAKE_CONFIG
 #include "cmake_config.h"
#elif defined (HAVE_CONFIG_H)
 #include <config.h>
#endif // ifdef HAVE_CMAKE_CONFIG

#include <dune/common/deprecated.hh>
#include <dune/common/parametertree.hh>
#include <dune/common/parametertreeparser.hh>
#include <dune/common/exceptions.hh>

#include <dune/stuff/common/logging.hh>
#include <dune/stuff/common/filesystem.hh>
#include <dune/stuff/common/misc.hh>
#include <dune/stuff/common/parameter/validation.hh>
#include <dune/stuff/common/parameter/tree.hh>
#include <dune/stuff/common/type_utils.hh>

#include <boost/format.hpp>
#include <set>

#define DSC_ORDER_REL_GENERIC(var,a,b) \
  if (a.var < b.var) { \
    return true;} \
  if (a.var > b.var) { \
    return false; }

#define DSC_ORDER_REL(var) DSC_ORDER_REL_GENERIC(var,(*this),other)

namespace Dune {
namespace Stuff {
namespace Common {

//! use this to record defaults, placements and so forth
struct Request {
  const int line;
  const std::string file;
  const std::string key;
  const std::string def;
  const std::string validator;
  Request(const int _line, const std::string _file, const std::string _key,
          const std::string _def, const std::string _validator)
    : line(_line)
    , file(_file)
    , key(_key)
    , def(_def)
    , validator(_validator)
  {}

  //! requests are considered
  bool operator < (const Request& other) const {
    DSC_ORDER_REL(key)
    DSC_ORDER_REL(def)
    DSC_ORDER_REL(file)
    DSC_ORDER_REL(line)
    return validator < other.validator;
  }

};

bool strictRequestCompare(const Request& a, const Request& b) {
  DSC_ORDER_REL_GENERIC(key,a,b);
  return a.def < b.def;
}

std::ostream& operator <<(std::ostream& out, const Request& r) {
  boost::format out_f("Request for %s with default %s in %s:%d (validation: %s)");
  out_f % r.key % r.def % r.file % r.line % r.validator;
  out << out_f.str();
  return out;
}

class ConfigContainer {
private:
  typedef std::map<std::string, std::set<Request> >
    RequestMapType;

    template< typename T, class Validator >
    T getValidValue(std::string name, T def,
                    const ValidatorInterface< T, Validator >& validator)
    {
        T val = tree_.get(name, def);
        if( validator(val) )
            return val;
        std::stringstream ss;
        validator.print(ss);
        DUNE_THROW(Dune::ParameterInvalid, ss.str());
    }

    //! return a set of Request objects for keys that have been queried with non-matching default values
    std::set<Request> getMismatchedDefaults(RequestMapType::value_type pair) const {
        typedef bool (*func)(const Request&,const Request&);
        std::set<Request,func> mismatched(&strictRequestCompare);
        mismatched.insert(pair.second.begin(), pair.second.end());
        return std::set<Request>(std::begin(mismatched), std::end(mismatched));
    }

    //! all public get signatures call this one
    template< typename T, class Validator >
    T get(std::string name, T def,
          const ValidatorInterface< T, Validator >& validator,
          bool UNUSED_UNLESS_DEBUG(useDbgStream),
          const Request& request)
    {
      requests_map_[name].insert(request);
      #ifndef NDEBUG
      if ( warning_output_ && !tree_.hasKey(name) )
      {
        if (useDbgStream)
          Logger().debug() << "WARNING: using default value for parameter \"" << name << "\"" << std::endl;
        else
          std::cerr << "WARNING: using default value for parameter \"" << name << "\"" << std::endl;
      }
      #endif // ifndef NDEBUG
      if ( record_defaults_ && !tree_.hasKey(name) )
        set(name, def);
      return getValidValue(name, def, validator);
    } // getParam

public:
    ConfigContainer(const Dune::ParameterTree& tree)
        : warning_output_(false)
        , tree_(tree)
        , record_defaults_(false)
    {}

    ConfigContainer()
        : warning_output_(true)
        , record_defaults_(false)
    {}

    ~ConfigContainer() {
      boost::filesystem::path logdir(get("global.datadir", "data", false));
      logdir /= get("logging.dir", "log", false);
      boost::filesystem::ofstream out(logdir / "paramter.log");
      tree_.report(out);
    }

    void readCommandLine(int argc, char* argv[]) {
      if (argc < 2)
      {
        boost::format usage("usage: %s parameter.file *[-section.key override-value]");
        DUNE_THROW(Dune::Exception, (usage % argv[0]).str());
      }
      Dune::ParameterTreeParser::readINITree(argv[1],tree_);
      Dune::ParameterTreeParser::readOptions(argc, argv, tree_);
    } // ReadCommandLine

    /** \brief  passthrough to underlying Dune::ParameterTree
       *  \param  useDbgStream
       *          needs to be set to false when using this function in Logging::Create,
       *              otherwise an assertion will fire cause streams aren't available yet
       **/
    template< typename T >
    T get(std::string name, T def, bool useDbgStream = true) {
      return get(name, def, ValidateAny< T >(), useDbgStream);
    }

    //! get variation with request recording
    template< typename T >
    T get(std::string name, T def, Request req, bool useDbgStream = true) {
      return get(name, def, ValidateAny< T >(), useDbgStream, req);
    }
    template< typename T, class Validator >
    T get(std::string name, T def, const ValidatorInterface< T, Validator >& validator,
          Request req, bool useDbgStream = true) {
      return get(name, def, validator, useDbgStream, req);
    }

    //! hack around the "CHARS" is no string issue
    std::string get(std::string name, const char* def, bool useDbgStream = true) {
      return get(name, std::string(def), ValidateAny< std::string >(), useDbgStream);
    }

    template< typename T, class Validator >
    T get(std::string name, T def,
          const ValidatorInterface< T, Validator >& validator,
          bool useDbgStream = true) {
      Request req(-1, std::string(), name,
                  Dune::Stuff::Common::toString(def),
                  Dune::Stuff::Common::getTypename(validator));
      return get(name, def, validator, useDbgStream, req);
    }

    //! hack around the "CHARS" is no string issue again
    template< class Validator >
    std::string get(std::string name, const char* def,
          const ValidatorInterface< std::string, Validator >& validator,
          bool useDbgStream = true)
    {
      return get<std::string,Validator>(name, def, validator, useDbgStream);
    }

    //! get variation with request recording
    std::string get(std::string name, const char* def, Request req, bool useDbgStream = true) {
      return get(name, std::string(def), ValidateAny< std::string >(), useDbgStream, req);
    }

    template<class T>
    void set(const std::string key, const T value) {
      tree_[key] = toString(value);
    }

    void printRequests(std::ostream& out) const {
      out << "Config requests:";
      for( const auto& pair : requests_map_ ) {
        out << "Key: " << pair.first;
        for( const auto& req : pair.second ) {
          out << "\n\t" << req;
        }
        out << std::endl;
      }
    }

    RequestMapType getMismatchedDefaultsMap() const {
      RequestMapType ret;
      for( const auto& pair : requests_map_ ) {
        auto mismatches = getMismatchedDefaults(pair);
        if(mismatches.size())
          ret[pair.first] = mismatches;
      }
      return ret;
    }

    void printMismatchedDefaults(std::ostream& out) const {
      for( const auto& pair : requests_map_ ) {
        out << "Mismatched uses for key " << pair.first << ":";
        for( const auto& req : getMismatchedDefaults(pair) ) {
          out << "\n\t" << req;
        }
        out << "\n";
      }
    }

    /**
     *  Control if the value map is filled with default values for missing entries
     *  Initially false
     **/
    void setRecordDefaults(bool record) {
      record_defaults_ = record;
    }

private:
    bool warning_output_;
    ExtendedParameterTree tree_;
    //! config key -> requests map
    RequestMapType requests_map_;
    bool record_defaults_;
};

//! global ConfigContainer instance
ConfigContainer& Config()  {
  static ConfigContainer parameters;
  return parameters;
}

} // namespace Common
} // namespace Stuff
} // namespace Dune

#define DSC_CONFIG Dune::Stuff::Common::Config()

#define DSC_CONFIG_GET(key,def) \
  DSC_CONFIG.get(key,def,Dune::Stuff::Common::Request(__LINE__, __FILE__,key,Dune::Stuff::Common::toString(def), "none"))

#define DSC_CONFIG_GETV(key,def,validator) \
  DSC_CONFIG.get(key,def, validator, Dune::Stuff::Common::Request(__LINE__, __FILE__,key,Dune::Stuff::Common::toString(def), #validator ))

#define DSC_CONFIG_GETB(key,def,use_logger) \
  DSC_CONFIG.get(key,def,Dune::Stuff::Common::Request(__LINE__, __FILE__,key,Dune::Stuff::Common::toString(def), "none" ), use_logger)

#endif // DUNE_STUFF_CONFIGCONTAINER_HH_INCLUDED

/** Copyright (c) 2012, Rene Milk
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of source code must retain the above copyright notice, this
   *    list of conditions and the following disclaimer.
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * The views and conclusions contained in the software and documentation are those
   * of the authors and should not be interpreted as representing official policies,
   * either expressed or implied, of the FreeBSD Project.
   **/
<commit_msg>[common.parameter.configcontianer] fixed wrond exception<commit_after>#ifndef DUNE_STUFF_CONFIGCONTAINER_HH_INCLUDED
#define DUNE_STUFF_CONFIGCONTAINER_HH_INCLUDED

#ifdef HAVE_CMAKE_CONFIG
 #include "cmake_config.h"
#elif defined (HAVE_CONFIG_H)
 #include <config.h>
#endif // ifdef HAVE_CMAKE_CONFIG

#include <dune/common/deprecated.hh>
#include <dune/common/parametertree.hh>
#include <dune/common/parametertreeparser.hh>
#include <dune/common/exceptions.hh>

#include <dune/stuff/common/logging.hh>
#include <dune/stuff/common/filesystem.hh>
#include <dune/stuff/common/misc.hh>
#include <dune/stuff/common/parameter/validation.hh>
#include <dune/stuff/common/parameter/tree.hh>
#include <dune/stuff/common/type_utils.hh>

#include <boost/format.hpp>
#include <set>

#define DSC_ORDER_REL_GENERIC(var,a,b) \
  if (a.var < b.var) { \
    return true;} \
  if (a.var > b.var) { \
    return false; }

#define DSC_ORDER_REL(var) DSC_ORDER_REL_GENERIC(var,(*this),other)

namespace Dune {
namespace Stuff {
namespace Common {

//! use this to record defaults, placements and so forth
struct Request {
  const int line;
  const std::string file;
  const std::string key;
  const std::string def;
  const std::string validator;
  Request(const int _line, const std::string _file, const std::string _key,
          const std::string _def, const std::string _validator)
    : line(_line)
    , file(_file)
    , key(_key)
    , def(_def)
    , validator(_validator)
  {}

  //! requests are considered
  bool operator < (const Request& other) const {
    DSC_ORDER_REL(key)
    DSC_ORDER_REL(def)
    DSC_ORDER_REL(file)
    DSC_ORDER_REL(line)
    return validator < other.validator;
  }

};

bool strictRequestCompare(const Request& a, const Request& b) {
  DSC_ORDER_REL_GENERIC(key,a,b);
  return a.def < b.def;
}

std::ostream& operator <<(std::ostream& out, const Request& r) {
  boost::format out_f("Request for %s with default %s in %s:%d (validation: %s)");
  out_f % r.key % r.def % r.file % r.line % r.validator;
  out << out_f.str();
  return out;
}

class InvalidParameter
    : public Dune::Exception
{};

class ConfigContainer {
private:
  typedef std::map<std::string, std::set<Request> >
    RequestMapType;

    template< typename T, class Validator >
    T getValidValue(std::string name, T def,
                    const ValidatorInterface< T, Validator >& validator)
    {
        T val = tree_.get(name, def);
        if( validator(val) )
            return val;
        std::stringstream ss;
        validator.print(ss);
        DUNE_THROW(InvalidParameter, ss.str());
    }

    //! return a set of Request objects for keys that have been queried with non-matching default values
    std::set<Request> getMismatchedDefaults(RequestMapType::value_type pair) const {
        typedef bool (*func)(const Request&,const Request&);
        std::set<Request,func> mismatched(&strictRequestCompare);
        mismatched.insert(pair.second.begin(), pair.second.end());
        return std::set<Request>(std::begin(mismatched), std::end(mismatched));
    }

    //! all public get signatures call this one
    template< typename T, class Validator >
    T get(std::string name, T def,
          const ValidatorInterface< T, Validator >& validator,
          bool UNUSED_UNLESS_DEBUG(useDbgStream),
          const Request& request)
    {
      requests_map_[name].insert(request);
      #ifndef NDEBUG
      if ( warning_output_ && !tree_.hasKey(name) )
      {
        if (useDbgStream)
          Logger().debug() << "WARNING: using default value for parameter \"" << name << "\"" << std::endl;
        else
          std::cerr << "WARNING: using default value for parameter \"" << name << "\"" << std::endl;
      }
      #endif // ifndef NDEBUG
      if ( record_defaults_ && !tree_.hasKey(name) )
        set(name, def);
      return getValidValue(name, def, validator);
    } // getParam

public:
    ConfigContainer(const Dune::ParameterTree& tree)
        : warning_output_(false)
        , tree_(tree)
        , record_defaults_(false)
    {}

    ConfigContainer()
        : warning_output_(true)
        , record_defaults_(false)
    {}

    ~ConfigContainer() {
      boost::filesystem::path logdir(get("global.datadir", "data", false));
      logdir /= get("logging.dir", "log", false);
      boost::filesystem::ofstream out(logdir / "paramter.log");
      tree_.report(out);
    }

    void readCommandLine(int argc, char* argv[]) {
      if (argc < 2)
      {
        boost::format usage("usage: %s parameter.file *[-section.key override-value]");
        DUNE_THROW(Dune::Exception, (usage % argv[0]).str());
      }
      Dune::ParameterTreeParser::readINITree(argv[1],tree_);
      Dune::ParameterTreeParser::readOptions(argc, argv, tree_);
    } // ReadCommandLine

    /** \brief  passthrough to underlying Dune::ParameterTree
       *  \param  useDbgStream
       *          needs to be set to false when using this function in Logging::Create,
       *              otherwise an assertion will fire cause streams aren't available yet
       **/
    template< typename T >
    T get(std::string name, T def, bool useDbgStream = true) {
      return get(name, def, ValidateAny< T >(), useDbgStream);
    }

    //! get variation with request recording
    template< typename T >
    T get(std::string name, T def, Request req, bool useDbgStream = true) {
      return get(name, def, ValidateAny< T >(), useDbgStream, req);
    }
    template< typename T, class Validator >
    T get(std::string name, T def, const ValidatorInterface< T, Validator >& validator,
          Request req, bool useDbgStream = true) {
      return get(name, def, validator, useDbgStream, req);
    }

    //! hack around the "CHARS" is no string issue
    std::string get(std::string name, const char* def, bool useDbgStream = true) {
      return get(name, std::string(def), ValidateAny< std::string >(), useDbgStream);
    }

    template< typename T, class Validator >
    T get(std::string name, T def,
          const ValidatorInterface< T, Validator >& validator,
          bool useDbgStream = true) {
      Request req(-1, std::string(), name,
                  Dune::Stuff::Common::toString(def),
                  Dune::Stuff::Common::getTypename(validator));
      return get(name, def, validator, useDbgStream, req);
    }

    //! hack around the "CHARS" is no string issue again
    template< class Validator >
    std::string get(std::string name, const char* def,
          const ValidatorInterface< std::string, Validator >& validator,
          bool useDbgStream = true)
    {
      return get<std::string,Validator>(name, def, validator, useDbgStream);
    }

    //! get variation with request recording
    std::string get(std::string name, const char* def, Request req, bool useDbgStream = true) {
      return get(name, std::string(def), ValidateAny< std::string >(), useDbgStream, req);
    }

    template<class T>
    void set(const std::string key, const T value) {
      tree_[key] = toString(value);
    }

    void printRequests(std::ostream& out) const {
      out << "Config requests:";
      for( const auto& pair : requests_map_ ) {
        out << "Key: " << pair.first;
        for( const auto& req : pair.second ) {
          out << "\n\t" << req;
        }
        out << std::endl;
      }
    }

    RequestMapType getMismatchedDefaultsMap() const {
      RequestMapType ret;
      for( const auto& pair : requests_map_ ) {
        auto mismatches = getMismatchedDefaults(pair);
        if(mismatches.size())
          ret[pair.first] = mismatches;
      }
      return ret;
    }

    void printMismatchedDefaults(std::ostream& out) const {
      for( const auto& pair : requests_map_ ) {
        out << "Mismatched uses for key " << pair.first << ":";
        for( const auto& req : getMismatchedDefaults(pair) ) {
          out << "\n\t" << req;
        }
        out << "\n";
      }
    }

    /**
     *  Control if the value map is filled with default values for missing entries
     *  Initially false
     **/
    void setRecordDefaults(bool record) {
      record_defaults_ = record;
    }

private:
    bool warning_output_;
    ExtendedParameterTree tree_;
    //! config key -> requests map
    RequestMapType requests_map_;
    bool record_defaults_;
};

//! global ConfigContainer instance
ConfigContainer& Config()  {
  static ConfigContainer parameters;
  return parameters;
}

} // namespace Common
} // namespace Stuff
} // namespace Dune

#define DSC_CONFIG Dune::Stuff::Common::Config()

#define DSC_CONFIG_GET(key,def) \
  DSC_CONFIG.get(key,def,Dune::Stuff::Common::Request(__LINE__, __FILE__,key,Dune::Stuff::Common::toString(def), "none"))

#define DSC_CONFIG_GETV(key,def,validator) \
  DSC_CONFIG.get(key,def, validator, Dune::Stuff::Common::Request(__LINE__, __FILE__,key,Dune::Stuff::Common::toString(def), #validator ))

#define DSC_CONFIG_GETB(key,def,use_logger) \
  DSC_CONFIG.get(key,def,Dune::Stuff::Common::Request(__LINE__, __FILE__,key,Dune::Stuff::Common::toString(def), "none" ), use_logger)

#endif // DUNE_STUFF_CONFIGCONTAINER_HH_INCLUDED

/** Copyright (c) 2012, Rene Milk
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of source code must retain the above copyright notice, this
   *    list of conditions and the following disclaimer.
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * The views and conclusions contained in the software and documentation are those
   * of the authors and should not be interpreted as representing official policies,
   * either expressed or implied, of the FreeBSD Project.
   **/
<|endoftext|>