11 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_TINY_SETS_H
12 #define INCLUDE_MOLASSEMBLER_TEMPLE_TINY_SETS_H
18 namespace Molassembler {
45 using iterator =
typename std::vector<T>::iterator;
61 std::is_same<T,
typename std::decay<T>::type>::value,
62 "Tiny sets can only contain non-cv qualified types"
68 :
data(std::forward<std::initializer_list<T>>(list))
71 std::is_same<T,
typename std::decay<T>::type>::value,
72 "Tiny sets can only contain non-cv qualified types"
88 if(!contains(set, a)) {
89 set.push_back(std::move(a));
106 template<
typename ... Args>
108 set.emplace_back(std::forward<Args>(args)...);
115 ) != std::end(set) - 1
150 data.push_back(std::move(a));
158 template<
typename ... Args>
160 data.emplace_back(std::forward<Args>(args)...);
164 template<
typename It>
166 auto backIterator = --
end();
168 if(a == backIterator) {
171 std::iter_swap(a, backIterator);
172 data.erase(backIterator);
177 template<
typename It>
219 return std::begin(
data);
224 return std::end(
data);
229 return std::begin(
data);
234 return std::end(
data);
239 return std::cbegin(
data);
244 return std::cend(
data);
273 using iterator =
typename std::vector<T>::iterator;
289 std::is_same<T,
typename std::decay<T>::type>::value,
290 "Tiny sets can only contain non-cv qualified types"
295 :
data(std::forward<std::initializer_list<T>>(list))
298 std::is_same<T,
typename std::decay<T>::type>::value,
299 "Tiny sets can only contain non-cv qualified types"
307 return std::lower_bound(
315 return std::lower_bound(
328 if(findIter != std::end(set) && !(a < *findIter)) {
332 set.insert(std::move(findIter), std::move(a));
350 template<
typename ... Args>
353 set.emplace_back(std::forward<Args>(args)...);
355 iterator lastElementIter = std::end(set) - 1;
358 iterator findIter = std::lower_bound(
364 if(findIter != lastElementIter && !(set.back() < *findIter)) {
386 return std::binary_search(
402 template<
typename It>
414 return std::lower_bound(
436 template<
typename It>
461 for(
const auto& value : init) {
473 return std::binary_search(
505 return data.at(position);
513 return std::begin(
data);
518 return std::end(
data);
523 return std::begin(
data);
528 return std::end(
data);
533 return std::cbegin(
data);
538 return std::cend(
data);
551 return !(*
this == other);
UnderlyingContainer data
The owned underlying linear memory container.
Definition: TinySet.h:281
TinySet()
Default constructor.
Definition: TinySet.h:287
typename std::vector< T >::iterator iterator
Iterator type.
Definition: TinySet.h:45
TinyUnorderedSet(std::initializer_list< T > list)
Initializer-list constructor.
Definition: TinySet.h:67
static iterator find(std::vector< T > &set, const T &a)
Emplaces an element in an ordered vector if the element is not contained.
Definition: TinySet.h:306
void insert(It a, const It &b)
Inserts all elements contained in a range.
Definition: TinySet.h:437
const_iterator cbegin() const
Yields a begin const iterator.
Definition: TinySet.h:238
const_reference front() const
Yields a const reference to the first element in the set.
Definition: TinySet.h:494
iterator end()
Yields an end iterator.
Definition: TinySet.h:517
static void checked_emplace(std::vector< T > &set, Args &&...args)
Emplaces an element in an ordered vector if the element is not contained.
Definition: TinySet.h:351
void erase(It a)
Erases a position in the set.
Definition: TinySet.h:165
T value_type
What types this container stores.
Definition: TinySet.h:39
bool operator==(const TinySet &other) const
Lexicographically compare two sets.
Definition: TinySet.h:545
void erase(It a)
Erases an element marked by a position in the set.
Definition: TinySet.h:403
static void checked_insert(std::vector< T > &set, T a)
Emplaces an element in an ordered vector if the element is not contained.
Definition: TinySet.h:322
void insert(T a)
Inserts an element into the set.
Definition: TinySet.h:149
std::size_t size() const
Returns the number of contained elements.
Definition: TinySet.h:486
TinyUnorderedSet()
Default constructor.
Definition: TinySet.h:59
T & reference
Reference to a value_type.
Definition: TinySet.h:269
const T & const_reference
Const reference to a value type.
Definition: TinySet.h:271
std::size_t size() const
Returns the number of elements in the set.
Definition: TinySet.h:205
void clear()
Empties the set.
Definition: TinySet.h:141
const_iterator end() const
Yields an end const iterator.
Definition: TinySet.h:527
typename std::vector< T >::const_iterator const_iterator
Const iterator type.
Definition: TinySet.h:275
TinySet(std::initializer_list< T > list)
Initializer-list constructor.
Definition: TinySet.h:294
An adapter class for std::vector that acts like an ordered set and stores its data in the underlying ...
Definition: TinySet.h:261
typename std::vector< T >::iterator iterator
Iterator type.
Definition: TinySet.h:273
An adapter class for std::vector that acts like an unordered set and stores its data in the underlyin...
Definition: TinySet.h:33
void emplace(T a)
Insert an element.
Definition: TinySet.h:451
static bool binary_search(const std::vector< T > &set, const T &value)
Binary search for a value in an ordered set.
Definition: TinySet.h:385
const_iterator begin() const
Yields a begin const iterator.
Definition: TinySet.h:228
const T & const_reference
Const reference to a value type.
Definition: TinySet.h:43
void clear()
Empties the set.
Definition: TinySet.h:397
unsigned count(T a) const
Count the number of occurrences of an element.
Definition: TinySet.h:472
iterator begin()
Yields a begin iterator.
Definition: TinySet.h:218
const_reference back() const
Yields a const reference to the last element in the set.
Definition: TinySet.h:499
iterator end()
Yields an end iterator.
Definition: TinySet.h:223
void reserve(std::size_t size)
Reserves space in memory.
Definition: TinySet.h:408
void emplace(Args &&...args)
Emplaces an element into the set.
Definition: TinySet.h:159
void insert(It a, const It &b)
Inserts all elements contained in a range.
Definition: TinySet.h:178
bool operator!=(const TinySet &other) const
Invertes operator==.
Definition: TinySet.h:550
static void checked_emplace(std::vector< T > &set, Args &&...args)
Checked emplace of an element into a set.
Definition: TinySet.h:107
static bool linear_search(std::vector< T > &set, const T &value)
Linear search for a value.
Definition: TinySet.h:129
std::vector< T > UnderlyingContainer
The type of the underlying container with linear memory.
Definition: TinySet.h:265
bool empty() const
Returns whether the set is empty.
Definition: TinySet.h:210
T & reference
Reference to a value_type.
Definition: TinySet.h:41
iterator find(const T &a)
Finds an element in the set.
Definition: TinySet.h:413
std::vector< T > UnderlyingContainer
The type of the underlying container with linear memory.
Definition: TinySet.h:37
const_reference at(std::size_t position) const
Yields a const reference to an element in the set.
Definition: TinySet.h:504
typename std::vector< T >::const_iterator const_iterator
Const iterator type.
Definition: TinySet.h:47
const_iterator begin() const
Yields a begin const iterator.
Definition: TinySet.h:522
iterator begin()
Yields a begin iterator.
Definition: TinySet.h:512
bool empty() const
Returns whether the set is empty.
Definition: TinySet.h:481
unsigned count(T a) const
Counts the number of occurrences of an element.
Definition: TinySet.h:196
void reserve(std::size_t size)
Reserves space in memory.
Definition: TinySet.h:188
const_iterator cend() const
Yields an end const iterator.
Definition: TinySet.h:537
const_iterator end() const
Yields an end const iterator.
Definition: TinySet.h:233
UnderlyingContainer data
The owned underlying linear memory container.
Definition: TinySet.h:53
void insert(T a)
Inserts an element into the set.
Definition: TinySet.h:425
static const_iterator find(const std::vector< T > &set, const T &a)
Emplaces an element in an ordered vector if the element is not contained.
Definition: TinySet.h:314
const_iterator cend() const
Yields an end const iterator.
Definition: TinySet.h:243
const_iterator cbegin() const
Yields a begin const iterator.
Definition: TinySet.h:532
TinySet & operator=(std::initializer_list< T > init)
Assign from an initializer list.
Definition: TinySet.h:459
auto find(const Container &container, const T &needle)
std::find shorthand
Definition: Functional.h:346
T value_type
What types this container stores.
Definition: TinySet.h:267
static bool checked_insert(std::vector< T > &set, T a)
Inserts an element only if the element is not already in the set.
Definition: TinySet.h:87