Sane C++ Libraries
C++ Platform Abstraction Libraries
SmallVector.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Containers/Array.h"
5#include "../Containers/Vector.h"
6
7namespace SC
8{
9template <typename T, int N>
10struct SmallVector;
11} // namespace SC
12
15
25template <typename T, int N>
26struct SC::SmallVector : public Vector<T>
27{
28 Array<T, N> buffer;
30 {
32 static_assert(SC_COMPILER_OFFSETOF(SmallVector, buffer) == alignof(SegmentHeader), "Wrong Alignment");
34 init();
35 }
37 {
38 init();
40 }
41 SmallVector(const SmallVector& other)
42 {
43 init();
45 }
46 SmallVector& operator=(SmallVector&& other)
47 {
49 return *this;
50 }
51 SmallVector& operator=(const SmallVector& other)
52 {
54 return *this;
55 }
56
57 SmallVector(Vector<T>&& other)
58 {
59 init();
61 }
62 SmallVector(const Vector<T>& other)
63 {
64 init();
66 }
67 SmallVector& operator=(Vector<T>&& other)
68 {
70 return *this;
71 }
72 SmallVector& operator=(const Vector<T>& other)
73 {
75 return *this;
76 }
77
78 private:
79 void init()
80 {
81 SegmentHeader* header = SegmentHeader::getSegmentHeader(buffer.items);
82 header->isSmallVector = true;
83 Vector<T>::items = buffer.items;
84 }
85};
#define SC_COMPILER_WARNING_POP
Pops warning from inside a macro.
Definition: Compiler.h:107
#define SC_COMPILER_OFFSETOF(Class, Field)
Returns offset of Class::Field in bytes.
Definition: Compiler.h:111
#define SC_COMPILER_WARNING_PUSH_OFFSETOF
Disables invalid-offsetof gcc and clang warning.
Definition: Compiler.h:131
constexpr T && forward(typename TypeTraits::RemoveReference< T >::type &value)
Forwards an lvalue or an rvalue as an rvalue reference.
Definition: Compiler.h:272
A contiguous sequence of elements kept inside its inline storage.
Definition: Array.h:43
A Vector that can hold up to N elements inline and > N on heap.
Definition: SmallVector.h:27
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51
Vector & operator=(Vector &&other)
Move assigns another vector to this one.
Definition: Vector.h:395