HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes | Friends
hi::v1::lean_vector< T > Class Template Reference

#include <hikogui/container/lean_vector.hpp>

Public Types

using value_type = T
 
using pointer = value_type *
 
using const_pointer = value_type const *
 
using reference = value_type&
 
using const_reference = value_type const&
 
using iterator = value_type *
 
using const_iterator = value_type const *
 
using reverse_iterator = std::reverse_iterator<iterator>
 
using const_reverse_iterator = std::reverse_iterator<const_iterator>
 
using size_type = std::size_t
 
using difference_type = std::ptrdiff_t
 
using allocator_type = std::allocator<value_type>
 

Public Member Functions

constexpr allocator_type get_allocator () const noexcept
 The allocator_type used to allocate items.
 
constexpr lean_vector () noexcept=default
 Construct an empty vector.
 
 ~lean_vector ()
 Destruct the vector.
 
 lean_vector (lean_vector const &other)
 Copy-construct a vector.
 
 lean_vector (lean_vector &&other) noexcept(std::is_nothrow_move_constructible_v< value_type >)
 Move-construct a vector.
 
lean_vectoroperator= (lean_vector const &other) noexcept
 Copy-assign a vector.
 
lean_vectoroperator= (lean_vector &&other) noexcept
 Move-assign a vector.
 
void swap (lean_vector &other) noexcept
 
 lean_vector (size_type count)
 
 lean_vector (size_type count, value_type const &value)
 
 lean_vector (std::input_iterator auto first, std::input_iterator auto last)
 Construct a vector with the data pointed by iterators.
 
 lean_vector (std::initializer_list< value_type > list)
 Construct a vector with the given initializer list.
 
void assign (size_type count, value_type const &value)
 Replace the data in the vector.
 
void assign (std::input_iterator auto first, std::input_iterator auto last)
 Replace the data in the vector.
 
void assign (std::initializer_list< value_type > list)
 Replace the data in the vector.
 
pointer data () noexcept
 Get a pointer to the first item.
 
const_pointer data () const noexcept
 Get a const-pointer to the first item.
 
constexpr bool empty () const noexcept
 Check if the vector is empty.
 
constexpr size_type size () const noexcept
 Get the number of items in the vector.
 
constexpr size_type max_size () const noexcept
 
constexpr size_t short_capacity () const noexcept
 The maximum number of items that can fit without allocation.
 
size_t capacity () const noexcept
 Get the current capacity of the vector.
 
reference at (size_type index)
 Get a reference to an item in the vector.
 
const_reference at (size_type index) const
 Get a const-reference to an item in the vector.
 
reference operator[] (size_type index) noexcept
 Get a reference to an item in the vector.
 
const_reference operator[] (size_type index) const noexcept
 Get a const-reference to an item in the vector.
 
reference front () noexcept
 Get a reference to the first item in the vector.
 
const_reference front () const noexcept
 Get a const-reference to the first item in the vector.
 
reference back () noexcept
 Get a reference to the last item in the vector.
 
const_reference back () const noexcept
 Get a const-reference to the last item in the vector.
 
iterator begin () noexcept
 Get an iterator to the first item in the vector.
 
const_iterator begin () const noexcept
 Get an const-iterator to the first item in the vector.
 
const_iterator cbegin () const noexcept
 Get an const-iterator to the first item in the vector.
 
iterator end () noexcept
 Get an iterator beyond the last item in the vector.
 
const_iterator end () const noexcept
 Get an const-iterator beyond the last item in the vector.
 
const_iterator cend () const noexcept
 Get an const-iterator beyond the last item in the vector.
 
void clear () noexcept
 Remove all items from the vector.
 
void reserve (size_type new_capacity)
 Reserve capacity for items.
 
void shrink_to_fit ()
 Shrink the allocation to fit the current number of items.
 
template<typename... Args>
iterator emplace (const_iterator pos, Args &&...args)
 Construct in-place a new item.
 
iterator insert (const_iterator pos, value_type const &value)
 Insert a new item.
 
iterator insert (const_iterator pos, value_type &&value)
 Insert a new item.
 
iterator insert (const_iterator pos, size_type count, value_type const &value)
 Insert a new item.
 
template<std::input_iterator First, std::input_iterator Last>
iterator insert (const_iterator pos, First first, Last last)
 Insert new items.
 
