Sux
Public Member Functions | Static Public Attributes | Friends | List of all members
sux::util::Vector< T, AT > Class Template Reference

#include <Vector.hpp>

Inheritance diagram for sux::util::Vector< T, AT >:
sux::util::Expandable

Public Member Functions

 Vector ()=default
 
 Vector (size_t length)
 
 Vector (const T *data, size_t length)
 
 ~Vector ()
 
 Vector (const Vector &)=delete
 
Vectoroperator= (const Vector &)=delete
 
 Vector (Vector< T, AT > &&oth)
 
Vector< T, AT > & operator= (Vector< T, AT > &&oth)
 
void trim (size_t capacity)
 
void trimToFit ()
 
void reserve (size_t capacity)
 
void grow (size_t capacity)
 
void resize (size_t size)
 
void size (size_t size)
 
void pushBack (T elem)
 
popBack ()
 
T * operator& () const
 
const T & operator[] (size_t i) const
 
T & operator[] (size_t i)
 
size_t size () const
 
size_t capacity () const
 
size_t bitCount () const
 
- Public Member Functions inherited from sux::util::Expandable
void trimToFit ()
 

Static Public Attributes

static constexpr int PROT = PROT_READ | PROT_WRITE
 
static constexpr int FLAGS = MAP_PRIVATE | MAP_ANONYMOUS | (AT == FORCEHUGEPAGE ? MAP_HUGETLB : 0)
 

Friends

void swap (Vector< T, AT > &first, Vector< T, AT > &second) noexcept
 
std::ostream & operator<< (std::ostream &os, const Vector< T, AT > &vector)
 
std::istream & operator>> (std::istream &is, Vector< T, AT > &vector)
 

Detailed Description

template<typename T, AllocType AT = MALLOC>
class sux::util::Vector< T, AT >

An expandable vector with settable type of memory allocation.

Instances of this class have a behavior similar to std::vector. However, the strategy used for allocation memory can be selected. Moreover, the class is just a thin wrapper around a backing array: in particular, there are no bound checks.

Once enough capacity has been allocated through reserve(size_t), the operator operator&() will return a pointer to the backing array and the allocated space can be used directly, if necessary.

This class implements the standard << and >> operators for simple serialization and deserialization.

Template Parameters
Tthe data type of an element.
ATa type of memory allocation out of AllocType.

Constructor & Destructor Documentation

◆ Vector() [1/5]

template<typename T, AllocType AT = MALLOC>
sux::util::Vector< T, AT >::Vector ( )
default

◆ Vector() [2/5]

template<typename T, AllocType AT = MALLOC>
sux::util::Vector< T, AT >::Vector ( size_t  length)
inlineexplicit

◆ Vector() [3/5]

template<typename T, AllocType AT = MALLOC>
sux::util::Vector< T, AT >::Vector ( const T *  data,
size_t  length 
)
inlineexplicit

◆ ~Vector()

template<typename T, AllocType AT = MALLOC>
sux::util::Vector< T, AT >::~Vector ( )
inline

◆ Vector() [4/5]

template<typename T, AllocType AT = MALLOC>
sux::util::Vector< T, AT >::Vector ( const Vector< T, AT > &  )
delete

◆ Vector() [5/5]

template<typename T, AllocType AT = MALLOC>
sux::util::Vector< T, AT >::Vector ( Vector< T, AT > &&  oth)
inline

Member Function Documentation

◆ bitCount()

template<typename T, AllocType AT = MALLOC>
size_t sux::util::Vector< T, AT >::bitCount ( ) const
inline

Returns the number of bits used by this vector.

Returns
the number of bits used by this vector.

◆ capacity()

template<typename T, AllocType AT = MALLOC>
size_t sux::util::Vector< T, AT >::capacity ( ) const
inline

Returns the number of elements that this vector can hold currently without increasing its capacity.

Returns
the number of elements that this vector can hold currently without increasing its capacity.

◆ grow()

template<typename T, AllocType AT = MALLOC>
void sux::util::Vector< T, AT >::grow ( size_t  capacity)
inlinevirtual

Enlarges the backing array to that it can contain a given number of elements, plus possibly extra space.

If the current capacity is sufficient, nothing happens. Otherwise, the backing is enlarged to the maximum between the provided capacity and 50% more than the current capacity.

Parameters
capacitythe desired new capacity.

Implements sux::util::Expandable.

◆ operator&()

