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

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 swap (Nlist &rhs) noexcept
 Swaps the container.
 
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 A1 >
pointer EmplaceBack (const A1 &a1)
 Adds elements in-place. More...
 
template<class A1 , class A2 >
pointer EmplaceBack (const A1 &a1, const A2 &a2)
 Adds elements in-place. More...
 
template<class A1 , class A2 , class A3 >
pointer EmplaceBack (const A1 &a1, const A2 &a2, const A3 &a3)
 Adds elements in-place. More...
 
template<class A1 , class A2 , class A3 , class A4 >
pointer EmplaceBack (const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
 Adds elements in-place. More...
 
template<class A1 , class A2 , class A3 , class A4 , class A5 >
pointer EmplaceBack (const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5)
 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.
 
Nlistassign (Nlist &rhs, move_tag)
 Assigns the object by using swap for a move.
 
 Nlist (Nlist &rhs, move_tag)
 Instantiates the object by using swap for a move.
 
 Nlist (Nlist &&rhs)
 Instantiates the object (move constructor). This function is useful when using C++11.
 
Nlistoperator= (Nlist &&rhs)
 Move assignment operator. This function is useful when using C++11.
 

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 19 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 426 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 441 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 445 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 368 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 449 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 462 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 535 of file Nlist.h.

§ EmplaceBack() [1/5]

template<class T, class AL = std::allocator<char>>
template<class A1 >
nn::nlib::Nlist< T, AL >::EmplaceBack ( const A1 &  a1)
inline

Adds elements in-place.

Template Parameters
A1The type for an element type T constructor argument.
Parameters
[in]a1An argument to forward to the element type T constructor.
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 484 of file Nlist.h.

§ EmplaceBack() [2/5]

template<class T, class AL = std::allocator<char>>
template<class A1 , class A2 >
nn::nlib::Nlist< T, AL >::EmplaceBack ( const A1 &  a1,
const A2 &  a2 
)
inline

Adds elements in-place.

Template Parameters
A1The type for an element type T constructor argument.
A2The type for an element type T constructor argument.
Parameters
[in]a1An argument to forward to the element type T constructor.
[in]a2An argument to forward to the element type T constructor.
Returns
Returns the pointer to the added element. NULL if process fails.

Definition at line 492 of file Nlist.h.

§ EmplaceBack() [3/5]

template<class T, class AL = std::allocator<char>>
template<class A1 , class A2 , class A3 >
nn::nlib::Nlist< T, AL >::EmplaceBack ( const A1 &  a1,
const A2 &  a2,
const A3 &  a3 
)
inline

Adds elements in-place.

Template Parameters
A1The type for an element type T constructor argument.
A2The type for an element type T constructor argument.
A3The type for an element type T constructor argument.
Parameters
[in]a1An argument to forward to the element type T constructor.
[in]a2An argument to forward to the element type T constructor.
[in]a3An argument to forward to the element type T constructor.
Returns
Returns the pointer to the added element. NULL if process fails.

Definition at line 501 of file Nlist.h.

§ EmplaceBack() [4/5]

template<class T, class AL = std::allocator<char>>
template<class A1 , class A2 , class A3 , class A4 >
nn::nlib::Nlist< T, AL >::EmplaceBack ( const A1 &  a1,
const A2 &  a2,
const A3 &  a3,
const A4 &  a4 
)
inline

Adds elements in-place.

Template Parameters
A1The type for an element type T constructor argument.
A2The type for an element type T constructor argument.
A3The type for an element type T constructor argument.
A4The type for an element type T constructor argument.
Parameters
[in]a1An argument to forward to the element type T constructor.
[in]a2An argument to forward to the element type T constructor.
[in]a3An argument to forward to the element type T constructor.
[in]a4An argument to forward to the element type T constructor.
Returns
Returns the pointer to the added element. NULL if process fails.

Definition at line 511 of file Nlist.h.

§ EmplaceBack() [5/5]

template<class T, class AL = std::allocator<char>>
template<class A1 , class A2 , class A3 , class A4 , class A5 >
nn::nlib::Nlist< T, AL >::EmplaceBack ( const A1 &  a1,
const A2 &  a2,
const A3 &  a3,
const A4 &  a4,
const A5 &  a5 
)
inline

Adds elements in-place.

Template Parameters
A1The type for an element type T constructor argument.
A2The type for an element type T constructor argument.
A3The type for an element type T constructor argument.
A4The type for an element type T constructor argument.
A5The type for an element type T constructor argument.
Parameters
[in]a1An argument to forward to the element type T constructor.
[in]a2An argument to forward to the element type T constructor.
[in]a3An argument to forward to the element type T constructor.
[in]a4An argument to forward to the element type T constructor.
[in]a5An argument to forward to the element type T constructor.
Returns
Returns the pointer to the added element. NULL if process fails.

Definition at line 523 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 372 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 454 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 458 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 431 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 418 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 436 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 403 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 377 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 661 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 373 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 365 of file Nlist.h.


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