-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepeat_example.cpp
More file actions
141 lines (110 loc) · 3.99 KB
/
repeat_example.cpp
File metadata and controls
141 lines (110 loc) · 3.99 KB
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
133
134
135
136
137
138
139
140
141
/** @file
*
* @brief Example code demonstrating use of @c chops::repeat.
*
* @ingroup example_module
*
* @author Thurman Gillespy
*
* Copyright (c)2019 by Thurman Gillespy
* 3/22/19
*
* Minor update 4/4/2025, adding <cstdint> include.
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
* Sample make file:
* g++ -std=c++17 -I ~/Projects/utility-rack/include/ repeatDemo.cpp
*/
#include <iostream>
#include <cstdlib> // EXIT_SUCCESS
#include <string>
#include <cstdint> // std::uint64_t
#include "utility/repeat.hpp"
void printHello() { std::cout << "Hello, world\n"; } // simple function
void (*helloPtr)() = printHello; // pointer to simple function
void printNum(int num) { std::cout << num << " "; } // print the number passed to function
// calculate factorial - each call calculates the next factorial
const void calcFactorial(bool print) {
static int count = 1;
static std::uint64_t fact = 1;
fact *= count;
if (print) {
std::cout << count << "!= " << fact <<std::endl;
}
++count;
}
// wrapper functions determine whether values are printed or not
void advanceFactorial() { calcFactorial(false); }
void printFactorial() { calcFactorial(true); }
// calculate factorial
class Factorial {
public:
// default constructor (with 0!)
constexpr Factorial() = default;
// construct with num!
constexpr Factorial(int num) { next(num); }
// get the current calculated factorial (default is 0!)
constexpr std::uint64_t getFact() const { return m_fact; }
constexpr int getCount() const { return m_count; }
// calculate the next num factorials
constexpr std::uint64_t next(int num) {
chops::repeat(num, [&] () { m_fact *= (m_count++ == 0 ? 1 : m_count); } );
return m_fact;
}
// calculate the next factorial
constexpr std::uint64_t next() {
return next(1);
}
// print methods
void print() { std::cout << m_fact <<std::endl; }
void print(char end) { std::cout << m_fact << end; }
private:
int m_count = 0;
std::uint64_t m_fact = 1;
};
// tasty utility lambdas
constexpr auto printLn = [] () { std::cout << std::endl; };
constexpr auto printStr = [] (std::string str) { std::cout << str << std::endl; };
int main() {
printStr("calling chops::repeat with function, pointer to function, lambda function");
chops::repeat(3, printHello); // use a function
printLn();
chops::repeat(2, helloPtr); // use pointer to function
printLn();
chops::repeat(7, [] () {std::cout << "Hello" << " ";}); // use lambda function
printLn();
// print varialble passed to function
chops::repeat(5, printNum);
printLn();
// sum of the first n numbers
int sum = 0;
int n = 10;
chops::repeat(n, [&] () { static int i = 1; sum += i++; });
std::cout << "sum of the first " << n << " numbers = " << sum << std::endl;
printLn();
// factorials using lambda function
printStr("factorial using lambda function");
std::uint64_t fact = 1;
int num = 5;
chops::repeat(5, [&] () { static int count = 1; fact *= count++; });
std::cout << num <<"! = " << fact <<std::endl << std::endl;
// factorials using functions
printStr("factorials using functions");
printStr("print the first 10 factorials");
chops::repeat(10, printFactorial);
printStr("print factorials 15 - 20");
chops::repeat(4, advanceFactorial);
chops::repeat(6, printFactorial);
printLn();
// factorials using class
printStr("factorials using class Factorial");
Factorial f1; // 4!
f1.next(); f1.next(); f1.next(); f1.next(); // call ::next explicitly
std::cout << f1.getCount() << "! = " << f1.getFact() << std::endl;
Factorial f2; // 10!
chops::repeat(10, [&] () { f2.next(); }); // call ::next using chops::repeat
std::cout << f2.getCount() << "! = " << f2.getFact() << std::endl;
return EXIT_SUCCESS;
}