iterator insert (const_iterator pos, std::initializer_list< value_type > list)
 Insert new items.
 
iterator erase (const_iterator pos)
 Erase an item at position.
 
iterator erase (const_iterator first, const_iterator last)
 Erase an items.
 
template<typename... Args>
reference emplace_back (Args &&...args)
 In-place construct an item at the end of the vector.
 
void push_back (value_type const &value)
 Copy an item to the end of the vector.
 
void push_back (value_type &&value)
 Move an item to the end of the vector.
 
void pop_back ()
 Remove the last item from the vector.
 
void resize (size_type new_size)
 Resize a vector.
 
void resize (size_type new_size, value_type const &value)
 Resize a vector.
 

Static Public Attributes

static constexpr size_t value_alignment = alignof(value_type)
 

Friends

bool operator== (lean_vector const &lhs, lean_vector const &rhs) noexcept
 Compare two vectors.
 
auto operator<=> (lean_vector const &lhs, lean_vector const &rhs) noexcept
 Compare two vectors lexicographically.
 
size_type erase (lean_vector &c, value_type const &value)
 Erase items of a value from a vector.
 
template<typename Pred >
size_type erase (lean_vector &c, Pred pred)
 Erase items of a value from a vector.
 
void swap (lean_vector &lhs, lean_vector &rhs) noexcept
 

Detailed Description

template<typename T>
class hi::v1::lean_vector< T >

Lean-vector with (SVO) short-vector-optimization.

The maximum number of items in SVO are:(sizeof(T *) * 3 - 1) / sizeof(T)

Constructor & Destructor Documentation

◆ lean_vector() [1/5]

template<typename T >
constexpr hi::v1::lean_vector< T >::lean_vector ( )
constexprdefaultnoexcept

Construct an empty vector.

◆ ~lean_vector()

template<typename T >
hi::v1::lean_vector< T >::~lean_vector ( )
inline

Destruct the vector.

◆ lean_vector() [2/5]

template<typename T >
hi::v1::lean_vector< T >::lean_vector ( lean_vector< T > const & other)
inline

Copy-construct a vector.

This will copy a vector. If the copy requires an allocation the new allocation will fit the number of items exactly.

Parameters
otherThe vector to copy.

◆ lean_vector() [3/5]

template<typename T >
hi::v1::lean_vector< T >::lean_vector ( lean_vector< T > && other)
inlinenoexcept

Move-construct a vector.

This will steal the allocation from the other vector, or move the items if the number of items is less than or equal to short_capacity().

Parameters
otherThe vector to move.

◆ lean_vector() [4/5]

template<typename T >
hi::v1::lean_vector< T >::lean_vector ( std::input_iterator auto first,
std::input_iterator auto last )
inline

Construct a vector with the data pointed by iterators.

Parameters
firstAn iterator pointing to the first item to copy.
lastAn iterator pointing beyond the last item to copy.

◆ lean_vector() [5/5]

template<typename T >
hi::v1::lean_vector< T >::lean_vector ( std::initializer_list< value_type > list)
inline

Construct a vector with the given initializer list.

Parameters
listThe list of values to copy into the vector.

Member Function Documentation

◆ assign() [1/3]

template<typename T >
void hi::v1::lean_vector< T >::assign ( size_type count,
value_type const & value )
inline

Replace the data in the vector.

Parameters
countThe number of times to copy value into the vector.
valueThe value to copy into the vector.

◆ assign() [2/3]

template<typename T >
void hi::v1::lean_vector< T >::assign ( std::initializer_list< value_type > list)
inline

Replace the data in the vector.

Parameters
listA list of data to copy into the vector.

◆ assign() [3/3]

template<typename T >
void hi::v1::lean_vector< T >::assign ( std::input_iterator auto first,
std::input_iterator auto last )
inline

Replace the data in the vector.

Parameters
firstA iterator pointing to the first value to copy.
lastA iterator pointing beyond the last value to copy.

◆ at() [1/2]

template<typename T >
reference hi::v1::lean_vector< T >::at ( size_type index)
inline

Get a reference to an item in the vector.

Parameters
indexThe index to the item in the vector.
Returns
A reference to the item in the vector.
Exceptions
std::out_of_rangeWhen index points beyond the size of the vector.

◆ at() [2/2]

