Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site













SourceForge.net Logo

BiArray

 

template <class T>

class BiArray : private MoveableAndDeepCopyOption< BiArray<T> > 

T

Type or base class of elements stored in the BiArray. There is no common requirement for T.

The universal form of bidirectional random access container. Its features are derived from the fact that it is typically implemented using an indirect container of pointers to T like BiVector<T*>. Nevertheless it supports common concepts as ownership of contained elements, reference (not pointer) access to elements and so on.

It provides almost all operations of BiVector with the same semantics and almost any BiVector can be directly replaced by BiArray. On the other hand, it provides some special operations impossible for BiVector and most important, it never invalidates references (that means C++ references and pointers) to elements (it often invalidates iterators, though).

BiArray can also be used to store polymorphic elements - stored elements could be derived from T. To store such elements, you pass pointer to element previously created on the heap. Still, BiArray takes over ownership of such element (it e.g. deletes it when appropriate). You can also use this method to create BiArray of elements that do not have pick nor deep copy constructor.

There are also operations that allow detaching an element from BiArray, removing it but not destroying. Pointer to such element is returned from these operations and BiArray gives up ownership.

Disadvantage of BiArray over BiVector is performance - most operations are significantly slower than with BiVectors (by factor up to 8, it depends on speed of malloc/free).

As for memory, for small sized elements, BiArray memory consumption is significantly higher than BiVector consumption. As the size of the elements grow, BiArray starts to be better than BiVector.

Iterators to BiArray satisfy all C++ standard library requirements for random access iterator plus they allow assignment (and copy constructor) and testing for 0 (that is NULL) pointer.

Like any other NTL container, BiArray is a moveable type with pick and optional deep copy transfer semantics. Calling methods of picked BiArray is logic error with the exceptions of

void operator=(pick_ BiArray& v)

void operator<<=(const BiArray& v) (defined using DeepCopyOptionTemplate)

void Clear()

bool IsPicked() const

Optional deep copy is implemented through DeepCopyOptionTemplate macro.

 

 

Constructor Detail

 

BiArray()

Default constructor. Constructs empty BiArray.

 


 

BiArray(const BiArray& v, int)

Optional deep copy constructor.

Requires T to have deep copy constructor or optional deep copy constructor if Array stores only objects of type T.

Requires polymorphic deep copy if Array stores also objects of type derived from T.

v

Source Array.

 


 

BiArray(pick_ BiArray& src)

Pick constructor. Transfers source BiArray in low constant time, but destroys it by picking.

v

Source BiArray.

 


 

BiArray(std::initializer_list<Tinit)

C++ 11 initialization.

 


 

~BiArray()

Destructor. Invokes the destructor of every element in the BiArray.

 

 

Public Member List

 

T& AddHead()

Adds a new default constructed element at the head of the BiArray. The new element will be at position 0.

Requires T to have default constructor.

Invalidates iterators to the BiArray.

Return value

Reference to the newly added default constructed element.

 


 

T& AddTail()

Adds a new default constructed element at the tail of BiArray. The new element will be at position GetCount() - 1.

Requires T to have default constructor.

Invalidates iterators to the BiArray.

Return value

Reference to the newly added default constructed element.

 


 

void AddHead(const T& x)

Adds a new element with the specified value at the head of BiArray. The new element will be at position 0.

Requires T to have deep copy constructor.

Invalidates iterators to the BiArray.

x

The value that is copied to the newly created element.

 


 

void AddTail(const T& x)

Adds a new element with the specified value at the tail of BiArray. The new element will be at position GetCount() - 1.

Requires T to have deep copy constructor.

Invalidates iterators to the BiArray.

x

The value that is copied to the newly created element.

 


 

void AddHeadPick(T&& x)

Adds a new element at the head of BiArray and picks value of the parameter. The new element will be at position 0.

 


 

void AddTailPick(T&& x)

Adds a new element at the tail of BiArray and picks the value of the parameter. The new element will be at position GetCount() - 1.

 


 

T& AddHead(T *newt)

Adds a new element at the head of BiArray. Element is specified by a pointer to the object. BiArray takes over ownership of the object. This variant allows use of BiArray as polymorphic container, because the type of added element can also be derived from T as well. No constructor is applied. The new element will be at position 0.

Invalidates iterators to the BiArray.

newt

The object to be added.

Return value

Reference to the object = *newt.

 


 

T& AddTail(T *newt)

Adds new element at the tail of BiArray. Element is specified by a pointer to the object. BiArray takes over ownership of this this object. This variant allows use of BiArray as polymorphic container, because the type of added element can also be derived from T as well. No constructor is applied. The new element will be at position GetCount() - 1.

Invalidates iterators to the BiArray.

newt

The object to be added.

Return value

Reference to the object - *newt.

 


 

template <class TTTT& CreateHead()

Creates a new element of type TT at the head.

 


 

template <class TTTT& CreateTail()

Creates a new element of type TT at the tail.

 


 

T& Head()

Returns reference to the head of the BiArray. Same as operator[](0).

Return value

