OnixS C++ CME MDP Premium Market Data Handler 5.11.1
Users' manual and API documentation
Loading...
Searching...
No Matches
VectorOverArray.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 <cassert>
24#include <cstdint>
25#include <type_traits>
26#include <utility>
27#include <new>
28
30
32
33namespace details {
36void throwVectorIndexOutOfRange(std::uintmax_t requestedIndex, std::uintmax_t actualSize);
37
40void throwVectorCopyOverflowImpl(std::uintmax_t targetCapacity, std::uintmax_t sourceItemCount);
41}
42
66template <typename ItemType, typename SizeType>
68{
69 static_assert(std::is_nothrow_destructible<ItemType>::value, "ItemType must be nothrow-destructible");
70
71 static_assert(std::is_integral<SizeType>::value, "SizeType must be integral");
72 static_assert(std::is_unsigned<SizeType>::value, "SizeType must be unsigned");
73 static_assert(!std::is_same<SizeType, bool>::value, "SizeType must not be bool");
74
75public:
77 using Item = ItemType;
78
80 using Size = SizeType;
81
83 using ConstIterator = const Item*;
84
86 using Iterator = Item*;
87
91 VectorOverArray() noexcept = default;
92
101 : items_(items)
102 , size_(0)
103 , capacity_(capacity)
104 {
105 }
106
109 {
110 clear();
111 }
112
116 bool empty() const noexcept
117 {
118 return (0 == size_);
119 }
120
124 Size size() const noexcept
125 {
126 return size_;
127 }
128
132 Size capacity() const noexcept
133 {
134 return capacity_;
135 }
136
140 Iterator begin() noexcept
141 {
142 return items_;
143 }
144
148 ConstIterator begin() const noexcept
149 {
150 return items_;
151 }
152
156 Iterator end() noexcept
157 {
158 return (size_ == 0) ? items_ : (items_ + size_);
159 }
160
164 ConstIterator end() const noexcept
165 {
166 return (size_ == 0) ? items_ : (items_ + size_);
167 }
168
174 Item& front() noexcept
175 {
176 assert(0 < size_);
177
178 return items_[0];
179 }
180
186 const Item& front() const noexcept
187 {
188 assert(0 < size_);
189
190 return items_[0];
191 }
192
199 Item& operator[](Size index) noexcept
200 {
201 assert(index < size_);
202
203 return items_[index];
204 }
205
212 const Item& operator[](Size index) const noexcept
213 {
214 assert(index < size_);
215
216 return items_[index];
217 }
218
224 Item& at(Size index)
225 {
226 if ONIXS_CMEMDH_LIKELY(index < size_)
227 return items_[index];
228
229 details::throwVectorIndexOutOfRange(static_cast<std::uintmax_t>(index), static_cast<std::uintmax_t>(size_));
230 }
231
237 const Item& at(Size index) const
238 {
239 if ONIXS_CMEMDH_LIKELY(index < size_)
240 return items_[index];
241
242 details::throwVectorIndexOutOfRange(static_cast<std::uintmax_t>(index), static_cast<std::uintmax_t>(size_));
243 }
244
255 Item& insertExtrude(Size index) noexcept
256 {
257 static_assert(std::is_nothrow_copy_constructible<Item>::value, "Item must be nothrow-copy-constructible");
258
259 assert(index < size_);
260
261 Item* const toBeUpdated = items_ + index;
262
263 Item* target = items_ + size_ - 1;
264
265 while (target != toBeUpdated)
266 {
267 target->~Item();
268
269 new (target) Item(*(target - 1));
270 --target;
271 }
272
273 return *toBeUpdated;
274 }
275
282 void erase(Size index) noexcept
283 {
284 static_assert(std::is_nothrow_move_constructible<Item>::value, "Item must be nothrow-move-constructible");
285
286 assert(index < size_);
287
288 Item* const theEnd = end();
289 Item* const toBeErased = items_ + index;
290
291 Item* target = toBeErased;
292 Item* source = toBeErased + 1;
293
294 while (source != theEnd)
295 {
296 target->~Item();
297
298 new (target) Item(std::move(*source));
299
300 ++target;
301 ++source;
302 }
303
304 target->~Item();
305 --size_;
306 }
307
311 void clear() noexcept
312 {
313 shrinkTo(0);
314 }
315
328 void resize(Size newSize)
329 {
330 static_assert(std::is_default_constructible<Item>::value, "Item must be default-constructible");
331 static_assert(std::is_copy_constructible<Item>::value, "Item must be copy-constructible");
332
333 assert(newSize <= capacity_);
334
335 if (newSize > size_)
336 growTo(newSize, Item {});
337 else
338 shrinkTo(newSize);
339 }
340
353 void resize(Size newSize, const Item& item)
354 {
355 static_assert(std::is_copy_constructible<Item>::value, "Item must be copy-constructible");
356
357 assert(newSize <= capacity_);
358
359 if (newSize > size_)
360 growTo(newSize, item);
361 else
362 shrinkTo(newSize);
363 }
364
365private:
366 void growTo(Size newSize, const Item& item)
367 {
368 while (size_ < newSize)
369 {
370 new (items_ + size_) Item(item);
371 ++size_;
372 }
373 }
374
375 void shrinkTo(Size newSize) noexcept
376 {
377 while (size_ > newSize)
378 (items_ + --size_)->~Item();
379 }
380
381 Item* items_ = nullptr;
382 Size size_ = 0;
383
384 const Size capacity_ = 0;
385
386 VectorOverArray(const VectorOverArray&) = delete;
387 VectorOverArray& operator=(const VectorOverArray&) = delete;
388 VectorOverArray(VectorOverArray&&) = delete;
389 VectorOverArray& operator=(VectorOverArray&&) = delete;
390};
391
398template <typename TargetSize, typename SourceSize>
399ONIXS_CMEMDH_NORETURN void throwVectorCopyOverflow(TargetSize targetCapacity, SourceSize sourceItemCount)
400{
401 details::throwVectorCopyOverflowImpl(static_cast<std::uintmax_t>(targetCapacity), static_cast<std::uintmax_t>(sourceItemCount));
402}
403
420template <typename TargetItem, typename TargetSize, typename SourceItem, typename SourceSize, typename ItemCount>
422{
423 static_assert(std::is_assignable<TargetItem&, const SourceItem&>::value, "TargetItem must be assignable from the SourceItem");
424
425 std::uintmax_t remainingCount = static_cast<std::uintmax_t>(itemCount);
426 const std::uintmax_t sourceSize = static_cast<std::uintmax_t>(source.size());
427 remainingCount = (remainingCount > sourceSize) ? sourceSize : remainingCount;
428
429 if ONIXS_CMEMDH_UNLIKELY(remainingCount > static_cast<std::uintmax_t>(target.capacity()))
430 throwVectorCopyOverflow(target.capacity(), remainingCount);
431
432 target.resize(static_cast<TargetSize>(remainingCount));
433
434 while (remainingCount)
435 {
436 --remainingCount;
437
438 target[static_cast<TargetSize>(remainingCount)] = source[static_cast<SourceSize>(remainingCount)];
439 }
440}
441
445template <typename TargetItem, typename TargetSize, typename SourceItem, typename SourceSize>
447{
448 copy(target, source, source.size());
449}
450
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:69
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:73
#define ONIXS_CMEMDH_COLDPATH
Definition Compiler.h:168
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:163
#define ONIXS_CMEMDH_NORETURN
Definition Compiler.h:164
#define ONIXS_CMEMDH_NODISCARD
Definition Compiler.h:165
Provides vector-like access to caller-owned, fixed-capacity storage.
~VectorOverArray() noexcept
Destroys all active items without deallocating the backing storage.
VectorOverArray() noexcept=default
Creates an empty container without backing storage.
void resize(Size newSize)
Changes the number of active items within the fixed capacity.
Item & insertExtrude(Size index) noexcept
Opens a position by shifting subsequent items toward the end.
bool empty() const noexcept
Checks whether the container has no active items.
ConstIterator begin() const noexcept
Obtains a read-only iterator to the first item.
void erase(Size index) noexcept
Erases an item and shifts subsequent items toward the beginning.
Iterator begin() noexcept
Obtains a mutable iterator to the first item.
Item & at(Size index)
Accesses an active item with bounds checking.
Size size() const noexcept
Obtains the number of active items.
const Item & operator[](Size index) const noexcept
Accesses an active item without runtime bounds checking.
const Item & front() const noexcept
Accesses the first active item.
void resize(Size newSize, const Item &item)
Changes the number of active items within the fixed capacity.
ConstIterator end() const noexcept
Obtains a read-only iterator past the last active item.
Item & operator[](Size index) noexcept
Accesses an active item without runtime bounds checking.
Iterator end() noexcept
Obtains a mutable iterator past the last active item.
Item & front() noexcept
Accesses the first active item.
const Item & at(Size index) const
Accesses an active item with bounds checking.
MboBook & copy(MboBook &target, const MboBook &source)
Copies content of MBO book to the other one.
Definition MboBook.h:210
void throwVectorCopyOverflow(TargetSize targetCapacity, SourceSize sourceItemCount)
Reports that a source range does not fit in a target container.