Skip to content

Commit 9919299

Browse files
author
Henry Wu
committed
init check
1 parent dc8c9e8 commit 9919299

File tree

346 files changed

+6819
-0
lines changed

Some content is hidden

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

346 files changed

+6819
-0
lines changed

basics/addspace.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
template<typename T>
2+
class AddSpace
3+
{
4+
private:
5+
T const& ref; // refer to argument passed in constructor
6+
public:
7+
AddSpace(T const& r): ref(r) {
8+
}
9+
friend std::ostream& operator<< (std::ostream& os, AddSpace<T> s) {
10+
return os << s.ref << ' '; // output passed argument and a space
11+
}
12+
};
13+
14+
template<typename... Args>
15+
void print (Args... args) {
16+
( std::cout << ... << AddSpace(args) ) << '\n';
17+
}

basics/addvalue.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
template<int Val, typename T>
2+
T addValue (T x)
3+
{
4+
return x + Val;
5+
}

basics/arrays.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "arrays.hpp"
2+
3+
template<typename T1, typename T2, typename T3>
4+
void foo(int a1[7], int a2[], // pointers by language rules
5+
int (&a3)[42], // reference to array of known bound
6+
int (&x0)[], // reference to array of unknown bound
7+
T1 x1, // passing by value decays
8+
T2& x2, T3&& x3) // passing by reference
9+
{
10+
MyClass<decltype(a1)>::print(); // uses MyClass<T*>
11+
MyClass<decltype(a2)>::print(); // uses MyClass<T*>
12+
MyClass<decltype(a3)>::print(); // uses MyClass<T(\&)[SZ]>
13+
MyClass<decltype(x0)>::print(); // uses MyClass<T(\&)[]>
14+
MyClass<decltype(x1)>::print(); // uses MyClass<T*>
15+
MyClass<decltype(x2)>::print(); // uses MyClass<T(\&)[]>
16+
MyClass<decltype(x3)>::print(); // uses MyClass<T(\&)[]>
17+
}
18+
19+
int main()
20+
{
21+
int a[42];
22+
MyClass<decltype(a)>::print(); // uses MyClass<T[SZ]>
23+
24+
extern int x[]; // forward declare array
25+
MyClass<decltype(x)>::print(); // uses MyClass<T[]>
26+
27+
foo(a, a, a, x, x, x, x);
28+
}
29+
30+
int x[] = {0, 8, 15}; // define forward-declared array

basics/arrays.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
3+
template<typename T>
4+
struct MyClass; // primary template
5+
6+
template<typename T, std::size_t SZ>
7+
struct MyClass<T[SZ]> // partial specialization for arrays of known bounds
8+
{
9+
static void print() { std::cout << "print() for T[" << SZ << "]\n"; }
10+
};
11+
12+
template<typename T, std::size_t SZ>
13+
struct MyClass<T(&)[SZ]> // partial spec. for references to arrays of known bounds
14+
{
15+
static void print() { std::cout << "print() for T(&)[" << SZ << "]\n"; }
16+
};
17+
18+
template<typename T>
19+
struct MyClass<T[]> // partial specialization for arrays of unknown bounds
20+
{
21+
static void print() { std::cout << "print() for T[]\n"; }
22+
};
23+
24+
template<typename T>
25+
struct MyClass<T(&)[]> // partial spec. for references to arrays of unknown bounds
26+
{
27+
static void print() { std::cout << "print() for T(&)[]\n"; }
28+
};
29+
30+
template<typename T>
31+
struct MyClass<T*> // partial specialization for pointers
32+
{
33+
static void print() { std::cout << "print() for T*\n"; }
34+
};

basics/boolstring.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class BoolString {
2+
private:
3+
std::string value;
4+
public:
5+
BoolString (std::string const& s)
6+
: value(s) {
7+
}
8+
template<typename T = std::string>
9+
T get() const { // get value (converted to T)
10+
return value;
11+
}
12+
};

basics/boolstringgetbool.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// full specialization for BoolString::getValue<>() for bool
2+
template<>
3+
inline bool BoolString::get<bool>() const {
4+
return value == "true" || value == "1" || value == "on";
5+
}

basics/byref.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
template<typename T>
2+
T const& checked (T const& a, T const& b)
3+
{
4+
return test() ? a : b;
5+
}

