John Bailey / FixedLengthList

Dependents:   FixedLengthListTest XBeeApi

Embed: (wiki syntax)

« Back to documentation index

FixedLengthList< T, queueMax > Class Template Reference

FixedLengthList< T, queueMax > Class Template Reference

Template class to implement a list with a fixed maximum number of elements (i.e. More...

#include <FixedLengthList.hpp>

Public Member Functions

 FixedLengthList (void)
 Constructor for FixedLengthList.
 FixedLengthList (const T *const p_items, size_t p_count)
 Initialising constructor for FixedLengthList.
bool push (const T p_item)
 push an item onto the front of the list
bool pop (T *const p_item)
 pop an item from the front of the list (item is removed and returned
bool queue (const T p_item)
 queue an item onto the end of the list
bool dequeue (T *const p_item)
 dequeue an item from the end of the list (item is removed and returned
bool remove (const T p_item)
 Remove the first node matching p_item from the list.
size_t used () const
 Used to find out how many items are in the list.
size_t available () const
 Used to find out how many slots are still available in the list.
bool inList (const T p_val) const
 Determine whether or not a particular item is in the list.
void clear (void)
 Remove the entire contents of the list and return it back to an empty state.

Detailed Description

template<class T, size_t queueMax>
class FixedLengthList< T, queueMax >

Template class to implement a list with a fixed maximum number of elements (i.e.

the number of elements in the list is variable but cannot exceed a defined maximum).

While stl::list may be suitable for many occasions, it invariably makes use of dynamic memory allocation which can either be undesirable or overkill in some situations. Of course, the down side of this class is that the memory required to store all items in the list is allocated for the duration of the instantiation.

The implementation is based around a singly linked list with a stack of free elements. Adding an item to the list causes one of the free elements to be popped, populated then inserted into the linked list of used elements. For convenience both a head and tail pointer of the used list are maintained.

Note that the class currently is not thread safe.

Example:

          #define LIST_LEN (20U)
          FixedLengthList<int,  LIST_LEN > list;

          int main( void ) {
             int i;
             
             // List is empty
             
             list.queue( 111 );
             // List now contains 111
             
             list.queue( 222 );
             // List now contains 111, 222
             
             list.push( 333 );
             // List now contains 333, 111, 222
          
             list.pop( &i );
             // i == 333
             // List now contains 111, 222
          
             return 0;    
          }

Definition at line 152 of file FixedLengthList.hpp.


Constructor & Destructor Documentation

FixedLengthList ( void   )

Constructor for FixedLengthList.

Definition at line 279 of file FixedLengthList.hpp.

FixedLengthList ( const T *const   p_items,
size_t  p_count 
)

Initialising constructor for FixedLengthList.

Parameters will be used to initialise the list

Parameters:
p_itemsAn array of items used to initialise the list. They will be added in the order in which they appear in p_items
p_countThe number of items in p_items. Only up to queueMax items will be used - any additional items will be ignored

Definition at line 285 of file FixedLengthList.hpp.


Member Function Documentation

size_t available (  ) const

Used to find out how many slots are still available in the list.

Returns:
Number of available slots, ranging from 0 to queueMax

Definition at line 586 of file FixedLengthList.hpp.

void clear ( void   )

Remove the entire contents of the list and return it back to an empty state.

Definition at line 343 of file FixedLengthList.hpp.

bool dequeue ( T *const   p_item )

dequeue an item from the end of the list (item is removed and returned

Parameters:
p_itemPointer to be populated with the value of the item
Returns:
true in the case that an item was returned false in the case that an item was not returned (list empty)

Definition at line 471 of file FixedLengthList.hpp.

bool inList ( const T  p_val ) const

Determine whether or not a particular item is in the list.

Parameters:
p_valItem to be matched against
Returns:
true in the case that the item is found in the list false in the case that it is not found in the list

Definition at line 592 of file FixedLengthList.hpp.

bool pop ( T *const   p_item )

pop an item from the front of the list (item is removed and returned

Parameters:
p_itemPointer to be populated with the value of the item
Returns:
true in the case that an item was returned false in the case that an item was not returned (list empty)

Definition at line 440 of file FixedLengthList.hpp.

bool push ( const T  p_item )

push an item onto the front of the list

Parameters:
p_itemThe item to be added to the list
Returns:
true in the case that the item was added false in the case that the item was not added (no space)

Definition at line 366 of file FixedLengthList.hpp.

bool queue ( const T  p_item )

queue an item onto the end of the list

Parameters:
p_itemThe item to be added to the list
Returns:
true in the case that the item was added false in the case that the item was not added (no space)

Definition at line 399 of file FixedLengthList.hpp.

bool remove ( const T  p_item )

Remove the first node matching p_item from the list.

In the case that there are multiple matches, only the first is removed.

Parameters:
p_itemValue to be removed
Returns:
true in the case that an item was matched and remove false in the case that no item could be matched

Definition at line 530 of file FixedLengthList.hpp.

size_t used (  ) const

Used to find out how many items are in the list.

Returns:
Number of used items, ranging from 0 to queueMax

Definition at line 580 of file FixedLengthList.hpp.