/src/cppcheck/lib/smallvector.h
Line | Count | Source |
1 | | /* |
2 | | * Cppcheck - A tool for static C/C++ code analysis |
3 | | * Copyright (C) 2007-2023 Cppcheck team. |
4 | | * |
5 | | * This program is free software: you can redistribute it and/or modify |
6 | | * it under the terms of the GNU General Public License as published by |
7 | | * the Free Software Foundation, either version 3 of the License, or |
8 | | * (at your option) any later version. |
9 | | * |
10 | | * This program is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | * GNU General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU General Public License |
16 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | | */ |
18 | | |
19 | | #ifndef smallvectorH |
20 | | #define smallvectorH |
21 | | |
22 | | #include <cstddef> |
23 | | |
24 | | static constexpr std::size_t DefaultSmallVectorSize = 3; |
25 | | |
26 | | #ifdef HAVE_BOOST |
27 | | #include <boost/container/small_vector.hpp> |
28 | | |
29 | | template<typename T, std::size_t N = DefaultSmallVectorSize> |
30 | | using SmallVector = boost::container::small_vector<T, N>; |
31 | | #else |
32 | | #include <utility> |
33 | | #include <vector> |
34 | | |
35 | | template<class T, std::size_t N> |
36 | | struct TaggedAllocator : std::allocator<T> |
37 | | { |
38 | | template<class ... Ts> |
39 | | // cppcheck-suppress noExplicitConstructor |
40 | | // NOLINTNEXTLINE(google-explicit-constructor) |
41 | | TaggedAllocator(Ts&&... ts) |
42 | | : std::allocator<T>(std::forward<Ts>(ts)...) |
43 | 317k | {} TaggedAllocator<Token const*, 9ul>::TaggedAllocator<>() Line | Count | Source | 43 | 145k | {} |
TaggedAllocator<ReferenceToken, 3ul>::TaggedAllocator<>() Line | Count | Source | 43 | 171k | {} |
TaggedAllocator<Token*, 9ul>::TaggedAllocator<>() Line | Count | Source | 43 | 573 | {} |
|
44 | | |
45 | | template<class U> |
46 | | // cppcheck-suppress noExplicitConstructor |
47 | | // NOLINTNEXTLINE(google-explicit-constructor) |
48 | | TaggedAllocator(const TaggedAllocator<U, N> /*unused*/) {} |
49 | | |
50 | | template<class U> |
51 | | struct rebind |
52 | | { |
53 | | using other = TaggedAllocator<U, N>; |
54 | | }; |
55 | | }; |
56 | | |
57 | | template<typename T, std::size_t N = DefaultSmallVectorSize> |
58 | | class SmallVector : public std::vector<T, TaggedAllocator<T, N>> |
59 | | { |
60 | | public: |
61 | | template<class ... Ts> |
62 | | // NOLINTNEXTLINE(google-explicit-constructor) |
63 | | SmallVector(Ts&&... ts) |
64 | | : std::vector<T, TaggedAllocator<T, N>>(std::forward<Ts>(ts)...) |
65 | 317k | { |
66 | 317k | this->reserve(N); |
67 | 317k | } SmallVector<Token const*, 9ul>::SmallVector<>() Line | Count | Source | 65 | 145k | { | 66 | 145k | this->reserve(N); | 67 | 145k | } |
SmallVector<ReferenceToken, 3ul>::SmallVector<>() Line | Count | Source | 65 | 171k | { | 66 | 171k | this->reserve(N); | 67 | 171k | } |
SmallVector<Token*, 9ul>::SmallVector<>() Line | Count | Source | 65 | 573 | { | 66 | 573 | this->reserve(N); | 67 | 573 | } |
|
68 | | }; |
69 | | #endif |
70 | | |
71 | | #endif |