supereight
Loading...
Searching...
No Matches
bounded_vector.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Smart Robotics Lab, Imperial College London, Technical University of Munich
3 * SPDX-FileCopyrightText: 2023 Sotiris Papatheodorou
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef SE_COMMON_BOUNDED_VECTOR_HPP
8#define SE_COMMON_BOUNDED_VECTOR_HPP
9
10#include <vector>
11
12#include "math_util.hpp"
13
14namespace se {
15
16namespace detail {
17
22template<typename T, std::size_t N>
24 static_assert(math::is_power_of_two(N), "std::vector typically allocates powers of 2");
25
26 T data_[N];
27
28 public:
29 typedef T value_type;
32 typedef std::true_type propagate_on_container_swap;
33
34 static constexpr std::size_t capacity = N;
35
36 template<typename U>
37 struct rebind {
39 };
40
41 T* allocate(std::size_t n)
42 {
43 if (n > N) {
44 throw std::bad_alloc();
45 }
46 return data_;
47 }
48
49 void deallocate(T*, std::size_t)
50 {
51 // Nothing to do on deallocation.
52 }
53
54 template<typename U, std::size_t M>
56 {
57 // Each instance of ArrayAllocator can only deallocate objects that were allocated by
58 // itself so it should compare equal only with itself.
59 return this == &rhs;
60 }
61
62 template<typename U, std::size_t M>
64 {
65 return !(*this == rhs);
66 }
67};
68
69} // namespace detail
70
71
72
78template<typename T, std::size_t N>
79using BoundedVector = std::vector<T, detail::ArrayAllocator<T, N>>;
80
81} // namespace se
82
83#endif // SE_COMMON_BOUNDED_VECTOR_HPP
Definition image.hpp:19
An allocator using a static array of N elements of type T.
Definition bounded_vector.hpp:23
T * allocate(std::size_t n)
Definition bounded_vector.hpp:41
std::true_type propagate_on_container_move_assignment
Definition bounded_vector.hpp:31
T value_type
Definition bounded_vector.hpp:29
bool operator==(const ArrayAllocator< U, M > &rhs)
Definition bounded_vector.hpp:55
std::true_type propagate_on_container_swap
Definition bounded_vector.hpp:32
void deallocate(T *, std::size_t)
Definition bounded_vector.hpp:49
bool operator!=(const ArrayAllocator< U, M > &rhs)
Definition bounded_vector.hpp:63
std::true_type propagate_on_container_copy_assignment
Definition bounded_vector.hpp:30
static constexpr std::size_t capacity
Definition bounded_vector.hpp:34
constexpr bool is_power_of_two(const T x)
Helper wrapper to allocate and de-allocate octants in the octree.
Definition bounded_vector.hpp:14
std::vector< T, detail::ArrayAllocator< T, N > > BoundedVector
A statically-allocated std::vector that can store at most N elements.
Definition bounded_vector.hpp:79
Definition bounded_vector.hpp:37