template<typename T >
const_reference hi::v1::lean_vector< T >::at ( size_type index) const
inline

Get a const-reference to an item in the vector.

Parameters
indexThe index to the item in the vector.
Returns
A reference to the item in the vector.
Exceptions
std::out_of_rangeWhen index points beyond the size of the vector.

◆ back() [1/2]

template<typename T >
const_reference hi::v1::lean_vector< T >::back ( ) const
inlinenoexcept

Get a const-reference to the last item in the vector.

Note
It is undefined-behavior to call this function on an empty vector.
Returns
A reference to the last item in the vector.

◆ back() [2/2]

template<typename T >
reference hi::v1::lean_vector< T >::back ( )
inlinenoexcept

Get a reference to the last item in the vector.

Note
It is undefined-behavior to call this function on an empty vector.
Returns
A reference to the last item in the vector.

◆ begin() [1/2]

template<typename T >
const_iterator hi::v1::lean_vector< T >::begin ( ) const
inlinenoexcept

Get an const-iterator to the first item in the vector.

Returns
A const-iterator to the first item in the vector.

◆ begin() [2/2]

template<typename T >
iterator hi::v1::lean_vector< T >::begin ( )
inlinenoexcept

Get an iterator to the first item in the vector.

Returns
A iterator to the first item in the vector.

◆ capacity()

template<typename T >
size_t hi::v1::lean_vector< T >::capacity ( ) const
inlinenoexcept

Get the current capacity of the vector.

Returns
The number of items that fit in the current allocation.

◆ cbegin()

template<typename T >
const_iterator hi::v1::lean_vector< T >::cbegin ( ) const
inlinenoexcept

Get an const-iterator to the first item in the vector.

Returns
A const-iterator to the first item in the vector.

◆ cend()

template<typename T >
const_iterator hi::v1::lean_vector< T >::cend ( ) const
inlinenoexcept

Get an const-iterator beyond the last item in the vector.

Returns
A const-iterator beyond the last item in the vector.

◆ clear()

template<typename T >
void hi::v1::lean_vector< T >::clear ( )
inlinenoexcept

Remove all items from the vector.

The allocation of the items remains.

◆ data() [1/2]

template<typename T >
const_pointer hi::v1::lean_vector< T >::data ( ) const
inlinenoexcept

Get a const-pointer to the first item.

Returns
The pointer to the first item.

◆ data() [2/2]

template<typename T >
pointer hi::v1::lean_vector< T >::data ( )
inlinenoexcept

Get a pointer to the first item.

Returns
The pointer to the first item.

◆ emplace()

template<typename T >
template<typename... Args>
iterator hi::v1::lean_vector< T >::emplace ( const_iterator pos,
Args &&... args )
inline

Construct in-place a new item.

If the item is not placed at the end, the item will be moved to the correct position after it is created at the end.

Parameters
posThe position where the item will be created.
argsThe arguments to pass to the constructor of the new item.
Returns
An iterator pointing to the newly inserted item.

◆ emplace_back()

template<typename T >
template<typename... Args>
reference hi::v1::lean_vector< T >::emplace_back ( Args &&... args)
inline

In-place construct an item at the end of the vector.

The item is directly constructed at the end of the vector.

Parameters
argsThe arguments passed to the constructor.
Returns
A reference to the newly constructed item in the vector.

◆ empty()

template<typename T >
constexpr bool hi::v1::lean_vector< T >::empty ( ) const
inlineconstexprnoexcept

Check if the vector is empty.

Return values
trueThe vector is empty.
falseThe vector contains at least one item.

◆ end() [1/2]

template<typename T >
const_iterator hi::v1::lean_vector< T >::end ( ) const
inlinenoexcept

Get an const-iterator beyond the last item in the vector.

Returns
A const-iterator beyond the last item in the vector.

◆ end() [2/2]

template<typename T >
iterator hi::v1::lean_vector< T >::end ( )
inlinenoexcept

Get an iterator beyond the last item in the vector.

Returns
A iterator beyond the last item in the vector.

◆ erase() [1/2]

template<typename T >
iterator hi::v1::lean_vector< T >::erase ( const_iterator first,
const_iterator last )
inline

Erase an items.

Erases an items between first and last. Items beyond the position will be moved.

This function will not change the allocation and iterators up to the erased items remain valid.

