Storage#

namespace librapid
template<typename Scalar_, typename Allocator_ = std::allocator<Scalar_>>
class Storage
#include <storage.hpp>

Public Types

using Allocator = Allocator_
using Scalar = Scalar_
using Pointer = typename std::allocator_traits<Allocator>::pointer
using ConstPointer = typename std::allocator_traits<Allocator>::const_pointer
using Reference = Scalar&
using ConstReference = const Scalar&
using SizeType = typename std::allocator_traits<Allocator>::size_type
using DifferenceType = typename std::allocator_traits<Allocator>::difference_type
using Iterator = Pointer
using ConstIterator = ConstPointer
using ReverseIterator = std::reverse_iterator<Iterator>
using ConstReverseIterator = std::reverse_iterator<ConstIterator>

Public Functions

Storage() = default

Default constructor.

explicit Storage(SizeType size, const Allocator &alloc = Allocator())

Create a Storage object with size elements and, optionally, a custom allocator.

Parameters
  • size – Number of elements to allocate

  • alloc – Allocator to use

explicit Storage(Scalar *begin, Scalar *end, bool ownsData)
Storage(SizeType size, ConstReference value, const Allocator &alloc = Allocator())

Create a Storage object with size elements, each initialized to value. Optionally, a custom allocator can be used.

Parameters
  • size – Number of elements to allocate

  • value – Value to initialize each element to

  • alloc – Allocator to use

Storage(const Storage &other, const Allocator &alloc = Allocator())

Create a Storage object from another Storage object. Additionally a custom allocator can be used.

Parameters
  • otherStorage object to copy

  • alloc – Allocator to use

Storage(Storage &&other) noexcept

Move a Storage object into this object.

Parameters

otherStorage object to move

template<typename V>
Storage(const std::initializer_list<V> &list, const Allocator &alloc = Allocator())

Create a Storage object from an std::initializer_list

Template Parameters

V – Type of the elements in the initializer list

Parameters
  • list – Initializer list to copy

  • alloc – Allocator to use

template<typename V>
explicit Storage(const std::vector<V> &vec, const Allocator &alloc = Allocator())

Create a Storage object from a std::vector

Template Parameters

V – Type of the elements in the vector

Parameters
  • vec – Vector to copy

  • alloc – Allocator to use

Storage &operator=(const Storage &other)

Assignment operator for a Storage object

Parameters

otherStorage object to copy

Returns

*this

Storage & operator= (Storage &&other) LIBRAPID_RELEASE_NOEXCEPT

Move assignment operator for a Storage object

Parameters

otherStorage object to move

Returns

*this

~Storage()

Free a Storage object.

void set(const Storage &other)
void resize(SizeType newSize)

Resize a Storage object to size elements. Existing elements are preserved.

Parameters

size – New size of the Storage object

void resize(SizeType newSize, int)

Resize a Storage object to size elements. Existing elements are not preserved

Parameters

size – New size of the Storage object

SizeType size() const noexcept

Return the number of elements in the Storage object

Returns

ConstReference operator[](SizeType index) const

Const access to the element at index index

Parameters

index – Index of the element to access

Returns

Const reference to the element at index index

Reference operator[](SizeType index)

Access to the element at index index

Parameters

index – Index of the element to access

Returns

Reference to the element at index index

Pointer data() const noexcept
Iterator begin() noexcept
Iterator end() noexcept
ConstIterator begin() const noexcept
ConstIterator end() const noexcept
ConstIterator cbegin() const noexcept
ConstIterator cend() const noexcept
ReverseIterator rbegin() noexcept
ReverseIterator rend() noexcept
ConstReverseIterator rbegin() const noexcept
ConstReverseIterator rend() const noexcept
ConstReverseIterator crbegin() const noexcept
ConstReverseIterator crend() const noexcept
template<typename V>
auto fromData(const std::initializer_list<V> &list) -> Storage
template<typename V>
auto fromData(const std::vector<V> &vec) -> Storage
template<typename ShapeType>
auto defaultShape() -> ShapeType

Public Static Functions

template<typename V>
static Storage fromData(const std::initializer_list<V> &vec)
template<typename V>
static Storage fromData(const std::vector<V> &vec)
template<typename ShapeType>
static ShapeType defaultShape()

Private Functions

template<typename P>
void initData(P begin, P end)#

Copy data from begin to end into this Storage object

Template Parameters

P – Pointer type

Parameters
  • begin – Beginning of data to copy

  • end – End of data to copy

void resizeImpl(SizeType newSize, int)#

Resize the Storage Object to newSize elements, retaining existing data.

Parameters

newSize – New size of the Storage object

void resizeImpl(SizeType newSize)#

Resize the Storage object to newSize elements. Note this does not initialize the new elements or maintain existing data.

Parameters

newSize – New size of the Storage object

Private Members

Allocator m_allocator#
Pointer m_begin = nullptr#
Pointer m_end = nullptr#
bool m_ownsData = true#
template<typename Scalar_, size_t... Size_>
class FixedStorage
#include <storage.hpp>

Public Types

