Friday, 20 November 2015

Session 8

Do not use windows.h
Windows.h bloats codespace (possible code duplication, slower performance etc)
You also lose portability using windows.h as it will only work with Windows systems
Also avoid using console commands like system(" ")
Instead, to sleep (pause), use: std::this_thread::sleep_for(std::chrono::milliseconds(frameWait));
In C++14 and higher, literals can be used if you #include chronos, you can write 5s for 5 seconds, 10ms for 10 milliseconds, etc.
Polling for input also - getting key input

Clear screen - can't use system commands so an alternative is to simply print enough new lines to replace all onscreen.
#ifndef is a pre-processing statement, happens before any compilation
If using C++ 11 or higher there is a C++ specific clear screen function that can be used
As well as a Visual Studio-specific one...
#ifndef _MSC_VER to use Visual Studio version
must #endif

Auto - the auto keyword can be used to save time.
Auto deduces the correct variable type, like float/int/char etc based on what follows.
For example, a number with a decimal point should be classed as a float, if auto is used
Only safe to use when you are certain of what the computer will treat the variable as

example syntax of using auto in a for loop:
for(auto& player : players)
{
//for every player in players
}

Weak pointers can only be created from shared pointers
Weak pointers cannot influence when an item gets destroyed, hence 'weak'
Locking a weak pointer creates a shared version for that line (temporarily)

Big O notation
O = order
Complexity refers to how the numbers of inputs impact the processing time
The order is the rate of growth
T(n) = O(n^2) where T is time, and grows in the order of n^2, this is an exponential growth
An order of magnitude lets us see the scale or magnitude of the algorithm in a nice, easy to follow visual example - no need to see the numbers explicitly
Given x number, how does that impact processing time

Algorithm - sort of like a recipe, it is a set of rules that govern a series of calculations or problem-solving solutions
Every line, operation or formation can be considered an algorithm as it tells the computer what to do
Algorithms are unambiguous, very specific

In a constant expression, time taken will never change
In a linear expression, time taken increasing with more inputs can be represented using a straight line
Polynomial expressions show a steep curve in growth
Still all just a visual representation without explicit values
Logarithmic expressions are the opposite of polynomial; diminishing returns (smaller difference between inputs, the more inputs there are)

Sunday, 8 November 2015

Session 7

Vectors and Pointers

A vector is a container, you can put pointers in vectors
Pointers are stored contiguously but the memory it points to is on the heap, allocated by default
The memory that is pointed to is thus not guaranteed to be contiguous
If a vector pointer points to some information that was manually allocated, it will have to be manually deallocated too

When you don't want to share copies of objects, arrays or pointers are good
If you do copy, a change will only affect one and it will cause slowdown
Push-back is used to add memory to the end of a container
Must deallocate the pointers before clearing the vector
Unique pointers, however, will deallocate themselves, but any information can only have 1 unique pointer pointing to it
level3.push-back(RoomUniPointer((new _____)) example unique pointer declaration syntax

Using shared pointers, use std::make_shared(room x)
Shared pointers guarantee that the pointers will stay valid even if the container is cleared
Can use a weak pointer to make a cache that can't be cleared
Weak pointers must be connected to be used

Associative containers are related to what is in them
For example, a container of strings would then have the properties of a string
A container of strings could be sorted alphabetically
It can also be searched by name, as opposed to being searched by its number
Until now, a container's order was only determined by what order the items were added

A set is like a container but it can only hold unique 'keys'
Sets are also in a specific order
The value of the key also determines how unique ?
Sorting in a set uses a less-than comparison (lowest first)
Sorting in a vector is slower, but it still works
Vector sorting has to go through every item to make sure it is in the right place, and this must be done every time a new item is added too
Iteration is faster in a vector though as it is simply added to the end
Sets need to know only what is to the left and right of the current item, making for faster sorting
Sets also do not have to shift all items left or right when an item is added or removed
In a set, the value is the key, which is also the element

Find:set is a logarithmic search, whereas find:std is a linear search
find_if requires a boolean predicate; a yes or no state
Associative containers cannot do this
In a map, the lookup key is separate from the value stores in the array
Pair - look up first, return second. A string is usually the key
This has the same overheard as a set

Multi-sets can have duplicates, but they are the same as a set otherwise
Same for multi-maps
Useful when caching where hash might be duplicated
No real reason to use...

Monday, 2 November 2015

Session 6

Shared pointers - access same memory with different pointers
A weak pointer checks if the data that a shared pointer points to is valid

Casting is making a variable act like a different type of variable (for one single operation), for example, you can tell the computer to interpret a number as a char instead. The char output should be the equivalent of the number... based on ASCII value?
Implicit (implied) conversions are automatically performed when a value is copied to a compatible type

Libraries are good because they're made by the experts; saves time and effort

When a vector is created, 3 pointers are also made - a beginning, end and last used. Example: array of size 10, 1-7 are in use. There will be pointers to 1, 10 and 7 as beginning end and last (in that order).
Size vs capacity - the size is the number of objects, whereas capacity is how much space there is for objects altogether
Consider using vectors instead of arrays as they are fast and efficient, but similar to use (ease)

A contiguous memory block benefits caching and prevents fragmentation
An iterator is any object that points to an element in a container, like ++ and -- (increment and decrement)

Erase-remove process, must do both. Remove-erase is the correct order...
Linked list - loads of nodes that link to each other
Linked list can go backwards and forwards
Memory is allocated node by node, and can be assigned anywhere but if it isn't contiguous it might end up going to RAM (which is slower)
As long as there are pointers to the next item, that item can be anything at all
Removal - nodes removed have their memory location automatically deallocated
Easy to add or remove new items by breaking chains within linked lists
Link lists are good for performance as there is no overhead other than the allocation/deallocation
Adding and removing new items is easy because other items don't have to be shifted left or right

Double-ended containers can be expanded or contracted at either end
They are similar to vectors but items may be modified at the front and end
Efficient for adding/removing new items
Memory is not guaranteed to be contiguous.