status.hh 4.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
 *   See the file COPYING for full copying permissions.                      *
 *                                                                           *
 *   This program is free software: you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
 *   the Free Software Foundation, either version 2 of the License, or       *
 *   (at your option) any later version.                                     *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 *****************************************************************************/
/*!
 * \file
 * \brief Class that can be used to define target counters in
 *        sampling procedures and which outputs the current status.
 */
#ifndef FRACKIT_SAMPLING_STATUS_HH
#define FRACKIT_SAMPLING_STATUS_HH

#include <unordered_map>
#include <initializer_list>

#include <frackit/common/id.hh>

namespace Frackit {

/*!
 * \brief Class that can be used to define target counters in
 *        sampling procedures and which outputs the current status.
 */
class SamplingStatus
{

public:

    /*!
     * \brief TODO: Doc
     */
    void setTargetCount(const Id& id, std::size_t targetCount)
    {
        targetCount_[id.get()] = targetCount;

        auto it = count_.find(id.get());
        if (it == count_.end())
            count_[id.get()] = 0;
    }

    /*!
     * \brief TODO: Doc
     */
    bool finished()
    {
        for (const auto& pair : count_)
            if (pair.second != targetCount_.at(pair.first))
                return false;
        return true;
    }

    /*!
     * \brief TODO: Doc
     */
    bool finished(const Id& id)
    {
        auto it = count_.find(id.get());
        if (it == count_.end())
            throw std::runtime_error("Target count not set for given id");
        return it->second == targetCount_.at(it->first);
    }

    /*!
     * \brief TODO: Doc
     */
    void increaseCounter(const Id& id)
    {
        count_[id.get()]++;
    }

    /*!
     * \brief TODO: Doc
     */
    void increaseRejectedCounter()
    {
        rejectedCount_++;
    }

    /*!
     * \brief TODO: Doc
     */
    void print(bool forceHeaderPrint = false)
    {
        if (!headerPrinted_ || forceHeaderPrint)
        {
            std::cout << "#################################################################################\n"
                      << "# Accepted count   |   Rejected count   |   Acceptance Ratio   |   Progress [%] #\n"
                      << "#-------------------------------------------------------------------------------#\n";
            headerPrinted_ = true;
        }

        std::size_t curCount = 0;
        std::size_t curTargetCount = 0;
        for (const auto& pair : count_) curCount += pair.second;
        for (const auto& pair : targetCount_) curTargetCount += pair.second;

        const auto ratio = double(double(curCount)/double(curCount+rejectedCount_));
        const auto progress = 100.0*double(curCount)/double(curTargetCount);

        std::cout << "  "   << std::setw(17) << std::setfill(' ')
                            << std::to_string(curCount) + std::string(9, ' ')
                  << "|   " << std::setprecision(6) << std::setw(17) << std::setfill(' ')
                            << std::to_string(rejectedCount_) + std::string(9, ' ')
                  << "|   " << std::setprecision(6) << std::setw(19) << std::setfill(' ')
                            << std::to_string( ratio ) + std::string(7, ' ');
        std::cout << "|   " << std::setprecision(2) << std::string(2, ' ') + std::to_string( progress ) << std::endl;
    }

private:
    bool headerPrinted_ = false;
    std::size_t rejectedCount_ = 0;
    std::unordered_map<std::size_t, std::size_t> count_;
    std::unordered_map<std::size_t, std::size_t> targetCount_;
};

} // end namespace Frackit

#endif // FRACKIT_SAMPLING_STATUS_HH