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