Assignment Search Framework
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
C++ Tips

This page is not intended to teach you C++, but can give you some quick tips on things that may be unfamiliar. Most of these will not matter for the implementation work you will have to do for the assignment, but may help when reading the code.

Templates

First, be aware that the code is intended to allow for quickly implementing new domains and running existing algorithms on them. As such, there is extensive use of templates. Classes that use templates begin their class definition with 'template<class T>'. This means that the defined code can be used for various different types. 'T' is just a standin for whatever type you will use. For example, the vector class is defined as vector<T> since it is a container that can hold just about anything. To create a vector for 'double' values, you would write vector<double>. Our class definitions would work similarly.

Of particular note, you may recall that in C one usually puts the function definition in a header file and the function implentation in another file (usually a '.c' file). Templated functions and classes don't allow for this (the reason why is technical), so the implmentations of these will be in the header file for such templated cases.

Inheritance

The codebase also uses inheritance to separate parts of the search algorithms that are in common. For example, in best_first_search.h, you can see the following:

template<class state_t, class action_t>
class BestFirstSearch: public SearchEngine<state_t, action_t>

This says that the BestFirstSearch class inherits from SearchEngine using template types state_t and action_t.

You will see functions designated as 'virtual', which means that the function can be overloaded in a child class, and when this is done, call the child's version of the function by default. If the function definition has '= 0' at the end, this means it is an abstract function in an abstract class.

Of note, mixing inheritance and templates can get a bit ugly at times. In particular, while one can usually access the parents protected or public member variables or functions from a child, you have to be a bit more explicit when templates are involved. For example, at the beginning of the definition of best_first_search.h you will see the following:

using SearchEngine<state_t, action_t>::goal_test;

This means that when 'goal_test' is used to in the BestFirstSearch code, it refers to the member variable 'goal_test' from the parent class SearchEngine.

Other

This section just contains a number of particular quick notes.

  • By default, functions take copies of an object when given a parameter. The '&' sign specifies that a reference should be used instead. For example, if the parameter is 'int &i' and 'i' is changed within the function, 'i' will remain changed after the function returns.
  • The number of bits used to denote different primitive datatypes can differ from system to system. When something more specific is desired, there are special data types for this. For example, 'uint64_t' is an unsigned 64-bit integer.
  • 'enum class' always for the definition of a finite set of constants. For example, it is used to define the type for moves in the sliding tile puzzle in tile_puzzle_transitions.h since there is only a very limited number of such moves.
  • 'typedef' allows you to give an alias to a type to make reading the code more easily. For example, in state_hash_function.h, you will see that a 'StateHash' is defined as an unsigned 64-bit integer. This allows our hash function to return a 'StateHash' instead of 'uint64_t'
  • 'const' is used throughout, and it is a keyword that means many different things depending on the context. This framework more commonly uses it in two contexts. At the end of a class function, like 'unsigned getValue() const', it means that the function will not affect any of the member variables of the function. In a parameter, it is most commonly used as 'void myFunction(const vector<unsigned> &v)', which means that 'v' is passed as a reference to the function, but can not be changed during the function.
  • Anything else should generally be Google-able.