PxArray#

Defined in include/foundation/PxArray.h

template<class T, class Alloc>
class PxArray#

An array is a sequential container.

Implementation note:

  • entries between 0 and size are valid objects

  • we use inheritance to build this because the array is included inline in a lot of objects and we want the allocator to take no space if it’s not stateful, which aggregation doesn’t allow. Also, we want the metadata at the front for the inline case where the allocator contains some inline storage space

Template Parameters:
  • T – Element type

  • Alloc – Allocator type (defaults to PxAllocatorTraits<T>::Type)

Subclassed by PxInlineArray< T, N, Alloc >

Public Types

typedef T *Iterator#
typedef const T *ConstIterator#

Public Functions

inline explicit PxArray(const PxEMPTY v)#

Deserialization constructor.

Parameters:

v – Empty tag for deserialization

inline explicit PxArray(const Alloc &alloc = Alloc())#

Default array constructor. Initialize an empty array.

Parameters:

alloc – Allocator to use

inline explicit PxArray(
uint32_t size,
const T &a = T(),
const Alloc &alloc = Alloc(),
)#

Initialize array with given size.

Parameters:
  • size – Initial size (and capacity) of the array

  • a – Default value for elements

  • alloc – Allocator to use

template<class A>
inline explicit PxArray(
const PxArray<T, A> &other,
const Alloc &alloc = Alloc(),
)#

Copy-constructor. Copy all entries from other array.

Parameters:
  • other – Source array to copy from

  • alloc – Allocator to use

inline PxArray(const PxArray &other, const Alloc &alloc = Alloc())#
inline explicit PxArray(
const T *first,
const T *last,
const Alloc &alloc = Alloc(),
)#

Initialize array with given length.

Parameters:
  • first – Pointer to the first element to copy

  • last – Pointer past the last element to copy

  • alloc – Allocator to use

inline ~PxArray()#

Destructor.

template<class A>
inline PxArray &operator=(
const PxArray<T, A> &rhs,
)#

Assignment operator. Copy content (deep-copy)

Parameters:

rhs – Source array to copy from

Returns:

Reference to this array

inline PxArray &operator=(const PxArray &t)#
inline const T &operator[](uint32_t i) const#

Array indexing operator.

Parameters:

i – The index of the element that will be returned.

Returns:

The element i in the array.

inline T &operator[](uint32_t i)#

Array indexing operator.

Parameters:

i – The index of the element that will be returned.

Returns:

The element i in the array.

inline ConstIterator begin() const#

Returns a pointer to the first element of the array.

Returns:

Pointer to the first element of the array.

inline Iterator begin()#
inline ConstIterator end() const#

Returns a pointer beyond the last element of the array. Do not dereference.

Returns:

Pointer to the element beyond the last element of the array.

inline Iterator end()#
inline const T &front() const#

Returns a reference to the first element of the array. Undefined if the array is empty.

Returns:

Reference to the first element of the array

inline T &front()#
inline const T &back() const#

Returns a reference to the last element of the array. Undefined if the array is empty.

Returns:

Reference to the last element of the array

inline T &back()#
inline uint32_t size() const#

Returns the number of entries in the array. This can, and probably will, differ from the array capacity.

Returns:

The number of entries in the array.

inline void clear()#

Clears the array. Calls destructor on all elements.

inline bool empty() const#

Returns whether the array is empty (i.e. whether its size is 0).

Returns:

True if the array is empty

inline Iterator find(const T &a)#

Finds the first occurrence of an element in the array.

Parameters:

a – The element to find.

Returns:

Pointer to the found element or end() if not found

inline ConstIterator find(const T &a) const#
inline T *pushBack(const T &a)#

Adds one element to the end of the array. Operation is O(1).

Parameters:

a – The element that will be added to this array.

Returns:

Pointer to the element that has been added, NULL if operation failed.

inline T popBack()#

Removes and returns the element at the end of the array. Only legal if the array is non-empty.