basics/byvalue.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
template<typename T>
2+
T checked (T a, T b)
3+
{
4+
return test() ? a : b;
5+
}

basics/cref.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <functional> // for std::cref()
2+
#include <string>
3+
#include <iostream>
4+
5+
void printString(std::string const& s)
6+
{
7+
std::cout << s << '\n';
8+
}
9+
10+
template<typename T>
11+
void printT (T arg)
12+
{
13+
printString(arg); // might convert arg back to std::string
14+
}
15+
16+
int main()
17+
{
18+
std::string s = "hello";
19+
printT(s); // print s passed by value
20+
printT(std::cref(s)); // print s passed ``as if by reference''
21+
}

basics/errornovel1.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <string>
2+
#include <map>
3+
#include <algorithm>
4+
5+
int main()
6+
{
7+
std::map<std::string,double> coll;
8+
//...
9+
// find the first nonempty string in coll:
10+
auto pos = std::find_if (coll.begin(), coll.end(),
11+
[] (std::string const& s) {
12+
return s != "";
13+
});
14+
}

basics/errornovel2.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <string>
2+
#include <unordered_set>
3+
4+
class Customer
5+
{
6+
private:
7+
std::string name;
8+
public:
9+
Customer (std::string const& n)
10+
: name(n) {
11+
}
12+
std::string getName() const {
13+
return name;
14+
}
15+
};
16+
17+
int main()
18+
{
19+
// provide our own hash function:
20+
struct MyCustomerHash {
21+
// NOTE: missing const is only an error with g++ and clang:
22+
std::size_t operator() (Customer const& c) {
23+
return std::hash<std::string>()(c.getName());
24+
}
25+
};
26+
27+
// and use it for a hash table of Customers:
28+
std::unordered_set<Customer,MyCustomerHash> coll;
29+
//...
30+
}

basics/foldtraverse.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// define binary tree structure and traverse helpers:
2+
struct Node {
3+
int value;
4+
Node* left;
5+
Node* right;
6+
Node(int i=0) : value(i), left(nullptr), right(nullptr) {
7+
}
8+
//...
9+
};
10+
auto left = &Node::left;
11+
auto right = &Node::right;
12+
13+
// traverse tree, using fold expression:
14+
template<typename T, typename... TP>
15+
Node* traverse (T np, TP... paths) {
16+
return (np ->* ... ->* paths); // np ->* paths1 ->* paths2 ...
17+
}
18+
19+
int main()
20+
{
21+
// init binary tree structure:
22+
Node* root = new Node{0};
23+
root->left = new Node{1};
24+
root->left->right = new Node{2};
25+
//...
26+
// traverse binary tree:
27+
Node* node = traverse(root, left, right);
28+
//...
29+
}

basics/foreach.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include "foreach.hpp"
4+
5+
// a function to call:
6+
void func(int i)
7+
{
8+
std::cout << "func() called for: " << i << '\n';
9+
}
10+
11+
// a function object type (for objects that can be used as functions):
12+
class FuncObj {
13+
public:
14+
void operator() (int i) const { // Note: const member function
15+
std::cout << "FuncObj::op() called for: " << i << '\n';
16+
}
17+
};
18+
19+
int main()
20+
{
21+
std::vector<int> primes = { 2, 3, 5, 7, 11, 13, 17, 19 };
22+
23+
foreach(primes.begin(), primes.end(), // range
24+
func); // function as callable (decays to pointer)
25+
26+
foreach(primes.begin(), primes.end(), // range
27+
&func); // function pointer as callable
28+
29+
foreach(primes.begin(), primes.end(), // range
30+
FuncObj()); // function object as callable
31+
32+
foreach(primes.begin(), primes.end(), // range
33+
[] (int i) { // lambda as callable
34+
std::cout << "lambda called for: " << i << '\n';
35+
});
36+
}

basics/foreach.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
template<typename Iter, typename Callable>
2+
void foreach (Iter current, Iter end, Callable op)
3+
{
4+
while (current != end) { // as long as not reached the end
5+
op(*current); // call passed operator for current element
6+
++current; // and move iterator to next element
7+
}
8+
}