Reference to the head of BiArray.

 


 

T& Tail()

Returns reference to the tail of the BiArray. Same as operator[](GetCount() - 1).

Return value

Reference to the tail of BiArray.

 


 

const T& Head() const

Returns reference to the head of the BiArray. Same as operator[](0).

Return value

Constant reference to the head of BiArray.

 


 

const T& Tail() const

Returns reference to the tail of the BiArray. Same as operator[](GetCount() - 1).

Return value

Constant reference to the tail of BiArray.

 


 

void DropHead()

Removes element at the head of the BiArray.

Invalidates iterators to the BiArray.

 


 

void DropTail()

Removes element at the tail of the BiArray.

Invalidates iterators to the BiArray.

 


 

T *DetachHead()

Removes element at the head of the BiArray, giving up ownership. Client is responsible for deletion of the element.

Invalidates iterators to the BiArray.

Return value

Pointer to the element allocated on the heap.

 


 

T *DetachTail()

Removes element at the tail of the BiArray, giving up ownership. Client is responsible for deletion of the element.

Invalidates iterators to the BiArray.

Return value

Pointer to the element allocated on the heap.

 


 

T& operator[](int i)

Returns a reference to the element at the specified position.

i

Position of the element.

Return value

Reference to the element.

 


 

const T& operator[](int iconst

Returns a reference to the element at the specified position.

i

Position of the element.

Return value

Constant reference to the element.

 


 

int GetCount() const

Returns the number of elements in the BiArray.

Return value

Actual number of elements.

 


 

bool IsEmpty() const

Tests whether the BiArray is empty. Same as GetCount() == 0.

Return value

true if Vector is empty, false otherwise.

 


 

void Clear()

Removes all elements from the BiArray.

 


 

void Shrink()

Minimizes memory consumption of the BiArray by minimizing capacity.

 


 

void Reserve(int n)

Reserves capacity. If required capacity is greater than the current capacity, capacity is increased to the required value.

n

Required capacity.

 


 

int GetAlloc() const

Returns current capacity of BiArray.

Return value

Capacity of the BiArray.

 


 

friend BiArray& operator<<(BiArray& b, const T& x)

Operator replacement of void AddTail(const T&x). By returning a reference to the BiArray it allows adding more elements in a single expression, thus e.g. allowing to construct a temporary BiArray as part of an expression like Foo(BiArray<int>() << 1 << 2 << 4).

Requires T to have deep copy constructor.

Invalidates iterators to the BiArray.

x

The value that is copied to the newly created element.

Return value

Reference to the BiArray (that is *this).

 


 

friend BiArray& operator>>(const T& x, BiArray& b)

Operator replacement of void AddHead(const T&x). By returning a reference to the BiArray it allows adding more elements in a single expression, thus e.g. allowing to construct a temporary BiArray as part of an expression like Foo(1 >> (2 >> BiArray<int>())).

Requires T to have deep copy constructor.

Invalidates iterators to the BiArray.

x

The value that is copied to the newly created element.

Return value

Reference to the BiArray (that is *this).

 


 

void Serialize(Stream& s)

Serializes the content of the BiArray to/from a Stream. Works only if NTL is used as part of UPP.

Requires T to have serialization operator defined.

s

Target/source stream.

 


 

bool IsPicked() const

Returns true if BiArray is in picked state.

Return value

true if BiArray is in picked state, false otherwise.

 


 

BiArray(const BiArray& v, int)

Optional deep copy constructor.

 


 

BiArray(BiArray&& src)

Pick constructor.

 


 

void operator=(BiArray&& src)

Pick assignment.

 


 

typedef T ValueType

Typedef of T for use in templated algorithms.

 


 

typedef IIterator<BiArray> Iterator

Iterator type.

 


 

typedef ConstIIterator<BiArray> ConstIterator

Constant iterator type.

 


 

Iterator Begin()

Returns a non-constant iterator to the head of the BiArray.

Return value

Iterator.

 


 

Iterator End()

Returns a non-constant iterator to the position just beyond the tail of the BiArray.

Return value

Iterator.

 


 

Iterator GetIter(int pos)

Returns a non-constant iterator to the element at the specified position. Same as Begin() + pos. The benefit of this method is that pos is range checked in debug mode.

pos

Required position.

Return value

Iterator.

 


 

ConstIterator Begin() const

Returns a constant iterator to the head of the BiArray.

Return value

Iterator.

 


 

ConstIterator End() const

Returns a constant iterator to the position just beyond the tail of the Array.

Return value

Iterator.

 


 

ConstIterator GetIter(int posconst

Returns a constant iterator to the element at the specified position. Same as Begin() + pos. The benefit of this method is that pos is range checked in debug mode.

pos

Required position.

Return value

Iterator.

 


 

friend void Swap(BiArray& a, BiArray& b)

Specialization of the generic Swap for BiArrays. Swaps BiArray in low constant time operation.

a

First BiArray to swap.

b

Second BiArray to swap.

 

 

Last edit by klugier on 06/12/2016. Do you want to contribute?. T++