OnixS C++ CME MDP Conflated UDP Handler  1.1.2
API documentation
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 <vector>
24 #include <iterator>
25 #include <algorithm>
26 
28 
30 
31 /// Class implementing set optimized
32 /// for storing small number of items.
33 template
34 <
35  class Key
36 >
37 class TinySet
38 {
39 public:
40  /// Aliases set item type.
41  typedef Key Item;
42 
43  /// Aliases internal representation.
44  typedef
45  std::vector<Item>
47 
48  /// Aliases iterator over set items.
49  typedef
50  typename
51  Items::const_iterator
53 
54  /// Initializes the empty set.
56  {
57  }
58 
59  /// Initializes the empty set and configures
60  /// the internal storage to be enough capacious
61  /// to store the given number of items.
62  explicit
64  size_t capacity)
65  {
66  reserve(capacity);
67  }
68 
69  /// Initializes the set as a copy of the other one.
71  const TinySet& other)
72  : items_(other.items_)
73  {
74  }
75 
76  /// Cleans everything up.
78  {
79  }
80 
81  /// Indicates whether the set is empty.
82  bool empty() const
83  {
84  return items_.empty();
85  }
86 
87  /// Returns the number of items in the set.
88  size_t size() const
89  {
90  return items_.size();
91  }
92 
93  /// Makes the internal storage capacious
94  /// enough to store the given number of items.
95  void
97  size_t capacity)
98  {
99  items_.reserve(capacity);
100  }
101 
102  /// Provides iterating facilities.
104  begin() const
105  {
106  return items_.begin();
107  }
108 
109  /// Provides iterating facilities.
111  end() const
112  {
113  return items_.end();
114  }
115 
116  /// Tells whether the set contains given item.
119  const Item& item) const
120  {
121  return
122  std::find(
123  items_.begin(),
124  items_.end(),
125  item);
126  }
127 
128  /// Inserts the given item into the set.
129  ///
130  /// Returned value indicates whether the
131  /// item was inserted (true) or the set
132  /// already contained the given item (false).
133  bool
135  const Item& item)
136  {
137  if (end() == find(item))
138  {
139  items_.push_back(item);
140 
141  return true;
142  }
143 
144  return false;
145  }
146 
147  /// Removes the given item from the set.
148  ///
149  /// Returned value indicates whether
150  /// the item was actually removed.
151  bool
153  const Item& item)
154  {
155  typename
156  Items::iterator
157  const
158  found =
159  std::find(
160  items_.begin(),
161  items_.end(),
162  item);
163 
164  if (items_.end() == found)
165  return false;
166 
167  items_.erase(found);
168 
169  return true;
170  }
171 
172  /// Brings the set to the blank state.
173  void
175  {
176  items_.clear();
177  }
178 
179  /// Swaps content with the other instance.
180  void
182  TinySet& other)
183  {
184  std::swap(
185  items_,
186  other.items_);
187  }
188 
189  /// Re-initializes the instance
190  /// as a copy of the other one.
191  TinySet&
192  operator =(
193  const TinySet& other)
194  {
195  if (&other != this)
196  {
197  TinySet copy(other);
198 
199  swap(copy);
200  }
201 
202  return *this;
203  }
204 
205 private:
206  Items items_;
207 };
208 
209 /// Implements iterator for the TinyStrSet class.
212 {
213 public:
214  /// Aliases for internal representation.
215  typedef
216  std::vector<std::string>
218 
219  /// Aliases iterator over the internal store.
220  typedef
221  Items::const_iterator
223 
224  /// Initializes the iterator pointing to nowhere.
226  {
227  }
228 
229  /// Initializes the instance as a copy of the other one.
231  const TinyStrSetIterator& other)
232  : iterator_(other.iterator_)
233  {
234  }
235 
236  /// Initializes from iterator over the internal storage.
237  explicit
239  const ItemIterator& iterator)
240  : iterator_(iterator)
241  {
242  }
243 
244  /// Cleans everything up.
246  {
247  }
248 
249  /// Provides access to the underlying object.
250  StrRef
251  operator *() const
252  {
253  return toStrRef(*iterator_);
254  }
255 
256  /// Compares with the other instance.
257  bool
259  const
260  TinyStrSetIterator& other) const
261  {
262  return iterator_ == other.iterator_;
263  }
264 
265  /// Compares with the other instance.
266  bool
268  const
269  TinyStrSetIterator& other) const
270  {
271  return iterator_ != other.iterator_;
272  }
273 
274  /// Re-initializes as a copy of the other instance.
276  operator =(
277  const
278  TinyStrSetIterator& other)
279  {
280  iterator_ = other.iterator_;
281 
282  return *this;
283  }
284 
285  /// Advances the instance to the
286  /// next item in the collection.
288  operator ++()
289  {
290  ++iterator_;
291 
292  return *this;
293  }
294 
295 private:
296  ItemIterator iterator_;
297 };
298 
299 /// Implements TinySet for StrRef class.
300 ///
301 /// In contrast to TinySet<StrRef> instantiation,
302 /// given class allocates space for content being
303 /// referenced by StrRef instances inserted into
304 /// the given set thus making content safe for
305 /// referencing.
308 {
309  typedef
310  std::vector<std::string>
311  Items;
312 
313  typedef
314  Items::const_iterator
315  ItemConstIterator;
316 
317  typedef
318  Items::iterator
319  ItemIterator;
320 
321  class ItemLocator
322  {
323  StrRef sought_;
324 
325  public:
326  ItemLocator(
327  const StrRef& sought)
328  : sought_(sought)
329  {
330  }
331 
332  bool
333  operator ()(
334  const
335  std::string& other) const
336  {
337  return sought_ == other;
338  }
339  };
340 
341  Items items_;
342 
343 public:
344  /// Items of collection.
345  typedef StrRef Item;
346 
347  /// Aliases iterator type.
348  typedef
351 
352  /// Initializes the empty set.
354  {
355  }
356 
357  /// Initializes the empty set capacious
358  /// enough to store the given number of items.
359  explicit
361  size_t capacity)
362  {
363  reserve(capacity);
364  }
365 
366  /// Initializes as a copy of the other instance.
368  const TinyStrSet& other)
369  : items_(other.items_)
370  {
371  }
372 
373  /// Destructs the internal storage.
375  {
376  }
377 
378  /// Indicates whether the set is empty.
379  bool empty() const
380  {
381  return items_.empty();
382  }
383 
384  /// Returns the number of items in the set.
385  size_t size() const
386  {
387  return items_.size();
388  }
389 
390  /// Makes the internal storage capacious
391  /// enough to store the given number of items.
392  void
394  size_t capacity)
395  {
396  items_.reserve(capacity);
397  }
398 
399  /// Provides iterating facilities.
401  begin() const
402  {
403  return
405  items_.begin());
406  }
407 
408  /// Provides iterating facilities.
410  end() const
411  {
412  return
414  items_.end());
415  }
416 
417  /// Looks for the given item and returns iterator
418  /// pointing to the found entry or to nowhere.
421  const Item& item) const
422  {
423  return
425  std::find_if(
426  items_.begin(),
427  items_.end(),
428  ItemLocator(item)));
429  }
430 
431  /// Inserts the given item into the set.
432  ///
433  /// Content referenced by the item being
434  /// inserted is copied into the internal
435  /// storage.
436  ///
437  /// Returned value indicates whether
438  /// the item was actually inserted.
439  bool
441  const Item& item)
442  {
443  if (end() == find(item))
444  {
445  items_.push_back(toStr(item));
446 
447  return true;
448  }
449 
450  return false;
451  }
452 
453  /// Removes the given item from the set.
454  ///
455  /// Returned value indicates whether
456  /// item was actually erased.
457  bool
459  const Item& item)
460  {
461  ItemIterator
462  const found =
463  std::find_if(
464  items_.begin(),
465  items_.end(),
466  ItemLocator(item));
467 
468  if (items_.end() == found)
469  return false;
470 
471  items_.erase(found);
472 
473  return true;
474  }
475 
476  /// Brings the set to the blank (empty) state.
477  void
479  {
480  items_.clear();
481  }
482 
483  /// Exchanges content with the other instance.
484  void
486  TinyStrSet& other)
487  {
488  std::swap
489  (
490  items_,
491  other.items_
492  );
493  }
494 };
495 
std::vector< std::string > Items
Aliases for internal representation.
Definition: TinySet.h:217
ConstIterator end() const
Provides iterating facilities.
Definition: TinySet.h:410
Items::const_iterator ItemIterator
Aliases iterator over the internal store.
Definition: TinySet.h:222
StrRef Item
Items of collection.
Definition: TinySet.h:345
void reserve(size_t capacity)
Definition: TinySet.h:393
Implements iterator for the TinyStrSet class.
Definition: TinySet.h:210
void clear()
Brings the set to the blank state.
Definition: TinySet.h:174
ConstIterator begin() const
Provides iterating facilities.
Definition: TinySet.h:401
TinyStrSet()
Initializes the empty set.
Definition: TinySet.h:353
TinyStrSetIterator(const TinyStrSetIterator &other)
Initializes the instance as a copy of the other one.
Definition: TinySet.h:230
std::vector< Item > Items
Aliases internal representation.
Definition: TinySet.h:46
ConstIterator find(const Item &item) const
Tells whether the set contains given item.
Definition: TinySet.h:118
void toStr(std::string &str, const Message &message)
Serializes FIX message into tag=value format.
Definition: Message.h:379
~TinyStrSet()
Destructs the internal storage.
Definition: TinySet.h:374
void swap(TinyStrSet &other)
Exchanges content with the other instance.
Definition: TinySet.h:485
TinySet(const TinySet &other)
Initializes the set as a copy of the other one.
Definition: TinySet.h:70
TinyStrSetIterator()
Initializes the iterator pointing to nowhere.
Definition: TinySet.h:225
void clear()
Brings the set to the blank (empty) state.
Definition: TinySet.h:478
TinyStrSetIterator(const ItemIterator &iterator)
Initializes from iterator over the internal storage.
Definition: TinySet.h:238
bool erase(const Item &item)
Definition: TinySet.h:458
bool empty() const
Indicates whether the set is empty.
Definition: TinySet.h:82
void reserve(size_t capacity)
Definition: TinySet.h:96
~TinySet()
Cleans everything up.
Definition: TinySet.h:77
bool erase(const Item &item)
Definition: TinySet.h:152
void swap(TinySet &other)
Swaps content with the other instance.
Definition: TinySet.h:181
bool insert(const Item &item)
Definition: TinySet.h:134
TinySet(size_t capacity)
Definition: TinySet.h:63
~TinyStrSetIterator()
Cleans everything up.
Definition: TinySet.h:245
void copy(MbpBook< TargetPriceLevel, Depth > &target, const MbpBook< SourcePriceLevel, Depth > &source, Depth maxDepth=static_cast< Depth >(-1))
Definition: MbpBook.h:280
#define ONIXS_CONFLATEDUDP_NAMESPACE_END
Definition: Bootstrap.h:157
ConstIterator find(const Item &item) const
Definition: TinySet.h:420
ConstIterator end() const
Provides iterating facilities.
Definition: TinySet.h:111
TinySet()
Initializes the empty set.
Definition: TinySet.h:55
bool insert(const Item &item)
Definition: TinySet.h:440
bool empty() const
Indicates whether the set is empty.
Definition: TinySet.h:379
StrRef toStrRef(const std::string &str)
Constructs StrRef instance over std::string content.
Definition: String.h:174
Key Item
Aliases set item type.
Definition: TinySet.h:41
TinyStrSet(const TinyStrSet &other)
Initializes as a copy of the other instance.
Definition: TinySet.h:367
Items::const_iterator ConstIterator
Aliases iterator over set items.
Definition: TinySet.h:52
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:121
#define ONIXS_CONFLATEDUDP_LTWT_CLASS
Definition: Bootstrap.h:95
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:99
TinyStrSetIterator ConstIterator
Aliases iterator type.
Definition: TinySet.h:350
ConstIterator begin() const
Provides iterating facilities.
Definition: TinySet.h:104
size_t size() const
Returns the number of items in the set.
Definition: TinySet.h:88
#define ONIXS_CONFLATEDUDP_NAMESPACE_BEGIN
Definition: Bootstrap.h:153
size_t size() const
Returns the number of items in the set.
Definition: TinySet.h:385