8 #ifndef META_OCEAN_BASE_SUBSET_H
9 #define META_OCEAN_BASE_SUBSET_H
28 template <
typename TIndex>
43 static inline std::vector<T>
subset(
const std::vector<T>& objects,
const std::vector<TIndex>& indices);
54 static inline std::vector<T>
subset(
const std::vector<T>& objects,
const std::set<TIndex>& indices);
65 static inline std::vector<T>
invertedSubset(
const std::vector<T>& objects,
const std::unordered_set<TIndex>& indices);
76 static inline std::vector<T>
invertedSubset(
const std::vector<T>& objects,
const std::set<TIndex>& indices);
90 static inline std::vector<T>
subset(
const T* objects,
const size_t numberObjects,
const TIndex* indices,
const size_t numberIndices);
102 template <
typename T>
103 static inline std::vector<T>
subset(
const T* objects,
const size_t numberObjects,
const std::vector<TIndex>& indices);
114 template <
typename T>
115 static inline std::vector<T>
subset(
const T* objects,
const size_t numberObjects,
const std::set<TIndex>& indices);
126 template <
typename T>
127 static inline std::vector<T>
invertedSubset(
const T* objects,
const size_t numberObjects,
const std::unordered_set<TIndex>& indices);
138 template <
typename T>
139 static inline std::vector<T>
invertedSubset(
const T* objects,
const size_t numberObjects,
const std::set<TIndex>& indices);
149 template <
typename TContainer, u
int8_t tValue>
150 static inline std::vector<uint8_t>
indices2statements(
const TContainer& indices,
const size_t numberObjects);
160 template <u
int8_t tValue>
161 static inline std::vector<uint8_t>
indices2statements(
const TIndex* indices,
const size_t numberIndices,
const size_t numberObjects);
169 template <u
int8_t tValue>
170 static inline std::vector<TIndex>
statements2indices(
const std::vector<uint8_t>& statements);
179 template <u
int8_t tValue>
180 static inline std::vector<TIndex>
statements2indices(
const uint8_t* statements,
const size_t numberStatements);
195 template <
typename TIndex,
typename T>
196 static inline std::vector<T>
subset(
const std::vector<T>& objects,
const std::vector<TIndex>& indices);
207 template <
typename TIndex,
typename T>
208 static inline std::vector<T>
subset(
const std::vector<T>& objects,
const std::set<TIndex>& indices);
219 template <
typename TIndex,
typename T>
220 static inline std::vector<T>
invertedSubset(
const std::vector<T>& objects,
const std::unordered_set<TIndex>& indices);
231 template <
typename TIndex,
typename T>
232 static inline std::vector<T>
invertedSubset(
const std::vector<T>& objects,
const std::set<TIndex>& indices);
246 template <
typename TIndex,
typename T>
247 static inline std::vector<T>
subset(
const T* objects,
const size_t numberObjects,
const TIndex* indices,
const size_t numberIndices);
260 template <
typename TIndex,
typename T>
261 static inline std::vector<T>
subset(
const T* objects,
const size_t numberObjects,
const std::vector<TIndex>& indices);
273 template <
typename TIndex,
typename T>
274 static inline std::vector<T>
subset(
const T* objects,
const size_t numberObjects,
const std::set<TIndex>& indices);
282 template <
typename TIndex>
283 static std::vector<TIndex>
invertedIndices(
const std::vector<TIndex>& indices,
const size_t numberElements);
291 template <
typename TIndex>
292 static std::unordered_set<TIndex>
invertedIndices(
const std::unordered_set<TIndex>& indices,
const size_t numberElements);
300 template <
typename TIndex>
301 static std::set<TIndex>
invertedIndices(
const std::set<TIndex>& indices,
const size_t numberElements);
313 template <
typename TIndex,
typename T>
314 static inline std::vector<T>
invertedSubset(
const T* objects,
const size_t numberObjects,
const std::unordered_set<TIndex>& indices);
326 template <
typename TIndex,
typename T>
327 static inline std::vector<T>
invertedSubset(
const T* objects,
const size_t numberObjects,
const std::set<TIndex>& indices);
337 template <
typename TIndex, u
int8_t tValue>
338 static inline std::vector<uint8_t>
indices2statements(
const std::vector<TIndex>& indices,
const size_t numberObjects);
348 template <
typename TIndex, u
int8_t tValue>
349 static inline std::vector<uint8_t>
indices2statements(
const std::set<TIndex>& indices,
const size_t numberObjects);
359 template <
typename TIndex, u
int8_t tValue>
360 static inline std::vector<uint8_t>
indices2statements(
const std::unordered_set<TIndex>& indices,
const size_t numberObjects);
371 template <
typename TIndex, u
int8_t tValue>
372 static inline std::vector<uint8_t>
indices2statements(
const TIndex* indices,
const size_t numberIndices,
const size_t numberObjects);
381 template <
typename TIndex, u
int8_t tValue>
382 static inline std::vector<TIndex>
statements2indices(
const std::vector<uint8_t>& statements);
392 template <
typename TIndex, u
int8_t tValue>
393 static inline std::vector<TIndex>
statements2indices(
const uint8_t* statements,
const size_t numberStatements);
405 template <
typename TKey,
typename TElement>
406 static void correspondingElements(
const std::map<TKey, TElement>& elementMapA,
const std::map<TKey, TElement>& elementMapB, std::vector<TElement>& elementsA, std::vector<TElement>& elementsB);
417 template <
typename TIterator>
418 static bool hasIntersectingElement(
const TIterator& firstA,
const TIterator& endA,
const TIterator& firstB,
const TIterator& endB);
426 template <
typename T>
435 template <
typename T>
439 template <
typename TIndex>
440 template <
typename T>
443 std::vector<T> result;
444 result.reserve(indices.size());
446 for (
const TIndex& index : indices)
448 ocean_assert(
size_t(index) < objects.size());
449 result.push_back(objects[
size_t(index)]);
456 template <
typename T>
459 std::vector<T> result;
460 result.reserve(indices.size());
462 typename std::vector<T>::const_iterator iObject = objects.begin();
464 for (
const uint8_t index : indices)
468 result.push_back(*iObject);
477 template <
typename TIndex>
478 template <
typename T>
481 std::vector<T> result;
482 result.reserve(indices.size());
484 for (
typename std::set<TIndex>::const_iterator i = indices.begin(); i != indices.end(); ++i)
486 ocean_assert(*i < objects.size());
487 result.push_back(objects[
size_t(*i)]);
493 template <
typename TIndex>
494 template <
typename T>
497 ocean_assert(objects.size() >= indices.size());
498 ocean_assert((
unsigned long long)(objects.size()) < (
unsigned long long)(std::numeric_limits<TIndex>::max()));
500 if (objects.size() == indices.size())
503 for (
const TIndex& index : indices)
505 ocean_assert(index < objects.size());
509 return std::vector<T>();
512 std::vector<T> result;
513 result.reserve(objects.size() - indices.size());
515 for (
size_t n = 0u; n < objects.size(); ++n)
517 if (indices.find(TIndex(n)) == indices.cend())
519 result.push_back(objects[n]);
526 template <
typename TIndex>
527 template <
typename T>
530 ocean_assert(objects.size() >= indices.size());
531 ocean_assert((
unsigned long long)(objects.size()) < (
unsigned long long)(std::numeric_limits<TIndex>::max()));
533 if (objects.size() == indices.size())
536 for (
const TIndex& index : indices)
538 ocean_assert(index < objects.size());
542 return std::vector<T>();
545 std::vector<T> result;
546 result.reserve(objects.size() - indices.size());
548 for (
size_t n = 0u; n < objects.size(); ++n)
550 if (indices.find(TIndex(n)) == indices.cend())
552 result.push_back(objects[n]);
559 template <
typename TIndex>
560 template <
typename T>
563 std::vector<T> result;
564 result.reserve(numberIndices);
566 for (
size_t n = 0; n < numberIndices; ++n)
568 ocean_assert_and_suppress_unused(indices[n] < numberObjects, numberObjects);
570 result.push_back(objects[indices[n]]);
577 template <
typename T>
580 ocean_assert_and_suppress_unused(numberIndices <= numberObjects, numberObjects);
582 std::vector<T> result;
583 result.reserve(numberIndices);
585 for (
size_t n = 0; n < numberIndices; ++n)
587 if (indices[n] != 0u)
589 result.push_back(objects[n]);
596 template <
typename TIndex>
597 template <
typename T>
600 return subset(objects, numberObjects, indices.data(), indices.size());
603 template <
typename TIndex>
604 template <
typename T>
607 std::vector<T> result;
608 result.reserve(indices.size());
610 for (
const TIndex& index : indices)
612 ocean_assert_and_suppress_unused(index < numberObjects, numberObjects);
614 result.push_back(objects[index]);
620 template <
typename TIndex>
621 template <
typename T>
624 ocean_assert(numberObjects >= indices.size());
625 ocean_assert((
unsigned long long)(numberObjects) < (
unsigned long long)(std::numeric_limits<TIndex>::max()));
627 if (numberObjects == indices.size())
630 for (
const TIndex& index : indices)
632 ocean_assert(indices < numberObjects);
636 return std::vector<T>();
639 std::vector<T> result;
640 result.reserve(numberObjects - indices.size());
642 for (
size_t n = 0; n < numberObjects; ++n)
644 if (indices.find(TIndex(n)) == indices.cend())
646 result.push_back(objects[n]);
653 template <
typename TIndex>
654 template <
typename T>
657 ocean_assert(numberObjects >= indices.size());
658 ocean_assert((
unsigned long long)(numberObjects) < (
unsigned long long)(std::numeric_limits<TIndex>::max()));
660 if (numberObjects == indices.size())
663 for (
const TIndex& index : indices)
665 ocean_assert(index < numberObjects);
669 return std::vector<T>();
672 std::vector<T> result;
673 result.reserve(numberObjects - indices.size());
675 for (
size_t n = 0; n < numberObjects; ++n)
677 if (indices.find(TIndex(n)) == indices.cend())
679 result.push_back(objects[n]);
686 template <
typename TIndex>
687 template <
typename TContainer, u
int8_t tValue>
690 std::vector<uint8_t> result(numberObjects, !tValue);
692 for (
const TIndex& index : indices)
694 ocean_assert(index < result.size());
695 result[index] = tValue;
701 template <
typename TIndex>
702 template <u
int8_t tValue>
705 std::vector<uint8_t> result(numberObjects, !tValue);
707 for (
size_t n = 0; n < numberIndices; ++n)
709 ocean_assert(indices[n] < result.size());
710 result[indices[n]] = tValue;
716 template <
typename TIndex>
717 template <u
int8_t tValue>
720 std::vector<TIndex> result;
722 for (
size_t n = 0; n < statements.size(); ++n)
724 if (statements[n] == tValue)
726 result.push_back(TIndex(n));
733 template <
typename TIndex>
734 template <u
int8_t tValue>
737 std::vector<TIndex> result;
739 for (
size_t n = 0; n < numberStatements; ++n)
741 if (statements[n] == tValue)
743 result.push_back(TIndex(n));
750 template <
typename TIndex,
typename T>
751 inline std::vector<T>
Subset::subset(
const std::vector<T>& objects,
const std::vector<TIndex>& indices)
756 template <
typename TIndex,
typename T>
757 inline std::vector<T>
Subset::subset(
const std::vector<T>& objects,
const std::set<TIndex>& indices)
762 template <
typename TIndex>
765 ocean_assert(indices.size() <= TIndex(numberElements));
767 if (indices.size() == numberElements)
770 const std::unordered_set<TIndex> indexSet(indices.begin(), indices.end());
771 ocean_assert(indexSet.size() == indices.size());
773 for (
size_t n = 0; n < numberElements; ++n)
775 ocean_assert(indexSet.find(TIndex(n)) != indexSet.cend());
779 return std::vector<TIndex>();
782 const std::unordered_set<TIndex> indexSet(indices.begin(), indices.end());
783 ocean_assert(indexSet.size() == indices.size());
785 std::vector<TIndex> result;
786 result.reserve(numberElements - indices.size());
788 for (TIndex i = TIndex(0); i < TIndex(numberElements); ++i)
790 if (indexSet.find(i) == indexSet.cend())
799 template <
typename TIndex>
802 ocean_assert(indices.size() <= TIndex(numberElements));
804 if (indices.size() == numberElements)
807 for (
size_t n = 0; n < numberElements; ++n)
809 ocean_assert(indices.find(TIndex(n)) != indices.end());
813 return std::unordered_set<TIndex>();
816 std::unordered_set<TIndex> result;
817 result.reserve(numberElements - indices.size());
819 for (TIndex i = TIndex(0); i < TIndex(numberElements); ++i)
821 if (indices.find(i) == indices.cend())
830 template <
typename TIndex>
833 ocean_assert(indices.size() <= TIndex(numberElements));
835 if (indices.size() == numberElements)
838 for (
size_t n = 0; n < numberElements; ++n)
840 ocean_assert(indices.find(TIndex(n)) != indices.end());
844 return std::set<TIndex>();
847 std::set<TIndex> result;
849 for (TIndex i = TIndex(0); i < TIndex(numberElements); ++i)
851 if (indices.find(i) == indices.cend())
860 template <
typename TIndex,
typename T>
866 template <
typename TIndex,
typename T>
872 template <
typename TIndex,
typename T>
873 inline std::vector<T>
Subset::subset(
const T* objects,
const size_t numberObjects,
const TIndex* indices,
const size_t numberIndices)
878 template <
typename TIndex,
typename T>
879 inline std::vector<T>
Subset::subset(
const T* objects,
const size_t numberObjects,
const std::vector<TIndex>& indices)
884 template <
typename TIndex,
typename T>
885 inline std::vector<T>
Subset::subset(
const T* objects,
const size_t numberObjects,
const std::set<TIndex>& indices)
890 template <
typename TIndex,
typename T>
891 inline std::vector<T>
Subset::invertedSubset(
const T* objects,
const size_t numberObjects,
const std::unordered_set<TIndex>& indices)
896 template <
typename TIndex,
typename T>
897 inline std::vector<T>
Subset::invertedSubset(
const T* objects,
const size_t numberObjects,
const std::set<TIndex>& indices)
902 template <
typename TIndex, u
int8_t tValue>
908 template <
typename TIndex, u
int8_t tValue>
914 template <
typename TIndex, u
int8_t tValue>
920 template <
typename TIndex, u
int8_t tValue>
926 template <
typename TIndex, u
int8_t tValue>
932 template <
typename TIndex, u
int8_t tValue>
938 template <
typename TKey,
typename TElement>
939 void Subset::correspondingElements(
const std::map<TKey, TElement>& elementMapA,
const std::map<TKey, TElement>& elementMapB, std::vector<TElement>& elementsA, std::vector<TElement>& elementsB)
945 ocean_assert(elementMapA.size() < 2 || elementMapA.cbegin()->first < elementMapA.crbegin()->first);
946 ocean_assert(elementMapB.size() < 2 || elementMapB.cbegin()->first < elementMapB.crbegin()->first);
948 typename std::map<TKey, TElement>::const_iterator iA = elementMapA.cbegin();
949 typename std::map<TKey, TElement>::const_iterator iB = elementMapB.cbegin();
951 while (iA != elementMapA.cend() && iB != elementMapB.cend())
955 if (iA->first > iB->first)
959 else if (iA->first < iB->first)
965 ocean_assert(iA->first == iB->first);
967 elementsA.emplace_back(iA->second);
968 elementsB.emplace_back(iB->second);
976 template <
typename TIterator>
979 if (firstA == endA || firstB == endB)
985 TIterator debugLeftA = firstA;
986 TIterator debugRightA = firstA;
989 TIterator debugLeftB = firstB;
990 TIterator debugRightB = firstB;
993 while (debugRightA != endA && debugRightB != endB)
995 ocean_assert(*debugLeftA++ <= *debugRightA++);
996 ocean_assert(*debugLeftB++ <= *debugRightB++);
1000 TIterator lastA = endA;
1001 TIterator lastB = endB;
1003 if (*(--lastA) < *firstB || *(--lastB) < *firstA)
1008 TIterator iA = firstA;
1009 TIterator iB = firstB;
1011 while (iA != endA && iB != endB)
1024 ocean_assert(*iA > *iB);
1032 template <
typename T>
1038 template <
typename T>
This helper class implements a subset functions.
Definition: Subset.h:30
static std::vector< T > invertedSubset(const std::vector< T > &objects, const std::unordered_set< TIndex > &indices)
Extracts a subset of a given set of objects by usage of a set of indices of all objects to be not use...
Definition: Subset.h:495
static std::vector< uint8_t > indices2statements(const TContainer &indices, const size_t numberObjects)
Converts object indices to an uint8_t vector holding statements for each object.
Definition: Subset.h:688
static std::vector< TIndex > statements2indices(const std::vector< uint8_t > &statements)
Converts an uint8_t vector holding statements for each object into object indices.
Definition: Subset.h:718
static std::vector< T > subset(const std::vector< T > &objects, const std::vector< TIndex > &indices)
Extracts a subset of a given set of objects by usage of an index vector holding the indices of all ob...
Definition: Subset.h:441
This class implements subset functions.
Definition: Subset.h:21
static std::vector< uint8_t > indices2statements(const std::vector< TIndex > &indices, const size_t numberObjects)
Converts object indices to an uint8_t vector holding statements for each object.
Definition: Subset.h:903
static bool hasIntersectingElement(const TIterator &firstA, const TIterator &endA, const TIterator &firstB, const TIterator &endB)
Determines whether two (ordered) sets have at least one intersecting element.
Definition: Subset.h:977
static std::vector< T > subset(const std::vector< T > &objects, const std::vector< TIndex > &indices)
Extracts a subset of a given set of objects by usage of an index vector holding the indices of all ob...
Definition: Subset.h:751
static std::vector< TIndex > invertedIndices(const std::vector< TIndex > &indices, const size_t numberElements)
Extracts the indices that are not given within a set indices.
Definition: Subset.h:763
static void correspondingElements(const std::map< TKey, TElement > &elementMapA, const std::map< TKey, TElement > &elementMapB, std::vector< TElement > &elementsA, std::vector< TElement > &elementsB)
Determines corresponding element pairs from two sets of element maps.
Definition: Subset.h:939
static std::vector< TIndex > statements2indices(const std::vector< uint8_t > &statements)
Converts an uint8_t vector holding statements for each object into object indices.
Definition: Subset.h:927
static std::vector< T > invertedSubset(const std::vector< T > &objects, const std::unordered_set< TIndex > &indices)
Extracts a subset of a given set of objects by usage of a set of indices of all objects to be not use...
Definition: Subset.h:861
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15