Returns:

The last element of the array

inline T *insert()#

Construct one element at the end of the array. Operation is O(1).

Returns:

Pointer to the element that has been inserted, NULL if operation failed.

inline void replaceWithLast(uint32_t i)#

Removes the element at position i from the array and replaces it with the last element. Operation is O(1)

Parameters:

i – The position of the element that will be removed from this array.

inline void replaceWithLast(Iterator i)#
inline bool findAndReplaceWithLast(const T &a)#

Replaces the first occurrence of the element a with the last element. Operation is O(n)

Parameters:

a – The element to find and remove from this array.

Returns:

True if the element has been removed.

inline void remove(uint32_t i)#

Subtracts the element on position i from the array. Shift the entire array one step. Operation is O(n)

Parameters:

i – The position of the element that will be subtracted from this array.

inline void removeRange(uint32_t begin, uint32_t count)#

Removes a range from the array. Shifts the array so order is maintained. Operation is O(n)

Parameters:
  • begin – The starting position of the element that will be subtracted from this array.

  • count – The number of elements that will be subtracted from this array.

bool resize(const uint32_t size, const T &a = T())#

Resize array.

Parameters:
  • size – New size of the array

  • a – Default value for new elements

Returns:

True if operation succeeded

bool resizeUninitialized(const uint32_t size)#

Resize array without initializing new elements.

Parameters:

size – New size of the array

Returns:

True if operation succeeded

inline bool shrink()#

Resize array such that only as much memory is allocated to hold the existing elements.

Returns:

True if operation succeeds, false otherwise.

inline void reset()#

Deletes all array elements and frees memory.

inline void resetOrClear()#

Resets or clears the array depending on occupancy.

inline bool reserve(const uint32_t capacity)#

Ensure that the array has at least the requested capacity.

Parameters:

capacity – Minimum capacity to reserve

Returns:

True if operation succeeds, false otherwise.

inline uint32_t capacity() const#

Query the capacity(allocated mem) for the array.

Returns:

The capacity of the array

inline void forceSize_Unsafe(uint32_t size)#

Unsafe function to force the size of the array.

Parameters:

size – New size to force

inline void swap(PxArray<T, Alloc> &other)#

Swap contents of an array without allocating temporary storage.

Parameters:

other – Array to swap with

inline bool assign(const T *first, const T *last)#

Assign a range of values to this array (resizes to length of range)

Parameters:
  • first – Pointer to the first element to copy

  • last – Pointer past the last element to copy

Returns:

True if operation succeeds, false otherwise.

inline uint32_t isInUserMemory() const#
inline Alloc &getAllocator()#

return reference to allocator

Protected Functions

inline PxArray(
T *memory,
uint32_t size,
uint32_t capacity,
const Alloc &alloc = Alloc(),
)#
template<class A>
void copy(const PxArray<T, A> &other)#
inline T *allocate(uint32_t size, uint32_t *cookie = NULL)#
inline void deallocate(void *mem, uint32_t *cookie = NULL)#
T *growAndPushBack(const T &a)#

Called when pushBack() needs to grow the array.

Parameters:

a – The element that will be added to this array.

Returns:

Pointer to the pushed element or NULL if the operation failed

inline bool grow(uint32_t capacity)#

Resizes the available memory for the array.

Parameters:

capacity – The number of entries that the array should be able to hold.

Returns:

True if operation succeeds, false otherwise.

bool recreate(uint32_t capacity)#

Creates a new memory block, copies all entries to the new block and destroys old entries.

Parameters:

capacity – The number of entries that the array should be able to hold.

Returns:

True if operation succeeds, false otherwise.

inline uint32_t capacityIncrement() const#

Protected Attributes

T *mData#
uint32_t mSize#
uint32_t mCapacity#

Protected Static Functions

static inline void create(T *first, T *last, const T &a)#
static inline void copy(T *first, T *last, const T *src)#
static inline void destroy(T *first, T *last)#