NEST main@caf0ae8
 
Loading...
Searching...
No Matches
vector_util.h
Go to the documentation of this file.
1/*
2 * vector_util.h
3 *
4 * This file is part of NEST.
5 *
6 * Copyright (C) 2004 The NEST Initiative
7 *
8 * NEST is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NEST is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef VECTOR_UTIL_H
24#define VECTOR_UTIL_H
25
26#include <cstddef>
27#include <vector>
28
29namespace vector_util
30{
31
32template < typename T >
33inline void
34grow( std::vector< T >& v )
35{
36 // set maximal growth of vector to 256MiB; this allows for fast
37 // growth while the vector is small, but limits capacity wasted
38 // while growing large vectors; value determined by experimenting
39 // with different max block sizes
40 const size_t max_block_size_MiB = 256;
41 const size_t max_block_size =
42 static_cast< size_t >( max_block_size_MiB * ( 2 << 20 ) / static_cast< double >( sizeof( T ) ) );
43
44 if ( v.size() == v.capacity() )
45 {
46 v.reserve( v.size() < max_block_size ? 2 * v.size() : ( v.size() + max_block_size ) );
47 }
48}
49
50} // namespace vector_util
51
52#endif // VECTOR_UTIL_H
constexpr int max_block_size
Definition block_vector.h:40
Definition vector_util.h:30
void grow(std::vector< T > &v)
Definition vector_util.h:34