Commit b49b00a8 authored by Praetorius, Simon's avatar Praetorius, Simon

initial commit

parents
# Ignore all
*
# Unignore all with extensions
!*.*
# Unignore all dirs
!*/
# Unignore Makefiles
!Makefile
### Above combination will ignore all files without extension ###
# Ignore files with extension `.o` & `.so` & `.a`
*.o
*.so
*.a
a.out
# Ignore `bin` dir
bin/
# or
*/bin/*
.vscode/
#pragma once
#include <chrono>
namespace scprog
{
/// time measurement methods
class Timer
{
using value_type = double;
using Clock = std::chrono::high_resolution_clock;
using TimePoint = std::chrono::time_point<Clock>;
using fsec = std::chrono::duration<value_type>;
public:
/// initializes the timer with current time
Timer()
: t0(Clock::now())
{}
/// resets the timer to current time
void reset()
{
t0 = Clock::now();
}
/// returns the elapsed time (from construction or last reset) to now in seconds
value_type elapsed() const
{
auto t1 = Clock::now();
fsec fs = t1 - t0;
return fs.count();
}
private:
/// start time
TimePoint t0;
};
} // end namespace scprog
#include <iostream> // for std::cout
#include <cmath> // for std::sqrt
// Struct defines a point type
struct Point
{
double x, y;
};
/*
Distance of two points
*/
double distance(Point const& a, Point const& b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return std::sqrt(dx * dx + dy * dy); // consider operator precedence!
}
int main() // or int main(int argc, char** argv)
{
Point a{ 1.0, 2.0 };
Point b{ 7.0,-1.5 };
std::cout << "Distance of a and b = " << distance(a,b) << std::endl;
return 0;
}
// compile with
// g++ -o ex01_1 ex01_1.cc
#include <algorithm>
#include <ctime> // for std::time
#include <iostream> // for std::cout
#include <vector> // for std::vector
// Struct defines a point type
struct Point
{
double x, y;
};
// comparison operator for point types (lexicographical comparison)
bool operator<(Point const& p1, Point const& p2)
{
return std::make_pair(p1.x,p1.y) < std::make_pair(p2.x,p2.y);
}
int main()
{
// initialize random number generator
std::srand(std::time(0));
// fill vector of points with random coordinates in [0,1]
std::vector<Point> points(1e6);
std::generate(points.begin(), points.end(), [](){
return Point{ double(std::rand())/RAND_MAX, double(std::rand())/RAND_MAX };
});
// sort vector
std::sort(points.begin(), points.end());
// print the first 10 entries
for (std::size_t i = 0; i < 10; ++i)
std::cout << "point[" << i << "] = (" << points[i].x << ", " << points[i].y << ")" << std::endl;
return 0;
}
// compile with
// g++ [-std=c++11] -O[0-2] -o ex01_2 ex01_2.cc
struct X {
int i;
X(int); // constructor
X operator+(int); // X + int
};
struct Y {
int i;
Y(X); // constructor
Y operator+(X); // Y + X
operator int(); // cast-to-int operator
};
inline X operator*(X,Y); // X * Y
inline int f(X); // f(X) -> int
int main() {
X x = 1;
Y y = x;
int i = 2;
i + 10; y + 10; y + 10 * y;
x + y + i; x * x + i; f(7);
f(y); y + y; 106 + y;
}
#include <iostream>
namespace math {
double foo(double x) { return 2 * x; } // (a)
double foo2(double x) { return foo(x); } // (b)
}
namespace math1 {
double foo(double x) { return 3 * x; } // (c)
struct dd { double d; };
double foo(dd x) { return 4 * x.d; } // (d)
namespace ns {
double foo2(double x) { return foo(x); } // (e)
}
namespace ns2 {
double foo2(double x) { using math::foo; return foo(x); } // (f)
}
}
int main () {
using math::foo; using math1::ns::foo2;
std::cout << foo(6.0) << "\n"; // (1)
std::cout << foo2(6.0) << "\n"; // (2)
std::cout << math1::foo(6.0) << "\n"; // (3)
math1::dd x; x.d= 6.0;
std::cout << foo(x) << "\n"; // (4)
{
using namespace math;
std::cout << foo(6.0) << "\n"; // (5)
std::cout << foo2(6.0) << "\n"; // (6)
}
{
using math1::foo; using math::foo2;
std::cout << foo(6.0) << "\n"; // (7)
std::cout << foo2(6.0) << "\n"; // (8)
}
std::cout << foo(6.0) << "\n"; // (9)
std::cout << foo2(6.0) << "\n"; // (10)
std::cout << math1::ns2::foo2(6.0) << "\n"; // (11)
return 0;
}
namespace A {
struct X;
struct Y;
void f(int); // (a)
void g(X); // (b)
}
namespace B {
void f(int i) { // (c)
f(i); // which f()?
}
void g(A::X x) { // (d)
g(x); // which g()?
}
void h(A::Y y) { // (e)
h(y); // which h()?
}
}
class vector
{
vector(int s)
: s(s), data(new double[s]);
vector(vector const& that)
: s(that.s), data(new double[s])
{
for (int i= 0; i <= s; i++)
that.data[i] = data[i];
}
~vector<T>() { delete data; }
vector& operator=(vector const& that)
{
assert(s != that.s)
for (unsigned i= s-1; i >= 0; i--)
data[i]= that.data[i];
return this;
}
double const& operator[](unsigned i)
{
return data(i);
}
double& operator[](unsigned i)
{
return data(i);
}
unsigned size(const vector& v) { return v->s; }
private:
double const data[];
unsigned const s;
};
int main()
{
vector<int> v1(6);
vector v2(v1);
vector<int> v3(10);
v1 = v2;
v3 = v2;
v1(2) = 1;
const vector<double> v4(v2);
v4[2] = 1.0;
v4.size(v3);
return 0;
}
class complex {
public:
complex( double r, double i = 0 )
: real(r), imag(i)
{ }
void operator+ ( complex other ) {
real = real + other.real;
imag = imag + other.imag;
}
void operator<<( ostream os ) {
os << "(" << real << "," << imag << ")";
}
complex operator++() {
++real;
return *this;
}
complex operator++( int ) {
auto temp = *this;
++real;
return temp;
}
// ... more functions that complement the above ...
private:
double real, imag;
};
template<> void A<double>::foo() { std::cout << "foo-double\n"; }
template<typename T> void bar2() { std::cout << "bar2\n"; }
int main()
{
A<float> a;
a.foo();
bar1<float>();
bar1<double>();
bar2<double>();
bar3();
}
template<typename T> struct A { void foo(); };
template<typename T> void bar1();
template<typename T> void bar2();
void bar3();
template<typename T>
void A<T>::foo() { std::cout << "foo-T\n"; }
template<typename T> void bar1() { std::cout << "bar1\n"; }
void bar3() { bar1<double>(); }
template <class T1, class T2>
void f( T1, T2 ); // 1
template <class T> void f( T ); // 2
template <class T> void f( T, T ); // 3
template <class T> void f( T* ); // 4
template <class T> void f( T*, T ); // 5
template <class T> void f( T, T* ); // 6
template <class T> void f( int, T* ); // 7
template <> void f<int>( int ); // 8
void f( int, double ); // 9
void f( int ); // 10
int main()
{
int i;
double d;
float ff;
complex<double> c;
f( i ); // a
f<int>( i ); // b
f( i, i ); // c
f( c ); // d
f( i, ff ); // e
f( i, d ); // f
f( c, &c ); // g
f( i, &d ); // h
f( &d, d ); // i
f( &d ); // j
f( d, &i ); // k
f( &i, &i ); // l
}
SmartPointer<double> make_smart_pointer() {
return SmartPointer<double>{ new double };
}
int main() {
auto p = make_smart_pointer();
*p = 1.0;
auto q = p; // Is this meaningful?
*q = 2.0; // What are the effects on p?
}
#include <iostream>
namespace scprog
{
/// Range adaptor that maps the values by a given functor
template <class Functor, class Range>
class Map
{
public:
using value_type = /* type of the mapped elements of range? (optional) */
class iterator
{
public:
iterator(Functor f, /* iterator-type */ it)
: /* initialization */
{}
// implementation of operator++, operator==, and operator!=
auto operator*() const
{
return f_(*it_);
}
private:
Functor f_;
/* iterator-type */ it_;
};
public:
Map(Functor f, Range const& range)
: f_(f)
, it_(range.begin())
, end_(range.end())
{}
iterator begin() const
{
/* ... */
}
iterator end() const
{
/* ... */
}
private:
Functor f_;
/* iterator-type */ it_;
/* iterator-type */ end_;
};
/// Generator function for \ref Map
template <class Functor, class Range>
Map<Functor, Range> map(Functor f, Range const& range)
{
/* implementation here */
}
} // end namespace scprog
int main()
{
/* create container `vec` and initialize with some values */
for (auto x : map([](auto x) { return 2*x; }, vec))
std::cout << x << "\n";
}
#include <iostream>
template<int I>
struct Fibo
{
static const unsigned long long int value = Fibo<I-1>::value + Fibo<I-2>::value;
};
template<> struct Fibo<1> { static const unsigned long long int value = 1; };
template<> struct Fibo<0> { static const unsigned long long int value = 0; };
int main(int argc, char* argv[])
{
std::cout << "Fibo<0>::value = " << Fibo<0>::value << "\n";
std::cout << "Fibo<1>::value = " << Fibo<1>::value << "\n";
std::cout << "Fibo<2>::value = " << Fibo<2>::value << "\n";
std::cout << "Fibo<3>::value = " << Fibo<3>::value << "\n";
std::cout << "Fibo<4>::value = " << Fibo<4>::value << "\n";
std::cout << "Fibo<5>::value = " << Fibo<5>::value << "\n";
std::cout << "Fibo<6>::value = " << Fibo<6>::value << "\n";
std::cout << "Fibo<7>::value = " << Fibo<7>::value << "\n";
std::cout << "Fibo<8>::value = " << Fibo<8>::value << "\n";
std::cout << "Fibo<9>::value = " << Fibo<9>::value << "\n";
std::cout << "Fibo<10>::value = " << Fibo<10>::value << "\n";
std::cout << "Fibo<50>::value = " << Fibo<100>::value << "\n";
}
// 1) Compile with g++ / clang++ [g++ -Wall -pedantic -o fibo1 finonacci1.cc]
// 2) Compile with option [-S -o fibo.s] and have a look inside fibo.s!
// 3) Enable line 26 (Fibo<50>::value) and try again! (what is the problem?)
// 4) How to print all fibonacci numbers from 1 to 40? Write a generic loop!
#include <iostream>
template<int I>
struct Fibo
{
static const int value = Fibo<I-1>::value + Fibo<I-2>::value;
};
int main(int argc, char* argv[])
{
std::cout << "Fibo<5>::value = " << Fibo<5>::value << "\n";
}
// 1) compile with g++ / clang++ [g++ -Wall -pedantic -o fibo2 finonacci2.cc]
// What is the problem?
#include <iostream>
template<int I>
struct Fibo
{
static const unsigned long value = Fibo<I-1>::value + Fibo<I-2>::value;
};
template<> struct Fibo<1> { static const unsigned long value = 1; };
template<> struct Fibo<0> { static const unsigned long value = 0; };
int main(int argc, char* argv[])
{
std::cout << "Fibo<0>::value = " << Fibo<0>::value << "\n";
std::cout << "Fibo<1>::value = " << Fibo<1>::value << "\n";
std::cout << "Fibo<2>::value = " << Fibo<2>::value << "\n";
std::cout << "Fibo<3>::value = " << Fibo<3>::value << "\n";
std::cout << "Fibo<4>::value = " << Fibo<4>::value << "\n";
std::cout << "Fibo<5>::value = " << Fibo<5>::value << "\n";
std::cout << "Fibo<6>::value = " << Fibo<6>::value << "\n";
std::cout << "Fibo<7>::value = " << Fibo<7>::value << "\n";
std::cout << "Fibo<8>::value = " << Fibo<8>::value << "\n";
std::cout << "Fibo<9>::value = " << Fibo<9>::value << "\n";
std::cout << "Fibo<10>::value = " << Fibo<10>::value << "\n";
std::cout << "Fibo<50>::value = " << Fibo<50>::value << "\n";
// std::cout << "Fibo<93>::value = " << Fibo<93>::value << "\n";
}
// 1) Compile with g++ / clang++ [g++ -Wall -pedantic -o fibo3 finonacci3.cc]
#include <iostream>
template<int I>
struct Fibo
{
static const int value = Fibo<I-1>::value + Fibo<I-2>::value;
};
template<> struct Fibo<1> { static const int value = 1; };
template<> struct Fibo<0> { static const int value = 0; };
template<int I>
struct Fibo2
{
static const int value = Fibo2<I-1>::value + Fibo2<I-2>::value;
};
template<> struct Fibo2<1> { static const int value = 1; };
template<> struct Fibo2<0> { static const int value = 0; };
int main(int argc, char* argv[])
{
std::cout << "Fibo<5>::value = " << Fibo<5>::value << "\n";
std::cout << "Fibo2<5>::value = " << Fibo2<5>::value << "\n";
}
// 1) Compile with g++ / clang++ [g++ -Wall -pedantic -o fibo4 finonacci4.cc]
// What is the problem?
#include <iostream>
template<unsigned long I>
struct Fibo
{
enum { value = Fibo<I-1>::value + Fibo<I-2>::value };
// enum : unsigned long { value = Fibo<I-1>::value + Fibo<I-2>::value }; // C++11 only
};
template<> struct Fibo<1> { enum { value = 1 }; };
template<> struct Fibo<0> { enum { value = 0 }; };
int main(int argc, char* argv[])
{
std::cout << "Fibo<5>::value = " << Fibo<5>::value << "\n";
std::cout << "Fibo<50>::value = " << Fibo<50>::value << "\n";
}
// 1) Compile with g++ / clang++ [g++ -Wall -pedantic -o fibo5 finonacci5.cc]
// What is the problem?
#include <iostream>
constexpr unsigned long fibo(int n)
{
return n <= 0 ? 0 : ( n == 1 ? 1 : fibo(n-1) + fibo(n-2) );
}
template<int N>
struct Fibo { static const unsigned long value = fibo(N); };
int main(int argc, char* argv[])
{
std::cout << "Fibo<1>::value = " << Fibo<1>::value << "\n";
std::cout << "Fibo<5>::value = " << Fibo<5>::value << "\n";
std::cout << "Fibo<10>::value = " << Fibo<10>::value << "\n";
std::cout << "Fibo<50>::value = " << Fibo<50>::value << "\n";
std::cout << "fibo(50) = " << fibo(50) << "\n";
}
// 1) Compile with g++ [g++ -Wall -pedantic -std=c++11 -o fibo6 finonacci6.cc]
#include <iostream>
// Annahme: I <= N (müsste eigentlich explizit überprüft werden!)
template<long I, long N, class F>
struct FOR
{
static const typename F::value_type value = FOR<I+1, N, typename F::Next>::value;
};
// Abbruchbedingung I==N
template<long N, class F>
struct FOR<N, N, F>
{
static const typename F::value_type value = F::value;
};
// Hilfsstruktur für die eigentlich iterative Berechnung
template<typename T, T X, T Y>
struct FiboAux
{
typedef T value_type;
static const value_type value = X;
typedef FiboAux<T, X+Y, X> Next;
};
template<typename T, long N>
struct Fibo
{
static const T value = FOR<1, N, FiboAux<T, 1, 0> >::value;
};
int main(int argc, char* argv[])
{
std::cout << "Fibo<5>::value = " << Fibo<int, 5>::value << "\n";
std::cout << "Fibo<50>::value = " << Fibo<unsigned long, 50>::value << "\n";
}
// 1) Compile with g++ / clang++ [g++ -Wall -pedantic -o fibo7 finonacci7.cc]
#include <iostream>
typedef unsigned long value_type;
template <int u>
struct is_even
{
static const bool value = (u % 2 == 0);
};
template<int I> struct Fibo;
// f_(2*n) = f_n * (f_(n+1) + f_(n-1))