|
| 1 | +#include "../src/numeric_range.hpp" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <iostream> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +// Helper function to print a numeric range using math-esque notation of |
| 8 | +// bound inclusivity/exclusivity. |
| 9 | +template <typename T> |
| 10 | +void print_numeric_range (std::ostream& os, const NumericRange<T>& nr) |
| 11 | +{ |
| 12 | + if (nr.lb_inclusive) |
| 13 | + os << "["; |
| 14 | + else |
| 15 | + os << "("; |
| 16 | + |
| 17 | + os << nr.lb << ", " << nr.ub; |
| 18 | + |
| 19 | + if (nr.ub_inclusive) |
| 20 | + os << "]"; |
| 21 | + else |
| 22 | + os << ")"; |
| 23 | +} |
| 24 | + |
| 25 | +int main () |
| 26 | +{ |
| 27 | + // Remember to use the custom comparator |
| 28 | + std::vector<NumericRange<double> > range_vector; |
| 29 | + |
| 30 | + // Populate the vector with some sample ranges and a mix of bound types |
| 31 | + range_vector.emplace_back(NumericRange<double>{0.0, true, 1.0, false}); |
| 32 | + range_vector.emplace_back(NumericRange<double>{3.0, false, 3.1, false}); |
| 33 | + range_vector.emplace_back(NumericRange<double>{-5.0, false, -1.0, true}); |
| 34 | + range_vector.emplace_back(NumericRange<double>{1.1, true, 2.1, true}); |
| 35 | + |
| 36 | + // Iterate through the map and print elements (unsorted) |
| 37 | + std::cout << "Unsorted elements:" << std::endl; |
| 38 | + for (auto & i : range_vector) |
| 39 | + { |
| 40 | + print_numeric_range(std::cout, i); |
| 41 | + std::cout << std::endl; |
| 42 | + } |
| 43 | + |
| 44 | + // Sort the vector using the comparator function in the library |
| 45 | + std::sort(range_vector.begin(), range_vector.end(), NumericRangeComparator<double>()); |
| 46 | + |
| 47 | + // Iterate through the map and print elements (sorted) |
| 48 | + std::cout << "Sorted elements:" << std::endl; |
| 49 | + for (auto & i : range_vector) |
| 50 | + { |
| 51 | + print_numeric_range(std::cout, i); |
| 52 | + std::cout << std::endl; |
| 53 | + } |
| 54 | + |
| 55 | + return 0; |
| 56 | +} |
0 commit comments