Skip to content

Commit 3fc20f3

Browse files
Merge pull request hsf-training#208 from bernhardmgruber/lessc
Refactor examples to use less C
2 parents af02945 + 8c74724 commit 3fc20f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+269
-255
lines changed

code/callgrind/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
cmake_minimum_required( VERSION 3.1 )
44
project( callgrind LANGUAGES CXX )
55

6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
69
# Set up the compilation environment.
710
include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" )
811
include( "${CMAKE_CURRENT_SOURCE_DIR}/../SolutionTarget.cmake" )

code/callgrind/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ clean:
55
rm -f *o fibocrunch *~ fibocrunch.sol fibocrunch.nostl core callgrind.out.*
66

77
fibocrunch : fibocrunch.cpp
8-
${CXX} -g -O0 -Wall -Wextra -L. -o $@ $<
8+
${CXX} -std=c++17 -g -O0 -Wall -Wextra -L. -o $@ $<
99

1010
fibocrunch.sol : solution/fibocrunch.sol.cpp
11-
${CXX} -Wall -Wextra -L. -o $@ $<
11+
${CXX} -std=c++17 -Wall -Wextra -L. -o $@ $<

code/callgrind/fibocrunch.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#include <iostream>
2-
#include <cstdlib>
3-
#include <math.h>
1+
#include <random>
2+
#include <cmath>
43

5-
#define NBITERATIONS 20
6-
#define MAX 40
4+
constexpr auto NBITERATIONS = 20;
5+
constexpr auto MAX = 40u;
76

