nvidia-texture-tools/src/nvcore/Array.h

183 lines
5.7 KiB
C
Raw Normal View History

2010-05-27 23:18:08 +00:00
// This code is in the public domain -- Ignacio Casta<74>o <castano@gmail.com>
#pragma once
#ifndef NV_CORE_ARRAY_H
#define NV_CORE_ARRAY_H
/*
This array class requires the elements to be relocable; it uses memmove and realloc. Ideally I should be
using swap, but I honestly don't care. The only thing that you should be aware of is that internal pointers
are not supported.
2010-05-27 23:18:08 +00:00
2012-02-14 16:16:27 +00:00
Note also that push_back and resize does not support inserting arguments elements that are in the same
container. This is forbidden to prevent an extra copy.
2010-05-27 23:18:08 +00:00
*/
#include "Memory.h"
#include "Debug.h"
2012-02-14 16:16:27 +00:00
#include "ForEach.h" // PseudoIndex
2010-05-27 23:18:08 +00:00
namespace nv
{
2012-04-30 23:02:23 +00:00
class Stream;
2010-05-27 23:18:08 +00:00
/**
* Replacement for std::vector that is easier to debug and provides
* some nice foreach enumerators.
*/
template<typename T>
class NVCORE_CLASS Array {
2010-05-27 23:18:08 +00:00
public:
2012-07-20 16:19:03 +00:00
typedef uint size_type;
2010-05-27 23:18:08 +00:00
// Default constructor.
NV_FORCEINLINE Array() : m_buffer(NULL), m_capacity(0), m_size(0) {}
2010-05-27 23:18:08 +00:00
// Copy constructor.
NV_FORCEINLINE Array(const Array & a) : m_buffer(NULL), m_capacity(0), m_size(0) {
copy(a.m_buffer, a.m_size);
2010-05-27 23:18:08 +00:00
}
// Constructor that initializes the vector with the given elements.
2012-02-14 16:16:27 +00:00
NV_FORCEINLINE Array(const T * ptr, uint num) : m_buffer(NULL), m_capacity(0), m_size(0) {
2010-05-27 23:18:08 +00:00
copy(ptr, num);
}
// Allocate array.
NV_FORCEINLINE explicit Array(uint capacity) : m_buffer(NULL), m_capacity(0), m_size(0) {
setArrayCapacity(capacity);
2010-05-27 23:18:08 +00:00
}
// Destructor.
NV_FORCEINLINE ~Array() {
2010-05-27 23:18:08 +00:00
clear();
2011-04-22 00:04:29 +00:00
free<T>(m_buffer);
2010-05-27 23:18:08 +00:00
}
/// Const element access.
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE const T & operator[]( uint index ) const
2010-05-27 23:18:08 +00:00
{
nvDebugCheck(index < m_size);
return m_buffer[index];
2010-05-27 23:18:08 +00:00
}
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE const T & at( uint index ) const
2010-05-27 23:18:08 +00:00
{
nvDebugCheck(index < m_size);
return m_buffer[index];
2010-05-27 23:18:08 +00:00
}
/// Element access.
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE T & operator[] ( uint index )
2010-05-27 23:18:08 +00:00
{
nvDebugCheck(index < m_size);
return m_buffer[index];
2010-05-27 23:18:08 +00:00
}
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE T & at( uint index )
2010-05-27 23:18:08 +00:00
{
nvDebugCheck(index < m_size);
return m_buffer[index];
2010-05-27 23:18:08 +00:00
}
/// Get vector size.
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE uint size() const { return m_size; }
2010-05-27 23:18:08 +00:00
/// Get vector size.
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE uint count() const { return m_size; }
2010-05-27 23:18:08 +00:00
2012-07-20 16:19:03 +00:00
/// Get vector capacity.
NV_FORCEINLINE uint capacity() const { return m_capacity; }
2010-05-27 23:18:08 +00:00
/// Get const vector pointer.
NV_FORCEINLINE const T * buffer() const { return m_buffer; }
2010-05-27 23:18:08 +00:00
/// Get vector pointer.
NV_FORCEINLINE T * buffer() { return m_buffer; }
2010-05-27 23:18:08 +00:00
2014-11-04 17:49:29 +00:00
/// Provide begin/end pointers for C++11 range-based for loops.
2012-07-20 16:19:03 +00:00
NV_FORCEINLINE T * begin() { return m_buffer; }
NV_FORCEINLINE T * end() { return m_buffer + m_size; }
2014-11-04 17:49:29 +00:00
NV_FORCEINLINE const T * begin() const { return m_buffer; }
NV_FORCEINLINE const T * end() const { return m_buffer + m_size; }
2012-07-20 16:19:03 +00:00
2010-05-27 23:18:08 +00:00
/// Is vector empty.
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE bool isEmpty() const { return m_size == 0; }
2010-05-27 23:18:08 +00:00
/// Is a null vector.
NV_FORCEINLINE bool isNull() const { return m_buffer == NULL; }
2010-05-27 23:18:08 +00:00
2014-11-04 17:49:29 +00:00
T & append();
2012-04-30 23:02:23 +00:00
void push_back( const T & val );
void pushBack( const T & val );
2013-06-07 17:53:55 +00:00
Array<T> & append( const T & val );
2012-04-30 23:02:23 +00:00
Array<T> & operator<< ( T & t );
void pop_back();
2015-10-29 06:53:08 +00:00
void popBack(uint count = 1);
void popFront(uint count = 1);
2012-04-30 23:02:23 +00:00
const T & back() const;
T & back();
const T & front() const;
T & front();
bool contains(const T & e) const;
bool find(const T & element, uint * indexPtr) const;
bool find(const T & element, uint begin, uint end, uint * indexPtr) const;
void removeAt(uint index);
bool remove(const T & element);
void insertAt(uint index, const T & val = T());
void append(const Array<T> & other);
void append(const T other[], uint count);
void replaceWithLast(uint index);
void resize(uint new_size);
void resize(uint new_size, const T & elem);
2012-07-20 16:19:03 +00:00
void fill(const T & elem);
2012-04-30 23:02:23 +00:00
void clear();
void shrink();
void reserve(uint desired_size);
void copy(const T * data, uint count);
Array<T> & operator=( const Array<T> & a );
T * release();
2010-05-27 23:18:08 +00:00
// Array enumerator.
typedef uint PseudoIndex;
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE PseudoIndex start() const { return 0; }
NV_FORCEINLINE bool isDone(const PseudoIndex & i) const { nvDebugCheck(i <= this->m_size); return i == this->m_size; }
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE void advance(PseudoIndex & i) const { nvDebugCheck(i <= this->m_size); i++; }
2010-05-27 23:18:08 +00:00
2018-02-06 02:55:07 +00:00
#if NV_NEED_PSEUDOINDEX_WRAPPER
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE T & operator[]( const PseudoIndexWrapper & i ) {
return m_buffer[i(this)];
2010-05-27 23:18:08 +00:00
}
2010-10-21 18:44:10 +00:00
NV_FORCEINLINE const T & operator[]( const PseudoIndexWrapper & i ) const {
return m_buffer[i(this)];
2010-05-27 23:18:08 +00:00
}
#endif
2012-04-30 23:02:23 +00:00
// Friends.
template <typename Typ>
friend Stream & operator<< ( Stream & s, Array<Typ> & p );
2012-04-30 23:02:23 +00:00
template <typename Typ>
friend void swap(Array<Typ> & a, Array<Typ> & b);
2012-02-14 16:16:27 +00:00
2013-06-07 17:53:55 +00:00
protected:
2010-05-27 23:18:08 +00:00
2012-04-30 23:02:23 +00:00
void setArraySize(uint new_size);
void setArrayCapacity(uint new_capacity);
T * m_buffer;
uint m_capacity;
uint m_size;
2010-05-27 23:18:08 +00:00
};
2012-02-14 16:16:27 +00:00
2010-05-27 23:18:08 +00:00
} // nv namespace
#endif // NV_CORE_ARRAY_H