template<typename T, AllocType AT = MALLOC>
T* sux::util::Vector< T, AT >::operator& ( ) const
inline

Returns a pointer at the start of the backing array.

◆ operator=() [1/2]

template<typename T, AllocType AT = MALLOC>
Vector& sux::util::Vector< T, AT >::operator= ( const Vector< T, AT > &  )
delete

◆ operator=() [2/2]

template<typename T, AllocType AT = MALLOC>
Vector<T, AT>& sux::util::Vector< T, AT >::operator= ( Vector< T, AT > &&  oth)
inline

◆ operator[]() [1/2]

template<typename T, AllocType AT = MALLOC>
T& sux::util::Vector< T, AT >::operator[] ( size_t  i)
inline

Returns the given element of the vector.

◆ operator[]() [2/2]

template<typename T, AllocType AT = MALLOC>
const T& sux::util::Vector< T, AT >::operator[] ( size_t  i) const
inline

Returns the given element of the vector.

◆ popBack()

template<typename T, AllocType AT = MALLOC>
T sux::util::Vector< T, AT >::popBack ( )
inline

Pops the element at the end of this vector.

The last element of this vector is removed and returned.

Returns
the last element of this vector.

◆ pushBack()

template<typename T, AllocType AT = MALLOC>
void sux::util::Vector< T, AT >::pushBack ( elem)
inline

Adds a given element at the end of this vector.

Parameters
eleman element.

◆ reserve()

template<typename T, AllocType AT = MALLOC>
void sux::util::Vector< T, AT >::reserve ( size_t  capacity)
inlinevirtual

Enlarges the backing array to that it can contain a given number of elements.

If the current capacity is sufficient, nothing happens. Otherwise, the backing is enlarged to the provided capacity.

Parameters
capacitythe desired new capacity.

Implements sux::util::Expandable.

◆ resize()

template<typename T, AllocType AT = MALLOC>
void sux::util::Vector< T, AT >::resize ( size_t  size)
inlinevirtual

Changes the vector size to the given value.

If the argument is smaller than or equal to the current size, the backing array is unmodified. Otherwise, the backing array is enlarged to the given size using grow(). New elements are initialized to zero.

Parameters
sizethe desired new size.

Implements sux::util::Expandable.

◆ size() [1/2]

template<typename T, AllocType AT = MALLOC>
size_t sux::util::Vector< T, AT >::size ( ) const
inlinevirtual

Returns the number of elements in this vector.

Implements sux::util::Expandable.

◆ size() [2/2]

template<typename T, AllocType AT = MALLOC>
void sux::util::Vector< T, AT >::size ( size_t  size)
inlinevirtual

Changes the vector size and capacity to the given value.

Both size and capacity are set to the provided size. If necessary, new elements are initialized to zero.

Parameters
sizethe desired new size.

Implements sux::util::Expandable.

◆ trim()

template<typename T, AllocType AT = MALLOC>
void sux::util::Vector< T, AT >::trim ( size_t  capacity)
inlinevirtual

Trim the the memory allocated so that it holds at most the given number of elements.

Parameters
capacitythe new desired capacity.

Implements sux::util::Expandable.

◆ trimToFit()

template<typename T, AllocType AT = MALLOC>
void sux::util::Vector< T, AT >::trimToFit ( )
inline

Trim the the memory allocated so that it holds exactly size() elements.

Friends And Related Function Documentation

◆ operator<<

template<typename T, AllocType AT = MALLOC>
std::ostream& operator<< ( std::ostream &  os,
const Vector< T, AT > &  vector 
)
friend

◆ operator>>

template<typename T, AllocType AT = MALLOC>
std::istream& operator>> ( std::istream &  is,
Vector< T, AT > &  vector 
)
friend

◆ swap

template<typename T, AllocType AT = MALLOC>
void swap ( Vector< T, AT > &  first,
Vector< T, AT > &  second 
)
friend

Member Data Documentation

◆ FLAGS

template<typename T, AllocType AT = MALLOC>
constexpr int sux::util::Vector< T, AT >::FLAGS = MAP_PRIVATE | MAP_ANONYMOUS | (AT == FORCEHUGEPAGE ? MAP_HUGETLB : 0)
staticconstexpr

◆ PROT

template<typename T, AllocType AT = MALLOC>
constexpr int sux::util::Vector< T, AT >::PROT = PROT_READ | PROT_WRITE
staticconstexpr

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