9 #ifndef INCLUDE_TEMPLE_POSET_H
10 #define INCLUDE_TEMPLE_POSET_H
12 #include "boost/logic/tribool.hpp"
19 namespace Molassembler {
50 using const_iterator =
typename std::vector<T>::const_iterator;
52 std::vector<T> values;
57 values.push_back(std::move(value));
61 Subset(std::vector<T> passValues) : values(std::move(passValues)) {}
64 template<
typename Iter>
65 Subset(Iter begin, Iter end) : values(begin, end) {}
67 const_iterator begin()
const {
68 return std::begin(values);
71 const_iterator end()
const {
72 return std::end(values);
76 using const_iterator =
typename std::vector<Subset>::const_iterator;
82 template<
typename Container>
85 std::begin(container),
94 template<
typename Iter>
95 Poset(Iter&& begin, Iter&& end) {
97 std::forward<Iter>(begin),
98 std::forward<Iter>(end)
110 template<
typename Iter>
113 subsets.emplace_back(std::forward<Iter>(begin), std::forward<Iter>(end));
127 template<
typename Comparator>
129 auto lowerBoundComparator = [&comparator](
const Subset& set,
const T& t) ->
bool {
130 return comparator(set.values.front(), t);
133 std::vector<Subset> newSubsets;
134 newSubsets.reserve(subsets.size());
135 for(
Subset& subset : subsets) {
138 newSubsets.push_back(std::move(subset));
143 std::vector<Subset> splat;
144 for(
auto& value : subset) {
146 auto findIter = std::lower_bound(
153 if(findIter == std::end(splat)) {
155 splat.emplace_back(std::move(value));
159 if(!comparator(value, findIter->values.front())) {
161 findIter->values.push_back(std::move(value));
175 for(
auto& splatSet : splat) {
176 if(splatSet.values.size() == 1) {
177 splatSet.ordered =
true;
185 std::back_inserter(newSubsets)
190 subsets = std::move(newSubsets);
203 for(
Subset& subset : subsets) {
204 subset.ordered =
true;
208 const_iterator begin()
const {
209 return std::begin(subsets);
212 const_iterator end()
const {
213 return std::end(subsets);
221 std::string str =
"{";
222 for(
const Subset& subset : subsets) {
223 auto it = std::begin(subset);
225 str += std::to_string(*it);
226 if(++it != std::end(subset)) {
250 const_iterator
find(
const T& a)
const {
254 [&a](
const Subset& subset) ->
bool {
259 ) != std::end(subset);
279 boost::tribool
compare(
const T& a,
const T& b)
const {
280 const auto subsetEnd = std::end(subsets);
281 auto aIter =
find(a);
283 if(aIter == subsetEnd) {
284 throw std::invalid_argument(
"The first argument to compare is not in the poset");
287 auto bIter =
find(b);
289 if(bIter == subsetEnd) {
290 throw std::invalid_argument(
"The second argument to compare is not in the poset");
297 return aIter < bIter;
307 if(aIter.first->ordered) {
311 return boost::indeterminate;
321 const unsigned N = subsets.size();
326 for(
unsigned i = 0; i < N; ++i) {
327 extracted[i] = std::move(subsets[i].values);
335 std::vector<Subset> subsets;
void orderUnordered(Comparator &&comparator)
Adds order to unordered Subsets.
Definition: Poset.h:128
auto find_if(const Container &container, UnaryPredicate &&predicate)
std::find_if shorthand
Definition: Functional.h:356
Poset(Iter &&begin, Iter &&end)
Range constructor.
Definition: Poset.h:95
Subset(std::vector< T > passValues)
Vector constructor.
Definition: Poset.h:61
void setUnorderedValues(Iter &&begin, Iter &&end)
Sets the values in the specified range as the only unordered Subset.
Definition: Poset.h:111
Poset(const Container &container)
Container constructor.
Definition: Poset.h:83
std::string toString() const
Convert the Poset to a string representation for debug purposes.
Definition: Poset.h:220
Data structure for partially ordered sets with support for gradual ordering discovery.
Definition: Poset.h:44
Subset of T values.
Definition: Poset.h:49
const_iterator find(const T &a) const
Find an element.
Definition: Poset.h:250
Subset(Iter begin, Iter end)
Range constructor.
Definition: Poset.h:65
std::vector< std::vector< T > > extract()
Extracts the contained order, moving from each contained subset and clearing the Poset.
Definition: Poset.h:320
Subset(T value)
Single-value constructor.
Definition: Poset.h:56
void finalize()
Sets all subsets as ordered.
Definition: Poset.h:202
boost::tribool compare(const T &a, const T &b) const
Less-than compare two elements in the poset.
Definition: Poset.h:279
auto find(const Container &container, const T &needle)
std::find shorthand
Definition: Functional.h:346