basics/foreachinvoke.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include "foreachinvoke.hpp"
5+
6+
// a class with a member function that shall be called
7+
class MyClass {
8+
public:
9+
void memfunc(int i) const {
10+
std::cout << "MyClass::memfunc() called for: " << i << '\n';
11+
}
12+
};
13+
14+
int main()
15+
{
16+
std::vector<int> primes = { 2, 3, 5, 7, 11, 13, 17, 19 };
17+
18+
// pass lambda as callable and an additional argument:
19+
foreach(primes.begin(), primes.end(), // elements for 2nd arg of lambda
20+
[](std::string const& prefix, int i) { // lambda to call
21+
std::cout << prefix << i << '\n';
22+
},
23+
"- value: "); // 1st arg of lambda
24+
25+
// call obj.memfunc() for/with each elements in primes passed as argument
26+
MyClass obj;
27+
foreach(primes.begin(), primes.end(), // elements used as args
28+
&MyClass::memfunc, // member function to call
29+
obj); // object to call memfunc() for
30+
}

basics/foreachinvoke.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <utility>
2+
#include <functional>
3+
4+
template<typename Iter, typename Callable, typename... Args>
5+
void foreach (Iter current, Iter end, Callable op, Args const&... args)
6+
{
7+
while (current != end) { // as long as not reached the end of the elements
8+
std::invoke(op, // call passed callable with
9+
args..., // any additional args
10+
*current); // and the current element
11+
++current;
12+
}
13+
}

basics/invoke.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <utility> // for std::invoke()
2+
#include <functional> // for std::forward()
3+
4+
template<typename Callable, typename... Args>
5+
decltype(auto) call(Callable&& op, Args&&... args)
6+
{
7+
return std::invoke(std::forward<Callable>(op), // passed callable with
8+
std::forward<Args>(args)...); // any additional args
9+
}

basics/invokeret.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <utility> // for std::invoke()
2+
#include <functional> // for std::forward()
3+
#include <type_traits> // for std::is_same<> and invoke_result<>
4+
5+
template<typename Callable, typename... Args>
6+
decltype(auto) call(Callable&& op, Args&&... args)
7+
{
8+
if constexpr(std::is_same_v<std::invoke_result_t<Callable, Args...>,
9+
void>) {
10+
// return type is void:
11+
std::invoke(std::forward<Callable>(op),
12+
std::forward<Args>(args)...);
13+
//...
14+
return;
15+
}
16+
else {
17+
// return type is not void:
18+
decltype(auto) ret{std::invoke(std::forward<Callable>(op),
19+
std::forward<Args>(args)...)};
20+
//...
21+
return ret;
22+
}
23+
}

basics/isprime.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
template<unsigned p, unsigned d> // p: number to check, d: current divisor
2+
struct DoIsPrime {
3+
static constexpr bool value = (p%d != 0) && DoIsPrime<p,d-1>::value;
4+
};
5+
6+
template<unsigned p> // end recursion if divisor is 2
7+
struct DoIsPrime<p,2> {
8+
static constexpr bool value = (p%2 != 0);
9+
};
10+
11+
template<unsigned p> // primary template
12+
struct IsPrime {
13+
// start recursion with divisor from p/2:
14+
static constexpr bool value = DoIsPrime<p,p/2>::value;
15+
};
16+
17+
// special cases (to avoid endless recursion with template instantiation):
18+
template<>
19+
struct IsPrime<0> { static constexpr bool value = false; };
20+
template<>
21+
struct IsPrime<1> { static constexpr bool value = false; };
22+
template<>
23+
struct IsPrime<2> { static constexpr bool value = true; };
24+
template<>
25+
struct IsPrime<3> { static constexpr bool value = true; };

basics/isprime11.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
constexpr bool
2+
doIsPrime (unsigned p, unsigned d) // p: number to check, d: current divisor
3+
{
4+
return d!=2 ? (p%d!=0) && doIsPrime(p,d-1) // check this and smaller divisors
5+
: (p%2!=0); // end recursion if divisor is 2
6+
}
7+
8+
constexpr bool isPrime (unsigned p)
9+
{
10+
return p < 4 ? !(p<2) // handle special cases
11+
: doIsPrime(p,p/2); // start recursion with divisor from p/2
12+
}

basics/isprime14.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
constexpr bool isPrime (unsigned int p)
2+
{
3+
for (unsigned int d=2; d<=p/2; ++d) {
4+
if (p % d == 0) {
5+
return false; // found divisor without remainder
6+
}
7+
}
8+
return p > 1; // no divisor without remainder found
9+
}

0 commit comments

Comments
 (0)