Flow 3.0.0
Flow project: Full implementation reference.
traits.hpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
22#include <boost/unordered/unordered_set.hpp>
23
24namespace flow::util
25{
26
27/* Normally Container_traits would have all been forward-declared in a _fwd.hpp and defined here; instead it is
28 * defined here, and that's it. This is in accordance with the list of known reasonable exceptions to
29 * the _fwd.hpp pattern; namely since Container_traits is a bunch of sizeof=0 types storing constants only. */
30
31/**
32 * Properties of various container types. For example,
33 * `"Container_traits<std::vector>::S_CHANGE_INVALIDATES_ITERATORS == true"`, while
34 * `"Container_traits<std::list>::S_CHANGE_INVALIDATES_ITERATORS == false"`.
35 * The template defines the interface, then the specializations for various container types define
36 * the implementations.
37 *
38 * @todo Add things like `unordered_flat_map/set`, `std::` counterparts, `boost::container` things, and more.
39 * Basically make this more complete, given that since this little facility was initially added, quite some
40 * containers have materialized and entered common use.
41 *
42 * @tparam Container
43 * The container type being described.
44 */
45template<typename Container>
47{
48public:
49 // Constants.
50
51 /**
52 * If false, a change (erasure, addition of an element) in the `Container` will invalidate NO
53 * iterator to that `Container`, previously obtained, except for potentially any iterator to the
54 * element being erased (if applicable).
55 */
56 static constexpr bool S_CHANGE_INVALIDATES_ITERATORS = false;
57 /// `true` if and only if iterating over the elements of a `Container` yields them in sorted order.
58 static constexpr bool S_SORTED = false;
59 /// `true` if and only if finding an element of `Container` by key takes at most constant amortized time.
60 static constexpr bool S_CONSTANT_TIME_SEARCH = false;
61 /// `true` if and only if adding an element to a `Container` (anywhere) takes at most constant amortized time.
62 static constexpr bool S_CONSTANT_TIME_INSERT = false;
63private:
64 // Constructors/destructor.
65
66 /// Forbid all instantion.
67 Container_traits() = delete;
68}; // class Container_traits<>
69
70/**
71 * Traits of `std::set`.
72 *
73 * @tparam T
74 * Type of set element.
75 */
76template<typename T>
77class Container_traits<std::set<T>>
78{
79public:
80 // Constants.
81
82 /// See Container_traits.
83 static constexpr bool S_CHANGE_INVALIDATES_ITERATORS = false;
84 /// See Container_traits.
85 static constexpr bool S_SORTED = true;
86 /// See Container_traits.
87 static constexpr bool S_CONSTANT_TIME_SEARCH = false;
88 /// See Container_traits.
89 static constexpr bool S_CONSTANT_TIME_INSERT = false;
90private:
91 // Constructors/destructor.
92
93 /// Forbid all instantion.
94 Container_traits() = delete;
95}; // class Container_traits<std::set>
96
97/**
98 * Traits of `std::map`.
99 *
100 * @tparam K
101 * Type of map key element.
102 * @tparam V
103 * Type of map value element.
104 */
105template<typename K, typename V>
106class Container_traits<std::map<K, V>>
107{
108public:
109 // Constants.
110
111 /// See Container_traits.
112 static constexpr bool S_CHANGE_INVALIDATES_ITERATORS = false;
113 /// See Container_traits.
114 static constexpr bool S_SORTED = true;
115 /// See Container_traits.
116 static constexpr bool S_CONSTANT_TIME_SEARCH = false;
117 /// See Container_traits.
118 static constexpr bool S_CONSTANT_TIME_INSERT = false;
119private:
120 // Constructors/destructor.
121
122 /// Forbid all instantion.
124}; // class Container_traits<std::map>
125
126/**
127 * Traits of `boost::unordered_set`.
128 *
129 * @tparam T
130 * Type of set element.
131 */
132template<typename T>
133class Container_traits<boost::unordered_set<T>>
134{
135public:
136 // Constants.
137
138 /// See Container_traits.
139 static constexpr bool S_CHANGE_INVALIDATES_ITERATORS = true;
140 /// See Container_traits.
141 static constexpr bool S_SORTED = false;
142 /// See Container_traits.
143 static constexpr bool S_CONSTANT_TIME_SEARCH = true;
144 /// See Container_traits.
145 static constexpr bool S_CONSTANT_TIME_INSERT = true;
146private:
147 // Constructors/destructor.
148
149 /// Forbid all instantion.
151}; // class Container_traits<boost::unordered_set>
152
153/**
154 * Traits of flow::util::Linked_hash_map.
155 *
156 * @tparam K
157 * Type of map key element.
158 * @tparam V
159 * Type of map value element.
160 */
161template<typename K, typename V>
163{
164public:
165 // Constants.
166
167 /// See Container_traits.
168 static constexpr bool S_CHANGE_INVALIDATES_ITERATORS = false;
169 /// See Container_traits.
170 static constexpr bool S_SORTED = false;
171 /// See Container_traits.
172 static constexpr bool S_CONSTANT_TIME_SEARCH = true;
173 /// See Container_traits.
174 static constexpr bool S_CONSTANT_TIME_INSERT = true;
175private:
176 // Constructors/destructor.
177
178 /// Forbid all instantion.
180}; // class Container_traits<util::Linked_hash_map>
181
182/**
183 * Traits of flow::util::Linked_hash_set.
184 *
185 * @tparam T
186 * Type of set element.
187 * @tparam Hash
188 * Hasher.
189 * @tparam Pred
190 * Equality predicate.
191 */
192template<typename T, typename Hash, typename Pred>
193class Container_traits<util::Linked_hash_set<T, Hash, Pred>>
194{
195public:
196 // Constants.
197
198 /// See Container_traits.
199 static constexpr bool S_CHANGE_INVALIDATES_ITERATORS = false;
200 /// See Container_traits.
201 static constexpr bool S_SORTED = false;
202 /// See Container_traits.
203 static constexpr bool S_CONSTANT_TIME_SEARCH = true;
204 /// See Container_traits.
205 static constexpr bool S_CONSTANT_TIME_INSERT = true;
206private:
207 // Constructors/destructor.
208
209 /// Forbid all instantion.
211}; // class Container_traits<util::Linked_hash_set>
212
213} // namespace flow::util
Container_traits()=delete
Forbid all instantion.
Container_traits()=delete
Forbid all instantion.
Properties of various container types.
Definition: traits.hpp:47
static constexpr bool S_CHANGE_INVALIDATES_ITERATORS
If false, a change (erasure, addition of an element) in the Container will invalidate NO iterator to ...
Definition: traits.hpp:56
static constexpr bool S_SORTED
true if and only if iterating over the elements of a Container yields them in sorted order.
Definition: traits.hpp:58
Container_traits()=delete
Forbid all instantion.
static constexpr bool S_CONSTANT_TIME_SEARCH
true if and only if finding an element of Container by key takes at most constant amortized time.
Definition: traits.hpp:60
static constexpr bool S_CONSTANT_TIME_INSERT
true if and only if adding an element to a Container (anywhere) takes at most constant amortized time...
Definition: traits.hpp:62
An object of this class is a map that combines the lookup speed of an unordered_flat_map<> and orderi...
An object of this class is a set that combines the lookup speed of an unordered_flat_set<> and orderi...
Flow module containing miscellaneous general-use facilities that don't fit into any other Flow module...