nlib
nn::nlib::Nlist< T, AL > Class Template Reference

A container-like class similar to std::vector that can store objects that do not have copy constructors. More...

#include "nn/nlib/Nlist.h"

Inherits nn::nlib::nlist::NlistBaseT< T >, and nn::nlib::nlist::NlistAlloc< is_empty, AL >.

Public Types

Typedef
typedef BaseType::value_type value_type
 Element type T.
 
typedef AL allocator_type
 Allocator type AL.
 
typedef BaseType::size_type size_type
 Unsigned integer type (size_t).
 
typedef BaseType::difference_type difference_type
 Signed integer type (ptrdiff_t).
 
typedef BaseType::reference reference
 T&.
 
typedef BaseType::reference const_reference
 const T&.
 
typedef BaseType::pointer pointer
 T*.
 
typedef BaseType::const_pointer const_pointer
 const T*.
 
typedef BaseType::iterator iterator
 Forward iterator.
 
typedef BaseType::const_iterator const_iterator
 Read-only forward iterator.
 

Public Member Functions

void swap (Nlist &rhs) noexcept
 Swaps the container. More...
 
size_type size () const noexcept
 Returns the number of stored elements. More...
 
size_type capacity () const noexcept
 Returns the number of allocated elements. More...
 
bool empty () const noexcept
 Checks whether the container is empty. More...
 
bool resize (size_type n) noexcept
 Resizes the container. More...
 
bool reserve (size_type n) noexcept
 Allocates memory for n number of elements. More...
 
pointer push_back ()
 Adds an element to the end and initializes it with the default constructor. More...
 
bool pop_back () noexcept
 Deletes the last element. More...
 
void clear () noexcept
 Clears the container.
 
allocator_type get_allocator () const noexcept
 Gets the allocator. More...
 
const_reference back () const
 The same as back.
 
reference back ()
 Returns a reference to the last element. More...
 
const_reference front () const
 The same as front.
 
reference front ()
 Returns a reference to the first element. More...
 
const_reference operator[] (size_type idx) const
 The same as operator[](size_t idx).
 
reference operator[] (size_type idx)
 Uses an index to reference an element. More...
 
iterator begin () noexcept
 Returns the iterator pointing to the beginning of the container. More...
 
const_iterator begin () const noexcept
 Returns the iterator pointing to the beginning of the container. More...
 
const_iterator cbegin () const noexcept
 Returns the iterator pointing to the beginning of the container. More...
 
iterator end () noexcept
 Returns the iterator pointing to the end of the container. More...
 
const_iterator end () const noexcept
 Returns the iterator pointing to the end of the container. More...
 
const_iterator cend () const noexcept
 Returns the iterator pointing to the end of the container. More...
 
void shrink_to_fit () noexcept
 Returns allocated memory, if possible.
 
template<class... Args>
pointer EmplaceBack (Args &&... args)
 Adds elements in-place. More...
 
void Clobber () noexcept
 Returns the container empty without calling the element destructor and without releasing the memory for the container. More...
 
Basic Member Functions
 Nlist () noexcept
 Instantiates the object with default parameters (default constructor). Creates an empty container.
 
 Nlist (const AL &al) noexcept
 Specifies an allocator. Creates an empty container.
 
 ~Nlist () noexcept
 Destructor.
 
 Nlist (Nlist &&rhs)=default
 Instantiates the object (move constructor). This function is useful when using C++11.
 
Nlistoperator= (Nlist &&rhs) noexcept
 Move assignment operator. This function is useful when using C++11.
 
 Nlist (Nlist &rhs, move_tag) noexcept
 Corresponds to a move constructor.
 
Nlistassign (Nlist &rhs, move_tag) noexcept
 Corresponds to a move assignment operator.
 

Detailed Description

template<class T, class AL = std::allocator<char>>
class nn::nlib::Nlist< T, AL >

A container-like class similar to std::vector that can store objects that do not have copy constructors.

Template Parameters
TElement type.
ALThe allocator type. The default is std::allocator<char>.
Description
The following bullet points describe details about the behavior and list cautions for this class.
  • std::push_back can crash due to an out-of-memory event when elements are added (providing that exceptions are not enabled). With the Nlist class, the NULL pointer is returned if adding an element fails, so you can deal with problems by checking the return values.
  • Objects are not relocated. The way std::vector generally works is that when elements are added (or when reserve is called), more memory is allocated and the data is copied to that memory. With Nlist, new memory is allocated only for the added elements, and there is no relocation of objects.
    As a result of this behavior, push_back is faster than with std::vector because there are no costs associated with copying the memory required for relocation and freeing the original memory. In addition, memory allocation can be more efficient because you can use a simple allocator like a stack allocator.
  • The coding inside Nlist does not make use of an element type T copy constructor. As a result, an element type T copy constructor does not need to be defined.
  • With Nlist, elements can be added by in-place initialization. Because Nlist does not use a copy constructor, the standard push_back function does not need to be implemented.
    Elements can be added using the following member functions.
    • T* push_back(void): Adds elements created by the default constructor.
    • T* EmplaceBack(argument list to forward to the T constructor): Passes an argument list and Nlist starts the constructor internally to add elements (which avoids invoking the copy constructor).
  • With Nlist, you implement forward iterators.
    • There is a list of arrays of different sizes in Nlist, so referencing in the forward direction of the iterator is as fast as std::vector.
    • The execution efficiency of the std::advance function is \(O(\log n)\).
    • The execution efficiency of the std::distance function is \(O(1)\).
  • Supports the square brackets operator ([]). However, the execution efficiency is \(O(\log idx)\).
  • The kinds of constructors defined by Nlist are limited. The reasons for this limitation are as follows.
    • Avoids out-of-memory problems.
    • The element type does not require a copy constructor.
