Friday, 6 November 2015

Low-Level Programming_Post7

Low-Level Programming: Post 7 05/11/15

November 5th 2015 is the date of the seventh lesson on Low-Level Programming.

At the beginning of the lecture, we were given/shown, as per usual, an overview on the lecture for today. The following bullet points covered what content we were going to be shown today and what we would take away from it.
  • Pointers and Containers: Raw and Smart Pointers and their usage in Containers.
  • Containers: Sets, Maps, Multi-Sets, and Multi-Maps.
  • Algorithms: Find, Find_if and Binary Predicates.
  • State Systems: Game States and how a system might compose of them.
Unlike some of the previous lessons, there wasn't anything particularly 'unique' at the beginning (such as an assignment overview, Dev blogs reviewing and how it's marked etc). Instead we went straight into the lesson for the day.

On the first bullet and it's contents, we learnt about Pointers and Containers. We have already gone into some depth on Pointers before and it's varieties (Smart_Ptrs, Unique_Ptrs, Shared_Ptrs and Weak_Ptrs). However, unlike in previous blog posts where I move onto the next point, I am going to make an exception to this one, since it also goes into containers, vectors and how they seem to link together.

Vectors and Ptrs
  • Containers: It's acceptable and common to have containers of pointers. These can be traditional raw, unique or smart.
  • Considerations: With a Vector, the pointers are stored contiguously in memory.
  • Heap: They're on the heap, but the default allocator has made the decision on their location.
  • Cleanup: The Vector will free the memory it's allocated for the array contents, but any manual heap allocations need to be freed by you.
  • Elements: When you don't want to store copies of an object's array or pointers are a good choice.
Vectors and Unique_Ptrs
  • Unique Ptrs: Storing a vector of unique ptrs allows us to resolve the deallocation issues.
  • Ownership: Remember the pointer is unique and will remove the data on the heap regardless of any other pointers or refs.
  • Memory Location: Much the same as raw pointers, we just have the wrapper that provides us with deallocation.
Vectors and Shared_Ptrs
  • Shared Ptrs: Sometimes we might want to store a container of reference counted ptrs.
  • Shared Ownership: Multiple reference to the same data can be controlled.
  • Central Storage: It's possible to return a shared pointer that can be used elsewhere in the game, but also stored in a central container.
Associative Containers
  • Associative: At the moment the container types we've been looking at are all unsorted with no association between the data orders.
  • Key Values: The key which stores the associated value is used to maintain the sort appropriate to the container.
  • Elements: The key values are how we can look up the elements in the container.
Set
  • Sets: Containers that store unique elements following a specific order.
  • Unique: Sets only allow one entry per unique key.
  • Key: The value of the object being inserted into the container also dictates the unique key.
  • Sorted: The items are sorted using a comparison object, by default this is the less than operation.
  • Insertions: Sets are good at insertions, regardless of the location the new object is being added, unlike vectors and deques.
  • Binary Search Tree: Underlying principles are based on binary search trees.
Set Vs Vector
  • Ordered: A set will always retain it's order, a vector will need to be sorted to guarantee it's order.
  • Vector Sort: Sorting a vector can be slow, especially if you need to repeat the process on a frequent basis.
  • Proportional: The time it takes an item to insert into a vector is proportional to the number of items in it. The set is proportional to the log.
  • Initialisation: If you add all the items upfront and then sort one, the performance is negated.
  • Iteration: Faster on a vector than on a set.
  • Set Overhead: Needs to store additional information for tree traversal. Insertions only faster if you're not inserting to the end of a vector.
Find
  • STL: There's an algorithm include that's part of the STL and allows a linear search of a container.
  • Sets: In associative container like sets, they include their own find algorithm that performs logarithmically instead.
  • Usage: You can use either the associative container version of the STL version depending on your requirements. 
  • Binary Search: If you have a sorted vector, you can perform a binary search using lower or upper bounds.
Find_If
  • STL: Much like remove_if which requires a predicate you can use a find_if which needs a unary predicate.
  • Boolean: It takes a single parameter and returns a boolean to determine whether the element should be selected/highlighted.
  • Usage: The associative container does not provide this function, it's in the algorithms section.
Map
  • Indexed Key: Our lookup key is separate from the value stored in the array.
  • Pairs: They use pairs, which has two members, first and second. The first is the key.
  • Set Overhead: Fast insertions and logarithmic lookup times and like set, unique values must be used.
Multi-Set
  • Sets: Work the same as a set. The difference is that the key does not need to be unique, we can have duplicates.
  • Vector Sort: Sorting a vector can be slow, especially if you need to repeat the process on a frequent basis.
  • Search Trees: Again, often implement as binary search trees.
  • Binary Predicate: We can provide a custom sort predicate that will take both objects and perform a comparison.
  • Function Ptr: Basically, you're passing a pointer to the specialised sort function.
  • Operator Overload: Alternatively we can provide an operator overloaded function that provides a custom sort.
  • Clarity: Using the operator overloader could be considered clearer than passing a function pointer through and is automagically called.
Multi-Map
  • Multiple Keys: The Multi-Map allows a separate lookup value to the element. With a Multi-Map duplicate, keys are now supported.
  • Usage: If you need to store multiple instances of objects but need to look them up by a key value, then this is the container. Caches which use hashes are one example.
State
  • Game: In most games, the logic being process is dependent on the current state of the game. (Running -> Jump Button -> Jumping -> Reach apex of jump -> Falling etc).
  • Game Loop: On simple games, you often find that all the logic may be in a single loop i.e. the game loop.
  • Modular: If instead we look at our games as a series of states, we can more easily control what's been updated or checked every update.
  • Specialised: Using states allows us to keep the logic required for updating our objects more succinct and specialised.
  • State Machines: Conceptually the idea of differeing states and transitioning between them is handled by our state machine.
State Behaviours
  • State-Dependent Behaviour: State machines can be used in any part of our game, where the design lends itself well to processing the logic in a series of states.
At the end of the Straight-forward lectural side of the lesson (and once the 15 min break finished), the practical side of the lesson began. The practical once again saw us try and work on the DungeonDeath game. Since I wasn't working alone on the game this week, we had an attempt to change the player to have a unique_ptr, which I think we succeeded at. We were also about to attempt to move the static array of rooms into vectors of levels, but we ran out of time.

No comments:

Post a Comment