Ocean
Loading...
Searching...
No Matches
Accessor.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_BASE_ACCESSOR_H
9#define META_OCEAN_BASE_ACCESSOR_H
10
11#include "ocean/base/Base.h"
12#include "ocean/base/Callback.h"
13
14namespace Ocean
15{
16
17/**
18 * This class implements a base class for all accessors.
19 * Accessors provide access to any kind of data elements which are stored in any kind of data structure by any kind of access method.<br>
20 * @ingroup base
21 */
23{
24 public:
25
26 /**
27 * Default destructor
28 */
29 virtual ~Accessor() = default;
30
31 /**
32 * Returns the number of accessible elements of this accessor object.
33 * @return The number of elements
34 */
35 virtual size_t size() const = 0;
36
37 /**
38 * Returns whether this accessor provides no elements.
39 * @return True, if so
40 */
41 inline bool isEmpty() const;
42
43 /**
44 * Returns all elements of a given accessor (as a block).
45 * @param accessor The accessor from which all elements are extracted
46 * @return The elements as a block
47 */
48 template <typename TAccessor>
49 static std::vector<typename TAccessor::Type> accessor2elements(const TAccessor& accessor);
50
51 /**
52 * Returns all elements of a given accessor as a map with key and elements.
53 * @param accessor The accessor from which all elements are extracted
54 * @return The elements as a map
55 */
56 template <typename TAccessor>
57 static std::unordered_map<typename TAccessor::KeyType, typename TAccessor::Type> accessor2map(const TAccessor& accessor);
58
59 /**
60 * Returns a subset of all elements of a given accessor (as a block).
61 * @param accessor The accessor from which all sub elements are extracted.
62 * @param subset The individual indices of the subset's elements
63 * @return Subset elements as a block
64 */
65 template <typename TAccessor, typename TIndex>
66 static std::vector<typename TAccessor::Type> accessor2subsetElements(const TAccessor& accessor, const std::vector<TIndex>& subset);
67
68 protected:
69
70 /**
71 * Protected default constructor.
72 */
73 Accessor() = default;
74
75 /**
76 * Protected copy constructor.
77 * @param accessor Accessor to copy
78 */
79 Accessor(const Accessor& accessor) = default;
80
81 /**
82 * Deleted assign operator.
83 * @param accessor Accessor which would be assigned
84 * @return Reference to this object
85 */
86 Accessor& operator=(const Accessor& accessor) = delete;
87};
88
89/**
90 * This class implements a base class for accessors allowing a constant reference access.
91 * @tparam T The data type of the elements of the accessor
92 * @tparam TKey The data type of the keys of the accessor
93 * @ingroup base
94 */
95template <typename T, typename TKey>
96class ConstAccessor : public Accessor
97{
98 public:
99
100 /**
101 * Definition of the element type of this accessor.
102 */
103 using Type = T;
104
105 /**
106 * Definition of the key (or e.g., index) type of this accessor.
107 */
108 using KeyType = TKey;
109
110 public:
111
112 /**
113 * Returns a pointer to the elements of this accessor if the data exists within one memory block without gaps.
114 * Beware: There is not guarantee that the data of an accessor is stored as one memory block, thus be prepared that the resulting pointer is nullptr.
115 * @return The pointer to the memory block of this accessor, if available
116 */
117 virtual const T* data() const;
118
119 /**
120 * Returns whether this accessor has a specific element.
121 * @param key The key of the element to be checked
122 * @return True, if the element exists
123 */
124 virtual bool canAccess(const TKey& key) const = 0;
125
126 /**
127 * Returns the first element of this accessor.
128 * @param element The resulting first element by copying the element (by using the assign operator)
129 * @param key The resulting key of the first element
130 * @return True, if at least one element exists
131 */
132 virtual bool firstElement(T& element, TKey& key) const = 0;
133
134 /**
135 * Returns the next element which follows a given key of the previous element.
136 * @param previousKey The previous key for which the next following element is requested
137 * @param nextElement The resulting next element by copying the element (by using the assign operator)
138 * @param nextKey The resulting key of the next element
139 * @return True, if a next element exists
140 */
141 virtual bool nextElement(const TKey& previousKey, T& nextElement, TKey& nextKey) const = 0;
142
143 /**
144 * Returns one element of this accessor object by a given key.
145 * @param key The key of element to be accessed, must be valid
146 * @return The requested element
147 */
148 virtual const T& operator[](const TKey& key) const = 0;
149
150 protected:
151
152 /**
153 * Protected default constructor.
154 */
155 ConstAccessor() = default;
156};
157
158/**
159 * This class implements a base class for accessors allowing a non-constant reference access.
160 * @tparam T The data type of the elements of the accessor
161 * @tparam TKey The data type of the keys of the accessor
162 * @ingroup base
163 */
164template <typename T, typename TKey>
165class NonconstAccessor : public ConstAccessor<T, TKey>
166{
167 // we want to keep the const data function from the base class
168 using ConstAccessor<T, TKey>::data;
169
170 public:
171
172 /**
173 * Returns a pointer to the elements of this accessor if the data exists within one memory block without gaps.
174 * Beware: There is not guarantee that the data of an accessor is stored as one memory block, thus be prepared that the resulting pointer is nullptr.
175 * @return The pointer to the memory block of this accessor, if available
176 */
177 virtual T* data();
178
179 /**
180 * Returns one element of this accessor object by a given key.
181 * @param key The key of element to be accessed, must be valid
182 * @return The requested element
183 */
184 virtual T& operator[](const TKey& key) = 0;
185
186 protected:
187
188 /**
189 * Creates a new indexed-based accessor object.
190 */
191 NonconstAccessor() = default;
192};
193
194/**
195 * This class implements a base class for all accessors allowing to access temporary elements.
196 * @tparam T The data type of the elements of the accessor
197 * @tparam TKey The data type of the keys of the accessor
198 * @ingroup base
199 */
200template <typename T, typename TKey>
202{
203 public:
204
205 /**
206 * Definition of the element type of this accessor.
207 */
208 using Type = T;
209
210 public:
211
212 /**
213 * Returns one element of this accessor object by a given index.
214 * @param key The key of element to be accessed, must be valid
215 * @return The requested element
216 */
217 virtual T operator[](const TKey& key) const = 0;
218
219 /**
220 * Returns whether this accessor has a specific element.
221 * @param key The key of the element to be checked
222 * @return True, if the element exists
223 */
224 virtual bool canAccess(const TKey& key) const = 0;
225
226 protected:
227
228 /**
229 * Creates a new indexed-based accessor object.
230 */
231 TemporaryAccessor() = default;
232};
233
234/**
235 * This class implements a base class for all indexed-based accessors allowing a constant reference access only.
236 * @tparam T The data type of the elements of the accessor
237 * @ingroup base
238 */
239template <typename T>
240class ConstIndexedAccessor : public ConstAccessor<T, size_t>
241{
242 public:
243
244 /**
245 * Returns whether this accessor has a specific element.
246 * @see ConstAccessor::canAccess().
247 */
248 virtual bool canAccess(const size_t& index) const;
249
250 /**
251 * Returns the first element of this accessor.
252 * @see ConstAccessor:firstElement().
253 */
254 virtual bool firstElement(T& element, size_t& index) const;
255
256 /**
257 * Returns the next element which follows a given key of the previous element.
258 * @see ConstAccessor::nextElement().
259 */
260 virtual bool nextElement(const size_t& previousIndex, T& nextElement, size_t& nextIndex) const;
261
262 /**
263 * Returns one element of this accessor object by a given index.
264 * @param index The index of element to be accessed, with range [0, size())
265 * @return The requested element
266 */
267 virtual const T& operator[](const size_t& index) const = 0;
268
269 protected:
270
271 /**
272 * Creates a new indexed-based accessor object.
273 */
275};
276
277/**
278 * This class implements a base class for all indexed-based accessors allowing a non-constant reference access.
279 * @tparam T The data type of the elements of the accessor
280 * @ingroup base
281 */
282template <typename T>
284{
285 public:
286
287 /**
288 * Returns whether this accessor has a specific element.
289 * @see ConstAccessor::canAccess().
290 */
291 virtual bool canAccess(const size_t& index) const;
292
293 /**
294 * Returns the first element of this accessor.
295 * @see ConstAccessor:firstElement().
296 */
297 virtual bool firstElement(T& element, size_t& index) const;
298
299 /**
300 * Returns the next element which follows a given key of the previous element.
301 * @see ConstAccessor::nextElement().
302 */
303 virtual bool nextElement(const size_t& previousIndex, T& nextElement, size_t& nextIndex) const;
304
305 /**
306 * Returns the pointer to this object if this accessor holds at least one element (if this accessor is not empty).
307 * This function can be used to simplify code fragments in which an optional pointer to a non-const accessor is used depending on the factor whether the accessor holds elements or not.
308 * @return The accessor's pointer, otherwise nullptr
309 */
311
312 /**
313 * Returns one element of this accessor object by a given key.
314 * @param index The index of element to be accessed, with range [0, size())
315 * @return The requested element
316 */
317 virtual const T& operator[](const size_t& index) const = 0;
318
319 /**
320 * Returns one element of this accessor object by a given index.
321 * @param index The index of element to be accessed, with range [0, size())
322 * @return The requested element
323 */
324 virtual T& operator[](const size_t& index) = 0;
325
326 protected:
327
328 /**
329 * Creates a new accessor object.
330 */
332};
333
334/**
335 * This class implements a base class for all indexed-based accessors allowing to access temporary elements.
336 * @tparam T The data type of the elements of the accessor
337 * @ingroup base
338 */
339template <typename T>
341{
342 public:
343
344 /**
345 * Returns one element of this accessor object by a given index.
346 * @param index The index of element to be accessed, with range [0, size())
347 * @return The requested element
348 */
349 virtual T operator[](const size_t& index) const = 0;
350
351 /**
352 * Returns whether this accessor has a specific element.
353 * @param index The index of the element to be checked
354 * @return True, if the element exists
355 */
356 virtual bool canAccess(const size_t& index) const;
357
358 protected:
359
360 /**
361 * Creates a new indexed-based accessor object.
362 */
364};
365
366/**
367 * This class implements an accessor providing direct access to a constant array of elements.
368 * An instance of this accessor does not copy the elements, thus the caller has to ensure that the actual elements exist as long as the instance of the accessor exists.<br>
369 *
370 * The application of the this class is demonstrated in the following code example (compare the code example for ConstTemplateArrayAccessor):
371 * @code
372 * // the parameter type is a base class of ConstArrayAccessor<Object> allowing a more flexible application of this function
373 * void iterate(const ConstIndexedAccessor<Object>& accessor)
374 * {
375 * for (size_t n = 0; n < accessor.size(); ++n)
376 * {
377 * // access the object by application of the !virtual! index operator
378 * const Object& object = accessor[n];
379 *
380 * // ... do something with the object ...
381 * }
382 * }
383 *
384 * void main()
385 * {
386 * std::vector<Object> objects;
387 *
388 * // ... add some objects ...
389 *
390 * // iterate over all objects
391 * iterate(ConstArrayAccessor<Object>(objects));
392 * }
393 * @endcode
394 * @tparam T The data type of the elements of the accessor
395 * @see ConstTemplateArrayAccessor, ConstElementAccessor.
396 * @ingroup base
397 */
398template <typename T>
400{
401 public:
402
403 /**
404 * Creates a new empty accessor.
405 */
407
408 /**
409 * Move constructor.
410 * @param accessor Accessor to be moved
411 */
412 inline ConstArrayAccessor(ConstArrayAccessor<T>&& accessor) noexcept;
413
414 /**
415 * Creates a new accessor object.
416 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
417 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
418 * @param size The number of elements that can be accessed, may be 0 if elements is nullptr
419 */
420 inline ConstArrayAccessor(const T* elements, const size_t size);
421
422 /**
423 * Creates a new accessor object.
424 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
425 * @param elements A vector holding all elements
426 */
427 explicit inline ConstArrayAccessor(const std::vector<T>& elements);
428
429 /**
430 * Returns a pointer to the elements of this accessor if the data exists within one memory block without gaps.
431 * @see ConstAccessor::size().
432 */
433 virtual const T* data() const;
434
435 /**
436 * Returns the number of accessible elements of this accessor object.
437 * @see ConstAccessor::size().
438 */
439 virtual size_t size() const;
440
441 /**
442 * Returns one element of this accessor object.
443 * @see ConstAccessor::operator[].
444 */
445 virtual const T& operator[](const size_t& index) const;
446
447 /**
448 * Move operator.
449 * @param accessor Accessor to be moved
450 * @return Reference to this accessor
451 */
452 inline ConstArrayAccessor<T>& operator=(ConstArrayAccessor<T>&& accessor) noexcept;
453
454 protected:
455
456 /// The elements of this accessor.
457 const T* elements_ = nullptr;
458
459 /// The number of elements that can be accessed.
460 size_t size_ = 0;
461};
462
463/**
464 * This class implements an accessor providing direct access to std::shared_ptr<T> elements returned as const T* pointers.
465 * An instance of this accessor does not copy the elements, thus the caller has to ensure that the actual elements exist as long as the instance of the accessor exists.<br>
466 * This class is mainly a helper class to avoid extracting pointer from a shared_ptr objects and using a normal `ConstArrayAccessor`.
467 *
468 * The application of the this class is demonstrated in the following code example:
469 * @code
470 * void iterate(const ConstIndexedAccessor<const Object*>& accessor)
471 * {
472 * for (size_t n = 0; n < accessor.size(); ++n)
473 * {
474 * // access the object by application of the !virtual! index operator
475 * const Object* object = accessor[n];
476 *
477 * // ... do something with the object ...
478 * }
479 * }
480 *
481 * void main()
482 * {
483 * std::vector<std::shared_ptr<Object>> objects;
484 *
485 * // ... add some objects ...
486 *
487 * // iterate over all objects
488 * iterate(SharedPointerConstArrayAccessor<Object>(objects));
489 * }
490 * @endcode
491 * @tparam T The data type of the shared_ptr's element_type
492 * @see ConstArrayAccessor.
493 * @ingroup base
494 */
495template <typename T>
497{
498 public:
499
500 /**
501 * Definition of the shared pointer object.
502 */
503 using SharedPointer = std::shared_ptr<T>;
504
505 /**
506 * Definition of a vector holding the shared pointer objects.
507 */
508 using SharedPointers = std::vector<SharedPointer>;
509
510 public:
511
512 /**
513 * Creates a new empty accessor.
514 */
516
517 /**
518 * Move constructor.
519 * @param accessor Accessor to be moved
520 */
522
523 /**
524 * Creates a new accessor object.
525 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
526 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
527 * @param size The number of elements that can be accessed, may be 0 if elements is nullptr
528 */
529 inline SharedPointerConstArrayAccessor(const SharedPointer* elements, const size_t size);
530
531 /**
532 * Creates a new accessor object.
533 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
534 * @param elements A vector holding all elements
535 */
536 explicit inline SharedPointerConstArrayAccessor(const SharedPointers& elements);
537
538 /**
539 * Returns the number of accessible elements of this accessor object.
540 * @see ConstAccessor::size().
541 */
542 virtual size_t size() const;
543
544 /**
545 * Returns one element of this accessor object.
546 * @see ConstAccessor::operator[].
547 */
548 virtual const T* const & operator[](const size_t& index) const;
549
550 /**
551 * Move operator.
552 * @param accessor Accessor to be moved
553 * @return Reference to this accessor
554 */
556
557 protected:
558
559 /// The pointers to the actual elements wrapped in the shared pointers.
560 std::vector<const T*> elements_;
561};
562
563/**
564 * This class implements an accessor providing direct access to a constant array of elements.
565 * An instance of this accessor does not copy the elements, thus the caller has to ensure that the actual elements exist as long as the instance of the accessor exists.<br>
566 * This class is not derived from Accessor (or any other Accessor class) to avoid virtual functions ensuring higher element-access performances.<br>
567 * Thus, this class will mainly be applied in template functions where the type of the accessor is defined at compile time.
568 *
569 * The application of the this class is demonstrated in the following code example (compare the code example for ConstArrayAccessor):
570 * @code
571 * // the parameter type is a template type allowing for fast but not flexible application of this function
572 * template <typename TAccessor>
573 * void iterate(const TAccessor& accessor)
574 * {
575 * for (size_t n = 0; n < accessor.size(); ++n)
576 * {
577 * // access the object by application of the !inlined! index operator
578 * const Object& object = accessor[n];
579 *
580 * // ... do something with the object ...
581 * }
582 * }
583 *
584 * void main()
585 * {
586 * std::vector<Object> objects;
587 *
588 * // ... add some objects ...
589 *
590 * // iterate over all objects
591 * iterate(ConstTemplateArrayAccessor<Object>(objects));
592 * }
593 * @endcode
594 * @tparam T The data type of the elements of the accessor
595 * @see ConstArrayAccessor, NonconstTemplateArrayAccessor.
596 * @ingroup base
597 */
598template <typename T>
600{
601 public:
602
603 /**
604 * Definition of the element type of this accessor.
605 */
606 using Type = T;
607
608 /**
609 * Definition of the key (or e.g., index) type of this accessor.
610 */
612
613 public:
614
615 /**
616 * Creates a new empty accessor.
617 */
619
620 /**
621 * Move constructor.
622 * @param accessor Accessor to be moved
623 */
625
626 /**
627 * Creates a new accessor object.
628 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
629 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
630 * @param size The number of elements that can be accessed, may be 0 if elements is nullptr
631 */
632 inline ConstTemplateArrayAccessor(const T* elements, const size_t size);
633
634 /**
635 * Creates a new accessor object.
636 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
637 * @param elements A vector holding all elements
638 */
639 explicit inline ConstTemplateArrayAccessor(const std::vector<T>& elements);
640
641 /**
642 * Returns a pointer to the elements of this accessor if the data exists within one memory block without gaps.
643 * @see ConstAccessor::data().
644 */
645 inline const T* data() const;
646
647 /**
648 * Returns the number of accessible elements of this accessor object.
649 * @see ConstAccessor::size().
650 */
651 inline size_t size() const;
652
653 /**
654 * Returns whether this accessor provides no elements.
655 * @see Accessor::isEmpty().
656 */
657 inline bool isEmpty() const;
658
659 /**
660 * Returns whether this accessor has a specific element.
661 * @see ConstAccessor::canAccess().
662 */
663 inline bool canAccess(const size_t& index) const;
664
665 /**
666 * Returns the first element of this accessor.
667 * @see ConstAccessor:firstElement().
668 */
669 inline bool firstElement(T& element, size_t& index) const;
670
671 /**
672 * Returns the next element which follows a given key of the previous element.
673 * @see ConstAccessor::nextElement().
674 */
675 inline bool nextElement(const size_t& previousIndex, T& nextElement, size_t& nextIndex) const;
676
677 /**
678 * Returns one element of this accessor object.
679 * @see ConstAccessor::operator[].
680 */
681 inline const T& operator[](const size_t& index) const;
682
683 /**
684 * Move operator.
685 * @param accessor Accessor to be moved
686 * @return Reference to this accessor
687 */
689
690 protected:
691
692 /// The elements of this accessor.
693 const T* elements_ = nullptr;
694
695 /// The number of elements that can be accessed.
696 size_t size_ = 0;
697};
698
699/**
700 * This class implements an accessor providing direct access to an array of elements.
701 * An instance of this accessor does not copy the elements, thus the caller has to ensure that the actual elements exist as long as the instance of the accessor exists.<br>
702 * @tparam T The data type of the elements of the accessor
703 * @ingroup base
704 */
705template <typename T>
707{
708 public:
709
710 /**
711 * Creates a new empty accessor.
712 */
714
715 /**
716 * Move constructor.
717 * @param accessor Accessor to be moved
718 */
719 inline NonconstArrayAccessor(NonconstArrayAccessor<T>&& accessor) noexcept;
720
721 /**
722 * Creates a new accessor object.
723 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.
724 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
725 * @param size The number of elements that can be accessed, may be 0 if elements is nullptr
726 */
727 inline NonconstArrayAccessor(T* elements, const size_t size);
728
729 /**
730 * Creates a new accessor object.
731 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.
732 * @param elements A vector holding all elements
733 */
734 explicit inline NonconstArrayAccessor(std::vector<T>& elements);
735
736 /**
737 * Creates a new accessor object.
738 * This constructor simplifies the creation of an optional NonconstArrayAccessor object by allowing to provide an empty vector object while defining an explicit resize value.<br>
739 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.
740 * @param elements A vector holding all elements
741 * @param resizeSize An explicit resize value that will invoke a resizing of the given vector before the vector is connected with the accessor, with range [1, infinity), 0 to avoid any resizing
742 */
743 inline NonconstArrayAccessor(std::vector<T>& elements, const size_t resizeSize);
744
745 /**
746 * Creates a new accessor object.
747 * This constructor simplifies the creation of an optional NonconstArrayAccessor object.<br>
748 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.
749 * @param elements An optional vector holding all elements, nullptr to create an empty invalid accessor
750 * @param resizeSize An optional possibility to resize the provided elements vector (if defined - otherwise nothing happens), with range [1, infinity), 0 to avoid any resizing
751 */
752 explicit inline NonconstArrayAccessor(std::vector<T>* elements, const size_t resizeSize = 0);
753
754 /**
755 * Returns a pointer to the elements of this accessor if the data exists within one memory block without gaps.
756 * @see NonconstAccessor::data().
757 */
758 virtual T* data();
759
760 /**
761 * Returns the number of accessible elements of this accessor object.
762 * @see ConstAccessor::size().
763 */
764 virtual size_t size() const;
765
766 /**
767 * Returns one element of this accessor object.
768 * @see ConstAccessor::operator[].
769 */
770 virtual const T& operator[](const size_t& index) const;
771
772 /**
773 * Returns one element of this accessor object.
774 * @see ConstAccessor::operator[].
775 */
776 virtual T& operator[](const size_t& index);
777
778 /**
779 * Move operator.
780 * @param accessor Accessor to be moved
781 * @return Reference to this accessor
782 */
784
785 protected:
786
787 /// The elements of this accessor.
788 T* elements_ = nullptr;
789
790 /// The number of elements that can be accessed.
791 size_t size_ = 0;
792};
793
794/**
795 * This class implements an accessor providing direct access to an array of elements.
796 * An instance of this accessor does not copy the elements, thus the caller has to ensure that the actual elements exist as long as the instance of the accessor exists.<br>
797 * This class is not derived from Accessor (or any other Accessor class) to avoid virtual functions ensuring higher element-access performances.<br>
798 * Thus, this class will mainly be applied in template functions where the type of the accessor is defined at compile time.
799 * @tparam T The data type of the elements of the accessor
800 * @see NonconstArrayAccessor, ConstTemplateArrayAccessor.
801 * @ingroup base
802 */
803template <typename T>
805{
806 public:
807
808 /**
809 * Definition of the element type of this accessor.
810 */
811 using Type = T;
812
813 /**
814 * Definition of the key (or e.g., index) type of this accessor.
815 */
817
818 public:
819
820 /**
821 * Creates a new empty accessor.
822 */
824
825 /**
826 * Move constructor.
827 * @param accessor Accessor to be moved
828 */
830
831 /**
832 * Creates a new accessor object.
833 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.
834 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
835 * @param size The number of elements that can be accessed, may be 0 if elements is nullptr
836 */
837 inline NonconstTemplateArrayAccessor(T* elements, const size_t size);
838
839 /**
840 * Creates a new accessor object.
841 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.
842 * @param elements A vector holding all elements
843 */
844 explicit inline NonconstTemplateArrayAccessor(std::vector<T>& elements);
845
846 /**
847 * Creates a new accessor object.
848 * This constructor simplifies the creation of an optional NonconstTemplateArrayAccessor object by allowing to provide an empty vector object while defining an explicit resize value.<br>
849 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.
850 * @param elements A vector holding all elements
851 * @param resizeSize An explicit resize value that will invoke a resizing of the given vector before the vector is connected with the accessor, with range [1, infinity), 0 to avoid any resizing
852 */
853 inline NonconstTemplateArrayAccessor(std::vector<T>& elements, const size_t resizeSize);
854
855 /**
856 * Creates a new accessor object.
857 * This constructor simplifies the creation of an optional NonconstTemplateArrayAccessor object.<br>
858 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.
859 * @param elements An optional vector holding all elements, nullptr to create an empty invalid accessor
860 * @param resizeSize An optional possibility to resize the provided elements vector (if defined - otherwise nothing happens), with range [1, infinity), 0 to avoid any resizing
861 */
862 explicit inline NonconstTemplateArrayAccessor(std::vector<T>* elements, const size_t resizeSize = 0);
863
864 /**
865 * Returns a pointer to the elements of this accessor if the data exists within one memory block without gaps.
866 * @see ConstAccessor::data().
867 */
868 inline const T* data() const;
869
870 /**
871 * Returns a pointer to the elements of this accessor if the data exists within one memory block without gaps.
872 * @see NonconstAccessor::data().
873 */
874 inline T* data();
875
876 /**
877 * Returns the number of accessible elements of this accessor object.
878 * @see NonconstAccessor::size().
879 */
880 inline size_t size() const;
881
882 /**
883 * Returns whether this accessor provides no elements.
884 * @see NonconstAccessor::isEmpty().
885 */
886 inline bool isEmpty() const;
887
888 /**
889 * Returns whether this accessor has a specific element.
890 * @see NonconstAccessor::canAccess().
891 */
892 inline bool canAccess(const size_t& index) const;
893
894 /**
895 * Returns the first element of this accessor.
896 * @see ConstAccessor:firstElement().
897 */
898 inline bool firstElement(T& element, size_t& index) const;
899
900 /**
901 * Returns the next element which follows a given key of the previous element.
902 * @see ConstAccessor::nextElement().
903 */
904 inline bool nextElement(const size_t& previousIndex, T& nextElement, size_t& nextIndex) const;
905
906 /**
907 * Returns one element of this accessor object.
908 * @see ConstAccessor::operator[].
909 */
910 inline const T& operator[](const size_t& index) const;
911
912 /**
913 * Returns one element of this accessor object.
914 * @see NonconstAccessor::operator[].
915 */
916 inline T& operator[](const size_t& index);
917
918 /**
919 * Move operator.
920 * @param accessor Accessor to be moved
921 * @return Reference to this accessor
922 */
924
925 protected:
926
927 /// The elements of this accessor.
928 T* elements_ = nullptr;
929
930 /// The number of elements that can be accessed.
931 size_t size_ = 0;
932};
933
934/**
935 * This class implements an accessor providing direct access to a constant array of elements while all elements are identical (all elements point to one unique object).
936 * This accessor can be used to simulate a large array with several identical entries.
937 * @see ConstArrayAccessor.
938 * @ingroup base
939 */
940template <typename T>
942{
943 public:
944
945 /**
946 * Creates a new empty accessor.
947 */
949
950 /**
951 * Move constructor.
952 * @param accessor Accessor to be moved
953 */
954 inline ConstElementAccessor(ConstElementAccessor<T>&& accessor) noexcept;
955
956 /**
957 * Creates a new accessor object.
958 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.
959 * @param size The number of elements that can be accessed, with range [1, infinity)
960 * @param element The element that can be accessed though any index
961 */
962 inline ConstElementAccessor(const size_t size, const T& element);
963
964 /**
965 * Returns the number of accessible elements of this accessor object.
966 * @see ConstAccessor::size().
967 */
968 virtual size_t size() const;
969
970 /**
971 * Returns one element of this accessor object.
972 * @see ConstAccessor::operator[].
973 */
974 virtual const T& operator[](const size_t& index) const;
975
976 /**
977 * Move operator.
978 * @param accessor Accessor to be moved
979 * @return Reference to this accessor
980 */
981 inline ConstElementAccessor<T>& operator=(ConstElementAccessor<T>&& accessor) noexcept;
982
983 protected:
984
985 /// The element of this accessor.
986 const T* element_ = nullptr;
987
988 /// The number of elements that can be accessed.
989 size_t size_ = 0;
990};
991
992/**
993 * This class implements an accessor providing direct access to a constant (unordered) map of elements.
994 * An instance of this accessor does not copy the elements, thus the caller has to ensure that the actual elements exist as long as the instance of the accessor exists.<br>
995 * @tparam T The data type of the elements of the accessor
996 * @tparam TKey The data type of the keys of the accessor
997 * @ingroup base
998 */
999template <typename T, typename TKey>
1000class ConstMapAccessor : public ConstAccessor<T, TKey>
1001{
1002 public:
1003
1004 /**
1005 * Creates a new empty accessor.
1006 */
1007 ConstMapAccessor() = default;
1008
1009 /**
1010 * Move constructor.
1011 * @param accessor Accessor to be moved
1012 */
1013 inline ConstMapAccessor(ConstMapAccessor<T, TKey>&& accessor) noexcept;
1014
1015 /**
1016 * Creates a new accessor object.
1017 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
1018 * @param elements A map holding all elements
1019 */
1020 explicit inline ConstMapAccessor(const std::unordered_map<TKey, T>& elements);
1021
1022 /**
1023 * Returns the number of accessible elements of this accessor object.
1024 * @see ConstAccessor::size().
1025 */
1026 virtual size_t size() const;
1027
1028 /**
1029 * Returns whether this accessor has a specific element.
1030 * @see ConstAccessor::canAccess().
1031 */
1032 virtual bool canAccess(const TKey& key) const;
1033
1034 /**
1035 * Returns the first element of this accessor.
1036 * @see ConstAccessor::firstElement().
1037 */
1038 virtual bool firstElement(T& element, TKey& key) const;
1039
1040 /**
1041 * Returns the next element which follows a given key of the previous element.
1042 * @see ConstAccessor::nextElement().
1043 */
1044 virtual bool nextElement(const TKey& previousKey, T& nextElement, TKey& nextKey) const;
1045
1046 /**
1047 * Returns one element of this accessor object.
1048 * @see ConstAccessor::operator[].
1049 */
1050 virtual const T& operator[](const TKey& key) const;
1051
1052 /**
1053 * Move operator.
1054 * @param accessor Accessor to be moved
1055 * @return Reference to this accessor
1056 */
1058
1059 protected:
1060
1061 /// The elements of this accessor.
1062 const std::unordered_map<TKey, T>* elementMap_ = nullptr;
1063};
1064
1065/**
1066 * This class implements an accessor providing direct access to an (unordered) map of elements.
1067 * An instance of this accessor does not copy the elements, thus the caller has to ensure that the actual elements exist as long as the instance of the accessor exists.<br>
1068 * @tparam T The data type of the elements of the accessor
1069 * @tparam TKey The data type of the keys of the accessor
1070 * @ingroup base
1071 */
1072template <typename T, typename TKey>
1074{
1075 public:
1076
1077 /**
1078 * Creates a new empty accessor.
1079 */
1081
1082 /**
1083 * Creates a new accessor object.
1084 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
1085 * @param elements A map holding all elements
1086 */
1087 explicit inline NonconstMapAccessor(std::unordered_map<TKey, T>& elements);
1088
1089 /**
1090 * Returns the number of accessible elements of this accessor object.
1091 * @see ConstAccessor::size().
1092 */
1093 virtual size_t size() const;
1094
1095 /**
1096 * Returns whether this accessor has a specific element.
1097 * @see ConstAccessor::canAccess().
1098 */
1099 virtual bool canAccess(const TKey& key) const;
1100
1101 /**
1102 * Returns the first element of this accessor.
1103 * @see ConstAccessor::firstElement().
1104 */
1105 virtual bool firstElement(T& element, TKey& key) const;
1106
1107 /**
1108 * Returns the next element which follows a given key of the previous element.
1109 * @see ConstAccessor::nextElement().
1110 */
1111 virtual bool nextElement(const TKey& previousKey, T& nextElement, TKey& nextKey) const;
1112
1113 /**
1114 * Returns one element of this accessor object.
1115 * @see ConstAccessor::operator[].
1116 */
1117 virtual const T& operator[](const TKey& key) const;
1118
1119 /**
1120 * Returns one element of this accessor object.
1121 * @see ConstAccessor::operator[].
1122 */
1123 virtual T& operator[](const TKey& key);
1124
1125 protected:
1126
1127 /// The elements of this accessor.
1128 std::unordered_map<TKey, T>* elementMap_ = nullptr;
1129};
1130
1131/**
1132 * This class implements an indexed-based constant accessor providing access to a subset of elements stored in an array.
1133 * The subset is defined by a set of indices defining also the order of the accessible elements.<br>
1134 * An instance of this accessor does not copy the elements or the subset indices, thus the caller has to ensure that the actual elements exist as long as the instance of the accessor exists.<br>
1135 * @tparam T The data type of the elements of the accessor
1136 * @tparam TIndex The data type of the indices defining the subset of elements
1137 * @ingroup base
1138 */
1139template <typename T, typename TIndex>
1141{
1142 public:
1143
1144 /**
1145 * Definition of the data type of the indices.
1146 */
1147 using IndexType = TIndex;
1148
1149 public:
1150
1151 /**
1152 * Creates a new empty accessor.
1153 */
1155
1156 /**
1157 * Move constructor.
1158 * @param accessor Accessor to be moved
1159 */
1161
1162 /**
1163 * Creates a new accessor object.
1164 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
1165 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
1166 * @param subsetIndices The indices that define a subset of the elements to be accessed, the maximal index must be smaller than the number of given elements
1167 * @param subsetSize The number of indices (not the number of elements) that define the subset, may be 0 if indices is nullptr
1168 */
1169 inline ConstArraySubsetAccessor(const T* elements, const TIndex* subsetIndices, const size_t subsetSize);
1170
1171 /**
1172 * Creates a new accessor object.
1173 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
1174 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
1175 * @param subsetIndices The indices that define a subset of the elements to be accessed, the maximal index must be smaller than the number of given elements
1176 */
1177 inline ConstArraySubsetAccessor(const T* elements, const std::vector<TIndex>& subsetIndices);
1178
1179 /**
1180 * Creates a new accessor object.
1181 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
1182 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
1183 * @param subsetIndices The indices that define a subset of the elements to be accessed, the maximal index must be smaller than the number of given elements
1184 */
1185 inline ConstArraySubsetAccessor(const std::vector<T>& elements, const std::vector<TIndex>& subsetIndices);
1186
1187 /**
1188 * Returns the number of accessible elements of this accessor object.
1189 * @see ConstAccessor::size().
1190 */
1191 virtual size_t size() const;
1192
1193 /**
1194 * Returns one element of this accessor object.
1195 * @see ConstAccessor::operator[].
1196 */
1197 virtual const T& operator[](const size_t& index) const;
1198
1199 /**
1200 * Move operator.
1201 * @param accessor Accessor to be moved
1202 * @return Reference to this object
1203 */
1205
1206 protected:
1207
1208 /// The elements of this accessor.
1209 const T* elements_ = nullptr;
1210
1211 /// The subset indices of this accessor.
1212 const TIndex* subsetIndices_ = nullptr;
1213
1214 /// The number of elements that can be accessed.
1215 size_t subsetSize_ = 0;
1216};
1217
1218/**
1219 * This class implements an indexed-based constant accessor providing access to a subset of elements stored in an array.
1220 * The subset is defined by a set of indices defining also the order of the accessible elements.<br>
1221 * An instance of this accessor does not copy the elements or the subset indices, thus the caller has to ensure that the actual elements exist as long as the instance of the accessor exists.<br>
1222 * This class is not derived from Accessor (or any other Accessor class) to avoid virtual functions ensuring higher element-access performances.<br>
1223 * Thus, this class will mainly be applied in template functions where the type of the accessor is defined at compile time.
1224 * @tparam T The data type of the elements of the accessor
1225 * @tparam TIndex The data type of the indices defining the subset of elements
1226 * @see ConstTemplateArrayAccessor.
1227 * @ingroup base
1228 */
1229template <typename T, typename TIndex>
1231{
1232 public:
1233
1234 /**
1235 * Definition of the element type of this accessor.
1236 */
1237 using Type = T;
1238
1239 /**
1240 * Definition of the key (or e.g., index) type of this accessor.
1241 */
1243
1244 /**
1245 * Definition of the data type of the indices.
1246 */
1247 using IndexType = TIndex;
1248
1249 public:
1250
1251 /**
1252 * Creates a new empty accessor.
1253 */
1255
1256 /**
1257 * Move constructor.
1258 * @param accessor Accessor to be moved
1259 */
1261
1262 /**
1263 * Creates a new accessor object.
1264 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
1265 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
1266 * @param subsetIndices The indices that define a subset of the elements to be accessed, the maximal index must be smaller than the number of given elements
1267 * @param subsetSize The number of indices (not the number of elements) that define the subset, may be 0 if indices is nullptr
1268 */
1269 inline ConstTemplateArraySubsetAccessor(const T* elements, const TIndex* subsetIndices, const size_t subsetSize);
1270
1271 /**
1272 * Creates a new accessor object.
1273 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
1274 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
1275 * @param subsetIndices The indices that define a subset of the elements to be accessed, the maximal index must be smaller than the number of given elements
1276 */
1277 inline ConstTemplateArraySubsetAccessor(const T* elements, const std::vector<TIndex>& subsetIndices);
1278
1279 /**
1280 * Creates a new accessor object.
1281 * Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.<br>
1282 * @param elements The elements that can be accessed, may be nullptr if size is equal 0
1283 * @param subsetIndices The indices that define a subset of the elements to be accessed, the maximal index must be smaller than the number of given elements
1284 */
1285 inline ConstTemplateArraySubsetAccessor(const std::vector<T>& elements, const std::vector<TIndex>& subsetIndices);
1286
1287 /**
1288 * Returns a pointer to the elements of this accessor if the data exists within one memory block without gaps.
1289 * @see ConstAccessor::data().
1290 */
1291 inline const T* data() const;
1292
1293 /**
1294 * Returns the number of accessible elements of this accessor object.
1295 * @see ConstAccessor::size().
1296 */
1297 inline size_t size() const;
1298
1299 /**
1300 * Returns whether this accessor provides no elements.
1301 * @return True, if so
1302 */
1303 inline bool isEmpty() const;
1304
1305 /**
1306 * Returns whether this accessor has a specific element.
1307 * @see ConstAccessor::canAccess().
1308 */
1309 inline bool canAccess(const size_t& index) const;
1310
1311 /**
1312 * Returns the first element of this accessor.
1313 * @see ConstAccessor:firstElement().
1314 */
1315 inline bool firstElement(T& element, size_t& index) const;
1316
1317 /**
1318 * Returns the next element which follows a given key of the previous element.
1319 * @see ConstAccessor::nextElement().
1320 */
1321 inline bool nextElement(const size_t& previousIndex, T& nextElement, size_t& nextIndex) const;
1322
1323 /**
1324 * Returns one element of this accessor object.
1325 * @see ConstAccessor::operator[].
1326 */
1327 inline const T& operator[](const size_t& index) const;
1328
1329 /**
1330 * Move operator.
1331 * @param accessor Accessor to be moved
1332 * @return Reference to this object
1333 */
1335
1336 protected:
1337
1338 /// The elements of this accessor.
1339 const T* elements_ = nullptr;
1340
1341 /// The subset indices of this accessor.
1342 const TIndex* subsetIndices_ = nullptr;
1343
1344 /// The number of elements that can be accessed.
1345 size_t subsetSize_ = 0;
1346};
1347
1348/**
1349 * This class implements an indexed-based constant accessor providing access to a subset of elements stored in a specified indexed-based child accessor.
1350 * @tparam T The data type of the elements of the accessor
1351 * @tparam TIndex The data type of the indices defining the subset of elements
1352 * @ingroup base
1353 */
1354template <typename T, typename TIndex>
1356{
1357 public:
1358
1359 /**
1360 * Creates a new accessor which uses an accessor as base.
1361 * @param child The child accessor
1362 * @param subsetIndices The indices that define a subset of the elements to be accessed, the maximal index must be smaller than the number of elements in the child accessor
1363 * @param subsetSize The number of indices (not the number of elements) that define the subset, may be 0 if indices is nullptr
1364 */
1365 inline ConstIndexedAccessorSubsetAccessor(const ConstIndexedAccessor<T>& child, const TIndex* subsetIndices, const size_t subsetSize);
1366
1367 /**
1368 * Creates a new accessor which uses an accessor as base.
1369 * @param child The child accessor
1370 * @param subsetIndices The indices that define a subset of the elements to be accessed, the maximal index must be smaller than the number of elements in the child accessor
1371 */
1372 inline ConstIndexedAccessorSubsetAccessor(const ConstIndexedAccessor<T>& child, const std::vector<TIndex>& subsetIndices);
1373
1374 /**
1375 * Returns the number of accessible elements of this accessor object.
1376 * @see Accessor::size().
1377 */
1378 virtual size_t size() const;
1379
1380 /**
1381 * Returns one element of this accessor object.
1382 * @param index The index of element to be accessed, with range [0, size())
1383 * @return The requested element
1384 */
1385 virtual const T& operator[](const size_t& index) const;
1386
1387 protected:
1388
1389 // The child accessor of this accessor.
1391
1392 /// The subset indices of this accessor.
1393 const TIndex* subsetIndices_ = nullptr;
1394
1395 /// The number of elements that can be accessed.
1396 size_t subsetSize_ = 0;
1397};
1398
1399/**
1400 * This class implements an accessor providing access to a elements by using a callback function.
1401 * @tparam T The data type of the elements of the accessor
1402 * @ingroup base
1403 */
1404template <typename T>
1406{
1407 public:
1408
1409 /**
1410 * Definition of a callback function providing access to individual elements.
1411 * The first parameter is the index of the elements to be accessed
1412 * The return parameter is the constant reference to the requested element
1413 */
1415
1416 public:
1417
1418 /**
1419 * Creates a new empty accessor.
1420 */
1422
1423 /**
1424 * Creates a new accessor object.
1425 * @param callback The callback function providing access to individual elements
1426 * @param size The number of elements of this accessor
1427 */
1428 inline ConstCallbackIndexedAccessor(const CallbackFunction& callback, const size_t size);
1429
1430 /**
1431 * Returns one element of this accessor object.
1432 * @see ConstAccessor::operator[].
1433 */
1434 virtual const T& operator[](const size_t& index) const;
1435
1436 /**
1437 * Returns the number of accessible elements of this accessor object.
1438 * @see ConstAccessor::size().
1439 */
1440 virtual size_t size() const;
1441
1442 protected:
1443
1444 /// The callback function of this accessor.
1446
1447 /// The number of elements that can be accessed.
1448 size_t size_ = 0;
1449};
1450
1451/**
1452 * This class implements an accessor that guarantees memory access to the elements of an indexed accessor object until the scope ends.
1453 * The memory access ensures that the entire data of the indexed accessor objects is provided as one memory block without any gaps.<br>
1454 * Beware: The data of the given indexed accessor may be copied (if necessary) to ensure the functionality.<br>
1455 * Thus, the accessible elements of this object may be clones/copies of the actual provided accessor object.
1456 */
1457template <typename T>
1459{
1460 public:
1461
1462 /**
1463 * Creates a new scoped accessor object by a given indexed accessor object.
1464 * @param accessor The accessor object providing the data for this scoped object, ensure that the provided accessor exists at least until the scope ends
1465 * @tparam TAccessor The data type of the accessor providing the data, must be of type ConstIndexedAccessor<T>, or ConstTemplateArrayAccessor<T, size_t> or ConstTemplateArraySubsetAccessor<T, size_t>
1466 */
1467 template <typename TAccessor>
1468 inline explicit ScopedConstMemoryAccessor(const TAccessor& accessor);
1469
1470 /**
1471 * Returns the pointer to the memory block providing the data of the accessor.
1472 * @return The memory block's pointer, nullptr if the accessor does not provide any data
1473 */
1474 inline const T* data() const;
1475
1476 /**
1477 * Returns the number of elements the accessor provides.
1478 * @return The accessor's number of elements, with range [0, infinity)
1479 */
1480 inline size_t size() const;
1481
1482 /**
1483 * Returns one element of this accessor.
1484 * @param index The index of the element, with range [0, size())
1485 * @return The requested element
1486 */
1487 inline const T& operator[](const size_t index) const;
1488
1489 /**
1490 * Returns whether this object provides access to at least one element of the accessor.
1491 * @return True, if so
1492 */
1493 explicit inline operator bool() const;
1494
1495 protected:
1496
1497 /// The pointer to the memory block of the accessor.
1498 const T* data_ = nullptr;
1499
1500 /// The number of elements the accessor provides.
1501 size_t size_ = 0;
1502
1503 /// The individual elements of the accessor, if necessary.
1504 std::vector<T> intermediateValues_;
1505};
1506
1507/**
1508 * This class implements an accessor that guarantees memory access to the elements of an indexed accessor object until the scope ends.
1509 * The memory access ensures that the entire data of the indexed accessor objects is provided as one memory block without any gaps.<br>
1510 * Beware: The data of the given indexed accessor may be copied (if necessary) to ensure the functionality.<br>
1511 * Thus, the accessible elements of this object may be clones/copies of the actual provided accessor object.
1512 * The connected indexed accessor object will hold the data of this accessor object after this object is disposed.
1513 */
1514template <typename T>
1516{
1517 public:
1518
1519 /**
1520 * Creates a new scoped accessor object by a given indexed accessor object.
1521 * @param accessor The accessor object providing the data for this scoped object, the provided accessor needs to exist at least until the scope ends
1522 */
1524
1525 /**
1526 * Creates a new scoped accessor object by an optional indexed accessor object or creates an internal temporary memory with specified size.
1527 * @param accessor The accessor object providing the data for this scoped object, the provided accessor needs to exist at least until the scope ends, nullptr to create an internal temporary memory without connected accessor
1528 * @param temporarySize The explicit size of the internal memory if no accessor is provided, does not have any meaning if an accessor is provided
1529 */
1530 explicit inline ScopedNonconstMemoryAccessor(NonconstIndexedAccessor<T>* accessor, const size_t temporarySize = 0);
1531
1532 /**
1533 * Destructs the scoped accessor object.
1534 * Further, the intermediate values of this object may be copied back to the accessor object, if necessary.
1535 */
1537
1538 /**
1539 * Returns the pointer to the memory block providing the data of the accessor.
1540 * @return The memory block's pointer, nullptr if the accessor does not provide any data
1541 */
1542 inline T* data();
1543
1544 /**
1545 * Returns the number of elements the accessor provides.
1546 * @return The accessor's number of elements, with range [0, infinity)
1547 */
1548 inline size_t size() const;
1549
1550 /**
1551 * Returns one element of this accessor.
1552 * @param index The index of the element, with range [0, size())
1553 * @return The requested element
1554 */
1555 inline T& operator[](const size_t index);
1556
1557 /**
1558 * Returns whether this object provides access to at least one element of the accessor.
1559 * @return True, if so
1560 */
1561 explicit inline operator bool() const;
1562
1563 protected:
1564
1565 /// The given accessor object providing the data for this object, nullptr if no accessor object was provided during creation.
1567
1568 /// The pointer to the memory block of the accessor.
1569 T* data_ = nullptr;
1570
1571 /// The number of elements the accessor provides.
1572 size_t size_ = 0;
1573
1574 /// The individual elements of the accessor, if necessary.
1575 std::vector<T> intermediateValues_;
1576};
1577
1578inline bool Accessor::isEmpty() const
1579{
1580 return size() == 0;
1581}
1582
1583template <typename TAccessor>
1584std::vector<typename TAccessor::Type> Accessor::accessor2elements(const TAccessor& accessor)
1585{
1586 std::vector<typename TAccessor::Type> result;
1587 result.reserve(accessor.size());
1588
1589 for (size_t n = 0; n < accessor.size(); ++n)
1590 {
1591 result.emplace_back(accessor[n]);
1592 }
1593
1594 return result;
1595}
1596
1597template <typename TAccessor>
1598std::unordered_map<typename TAccessor::KeyType, typename TAccessor::Type> Accessor::accessor2map(const TAccessor& accessor)
1599{
1600 std::unordered_map<typename TAccessor::KeyType, typename TAccessor::Type> result;
1601
1602 typename TAccessor::Type element;
1603 typename TAccessor::KeyType key;
1604
1605 if (accessor.firstElement(element, key))
1606 {
1607 result[key] = element;
1608
1609 typename TAccessor::KeyType nextKey;
1610
1611 while (accessor.nextElement(key, element, nextKey))
1612 {
1613 key = nextKey;
1614 result[key] = element;
1615 }
1616 }
1617
1618 return result;
1619}
1620
1621template <typename TAccessor, typename TIndex>
1622std::vector<typename TAccessor::Type> Accessor::accessor2subsetElements(const TAccessor& accessor, const std::vector<TIndex>& subset)
1623{
1624 std::vector<typename TAccessor::Type> result;
1625 result.reserve(subset.size());
1626
1627 for (size_t n = 0; n < subset.size(); ++n)
1628 {
1629 result.emplace_back(accessor[subset[n]]);
1630 }
1631
1632 return result;
1633}
1634
1635template <typename T, typename TKey>
1637{
1638 return nullptr;
1639}
1640
1641template <typename T, typename TKey>
1643{
1644 return nullptr;
1645}
1646
1647template <typename T>
1648bool ConstIndexedAccessor<T>::canAccess(const size_t& index) const
1649{
1650 return index < this->size();
1651}
1652
1653template <typename T>
1654bool ConstIndexedAccessor<T>::firstElement(T& element, size_t& index) const
1655{
1656 if (this->size() == 0)
1657 {
1658 return false;
1659 }
1660
1661 element = (*this)[0];
1662 index = 0;
1663
1664 return true;
1665}
1666
1667template <typename T>
1668bool ConstIndexedAccessor<T>::nextElement(const size_t& previousIndex, T& element, size_t& nextIndex) const
1669{
1670 if (previousIndex + 1 < this->size())
1671 {
1672 nextIndex = previousIndex + 1;
1673 element = (*this)[nextIndex];
1674
1675 return true;
1676 }
1677
1678 return false;
1679}
1680
1681template <typename T>
1682bool NonconstIndexedAccessor<T>::canAccess(const size_t& index) const
1683{
1684 return index < this->size();
1685}
1686
1687template <typename T>
1688bool NonconstIndexedAccessor<T>::firstElement(T& element, size_t& index) const
1689{
1690 if (this->size() == 0)
1691 {
1692 return false;
1693 }
1694
1695 element = (*this)[0];
1696 index = 0;
1697 return true;
1698}
1699
1700template <typename T>
1701bool NonconstIndexedAccessor<T>::nextElement(const size_t& previousIndex, T& nextElement, size_t& nextIndex) const
1702{
1703 if (previousIndex + 1 < this->size())
1704 {
1705 nextIndex = previousIndex + 1;
1706 nextElement = (*this)[nextIndex];
1707 return true;
1708 }
1709
1710 return false;
1711}
1712
1713template <typename T>
1715{
1716 if (this->isEmpty())
1717 {
1718 return nullptr;
1719 }
1720 else
1721 {
1722 return this;
1723 }
1724}
1725
1726template <typename T>
1727bool TemporaryIndexedAccessor<T>::canAccess(const size_t& index) const
1728{
1729 return index < this->size();
1730}
1731
1732template <typename T, typename TIndex>
1733inline ConstIndexedAccessorSubsetAccessor<T, TIndex>::ConstIndexedAccessorSubsetAccessor(const ConstIndexedAccessor<T>& child, const TIndex* subsetIndices, const size_t subsetSize) :
1734 child_(child),
1735 subsetIndices_(subsetIndices),
1736 subsetSize_(subsetSize)
1737{
1738 // nothing to do here
1739}
1740
1741template <typename T, typename TIndex>
1743 child_(child),
1744 subsetIndices_(subsetIndices.data()),
1745 subsetSize_(subsetIndices.size())
1746{
1747 // nothing to do here
1748}
1749
1750template <typename T, typename TIndex>
1752{
1753 return subsetSize_;
1754}
1755
1756template <typename T, typename TIndex>
1758{
1759 ocean_assert(index < subsetSize_);
1760
1761 return child_[subsetIndices_[index]];
1762}
1763
1764template <typename T>
1766{
1767 *this = std::move(accessor);
1768}
1769
1770template <typename T>
1771inline ConstArrayAccessor<T>::ConstArrayAccessor(const T* elements, const size_t size) :
1772 elements_(elements),
1773 size_(size)
1774{
1775 // nothing to do here
1776}
1777
1778template <typename T>
1779inline ConstArrayAccessor<T>::ConstArrayAccessor(const std::vector<T>& elements) :
1780 elements_(elements.data()),
1781 size_(elements.size())
1782{
1783 // nothing to do here
1784}
1785
1786template <typename T>
1788{
1789 return elements_;
1790}
1791
1792template <typename T>
1794{
1795 return size_;
1796}
1797
1798template <typename T>
1799const T& ConstArrayAccessor<T>::operator[](const size_t& index) const
1800{
1801 ocean_assert(index < size_);
1802 ocean_assert(elements_ != nullptr);
1803
1804 return elements_[index];
1805}
1806
1807template <typename T>
1809{
1810 if (this != &accessor)
1811 {
1812 elements_ = accessor.elements_;
1813 size_ = accessor.size_;
1814
1815 accessor.elements_ = nullptr;
1816 accessor.size_ = 0;
1817 }
1818
1819 return *this;
1820}
1821
1822template <typename T>
1824 elements_(size)
1825{
1826 for (size_t n = 0; n < size; ++n)
1827 {
1828 elements_[n] = elements[n].get();
1829 }
1830}
1831
1832template <typename T>
1834 elements_(elements.size())
1835{
1836 for (size_t n = 0; n < elements.size(); ++n)
1837 {
1838 elements_[n] = elements[n].get();
1839 }
1840}
1841
1842template <typename T>
1844{
1845 return elements_.size();
1846}
1847
1848template <typename T>
1849const T* const & SharedPointerConstArrayAccessor<T>::operator[](const size_t& index) const
1850{
1851 ocean_assert(index < elements_.size());
1852
1853 return elements_[index];
1854}
1855
1856template <typename T>
1858{
1859 *this = std::move(accessor);
1860}
1861
1862template <typename T>
1863inline ConstTemplateArrayAccessor<T>::ConstTemplateArrayAccessor(const T* elements, const size_t size) :
1864 elements_(elements),
1865 size_(size)
1866{
1867 // nothing to do here
1868}
1869
1870template <typename T>
1872 elements_(elements.data()),
1873 size_(elements.size())
1874{
1875 // nothing to do here
1876}
1877
1878template <typename T>
1880{
1881 return elements_;
1882}
1883
1884template <typename T>
1886{
1887 return size_;
1888}
1889
1890template <typename T>
1892{
1893 return size_ == 0;
1894}
1895
1896template <typename T>
1897inline bool ConstTemplateArrayAccessor<T>::canAccess(const size_t& index) const
1898{
1899 return index < size_;
1900}
1901
1902template <typename T>
1903inline bool ConstTemplateArrayAccessor<T>::firstElement(T& element, size_t& index) const
1904{
1905 if (size_ == 0)
1906 {
1907 return false;
1908 }
1909
1910 element = elements_[0];
1911 index = 0;
1912 return true;
1913}
1914
1915template <typename T>
1916inline bool ConstTemplateArrayAccessor<T>::nextElement(const size_t& previousIndex, T& nextElement, size_t& nextIndex) const
1917{
1918 if (previousIndex + 1 >= size_)
1919 {
1920 return false;
1921 }
1922
1923 nextIndex = previousIndex + 1;
1924 nextElement = elements_[nextIndex];
1925 return true;
1926}
1927
1928template <typename T>
1929inline const T& ConstTemplateArrayAccessor<T>::operator[](const size_t& index) const
1930{
1931 ocean_assert(index < size_);
1932 return elements_[index];
1933}
1934
1935template <typename T>
1937{
1938 if (this != &accessor)
1939 {
1940 elements_ = accessor.elements_;
1941 size_ = accessor.size_;
1942
1943 accessor.elements_ = nullptr;
1944 accessor.size_ = 0;
1945 }
1946
1947 return *this;
1948}
1949
1950template <typename T>
1952{
1953 *this = std::move(accessor);
1954}
1955
1956template <typename T>
1957inline NonconstArrayAccessor<T>::NonconstArrayAccessor(T* elements, const size_t size) :
1958 elements_(elements),
1959 size_(size)
1960{
1961 // nothing to do here
1962}
1963
1964template <typename T>
1965inline NonconstArrayAccessor<T>::NonconstArrayAccessor(std::vector<T>& elements) :
1966 elements_(elements.data()),
1967 size_(elements.size())
1968{
1969 // nothing to do here
1970}
1971
1972template <typename T>
1973inline NonconstArrayAccessor<T>::NonconstArrayAccessor(std::vector<T>& elements, const size_t resizeSize) :
1974 elements_(nullptr),
1975 size_(0)
1976{
1977 if (resizeSize != 0)
1978 {
1979 elements.resize(resizeSize);
1980 }
1981
1982 elements_ = elements.data();
1983 size_ = elements.size();
1984}
1985
1986template <typename T>
1987inline NonconstArrayAccessor<T>::NonconstArrayAccessor(std::vector<T>* elements, const size_t resizeSize) :
1988 elements_(nullptr),
1989 size_(0)
1990{
1991 if (elements)
1992 {
1993 if (resizeSize != 0)
1994 {
1995 elements->resize(resizeSize);
1996 }
1997
1998 elements_ = elements->data();
1999 size_ = elements->size();
2000 }
2001}
2002
2003template <typename T>
2004const T& NonconstArrayAccessor<T>::operator[](const size_t& index) const
2005{
2006 ocean_assert(index < size_);
2007 ocean_assert(elements_);
2008
2009 return elements_[index];
2010}
2011
2012template <typename T>
2014{
2015 ocean_assert(index < size_);
2016 ocean_assert(elements_);
2017
2018 return elements_[index];
2019}
2020
2021template <typename T>
2023{
2024 return elements_;
2025}
2026
2027template <typename T>
2029{
2030 return size_;
2031}
2032
2033template <typename T>
2035{
2036 if (this != &accessor)
2037 {
2038 elements_ = accessor.elements_;
2039 size_ = accessor.size_;
2040
2041 accessor.elements_ = nullptr;
2042 accessor.size_ = 0;
2043 }
2044
2045 return *this;
2046}
2047
2048template <typename T>
2050{
2051 *this = std::move(accessor);
2052}
2053
2054template <typename T>
2056 elements_(elements),
2057 size_(size)
2058{
2059 // nothing to do here
2060}
2061
2062template <typename T>
2064 elements_(elements.data()),
2065 size_(elements.size())
2066{
2067 // nothing to do here
2068}
2069
2070template <typename T>
2071inline NonconstTemplateArrayAccessor<T>::NonconstTemplateArrayAccessor(std::vector<T>& elements, const size_t resizeSize) :
2072 elements_(nullptr),
2073 size_(0)
2074{
2075 if (resizeSize != 0)
2076 {
2077 elements.resize(resizeSize);
2078 }
2079
2080 elements_ = elements.data();
2081 size_ = elements.size();
2082}
2083
2084template <typename T>
2085inline NonconstTemplateArrayAccessor<T>::NonconstTemplateArrayAccessor(std::vector<T>* elements, const size_t resizeSize) :
2086 elements_(nullptr),
2087 size_(0)
2088{
2089 if (elements)
2090 {
2091 if (resizeSize != 0)
2092 {
2093 elements->resize(resizeSize);
2094 }
2095
2096 elements_ = elements->data();
2097 size_ = elements->size();
2098 }
2099}
2100
2101template <typename T>
2103{
2104 return elements_;
2105}
2106
2107template <typename T>
2109{
2110 return elements_;
2111}
2112
2113template <typename T>
2115{
2116 return size_;
2117}
2118
2119template <typename T>
2121{
2122 return size_ == 0;
2123}
2124
2125template <typename T>
2126inline bool NonconstTemplateArrayAccessor<T>::canAccess(const size_t& index) const
2127{
2128 return index < size_;
2129}
2130
2131template <typename T>
2132inline bool NonconstTemplateArrayAccessor<T>::firstElement(T& element, size_t& index) const
2133{
2134 if (size_ == 0)
2135 {
2136 return false;
2137 }
2138
2139 element = elements_[0];
2140 index = 0;
2141 return true;
2142}
2143
2144template <typename T>
2145inline bool NonconstTemplateArrayAccessor<T>::nextElement(const size_t& previousIndex, T& nextElement, size_t& nextIndex) const
2146{
2147 if (previousIndex + 1 >= size_)
2148 {
2149 return false;
2150 }
2151
2152 nextIndex = previousIndex + 1;
2153 nextElement = elements_[nextIndex];
2154 return true;
2155}
2156
2157template <typename T>
2158inline const T& NonconstTemplateArrayAccessor<T>::operator[](const size_t& index) const
2159{
2160 ocean_assert(index < size_);
2161 return elements_[index];
2162}
2163
2164template <typename T>
2166{
2167 ocean_assert(index < size_);
2168 return elements_[index];
2169}
2170
2171template <typename T>
2173{
2174 if (this != &accessor)
2175 {
2176 elements_ = accessor.elements_;
2177 size_ = accessor.size_;
2178
2179 accessor.elements_ = nullptr;
2180 accessor.size_ = 0;
2181 }
2182
2183 return *this;
2184}
2185
2186template <typename T>
2188{
2189 *this = std::move(accessor);
2190}
2191
2192template <typename T>
2193inline ConstElementAccessor<T>::ConstElementAccessor(const size_t size, const T& element) :
2194 element_(&element),
2195 size_(size)
2196{
2197 ocean_assert(size_ != 0);
2198 ocean_assert(element_ != nullptr);
2199}
2200
2201template <typename T>
2203{
2204 return size_;
2205}
2206
2207template <typename T>
2208const T& ConstElementAccessor<T>::operator[](const size_t& index) const
2209{
2210 ocean_assert_and_suppress_unused(index < size_, index);
2211 ocean_assert(element_ != nullptr);
2212
2213 return *element_;
2214}
2215
2216template <typename T>
2218{
2219 if (this != &accessor)
2220 {
2221 element_ = accessor.element_;
2222 size_ = accessor.size_;
2223
2224 accessor.element_ = nullptr;
2225 accessor.size_ = 0;
2226 }
2227
2228 return *this;
2229}
2230
2231template <typename T, typename TKey>
2233{
2234 *this = std::move(accessor);
2235}
2236
2237template <typename T, typename TKey>
2238inline ConstMapAccessor<T, TKey>::ConstMapAccessor(const std::unordered_map<TKey, T>& elements) :
2239 elementMap_(&elements)
2240{
2241 // nothing to do here
2242}
2243
2244template <typename T, typename TKey>
2246{
2247 return elementMap_ ? elementMap_->size() : 0;
2248}
2249
2250template <typename T, typename TKey>
2251bool ConstMapAccessor<T, TKey>::canAccess(const TKey& key) const
2252{
2253 if (elementMap_ == nullptr)
2254 {
2255 return false;
2256 }
2257
2258 return elementMap_->find(key) != elementMap_->end();
2259}
2260
2261template <typename T, typename TKey>
2262bool ConstMapAccessor<T, TKey>::firstElement(T& element, TKey& key) const
2263{
2264 if (elementMap_ == nullptr || elementMap_->empty())
2265 {
2266 return false;
2267 }
2268
2269 key = elementMap_->begin()->first;
2270 element = elementMap_->begin()->second;
2271
2272 return true;
2273}
2274
2275template <typename T, typename TKey>
2276bool ConstMapAccessor<T, TKey>::nextElement(const TKey& previousKey, T& element, TKey& nextKey) const
2277{
2278 if (elementMap_ == nullptr)
2279 {
2280 return false;
2281 }
2282
2283 typename std::unordered_map<TKey, T>::const_iterator i = elementMap_->find(previousKey);
2284 if (i == elementMap_->cend() || (++i) == elementMap_->cend())
2285 {
2286 return false;
2287 }
2288
2289 nextKey = i->first;
2290 element = i->second;
2291
2292 return true;
2293}
2294
2295template <typename T, typename TKey>
2296const T& ConstMapAccessor<T, TKey>::operator[](const TKey& key) const
2297{
2298 ocean_assert(elementMap_);
2299 ocean_assert(elementMap_->find(key) != elementMap_->end());
2300
2301 return elementMap_->find(key)->second;
2302}
2303
2304template <typename T, typename TKey>
2306{
2307 if (this != &accessor)
2308 {
2309 elementMap_ = accessor.elementMap_;
2310 accessor.elementMap_ = nullptr;
2311 }
2312
2313 return *this;
2314}
2315
2316template <typename T, typename TKey>
2317inline NonconstMapAccessor<T, TKey>::NonconstMapAccessor(std::unordered_map<TKey, T>& elements) :
2318 elementMap_(&elements)
2319{
2320 // nothing to do here
2321}
2322
2323template <typename T, typename TKey>
2325{
2326 if (elementMap_ == nullptr)
2327 {
2328 return false;
2329 }
2330
2331 return elementMap_->find(key) != elementMap_->end();
2332}
2333
2334template <typename T, typename TKey>
2335bool NonconstMapAccessor<T, TKey>::firstElement(T& element, TKey& key) const
2336{
2337 if (elementMap_ == nullptr || elementMap_->empty())
2338 {
2339 return false;
2340 }
2341
2342 key = elementMap_->begin()->first;
2343 element = elementMap_->begin()->second;
2344
2345 return true;
2346}
2347
2348template <typename T, typename TKey>
2349bool NonconstMapAccessor<T, TKey>::nextElement(const TKey& previousKey, T& element, TKey& nextKey) const
2350{
2351 if (elementMap_ == nullptr)
2352 {
2353 return false;
2354 }
2355
2356 typename std::unordered_map<TKey, T>::const_iterator i = elementMap_->find(previousKey);
2357 if (i == elementMap_->cend() || (++i) == elementMap_->cend())
2358 {
2359 return false;
2360 }
2361
2362 nextKey = i->first;
2363 element = i->second;
2364
2365 return true;
2366}
2367
2368template <typename T, typename TKey>
2370{
2371 return elementMap_ ? elementMap_->size() : 0;
2372}
2373
2374template <typename T, typename TKey>
2375const T& NonconstMapAccessor<T, TKey>::operator[](const TKey& key) const
2376{
2377 ocean_assert(elementMap_);
2378 ocean_assert(elementMap_->find(key) != elementMap_->end());
2379
2380 return elementMap_->find(key)->second;
2381}
2382
2383template <typename T, typename TKey>
2385{
2386 ocean_assert(elementMap_);
2387 ocean_assert(elementMap_->find(key) != elementMap_->end());
2388
2389 return elementMap_->find(key)->second;
2390}
2391
2392template <typename T, typename TIndex>
2394{
2395 *this = std::move(accessor);
2396}
2397
2398template <typename T, typename TIndex>
2399inline ConstArraySubsetAccessor<T, TIndex>::ConstArraySubsetAccessor(const T* elements, const TIndex* subsetIndices, const size_t subsetSize) :
2400 elements_(elements),
2401 subsetIndices_(subsetIndices),
2402 subsetSize_(subsetSize)
2403{
2404 // nothing to do here
2405}
2406
2407template <typename T, typename TIndex>
2408inline ConstArraySubsetAccessor<T, TIndex>::ConstArraySubsetAccessor(const T* elements, const std::vector<TIndex>& subsetIndices) :
2409 elements_(elements),
2410 subsetIndices_(subsetIndices.data()),
2411 subsetSize_(subsetIndices.size())
2412{
2413 // nothing to do here
2414}
2415
2416template <typename T, typename TIndex>
2417inline ConstArraySubsetAccessor<T, TIndex>::ConstArraySubsetAccessor(const std::vector<T>& elements, const std::vector<TIndex>& subsetIndices) :
2418 elements_(elements.data()),
2419 subsetIndices_(subsetIndices.data()),
2420 subsetSize_(subsetIndices.size())
2421{
2422#ifdef OCEAN_DEBUG
2423 ocean_assert(subsetIndices.size() <= elements.size());
2424 for (size_t n = 0; n < subsetIndices.size(); ++n)
2425 {
2426 ocean_assert(subsetIndices[n] < elements.size());
2427 }
2428#endif
2429}
2430
2431template <typename T, typename TIndex>
2433{
2434 return subsetSize_;
2435}
2436
2437template <typename T, typename TIndex>
2438const T& ConstArraySubsetAccessor<T, TIndex>::operator[](const size_t& index) const
2439{
2440 ocean_assert(index < subsetSize_);
2441 ocean_assert(elements_ && subsetIndices_);
2442
2443 return elements_[subsetIndices_[index]];
2444}
2445
2446template <typename T, typename TIndex>
2448{
2449 if (this != &accessor)
2450 {
2451 elements_ = accessor.elements_;
2452 subsetIndices_ = accessor.subsetIndices_;
2453 subsetSize_ = accessor.subsetSize_;
2454
2455 accessor.elements_ = nullptr;
2456 accessor.subsetIndices_ = nullptr;
2457 accessor.subsetSize_ = 0;
2458 }
2459
2460 return *this;
2461}
2462
2463template <typename T, typename TIndex>
2468
2469template <typename T, typename TIndex>
2470inline ConstTemplateArraySubsetAccessor<T, TIndex>::ConstTemplateArraySubsetAccessor(const T* elements, const TIndex* subsetIndices, const size_t subsetSize) :
2471 elements_(elements),
2472 subsetIndices_(subsetIndices),
2473 subsetSize_(subsetSize)
2474{
2475 ocean_assert(subsetSize_ == 0 || (elements_ != nullptr && subsetIndices_ != nullptr));
2476}
2477
2478template <typename T, typename TIndex>
2479inline ConstTemplateArraySubsetAccessor<T, TIndex>::ConstTemplateArraySubsetAccessor(const T* elements, const std::vector<TIndex>& subsetIndices) :
2480 elements_(elements),
2481 subsetIndices_(subsetIndices.data()),
2482 subsetSize_(subsetIndices.size())
2483{
2484 ocean_assert(subsetSize_ == 0 || (elements_ != nullptr && subsetIndices_ != nullptr));
2485}
2486
2487template <typename T, typename TIndex>
2488inline ConstTemplateArraySubsetAccessor<T, TIndex>::ConstTemplateArraySubsetAccessor(const std::vector<T>& elements, const std::vector<TIndex>& subsetIndices) :
2489 elements_(elements.data()),
2490 subsetIndices_(subsetIndices.data()),
2491 subsetSize_(subsetIndices.size())
2492{
2493#ifdef OCEAN_DEBUG
2494 ocean_assert(subsetIndices.size() <= elements.size());
2495 for (size_t n = 0; n < subsetIndices.size(); ++n)
2496 {
2497 ocean_assert(subsetIndices[n] < elements.size());
2498 }
2499#endif
2500}
2501
2502template <typename T, typename TIndex>
2504{
2505 return nullptr;
2506}
2507
2508template <typename T, typename TIndex>
2510{
2511 return subsetSize_;
2512}
2513
2514template <typename T, typename TIndex>
2516{
2517 return subsetSize_ == 0;
2518}
2519
2520template <typename T, typename TIndex>
2522{
2523 return index < subsetSize_;
2524}
2525
2526template <typename T, typename TIndex>
2527inline bool ConstTemplateArraySubsetAccessor<T, TIndex>::firstElement(T& element, size_t& index) const
2528{
2529 if (subsetSize_ == 0)
2530 {
2531 return false;
2532 }
2533
2534 element = (*this)[0];
2535 index = 0;
2536
2537 return true;
2538}
2539
2540template <typename T, typename TIndex>
2541inline bool ConstTemplateArraySubsetAccessor<T, TIndex>::nextElement(const size_t& previousIndex, T& nextElement, size_t& nextIndex) const
2542{
2543 if (previousIndex + 1 >= subsetSize_)
2544 {
2545 return false;
2546 }
2547
2548 nextIndex = previousIndex + 1;
2549 nextElement = (*this)[nextIndex];
2550
2551 return true;
2552}
2553
2554template <typename T, typename TIndex>
2555inline const T& ConstTemplateArraySubsetAccessor<T, TIndex>::operator[](const size_t& index) const
2556{
2557 ocean_assert(index < subsetSize_);
2558
2559 return elements_[subsetIndices_[index]];
2560}
2561
2562template <typename T, typename TIndex>
2564{
2565 if (this != &accessor)
2566 {
2567 elements_ = accessor.elements_;
2568 subsetIndices_ = accessor.subsetIndices_;
2569 subsetSize_ = accessor.subsetSize_;
2570
2571 accessor.elements_ = nullptr;
2572 accessor.subsetIndices_ = nullptr;
2573 accessor.subsetSize_ = 0;
2574 }
2575
2576 return *this;
2577}
2578
2579template <typename T>
2581 callback_(callback),
2582 size_(size)
2583{
2584 // nothing to do here
2585}
2586
2587template <typename T>
2588const T& ConstCallbackIndexedAccessor<T>::operator[](const size_t& index) const
2589{
2590 ocean_assert(index < size_);
2591 ocean_assert(callback_);
2592
2593 return callback_(index);
2594}
2595
2596template <typename T>
2598{
2599 return size_;
2600}
2601
2602template <typename T>
2603template <typename TAccessor>
2605 data_(nullptr),
2606 size_(0)
2607{
2608 data_ = accessor.data();
2609 size_ = accessor.size();
2610
2611 if (data_ == nullptr && size_ != 0)
2612 {
2613 intermediateValues_.reserve(size_);
2614
2615 for (size_t n = 0; n < size_; ++n)
2616 {
2617 intermediateValues_.emplace_back(accessor[n]);
2618 }
2619
2620 data_ = intermediateValues_.data();
2621 }
2622}
2623
2624template <typename T>
2626{
2627 ocean_assert((data_ != nullptr && size_ != 0) || (data_ == nullptr && size_ == 0));
2628
2629 return data_;
2630}
2631
2632template <typename T>
2634{
2635 ocean_assert((data_ != nullptr && size_ != 0) || (data_ == nullptr && size_ == 0));
2636
2637 return size_;
2638}
2639
2640template <typename T>
2641inline const T& ScopedConstMemoryAccessor<T>::operator[](const size_t index) const
2642{
2643 ocean_assert(data_ && index < size());
2644
2645 return data_[index];
2646}
2647
2648template <typename T>
2650{
2651 ocean_assert((data_ != nullptr && size_ != 0) || (data_ == nullptr && size_ == 0));
2652
2653 return data_ != nullptr;
2654}
2655
2656template <typename T>
2658 accessor_(&accessor),
2659 data_(nullptr),
2660 size_(0)
2661{
2662 data_ = accessor.data();
2663 size_ = accessor.size();
2664
2665 if (data_ == nullptr && size_ != 0)
2666 {
2667 // the provided accessor does not allow to access the elements as a joined memory block, so we create out own intermediate memory block
2668
2669 intermediateValues_.reserve(size_);
2670
2671 for (size_t n = 0; n < size_; ++n)
2672 {
2673 intermediateValues_.emplace_back(accessor[n]);
2674 }
2675
2676 data_ = intermediateValues_.data();
2677 }
2678}
2679
2680template <typename T>
2682 accessor_(accessor),
2683 data_(nullptr),
2684 size_(0)
2685{
2686 if (accessor_)
2687 {
2688 data_ = accessor_->data();
2689 size_ = accessor_->size();
2690
2691 if (data_ == nullptr && size_ != 0)
2692 {
2693 // the provided accessor does not allow to access the elements as a joined memory block, so we create out own intermediate memory block
2694
2695 intermediateValues_.reserve(size_);
2696
2697 for (size_t n = 0; n < size_; ++n)
2698 {
2699 intermediateValues_.emplace_back((*accessor_)[n]);
2700 }
2701
2702 data_ = intermediateValues_.data();
2703 }
2704 }
2705 else
2706 {
2707 // no provided accessor so that we create our own intermediate memory block
2708
2709 intermediateValues_.resize(temporarySize);
2710
2711 data_ = intermediateValues_.data();
2712 size_ = intermediateValues_.size();
2713 }
2714}
2715
2716template <typename T>
2718{
2719 if (accessor_ && !intermediateValues_.empty())
2720 {
2721 ocean_assert(accessor_->data() == nullptr);
2722 ocean_assert(size_ == accessor_->size());
2723
2724 for (size_t n = 0; n < size_; ++n)
2725 {
2726 (*accessor_)[n] = std::move(intermediateValues_[n]);
2727 }
2728 }
2729}
2730
2731template <typename T>
2733{
2734 ocean_assert((data_ != nullptr && size_ != 0) || (data_ == nullptr && size_ == 0));
2735
2736 return data_;
2737}
2738
2739template <typename T>
2741{
2742 ocean_assert((data_ != nullptr && size_ != 0) || (data_ == nullptr && size_ == 0));
2743
2744 return size_;
2745}
2746
2747template <typename T>
2749{
2750 ocean_assert(data_ && index < size());
2751
2752 return data_[index];
2753}
2754
2755template <typename T>
2757{
2758 ocean_assert((data_ != nullptr && size_ != 0) || (data_ == nullptr && size_ == 0));
2759
2760 return data_ != nullptr;
2761}
2762
2763}
2764
2765#endif // META_OCEAN_BASE_ACCESSOR_H
This class implements a base class for all accessors.
Definition Accessor.h:23
Accessor(const Accessor &accessor)=default
Protected copy constructor.
static std::unordered_map< typename TAccessor::KeyType, typename TAccessor::Type > accessor2map(const TAccessor &accessor)
Returns all elements of a given accessor as a map with key and elements.
Definition Accessor.h:1598
virtual size_t size() const =0
Returns the number of accessible elements of this accessor object.
virtual ~Accessor()=default
Default destructor.
static std::vector< typename TAccessor::Type > accessor2subsetElements(const TAccessor &accessor, const std::vector< TIndex > &subset)
Returns a subset of all elements of a given accessor (as a block).
Definition Accessor.h:1622
Accessor()=default
Protected default constructor.
bool isEmpty() const
Returns whether this accessor provides no elements.
Definition Accessor.h:1578
Accessor & operator=(const Accessor &accessor)=delete
Deleted assign operator.
static std::vector< typename TAccessor::Type > accessor2elements(const TAccessor &accessor)
Returns all elements of a given accessor (as a block).
Definition Accessor.h:1584
This class implements a base class for accessors allowing a constant reference access.
Definition Accessor.h:97
virtual const T & operator[](const TKey &key) const =0
Returns one element of this accessor object by a given key.
virtual bool nextElement(const TKey &previousKey, T &nextElement, TKey &nextKey) const =0
Returns the next element which follows a given key of the previous element.
TKey KeyType
Definition of the key (or e.g., index) type of this accessor.
Definition Accessor.h:108
T Type
Definition of the element type of this accessor.
Definition Accessor.h:103
virtual const T * data() const
Returns a pointer to the elements of this accessor if the data exists within one memory block without...
Definition Accessor.h:1636
ConstAccessor()=default
Protected default constructor.
virtual bool canAccess(const TKey &key) const =0
Returns whether this accessor has a specific element.
virtual bool firstElement(T &element, TKey &key) const =0
Returns the first element of this accessor.
This class implements an accessor providing direct access to a constant array of elements.
Definition Accessor.h:400
ConstArrayAccessor()=default
Creates a new empty accessor.
ConstArrayAccessor< T > & operator=(ConstArrayAccessor< T > &&accessor) noexcept
Move operator.
Definition Accessor.h:1808
const T * elements_
The elements of this accessor.
Definition Accessor.h:457
virtual const T * data() const
Returns a pointer to the elements of this accessor if the data exists within one memory block without...
Definition Accessor.h:1787
size_t size_
The number of elements that can be accessed.
Definition Accessor.h:460
virtual size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:1793
virtual const T & operator[](const size_t &index) const
Returns one element of this accessor object.
Definition Accessor.h:1799
This class implements an indexed-based constant accessor providing access to a subset of elements sto...
Definition Accessor.h:1141
size_t subsetSize_
The number of elements that can be accessed.
Definition Accessor.h:1215
ConstArraySubsetAccessor< T, TIndex > & operator=(ConstArraySubsetAccessor< T, TIndex > &&accessor) noexcept
Move operator.
Definition Accessor.h:2447
virtual size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:2432
virtual const T & operator[](const size_t &index) const
Returns one element of this accessor object.
Definition Accessor.h:2438
TIndex IndexType
Definition of the data type of the indices.
Definition Accessor.h:1147
ConstArraySubsetAccessor()=default
Creates a new empty accessor.
const TIndex * subsetIndices_
The subset indices of this accessor.
Definition Accessor.h:1212
const T * elements_
The elements of this accessor.
Definition Accessor.h:1209
This class implements an accessor providing access to a elements by using a callback function.
Definition Accessor.h:1406
virtual const T & operator[](const size_t &index) const
Returns one element of this accessor object.
Definition Accessor.h:2588
CallbackFunction callback_
The callback function of this accessor.
Definition Accessor.h:1445
size_t size_
The number of elements that can be accessed.
Definition Accessor.h:1448
ConstCallbackIndexedAccessor()=default
Creates a new empty accessor.
virtual size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:2597
This class implements an accessor providing direct access to a constant array of elements while all e...
Definition Accessor.h:942
const T * element_
The element of this accessor.
Definition Accessor.h:986
virtual const T & operator[](const size_t &index) const
Returns one element of this accessor object.
Definition Accessor.h:2208
ConstElementAccessor()=default
Creates a new empty accessor.
size_t size_
The number of elements that can be accessed.
Definition Accessor.h:989
ConstElementAccessor< T > & operator=(ConstElementAccessor< T > &&accessor) noexcept
Move operator.
Definition Accessor.h:2217
virtual size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:2202
This class implements a base class for all indexed-based accessors allowing a constant reference acce...
Definition Accessor.h:241
virtual const T & operator[](const size_t &index) const =0
Returns one element of this accessor object by a given index.
virtual bool canAccess(const size_t &index) const
Returns whether this accessor has a specific element.
Definition Accessor.h:1648
virtual bool nextElement(const size_t &previousIndex, T &nextElement, size_t &nextIndex) const
Returns the next element which follows a given key of the previous element.
Definition Accessor.h:1668
ConstIndexedAccessor()=default
Creates a new indexed-based accessor object.
virtual bool firstElement(T &element, size_t &index) const
Returns the first element of this accessor.
Definition Accessor.h:1654
This class implements an indexed-based constant accessor providing access to a subset of elements sto...
Definition Accessor.h:1356
virtual size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:1751
virtual const T & operator[](const size_t &index) const
Returns one element of this accessor object.
Definition Accessor.h:1757
const ConstIndexedAccessor< T > & child_
Definition Accessor.h:1390
const TIndex * subsetIndices_
The subset indices of this accessor.
Definition Accessor.h:1393
size_t subsetSize_
The number of elements that can be accessed.
Definition Accessor.h:1396
ConstIndexedAccessorSubsetAccessor(const ConstIndexedAccessor< T > &child, const TIndex *subsetIndices, const size_t subsetSize)
Creates a new accessor which uses an accessor as base.
Definition Accessor.h:1733
This class implements an accessor providing direct access to a constant (unordered) map of elements.
Definition Accessor.h:1001
ConstMapAccessor()=default
Creates a new empty accessor.
virtual bool firstElement(T &element, TKey &key) const
Returns the first element of this accessor.
Definition Accessor.h:2262
virtual bool nextElement(const TKey &previousKey, T &nextElement, TKey &nextKey) const
Returns the next element which follows a given key of the previous element.
Definition Accessor.h:2276
const std::unordered_map< TKey, T > * elementMap_
The elements of this accessor.
Definition Accessor.h:1062
ConstMapAccessor< T, TKey > & operator=(ConstMapAccessor< T, TKey > &&accessor) noexcept
Move operator.
Definition Accessor.h:2305
virtual size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:2245
virtual const T & operator[](const TKey &key) const
Returns one element of this accessor object.
Definition Accessor.h:2296
virtual bool canAccess(const TKey &key) const
Returns whether this accessor has a specific element.
Definition Accessor.h:2251
This class implements an accessor providing direct access to a constant array of elements.
Definition Accessor.h:600
ConstTemplateArrayAccessor()=default
Creates a new empty accessor.
T Type
Definition of the element type of this accessor.
Definition Accessor.h:606
const T & operator[](const size_t &index) const
Returns one element of this accessor object.
Definition Accessor.h:1929
bool firstElement(T &element, size_t &index) const
Returns the first element of this accessor.
Definition Accessor.h:1903
bool isEmpty() const
Returns whether this accessor provides no elements.
Definition Accessor.h:1891
size_t size_
The number of elements that can be accessed.
Definition Accessor.h:696
const T * data() const
Returns a pointer to the elements of this accessor if the data exists within one memory block without...
Definition Accessor.h:1879
size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:1885
bool canAccess(const size_t &index) const
Returns whether this accessor has a specific element.
Definition Accessor.h:1897
ConstTemplateArrayAccessor< T > & operator=(ConstTemplateArrayAccessor< T > &&accessor) noexcept
Move operator.
Definition Accessor.h:1936
const T * elements_
The elements of this accessor.
Definition Accessor.h:693
bool nextElement(const size_t &previousIndex, T &nextElement, size_t &nextIndex) const
Returns the next element which follows a given key of the previous element.
Definition Accessor.h:1916
This class implements an indexed-based constant accessor providing access to a subset of elements sto...
Definition Accessor.h:1231
const T & operator[](const size_t &index) const
Returns one element of this accessor object.
Definition Accessor.h:2555
T Type
Definition of the element type of this accessor.
Definition Accessor.h:1237
const T * data() const
Returns a pointer to the elements of this accessor if the data exists within one memory block without...
Definition Accessor.h:2503
size_t subsetSize_
The number of elements that can be accessed.
Definition Accessor.h:1345
ConstTemplateArraySubsetAccessor< T, TIndex > & operator=(ConstTemplateArraySubsetAccessor< T, TIndex > &&accessor) noexcept
Move operator.
Definition Accessor.h:2563
bool isEmpty() const
Returns whether this accessor provides no elements.
Definition Accessor.h:2515
ConstTemplateArraySubsetAccessor()=default
Creates a new empty accessor.
bool canAccess(const size_t &index) const
Returns whether this accessor has a specific element.
Definition Accessor.h:2521
const TIndex * subsetIndices_
The subset indices of this accessor.
Definition Accessor.h:1342
bool firstElement(T &element, size_t &index) const
Returns the first element of this accessor.
Definition Accessor.h:2527
bool nextElement(const size_t &previousIndex, T &nextElement, size_t &nextIndex) const
Returns the next element which follows a given key of the previous element.
Definition Accessor.h:2541
TIndex IndexType
Definition of the data type of the indices.
Definition Accessor.h:1247
const T * elements_
The elements of this accessor.
Definition Accessor.h:1339
size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:2509
This class implements a base class for accessors allowing a non-constant reference access.
Definition Accessor.h:166
virtual T & operator[](const TKey &key)=0
Returns one element of this accessor object by a given key.
NonconstAccessor()=default
Creates a new indexed-based accessor object.
virtual T * data()
Returns a pointer to the elements of this accessor if the data exists within one memory block without...
Definition Accessor.h:1642
This class implements an accessor providing direct access to an array of elements.
Definition Accessor.h:707
T * elements_
The elements of this accessor.
Definition Accessor.h:788
virtual const T & operator[](const size_t &index) const
Returns one element of this accessor object.
Definition Accessor.h:2004
virtual T * data()
Returns a pointer to the elements of this accessor if the data exists within one memory block without...
Definition Accessor.h:2022
NonconstArrayAccessor< T > & operator=(NonconstArrayAccessor< T > &&accessor) noexcept
Move operator.
Definition Accessor.h:2034
size_t size_
The number of elements that can be accessed.
Definition Accessor.h:791
virtual size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:2028
NonconstArrayAccessor()=default
Creates a new empty accessor.
This class implements a base class for all indexed-based accessors allowing a non-constant reference ...
Definition Accessor.h:284
virtual T & operator[](const size_t &index)=0
Returns one element of this accessor object by a given index.
virtual bool canAccess(const size_t &index) const
Returns whether this accessor has a specific element.
Definition Accessor.h:1682
virtual bool firstElement(T &element, size_t &index) const
Returns the first element of this accessor.
Definition Accessor.h:1688
NonconstIndexedAccessor< T > * pointer()
Returns the pointer to this object if this accessor holds at least one element (if this accessor is n...
Definition Accessor.h:1714
virtual bool nextElement(const size_t &previousIndex, T &nextElement, size_t &nextIndex) const
Returns the next element which follows a given key of the previous element.
Definition Accessor.h:1701
virtual const T & operator[](const size_t &index) const =0
Returns one element of this accessor object by a given key.
NonconstIndexedAccessor()=default
Creates a new accessor object.
This class implements an accessor providing direct access to an (unordered) map of elements.
Definition Accessor.h:1074
NonconstMapAccessor()=default
Creates a new empty accessor.
std::unordered_map< TKey, T > * elementMap_
The elements of this accessor.
Definition Accessor.h:1128
virtual bool canAccess(const TKey &key) const
Returns whether this accessor has a specific element.
Definition Accessor.h:2324
virtual bool firstElement(T &element, TKey &key) const
Returns the first element of this accessor.
Definition Accessor.h:2335
virtual size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:2369
virtual const T & operator[](const TKey &key) const
Returns one element of this accessor object.
Definition Accessor.h:2375
virtual bool nextElement(const TKey &previousKey, T &nextElement, TKey &nextKey) const
Returns the next element which follows a given key of the previous element.
Definition Accessor.h:2349
This class implements an accessor providing direct access to an array of elements.
Definition Accessor.h:805
bool nextElement(const size_t &previousIndex, T &nextElement, size_t &nextIndex) const
Returns the next element which follows a given key of the previous element.
Definition Accessor.h:2145
NonconstTemplateArrayAccessor()=default
Creates a new empty accessor.
T Type
Definition of the element type of this accessor.
Definition Accessor.h:811
size_t size_
The number of elements that can be accessed.
Definition Accessor.h:931
bool firstElement(T &element, size_t &index) const
Returns the first element of this accessor.
Definition Accessor.h:2132
const T * data() const
Returns a pointer to the elements of this accessor if the data exists within one memory block without...
Definition Accessor.h:2102
bool canAccess(const size_t &index) const
Returns whether this accessor has a specific element.
Definition Accessor.h:2126
NonconstTemplateArrayAccessor< T > & operator=(NonconstTemplateArrayAccessor< T > &&accessor) noexcept
Move operator.
Definition Accessor.h:2172
bool isEmpty() const
Returns whether this accessor provides no elements.
Definition Accessor.h:2120
T * elements_
The elements of this accessor.
Definition Accessor.h:928
size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:2114
const T & operator[](const size_t &index) const
Returns one element of this accessor object.
Definition Accessor.h:2158
This class implements an accessor that guarantees memory access to the elements of an indexed accesso...
Definition Accessor.h:1459
const T & operator[](const size_t index) const
Returns one element of this accessor.
Definition Accessor.h:2641
size_t size_
The number of elements the accessor provides.
Definition Accessor.h:1501
const T * data_
The pointer to the memory block of the accessor.
Definition Accessor.h:1498
const T * data() const
Returns the pointer to the memory block providing the data of the accessor.
Definition Accessor.h:2625
std::vector< T > intermediateValues_
The individual elements of the accessor, if necessary.
Definition Accessor.h:1504
size_t size() const
Returns the number of elements the accessor provides.
Definition Accessor.h:2633
ScopedConstMemoryAccessor(const TAccessor &accessor)
Creates a new scoped accessor object by a given indexed accessor object.
Definition Accessor.h:2604
This class implements an accessor that guarantees memory access to the elements of an indexed accesso...
Definition Accessor.h:1516
T * data_
The pointer to the memory block of the accessor.
Definition Accessor.h:1569
size_t size_
The number of elements the accessor provides.
Definition Accessor.h:1572
ScopedNonconstMemoryAccessor(NonconstIndexedAccessor< T > &accessor)
Creates a new scoped accessor object by a given indexed accessor object.
Definition Accessor.h:2657
NonconstIndexedAccessor< T > * accessor_
The given accessor object providing the data for this object, nullptr if no accessor object was provi...
Definition Accessor.h:1566
T * data()
Returns the pointer to the memory block providing the data of the accessor.
Definition Accessor.h:2732
T & operator[](const size_t index)
Returns one element of this accessor.
Definition Accessor.h:2748
~ScopedNonconstMemoryAccessor()
Destructs the scoped accessor object.
Definition Accessor.h:2717
std::vector< T > intermediateValues_
The individual elements of the accessor, if necessary.
Definition Accessor.h:1575
size_t size() const
Returns the number of elements the accessor provides.
Definition Accessor.h:2740
This class implements an accessor providing direct access to std::shared_ptr<T> elements returned as ...
Definition Accessor.h:497
SharedPointerConstArrayAccessor(SharedPointerConstArrayAccessor< T > &&accessor)=default
Move constructor.
std::vector< SharedPointer > SharedPointers
Definition of a vector holding the shared pointer objects.
Definition Accessor.h:508
std::shared_ptr< T > SharedPointer
Definition of the shared pointer object.
Definition Accessor.h:503
SharedPointerConstArrayAccessor()=default
Creates a new empty accessor.
std::vector< const T * > elements_
The pointers to the actual elements wrapped in the shared pointers.
Definition Accessor.h:560
virtual size_t size() const
Returns the number of accessible elements of this accessor object.
Definition Accessor.h:1843
SharedPointerConstArrayAccessor< T > & operator=(SharedPointerConstArrayAccessor< T > &&accessor)=default
Move operator.
virtual const T *const & operator[](const size_t &index) const
Returns one element of this accessor object.
Definition Accessor.h:1849
This class implements a base class for all accessors allowing to access temporary elements.
Definition Accessor.h:202
T Type
Definition of the element type of this accessor.
Definition Accessor.h:208
virtual T operator[](const TKey &key) const =0
Returns one element of this accessor object by a given index.
TemporaryAccessor()=default
Creates a new indexed-based accessor object.
virtual bool canAccess(const TKey &key) const =0
Returns whether this accessor has a specific element.
This class implements a base class for all indexed-based accessors allowing to access temporary eleme...
Definition Accessor.h:341
virtual T operator[](const size_t &index) const =0
Returns one element of this accessor object by a given index.
virtual bool canAccess(const size_t &index) const
Returns whether this accessor has a specific element.
Definition Accessor.h:1727
TemporaryIndexedAccessor()=default
Creates a new indexed-based accessor object.
The namespace covering the entire Ocean framework.
Definition Accessor.h:15