16 #define UPSIZE_POLICY 1.1
18#ifndef DOWNSIZE_POLICY
19 #define DOWNSIZE_POLICY 0.85
21#ifndef MIN_VECTOR_SIZE
22 #define MIN_VECTOR_SIZE 1000
39 mutable T* ptr = NULL;
40 mutable T* unaligned_ptr = NULL;
41 mutable size_t size_ = 0;
44 template<
typename Int=
size_t>
65 return ptr[size_-- - 1];
73 template<
typename Int=
size_t>
74 T& operator[](Int index) {
75 return *(ptr + index);
78 template<
typename Int=
size_t>
79 T operator[](
size_t index)
const {
80 return *(ptr + index);
83 template<
typename Int=
size_t>
84 T& at(
const Int index) {
86 std::cerr <<
"index out of bound in fast vector !\n";
90 return *(ptr + index);
93 template<
typename Int=
size_t>
94 T at(
const Int index)
const {
96 std::cerr <<
"index out of bound in fast vector !\n";
100 return *(ptr + index);
110 template<
typename Int=
size_t>
111 void resize(
const Int n_,
const uint align_byte_length_=std::alignment_of<T>())
const {
117 size_t old_size_ = size_;
120 int offset = std::distance(unaligned_ptr, ptr);
121 unaligned_ptr = (T*)realloc(unaligned_ptr, (size_ + align_byte_length_)*
sizeof(T));
123 if (unaligned_ptr == NULL)
124 throw std::runtime_error(
"bad allocation in fast_vector !!");
126 ptr = unaligned_ptr + offset;
127 if (align_byte_length_ > 1) {
129 size_t allign_offset = ((size_t)ptr)%align_byte_length_;
131 if (allign_offset != 0)
133 if (allign_offset <= offset) {
134 std::rotate<char*>(((
char*)ptr) - allign_offset, (
char*)ptr, ((
char*)ptr) + old_size_*
sizeof(T));
135 ptr -= allign_offset;
138 allign_offset = align_byte_length_ - allign_offset;
139 std::rotate<char*>((
char*)ptr, ((
char*)ptr) + old_size_*
sizeof(T), ((
char*)ptr) + old_size_*
sizeof(T) + allign_offset);
140 ptr += allign_offset;
147 inline T* begin()
const {
152 inline T* end()
const {
153 return begin() + size_;
drop-in replacement for vectors, with more "efficient" memory usage and access.
Definition: vector.hpp:37
void resize(const Int n_, const uint align_byte_length_=std::alignment_of< T >()) const
align_byte_length_ should be used to reallign the buffer, which is not yet implemented as realloc doe...
Definition: vector.hpp:111
QuIDS utility function and variable namespace.
Definition: algorithm.hpp:6
float downsize_policy
size multiplicator when downsize_policy, to avoid repeated upsizing. !!! upsize_policy*downsize_polic...
Definition: vector.hpp:31
float upsize_policy
size multiplicator when upsizing, to avoid repeated upsizing.
Definition: vector.hpp:29
size_t min_vector_size
minimum size a vector is allocated to (to avoid resizing at small sizes).
Definition: vector.hpp:33