Simple Collections
Loading...
Searching...
No Matches
tccollection::BtreeList< K, V > Class Template Reference

#include <SimpleCollections.h>

Public Member Functions

 BtreeList (bsize_t size=DEFAULT_LIST_SIZE, GrowByMode howToGrow=DEFAULT_GROW_MODE)
 BtreeList (ioaTreeInternal::KeyAccessor customAccessor, bsize_t size=DEFAULT_LIST_SIZE, GrowByMode howToGrow=DEFAULT_GROW_MODE)
bool add (const V &item)
V * getByKey (K key) const
bool removeByKey (K key)
void removeIndex (bsize_t index)
bsize_t nearestLocation (const K &key) const
const V * items () const
V * itemAtIndex (bsize_t idx) const
bsize_t count () const
bsize_t capacity () const
void clear ()
BtreeIterator< V > end () const

Static Public Member Functions

static uint32_t keyAccessor (const void *itm)
static void copyInternal (void *dest, const void *src)

Detailed Description

template<class K, class V>
class tccollection::BtreeList< K, V >

A very simple binary search based list. It is useful for storage of items by key or just regular storage, it is very efficient for reading, but relatively inefficient for insertions, but only when compared to a linked list. It is however very memory efficient. There are some restrictions on it's use:

  • Class V must have a method to get the key: K getKey()
  • Class K must be directly comparable using less than, greater than and equals (any primitive would generally work).

On inserts, the list is re-sorted to get it back into order by key, and will grow automatically if needed using the howToGrow parameter passed to the constructor. The list is therefore always sorted by key, and this will generally always involve a memory copy to move around the items.

All keys must be of an unsigned integer type not exceeding uint32_t in length, you look up an item by it's key value, the list will either return NULL or a valid item. You can also obtain all values too, which is always sorted by key and is marked const, do not alter this array.

Constructor & Destructor Documentation

◆ BtreeList() [1/2]

template<class K, class V>
tccollection::BtreeList< K, V >::BtreeList ( bsize_t size = DEFAULT_LIST_SIZE,
GrowByMode howToGrow = DEFAULT_GROW_MODE )
explicit

Create a btree list that can be used to store simple objects that are not polymorphic. If no parameters are provided it will construct with the defaults for your board.

Parameters
sizethe initial size of the list, optional
howToGrowthe way in which it should grow if space runs out, optional

◆ BtreeList() [2/2]

template<class K, class V>
tccollection::BtreeList< K, V >::BtreeList ( ioaTreeInternal::KeyAccessor customAccessor,
bsize_t size = DEFAULT_LIST_SIZE,
GrowByMode howToGrow = DEFAULT_GROW_MODE )
explicit

Advanced usage constructor, prefer using the other constructor whenever possible.

This constructor allows you to provide your own key accessor, some other libraries require slightly different ways of accessing the object. For the key accessor you'll be provided with a pointer to a memory area, this area contains the item for which we need the key, you return the key value from the accessor as a uint32_t.

The default key accessor is as follows:

static uint32_t keyAccessor(const void *itm) {
return reinterpret_cast<const V *>(itm)->getKey();
}
Parameters
customAccessorthe custom accessor that is called to return the key
sizethe size of the list, optional parameter.
howToGrowthe method for growing when space runs out, optional parameter

Member Function Documentation

◆ add()

template<class K, class V>
bool tccollection::BtreeList< K, V >::add ( const V & item)

Adds an item into the current list of items, the list will be sorted by using getKey() on the object passed in. If the list cannot fit the item and cannot resize to do so, then false is returned.

Parameters
itemthe item to add
Returns
true if added, otherwise false

◆ capacity()

template<class K, class V>
bsize_t tccollection::BtreeList< K, V >::capacity ( ) const
Returns
current capacity of the list

◆ clear()

template<class K, class V>
void tccollection::BtreeList< K, V >::clear ( )

Completely clear down the list such that calls to count return 0.

◆ count()

template<class K, class V>
bsize_t tccollection::BtreeList< K, V >::count ( ) const
Returns
number of items in the list

◆ end()

template<class K, class V>
BtreeIterator< V > tccollection::BtreeList< K, V >::end ( ) const

Implements the range begin function for use with C++ ranges, represents the beginning of the list, supports using ++ to get the next item, but if using manually check against end() before accessing the value.

For example to iterate you can

(auto& item : mybtreelist) { item.dosomething(); }
Returns
an iterator for the beginning of the list. */ BtreeIterator<V> begin() const { return BtreeIterator<V>(0, treeStorage); }

/** An iterator that represnts the value beyond the end of the list, for use with C++ ranges.

Returns
an iterator that represents the end of the list. Normally used with begin().

◆ getByKey()

template<class K, class V>
V * tccollection::BtreeList< K, V >::getByKey ( K key) const

Get a value by it's key using a binary search algorithm.

Parameters
keythe key to be looked up
Returns
the value at that key position or null.

◆ itemAtIndex()

template<class K, class V>
V * tccollection::BtreeList< K, V >::itemAtIndex ( bsize_t idx) const

gets an item by it's index

Parameters
idxthe index to find
Returns
the item at the index or null.

◆ items()

template<class K, class V>
const V * tccollection::BtreeList< K, V >::items ( ) const
Returns
a list of all items

◆ nearestLocation()

template<class K, class V>
bsize_t tccollection::BtreeList< K, V >::nearestLocation ( const K & key) const

gets the nearest location to the key, this is an in-exact method in that it gives the exact match if available or otherwise the nearest one that is lower.

Parameters
keythe key to lookup
Returns
the position in the list

◆ removeByKey()

template<class K, class V>
bool tccollection::BtreeList< K, V >::removeByKey ( K key)

Remove an item using the key it was added with

Parameters
keythe key to remove
Returns
true if successful otherwise false.

◆ removeIndex()

template<class K, class V>
void tccollection::BtreeList< K, V >::removeIndex ( bsize_t index)

Remove an item by index in the underlying array

Parameters
indexthe index in the array

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