numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
iteration_period_checker.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
26
27namespace num_collect::util {
28
33public:
39 explicit iteration_period_checker(index_type period) : period_(period) {
41 period > 0, "Period of iterations must be a positive integer.");
42 }
43
47 void iterate() noexcept {
48 ++counter_;
49 if (counter_ >= period_) [[unlikely]] {
50 counter_ = 0;
51 }
52 }
53
59 [[nodiscard]] auto is_start_of_period() const noexcept {
60 return counter_ == 0;
61 }
62
66 void reset() noexcept { counter_ = 0; }
67
74 iterate();
75 return *this;
76 }
77
83 explicit operator bool() const noexcept { return is_start_of_period(); }
84
85private:
88
91};
92
93} // namespace num_collect::util
Class to check periods of iterations.
auto operator++() noexcept -> iteration_period_checker &
Iterate.
iteration_period_checker(index_type period)
Constructor.
auto is_start_of_period() const noexcept
Check whether the current iteration is a start of the period.
Definition of exceptions.
Definition of index_type type.
Definition of macros for logging.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of utilities.
Definition assert.h:30
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.