HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Friends
tt::gap_buffer< T, Allocator > Class Template Reference

#include <ttauri/gap_buffer.hpp>

Public Types

using value_type = T
 
using allocator_type = Allocator
 
using size_type = size_t
 
using difference_type = ptrdiff_t
 
using reference = T &
 
using const_reference = T const &
 
using pointer = T *
 
using const_pointer = T const *
 
using iterator = gap_buffer_iterator<T>
 
using const_iterator = gap_buffer_iterator<T const>
 

Public Member Functions

 gap_buffer (allocator_type const &allocator=allocator_type{}) noexcept
 Construct an empty buffer.
 
 gap_buffer (std::initializer_list< T > init, allocator_type const &allocator=allocator_type{})
 Construct a buffer with the given initializer list.
 
 gap_buffer (gap_buffer const &other) noexcept
 Copy constructor.
 
gap_bufferoperator= (gap_buffer const &other) noexcept
 Copy assignment.
 
 gap_buffer (gap_buffer &&other) noexcept
 Move constructor.
 
gap_bufferoperator= (gap_buffer &&other) noexcept
 Move assignment operator.
 
 ~gap_buffer ()
 Destructor.
 
reference operator[] (size_type index) noexcept
 Index operator.
 
const_reference operator[] (size_type index) const noexcept
 Index operator.
 
reference at (size_type index)
 Get item to reference at.
 
const_reference at (size_type index) const
 Get item to reference at.
 
reference front () noexcept
 
const_reference front () const noexcept
 
reference back () noexcept
 
const_reference back () const noexcept
 
void pop_back () noexcept
 
void pop_front () noexcept
 
void clear () noexcept
 Clears the buffer.
 
size_t size () const noexcept
 
bool empty () const noexcept
 
size_t capacity () const noexcept
 
void reserve (size_t new_capacity) noexcept
 
iterator begin () noexcept
 
const_iterator begin () const noexcept
 
const_iterator cbegin () const noexcept
 
iterator end () noexcept
 
const_iterator end () const noexcept
 
const_iterator cend () const noexcept
 
template<typename... Args>
reference emplace_back (Args &&...args) noexcept
 
void push_back (value_type const &value) noexcept
 
void push_back (value_type &&value) noexcept
 
template<typename... Args>
reference emplace_front (Args &&...args) noexcept
 
void push_front (value_type const &value) noexcept
 
void push_front (value_type &&value) noexcept
 
template<typename... Args>
reference emplace_before (const_iterator position, Args &&...args) noexcept
 Place the gap at the position and emplace at the end of the gap.
 
iterator insert_before (const_iterator position, value_type const &value) noexcept
 Place the gap at the position and emplace at the end of the gap.
 
iterator insert_before (const_iterator position, value_type &&value) noexcept
 Place the gap at the position and emplace at the end of the gap.
 
template<typename It >
iterator insert_before (const_iterator position, It first, It last) noexcept
 Insert items If an insert requires a reallocation then all current iterators become invalid.
 
template<typename... Args>
reference emplace_after (const_iterator position, Args &&...args) noexcept
 Place the gap at the position and emplace at the beginning of the gap.
 
iterator insert_after (const_iterator position, value_type const &value) noexcept
 Place the gap at the position and emplace at the beginning of the gap.
 
iterator insert_after (const_iterator position, value_type &&value) noexcept
 Place the gap at the position and emplace at the beginning of the gap.
 
template<typename It >
iterator insert_after (const_iterator position, It first, It last) noexcept
 Insert items.
 
iterator erase (const_iterator first, const_iterator last) noexcept
 Erase items.
 
iterator erase (const_iterator position) noexcept
 Erase item.
 

Friends

template<typename IT >
class gap_buffer_iterator
 
bool operator== (gap_buffer const &lhs, gap_buffer const &rhs) noexcept
 
template<typename Container >
bool operator== (gap_buffer const &lhs, Container const &rhs) noexcept
 
template<typename Container >
bool operator== (Container const &lhs, gap_buffer const &rhs) noexcept
 

