Helper classes

There are various useful classes.

Keeping track of states

template <class Key, class Value, bool hasDefault = false, bool alwaysContain = false>
class flipsta::Map

Associative map from Key to Value.

This performs roughly the same function as standard unordered associative containers, but with a different interface.

All operations are amortised constant time.

Normally this wraps a std::unordered_map. However, if hasDefault and alwaysContain are both true, and Key is Dense<>, then a std::vector is used. This should be faster by a constant factor in cases where a key space is dense.

Templates
  • Key -

    The type of the key. boost::hash must be implemented for this type.

  • Value -

    The type of the value.

  • hasDefault -

    If true, there is a default value, which will be assumed for keys that are not in the map. This behaviour is well-known from std::map, where default-construction is used. Here, on the other hand, the default value is specified and stored explicitly. If hasDefault == true, operator[] returns a const reference, because it might return a reference to the default value.

  • alwaysContain -

    Hint that most keys that will be inserted will actually be dense, starting close to zero. If Key type is dense, this may then use a vector instead of a hash map. This gives a constant-factor speedup.

Public Functions

Map()

Initialise empty.

This is only available for hasDefault == false.

Map(Value const & defaultValue)

Initialise with a default value.

This is only available for hasDefault == true.

Parameters
  • defaultValue -

    The value that will be returned for keys that are not in the map.

template <class Range, class Enable = typename boost::enable_if <range::is_range <Range>>::type>
Map(Range && initialValues)

Initialise with the initial values in initialValues.

This is only available for hasDefault == false.

Parameters
  • initialValues -

    Initial content of the map, in the form of a range of key-value pairs. If any key occurs more than once, the last entry is retained.

template <class Range, class Enable = typename boost::enable_if <range::is_range <Range>>::type>
Map(Value const & defaultValue, Range && initialValues)

Initialise with a default value, and with the initial values in initialValues.

This is only available for hasDefault == true.

Parameters
  • defaultValue -

    The value that will be returned for keys that are not in the map.

  • initialValues -

    Initial content of the map, in the form of a range of key-value pairs. If any key occurs more than once, the last entry is retained.

bool contains(Key const & key) const

Return Whether the map contains key.

If alwaysContain == true, the return type is rime::true_type.

void set(Key const & key, Value const & value)

Set the value associated to key key to value.

If the key is not in the map, it is inserted. If the key is already in the map, the value is replaced by value.

Value const & operator[](Key const & key) const

Return a const-reference to the value corresponding to key.

Pre
If !hasDefault: key must be already in the map.

WithDefault::DefaultReference operator[](Key const & key)

Return a reference to the value corresponding to key.

If hasDefault, this is a const reference, because it may be to the default value.

Pre
If !hasDefault: key must be already in the map.

void remove(Key const & key)

Remove the value from the map.

If alwaysContain == true, reset the value to the default.

Pre
key must be in the map.

template <class Element>
class flipsta::LifoQueue

Last-in, first-out queue.

This implements a stack: elements that are pushed onto this queue last are popped off first.

Public Functions

bool empty() const

Return whether this queue is empty.

void push(Element const & element)

Push an element onto the queue.

Element const & head() const

Return the next element that will be returned by pop().

Does not remove the element.

Pre
!empty().

Element & head()

Return a reference to the next element that will be returned by pop().

Does not remove the element.

Pre
!empty().

Element pop()

Pop an element off the stack.

This is the last element that was pushed onto the stack that has not yet been popped off.

Pre
!empty().

template <class Type>
class flipsta::Dense

Wrap an integer type promising that the space of values will be dense.

This means that all values will be non-negative, and the values that are used will be dense and close to zero.

This allows parts of Flipsta to use, essentially, array indexing instead of looking keys up in hash maps.

This class has implicit conversions to Type, comparison operators, and it works with boost::hash.

Exception types

The Flipsta library uses Boost.Exception for exception reporting. The following shows how to throw exceptions:

#include "flipsta/error.hpp"

// ...

// Throw error, attaching information about the state .
throw flipsta::Error() << flipsta::errorInfoState <State> (5);

This is how they can be caught:

#include <boost/exception/get_error_info.hpp>
#include "flipsta/error.hpp"

// ...

try {
    // ... Operation that may throw an exception.
} catch (boost::exception & error) // or catch a more specific type.
{
    std::cerr << "An error has occurred.\n";
    // See whether information about the state is attached.
    State const * state = boost::get_error_info <
        flipsta::TagErrorInfoStateType <State>::type> (error);
    // If there is, report it.
    if (state)
        std::cerr << "The error concerned state " << *state << '\n';
}
struct flipsta::Error

Base exception class for Flipsta.

struct flipsta::StateNotFound

Exception that indicates that a state was not found.

struct flipsta::StateExists

Exception that indicates that a state already exists.

struct flipsta::AutomatonNotAcyclic

Exception that indicates that an automaton is not acyclic.

template <class State>
struct flipsta::TagErrorInfoState

Tag for boost::error_info that indicates a state id.

template <class State>
struct flipsta::TagErrorInfoStateType

Table Of Contents

Previous topic

Defining automata

Next topic

Mathematical tools library

This Page