HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
v1::gap_buffer< T, Allocator > Class Template Reference

#include <hikogui/gap_buffer.hpp>

Data Structures

class  iterator
class  const_iterator

Public Types

using value_type = T
using allocator_type = Allocator
using size_type = std::size_t
using difference_type = ptrdiff_t
using reference = T&
using const_reference = T const&
using pointer = T *
using const_pointer = T const *

Public Member Functions

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

Friends

constexpr friend iterator begin (gap_buffer &rhs) noexcept
constexpr friend const_iterator begin (gap_buffer const &rhs) noexcept
constexpr friend const_iterator cbegin (gap_buffer const &rhs) noexcept
constexpr friend iterator end (gap_buffer &rhs) noexcept
constexpr friend const_iterator end (gap_buffer const &rhs) noexcept
constexpr friend const_iterator cend (gap_buffer const &rhs) noexcept
constexpr friend bool operator== (gap_buffer const &lhs, gap_buffer const &rhs) noexcept
template<typename Container>
constexpr friend bool operator== (gap_buffer const &lhs, Container const &rhs) noexcept
template<typename Container>
constexpr friend bool operator== (Container const &lhs, gap_buffer const &rhs) noexcept

Detailed Description

template<typename T, typename Allocator = std::allocator<T>>
class v1::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>>
v1::gap_buffer< T, Allocator >::gap_buffer ( allocator_type const & allocator = allocator_type{})
inlineconstexprnoexcept

Construct an empty buffer.

◆ gap_buffer() [2/4]

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

Construct a buffer with the given initializer list.

◆ gap_buffer() [3/4]

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

Copy constructor.

Allocates memory and copies all items from other into this.

◆ gap_buffer() [4/4]

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

Move constructor.

This constructor will move the allocation of the other gap_buffer.

◆ ~gap_buffer()

template<typename T, typename Allocator = std::allocator<T>>
v1::gap_buffer< T, Allocator >::~gap_buffer ( )
inlineconstexpr

Destructor.

Destroys all items and deallocates the buffer.

Member Function Documentation

◆ at() [1/2]

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

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 v1::gap_buffer< T, Allocator >::at ( size_type index) const
inlinenodiscardconstexpr

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 v1::gap_buffer< T, Allocator >::clear ( )
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::emplace_after ( const_iterator position,
Args &&... args )
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::emplace_before ( const_iterator position,
Args &&... args )
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::erase ( const_iterator first,
const_iterator last )
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::erase ( const_iterator position)
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::insert_after ( const_iterator position,
It first,
It last )
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::insert_after ( const_iterator position,
value_type && value )
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::insert_after ( const_iterator position,
value_type const & value )
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::insert_before ( const_iterator position,
It first,
It last )
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::insert_before ( const_iterator position,
value_type && value )
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::insert_before ( const_iterator position,
value_type const & value )
inlineconstexprnoexcept

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 & v1::gap_buffer< T, Allocator >::operator= ( gap_buffer< T, Allocator > && other)
inlineconstexprnoexcept

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 & v1::gap_buffer< T, Allocator >::operator= ( gap_buffer< T, Allocator > const & other)
inlineconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::operator[] ( size_type index) const
inlinenodiscardconstexprnoexcept

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 v1::gap_buffer< T, Allocator >::operator[] ( size_type index)
inlinenodiscardconstexprnoexcept

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: