OpenTTD Source 20241224-master-gf74b0cf984
SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size > Class Template Reference

Minimal stack that uses a pool to avoid pointers. More...

#include <smallstack_type.hpp>

Inheritance diagram for SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >:
SmallStackItem< Titem, Tindex >

Data Structures

struct  PooledSmallStack
 SmallStack item that can be kept in a pool. More...
 

Public Types

typedef SmallStackItem< Titem, Tindex > Item
 
typedef SimplePool< PooledSmallStack, Tindex, Tgrowth_step, Tmax_size > SmallStackPool
 

Public Member Functions

 SmallStack (const Titem &value=Tinvalid)
 Constructor for a stack with one or two items in it.
 
 ~SmallStack ()
 Remove the head of stack and all other items members that are unique to it.
 
 SmallStack (const SmallStack &other)
 Shallow copy the stack, marking the first item as branched.
 
SmallStackoperator= (const SmallStack &other)
 Shallow copy the stack, marking the first item as branched.
 
void Push (const Titem &item)
 Pushes a new item onto the stack if there is still space in the underlying pool.
 
Titem Pop ()
 Pop an item from the stack.
 
bool IsEmpty () const
 Check if the stack is empty.
 
bool Contains (const Titem &item) const
 Check if the given item is contained in the stack.
 
- Public Member Functions inherited from SmallStackItem< Titem, Tindex >
 SmallStackItem (const Titem &value, Tindex next)
 Create a new item.
 

Protected Member Functions

void Branch ()
 Create a branch in the pool if necessary.
 

Static Protected Member Functions

static SmallStackPoolGetPool ()
 

Additional Inherited Members

- Data Fields inherited from SmallStackItem< Titem, Tindex >
Tindex next
 Pool index of next item.
 
Titem value
 Value of current item.
 

Detailed Description

template<typename Titem, typename Tindex, Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
class SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >

Minimal stack that uses a pool to avoid pointers.

It has some peculiar properties that make it useful for passing around lists of IDs but not much else:

  1. It always includes an invalid item as bottom.
  2. It doesn't have a deep copy operation but uses smart pointers instead. Every copy is thus implicitly shared.
  3. Its items are immutable.
  4. Due to 2. and 3. memory management can be done by "branch counting". Whenever you copy a smallstack, the first item on the heap increases its branch_count, signifying that there are multiple items "in front" of it. When deleting a stack items are deleted up to the point where branch_count > 0.
  5. You can choose your own index type, so that you can align it with your value type. E.G. value types of 16 bits length like to be combined with index types of the same length.
  6. All accesses to the underlying pool are guarded by a mutex and atomic in the sense that the mutex stays locked until the pool has reacquired a consistent state. This means that even though a common data structure is used the SmallStack is still reentrant.
    Template Parameters
    TitemValue type to be used.
    TindexIndex type to use for the pool.
    TinvalidInvalid item to keep at the bottom of each stack.
    Tgrowth_stepGrowth step for pool.
    Tmax_sizeMaximum size for pool.

Definition at line 135 of file smallstack_type.hpp.

Member Typedef Documentation

◆ Item

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
typedef SmallStackItem<Titem, Tindex> SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::Item

Definition at line 138 of file smallstack_type.hpp.

◆ SmallStackPool

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
typedef SimplePool<PooledSmallStack, Tindex, Tgrowth_step, Tmax_size> SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::SmallStackPool

Definition at line 147 of file smallstack_type.hpp.

Constructor & Destructor Documentation

◆ SmallStack() [1/2]

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::SmallStack ( const Titem &  value = Tinvalid)
inline

Constructor for a stack with one or two items in it.

Parameters
valueInitial item. If not missing or Tinvalid there will be Tinvalid below it.

Definition at line 153 of file smallstack_type.hpp.

◆ ~SmallStack()

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::~SmallStack ( )
inline

Remove the head of stack and all other items members that are unique to it.

Definition at line 158 of file smallstack_type.hpp.

References SmallStackItem< Titem, Tindex >::next, and SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::Pop().

◆ SmallStack() [2/2]

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::SmallStack ( const SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size > &  other)
inline

Shallow copy the stack, marking the first item as branched.

Parameters
otherStack to copy from

Definition at line 168 of file smallstack_type.hpp.

References SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::Branch().

Member Function Documentation

◆ Branch()

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
void SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::Branch ( )
inlineprotected

◆ Contains()

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
bool SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::Contains ( const Titem &  item) const
inline

Check if the given item is contained in the stack.

Parameters
itemItem to look for.
Returns
If the item is in the stack.

Definition at line 253 of file smallstack_type.hpp.

References SimplePool< Titem, Tindex, Tgrowth_step, Tmax_size >::Get(), lock, SmallStackItem< Titem, Tindex >::next, and SmallStackItem< Titem, Tindex >::value.

Referenced by VehicleCargoList::ChooseAction().

◆ GetPool()

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
static SmallStackPool & SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::GetPool ( )
inlinestaticprotected

Definition at line 269 of file smallstack_type.hpp.

◆ IsEmpty()

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
bool SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::IsEmpty ( ) const
inline

◆ operator=()

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
SmallStack & SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::operator= ( const SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size > &  other)
inline

Shallow copy the stack, marking the first item as branched.

Parameters
otherStack to copy from
Returns
This smallstack.

Definition at line 175 of file smallstack_type.hpp.

References SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::Branch(), SmallStackItem< Titem, Tindex >::next, SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::Pop(), and SmallStackItem< Titem, Tindex >::value.

◆ Pop()

◆ Push()

template<typename Titem , typename Tindex , Titem Tinvalid, Tindex Tgrowth_step, Tindex Tmax_size>
void SmallStack< Titem, Tindex, Tinvalid, Tgrowth_step, Tmax_size >::Push ( const Titem &  item)
inline

The documentation for this class was generated from the following file: