OnixS C++ CME MDP Premium Market Data Handler 5.11.1
Users' manual and API documentation
Loading...
Searching...
No Matches
TinySet.h
Go to the documentation of this file.
1// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2//
3// This software owned by Onix Solutions Limited [OnixS] and is
4// protected by copyright law and international copyright treaties.
5//
6// Access to and use of the software is governed by the terms of the applicable
7// OnixS Software Services Agreement (the Agreement) and Customer end user license
8// agreements granting a non-assignable, non-transferable and non-exclusive license
9// to use the software for it's own data processing purposes under the terms defined
10// in the Agreement.
11//
12// Except as otherwise granted within the terms of the Agreement, copying or
13// reproduction of any part of this source code or associated reference material
14// to any other location for further reproduction or redistribution, and any
15// amendments to this copyright notice, are expressly prohibited.
16//
17// Any reproduction or redistribution for sale or hiring of the Software not in
18// accordance with the terms of the Agreement is a violation of copyright law.
19//
20
21#pragma once
22
23#include <cstddef>
24#include <string>
25#include <vector>
26#include <iterator>
27#include <algorithm>
28#include <type_traits>
29#include <utility>
30
33
35
44template <class Key>
46{
47 class IsTinySetKeyNothrowEqualityComparable
48 {
49 template <class Candidate>
50 static auto test(int) -> std::integral_constant
51 <bool, noexcept(static_cast<bool>(std::declval<const Candidate&>() == std::declval<const Candidate&>()))>;
52
53 template <class> static std::false_type test(...);
54
55 public:
56 static constexpr bool value = decltype(test<Key>(0))::value;
57 };
58
59 static_assert(std::is_copy_constructible<Key>::value, "Key must be copy-constructible");
60 static_assert(std::is_nothrow_move_assignable<Key>::value, "Key must be nothrow move-assignable");
61 static_assert(std::is_nothrow_destructible<Key>::value, "Key must be nothrow destructible");
62 static_assert(IsTinySetKeyNothrowEqualityComparable::value, "Key must be nothrow equality-comparable");
63
64public:
66 using Item = Key;
67
69 using Items = std::vector<Item>;
70
72 using ConstIterator = typename Items::const_iterator;
73
75 TinySet() = default;
76
82 explicit TinySet(size_t capacity)
83 {
84 reserve(capacity);
85 }
86
92 TinySet(const TinySet& other) = default;
93
98 TinySet(TinySet&& other) = default;
99
107 TinySet& operator=(const TinySet& other)
108 {
109 if ONIXS_CMEMDH_LIKELY(&other != this)
110 {
111 TinySet copy(other);
112
113 swap(copy);
114 }
115
116 return *this;
117 }
118
124 TinySet& operator=(TinySet&& other) noexcept
125 {
126 if ONIXS_CMEMDH_LIKELY(&other != this)
127 items_ = std::move(other.items_);
128
129 return *this;
130 }
131
133 ~TinySet() = default;
134
139 bool empty() const noexcept
140 {
141 return items_.empty();
142 }
143
148 size_t size() const noexcept
149 {
150 return items_.size();
151 }
152
160 void reserve(size_t capacity)
161 {
162 items_.reserve(capacity);
163 }
164
169 ConstIterator begin() const noexcept
170 {
171 return items_.begin();
172 }
173
178 ConstIterator end() const noexcept
179 {
180 return items_.end();
181 }
182
188 ConstIterator find(const Item& item) const noexcept
189 {
190 return std::find(items_.begin(), items_.end(), item);
191 }
192
201 bool insert(const Item& item)
202 {
203 if (end() == find(item))
204 {
205 items_.push_back(item);
206
207 return true;
208 }
209
210 return false;
211 }
212
218 bool erase(const Item& item) noexcept
219 {
220 const auto found = find(item);
221
222 if (items_.end() == found)
223 return false;
224
225 items_.erase(found);
226
227 return true;
228 }
229
233 void clear() noexcept
234 {
235 items_.clear();
236 }
237
243 void swap(TinySet& other) noexcept
244 {
245 items_.swap(other.items_);
246 }
247
248private:
249 Items items_;
250};
251
257template <class Key>
258inline void swap(TinySet<Key>& left, TinySet<Key>& right) noexcept
259{
260 left.swap(right);
261}
262
269{
270public:
272 using Items = std::vector<std::string>;
273
275 using ItemIterator = Items::const_iterator;
276
278 using iterator_category = std::input_iterator_tag;
279
282
284 using difference_type = typename std::iterator_traits<ItemIterator>::difference_type;
285
288 {
289 public:
293 explicit Pointer(value_type value) noexcept
294 : value_(value)
295 {
296 }
297
302 const value_type* operator->() const noexcept
303 {
304 return &value_;
305 }
306
307 private:
308 value_type value_;
309 };
310
313
316
321
325 TinyStrSetIterator(const TinyStrSetIterator& other) = default;
326
332
338
345
349 explicit TinyStrSetIterator(const ItemIterator& iterator)
350 : iterator_(iterator)
351 {
352 }
353
356
363 {
364 return Messaging::toStrRef(*iterator_);
365 }
366
373 {
374 return pointer(operator*());
375 }
376
383 bool operator==(const TinyStrSetIterator& other) const
384 {
385 return iterator_ == other.iterator_;
386 }
387
394 bool operator!=(const TinyStrSetIterator& other) const
395 {
396 return iterator_ != other.iterator_;
397 }
398
404 {
405 ++iterator_;
406
407 return *this;
408 }
409
415 {
416 TinyStrSetIterator previous(*this);
417
418 ++(*this);
419
420 return previous;
421 }
422
423private:
424 ItemIterator iterator_;
425};
426
436{
437public:
440
443
445 TinyStrSet() = default;
446
452 explicit TinyStrSet(size_t capacity)
453 {
454 reserve(capacity);
455 }
456
461 TinyStrSet(const TinyStrSet& other) = default;
462
467 TinyStrSet(TinyStrSet&& other) = default;
468
476 {
477 if ONIXS_CMEMDH_LIKELY(&other != this)
478 {
479 TinyStrSet copy(other);
480
481 swap(copy);
482 }
483
484 return *this;
485 }
486
492 TinyStrSet& operator=(TinyStrSet&& other) noexcept
493 {
494 if ONIXS_CMEMDH_LIKELY(&other != this)
495 items_ = std::move(other.items_);
496
497 return *this;
498 }
499
501 ~TinyStrSet() = default;
502
507 bool empty() const noexcept
508 {
509 return items_.empty();
510 }
511
516 size_t size() const noexcept
517 {
518 return items_.size();
519 }
520
527 void reserve(size_t capacity)
528 {
529 items_.reserve(capacity);
530 }
531
537 {
538 return ConstIterator(items_.begin());
539 }
540
546 {
547 return ConstIterator(items_.end());
548 }
549
556 {
557 return ConstIterator(std::find_if(items_.begin(), items_.end(), ItemLocator(item)));
558 }
559
568 bool insert(Item item)
569 {
570 if (end() == find(item))
571 {
572 items_.push_back(Messaging::toStr(item));
573
574 return true;
575 }
576
577 return false;
578 }
579
585 bool erase(Item item) noexcept
586 {
587 const auto found = std::find_if(items_.begin(), items_.end(), ItemLocator(item));
588
589 if (items_.end() == found)
590 return false;
591
592 items_.erase(found);
593
594 return true;
595 }
596
600 void clear() noexcept
601 {
602 items_.clear();
603 }
604
610 void swap(TinyStrSet& other) noexcept
611 {
612 items_.swap(other.items_);
613 }
614
615private:
616 using Items = std::vector<std::string>;
617
618 class ItemLocator
619 {
620 const Messaging::StrRef sought_;
621
622 public:
623 explicit ItemLocator(Messaging::StrRef sought) noexcept
624 : sought_(sought)
625 {
626 }
627
628 bool operator()(const std::string& other) const noexcept
629 {
630 return sought_ == other;
631 }
632 };
633
634 Items items_;
635};
636
641inline void swap(TinyStrSet& left, TinyStrSet& right) noexcept
642{
643 left.swap(right);
644}
645
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:69
#define ONIXS_CMEMDH_LTWT
Definition Bootstrap.h:61
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:73
#define ONIXS_CMEMDH_NODISCARD
Definition Compiler.h:165
Stores a small number of unique keys in contiguous storage.
Definition TinySet.h:46
void reserve(size_t capacity)
Definition TinySet.h:160
void clear() noexcept
Removes all keys without reducing reserved capacity.
Definition TinySet.h:233
bool erase(const Item &item) noexcept
Removes the key equal to the requested value.
Definition TinySet.h:218
TinySet(TinySet &&other)=default
Constructs a set by transferring another set's storage.
TinySet(const TinySet &other)=default
Constructs a set containing copies of another set's keys.
TinySet(size_t capacity)
Constructs an empty set with storage reserved for at least the requested capacity.
Definition TinySet.h:82
typename Items::const_iterator ConstIterator
Definition TinySet.h:72
size_t size() const noexcept
Returns the number of stored keys.
Definition TinySet.h:148
bool empty() const noexcept
Checks whether the set contains no keys.
Definition TinySet.h:139
ConstIterator find(const Item &item) const noexcept
Finds a key equal to the requested value.
Definition TinySet.h:188
ConstIterator begin() const noexcept
Returns an iterator to the first stored key.
Definition TinySet.h:169
~TinySet()=default
Releases the owned key storage.
TinySet()=default
Constructs an empty set.
bool insert(const Item &item)
Inserts a copy of a key unless an equal key is already present.
Definition TinySet.h:201
TinySet & operator=(TinySet &&other) noexcept
Replaces the stored keys by transferring another set's storage.
Definition TinySet.h:124
ConstIterator end() const noexcept
Returns the past-the-end iterator.
Definition TinySet.h:178
TinySet & operator=(const TinySet &other)
Replaces the stored keys with copies of another set's keys.
Definition TinySet.h:107
void swap(TinySet &other) noexcept
Definition TinySet.h:243
Proxy providing pointer-like access to a dereferenced string view.
Definition TinySet.h:288
const value_type * operator->() const noexcept
Returns a pointer to the proxied string view.
Definition TinySet.h:302
Pointer(value_type value) noexcept
Constructs a proxy for a string view.
Definition TinySet.h:293
Provides read-only iteration over strings owned by TinyStrSet.
Definition TinySet.h:269
TinyStrSetIterator operator++(int)
Advances to the next string while retaining the previous position.
Definition TinySet.h:414
bool operator!=(const TinyStrSetIterator &other) const
Checks whether two iterators refer to different positions.
Definition TinySet.h:394
std::vector< std::string > Items
String storage type traversed by the iterator.
Definition TinySet.h:272
TinyStrSetIterator(const TinyStrSetIterator &other)=default
Constructs an iterator referring to the same position as another iterator.
TinyStrSetIterator & operator++()
Advances to the next string.
Definition TinySet.h:403
TinyStrSetIterator & operator=(const TinyStrSetIterator &other)=default
Makes this iterator refer to the same position as another iterator.
Messaging::StrRef value_type
Value produced when the iterator is dereferenced.
Definition TinySet.h:281
Messaging::StrRef operator*() const
Returns a non-owning view of the current string.
Definition TinySet.h:362
Items::const_iterator ItemIterator
Underlying iterator type.
Definition TinySet.h:275
~TinyStrSetIterator()=default
Destroys the iterator without affecting the underlying string.
TinyStrSetIterator()=default
Constructs a singular iterator.
bool operator==(const TinyStrSetIterator &other) const
Checks whether two iterators refer to the same position.
Definition TinySet.h:383
TinyStrSetIterator(const ItemIterator &iterator)
Wraps an iterator over TinyStrSet string storage.
Definition TinySet.h:349
std::input_iterator_tag iterator_category
Iterator category used by standard algorithms.
Definition TinySet.h:278
TinyStrSetIterator(TinyStrSetIterator &&other)=default
Constructs an iterator by moving another iterator.
TinyStrSetIterator & operator=(TinyStrSetIterator &&other)=default
Replaces this iterator's state by moving another iterator.
value_type reference
Type returned when the iterator is dereferenced.
Definition TinySet.h:315
typename std::iterator_traits< ItemIterator >::difference_type difference_type
Signed type used to represent distances between iterators.
Definition TinySet.h:284
Pointer pointer
Pointer-like proxy type returned by operator->().
Definition TinySet.h:312
pointer operator->() const
Provides pointer-like access to the current string view.
Definition TinySet.h:372
Stores unique strings supplied through non-owning Messaging::StrRef views.
Definition TinySet.h:436
void reserve(size_t capacity)
Reserves storage for at least the requested number of strings.
Definition TinySet.h:527
void clear() noexcept
Removes all strings without reducing reserved capacity.
Definition TinySet.h:600
Messaging::StrRef Item
Non-owning string view accepted and returned by the set.
Definition TinySet.h:439
size_t size() const noexcept
Returns the number of stored strings.
Definition TinySet.h:516
bool empty() const noexcept
Checks whether the set contains no strings.
Definition TinySet.h:507
TinyStrSet(size_t capacity)
Constructs an empty string set with storage reserved for at least the requested capacity.
Definition TinySet.h:452
TinyStrSetIterator ConstIterator
Read-only iterator over owned strings.
Definition TinySet.h:442
TinyStrSet(const TinyStrSet &other)=default
Constructs a deep copy of another string set.
ConstIterator end() const
Returns the past-the-end iterator.
Definition TinySet.h:545
TinyStrSet & operator=(TinyStrSet &&other) noexcept
Replaces the content by transferring another set's storage.
Definition TinySet.h:492
TinyStrSet()=default
Constructs an empty string set.
TinyStrSet & operator=(const TinyStrSet &other)
Replaces the content with a deep copy of another string set.
Definition TinySet.h:475
void swap(TinyStrSet &other) noexcept
Exchanges owned strings and capacity with another set.
Definition TinySet.h:610
~TinyStrSet()=default
Releases all owned string storage.
bool erase(Item item) noexcept
Removes the string whose characters equal the supplied view.
Definition TinySet.h:585
bool insert(Item item)
Copies a string into the set unless equal content is already present.
Definition TinySet.h:568
ConstIterator find(Item item) const
Finds a string whose characters equal the supplied view.
Definition TinySet.h:555
ConstIterator begin() const
Returns an iterator to the first owned string.
Definition TinySet.h:536
TinyStrSet(TinyStrSet &&other)=default
Constructs a string set by transferring another set's storage.
StrRef toStrRef(const std::string &str) noexcept
Constructs a StrRef instance over th std::string content.
Definition StrRef.h:367
std::basic_string_view< Char > StrRef
Definition StrRef.h:46
void toStr(std::string &, Int8)
Serializes given integer into a string.
void swap(TinySet< Key > &left, TinySet< Key > &right) noexcept
Exchanges the content and capacity of two sets.
Definition TinySet.h:258
MboBook & copy(MboBook &target, const MboBook &source)
Copies content of MBO book to the other one.
Definition MboBook.h:210