OnixS C++ CME MDP Premium Market Data Handler  5.8.3
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 
27 #include <OnixS/CME/MDH/String.h>
28 
30 
31 /// Class implementing set optimized
32 /// for storing small number of items.
33 template <class Key>
34 class TinySet
35 {
36 public:
37  /// Aliases set item type.
38  typedef Key Item;
39 
40  /// Aliases internal representation.
41  typedef std::vector<Item> Items;
42 
43  /// Aliases iterator over set items.
44  typedef typename Items::const_iterator ConstIterator;
45 
46  /// Initializes the empty set.
47  TinySet() {}
48 
49  /// Initializes the empty set and configures
50  /// the internal storage to be enough capacious
51  /// to store the given number of items.
52  explicit TinySet(size_t capacity)
53  {
54  reserve(capacity);
55  }
56 
57  /// Initializes the set as a copy of the other one.
58  TinySet(const TinySet& other)
59  : items_(other.items_)
60  {
61  }
62 
63  /// Cleans everything up.
64  ~TinySet() {}
65 
66  /// Indicates whether the set is empty.
67  bool empty() const
68  {
69  return items_.empty();
70  }
71 
72  /// Returns the number of items in the set.
73  size_t size() const
74  {
75  return items_.size();
76  }
77 
78  /// Makes the internal storage capacious
79  /// enough to store the given number of items.
80  void reserve(size_t capacity)
81  {
82  items_.reserve(capacity);
83  }
84 
85  /// Provides iterating facilities.
86  ConstIterator begin() const
87  {
88  return items_.begin();
89  }
90 
91  /// Provides iterating facilities.
92  ConstIterator end() const
93  {
94  return items_.end();
95  }
96 
97  /// Tells whether the set contains given item.
98  ConstIterator find(const Item& item) const
99  {
100  return std::find(items_.begin(), items_.end(), item);
101  }
102 
103  /// Inserts the given item into the set.
104  ///
105  /// Returned value indicates whether the
106  /// item was inserted (true) or the set
107  /// already contained the given item (false).
108  bool insert(const Item& item)
109  {
110  if (end() == find(item))
111  {
112  items_.push_back(item);
113 
114  return true;
115  }
116 
117  return false;
118  }
119 
120  /// Removes the given item from the set.
121  ///
122  /// Returned value indicates whether
123  /// the item was actually removed.
124  bool erase(const Item& item)
125  {
126  typename Items::iterator const found = std::find(items_.begin(), items_.end(), item);
127 
128  if (items_.end() == found)
129  return false;
130 
131  items_.erase(found);
132 
133  return true;
134  }
135 
136  /// Brings the set to the blank state.
137  void clear()
138  {
139  items_.clear();
140  }
141 
142  /// Swaps content with the other instance.
143  void swap(TinySet& other)
144  {
145  std::swap(items_, other.items_);
146  }
147 
148  /// Re-initializes the instance
149  /// as a copy of the other one.
150  TinySet& operator=(const TinySet& other)
151  {
152  if (&other != this)
153  {
154  TinySet copy(other);
155 
156  swap(copy);
157  }
158 
159  return *this;
160  }
161 
162 private:
163  Items items_;
164 };
165 
166 /// Implements iterator for the TinyStrSet class.
168 {
169 public:
170  /// Aliases for internal representation.
171  typedef std::vector<std::string> Items;
172 
173  /// Aliases iterator over the internal store.
174  typedef Items::const_iterator ItemIterator;
175 
176  /// Initializes the iterator pointing to nowhere.
178 
179  /// Initializes the instance as a copy of the other one.
181  : iterator_(other.iterator_)
182  {
183  }
184 
185  /// Initializes from iterator over the internal storage.
186  explicit TinyStrSetIterator(const ItemIterator& iterator)
187  : iterator_(iterator)
188  {
189  }
190 
191  /// Cleans everything up.
193 
194  /// Provides access to the underlying object.
196  {
197  return toStrRef(*iterator_);
198  }
199 
200  /// Compares with the other instance.
201  bool operator==(const TinyStrSetIterator& other) const
202  {
203  return iterator_ == other.iterator_;
204  }
205 
206  /// Compares with the other instance.
207  bool operator!=(const TinyStrSetIterator& other) const
208  {
209  return iterator_ != other.iterator_;
210  }
211 
212  /// Re-initializes as a copy of the other instance.
214  {
215  iterator_ = other.iterator_;
216 
217  return *this;
218  }
219 
220  /// Advances the instance to the
221  /// next item in the collection.
223  {
224  ++iterator_;
225 
226  return *this;
227  }
228 
229 private:
230  ItemIterator iterator_;
231 };
232 
233 /// Implements TinySet for StrRef class.
234 ///
235 /// In contrast to TinySet<StrRef> instantiation,
236 /// given class allocates space for content being
237 /// referenced by StrRef instances inserted into
238 /// the given set thus making content safe for
239 /// referencing.
241 {
242 public:
243  /// Items of collection.
244  typedef StrRef Item;
245 
246  /// Aliases iterator type.
248 
249  /// Initializes the empty set.
251 
252  /// Initializes the empty set capacious
253  /// enough to store the given number of items.
254  explicit TinyStrSet(size_t capacity)
255  {
256  reserve(capacity);
257  }
258 
259  /// Initializes as a copy of the other instance.
260  TinyStrSet(const TinyStrSet& other)
261  : items_(other.items_)
262  {
263  }
264 
265  /// Destructs the internal storage.
267 
268  /// Indicates whether the set is empty.
269  bool empty() const
270  {
271  return items_.empty();
272  }
273 
274  /// Returns the number of items in the set.
275  size_t size() const
276  {
277  return items_.size();
278  }
279 
280  /// Makes the internal storage capacious
281  /// enough to store the given number of items.
282  void reserve(size_t capacity)
283  {
284  items_.reserve(capacity);
285  }
286 
287  /// Provides iterating facilities.
288  ConstIterator begin() const
289  {
290  return ConstIterator(items_.begin());
291  }
292 
293  /// Provides iterating facilities.
294  ConstIterator end() const
295  {
296  return ConstIterator(items_.end());
297  }
298 
299  /// Looks for the given item and returns iterator
300  /// pointing to the found entry or to nowhere.
301  ConstIterator find(const Item& item) const
302  {
303  return ConstIterator(std::find_if(items_.begin(), items_.end(), ItemLocator(item)));
304  }
305 
306  /// Inserts the given item into the set.
307  ///
308  /// Content referenced by the item being
309  /// inserted is copied into the internal
310  /// storage.
311  ///
312  /// Returned value indicates whether
313  /// the item was actually inserted.
314  bool insert(const Item& item)
315  {
316  if (end() == find(item))
317  {
318  items_.push_back(toStr(item));
319 
320  return true;
321  }
322 
323  return false;
324  }
325 
326  /// Removes the given item from the set.
327  ///
328  /// Returned value indicates whether
329  /// item was actually erased.
330  bool erase(const Item& item)
331  {
332  const ItemIterator found = std::find_if(items_.begin(), items_.end(), ItemLocator(item));
333 
334  if (items_.end() == found)
335  return false;
336 
337  items_.erase(found);
338 
339  return true;
340  }
341 
342  /// Brings the set to the blank (empty) state.
343  void clear()
344  {
345  items_.clear();
346  }
347 
348  /// Exchanges content with the other instance.
349  void swap(TinyStrSet& other)
350  {
351  std::swap(items_, other.items_);
352  }
353 
354 private:
355  typedef std::vector<std::string> Items;
356 
357  typedef Items::const_iterator ItemConstIterator;
358 
359  typedef Items::iterator ItemIterator;
360 
361  class ItemLocator
362  {
363  StrRef sought_;
364 
365  public:
366  ItemLocator(const StrRef& sought)
367  : sought_(sought)
368  {
369  }
370 
371  bool operator()(const std::string& other) const
372  {
373  return sought_ == other;
374  }
375  };
376 
377  Items items_;
378 };
379 
TinySet(const TinySet &other)
Initializes the set as a copy of the other one.
Definition: TinySet.h:58
size_t size() const
Returns the number of items in the set.
Definition: TinySet.h:73
Key Item
Aliases set item type.
Definition: TinySet.h:38
size_t size() const
Returns the number of items in the set.
Definition: TinySet.h:275
bool erase(const Item &item)
Removes the given item from the set.
Definition: TinySet.h:330
bool empty() const
Indicates whether the set is empty.
Definition: TinySet.h:269
void swap(TinySet &other)
Swaps content with the other instance.
Definition: TinySet.h:143
TinyStrSet(size_t capacity)
Initializes the empty set capacious enough to store the given number of items.
Definition: TinySet.h:254
ConstIterator end() const
Provides iterating facilities.
Definition: TinySet.h:92
void clear()
Brings the set to the blank (empty) state.
Definition: TinySet.h:343
TinyStrSetIterator & operator++()
Advances the instance to the next item in the collection.
Definition: TinySet.h:222
TinyStrSet()
Initializes the empty set.
Definition: TinySet.h:250
std::vector< Item > Items
Aliases internal representation.
Definition: TinySet.h:41
~TinyStrSet()
Destructs the internal storage.
Definition: TinySet.h:266
Items::const_iterator ItemIterator
Aliases iterator over the internal store.
Definition: TinySet.h:174
TinySet(size_t capacity)
Initializes the empty set and configures the internal storage to be enough capacious to store the giv...
Definition: TinySet.h:52
StrRef operator*() const
Provides access to the underlying object.
Definition: TinySet.h:195
ConstIterator end() const
Provides iterating facilities.
Definition: TinySet.h:294
TinySet()
Initializes the empty set.
Definition: TinySet.h:47
Implements TinySet for StrRef class.
Definition: TinySet.h:240
void clear()
Brings the set to the blank state.
Definition: TinySet.h:137
void swap(TinyStrSet &other)
Exchanges content with the other instance.
Definition: TinySet.h:349
~TinySet()
Cleans everything up.
Definition: TinySet.h:64
#define ONIXS_CMEMDH_LTWT
Definition: Bootstrap.h:46
bool insert(const Item &item)
Inserts the given item into the set.
Definition: TinySet.h:108
bool erase(const Item &item)
Removes the given item from the set.
Definition: TinySet.h:124
bool operator!=(const TinyStrSetIterator &other) const
Compares with the other instance.
Definition: TinySet.h:207
MboBook & copy(MboBook &target, const MboBook &source)
Copies content of MBO book to the other one.
Definition: MboBook.h:210
bool operator==(const TinyStrSetIterator &other) const
Compares with the other instance.
Definition: TinySet.h:201
void toStr(std::string &str, const Message &message)
Serializes FIX message into tag=value format.
Definition: Message.h:318
TinyStrSet(const TinyStrSet &other)
Initializes as a copy of the other instance.
Definition: TinySet.h:260
ConstIterator find(const Item &item) const
Tells whether the set contains given item.
Definition: TinySet.h:98
ConstIterator find(const Item &item) const
Looks for the given item and returns iterator pointing to the found entry or to nowhere.
Definition: TinySet.h:301
void reserve(size_t capacity)
Makes the internal storage capacious enough to store the given number of items.
Definition: TinySet.h:80
Class implementing set optimized for storing small number of items.
Definition: TinySet.h:34
Provides efficient way of accessing text-based values without copying content of the text being refer...
Definition: String.h:41
Items::const_iterator ConstIterator
Aliases iterator over set items.
Definition: TinySet.h:44
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:67
bool empty() const
Indicates whether the set is empty.
Definition: TinySet.h:67
StrRef Item
Items of collection.
Definition: TinySet.h:244
TinyStrSetIterator(const ItemIterator &iterator)
Initializes from iterator over the internal storage.
Definition: TinySet.h:186
TinyStrSetIterator()
Initializes the iterator pointing to nowhere.
Definition: TinySet.h:177
Implements iterator for the TinyStrSet class.
Definition: TinySet.h:167
StrRef toStrRef(const std::string &str)
Constructs StrRef instance over std::string content.
Definition: String.h:155
TinySet & operator=(const TinySet &other)
Re-initializes the instance as a copy of the other one.
Definition: TinySet.h:150
~TinyStrSetIterator()
Cleans everything up.
Definition: TinySet.h:192
ConstIterator begin() const
Provides iterating facilities.
Definition: TinySet.h:86
void reserve(size_t capacity)
Makes the internal storage capacious enough to store the given number of items.
Definition: TinySet.h:282
TinyStrSetIterator & operator=(const TinyStrSetIterator &other)
Re-initializes as a copy of the other instance.
Definition: TinySet.h:213
ConstIterator begin() const
Provides iterating facilities.
Definition: TinySet.h:288
bool insert(const Item &item)
Inserts the given item into the set.
Definition: TinySet.h:314
std::vector< std::string > Items
Aliases for internal representation.
Definition: TinySet.h:171
TinyStrSetIterator(const TinyStrSetIterator &other)
Initializes the instance as a copy of the other one.
Definition: TinySet.h:180
TinyStrSetIterator ConstIterator
Aliases iterator type.
Definition: TinySet.h:247
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:68