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 TIndexIterator,
typename T>
283 static void applySubset(std::vector<T>& objects,
const TIndexIterator& begin,
const TIndexIterator& end);
290 template <
typename TIndex,
typename T>
291 static inline void applySubset(std::vector<T>& objects,
const std::vector<TIndex>& indices);
299 template <
typename TIndex,
typename T>
300 static inline void applySubset(std::vector<T>& objects,
const TIndex* indices,
const size_t numberIndices);
308 template <
typename TIndex>
309 static std::vector<TIndex>
invertedIndices(
const std::vector<TIndex>& indices,
const size_t numberElements);
317 template <
typename TIndex>
318 static std::unordered_set<TIndex>
invertedIndices(
const std::unordered_set<TIndex>& indices,
const size_t numberElements);
326 template <
typename TIndex>
327 static std::set<TIndex>
invertedIndices(
const std::set<TIndex>& indices,
const size_t numberElements);
339 template <
typename TIndex,
typename T>
340 static inline std::vector<T>
invertedSubset(
const T* objects,
const size_t numberObjects,
const std::unordered_set<TIndex>& indices);
352 template <
typename TIndex,
typename T>
353 static inline std::vector<T>
invertedSubset(
const T* objects,
const size_t numberObjects,
const std::set<TIndex>& indices);
363 template <
typename TIndex, u
int8_t tValue>
364 static inline std::vector<uint8_t>
indices2statements(
const std::vector<TIndex>& indices,
const size_t numberObjects);
374 template <
typename TIndex, u
int8_t tValue>
375 static inline std::vector<uint8_t>
indices2statements(
const std::set<TIndex>& indices,
const size_t numberObjects);
385 template <
typename TIndex, u
int8_t tValue>
386 static inline std::vector<uint8_t>
indices2statements(
const std::unordered_set<TIndex>& indices,
const size_t numberObjects);
397 template <
typename TIndex, u
int8_t tValue>
398 static inline std::vector<uint8_t>
indices2statements(
const TIndex* indices,
const size_t numberIndices,
const size_t numberObjects);
407 template <
typename TIndex, u
int8_t tValue>
408 static inline std::vector<TIndex>
statements2indices(
const std::vector<uint8_t>& statements);
418 template <
typename TIndex, u
int8_t tValue>
419 static inline std::vector<TIndex>
statements2indices(
const uint8_t* statements,
const size_t numberStatements);
431 template <
typename TKey,
typename TElement>
432 static void correspondingElements(
const std::map<TKey, TElement>& elementMapA,
const std::map<TKey, TElement>& elementMapB, std::vector<TElement>& elementsA, std::vector<TElement>& elementsB);
443 template <
typename TIterator>
444 static bool hasIntersectingElement(
const TIterator& firstA,
const TIterator& endA,
const TIterator& firstB,
const TIterator& endB);
452 template <
typename T>
461 template <
typename T>
465template <
typename TIndex>
469 std::vector<T> result;
470 result.reserve(indices.size());
472 for (
const TIndex& index : indices)
474 ocean_assert(
size_t(index) < objects.size());
475 result.push_back(objects[
size_t(index)]);
485 std::vector<T> result;
486 result.reserve(indices.size());
488 typename std::vector<T>::const_iterator iObject = objects.begin();
490 for (
const uint8_t index : indices)
494 result.push_back(*iObject);
503template <
typename TIndex>
507 std::vector<T> result;
508 result.reserve(indices.size());
510 for (
typename std::set<TIndex>::const_iterator i = indices.begin(); i != indices.end(); ++i)
512 ocean_assert(*i < objects.size());
513 result.push_back(objects[
size_t(*i)]);
519template <
typename TIndex>
523 ocean_assert(objects.size() >= indices.size());
524 ocean_assert((
unsigned long long)(objects.size()) < (
unsigned long long)(std::numeric_limits<TIndex>::max()));
526 if (objects.size() == indices.size())
529 for (
const TIndex& index : indices)
531 ocean_assert(index < objects.size());
535 return std::vector<T>();
538 std::vector<T> result;
539 result.reserve(objects.size() - indices.size());
541 for (
size_t n = 0u; n < objects.size(); ++n)
543 if (indices.find(TIndex(n)) == indices.cend())
545 result.push_back(objects[n]);
552template <
typename TIndex>
556 ocean_assert(objects.size() >= indices.size());
557 ocean_assert((
unsigned long long)(objects.size()) < (
unsigned long long)(std::numeric_limits<TIndex>::max()));
559 if (objects.size() == indices.size())
562 for (
const TIndex& index : indices)
564 ocean_assert(index < objects.size());
568 return std::vector<T>();
571 std::vector<T> result;
572 result.reserve(objects.size() - indices.size());
574 for (
size_t n = 0u; n < objects.size(); ++n)
576 if (indices.find(TIndex(n)) == indices.cend())
578 result.push_back(objects[n]);
585template <
typename TIndex>
589 std::vector<T> result;
590 result.reserve(numberIndices);
592 for (
size_t n = 0; n < numberIndices; ++n)
594 ocean_assert_and_suppress_unused(indices[n] < numberObjects, numberObjects);
596 result.push_back(objects[indices[n]]);
606 ocean_assert_and_suppress_unused(numberIndices <= numberObjects, numberObjects);
608 std::vector<T> result;
609 result.reserve(numberIndices);
611 for (
size_t n = 0; n < numberIndices; ++n)
613 if (indices[n] != 0u)
615 result.push_back(objects[n]);
622template <
typename TIndex>
626 return subset(objects, numberObjects, indices.data(), indices.size());
629template <
typename TIndex>
633 std::vector<T> result;
634 result.reserve(indices.size());
636 for (
const TIndex& index : indices)
638 ocean_assert_and_suppress_unused(index < numberObjects, numberObjects);
640 result.push_back(objects[index]);
646template <
typename TIndex>
650 ocean_assert(numberObjects >= indices.size());
651 ocean_assert((
unsigned long long)(numberObjects) < (
unsigned long long)(std::numeric_limits<TIndex>::max()));
653 if (numberObjects == indices.size())
656 for (
const TIndex& index : indices)
658 ocean_assert(indices < numberObjects);
662 return std::vector<T>();
665 std::vector<T> result;
666 result.reserve(numberObjects - indices.size());
668 for (
size_t n = 0; n < numberObjects; ++n)
670 if (indices.find(TIndex(n)) == indices.cend())
672 result.push_back(objects[n]);
679template <
typename TIndex>
683 ocean_assert(numberObjects >= indices.size());
684 ocean_assert((
unsigned long long)(numberObjects) < (
unsigned long long)(std::numeric_limits<TIndex>::max()));
686 if (numberObjects == indices.size())
689 for (
const TIndex& index : indices)
691 ocean_assert(index < numberObjects);
695 return std::vector<T>();
698 std::vector<T> result;
699 result.reserve(numberObjects - indices.size());
701 for (
size_t n = 0; n < numberObjects; ++n)
703 if (indices.find(TIndex(n)) == indices.cend())
705 result.push_back(objects[n]);
712template <
typename TIndex>
713template <
typename TContainer, u
int8_t tValue>
716 std::vector<uint8_t> result(numberObjects, !tValue);
718 for (
const TIndex& index : indices)
720 ocean_assert(index < result.size());
721 result[index] = tValue;
727template <
typename TIndex>
728template <u
int8_t tValue>
731 std::vector<uint8_t> result(numberObjects, !tValue);
733 for (
size_t n = 0; n < numberIndices; ++n)
735 ocean_assert(indices[n] < result.size());
736 result[indices[n]] = tValue;
742template <
typename TIndex>
743template <u
int8_t tValue>
746 std::vector<TIndex> result;
748 for (
size_t n = 0; n < statements.size(); ++n)
750 if (statements[n] == tValue)
752 result.push_back(TIndex(n));
759template <
typename TIndex>
760template <u
int8_t tValue>
763 std::vector<TIndex> result;
765 for (
size_t n = 0; n < numberStatements; ++n)
767 if (statements[n] == tValue)
769 result.push_back(TIndex(n));
776template <
typename TIndex,
typename T>
777inline std::vector<T>
Subset::subset(
const std::vector<T>& objects,
const std::vector<TIndex>& indices)
782template <
typename TIndex,
typename T>
783inline std::vector<T>
Subset::subset(
const std::vector<T>& objects,
const std::set<TIndex>& indices)
788template <
typename TIndex>
791 ocean_assert(indices.size() <= TIndex(numberElements));
793 if (indices.size() == numberElements)
796 const std::unordered_set<TIndex> indexSet(indices.begin(), indices.end());
797 ocean_assert(indexSet.size() == indices.size());
799 for (
size_t n = 0; n < numberElements; ++n)
801 ocean_assert(indexSet.find(TIndex(n)) != indexSet.cend());
805 return std::vector<TIndex>();
808 const std::unordered_set<TIndex> indexSet(indices.begin(), indices.end());
809 ocean_assert(indexSet.size() == indices.size());
811 std::vector<TIndex> result;
812 result.reserve(numberElements - indices.size());
814 for (TIndex i = TIndex(0); i < TIndex(numberElements); ++i)
816 if (indexSet.find(i) == indexSet.cend())
825template <
typename TIndex>
828 ocean_assert(indices.size() <= TIndex(numberElements));
830 if (indices.size() == numberElements)
833 for (
size_t n = 0; n < numberElements; ++n)
835 ocean_assert(indices.find(TIndex(n)) != indices.end());
839 return std::unordered_set<TIndex>();
842 std::unordered_set<TIndex> result;
843 result.reserve(numberElements - indices.size());
845 for (TIndex i = TIndex(0); i < TIndex(numberElements); ++i)
847 if (indices.find(i) == indices.cend())
856template <
typename TIndex>
859 ocean_assert(indices.size() <= TIndex(numberElements));
861 if (indices.size() == numberElements)
864 for (
size_t n = 0; n < numberElements; ++n)
866 ocean_assert(indices.find(TIndex(n)) != indices.end());
870 return std::set<TIndex>();
873 std::set<TIndex> result;
875 for (TIndex i = TIndex(0); i < TIndex(numberElements); ++i)
877 if (indices.find(i) == indices.cend())
886template <
typename TIndex,
typename T>
892template <
typename TIndex,
typename T>
898template <
typename TIndex,
typename T>
899inline std::vector<T>
Subset::subset(
const T* objects,
const size_t numberObjects,
const TIndex* indices,
const size_t numberIndices)
904template <
typename TIndex,
typename T>
905inline std::vector<T>
Subset::subset(
const T* objects,
const size_t numberObjects,
const std::vector<TIndex>& indices)
910template <
typename TIndex,
typename T>
911inline std::vector<T>
Subset::subset(
const T* objects,
const size_t numberObjects,
const std::set<TIndex>& indices)
916template <
typename TIndexIterator,
typename T>
927 for (TIndexIterator iterator = begin; iterator < end; ++iterator)
929 objects[nIndex] = std::move(objects[*iterator]);
934 objects.resize(nIndex);
937template <
typename TIndex,
typename T>
940 applySubset(objects, sortedIndices.cbegin(), sortedIndices.cend());
943template <
typename TIndex,
typename T>
944void Subset::applySubset(std::vector<T>& objects,
const TIndex* sortedIndices,
const size_t numberSortedIndices)
946 applySubset(objects, sortedIndices, sortedIndices + numberSortedIndices);
949template <
typename TIndex,
typename T>
950inline std::vector<T>
Subset::invertedSubset(
const T* objects,
const size_t numberObjects,
const std::unordered_set<TIndex>& indices)
955template <
typename TIndex,
typename T>
956inline std::vector<T>
Subset::invertedSubset(
const T* objects,
const size_t numberObjects,
const std::set<TIndex>& indices)
961template <
typename TIndex, u
int8_t tValue>
967template <
typename TIndex, u
int8_t tValue>
973template <
typename TIndex, u
int8_t tValue>
979template <
typename TIndex, u
int8_t tValue>
985template <
typename TIndex, u
int8_t tValue>
991template <
typename TIndex, u
int8_t tValue>
997template <
typename TKey,
typename TElement>
998void Subset::correspondingElements(
const std::map<TKey, TElement>& elementMapA,
const std::map<TKey, TElement>& elementMapB, std::vector<TElement>& elementsA, std::vector<TElement>& elementsB)
1004 ocean_assert(elementMapA.size() < 2 || elementMapA.cbegin()->first < elementMapA.crbegin()->first);
1005 ocean_assert(elementMapB.size() < 2 || elementMapB.cbegin()->first < elementMapB.crbegin()->first);
1007 typename std::map<TKey, TElement>::const_iterator iA = elementMapA.cbegin();
1008 typename std::map<TKey, TElement>::const_iterator iB = elementMapB.cbegin();
1010 while (iA != elementMapA.cend() && iB != elementMapB.cend())
1014 if (iA->first > iB->first)
1018 else if (iA->first < iB->first)
1024 ocean_assert(iA->first == iB->first);
1026 elementsA.emplace_back(iA->second);
1027 elementsB.emplace_back(iB->second);
1035template <
typename TIterator>
1038 if (firstA == endA || firstB == endB)
1044 TIterator debugLeftA = firstA;
1045 TIterator debugRightA = firstA;
1048 TIterator debugLeftB = firstB;
1049 TIterator debugRightB = firstB;
1052 while (debugRightA != endA && debugRightB != endB)
1054 ocean_assert(*debugLeftA++ <= *debugRightA++);
1055 ocean_assert(*debugLeftB++ <= *debugRightB++);
1059 TIterator lastA = endA;
1060 TIterator lastB = endB;
1062 if (*(--lastA) < *firstB || *(--lastB) < *firstA)
1067 TIterator iA = firstA;
1068 TIterator iB = firstB;
1070 while (iA != endA && iB != endB)
1083 ocean_assert(*iA > *iB);
1091template <
typename T>
1097template <
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:521
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:714
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:744
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:467
This class implements subset functions.
Definition Subset.h:21
static void applySubset(std::vector< T > &objects, const TIndexIterator &begin, const TIndexIterator &end)
Applies a subset to a vector of objects.
Definition Subset.h:917
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:962
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:1036
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:777
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:789
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:998
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:986
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:887
The namespace covering the entire Ocean framework.
Definition Accessor.h:15