Parameters
firstAn iterator pointing to the first item to be removed.
lastAn iterator pointing beyond the last item to be removed.
Returns
An iterator to the item after the removed items, or end().

◆ erase() [2/2]

template<typename T >
iterator hi::v1::lean_vector< T >::erase ( const_iterator pos)
inline

Erase an item at position.

Erases an item at a position. Items beyond the position will be moved.

This function will not change the allocation and iterators up to the erased item remain valid.

Note
It is undefined behavior to pass a iterator not belonging to this vector.
Parameters
posAn iterator pointing to the item to be removed.
Returns
An iterator to the item after the removed item, or end().

◆ front() [1/2]

template<typename T >
const_reference hi::v1::lean_vector< T >::front ( ) const
inlinenoexcept

Get a const-reference to the first item in the vector.

Note
It is undefined-behavior to call this function on an empty vector.
Returns
A reference to the first item in the vector.

◆ front() [2/2]

template<typename T >
reference hi::v1::lean_vector< T >::front ( )
inlinenoexcept

Get a reference to the first item in the vector.

Note
It is undefined-behavior to call this function on an empty vector.
Returns
A reference to the first item in the vector.

◆ get_allocator()

template<typename T >
constexpr allocator_type hi::v1::lean_vector< T >::get_allocator ( ) const
inlineconstexprnoexcept

The allocator_type used to allocate items.

◆ insert() [1/5]

template<typename T >
template<std::input_iterator First, std::input_iterator Last>
iterator hi::v1::lean_vector< T >::insert ( const_iterator pos,
First first,
Last last )
inline

Insert new items.

If the items are not placed at the end, the items will be moved to the correct position after they have been copied to the end.

Parameters
posThe position where the items will be inserted.
firstAn iterator to the first item to copy.
lastAn iterator to one beyond the last item to copy.
Returns
An iterator pointing to the newly inserted item.

◆ insert() [2/5]

template<typename T >
iterator hi::v1::lean_vector< T >::insert ( const_iterator pos,
size_type count,
value_type const & value )
inline

Insert a new item.

If the item is not placed at the end, the item will be moved to the correct position after it is moved to the end.

Parameters
posThe position where the item will be inserted.
countThe number of value to add.
valueThe value to move into the vector.
Returns
An iterator pointing to the newly inserted item.

◆ insert() [3/5]

template<typename T >
iterator hi::v1::lean_vector< T >::insert ( const_iterator pos,
std::initializer_list< value_type > list )
inline

Insert new items.

If the items are not placed at the end, the items will be moved to the correct position after they have been copied to the end.

Parameters
posThe position where the items will be inserted.
listA initializer list of items to copy into the vector.
Returns
An iterator pointing to the newly inserted item.

◆ insert() [4/5]

template<typename T >
iterator hi::v1::lean_vector< T >::insert ( const_iterator pos,
value_type && value )
inline

Insert a new item.

If the item is not placed at the end, the item will be moved to the correct position after it is moved to the end.

Parameters
posThe position where the item will be inserted.
valueThe value to move into the vector.
Returns
An iterator pointing to the newly inserted item.

◆ insert() [5/5]

template<typename T >
iterator hi::v1::lean_vector< T >::insert ( const_iterator pos,
value_type const & value )
inline

Insert a new item.

If the item is not placed at the end, the item will be moved to the correct position after it is copied to the end.

Parameters
posThe position where the item will be inserted.
valueThe value to copy into the vector.
Returns
An iterator pointing to the newly inserted item.

◆ operator=() [1/2]

template<typename T >
lean_vector & hi::v1::lean_vector< T >::operator= ( lean_vector< T > && other)
inlinenoexcept

Move-assign a vector.

This will swap the allocations between the current and other vector. Or this will destroy the items in the current vector, then copy the items from other, potentially reusing the current allocation.

Parameters
otherThe vector to move.

◆ operator=() [2/2]

template<typename T >
lean_vector & hi::v1::lean_vector< T >::operator= ( lean_vector< T > const & other)
inlinenoexcept

Copy-assign a vector.

This will destroy the items in the current vector, then copy the items from other, potentially reusing the current allocation.

Parameters
otherThe vector to copy.

◆ operator[]() [1/2]

template<typename T >
const_reference hi::v1::lean_vector< T >::operator[] ( size_type index) const
inlinenoexcept

Get a const-reference to an item in the vector.

