diff --git a/configuration/nogui/meson.build b/configuration/nogui/meson.build index f03f127bcbea5cea39e81553fa82438374c484e1..5f00880c76a7aed2368ac6882af50a34cc85f2e9 100644 --- a/configuration/nogui/meson.build +++ b/configuration/nogui/meson.build @@ -15,4 +15,4 @@ endif src = ['main.cpp'] -exe = executable('program', src, dependencies : [animationwindow_dep, sdl2_dep], cpp_args : compiler_flags) +exe = executable('program', src, dependencies : [std_lib_facilities_dep], cpp_args : compiler_flags) diff --git a/dependencies/subprojects/animationwindow/include/Widget.h b/dependencies/subprojects/animationwindow/include/Widget.h index 1d796fadb6a8868f207df79c9764fcab65b88515..a18db1df1cc00a5382931b8029e2e71bc5bf29ee 100644 --- a/dependencies/subprojects/animationwindow/include/Widget.h +++ b/dependencies/subprojects/animationwindow/include/Widget.h @@ -32,5 +32,8 @@ namespace TDT4102 { void setCallback(std::function<void(void)> callback); virtual ~Widget() {} void setVisible(bool isVisible); + unsigned int getWidth() const; + unsigned int getHeight() const; + void setSize(unsigned int newWidth, unsigned int newHeight); }; } \ No newline at end of file diff --git a/dependencies/subprojects/animationwindow/src/Color.cpp b/dependencies/subprojects/animationwindow/src/Color.cpp index 78d61533e97a89950a0e3f3425ead681e020edf4..222c01e3b87b1b20e5b41de1010fc8fbaaba89a4 100644 --- a/dependencies/subprojects/animationwindow/src/Color.cpp +++ b/dependencies/subprojects/animationwindow/src/Color.cpp @@ -21,7 +21,8 @@ const TDT4102::Color TDT4102::Color::navy = TDT4102::Color{0x000080}; const TDT4102::Color TDT4102::Color::blue = TDT4102::Color{0x0000ff}; const TDT4102::Color TDT4102::Color::teal = TDT4102::Color{0x008080}; const TDT4102::Color TDT4102::Color::aqua = TDT4102::Color{0x00ffff}; -const TDT4102::Color TDT4102::Color::orange = TDT4102::Color{0xffa500}; +const TDT4102::Color TDT4102::Color::orange = TDT4102::Color{0xffa500}; +const TDT4102::Color TDT4102::Color::mid_gray = TDT4102::Color{0x6a6c6e}; const TDT4102::Color TDT4102::Color::alice_blue = TDT4102::Color{0xf0f8ff}; const TDT4102::Color TDT4102::Color::antique_white = TDT4102::Color{0xfaebd7}; const TDT4102::Color TDT4102::Color::aquamarine = TDT4102::Color{0x7fffd4}; diff --git a/dependencies/subprojects/animationwindow/src/Widget.cpp b/dependencies/subprojects/animationwindow/src/Widget.cpp index 12d23ee627931ed57864b4f3e4d505a5e55de769..97f6ff27264e0d39bbff2a6fbce50400e6fd875b 100644 --- a/dependencies/subprojects/animationwindow/src/Widget.cpp +++ b/dependencies/subprojects/animationwindow/src/Widget.cpp @@ -18,4 +18,17 @@ TDT4102::Widget::Widget(TDT4102::Point location, unsigned int widgetWidth, unsig void TDT4102::Widget::setVisible(bool visible) { isVisible = visible; +} + +unsigned int TDT4102::Widget::getWidth() const { + return width; +} + +unsigned int TDT4102::Widget::getHeight() const { + return height; +} + +void TDT4102::Widget::setSize(unsigned int newWidth, unsigned int newHeight) { + width = newWidth; + height = newHeight; } \ No newline at end of file diff --git "a/infobank/Articles/\303\230ving_9_bokm\303\245l.pdf" "b/infobank/Articles/\303\230ving_9_bokm\303\245l.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..7c7f1f5081467028e497ce85e622c396abfe604c Binary files /dev/null and "b/infobank/Articles/\303\230ving_9_bokm\303\245l.pdf" differ diff --git a/templates/_folder_Exercises/O10/Tile.cpp b/templates/_folder_Exercises/O10/Tile.cpp index 0d46d1b88d8100b9ada5f0c15255d9d2a4b3115b..729eaed76ed29f91fc781564ebcfaf52ac6d23d2 100644 --- a/templates/_folder_Exercises/O10/Tile.cpp +++ b/templates/_folder_Exercises/O10/Tile.cpp @@ -1,15 +1,6 @@ #include "Tile.h" -#include <map> -// For aa sette labelfarge i henhold til hvor mange miner som er rundt -const std::map<int, TDT4102::Color> minesToColor{{1, TDT4102::Color::blue}, - {2, TDT4102::Color::red}, - {3, TDT4102::Color::dark_green}, - {4, TDT4102::Color::dark_magenta}, - {5, TDT4102::Color::dark_blue}, - {6, TDT4102::Color::dark_cyan}, - {7, TDT4102::Color::dark_red}, - {8, TDT4102::Color::gold}}; + // For aa sette Tilelabel i henhold til state const std::map<Cell, std::string> cellToSymbol{{Cell::closed, ""}, diff --git a/templates/_folder_Exercises/O10/Tile.h b/templates/_folder_Exercises/O10/Tile.h index cbe9a282aa546b56616ad342cb102674c34ce6fb..fa5754b846944f69339c55f49967754e0a34e767 100644 --- a/templates/_folder_Exercises/O10/Tile.h +++ b/templates/_folder_Exercises/O10/Tile.h @@ -1,6 +1,7 @@ #pragma once #include "AnimationWindow.h" #include "widgets/Button.h" +#include <map> // De forskjellige tilstandene en Tile kan vaere i enum class Cell { closed, open, flagged }; @@ -10,6 +11,17 @@ class Tile : public TDT4102::Button Cell state = Cell::closed; void set_label(std::string s) { this->setLabel(s);} void set_label_color(TDT4102::Color c) {this->setLabelColor(c);} + + // For aa sette labelfarge i henhold til hvor mange miner som er rundt + const std::map<int, TDT4102::Color> minesToColor{{1, TDT4102::Color::blue}, + {2, TDT4102::Color::red}, + {3, TDT4102::Color::dark_green}, + {4, TDT4102::Color::dark_magenta}, + {5, TDT4102::Color::dark_blue}, + {6, TDT4102::Color::dark_cyan}, + {7, TDT4102::Color::dark_red}, + {8, TDT4102::Color::gold}}; + public: Tile(TDT4102::Point pos, int size); diff --git a/templates/_folder_Lectures/Lecture 13/parallel1_main.cpp b/templates/_folder_Lectures/Lecture 13/parallel1_main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b04d24ab6a90470f454e62ec98441e2d8f65a9e0 --- /dev/null +++ b/templates/_folder_Lectures/Lecture 13/parallel1_main.cpp @@ -0,0 +1,98 @@ + +#include "std_lib_facilities.h" + +template<typename T> +void print(T a) { + std::cout << a << std::endl; +} + +template<typename T> +struct NotAVector { + T a; +}; + +#include <queue> +#include <deque> + +struct Vector2 { + float x; + float y; + + Vector2 operator+(Vector2& a) { + Vector2 out; + out.x = x + a.x; + out.y = y + a.y; + return out; + } +}; + +int main(int argc, char** argv) { + std::vector<std::string> parameters; + for(int i = 0; i < argc; i++) { + parameters.push_back(std::string(argv[i])); + std::cout << "Parameter " << i << ": " << parameters.at(i) << std::endl; + } + + + + std::queue<std::string> koo; + + koo.push("Number one"); + koo.push("Number two"); + koo.push("Number three"); + + while(!koo.empty()) { + std::cout << koo.front() << std::endl; + koo.pop(); + } + + std::deque<std::string> d; + + std::vector<int> list = {1, 6, 3, 8, 5, 3, 7, 4}; + + std::vector<Vector2> vecList = {{2, 5}, {2, 7}, {65, 87}}; + + Vector2 sum = std::accumulate(vecList.begin(), vecList.end(), Vector2{0, 0}); + + std::random_device device; + std::default_random_engine engine(device()); + std::shuffle(list.begin(), list.end(), engine); + + int a = 5; + int b = 4; + + //std::swap(a, b); + + //std::min(a, b); + //std::max(a, b); + std::sort(list.begin(), list.end()); + + std::fill(list.begin(), list.end(), 55); + + std::vector<int> anotherList(100); + std::copy(list.begin(), list.end(), anotherList.begin() + 5); + + anotherList.erase(anotherList.begin() + 3, anotherList.begin() + 5); + + + + + for(std::vector<int>::reverse_iterator it = list.rbegin(); it != list.rend(); it++) { + std::cout << *it << std::endl; + } + + //NotAVector<int> vec; + print<std::string>("a"); + print<int>(10); + print(10.0); + + std::array<int, 5> array; + std::map<std::string, int> aMap; + + std::vector<int> vec(10); + std::unique_ptr<int*> test; + std::map<std::string, int> map; + + return 0; +} + diff --git a/templates/_folder_Lectures/Lecture 13/parallel2_STL_main.cpp b/templates/_folder_Lectures/Lecture 13/parallel2_STL_main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c1f59e2e780b0baa81c0e5015950ea9c94908d5 --- /dev/null +++ b/templates/_folder_Lectures/Lecture 13/parallel2_STL_main.cpp @@ -0,0 +1,150 @@ +// Lecture 13, Examples 3 and 4: STL containers and algorithms, Dragana Laketic + +#include "std_lib_facilities.h" + +#include <queue> +#include <stack> +#include <list> +#include <algorithm> +#include <cassert> + +int main() { + // STL containers + std::queue<int> queue; // Queue is a First-In-First-Out (FIFO) data + // structure. In STL C++ standard library it is + // implemented as a template. + queue.push(1); // We use push() method to enqueue an element in queue + queue.push(2); + queue.push(3); + + while(!queue.empty()) { // The value of an element of a queue which is + // first to leave the queue when we call pop() method + // can be accessed by front() method. + cout << queue.front() << endl; + queue.pop(); + } + assert(queue.size() == 0); // repetition from last lecture: use of assert + // to make sure all the elements of the queue have left + // the queue (have been popped from the queue). + + std::stack<string> stack; // Stack is a Last-In-First-Out data structure. That means + // that the last element placed on stack will be the first to + // leave the stack when we call stack's pop() method. + stack.push("First on Stack"); + stack.push("Second on Stack"); + stack.push("Third on Stack"); + + while(!stack.empty()) { + cout << stack.top() << endl; // We use top() method to get the values of + // the element which is placed last on stack. + // It will be the first element to leave the stack + // when we call pop() method. + stack.pop(); + } + // Note how we use push() and pop() methods for two different containers - + // a queue and a stack - but how these methods work in accordance to the + // definition of each of them, i.e., the definition of a FIFO and a definition + // of a LIFO data structure. + + + // Back to vector. Although we know it very well, now we understand + // a "bigger picture" as well: it is yet another template container + // class from STL library. + std::vector<int> vec{50, 70, 20, 40, 80, 10, 90}; + // Usually we traverse its elements in a for-loop like this: + for (int i = 0; i < vec.size(); i++) { + cout << vec.at(i) << endl; // Here we use a range-checked method at() + // to access the vector's elements. + } + cout << endl; + + // Here is an example of traversing the same vector with the use + // of iterators. Iterators are members of STL containers which contain + // a pointer to an element of the container and thereby enable us to + // access the values stored in the container's elements. + // Note that STL containers also provide begin() and end() methods + // which return an iterator which points to the first element of + // the container and to the location after the last element of the + // container. In other words, an STL container contains the elements in + // the interval [STLcontainer.begin(), STLcontainer.end()) - where + // mathematical notation for open and close limit of the interval applies. + cout << "Vector traversed with iterator: " << endl; + // Pay attention to how the iterator type is declared! It reflects + // the instantiated template of a container! + for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { + cout << *it << endl; + } + cout << endl; + + std::list<int> lst{1, 2, 3}; // Linked lists are containers which use pointers to connect + // the elements into it. There are two types of lists: + // singly linked lists and doubly linked lists. An element + // of a singly linked list beside the data value contains also + // a pointer to the next element in the list, while the doubly + // linked lists elements contain a pointer to a next element in + // the list and a pointer to the previous element in the list. + // With each insersion of a new element, search for an element of + // certain value or erasure of an element, the pointers to some + // memory locations where individual elements are stored must also + // be handled. This all takes much of the computing resources and + // increases the time needed for processing. Therefore, unless really + // necesary within the application at hand (or the task at the exam), + // they are advised to be avoided. Use vectors instead and + // whenever you can! + + cout << "List with iterator: " << endl; + // Pay attention to how the type of the iterator is declared in this case! + // Compare it with the iterator type from the vector traversal example! + for (std::list<int>::iterator it = lst.begin(); it != lst.end(); it++) { + cout << *it << endl; + } + cout << endl; + + // There exist also a reverse iterator which is analogous to an iterator + // discussed above with the distinction in the direction of the traversal + // which is illustrated in the following example: + cout << "Reverse iterator:" << endl; + for (std::list<int>::reverse_iterator rit = lst.rbegin(); rit != lst.rend(); rit++) { + cout << *rit << endl; + } + cout << endl; + + // STL algorithms + + // Iterators are especially handy when it comes to using algorithms from + // the STL standard library. + + // An example of calling the sort() algorithm for the first 4 + // elements of the vector vec defined above: + std::sort(vec.begin(), vec.begin() + 4); + cout << "Partially sorted vector:" << endl; + for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { + cout << *it << endl; + } + cout << endl; + + // An example of using an algorithm from the STL library which + // processes the elements of two different types of containers. + // We provide iterators to the copy algorithm and invoke the copying + // of list lst elements from the first to the last to the first element + // of the vector and the subsequent ones: + std::copy(lst.begin(), lst.end(), vec.begin()); + cout << "Vector after copying from the list:" << endl; + for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { + cout << *it << endl; + } + cout << endl; + // Note that the checks of the container limits are not performed + // so you need to take care of it. Test what would happen if instead + // of a vec.begin() you provided vec.end() / vec.end()-1 / vec.end()-2. + + // Am example of fill algorithm: + std::fill(vec.begin(), vec.begin()+5, 500); + cout << "Vector after fill:" << endl; + for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { + cout << *it << endl; + } + cout << endl; + + return 0; +} diff --git a/templates/_folder_Lectures/Lecture 13/parallel2_main_main.cpp b/templates/_folder_Lectures/Lecture 13/parallel2_main_main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cccd13ed9601d8fa9578b09a5c9b685c1116a0d --- /dev/null +++ b/templates/_folder_Lectures/Lecture 13/parallel2_main_main.cpp @@ -0,0 +1,39 @@ +// Lecture 13, Example 4: main() function, Dragana Laketic +#include <iostream> +#include <string> +#include <vector> + +// Extenden version of main() function has two arguments: +// int argc - total number of program arguments +// char** argv - array of strings which contains the arguments +// (char**-style is inherited from C programming language and +// stands for an array of char* variables (strings) where each +// char* element contains a string. In C, a string was represented +// as an array of char variables with special character '\0' +// at the end to denote the end of string.) + + // Run this program from the command line with some arguments listed, like this: + // <path_to_your_project>/builddir/ ./program 1001 textfile.txt binary + // What are the arguments written out in the terminal? + // How can you use these values passed to your program? +int main(int argc, char** argv) { + std::vector<std::string> arguments; + + // Each program has at least one argument and that + // is the location - the full path - of the program file within + // the file system where program is run. + std::cout << "I am a program with " << argc << " arguments:" << std::endl; + + // We can store arguments from argv array in a more C++-friendly + // manner, like in the example below within a vector of strings. + for (int i = 0; i < argc; i++) { + arguments.push_back(argv[i]); // Note how we use index operator + // on array variable argv. + } + // Her skriver vi ut argumenter fra vektoren: + for (int i = 0; i < arguments.size(); i++) { + std::cout << "Argument " << i << ": " << arguments.at(i) << std::endl; + } + + return 0; +} diff --git a/templates/_folder_Lectures/Lecture 13/parallel2_template_Points.h b/templates/_folder_Lectures/Lecture 13/parallel2_template_Points.h new file mode 100644 index 0000000000000000000000000000000000000000..6d6a0b47a73590d4f186bf5f30aacbdd7e490d57 --- /dev/null +++ b/templates/_folder_Lectures/Lecture 13/parallel2_template_Points.h @@ -0,0 +1,82 @@ +// Lecture 13, Example 1: templates, Dragana Laketic + +#pragma once +#include "std_lib_facilities.h" + +// Templates enable the creation of classes and functions which have +// the same interface for different data types. The interface is +// written only once with the data type(s) passed in as parameters. +// Beside one or more data type parameters, there can also be values of +// some other type passed in to a template - most often it is integer +// values. +// When a template class or a template function is used in the program, +// we need to provide parameters - data type(s) and/or values - for the +// concrete class or function. When compiler comes across a templated +// class or function in the prgram file, it instantiates the corresponding +// template with exactly those parameters provided in the program. It is +// worth noting that this type-resolution and template instantiation +// happens at compile time. +// We keep template definitions - template class, template class member functions +// and template functions - within a header file. This is because it is only a template +// and the instantiated classes and functions, those which will be actually used +// in the program, are made by the compiler at compilation time. + + // 1) An example of a class template + // With template we use a keyword 'template' followed by + // keyword 'typename' and some name (as of our choice) between + // less than and greater than signs. The name of a typename parameter + // can be whatever you choose but often it is 'T' or some variation + // of it for data types. +template<typename T> +class Point{ + // Where ever there is 'T' in the template class definition + // there will be a concrete data type set by the compiler when + // it comes across our invocation of the templated class with + // that concrete parameter passed between '<' and '>'. + T x; + T y; +public: + Point(T xx, T yy) : x{xx}, y{yy} {} + T getX() const {return x;} + T getY() const {return y;} + void printMe() { + cout << "(" << x << ", " << y << ")" << endl; + } +}; + + // 2) An example of a template class which takes more than one parameter +template<typename T1, typename T2> +class ChoppedPoint { + T1 x; + T2 y; +public: + ChoppedPoint(T1 xx, T2 yy) : x{xx}, y{yy} {} + void printMe() { + cout << "(" << x << ", " << y << ")" << endl; + } +}; + + // 3) An example of a function template +template<typename TT> // note: here we use TT (our free choice) + //after 'typename' +void PrintValue(TT var) { + cout << "Template function: " << endl; + cout << var << endl; +} + + // 4) An example of integer value as a template parameter + // (compare with std::array<> where we also pass in an integer value + // as a template parameter) +template<typename T, int N> +class Collection { + T* collection; +public: + Collection() { + cout << "Constructor of template<typename T, int N> class Collection" << endl; + collection = new T[N]; + } + ~Collection() { + cout << "Destructor of template<typename T, int N> class Collection" << endl; + delete[] collection; + } +} diff --git a/templates/_folder_Lectures/Lecture 13/parallel2_template_main.cpp b/templates/_folder_Lectures/Lecture 13/parallel2_template_main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..115b784ec619feeb5e4ecf78ed58c670d5f74ed6 --- /dev/null +++ b/templates/_folder_Lectures/Lecture 13/parallel2_template_main.cpp @@ -0,0 +1,41 @@ +// Lecture 13, Example 1: templates, Dragana Laketic + +#include "std_lib_facilities.h" +#include "Points.h" + +int main() { + // 1) Class template + Point<int> pointI{1, 3}; // Here the compiler instantiates template class Point + // to 'int' data type instead of generic 'T' ... + Point<double> pointD{10.5, 5.5}; // ... and here to 'double'. + + cout << "X coordinate int Point: " << pointI.getX() << endl; + pointD.PrintMe(); + + // 2) Class template with more than one typename parameter + ChoppedPoint<int, double> pointC{4, 7.5}; + pointC.PrintMe(); + + // 3) Function template + cout << "Function template:" << endl; + PrintValue<int>(5); // Providing explicitly data type to instantiate + // a function template with. + PrintValue<string>("ABC"); // Explicit instantiation of the function template + // with data type 'string'. + PrintValue(100); // An example of so-called template argument deduction when + // a compiler deduces which type the function template should + // be instantiated with based on the argument passed to a function. + // Here it was a simple case of a built-in data type int, but + // beware that for more complex types, for example user-defined + // classes, it is recommended providing an explicit data type + // for the function template instantiation like in the two examples + // above. + + // 4) Class template with one data type and one integer as parameters + // Note: during lecture we mentioned such case for std::array but here + // is an example of the use of a user-defined template with such parameters + Collection<int, 10> coll; // Note: for exercise, extend class Collection with + // some functions and call those functions ofr coll object. + + return 0; +}