status.hh 4.84 KB
Newer Older
1
2
3
4
5
6
7
// -*- 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    *
Dennis Gläser's avatar
Dennis Gläser committed
8
 *   the Free Software Foundation, either version 3 of the License, or       *
9
10
11
12
13
14
15
16
17
18
19
20
 *   (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
21
 * \ingroup Sampling
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 * \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 {

/*!
36
 * \ingroup Sampling
37
38
39
40
41
42
43
44
45
 * \brief Class that can be used to define target counters in
 *        sampling procedures and which outputs the current status.
 */
class SamplingStatus
{

public:

    /*!
46
     * \brief Set the target sample count for a specific id.
47
48
49
50
51
52
53
54
55
56
57
     */
    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;
    }

    /*!
58
59
     * \brief Returns true when the target
     *        sample count is reached.
60
61
62
63
64
65
66
67
68
69
     */
    bool finished()
    {
        for (const auto& pair : count_)
            if (pair.second != targetCount_.at(pair.first))
                return false;
        return true;
    }

    /*!
70
71
     * \brief Returns true when the target count
     *        for a specific id is reached.
72
73
74
75
76
77
78
79
80
81
     */
    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);
    }

    /*!
82
     * \brief Increase counter for a specific id.
83
84
85
86
87
88
89
     */
    void increaseCounter(const Id& id)
    {
        count_[id.get()]++;
    }

    /*!
90
     * \brief Increase counter of rejected samples.
91
92
93
94
95
96
97
     */
    void increaseRejectedCounter()
    {
        rejectedCount_++;
    }

    /*!
98
     * \brief Print current status to terminal.
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
133
134
135
136
     */
    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