Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Partition.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/Containers/SortedMapping.h"
7
8namespace Stroika::Foundation::Traversal {
9
10 /**
11 */
12 template <Traversal::IRange RANGETYPE, Common::IPotentiallyComparer<typename RANGETYPE::value_type> RANGE_ELT_COMPARER>
13 inline bool IsPartition (const Iterable<RANGETYPE>& iterable, RANGE_ELT_COMPARER comparer)
14 {
15 using Common::KeyValuePair;
16 using Containers::SortedMapping;
17 using Debug::TraceContextBumper;
18 TraceContextBumper ctx{"IsPartition_Helper_"};
19 using namespace Traversal;
20 using value_type = typename RANGETYPE::value_type;
21 SortedMapping<value_type, RANGETYPE> tmp;
22 for (const RANGETYPE& r : iterable) {
23 tmp.Add (r.GetLowerBound (), r);
24 }
25 optional<value_type> upperBoundSeenSoFar;
26 Openness upperBoundSeenSoFarOpenness{};
27 for (const KeyValuePair<value_type, RANGETYPE>& i : tmp) {
28 //DbgTrace ("i.fKey = {}, i.fValue = ({},{}, ol={}, or={})")f, i.fKey, i.fValue.GetLowerBound (), i.fValue.GetUpperBound (), i.fValue.GetLowerBoundOpenness (), i.fValue.GetUpperBoundOpenness ());
29 if (upperBoundSeenSoFar) {
30 if (not comparer (*upperBoundSeenSoFar, i.fValue.GetLowerBound ())) {
31 //DbgTrace ("i.fKey = {}, i.fValue = ({},{}, ol={}, or={})"_f, i.fKey, i.fValue.GetLowerBound (), i.fValue.GetUpperBound (), i.fValue.GetLowerBoundOpenness (), i.fValue.GetUpperBoundOpenness ());
32 //DbgTrace ("return false cuz bounds no match");
33 return false;
34 }
35 if (upperBoundSeenSoFarOpenness == i.fValue.GetLowerBoundOpenness ()) {
36 //DbgTrace ("i.fKey = %f, i.fValue = (%f,%f, ol=%d, or=%d)", i.fKey, i.fValue.GetLowerBound (), i.fValue.GetUpperBound (), i.fValue.GetLowerBoundOpenness (), i.fValue.GetUpperBoundOpenness ());
37 //DbgTrace ("return false cuz bounds openness no match: upperBoundSeenSoFarOpenness ={}, and i.fValue.GetLowerBoundOpenness ()={})"_f, upperBoundSeenSoFarOpenness, i.fValue.GetLowerBoundOpenness ());
38 return false;
39 }
40 }
41 upperBoundSeenSoFar = i.fValue.GetUpperBound ();
42 upperBoundSeenSoFarOpenness = i.fValue.GetUpperBoundOpenness ();
43 }
44 return true;
45 }
46 template <Traversal::IRange RANGETYPE>
47 inline bool IsPartition (const Iterable<RANGETYPE>& iterable)
48 {
49 return IsPartition (
50 iterable, [] (typename RANGETYPE::value_type lhs, typename RANGETYPE::value_type rhs) { return Math::NearlyEquals (lhs, rhs); });
51 }
52
53}
bool IsPartition(const Iterable< RANGETYPE > &iterable)
Definition Partition.inl:47
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237