Detailed Description

template<typename T, typename Allocator = std::allocator<T>>
class tt::gap_buffer< T, Allocator >

Gap Buffer This container is similar to a std::vector, optimized for repeated insertions and deletion at the same position.

This container is especially useful for text editing where inserts and deletes are happening at a cursor.

Like a std::vector a gap_buffer has extra capacity to do insertion without needing to reallocate, however this capacity can be located anywhere in the allocated memory in a single continues region called the gap.

When inserting/deleting data in the gap_buffer the gap will move to this location.

Constructor & Destructor Documentation

◆ gap_buffer() [1/4]

template<typename T , typename Allocator = std::allocator<T>>
tt::gap_buffer< T, Allocator >::gap_buffer ( allocator_type const & allocator = allocator_type{})
inlinenoexcept

Construct an empty buffer.

◆ gap_buffer() [2/4]

template<typename T , typename Allocator = std::allocator<T>>
tt::gap_buffer< T, Allocator >::gap_buffer ( std::initializer_list< T > init,
allocator_type const & allocator = allocator_type{} )
inline

Construct a buffer with the given initializer list.

◆ gap_buffer() [3/4]

template<typename T , typename Allocator = std::allocator<T>>
tt::gap_buffer< T, Allocator >::gap_buffer ( gap_buffer< T, Allocator > const & other)
inlinenoexcept

Copy constructor.

Allocates memory and copies all items from other into this.

◆ gap_buffer() [4/4]

template<typename T , typename Allocator = std::allocator<T>>
tt::gap_buffer< T, Allocator >::gap_buffer ( gap_buffer< T, Allocator > && other)
inlinenoexcept

Move constructor.

This constructor will move the allocation of the other gap_buffer.

◆ ~gap_buffer()

template<typename T , typename Allocator = std::allocator<T>>
tt::gap_buffer< T, Allocator >::~gap_buffer ( )
inline

Destructor.

Destroys all items and deallocates the buffer.

Member Function Documentation

◆ at() [1/2]

template<typename T , typename Allocator = std::allocator<T>>
reference tt::gap_buffer< T, Allocator >::at ( size_type index)
inline

Get item to reference at.

Exceptions
std::out_of_rangeThrown when index is out of range.
Parameters
indexThe index in the buffer.
Returns
A reference to the item in the buffer.

◆ at() [2/2]

template<typename T , typename Allocator = std::allocator<T>>
const_reference tt::gap_buffer< T, Allocator >::at ( size_type index) const
inline

Get item to reference at.

Exceptions
std::out_of_rangeThrown when index is out of range.
Parameters
indexThe index in the buffer.
Returns
A reference to the item in the buffer.

◆ clear()

template<typename T , typename Allocator = std::allocator<T>>
void tt::gap_buffer< T, Allocator >::clear ( )
inlinenoexcept

Clears the buffer.

Destroys all items in the buffer. This function will keep the memory allocated.

After this object was move() this function will allow the object to be reused.

◆ emplace_after()

template<typename T , typename Allocator = std::allocator<T>>
template<typename... Args>
reference tt::gap_buffer< T, Allocator >::emplace_after ( const_iterator position,
Args &&... args )
inlinenoexcept

Place the gap at the position and emplace at the beginning of the gap.

If an insert requires a reallocation (size() == capacity()) then all current iterators become invalid.

◆ emplace_before()

template<typename T , typename Allocator = std::allocator<T>>
template<typename... Args>
reference tt::gap_buffer< T, Allocator >::emplace_before ( const_iterator position,
Args &&... args )
inlinenoexcept

Place the gap at the position and emplace at the end of the gap.

If an insert requires a reallocation (size() == capacity()) then all current iterators become invalid.

◆ erase() [1/2]

template<typename T , typename Allocator = std::allocator<T>>
iterator tt::gap_buffer< T, Allocator >::erase ( const_iterator first,
const_iterator last )
inlinenoexcept

Erase items.

Parameters
firstLocation of first item to remove.
lastLocation beyond last item to remove.
Returns
iterator pointing to the element past the removed item, or end().