Note
It is undefined-behavior if the index is beyond the size of the vector.
Parameters
indexThe index to the item in the vector.
Returns
A reference to the item in the vector.

◆ operator[]() [2/2]

template<typename T >
reference hi::v1::lean_vector< T >::operator[] ( size_type index)
inlinenoexcept

Get a reference to an item in the vector.

Note
It is undefined-behavior if the index is beyond the size of the vector.
Parameters
indexThe index to the item in the vector.
Returns
A reference to the item in the vector.

◆ pop_back()

template<typename T >
void hi::v1::lean_vector< T >::pop_back ( )
inline

Remove the last item from the vector.

This function destroys the last item from the vector.

Note
It is undefined behavior to call this function on an empty vector.

◆ push_back() [1/2]

template<typename T >
void hi::v1::lean_vector< T >::push_back ( value_type && value)
inline

Move an item to the end of the vector.

Parameters
valueThe value to move to the vector.

◆ push_back() [2/2]

template<typename T >
void hi::v1::lean_vector< T >::push_back ( value_type const & value)
inline

Copy an item to the end of the vector.

Parameters
valueThe value to copy to the vector.

◆ reserve()

template<typename T >
void hi::v1::lean_vector< T >::reserve ( size_type new_capacity)
inline

Reserve capacity for items.

This will create a new allocation when new_capacity is larger than the current capacity and move the items from the previous allocation.

Parameters
new_capacityThe new capacity that the vector should take.
Note
This function is a no-op when the new_capacity is smaller than the current capacity.

◆ resize() [1/2]

template<typename T >
void hi::v1::lean_vector< T >::resize ( size_type new_size)
inline

Resize a vector.

When the new_size is larger than the current size a new allocation may be created and the new-items beyond the old size are default constructed.

When the new_size is smaller than the current size the allocation remains and the old-items beyond new_size are destroyed.

Parameters
new_sizeThe new size of the vector.

◆ resize() [2/2]

template<typename T >
void hi::v1::lean_vector< T >::resize ( size_type new_size,
value_type const & value )
inline

Resize a vector.

When the new_size is larger than the current size a new allocation may be created and the new-items beyond the old size are copied from value.

When the new_size is smaller than the current size the allocation remains and the old-items beyond new_size are destroyed.

Parameters
new_sizeThe new size of the vector.
valueThe value of the items being optionally created.

◆ short_capacity()

template<typename T >
constexpr size_t hi::v1::lean_vector< T >::short_capacity ( ) const
inlineconstexprnoexcept

The maximum number of items that can fit without allocation.

◆ shrink_to_fit()

template<typename T >
void hi::v1::lean_vector< T >::shrink_to_fit ( )
inline

Shrink the allocation to fit the current number of items.

If the current allocation is larger than the number of items in the vector, then a new allocation is created to exactly fit the number of items and the items are moved to the new allocation.

Note
This function is a no-op if the current allocation is the same as the number of items.

◆ size()

template<typename T >
constexpr size_type hi::v1::lean_vector< T >::size ( ) const
inlineconstexprnoexcept

Get the number of items in the vector.

Returns
The number of items in the vector.

Friends And Related Symbol Documentation

◆ erase [1/2]

template<typename T >
template<typename Pred >
size_type erase ( lean_vector< T > & c,
Pred pred )
friend

Erase items of a value from a vector.

Parameters
cThe vector with items
predA predicate function of the form bool(value_type const &).
Returns
The number of items removed.

◆ erase [2/2]

template<typename T >
size_type erase ( lean_vector< T > & c,
value_type const & value )
friend

Erase items of a value from a vector.

Parameters
cThe vector with items
valueThe value to remove from the vector.
Returns
The number of items removed.

◆ operator<=>

template<typename T >
auto operator<=> ( lean_vector< T > const & lhs,
lean_vector< T > const & rhs )
friend

Compare two vectors lexicographically.

Parameters
lhsThe left-hand-side vector.
rhsThe right-hand-side vector.
Returns
The three-way result of the lexicographical compare.

◆ operator==

template<typename T >
bool operator== ( lean_vector< T > const & lhs,
lean_vector< T > const & rhs )
friend

Compare two vectors.

Parameters
lhsThe left-hand-side vector.
rhsThe right-hand-side vector.
Return values
trueIf both vectors contain the same items.

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