using Scalar = Scalar_
using Pointer = Scalar*
using ConstPointer = const Scalar*
using Reference = Scalar&
using ConstReference = const Scalar&
using SizeType = size_t
using DifferenceType = ptrdiff_t
using Iterator = typename std::array<Scalar, product<Size_...>()>::iterator
using ConstIterator = typename std::array<Scalar, product<Size_...>()>::const_iterator
using ReverseIterator = std::reverse_iterator<Iterator>
using ConstReverseIterator = std::reverse_iterator<ConstIterator>

Public Functions

FixedStorage()

Default constructor.

explicit FixedStorage(const Scalar &value)

Create a FixedStorage object filled with value

Parameters

value – Value to fill the FixedStorage object with

FixedStorage(const FixedStorage &other)

Create a FixedStorage object from another FixedStorage object

Parameters

otherFixedStorage object to copy

FixedStorage(FixedStorage &&other) noexcept

Move constructor for a FixedStorage object

Parameters

otherFixedStorage object to move

explicit FixedStorage(const std::initializer_list<Scalar> &list)

Create a FixedStorage object from a std::initializer_list

Template Parameters

V – Type of the elements in the initializer list

Parameters

list – Initializer list to copy

explicit FixedStorage(const std::vector<Scalar> &vec)

Create a FixedStorage object from a std::vector

Template Parameters

V – Type of the elements in the vector

Parameters

vec – Vector to copy

FixedStorage &operator=(const FixedStorage &other)

Assignment operator for a FixedStorage object

Parameters

otherFixedStorage object to copy

Returns

*this

FixedStorage &operator=(FixedStorage &&other) noexcept

Move assignment operator for a FixedStorage object

Parameters

otherFixedStorage object to move

Returns

*this

~FixedStorage() = default

Free a FixedStorage object.

void resize(SizeType newSize)

Resize a Storage object to size elements. Existing elements are preserved.

Parameters

size – New size of the Storage object

void resize(SizeType newSize, int)

Resize a Storage object to size elements. Existing elements are not preserved

Parameters

size – New size of the Storage object

SizeType size() const noexcept

Return the number of elements in the FixedStorage object

Returns

Number of elements in the FixedStorage object

ConstReference operator[](SizeType index) const

Const access to the element at index index

Parameters

index – Index of the element to access

Returns

Const reference to the element at index index

Reference operator[](SizeType index)

Access to the element at index index

Parameters

index – Index of the element to access

Returns

Reference to the element at index index

Pointer data() const noexcept
Iterator begin() noexcept
Iterator end() noexcept
ConstIterator begin() const noexcept
ConstIterator end() const noexcept
ConstIterator cbegin() const noexcept
ConstIterator cend() const noexcept
ReverseIterator rbegin() noexcept
ReverseIterator rend() noexcept
ConstReverseIterator rbegin() const noexcept
ConstReverseIterator rend() const noexcept
ConstReverseIterator crbegin() const noexcept
ConstReverseIterator crend() const noexcept
template<typename ShapeType>
auto defaultShape() -> ShapeType

Public Static Functions

template<typename ShapeType>
static ShapeType defaultShape()

Public Static Attributes

static constexpr SizeType Size = product<Size_...>()

Private Members

std::array<Scalar, Size> m_data#
namespace detail

Functions

template<typename A>
std::allocator_traits<A>::pointer safeAllocate(A &alloc, typename std::allocator_traits<A>::size_type size)

Safely allocate memory for size elements using the allocator alloc. If the data can be trivially default constructed, then the constructor is not called and no data is initialized. Otherwise, the correct default constructor will be called for each element in the data, making sure the returned pointer is safe to use.

See also

safeDeallocate

Template Parameters

A – The allocator type to use

Parameters
  • alloc – The allocator object to use

  • size – Number of elements to allocate

Returns

Pointer to the first element

template<typename A>
void safeDeallocate(A &alloc, typename std::allocator_traits<A>::pointer ptr, typename std::allocator_traits<A>::size_type size)

Safely deallocate memory for size elements, using an std::allocator alloc. If the object cannot be trivially destroyed, the destructor will be called on each element of the data, ensuring that it is safe to free the allocated memory.

Template Parameters

A – The allocator type

Parameters
  • alloc – The allocator object

  • ptr – The pointer to free

  • size – The number of elements of type in the memory block

namespace typetraits

Functions

LIBRAPID_DEFINE_AS_TYPE (typename Scalar_ COMMA typename Allocator_, Storage< Scalar_ COMMA Allocator_ >)
template<typename Scalar_, typename Allocator_>
struct TypeInfo<Storage<Scalar_, Allocator_>>
#include <storage.hpp>

Public Types

using Scalar = Scalar_
using Backend = backend::CPU

Public Static Attributes

static constexpr bool isLibRapidType = true
template<typename Scalar_, size_t... Dims>
struct TypeInfo<FixedStorage<Scalar_, Dims...>>
#include <storage.hpp>

Public Types

using Scalar = Scalar_
using Backend = backend::CPU

Public Static Attributes

static constexpr bool isLibRapidType = true
template<typename T>
struct IsStorage : public std::false_type
#include <storage.hpp>
template<typename Scalar, typename Allocator>
struct IsStorage<Storage<Scalar, Allocator>> : public std::true_type
#include <storage.hpp>
template<typename T>
struct IsFixedStorage : public std::false_type
#include <storage.hpp>
template<typename Scalar, size_t... Size>
struct IsFixedStorage<FixedStorage<Scalar, Size...>> : public std::true_type
#include <storage.hpp>