From 5122667c1aa43d9577dacc02311262427f11d198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20L=C3=A4ufer?= Date: Sun, 10 Dec 2023 02:21:53 +0000 Subject: [PATCH] removed modern-cpp subdirectory (now separate repo), resolved #20 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Konstantin Läufer --- modern-cpp/CMakeLists.txt | 7 --- modern-cpp/auto-mva.cpp | 38 -------------- modern-cpp/auto-with-stl.cpp | 42 ---------------- modern-cpp/cli11-mva.cpp | 38 -------------- modern-cpp/const-constexpr.cpp | 16 ------ modern-cpp/mc-triangle-threads.cpp | 79 ------------------------------ modern-cpp/mc-triangle.cpp | 49 ------------------ modern-cpp/move-mvp.cpp | 41 ---------------- modern-cpp/point-struct.cpp | 59 ---------------------- modern-cpp/point.h | 44 ----------------- modern-cpp/point_demo.cpp | 29 ----------- modern-cpp/random-hist.cpp | 35 ------------- modern-cpp/range1.cpp | 24 --------- modern-cpp/range1.h | 8 --- modern-cpp/range2.cpp | 15 ------ modern-cpp/sleep-coroutine.cpp | 74 ---------------------------- modern-cpp/weak_ptr.cpp | 43 ---------------- 17 files changed, 641 deletions(-) delete mode 100644 modern-cpp/CMakeLists.txt delete mode 100644 modern-cpp/auto-mva.cpp delete mode 100644 modern-cpp/auto-with-stl.cpp delete mode 100644 modern-cpp/cli11-mva.cpp delete mode 100644 modern-cpp/const-constexpr.cpp delete mode 100644 modern-cpp/mc-triangle-threads.cpp delete mode 100644 modern-cpp/mc-triangle.cpp delete mode 100644 modern-cpp/move-mvp.cpp delete mode 100644 modern-cpp/point-struct.cpp delete mode 100644 modern-cpp/point.h delete mode 100644 modern-cpp/point_demo.cpp delete mode 100644 modern-cpp/random-hist.cpp delete mode 100644 modern-cpp/range1.cpp delete mode 100644 modern-cpp/range1.h delete mode 100644 modern-cpp/range2.cpp delete mode 100644 modern-cpp/sleep-coroutine.cpp delete mode 100644 modern-cpp/weak_ptr.cpp diff --git a/modern-cpp/CMakeLists.txt b/modern-cpp/CMakeLists.txt deleted file mode 100644 index 43752ab..0000000 --- a/modern-cpp/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -##arget_link_libraries(matrix_mul_dpc fmt::fmt spdlog::spdlog) - -#add_executable(mc-triangle-threads mc-triangle-threads.cpp) -#add_executable(mc-triangle mc-triangle.cpp) -#add_executable(random-hist random-hist.cpp) -#add_executable(range1 range1.cpp range1.h) -#add_executable(range2 range2.cpp) diff --git a/modern-cpp/auto-mva.cpp b/modern-cpp/auto-mva.cpp deleted file mode 100644 index 13e4ac0..0000000 --- a/modern-cpp/auto-mva.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -int main() { - int i = 42; - double d1 = 1.23, d2 = 4.56; - float f1 = 0.123f, f2 = 6.789f; - bool b1 = true, b2 = false; - char c1 = 'c', c2 = 'd'; - - auto v1 = std::vector {1, 2, 3, 4, 5}; - auto l1 = std::list {d1, d2, d1, d2, d1}; - auto m1 = std::map {{f1, b1}, {f2, b2}, {f1, b2}}; - auto s1 = std::set {c1, c2, c1, c2}; - auto str1 = std::string{"Hello, world!"}; - - fmt::print("i = {}\n", i); - fmt::print("d1 = {}\n", d1); - fmt::print("d2 = {}\n", d2); - fmt::print("f1 = {}\n", f1); - fmt::print("f2 = {}\n", f2); - fmt::print("b1 = {}\n", b1); - fmt::print("b2 = {}\n", b2); - fmt::print("c1 = '{}'\n", c1); - fmt::print("c2 = '{}'\n", c2); - fmt::print("v1 = {}\n", fmt::join(v1, ", ")); - fmt::print("l1 = {}\n", fmt::join(l1, ", ")); - fmt::print("m1 = {{{}, {}}}\n", m1.begin()->first, m1.begin()->second); - fmt::print("s1 = {{{}}}\n", fmt::join(s1, ", ")); - fmt::print("str1 = \"{}\"\n", str1); - - return 0; -} diff --git a/modern-cpp/auto-with-stl.cpp b/modern-cpp/auto-with-stl.cpp deleted file mode 100644 index 0508605..0000000 --- a/modern-cpp/auto-with-stl.cpp +++ /dev/null @@ -1,42 +0,0 @@ -.. code-block:: cpp - -#include -#include -#include -#include -#include -#include -#include - -int main() { - auto i{42}; - auto d1{1.23}, d2{4.56}; - auto f1{0.123f}, f2{6.789f}; - auto b1{true}, b2{false}; - auto c1{'c'}, c2{'d'}; - - auto v1{std::vector{1, 2, 3, 4, 5}}; - auto l1{std::list{d1, d2, d1, d2, d1}}; - auto m1{std::map{{f1, b1}, {f2, b2}, {f1, b2}}}; - auto s1{std::set{c1, c2, c1, c2}}; - auto str1{std::string{"Hello, world!"}}; - - fmt::print("i = {}\n", i); - fmt::print("d1 = {}\n", d1); - fmt::print("d2 = {}\n", d2); - fmt::print("f1 = {}\n", f1); - fmt::print("f2 = {}\n", f2); - fmt::print("b1 = {}\n", b1); - fmt::print("b2 = {}\n", b2); - fmt::print("c1 = '{}'\n", c1); - fmt::print("c2 = '{}'\n", c2); - fmt::print("v1 = {}\n", fmt::join(v1, ", ")); - fmt::print("l1 = {}\n", fmt::join(l1, ", ")); - fmt::print("m1 = {{{}, {}}}\n", m1.begin()->first, m1.begin()->second); - fmt::print("s1 = {{{}}}\n", fmt::join(s1, ", ")); - fmt::print("str1 = \"{}\"\n", str1); - - return 0; -} - - diff --git a/modern-cpp/cli11-mva.cpp b/modern-cpp/cli11-mva.cpp deleted file mode 100644 index d27324d..0000000 --- a/modern-cpp/cli11-mva.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include "CLI/CLI.hpp" -#include "fmt/format.h" - -int main(int argc, char* argv[]) { - CLI::App app{"Command-line interface example"}; - - // Add arguments for each built-in data type - int i = 42; - app.add_option("-i,--int", i, "Integer argument"); - - double d = 3.14; - app.add_option("-d,--double", d, "Double argument"); - - float f = 2.71; - app.add_option("-f,--float", f, "Float argument"); - - bool b = true; - app.add_flag("-b,--bool", b, "Boolean argument"); - - std::string s = "Hello, world!"; - app.add_option("-s,--string", s, "String argument"); - - // Parse the command-line arguments - CLI11_PARSE(app, argc, argv); - - // Print the parsed arguments - fmt::print("Parsed arguments:\n"); - fmt::print(" int: {}\n", i); - fmt::print(" double: {}\n", d); - fmt::print(" float: {}\n", f); - fmt::print(" bool: {}\n", b); - fmt::print(" string: {}\n", s); - return 0; -} - - diff --git a/modern-cpp/const-constexpr.cpp b/modern-cpp/const-constexpr.cpp deleted file mode 100644 index 82a9e9f..0000000 --- a/modern-cpp/const-constexpr.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include - -constexpr double pythagorean(double a, double b) { - return std::sqrt(a * a + b * b); -} - -int main() { - const double a = 3.0; - const double b = 4.0; - constexpr double c = pythagorean(a, b); - - std::cout << "The hypotenuse is " << c << std::endl; - - return 0; -} diff --git a/modern-cpp/mc-triangle-threads.cpp b/modern-cpp/mc-triangle-threads.cpp deleted file mode 100644 index 1975b09..0000000 --- a/modern-cpp/mc-triangle-threads.cpp +++ /dev/null @@ -1,79 +0,0 @@ -// -// Created by George K. Thiruvathukal on 3/19/23. -// - -#include -#include -#include -#include -#include -#include - -int main() { - // define the triangle - double x1 = 0, y1 = 0; - double x2 = 1, y2 = 1; - double x3 = 0, y3 = 1; - - // set up random number generator - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_real_distribution<> dis(0, 1); - - // number of random points to generate - int n = 1000000; - - // number of threads to use - int num_threads = std::thread::hardware_concurrency(); - std::cout << "Using " << num_threads << " threads\n"; - - // initialize the count of points inside the triangle - std::vector counts(num_threads, 0); - - // define the work function for each thread - auto work_function = [&](int thread_id) { - // calculate the number of points to generate in this thread - int num_points = n / num_threads; - if (thread_id == num_threads - 1) { - num_points += n % num_threads; - } - - // generate random points and count the ones inside the triangle - for (int i = 0; i < num_points; ++i) { - double x = dis(gen); - double y = dis(gen); - - // calculate the cross products - double cp1 = (x2 - x1) * (y - y1) - (y2 - y1) * (x - x1); - double cp2 = (x3 - x2) * (y - y2) - (y3 - y2) * (x - x2); - double cp3 = (x1 - x3) * (y - y3) - (y1 - y3) * (x - x3); - - // if the cross products have the same sign, the point is inside the triangle - if ((cp1 >= 0 && cp2 >= 0 && cp3 >= 0) || (cp1 <= 0 && cp2 <= 0 && cp3 <= 0)) { - ++counts[thread_id]; - } - } - }; - - // create the threads and start the work - std::vector threads(num_threads); - for (int i = 0; i < num_threads; ++i) { - threads[i] = std::thread(work_function, i); - } - - // join the threads - std::for_each(threads.begin(), threads.end(), [](std::thread& t) { - t.join(); - }); - - // sum up the counts from all the threads - int count = std::accumulate(counts.begin(), counts.end(), 0); - - // calculate the area of the triangle using Monte Carlo method - double area = count / static_cast(n) * (x2 - x1) * (y3 - y1); - - // output the estimated area - std::cout << "Estimated area: " << area << '\n'; - - return 0; -} diff --git a/modern-cpp/mc-triangle.cpp b/modern-cpp/mc-triangle.cpp deleted file mode 100644 index a18f68a..0000000 --- a/modern-cpp/mc-triangle.cpp +++ /dev/null @@ -1,49 +0,0 @@ -// -// Created by George K. Thiruvathukal on 3/19/23. -// - -#include -#include -#include - -int main() { - // define the triangle - double x1 = 0, y1 = 0; - double x2 = 1, y2 = 1; - double x3 = 0, y3 = 1; - - // set up random number generator - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_real_distribution<> dis(0, 1); - - // number of random points to generate - int n = 1000000; - - // initialize the count of points inside the triangle - int count = 0; - - // generate random points and check if they are inside the triangle - for (int i = 0; i < n; ++i) { - double x = dis(gen); - double y = dis(gen); - - // calculate the cross products - double cp1 = (x2 - x1) * (y - y1) - (y2 - y1) * (x - x1); - double cp2 = (x3 - x2) * (y - y2) - (y3 - y2) * (x - x2); - double cp3 = (x1 - x3) * (y - y3) - (y1 - y3) * (x - x3); - - // if the cross products have the same sign, the point is inside the triangle - if ((cp1 >= 0 && cp2 >= 0 && cp3 >= 0) || (cp1 <= 0 && cp2 <= 0 && cp3 <= 0)) { - ++count; - } - } - - // calculate the area of the triangle using Monte Carlo method - double area = count / static_cast(n) * (x2 - x1) * (y3 - y1); - - // output the estimated area - std::cout << "Estimated area: " << area << '\n'; - - return 0; -} diff --git a/modern-cpp/move-mvp.cpp b/modern-cpp/move-mvp.cpp deleted file mode 100644 index cbb469c..0000000 --- a/modern-cpp/move-mvp.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include - -class MyClass { -public: - MyClass() { - std::cout << "Default constructor" << std::endl; - data_ = new int[10]; - } - - ~MyClass() { - std::cout << "Destructor" << std::endl; - delete[] data_; - } - - // Move constructor - MyClass(MyClass&& other) noexcept { - std::cout << "Move constructor" << std::endl; - data_ = other.data_; - other.data_ = nullptr; - } - - // Move assignment operator - MyClass& operator=(MyClass&& other) noexcept { - std::cout << "Move assignment operator" << std::endl; - delete[] data_; - data_ = other.data_; - other.data_ = nullptr; - return *this; - } - -private: - int* data_; -}; - -int main() { - MyClass a; - MyClass b(std::move(a)); // move a to b - MyClass c = std::move(b); // move b to c - return 0; -} diff --git a/modern-cpp/point-struct.cpp b/modern-cpp/point-struct.cpp deleted file mode 100644 index ab8a4a7..0000000 --- a/modern-cpp/point-struct.cpp +++ /dev/null @@ -1,59 +0,0 @@ -class Point { -public: - Point(double x = 0.0, double y = 0.0, double z = 0.0) - : coords{x, y, z} {} - - // Copy constructor - Point(const Point& other) - : coords{other.coords} {} - - // Move constructor - Point(Point&& other) noexcept - : coords { - std::move(other.coords) - } {} - - // Copy assignment operator - Point& operator=(const Point& other) { - coords = other.coords; - return *this; - } - - // Move assignment operator - Point& operator=(Point&& other) noexcept { - coords = std::move(other.coords); - return *this; - } - - // Accessors - double x() const { - return coords.x; - } - double y() const { - return coords.y; - } - double z() const { - return coords.z; - } - - // Scale the Point by a double factor - void scale(double factor) { - coords.x *= factor; - coords.y *= factor; - coords.z *= factor; - } - - // Translate the Point by another Point - void operator+=(const Point& other) { - coords.x += other.coords.x; - coords.y += other.coords.y; - coords.z += other.coords.z; - } - -private: - struct Coords { - double x, y, z; - }; - - Coords coords; -}; diff --git a/modern-cpp/point.h b/modern-cpp/point.h deleted file mode 100644 index 9e2ddf1..0000000 --- a/modern-cpp/point.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef POINT_H -#define POINT_H - -class Point { -public: - Point(double x = 0.0, double y = 0.0, double z = 0.0) - : x_{x}, y_{y}, z_{z} {} - - // Copy constructor - Point(const Point& other) - : x_{other.x_}, y_{other.y_}, z_{other.z_} {} - - // Move constructor - Point(Point&& other) noexcept - : x_{std::exchange(other.x_, 0.0)}, - y_{std::exchange(other.y_, 0.0)}, - z_{std::exchange(other.z_, 0.0)} {} - - // Copy assignment operator - Point& operator=(const Point& other) { - x_ = other.x_; - y_ = other.y_; - z_ = other.z_; - return *this; - } - - // Move assignment operator - Point& operator=(Point&& other) noexcept { - x_ = std::exchange(other.x_, 0.0); - y_ = std::exchange(other.y_, 0.0); - z_ = std::exchange(other.z_, 0.0); - return *this; - } - - // Accessors - double x() const { return x_; } - double y() const { return y_; } - double z() const { return z_; } - -private: - double x_, y_, z_; -}; -#endif // POINT_H - diff --git a/modern-cpp/point_demo.cpp b/modern-cpp/point_demo.cpp deleted file mode 100644 index de1d54c..0000000 --- a/modern-cpp/point_demo.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include "point.h" - -int main() { - // Create a point object with x=1.0, y=2.0, z=3.0 - Point p1(1.0, 2.0, 3.0); - - // Copy the point object - Point p2 = p1; - - // Move the point object - Point p3 = std::move(p1); - - // Output the values of the point objects - std::cout << "p1: (" << p1.x() << ", " << p1.y() << ", " << p1.z() << ")" << std::endl; - std::cout << "p2: (" << p2.x() << ", " << p2.y() << ", " << p2.z() << ")" << std::endl; - std::cout << "p3: (" << p3.x() << ", " << p3.y() << ", " << p3.z() << ")" << std::endl; - - // Update the values of the point objects - p2 = Point(4.0, 5.0, 6.0); - p3 = std::move(p2); - - // Output the updated values of the point objects - std::cout << "p1: (" << p1.x() << ", " << p1.y() << ", " << p1.z() << ")" << std::endl; - std::cout << "p2: (" << p2.x() << ", " << p2.y() << ", " << p2.z() << ")" << std::endl; - std::cout << "p3: (" << p3.x() << ", " << p3.y() << ", " << p3.z() << ")" << std::endl; - - return 0; -} diff --git a/modern-cpp/random-hist.cpp b/modern-cpp/random-hist.cpp deleted file mode 100644 index d53e5b7..0000000 --- a/modern-cpp/random-hist.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by George K. Thiruvathukal on 3/19/23. -// - - -// Use modern C++ to randomly generate a million floating point values in the range 0 to 1 and bucket them in 0.1 increments? - -#include -#include -#include -#include -#include - -int main() { - std::vector buckets(10, 0); // initialize all buckets to 0 - - // set up random number generator - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_real_distribution<> dis(0, 1); - - // generate a million random numbers and bucket them - for (int i = 0; i < 1000000; ++i) { - double value = dis(gen); // generate a random number between 0 and 1 - int bucket = std::floor(value * 10); // calculate the bucket index - buckets[bucket]++; // increment the count in the corresponding bucket - } - - // print the bucket counts - for (int i = 0; i < 10; ++i) { - std::cout << i << " - " << (i + 1) / 10.0 << ": " << buckets[i] << '\n'; - } - - return 0; -} diff --git a/modern-cpp/range1.cpp b/modern-cpp/range1.cpp deleted file mode 100644 index 37dd3f1..0000000 --- a/modern-cpp/range1.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// -// Created by George K. Thiruvathukal on 3/19/23. -// - -#include "range1.h" - -#include -#include - -// Write a loop in modern C++ to count the integers up to 100000. -// This is stateful: Initializes vector and the performs the sum over the range. - -int main() { - std::vector range(100000); - for (int i = 0; i < 100000; ++i) { - range[i] = i + 1; - } - - for (int i : range) { - std::cout << i << '\n'; - } - - return 0; -} diff --git a/modern-cpp/range1.h b/modern-cpp/range1.h deleted file mode 100644 index d3f6c1f..0000000 --- a/modern-cpp/range1.h +++ /dev/null @@ -1,8 +0,0 @@ -// -// Created by George K. Thiruvathukal on 3/19/23. -// - -#ifndef UNOAPI_DPCPP_EXAMPLES_RANGE1_H -#define UNOAPI_DPCPP_EXAMPLES_RANGE1_H - -#endif //UNOAPI_DPCPP_EXAMPLES_RANGE1_H diff --git a/modern-cpp/range2.cpp b/modern-cpp/range2.cpp deleted file mode 100644 index 36f88ab..0000000 --- a/modern-cpp/range2.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// -// Created by George K. Thiruvathukal on 3/19/23. -// - -#include -#include - -// This is kind of wasteful... - -int main() { - for (int i : std::vector(100000)) { - std::cout << i + 1 << '\n'; - } - return 0; -} diff --git a/modern-cpp/sleep-coroutine.cpp b/modern-cpp/sleep-coroutine.cpp deleted file mode 100644 index deb61ad..0000000 --- a/modern-cpp/sleep-coroutine.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include -#include -#include -#include - -// A simple coroutine that sleeps for a random amount of time -// and returns the amount of time slept - -class SleepCoroutine { -public: - SleepCoroutine() = default; - - // The coroutine promise type - struct promise_type { - auto get_return_object() { - return SleepCoroutine{handle_type::from_promise(*this)}; - } - std::suspend_never initial_suspend() { - return {}; - } - std::suspend_always final_suspend() noexcept { - return {}; - } - void unhandled_exception() { - std::terminate(); - } - void return_void() {} - }; - - // The coroutine handle type - using handle_type = std::coroutine_handle; - - // The coroutine execution function - void operator()() { - // Generate a random sleep time - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_int_distribution<> distr(1000, 5000); - int sleep_time = distr(gen); - - // Sleep for the random time - std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time)); - } - -private: - SleepCoroutine(handle_type h) : handle(h) {} - handle_type handle; -}; - -// A function that creates and runs four sleep coroutines -void run_sleep_coroutines() { - // Create a vector to hold the coroutines - std::vector coroutines; - - // Create and start the coroutines - for (int i = 0; i < 4; i++) { - coroutines.push_back(SleepCoroutine{}()); - } - - // Join the coroutines - for (auto& coroutine : coroutines) { - coroutine.resume(); - coroutine.destroy(); - } -} - -// The main function -int main() { - run_sleep_coroutines(); - std::cout << "All coroutines joined." << std::endl; - return 0; -} - diff --git a/modern-cpp/weak_ptr.cpp b/modern-cpp/weak_ptr.cpp deleted file mode 100644 index 4a4b664..0000000 --- a/modern-cpp/weak_ptr.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include - -int main() { - // Create a shared pointer to an integer - std::shared_ptr ptr = std::make_shared(42); - - // Create a weak pointer to the integer - std::weak_ptr weak_ptr = ptr; - - // Check if the weak pointer is expired - if (weak_ptr.expired()) { - std::cout << "The weak pointer is expired." << std::endl; - } else { - std::cout << "The weak pointer is not expired." << std::endl; - } - - // Reset the shared pointer - ptr.reset(); - - // Check if the weak pointer is expired again - if (weak_ptr.expired()) { - std::cout << "The weak pointer is expired." << std::endl; - } else { - std::cout << "The weak pointer is not expired." << std::endl; - } - - // Create a new shared pointer to a different integer - std::shared_ptr new_ptr = std::make_shared(99); - - // Reset the weak pointer to the new shared pointer - weak_ptr = new_ptr; - - // Check if the weak pointer is expired - if (weak_ptr.expired()) { - std::cout << "The weak pointer is expired." << std::endl; - } else { - std::cout << "The weak pointer is not expired." << std::endl; - } - - return 0; -} -