generated from cpp-best-practices/cmake_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.hpp
More file actions
67 lines (55 loc) · 2.04 KB
/
array.hpp
File metadata and controls
67 lines (55 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <concepts>
#include "flexible_array_checked.hpp"
#include "library.h"
template <typename Element>
requires std::movable<Element> && std::destructible<Element>
class Array
{
struct Header
{
Int count;
Int capacity;
[[nodiscard]] explicit Header(const Int count, const Int capacity) noexcept : count(count), capacity(capacity)
{
}
/// Returns the number of elements of the storage (capacity)
///
/// Satisfies TrailingElementCountProvider concept.
[[nodiscard]] Int trailing_element_count() const { return capacity; }
};
static_assert(TrailingElementCountProvider<Header>);
/// The underlying storage for the array.
///
/// May be invalid while the capacity is zero.
FlexibleArrayChecked<Header, Element> storage;
/// Constructs the Array with given storage.
[[nodiscard]] explicit Array(FlexibleArrayChecked<Header, Element>&& storage) noexcept : storage(std::move(storage))
{
}
public:
/// Create an empty array with no heap allocation and zero capacity.
[[nodiscard]] static auto create_empty() noexcept -> Array
{
return Array{FlexibleArrayChecked<Header, Element>::create_empty()};
}
/// Creates an array with given capacity, heap-allocating storage unless capacity is zero.
[[nodiscard]] static auto create_empty(const Int capacity) noexcept -> Array
{
precondition(capacity >= 0);
if (capacity == 0)
{
return Array::create_empty();
}
return Array{FlexibleArrayChecked<Header, Element>::with_header(capacity, Header{0, capacity})};
}
/// The number of initialized elements in the array.
[[nodiscard]] constexpr auto count() const noexcept -> Int
{
return storage.is_valid() ? storage.header()->count : 0;
}
/// The number of elements the array currently has allocated space for.
[[nodiscard]] constexpr auto capacity() const noexcept -> Int
{
return storage.is_valid() ? storage.capacity() : 0;
}
};