Examples:
msgpack/json/json.cpp, and msgpack/jsonrpc/jsonrpc.cpp.

Definition at line 32 of file Nlist.h.

Member Function Documentation

◆ back()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::back ( )
inline

Returns a reference to the last element.

Returns
The reference to the last element.

Definition at line 547 of file Nlist.h.

◆ begin() [1/2]

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::begin ( )
inlinenoexcept

Returns the iterator pointing to the beginning of the container.

Returns
The iterator pointing to the beginning of the container.

Definition at line 562 of file Nlist.h.

◆ begin() [2/2]

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::begin ( ) const
inlinenoexcept

Returns the iterator pointing to the beginning of the container.

Returns
The iterator pointing to the beginning of the container.

Definition at line 566 of file Nlist.h.

◆ capacity()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::capacity ( ) const
inlinenoexcept

Returns the number of allocated elements.

Returns
The number of elements.

Definition at line 498 of file Nlist.h.

◆ cbegin()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::cbegin ( ) const
inlinenoexcept

Returns the iterator pointing to the beginning of the container.

Returns
The (const_iterator iterator pointing to the beginning of the container.

Definition at line 570 of file Nlist.h.

◆ cend()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::cend ( ) const
inlinenoexcept

Returns the iterator pointing to the end of the container.

Returns
The (const_iterator iterator pointing to the end of the container.

Definition at line 583 of file Nlist.h.

◆ Clobber()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::Clobber ( )
inlinenoexcept

Returns the container empty without calling the element destructor and without releasing the memory for the container.

Description
Use this function only if you have prepared a special allocator and otherwise created a situation where it would not cause problems to entirely skip the processes of the destructor and freeing memory. This function enables fast processing after it has been used.

Definition at line 656 of file Nlist.h.

◆ EmplaceBack()

template<class T, class AL = std::allocator<char>>
template<class... Args>
nn::nlib::Nlist< T, AL >::EmplaceBack ( Args &&...  args)
inline

Adds elements in-place.

Template Parameters
ArgsThe template parameter pack.
Parameters
[in]argsThe argument for constructing the elements.
Returns
Returns the pointer to the added element. NULL if process fails.
Description
Adds an element at the end of Nlist. The element is constructed in-place, and a copy constructor is not invoked. A user-specified argument is passed to the constructor.

Definition at line 596 of file Nlist.h.

◆ empty()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::empty ( ) const
inlinenoexcept

Checks whether the container is empty.

Returns
Returns true if empty.

Definition at line 502 of file Nlist.h.

◆ end() [1/2]

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::end ( )
inlinenoexcept

Returns the iterator pointing to the end of the container.

Returns
The iterator pointing to the end of the container.

Definition at line 575 of file Nlist.h.

◆ end() [2/2]

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::end ( ) const
inlinenoexcept

Returns the iterator pointing to the end of the container.

Returns
The iterator pointing to the end of the container.

Definition at line 579 of file Nlist.h.

◆ front()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::front ( )
inline

Returns a reference to the first element.

Returns
The reference to the first element.

Definition at line 552 of file Nlist.h.

◆ get_allocator()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::get_allocator ( ) const
inlinenoexcept

Gets the allocator.

Returns
The allocator.

Definition at line 539 of file Nlist.h.

◆ operator[]()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::operator[] ( size_type  idx)
inline

Uses an index to reference an element.

Parameters
[in]idxThe index of an element.
Returns
The reference for the element.

Definition at line 557 of file Nlist.h.

◆ pop_back()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::pop_back ( )
inlinenoexcept

Deletes the last element.

Returns
Returns true when successful.

Definition at line 533 of file Nlist.h.

◆ push_back()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::push_back ( void  )
inline

Adds an element to the end and initializes it with the default constructor.

Returns
Returns the pointer to the added element. NULL if process fails.

Definition at line 507 of file Nlist.h.

◆ reserve()

template<class T , class AL >
bool nn::nlib::Nlist< T, AL >::reserve ( size_type  n)
noexcept

Allocates memory for n number of elements.

Parameters
[in]nThe number of elements to allocate for.
Returns
Returns true on success.

Definition at line 782 of file Nlist.h.

◆ resize()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::resize ( size_type  n)
inlinenoexcept

Resizes the container.

Parameters
[in]nSize after resizing.
Returns
Returns true on success.

Definition at line 503 of file Nlist.h.

◆ size()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::size ( ) const
inlinenoexcept

Returns the number of stored elements.

Returns
The number of elements.

Definition at line 495 of file Nlist.h.

◆ swap()

template<class T, class AL = std::allocator<char>>
nn::nlib::Nlist< T, AL >::swap ( Nlist< T, AL > &  rhs)
inlinenoexcept

Swaps the container.

Deprecated:
This function will be deleted in a future release.

Definition at line 487 of file Nlist.h.


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