PTypes: lists: tpodlist

Top: Basic types: Lists: tpodlist

>     template <class X, bool initzero = false> class tpodlist {
>         tpodlist();

>         tpodlist& operator =(const tpodlist& t);

>         int  get/set_count(int);
>         int  get/set_capacity(int);

>         X&   ins(int index);
>         void ins(int index, const X& item);
>         void ins(int index, const tpodlist& t);
>         X&   add();
>         void add(const X& item);
>         void add(const tpodlist& t);
>         X&   operator [](int index);
>         const X& operator [](int index) const;
>         X&   top();
>         void del(int index, int count = 1);
>         void pop();

>         void clear();
>         void pack();
>     }

The tpodlist template implements a dynamic array of so-called POD (plain-old-data) objects. POD types in C++ are: all integral types, pointers, floating point types, and also compound types (i.e. arrays and structures) that contain only POD items. With optimizing compilation, the instantiation of this template produces no extra code.

The parameter X of this template specifies the element type for the list. The optional parameter initzero indicates whether the memory allocated for new elements should be initialized to zero in methods ins(int), add() and set_count(int).

tpodlist::tpodlist() is the default constructor.

tpodlist& tpodlist::operator =(const tpodlist& t) is an assignment operator that clears a list and then copies all items from t.

int tpodlist::get/set_count(int) gets or sets the number of items in a list.

int tpodlist::get/set_capacity(int) gets or sets the capacity of a list. The capacity property reflects the number of items actually allocated for a list and is set automatically by other methods whenever necessary. Tpodlist uses a 'lazy allocation' technique for better performance: when items are added with add() or ins(), the capacity grows in bigger increments. This property, however, does not change when you delete items from a list, with only exception when count becomes 0, in which case capacity is also set to 0. You can call pack() to optimize memory usage after deleting multiple items. Setting capacity to a value less than count is an error.

X& tpodlist::ins(int index) allocates a slot for a new item at the position index and returns a reference to the slot. All items with greater indexes are moved up to make room for the new item. The slot can be initialized to zero if the template parameter initzero was true. Calling ins() with index equal to count is equivalent to calling add().

void tpodlist::ins(int index, const X& item) inserts item at index.

void tpodlist::ins(int index, const tpodlist& t) inserts all items of the list t at the position index.

X& tpodlist::add() allocates a slot for a new item at the end of a list and returns a reference to the slot.

void tpodlist::add(const X& item) appends item to a list.

void tpodlist::add(const tpodlist& t) appends the list t to a given list.

X& tpodlist::operator [](int index) returns a reference to an item at the position index. A const version of this operator also exists.

X& tpodlist::top() returns a reference to the last item (one that has the greatest index) in a list.

void tpodlist::del(int index, int count = 1) deletes count items from a list and moves all items with greater indexes down. The parameter count is optional and defaults to 1.

void tpodlist::pop() deletes the last item from a list.

void tpodlist::clear() deletes all items from a list and also sets capacity to 0.

void tpodlist::pack() sets capacity equal to count. You can call pack() after deleting multiple items from a list to optimize memory usage.

See also: tobjlist

PTypes home