◆ erase() [2/2]

template<typename T , typename Allocator = std::allocator<T>>
iterator tt::gap_buffer< T, Allocator >::erase ( const_iterator position)
inlinenoexcept

Erase item.

Parameters
positionLocation of item to remove
Returns
iterator pointing to the element past the removed item, or end().

◆ insert_after() [1/3]

template<typename T , typename Allocator = std::allocator<T>>
template<typename It >
iterator tt::gap_buffer< T, Allocator >::insert_after ( const_iterator position,
It first,
It last )
inlinenoexcept

Insert items.

Parameters
positionLocation to insert at.
firstThe first item to insert.
lastThe one beyond last item to insert.
Returns
The iterator pointing to the last item inserted.

◆ insert_after() [2/3]

template<typename T , typename Allocator = std::allocator<T>>
iterator tt::gap_buffer< T, Allocator >::insert_after ( const_iterator position,
value_type && value )
inlinenoexcept

Place the gap at the position and emplace at the beginning of the gap.

If an insert requires a reallocation (size() == capacity()) then all current iterators become invalid.

◆ insert_after() [3/3]

template<typename T , typename Allocator = std::allocator<T>>
iterator tt::gap_buffer< T, Allocator >::insert_after ( const_iterator position,
value_type const & value )
inlinenoexcept

Place the gap at the position and emplace at the beginning of the gap.

If an insert requires a reallocation (size() == capacity()) then all current iterators become invalid.

◆ insert_before() [1/3]

template<typename T , typename Allocator = std::allocator<T>>
template<typename It >
iterator tt::gap_buffer< T, Allocator >::insert_before ( const_iterator position,
It first,
It last )
inlinenoexcept

Insert items If an insert requires a reallocation then all current iterators become invalid.

Parameters
positionLocation to insert before.
firstThe first item to insert.
lastThe one beyond last item to insert.
Returns
The iterator pointing to the first item inserted.

◆ insert_before() [2/3]

template<typename T , typename Allocator = std::allocator<T>>
iterator tt::gap_buffer< T, Allocator >::insert_before ( const_iterator position,
value_type && value )
inlinenoexcept

Place the gap at the position and emplace at the end of the gap.

If an insert requires a reallocation (size() == capacity()) then all current iterators become invalid.

◆ insert_before() [3/3]

template<typename T , typename Allocator = std::allocator<T>>
iterator tt::gap_buffer< T, Allocator >::insert_before ( const_iterator position,
value_type const & value )
inlinenoexcept

Place the gap at the position and emplace at the end of the gap.

If an insert requires a reallocation (size() == capacity()) then all current iterators become invalid.

◆ operator=() [1/2]

template<typename T , typename Allocator = std::allocator<T>>
gap_buffer & tt::gap_buffer< T, Allocator >::operator= ( gap_buffer< T, Allocator > && other)
inlinenoexcept

Move assignment operator.

This functional will allocate its own buffer and move the items from other.

◆ operator=() [2/2]

template<typename T , typename Allocator = std::allocator<T>>
gap_buffer & tt::gap_buffer< T, Allocator >::operator= ( gap_buffer< T, Allocator > const & other)
inlinenoexcept

Copy assignment.

This copies the items from the other buffer to this buffer. This may reuse allocation of this buffer if it was allocated before.

Complexity is linear with the number of items in other.

◆ operator[]() [1/2]

template<typename T , typename Allocator = std::allocator<T>>
const_reference tt::gap_buffer< T, Allocator >::operator[] ( size_type index) const
inlinenoexcept

Index operator.

Return a reference to the ittem at index.

Parameters
indexThe index in the buffer.
Returns
A reference to the item in the buffer.

◆ operator[]() [2/2]

template<typename T , typename Allocator = std::allocator<T>>
reference tt::gap_buffer< T, Allocator >::operator[] ( size_type index)
inlinenoexcept

Index operator.

Return a reference to the item at index.

Parameters
indexThe index in the buffer.
Returns
A reference to the item in the buffer.

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