87
unsigned int add(unsigned int a, unsigned int b) {
98
return a + b;
@@ -28,13 +27,15 @@ unsigned int fibo(int a) {
2827
}
2928

3029
unsigned int fibo2(int n) {
31-
return static_cast<unsigned int>((1/sqrt(5)) * (pow(((1 + sqrt(5)) / 2), n) - pow(((1 - sqrt(5)) / 2), n)));
30+
return static_cast<unsigned int>((1/std::sqrt(5)) * (std::pow(((1 + std::sqrt(5)) / 2), n) - std::pow(((1 - std::sqrt(5)) / 2), n)));
3231
}
3332

3433
int main() {
34+
std::default_random_engine e;
35+
std::uniform_int_distribution d{0u, MAX};
3536
for (unsigned int i = 0; i < NBITERATIONS; i++) {
36-
unsigned int a = rand()%MAX;
37-
unsigned int b = rand()%MAX;
37+
unsigned int a = d(e);
38+
unsigned int b = d(e);
3839
add(a, b);
3940
mul(a, b);
4041
power(a, b);

code/callgrind/solution/fibocrunch.sol.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#include <iostream>
2-
#include <cstdlib>
3-
#include <math.h>
1+
#include <random>
2+
#include <cmath>
43

5-
#define NBITERATIONS 20
6-
#define MAX 40
4+
constexpr auto NBITERATIONS = 20;
5+
constexpr auto MAX = 40u;
76

87
unsigned int add(unsigned int a, unsigned int b) {
98
return a + b;
@@ -28,13 +27,15 @@ unsigned int fibo(int a) {
2827
}
2928

3029
unsigned int fibo2(int n) {
31-
return static_cast<unsigned int>((1/sqrt(5)) * (pow(((1 + sqrt(5)) / 2), n) - pow(((1 - sqrt(5)) / 2), n)));
30+
return static_cast<unsigned int>((1/std::sqrt(5)) * (std::pow(((1 + std::sqrt(5)) / 2), n) - std::pow(((1 - std::sqrt(5)) / 2), n)));
3231
}
3332

3433
int main() {
34+
std::default_random_engine e;
35+
std::uniform_int_distribution d{0u, MAX};
3536
for (unsigned int i = 0; i < NBITERATIONS; i++) {
36-
unsigned int a = rand()%MAX;
37-
unsigned int b = rand()%MAX;
37+
unsigned int a = d(e);
38+
unsigned int b = d(e);
3839
add(a, b);
3940
mul(a, b);
4041
power(a, b);

code/condition_variable/condition_variable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
using namespace std::chrono_literals; // We can write 1s
1515

16-
std::mutex cout_mutex{}; // We need this to synchronise printing
16+
std::mutex cout_mutex; // We need this to synchronise printing
1717

1818
// Print contents of the stream to cout in a thread-safe manner.
1919
// This class consumes stream inputs, buffers them, and writes them
@@ -61,7 +61,7 @@ bool process(unsigned int threadIdx, Data const & data) {
6161

6262
// Burn some CPU cycles to simulate intensive data processing.
6363
const auto startTime = std::chrono::high_resolution_clock::now();
64-
int dummyCounter = 0;
64+
unsigned dummyCounter = 0;
6565
while (std::chrono::high_resolution_clock::now() - startTime < 5s) {
6666
++dummyCounter;
6767
}

code/condition_variable/solution/condition_variable.sol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
using namespace std::chrono_literals; // We can write 1s
1515

16-
std::mutex cout_mutex{}; // We need this to synchronise printing
16+
std::mutex cout_mutex; // We need this to synchronise printing
1717

1818
// Print contents of the stream to cout in a thread-safe manner.
1919
// This class consumes stream inputs, buffers them, and writes them
@@ -61,7 +61,7 @@ bool process(unsigned int threadIdx, Data const & data) {
6161

6262
// Burn some CPU cycles to simulate intensive data processing.
6363
const auto startTime = std::chrono::high_resolution_clock::now();
64-
int dummyCounter = 0;
64+
unsigned dummyCounter = 0;
6565
while (std::chrono::high_resolution_clock::now() - startTime < 5s) {
6666
++dummyCounter;
6767
}

code/cppcheck/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ clean:
55
rm -f *o randomize *~ randomize.sol
66

77
randomize : randomize.cpp
8-
${CXX} -g -O0 -Wall -Wextra -L. -o $@ $<
8+
${CXX} -std=c++17 -g -O0 -Wall -Wextra -L. -o $@ $<
99

1010
randomize.sol : solution/randomize.sol.cpp
11-
${CXX} -g -O0 -Wall -Wextra -L. -o $@ $<
11+
${CXX} -std=c++17 -g -O0 -Wall -Wextra -L. -o $@ $<

code/cppcheck/randomize.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <iostream>
2-
#include <math.h>
3-
#include <cstdlib>
2+
#include <cmath>
3+
#include <random>
44

5-
#define LEN 1000
6-
#define STEP 10
5+
constexpr auto LEN = 1000;
6+
constexpr auto STEP = 10;
77

88
void swap(int *a, int*b) {
99
int c = *a;
@@ -12,10 +12,12 @@ void swap(int *a, int*b) {
1212
}
1313

1414
void randomize(int* v, unsigned int len) {
15+
std::default_random_engine e;
16+
std::uniform_int_distribution d{0u, len};
1517
// we randomize via len random inversions
1618
for (unsigned int i = 0; i < len; i++) {
17-
int a = rand()%len;
18-
int b = rand()%len;
19+
int a = d(e);
20+
int b = d(e);
1921
swap(v+a, v+b);
2022
}
2123
}
@@ -43,7 +45,7 @@ int main() {
4345
sumsq += diffs[i]*diffs[i];
4446
}
4547
float mean = sum/LEN;
46-
float stddev = sqrt(sumsq/LEN - mean*mean) ;
48+
float stddev = std::sqrt(sumsq/LEN - mean*mean) ;
4749
std::cout << "Range = [0, " << STEP*LEN << "]\n"
4850
<< "Mean = " << mean
4951
<< "\nStdDev = " << stddev << '\n';

code/cppcheck/solution/randomize.sol.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <iostream>
2-
#include <math.h>
3-
#include <cstdlib>
2+
#include <cmath>
3+
#include <random>
44

5-
#define LEN 1000
6-
#define STEP 10
5+
constexpr auto LEN = 1000;
6+
constexpr auto STEP = 10;
77

88
void swap(int *a, int*b) {
99
int c = *a;
@@ -12,10 +12,12 @@ void swap(int *a, int*b) {
1212
}
1313

1414
void randomize(int* v, unsigned int len) {
15+
std::default_random_engine e;
16+
std::uniform_int_distribution d{0u, len};
1517
// we randomize via len random inversions
1618
for (unsigned int i = 0; i < len; i++) {
17-
int a = rand()%len;
18-
int b = rand()%len;
19+
int a = d(e);
20+
int b = d(e);
1921
swap(v+a, v+b);
2022
}
2123
}
@@ -43,7 +45,7 @@ int main() {
4345
sumsq += diffs[i]*diffs[i];
4446
}
4547
float mean = sum/LEN;
46-
float stddev = sqrt(sumsq/LEN - mean*mean) ;
48+
float stddev = std::sqrt(sumsq/LEN - mean*mean) ;
4749
std::cout << "Range = [0, " << STEP*LEN << "]\n"
4850
<< "Mean = " << mean
4951
<< "\nStdDev = " << stddev << '\n';

code/debug/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ clean:
55
rm -f *o randomize *~ randomize.sol core
66

77
randomize : randomize.cpp
8-
${CXX} -g -O0 -L. -o $@ $<
8+
${CXX} -std=c++17 -g -O0 -L. -o $@ $<
99

1010
randomize.sol : solution/randomize.sol.cpp
11-
${CXX} -g -O0 -Wall -Wextra -L. -o $@ $<
11+
${CXX} -std=c++17 -g -O0 -Wall -Wextra -L. -o $@ $<

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy