diff --git a/examples/getting_started_listing_01.cpp b/examples/getting_started_listing_01.cpp index d9ea6767..b7366ea7 100644 --- a/examples/getting_started_listing_01.cpp +++ b/examples/getting_started_listing_01.cpp @@ -4,80 +4,75 @@ #include int main(int, char**) { - namespace bh = boost::histogram; - using namespace bh::literals; // enables _c suffix + namespace bh = boost::histogram; + using namespace bh::literals; // enables _c suffix - /* - create a static 1d-histogram with an axis that has 6 equidistant - bins on the real line from -1.0 to 2.0, and label it as "x" - */ - auto h = bh::make_static_histogram( - bh::axis::regular<>(6, -1.0, 2.0, "x") - ); + /* + create a static 1d-histogram with an axis that has 6 equidistant + bins on the real line from -1.0 to 2.0, and label it as "x" + */ + auto h = bh::make_static_histogram(bh::axis::regular<>(6, -1.0, 2.0, "x")); - // fill histogram with data, typically this happens in a loop - // STL algorithms are supported - auto data = { -0.5, 1.1, 0.3, 1.7 }; - std::for_each(data.begin(), data.end(), h); + // fill histogram with data, typically this happens in a loop + // STL algorithms are supported + auto data = {-0.5, 1.1, 0.3, 1.7}; + std::for_each(data.begin(), data.end(), h); - /* - a regular axis is a sequence of semi-open bins; extra under- and - overflow bins extend the axis in the default configuration - index : -1 0 1 2 3 4 5 6 - bin edge: -inf -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 inf - */ - h(-1.5); // put in underflow bin -1 - h(-1.0); // put in bin 0, bin interval is semi-open - h(2.0); // put in overflow bin 6, bin interval is semi-open - h(20.0); // put in overflow bin 6 + /* + a regular axis is a sequence of semi-open bins; extra under- and + overflow bins extend the axis in the default configuration + index : -1 0 1 2 3 4 5 6 + bin edge: -inf -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 inf + */ + h(-1.5); // put in underflow bin -1 + h(-1.0); // put in bin 0, bin interval is semi-open + h(2.0); // put in overflow bin 6, bin interval is semi-open + h(20.0); // put in overflow bin 6 - /* - do a weighted fill using bh::weight, a wrapper for any type, - which may appear at the beginning of the argument list - */ - h(bh::weight(1.0), 0.1); + /* + do a weighted fill using bh::weight, a wrapper for any type, + which may appear at the beginning of the argument list + */ + h(bh::weight(1.0), 0.1); - /* - iterate over bins with a fancy histogram iterator - - order in which bins are iterated over is an implementation detail - - iterator dereferences to histogram::element_type, which is defined by - its storage class; by default something with value() and - variance() methods; the first returns the - actual count, the second returns a variance estimate of the count - (see Rationale section for what this means) - - idx(N) method returns the index of the N-th axis - - bin(N_c) method returns current bin of N-th axis; the suffx _c turns - the argument into a compile-time number, which is needed to return - different `bin_type`s for different axes - - `bin_type` usually is a semi-open interval representing the bin, whose - edges can be accessed with methods `lower()` and `upper()`, but the - implementation depends on the axis, please look it up in the reference - */ - std::cout.setf(std::ios_base::fixed); - for (auto it = h.begin(); it != h.end(); ++it) { - const auto bin = it.bin(0_c); - std::cout << "bin " << it.idx(0) << " x in [" - << std::setprecision(1) - << std::setw(4) << bin.lower() << ", " - << std::setw(4) << bin.upper() << "): " - << std::setprecision(1) - << it->value() << " +/- " - << std::setprecision(3) << std::sqrt(it->variance()) - << std::endl; - } + /* + iterate over bins with a fancy histogram iterator + - order in which bins are iterated over is an implementation detail + - iterator dereferences to histogram::element_type, which is defined by + its storage class; by default something with value() and + variance() methods; the first returns the + actual count, the second returns a variance estimate of the count + (see Rationale section for what this means) + - idx(N) method returns the index of the N-th axis + - bin(N_c) method returns current bin of N-th axis; the suffx _c turns + the argument into a compile-time number, which is needed to return + different `bin_type`s for different axes + - `bin_type` usually is a semi-open interval representing the bin, whose + edges can be accessed with methods `lower()` and `upper()`, but the + implementation depends on the axis, please look it up in the reference + */ + std::cout.setf(std::ios_base::fixed); + for (auto it = h.begin(); it != h.end(); ++it) { + const auto bin = it.bin(0_c); + std::cout << "bin " << it.idx(0) << " x in [" << std::setprecision(1) + << std::setw(4) << bin.lower() << ", " << std::setw(4) + << bin.upper() << "): " << std::setprecision(1) << it->value() + << " +/- " << std::setprecision(3) << std::sqrt(it->variance()) + << std::endl; + } - /* program output: (note that under- and overflow bins appear at the end) + /* program output: (note that under- and overflow bins appear at the end) - bin 0 x in [-1.0, -0.5): 1 +/- 1 - bin 1 x in [-0.5, 0.0): 0 +/- 0 - bin 2 x in [ 0.0, 0.5): 1 +/- 1 - bin 3 x in [ 0.5, 1.0): 0 +/- 0 - bin 4 x in [ 1.0, 1.5): 0 +/- 0 - bin 5 x in [ 1.5, 2.0): 0 +/- 0 - bin 6 x in [ 2.0, inf): 2 +/- 1.41421 - bin -1 x in [-inf, -1): 1 +/- 1 + bin 0 x in [-1.0, -0.5): 1 +/- 1 + bin 1 x in [-0.5, 0.0): 0 +/- 0 + bin 2 x in [ 0.0, 0.5): 1 +/- 1 + bin 3 x in [ 0.5, 1.0): 0 +/- 0 + bin 4 x in [ 1.0, 1.5): 0 +/- 0 + bin 5 x in [ 1.5, 2.0): 0 +/- 0 + bin 6 x in [ 2.0, inf): 2 +/- 1.41421 + bin -1 x in [-inf, -1): 1 +/- 1 - */ + */ } //] diff --git a/examples/guide_access_bin_counts.cpp b/examples/guide_access_bin_counts.cpp index 7855fb17..324fb66e 100644 --- a/examples/guide_access_bin_counts.cpp +++ b/examples/guide_access_bin_counts.cpp @@ -7,50 +7,43 @@ namespace bh = boost::histogram; int main() { - // make histogram with 2 x 2 = 4 bins (not counting under-/overflow bins) - auto h = bh::make_static_histogram( - bh::axis::regular<>(2, -1, 1), - bh::axis::regular<>(2, 2, 4) - ); + // make histogram with 2 x 2 = 4 bins (not counting under-/overflow bins) + auto h = bh::make_static_histogram(bh::axis::regular<>(2, -1, 1), + bh::axis::regular<>(2, 2, 4)); - h(bh::weight(1), -0.5, 2.5); // bin index 0, 0 - h(bh::weight(2), -0.5, 3.5); // bin index 0, 1 - h(bh::weight(3), 0.5, 2.5); // bin index 1, 0 - h(bh::weight(4), 0.5, 3.5); // bin index 1, 1 + h(bh::weight(1), -0.5, 2.5); // bin index 0, 0 + h(bh::weight(2), -0.5, 3.5); // bin index 0, 1 + h(bh::weight(3), 0.5, 2.5); // bin index 1, 0 + h(bh::weight(4), 0.5, 3.5); // bin index 1, 1 - // access count value, number of indices must match number of axes - std::cout << h.at(0, 0).value() << " " - << h.at(0, 1).value() << " " - << h.at(1, 0).value() << " " - << h.at(1, 1).value() - << std::endl; + // access count value, number of indices must match number of axes + std::cout << h.at(0, 0).value() << " " << h.at(0, 1).value() << " " + << h.at(1, 0).value() << " " << h.at(1, 1).value() << std::endl; - // prints: 1 2 3 4 + // prints: 1 2 3 4 - // access count variance, number of indices must match number of axes - std::cout << h.at(0, 0).variance() << " " - << h.at(0, 1).variance() << " " - << h.at(1, 0).variance() << " " - << h.at(1, 1).variance() - << std::endl; - // prints: 1 4 9 16 + // access count variance, number of indices must match number of axes + std::cout << h.at(0, 0).variance() << " " << h.at(0, 1).variance() << " " + << h.at(1, 0).variance() << " " << h.at(1, 1).variance() + << std::endl; + // prints: 1 4 9 16 - // you can also make a copy of the type that holds the bin count - auto c11 = h.at(1, 1); - std::cout << c11.value() << " " << c11.variance() << std::endl; - // prints: 4 16 + // you can also make a copy of the type that holds the bin count + auto c11 = h.at(1, 1); + std::cout << c11.value() << " " << c11.variance() << std::endl; + // prints: 4 16 - // histogram also supports access via container; using a container of - // wrong size trips an assertion in debug mode - auto idx = {0, 1}; - std::cout << h.at(idx).value() << std::endl; - // prints: 2 + // histogram also supports access via container; using a container of + // wrong size trips an assertion in debug mode + auto idx = {0, 1}; + std::cout << h.at(idx).value() << std::endl; + // prints: 2 - // histogram also provides extended iterators - auto sum = std::accumulate(h.begin(), h.end(), - typename decltype(h)::element_type(0)); - std::cout << sum.value() << " " << sum.variance() << std::endl; - // prints: 10 30 + // histogram also provides extended iterators + auto sum = std::accumulate(h.begin(), h.end(), + typename decltype(h)::element_type(0)); + std::cout << sum.value() << " " << sum.variance() << std::endl; + // prints: 10 30 } //] diff --git a/examples/guide_axis_with_labels.cpp b/examples/guide_axis_with_labels.cpp index e2fbfcda..2fe0dabc 100644 --- a/examples/guide_axis_with_labels.cpp +++ b/examples/guide_axis_with_labels.cpp @@ -5,13 +5,12 @@ namespace bh = boost::histogram; int main() { - // create a 2d-histogram with an "age" and an "income" axis - auto h = bh::make_static_histogram( - bh::axis::regular<>(20, 0, 100, "age in years"), - bh::axis::regular<>(20, 0, 100, "yearly income in $1000") - ); + // create a 2d-histogram with an "age" and an "income" axis + auto h = bh::make_static_histogram( + bh::axis::regular<>(20, 0, 100, "age in years"), + bh::axis::regular<>(20, 0, 100, "yearly income in $1000")); - // do something with h + // do something with h } //] diff --git a/examples/guide_axis_with_uoflow_off.cpp b/examples/guide_axis_with_uoflow_off.cpp index 786c61ea..a7b70e95 100644 --- a/examples/guide_axis_with_uoflow_off.cpp +++ b/examples/guide_axis_with_uoflow_off.cpp @@ -5,10 +5,11 @@ namespace bh = boost::histogram; int main() { - // create a 1d-histogram for dice throws with integer values from 1 to 6 - auto h = bh::make_static_histogram(bh::axis::integer<>(1, 7, "eyes", bh::axis::uoflow::off)); + // create a 1d-histogram for dice throws with integer values from 1 to 6 + auto h = bh::make_static_histogram( + bh::axis::integer<>(1, 7, "eyes", bh::axis::uoflow::off)); - // do something with h + // do something with h } //] diff --git a/examples/guide_custom_axis.cpp b/examples/guide_custom_axis.cpp index 1f140278..f037286e 100644 --- a/examples/guide_custom_axis.cpp +++ b/examples/guide_custom_axis.cpp @@ -7,36 +7,33 @@ namespace bh = boost::histogram; // custom axis, which adapts builtin integer axis struct custom_axis : public bh::axis::integer<> { - using value_type = const char*; // type that is fed to the axis + using value_type = const char*; // type that is fed to the axis - using integer::integer; // inherit ctors of base + using integer::integer; // inherit ctors of base - // the customization point - // - accept const char* and convert to int - // - then call index method of base class - int index(value_type s) const { - return integer::index(std::atoi(s)); - } + // the customization point + // - accept const char* and convert to int + // - then call index method of base class + int index(value_type s) const { return integer::index(std::atoi(s)); } }; int main() { - auto h = bh::make_static_histogram(custom_axis(0, 3)); - h("-10"); - h("0"); - h("1"); - h("9"); + auto h = bh::make_static_histogram(custom_axis(0, 3)); + h("-10"); + h("0"); + h("1"); + h("9"); - for (auto xi : h.axis()) { - std::cout << "bin " << xi.idx() << " [" << xi.lower() << ", " - << xi.upper() << ") " << h.at(xi).value() - << std::endl; - } + for (auto xi : h.axis()) { + std::cout << "bin " << xi.idx() << " [" << xi.lower() << ", " + << xi.upper() << ") " << h.at(xi).value() << std::endl; + } - /* prints: - bin 0 [0, 1) 1 - bin 1 [1, 2] 1 - bin 2 [2, 3] 0 - */ + /* prints: + bin 0 [0, 1) 1 + bin 1 [1, 2] 1 + bin 2 [2, 3] 0 + */ } //] diff --git a/examples/guide_custom_storage.cpp b/examples/guide_custom_storage.cpp index aaa4a000..0e64179b 100644 --- a/examples/guide_custom_storage.cpp +++ b/examples/guide_custom_storage.cpp @@ -6,12 +6,11 @@ namespace bh = boost::histogram; int main() { - // create static histogram with array_storage, using int as counter type - auto h = bh::make_static_histogram_with>( - bh::axis::regular<>(10, 0, 1) - ); + // create static histogram with array_storage, using int as counter type + auto h = bh::make_static_histogram_with>( + bh::axis::regular<>(10, 0, 1)); - // do something with h + // do something with h } //] diff --git a/examples/guide_fill_histogram.cpp b/examples/guide_fill_histogram.cpp index 0d4e247e..68c15c9d 100644 --- a/examples/guide_fill_histogram.cpp +++ b/examples/guide_fill_histogram.cpp @@ -1,38 +1,37 @@ //[ guide_fill_histogram #include -#include #include +#include namespace bh = boost::histogram; int main() { - auto h = bh::make_static_histogram(bh::axis::integer<>(0, 4), - bh::axis::regular<>(10, 0, 5)); + auto h = bh::make_static_histogram(bh::axis::integer<>(0, 4), + bh::axis::regular<>(10, 0, 5)); - // fill histogram, number of arguments must be equal to number of axes - h(0, 1.1); // increases bin counter by one - h(bh::weight(2), 3, 3.4); // increase bin counter by 2 instead of 1 + // fill histogram, number of arguments must be equal to number of axes + h(0, 1.1); // increases bin counter by one + h(bh::weight(2), 3, 3.4); // increase bin counter by 2 instead of 1 - // histogram also supports fills from a container of values; a container - // of wrong size trips an assertion in debug mode - auto xy1 = std::make_pair(4, 3.1); - h(xy1); - auto xy2 = std::vector({3.0, 4.9}); - h(xy2); + // histogram also supports fills from a container of values; a container + // of wrong size trips an assertion in debug mode + auto xy1 = std::make_pair(4, 3.1); + h(xy1); + auto xy2 = std::vector({3.0, 4.9}); + h(xy2); - // functional-style processing is also supported - std::vector> input_data{ - {0, 1.2}, {2, 3.4}, {4, 5.6} - }; - // Note that std::for_each takes the functor by value, thus it makes a - // potentially expensive copy of your histogram. Passing freshly created - // histograms is ok, though, because of return-value-optimization - auto h2 = std::for_each(input_data.begin(), input_data.end(), - bh::make_static_histogram( - bh::axis::integer<>(0, 4), - bh::axis::regular<>(10, 0, 5))); - // h is filled + // functional-style processing is also supported + std::vector> input_data{ + {0, 1.2}, {2, 3.4}, {4, 5.6}}; + // Note that std::for_each takes the functor by value, thus it makes a + // potentially expensive copy of your histogram. Passing freshly created + // histograms is ok, though, because of return-value-optimization + auto h2 = + std::for_each(input_data.begin(), input_data.end(), + bh::make_static_histogram(bh::axis::integer<>(0, 4), + bh::axis::regular<>(10, 0, 5))); + // h is filled } //] diff --git a/examples/guide_histogram_operators.cpp b/examples/guide_histogram_operators.cpp index 5f49d5df..1d0acb92 100644 --- a/examples/guide_histogram_operators.cpp +++ b/examples/guide_histogram_operators.cpp @@ -6,52 +6,52 @@ namespace bh = boost::histogram; int main() { - // make two histograms - auto h1 = bh::make_static_histogram(bh::axis::regular<>(2, -1, 1)); - auto h2 = bh::make_static_histogram(bh::axis::regular<>(2, -1, 1)); + // make two histograms + auto h1 = bh::make_static_histogram(bh::axis::regular<>(2, -1, 1)); + auto h2 = bh::make_static_histogram(bh::axis::regular<>(2, -1, 1)); - h1(-0.5); // counts are: 1 0 - h2(0.5); // counts are: 0 1 + h1(-0.5); // counts are: 1 0 + h2(0.5); // counts are: 0 1 - // add them - auto h3 = h1; - h3 += h2; // counts are: 1 1 + // add them + auto h3 = h1; + h3 += h2; // counts are: 1 1 - // adding multiple histograms at once is efficient and does not create - // superfluous temporaries since operator+ functions are overloaded to - // accept and return rvalue references where possible - auto h4 = h1 + h2 + h3; // counts are: 2 2 + // adding multiple histograms at once is efficient and does not create + // superfluous temporaries since operator+ functions are overloaded to + // accept and return rvalue references where possible + auto h4 = h1 + h2 + h3; // counts are: 2 2 - std::cout << h4.at(0).value() << " " << h4.at(1).value() << std::endl; - // prints: 2 2 + std::cout << h4.at(0).value() << " " << h4.at(1).value() << std::endl; + // prints: 2 2 - // multiply by number - h4 *= 2; // counts are: 4 4 + // multiply by number + h4 *= 2; // counts are: 4 4 - // divide by number - auto h5 = h4 / 4; // counts are: 1 1 + // divide by number + auto h5 = h4 / 4; // counts are: 1 1 - std::cout << h5.at(0).value() << " " << h5.at(1).value() << std::endl; - // prints: 1 1 + std::cout << h5.at(0).value() << " " << h5.at(1).value() << std::endl; + // prints: 1 1 - // compare histograms - std::cout << (h4 == 4 * h5) << " " << (h4 != h5) << std::endl; - // prints: 1 1 + // compare histograms + std::cout << (h4 == 4 * h5) << " " << (h4 != h5) << std::endl; + // prints: 1 1 - // note: special effect of multiplication on counter variance - auto h = bh::make_static_histogram(bh::axis::regular<>(2, -1, 1)); - h(-0.5); // counts are: 1 0 - std::cout << "value " << (2 * h).at(0).value() - << " " << (h + h).at(0).value() << "\n" - << "variance " << (2 * h).at(0).variance() - << " " << (h + h).at(0).variance() << std::endl; - // equality operator also checks variances, so the statement is false - std::cout << (h + h == 2 * h) << std::endl; - /* prints: - value 2 2 - variance 4 2 - 0 - */ + // note: special effect of multiplication on counter variance + auto h = bh::make_static_histogram(bh::axis::regular<>(2, -1, 1)); + h(-0.5); // counts are: 1 0 + std::cout << "value " << (2 * h).at(0).value() << " " + << (h + h).at(0).value() << "\n" + << "variance " << (2 * h).at(0).variance() << " " + << (h + h).at(0).variance() << std::endl; + // equality operator also checks variances, so the statement is false + std::cout << (h + h == 2 * h) << std::endl; + /* prints: + value 2 2 + variance 4 2 + 0 + */ } //] diff --git a/examples/guide_histogram_reduction.cpp b/examples/guide_histogram_reduction.cpp index 498d08b6..43670ebc 100644 --- a/examples/guide_histogram_reduction.cpp +++ b/examples/guide_histogram_reduction.cpp @@ -7,56 +7,51 @@ namespace bh = boost::histogram; // example of a generic function for histograms, this one sums all entries template -typename bh::histogram::element_type sum(const bh::histogram& h) { - auto result = typename bh::histogram::element_type(0); - for (auto x : h) - result += x; - return result; +typename bh::histogram::element_type sum( + const bh::histogram& h) { + auto result = typename bh::histogram::element_type(0); + for (auto x : h) result += x; + return result; } int main() { - using namespace bh::literals; // enables _c suffix + using namespace bh::literals; // enables _c suffix - // make a 2d histogram - auto h = bh::make_static_histogram(bh::axis::regular<>(3, -1, 1), - bh::axis::integer<>(0, 4)); + // make a 2d histogram + auto h = bh::make_static_histogram(bh::axis::regular<>(3, -1, 1), + bh::axis::integer<>(0, 4)); - h(-0.9, 0); - h(0.9, 3); - h(0.1, 2); + h(-0.9, 0); + h(0.9, 3); + h(0.1, 2); - auto hr0 = h.reduce_to(0_c); // keep only first axis - auto hr1 = h.reduce_to(1_c); // keep only second axis + auto hr0 = h.reduce_to(0_c); // keep only first axis + auto hr1 = h.reduce_to(1_c); // keep only second axis - /* - reduce does not remove counts; returned histograms are summed over - the removed axes, so h, hr0, and hr1 have same number of total counts - */ - std::cout << sum(h).value() << " " - << sum(hr0).value() << " " - << sum(hr1).value() << std::endl; - // prints: 3 3 3 + /* + reduce does not remove counts; returned histograms are summed over + the removed axes, so h, hr0, and hr1 have same number of total counts + */ + std::cout << sum(h).value() << " " << sum(hr0).value() << " " + << sum(hr1).value() << std::endl; + // prints: 3 3 3 - for (auto yi : h.axis(1_c)) { - for (auto xi : h.axis(0_c)) { - std::cout << h.at(xi, yi).value() << " "; - } - std::cout << std::endl; - } - // prints: 1 0 0 - // 0 0 0 - // 0 1 0 - // 0 0 1 - - for (auto xi : hr0.axis()) - std::cout << hr0.at(xi).value() << " "; + for (auto yi : h.axis(1_c)) { + for (auto xi : h.axis(0_c)) { std::cout << h.at(xi, yi).value() << " "; } std::cout << std::endl; - // prints: 1 1 1 + } + // prints: 1 0 0 + // 0 0 0 + // 0 1 0 + // 0 0 1 - for (auto yi : hr1.axis()) - std::cout << hr1.at(yi).value() << " "; - std::cout << std::endl; - // prints: 1 0 1 1 + for (auto xi : hr0.axis()) std::cout << hr0.at(xi).value() << " "; + std::cout << std::endl; + // prints: 1 1 1 + + for (auto yi : hr1.axis()) std::cout << hr1.at(yi).value() << " "; + std::cout << std::endl; + // prints: 1 0 1 1 } //] diff --git a/examples/guide_histogram_serialization.cpp b/examples/guide_histogram_serialization.cpp index 754297bc..7f6faffe 100644 --- a/examples/guide_histogram_serialization.cpp +++ b/examples/guide_histogram_serialization.cpp @@ -1,42 +1,42 @@ //[ guide_histogram_serialization -#include -#include // includes serialization code #include #include +#include +#include // includes serialization code #include namespace bh = boost::histogram; int main() { - auto a = bh::make_static_histogram(bh::axis::regular<>(3, -1, 1, "r"), - bh::axis::integer<>(0, 2, "i")); - a(0.5, 1); + auto a = bh::make_static_histogram(bh::axis::regular<>(3, -1, 1, "r"), + bh::axis::integer<>(0, 2, "i")); + a(0.5, 1); - std::string buf; // holds persistent representation + std::string buf; // holds persistent representation - // store histogram - { - std::ostringstream os; - boost::archive::text_oarchive oa(os); - oa << a; - buf = os.str(); - } + // store histogram + { + std::ostringstream os; + boost::archive::text_oarchive oa(os); + oa << a; + buf = os.str(); + } - auto b = decltype(a)(); // create a default-constructed second histogram + auto b = decltype(a)(); // create a default-constructed second histogram - std::cout << "before restore " << (a == b) << std::endl; - // prints: before restore 0 + std::cout << "before restore " << (a == b) << std::endl; + // prints: before restore 0 - // load histogram - { - std::istringstream is(buf); - boost::archive::text_iarchive ia(is); - ia >> b; - } + // load histogram + { + std::istringstream is(buf); + boost::archive::text_iarchive ia(is); + ia >> b; + } - std::cout << "after restore " << (a == b) << std::endl; - // prints: after restore 1 + std::cout << "after restore " << (a == b) << std::endl; + // prints: after restore 1 } //] diff --git a/examples/guide_histogram_streaming.cpp b/examples/guide_histogram_streaming.cpp index fb357499..e49267e5 100644 --- a/examples/guide_histogram_streaming.cpp +++ b/examples/guide_histogram_streaming.cpp @@ -7,33 +7,32 @@ namespace bh = boost::histogram; int main() { - namespace axis = boost::histogram::axis; + namespace axis = boost::histogram::axis; - enum { A, B, C }; + enum { A, B, C }; - auto h = bh::make_static_histogram( - axis::regular<>(2, -1, 1, "regular1", axis::uoflow::off), - axis::regular(2, 1, 10, "regular2"), - axis::circular<>(4, 0.1, 1.0, "polar"), - axis::variable<>({-1, 0, 1}, "variable", axis::uoflow::off), - axis::category<>({A, B, C}, "category"), - axis::integer<>(-1, 1, "integer", axis::uoflow::off) - ); + auto h = bh::make_static_histogram( + axis::regular<>(2, -1, 1, "regular1", axis::uoflow::off), + axis::regular(2, 1, 10, "regular2"), + axis::circular<>(4, 0.1, 1.0, "polar"), + axis::variable<>({-1, 0, 1}, "variable", axis::uoflow::off), + axis::category<>({A, B, C}, "category"), + axis::integer<>(-1, 1, "integer", axis::uoflow::off)); - std::cout << h << std::endl; + std::cout << h << std::endl; - /* prints: + /* prints: - histogram( - regular(2, -1, 1, label='regular1', uoflow=False), - regular_log(2, 1, 10, label='regular2'), - circular(4, phase=0.1, perimeter=1, label='polar'), - variable(-1, 0, 1, label='variable', uoflow=False), - category(0, 1, 2, label='category'), - integer(-1, 1, label='integer', uoflow=False), - ) + histogram( + regular(2, -1, 1, label='regular1', uoflow=False), + regular_log(2, 1, 10, label='regular2'), + circular(4, phase=0.1, perimeter=1, label='polar'), + variable(-1, 0, 1, label='variable', uoflow=False), + category(0, 1, 2, label='category'), + integer(-1, 1, label='integer', uoflow=False), + ) - */ + */ } //] diff --git a/examples/guide_make_dynamic_histogram.cpp b/examples/guide_make_dynamic_histogram.cpp index 39cfb7c4..2503fdee 100644 --- a/examples/guide_make_dynamic_histogram.cpp +++ b/examples/guide_make_dynamic_histogram.cpp @@ -6,15 +6,15 @@ namespace bh = boost::histogram; int main() { - // create vector of axes, axis::any is a polymorphic axis type - auto v = std::vector(); - v.push_back(bh::axis::regular<>(100, -1, 1)); - v.push_back(bh::axis::integer<>(1, 7)); + // create vector of axes, axis::any is a polymorphic axis type + auto v = std::vector(); + v.push_back(bh::axis::regular<>(100, -1, 1)); + v.push_back(bh::axis::integer<>(1, 7)); - // create dynamic histogram (make_static_histogram be used with iterators) - auto h = bh::make_dynamic_histogram(v.begin(), v.end()); + // create dynamic histogram (make_static_histogram be used with iterators) + auto h = bh::make_dynamic_histogram(v.begin(), v.end()); - // do something with h + // do something with h } //] diff --git a/examples/guide_make_static_histogram.cpp b/examples/guide_make_static_histogram.cpp index 6e353813..35ac0468 100644 --- a/examples/guide_make_static_histogram.cpp +++ b/examples/guide_make_static_histogram.cpp @@ -5,14 +5,14 @@ namespace bh = boost::histogram; int main() { - /* - create a 1d-histogram in default configuration which - covers the real line from -1 to 1 in 100 bins, the same - call with `make_dynamic_histogram` would also work - */ - auto h = bh::make_static_histogram(bh::axis::regular<>(100, -1, 1)); + /* + create a 1d-histogram in default configuration which + covers the real line from -1 to 1 in 100 bins, the same + call with `make_dynamic_histogram` would also work + */ + auto h = bh::make_static_histogram(bh::axis::regular<>(100, -1, 1)); - // do something with h + // do something with h } //] diff --git a/examples/guide_mixed_cpp_python.cpp b/examples/guide_mixed_cpp_python.cpp index 9662b63a..a2c05ebc 100644 --- a/examples/guide_mixed_cpp_python.cpp +++ b/examples/guide_mixed_cpp_python.cpp @@ -1,7 +1,7 @@ //[ guide_mixed_cpp_python_part_cpp -#include #include +#include namespace bh = boost::histogram; namespace bp = boost::python; @@ -9,13 +9,10 @@ namespace bp = boost::python; // function that runs in C++ and accepts reference to dynamic histogram void process(bh::dynamic_histogram<>& h) { // fill histogram, in reality this would be arbitrarily complex code - for (int i = 0; i < 4; ++i) - h(0.25 * i, i); + for (int i = 0; i < 4; ++i) h(0.25 * i, i); } // a minimal Python module, which exposes the process function to Python -BOOST_PYTHON_MODULE(cpp_filler) { - bp::def("process", process); -} +BOOST_PYTHON_MODULE(cpp_filler) { bp::def("process", process); } //] diff --git a/include/boost/histogram/arithmetic_operators.hpp b/include/boost/histogram/arithmetic_operators.hpp index f0913872..1aa9d1e5 100644 --- a/include/boost/histogram/arithmetic_operators.hpp +++ b/include/boost/histogram/arithmetic_operators.hpp @@ -20,7 +20,8 @@ histogram&& operator+(histogram&& a, } template -histogram&& operator+(histogram&& a, histogram&& b) { +histogram&& operator+(histogram&& a, + histogram&& b) { a += b; return std::move(a); } diff --git a/include/boost/histogram/axis/base.hpp b/include/boost/histogram/axis/base.hpp index 91c4bdf0..ded83401 100644 --- a/include/boost/histogram/axis/base.hpp +++ b/include/boost/histogram/axis/base.hpp @@ -28,7 +28,7 @@ enum class uoflow { off = 0, only_oflow = 1, on = 2 }; /// Base class for all axes class base { - public: +public: /// Returns the number of bins without overflow/underflow. int size() const noexcept { return size_; } /// Returns the number of bins, including overflow/underflow if enabled. @@ -40,14 +40,12 @@ class base { /// Change the label of an axis. void label(string_view label) { label_.assign(label.begin(), label.end()); } - protected: +protected: base(unsigned size, string_view label, axis::uoflow uo) : size_(size), shape_(size + static_cast(uo)), label_(label.begin(), label.end()) { - if (size_ == 0) { - throw std::invalid_argument("bins > 0 required"); - } + if (size_ == 0) { throw std::invalid_argument("bins > 0 required"); } } base() = default; @@ -73,7 +71,7 @@ class base { return size_ == rhs.size_ && shape_ == rhs.shape_ && label_ == rhs.label_; } - private: +private: int size_ = 0, shape_ = 0; std::string label_; @@ -85,7 +83,7 @@ class base { /// Iterator mixin, uses CRTP to inject iterator logic into Derived. template class iterator_mixin { - public: +public: using const_iterator = iterator_over; using const_reverse_iterator = reverse_iterator_over; diff --git a/include/boost/histogram/axis/interval_view.hpp b/include/boost/histogram/axis/interval_view.hpp index 87ca3db4..ecea5329 100644 --- a/include/boost/histogram/axis/interval_view.hpp +++ b/include/boost/histogram/axis/interval_view.hpp @@ -16,7 +16,7 @@ namespace axis { template class interval_view { - public: +public: interval_view(int idx, const Axis& axis) : idx_(idx), axis_(axis) {} interval_view(const interval_view&) = default; @@ -32,7 +32,9 @@ class interval_view { auto upper() const noexcept -> decltype(std::declval().lower(0)) { return axis_.lower(idx_ + 1); } - typename Axis::value_type width() const noexcept { return upper() - lower(); } + typename Axis::value_type width() const noexcept { + return upper() - lower(); + } bool operator==(const interval_view& rhs) const noexcept { return idx_ == rhs.idx_ && axis_ == rhs.axis_; @@ -43,7 +45,7 @@ class interval_view { explicit operator int() const noexcept { return idx_; } - private: +private: const int idx_; const Axis& axis_; }; diff --git a/include/boost/histogram/axis/iterator.hpp b/include/boost/histogram/axis/iterator.hpp index a2eb0668..b4ca35b3 100644 --- a/include/boost/histogram/axis/iterator.hpp +++ b/include/boost/histogram/axis/iterator.hpp @@ -14,17 +14,18 @@ namespace histogram { namespace axis { template -class iterator_over : public iterator_facade, - typename Axis::bin_type, - random_access_traversal_tag, - typename Axis::bin_type> { - public: - explicit iterator_over(const Axis& axis, int idx) : axis_(axis), idx_(idx) {} +class iterator_over + : public iterator_facade, typename Axis::bin_type, + random_access_traversal_tag, + typename Axis::bin_type> { +public: + explicit iterator_over(const Axis& axis, int idx) + : axis_(axis), idx_(idx) {} iterator_over(const iterator_over&) = default; iterator_over& operator=(const iterator_over&) = default; - protected: +protected: void increment() noexcept { ++idx_; } void decrement() noexcept { --idx_; } void advance(int n) noexcept { idx_ += n; } @@ -43,18 +44,17 @@ class iterator_over : public iterator_facade, template class reverse_iterator_over - : public iterator_facade, - typename Axis::bin_type, - random_access_traversal_tag, - typename Axis::bin_type> { - public: + : public iterator_facade< + reverse_iterator_over, typename Axis::bin_type, + random_access_traversal_tag, typename Axis::bin_type> { +public: explicit reverse_iterator_over(const Axis& axis, int idx) : axis_(axis), idx_(idx) {} reverse_iterator_over(const reverse_iterator_over&) = default; reverse_iterator_over& operator=(const reverse_iterator_over&) = default; - protected: +protected: void increment() noexcept { --idx_; } void decrement() noexcept { ++idx_; } void advance(int n) noexcept { idx_ -= n; } diff --git a/include/boost/histogram/axis/ostream_operators.hpp b/include/boost/histogram/axis/ostream_operators.hpp index b8ca4906..2407b91a 100644 --- a/include/boost/histogram/axis/ostream_operators.hpp +++ b/include/boost/histogram/axis/ostream_operators.hpp @@ -20,15 +20,9 @@ namespace histogram { namespace axis { namespace detail { -inline string_view to_string(const transform::identity&) { - return {}; -} -inline string_view to_string(const transform::log&) { - return {"_log", 4}; -} -inline string_view to_string(const transform::sqrt&) { - return {"_sqrt", 5}; -} +inline string_view to_string(const transform::identity&) { return {}; } +inline string_view to_string(const transform::log&) { return {"_log", 4}; } +inline string_view to_string(const transform::sqrt&) { return {"_sqrt", 5}; } } // namespace detail template @@ -52,36 +46,30 @@ inline std::ostream& operator<<(std::ostream& os, os << ", label="; ::boost::histogram::detail::escape(os, a.label()); } - if (!a.uoflow()) { - os << ", uoflow=False"; - } + if (!a.uoflow()) { os << ", uoflow=False"; } os << ")"; return os; } template inline std::ostream& operator<<( - std::ostream& os, - const regular& a) { + std::ostream& os, const regular& a) { os << "regular_pow(" << a.size() << ", " << a[0].lower() << ", " << a[a.size()].lower() << ", " << a.transform().power; if (!a.label().empty()) { os << ", label="; ::boost::histogram::detail::escape(os, a.label()); } - if (!a.uoflow()) { - os << ", uoflow=False"; - } + if (!a.uoflow()) { os << ", uoflow=False"; } os << ")"; return os; } template -inline std::ostream& operator<<(std::ostream& os, const circular& a) { +inline std::ostream& operator<<(std::ostream& os, + const circular& a) { os << "circular(" << a.size(); - if (a.phase() != 0.0) { - os << ", phase=" << a.phase(); - } + if (a.phase() != 0.0) { os << ", phase=" << a.phase(); } if (a.perimeter() != RealType(::boost::histogram::detail::two_pi)) { os << ", perimeter=" << a.perimeter(); } @@ -94,18 +82,15 @@ inline std::ostream& operator<<(std::ostream& os, const circular& a) { } template -inline std::ostream& operator<<(std::ostream& os, const variable& a) { +inline std::ostream& operator<<(std::ostream& os, + const variable& a) { os << "variable(" << a[0].lower(); - for (int i = 1; i <= a.size(); ++i) { - os << ", " << a[i].lower(); - } + for (int i = 1; i <= a.size(); ++i) { os << ", " << a[i].lower(); } if (!a.label().empty()) { os << ", label="; ::boost::histogram::detail::escape(os, a.label()); } - if (!a.uoflow()) { - os << ", uoflow=False"; - } + if (!a.uoflow()) { os << ", uoflow=False"; } os << ")"; return os; } @@ -117,9 +102,7 @@ inline std::ostream& operator<<(std::ostream& os, const integer& a) { os << ", label="; ::boost::histogram::detail::escape(os, a.label()); } - if (!a.uoflow()) { - os << ", uoflow=False"; - } + if (!a.uoflow()) { os << ", uoflow=False"; } os << ")"; return os; } diff --git a/include/boost/histogram/axis/types.hpp b/include/boost/histogram/axis/types.hpp index 744ac154..b9562006 100644 --- a/include/boost/histogram/axis/types.hpp +++ b/include/boost/histogram/axis/types.hpp @@ -97,7 +97,7 @@ struct pow { return power == other.power; } - private: +private: friend ::boost::serialization::access; template void serialize(Archive&, unsigned); @@ -114,7 +114,7 @@ template class regular : public base, public iterator_mixin>, Transform { - public: +public: using value_type = RealType; using bin_type = interval_view; @@ -127,11 +127,8 @@ class regular : public base, * \param uoflow whether to add under-/overflow bins. * \param trans arguments passed to the transform. */ - regular(unsigned n, - value_type lower, - value_type upper, - string_view label = {}, - axis::uoflow uo = axis::uoflow::on, + regular(unsigned n, value_type lower, value_type upper, + string_view label = {}, axis::uoflow uo = axis::uoflow::on, Transform trans = Transform()) : base(n, label, uo), Transform(trans), @@ -177,8 +174,8 @@ class regular : public base, bin_type operator[](int idx) const noexcept { return bin_type(idx, *this); } bool operator==(const regular& o) const noexcept { - return base::operator==(o) && Transform::operator==(o) && min_ == o.min_ && - delta_ == o.delta_; + return base::operator==(o) && Transform::operator==(o) && + min_ == o.min_ && delta_ == o.delta_; } /// Access properties of the transform. @@ -186,7 +183,7 @@ class regular : public base, return static_cast(*this); } - private: +private: value_type min_ = 0.0, delta_ = 1.0; friend class ::boost::serialization::access; @@ -202,7 +199,7 @@ class regular : public base, */ template class circular : public base, public iterator_mixin> { - public: +public: using value_type = RealType; using bin_type = interval_view; @@ -213,8 +210,7 @@ class circular : public base, public iterator_mixin> { * \param perimeter range after which value wraps around. * \param label description of the axis. */ - explicit circular(unsigned n, - value_type phase = 0.0, + explicit circular(unsigned n, value_type phase = 0.0, value_type perimeter = boost::histogram::detail::two_pi, string_view label = {}) : base(n, label, axis::uoflow::off), @@ -230,7 +226,8 @@ class circular : public base, public iterator_mixin> { /// Returns the bin index for the passed argument. int index(value_type x) const noexcept { const value_type z = (x - phase_) / perimeter_; - const int i = static_cast(std::floor(z * base::size())) % base::size(); + const int i = + static_cast(std::floor(z * base::size())) % base::size(); return i + (i < 0) * base::size(); } @@ -250,7 +247,7 @@ class circular : public base, public iterator_mixin> { value_type perimeter() const { return perimeter_; } value_type phase() const { return phase_; } - private: +private: value_type phase_ = 0.0, perimeter_ = 1.0; friend class ::boost::serialization::access; @@ -265,7 +262,7 @@ class circular : public base, public iterator_mixin> { */ template class variable : public base, public iterator_mixin> { - public: +public: using value_type = RealType; using bin_type = interval_view; @@ -275,8 +272,7 @@ class variable : public base, public iterator_mixin> { * \param label description of the axis. * \param uoflow whether to add under-/overflow bins. */ - variable(std::initializer_list x, - string_view label = {}, + variable(std::initializer_list x, string_view label = {}, axis::uoflow uo = axis::uoflow::on) : base(x.size() - 1, label, uo), x_(new value_type[x.size()]) { if (x.size() >= 2) { @@ -288,9 +284,7 @@ class variable : public base, public iterator_mixin> { } template - variable(Iterator begin, - Iterator end, - string_view label = {}, + variable(Iterator begin, Iterator end, string_view label = {}, axis::uoflow uo = axis::uoflow::on) : base(std::distance(begin, end) - 1, label, uo), x_(new value_type[std::distance(begin, end)]) { @@ -299,7 +293,8 @@ class variable : public base, public iterator_mixin> { } variable() = default; - variable(const variable& o) : base(o), x_(new value_type[base::size() + 1]) { + variable(const variable& o) + : base(o), x_(new value_type[base::size() + 1]) { std::copy(o.x_.get(), o.x_.get() + base::size() + 1, x_.get()); } variable& operator=(const variable& o) { @@ -321,9 +316,7 @@ class variable : public base, public iterator_mixin> { /// Returns the starting edge of the bin. value_type lower(int i) const noexcept { - if (i < 0) { - return -std::numeric_limits::infinity(); - } + if (i < 0) { return -std::numeric_limits::infinity(); } if (i > base::size()) { return std::numeric_limits::infinity(); } @@ -333,13 +326,11 @@ class variable : public base, public iterator_mixin> { bin_type operator[](int idx) const noexcept { return bin_type(idx, *this); } bool operator==(const variable& o) const noexcept { - if (!base::operator==(o)) { - return false; - } + if (!base::operator==(o)) { return false; } return std::equal(x_.get(), x_.get() + base::size() + 1, o.x_.get()); } - private: +private: std::unique_ptr x_; // smaller size compared to std::vector friend class ::boost::serialization::access; @@ -354,7 +345,7 @@ class variable : public base, public iterator_mixin> { */ template class integer : public base, public iterator_mixin> { - public: +public: using value_type = IntType; using bin_type = interval_view; @@ -365,9 +356,7 @@ class integer : public base, public iterator_mixin> { * \param label description of the axis. * \param uoflow whether to add under-/overflow bins. */ - integer(value_type lower, - value_type upper, - string_view label = {}, + integer(value_type lower, value_type upper, string_view label = {}, axis::uoflow uo = axis::uoflow::on) : base(upper - lower, label, uo), min_(lower) { if (!(lower < upper)) { @@ -389,12 +378,8 @@ class integer : public base, public iterator_mixin> { /// Returns lower edge of the integral bin. value_type lower(int i) const noexcept { - if (i < 0) { - return -std::numeric_limits::max(); - } - if (i > base::size()) { - return std::numeric_limits::max(); - } + if (i < 0) { return -std::numeric_limits::max(); } + if (i > base::size()) { return std::numeric_limits::max(); } return min_ + i; } @@ -404,7 +389,7 @@ class integer : public base, public iterator_mixin> { return base::operator==(o) && min_ == o.min_; } - private: +private: value_type min_ = 0; friend class ::boost::serialization::access; @@ -423,7 +408,7 @@ template class category : public base, public iterator_mixin> { using map_type = bimap; - public: +public: using value_type = T; using bin_type = value_view; @@ -446,10 +431,8 @@ class category : public base, public iterator_mixin> { category(std::initializer_list seq, string_view label = {}) : base(seq.size(), label, axis::uoflow::off), map_(new map_type()) { int index = 0; - for (const auto& x : seq) - map_->insert({x, index++}); - if (index == 0) - throw std::invalid_argument("sequence is empty"); + for (const auto& x : seq) map_->insert({x, index++}); + if (index == 0) throw std::invalid_argument("sequence is empty"); } template > { : base(std::distance(begin, end), label, axis::uoflow::off), map_(new map_type()) { int index = 0; - while (begin != end) - map_->insert({*begin++, index++}); - if (index == 0) - throw std::invalid_argument("iterator range is empty"); + while (begin != end) map_->insert({*begin++, index++}); + if (index == 0) throw std::invalid_argument("iterator range is empty"); } /// Returns the bin index for the passed argument. int index(const value_type& x) const noexcept { auto it = map_->left.find(x); - if (it == map_->left.end()) - return base::size(); + if (it == map_->left.end()) return base::size(); return it->second; } @@ -487,7 +467,7 @@ class category : public base, public iterator_mixin> { std::equal(map_->begin(), map_->end(), o.map_->begin()); } - private: +private: std::unique_ptr map_; friend class ::boost::serialization::access; diff --git a/include/boost/histogram/axis/value_view.hpp b/include/boost/histogram/axis/value_view.hpp index 5c0626c2..5a259b01 100644 --- a/include/boost/histogram/axis/value_view.hpp +++ b/include/boost/histogram/axis/value_view.hpp @@ -16,7 +16,7 @@ namespace axis { template class value_view { - public: +public: value_view(int idx, const Axis& axis) : idx_(idx), axis_(axis) {} value_view(const value_view&) = default; @@ -39,7 +39,7 @@ class value_view { explicit operator int() const noexcept { return idx_; } - private: +private: const int idx_; const Axis& axis_; }; diff --git a/include/boost/histogram/detail/axis_visitor.hpp b/include/boost/histogram/detail/axis_visitor.hpp index 7a221885..74bed225 100644 --- a/include/boost/histogram/detail/axis_visitor.hpp +++ b/include/boost/histogram/detail/axis_visitor.hpp @@ -59,15 +59,13 @@ struct axes_assign_vecvar_tuple { }; template -inline bool axes_equal_impl(mp11::mp_true, - const std::tuple& t, +inline bool axes_equal_impl(mp11::mp_true, const std::tuple& t, const std::tuple& u) { return t == u; } template -inline bool axes_equal_impl(mp11::mp_false, - const std::tuple&, +inline bool axes_equal_impl(mp11::mp_false, const std::tuple&, const std::tuple&) { return false; } @@ -75,27 +73,28 @@ inline bool axes_equal_impl(mp11::mp_false, } // namespace template -inline bool axes_equal(const std::tuple& t, const std::tuple& u) { +inline bool axes_equal(const std::tuple& t, + const std::tuple& u) { return axes_equal_impl( mp11::mp_same, mp11::mp_list>(), t, u); } template inline void axes_assign(std::tuple& t, const std::tuple& u) { - static_assert(std::is_same, mp11::mp_list>::value, - "cannot assign incompatible axes"); + static_assert( + std::is_same, mp11::mp_list>::value, + "cannot assign incompatible axes"); t = u; } template inline bool axes_equal(const std::tuple& t, const std::vector>& u) { - if (sizeof...(Ts) != u.size()) - return false; + if (sizeof...(Ts) != u.size()) return false; bool equal = true; auto fn = - axes_equal_tuple_vecvar, std::vector>>( - equal, t, u); + axes_equal_tuple_vecvar, + std::vector>>(equal, t, u); mp11::mp_for_each>(fn); return equal; } @@ -126,11 +125,9 @@ inline void axes_assign(std::vector>& t, template inline bool axes_equal(const std::vector>& t, const std::vector>& u) { - if (t.size() != u.size()) - return false; + if (t.size() != u.size()) return false; for (std::size_t i = 0; i < t.size(); ++i) { - if (t[i] != u[i]) - return false; + if (t[i] != u[i]) return false; } return true; } @@ -138,9 +135,7 @@ inline bool axes_equal(const std::vector>& t, template inline void axes_assign(std::vector>& t, const std::vector>& u) { - for (std::size_t i = 0; i < t.size(); ++i) { - t[i] = u[i]; - } + for (std::size_t i = 0; i < t.size(); ++i) { t[i] = u[i]; } } struct field_count_visitor : public static_visitor { diff --git a/include/boost/histogram/detail/index_cache.hpp b/include/boost/histogram/detail/index_cache.hpp index e606ad2d..1d22c06b 100644 --- a/include/boost/histogram/detail/index_cache.hpp +++ b/include/boost/histogram/detail/index_cache.hpp @@ -7,8 +7,8 @@ #ifndef _BOOST_HISTOGRAM_DETAIL_INDEX_CACHE_HPP_ #define _BOOST_HISTOGRAM_DETAIL_INDEX_CACHE_HPP_ -#include #include +#include namespace boost { namespace histogram { @@ -62,8 +62,7 @@ struct index_cache { } void operator()(std::size_t idx) { - if (idx == idx_) - return; + if (idx == idx_) return; idx_ = idx; auto dim_ptr = dims_.get(); auto dim = dim_; diff --git a/include/boost/histogram/detail/index_mapper.hpp b/include/boost/histogram/detail/index_mapper.hpp index 3fa7c928..1d58a82d 100644 --- a/include/boost/histogram/detail/index_mapper.hpp +++ b/include/boost/histogram/detail/index_mapper.hpp @@ -32,8 +32,9 @@ struct index_mapper { s1 *= ni; ++bi; } - std::sort(dims.begin(), dims.end(), - [](const dim& a, const dim& b) { return a.stride1 > b.stride1; }); + std::sort(dims.begin(), dims.end(), [](const dim& a, const dim& b) { + return a.stride1 > b.stride1; + }); nfirst = s1; } @@ -49,7 +50,7 @@ struct index_mapper { return first < nfirst; } - private: +private: std::size_t nfirst; struct dim { std::size_t stride1, stride2; diff --git a/include/boost/histogram/detail/meta.hpp b/include/boost/histogram/detail/meta.hpp index a9b9b2fd..a1f75ebb 100644 --- a/include/boost/histogram/detail/meta.hpp +++ b/include/boost/histogram/detail/meta.hpp @@ -55,16 +55,14 @@ struct no_container_tag {}; template using classify_container = typename std::conditional< - is_static_container::value, - static_container_tag, + is_static_container::value, static_container_tag, typename std::conditional::value, dynamic_container_tag, no_container_tag>::type>::type; -template ().size(), - std::declval().increase(0), - std::declval()[0])> +template ().size(), + std::declval().increase(0), + std::declval()[0])> struct requires_storage {}; template -using requires_axis = decltype(std::declval().size(), - std::declval().shape(), - std::declval().uoflow(), - std::declval().label(), - std::declval()[0]); +using requires_axis = + decltype(std::declval().size(), std::declval().shape(), + std::declval().uoflow(), std::declval().label(), + std::declval()[0]); namespace { struct bool_mask_impl { @@ -108,13 +105,11 @@ using mp_at_c = mp11::mp_at_c, D>; template using copy_qualifiers = mp11::mp_if< - std::is_rvalue_reference, - T2&&, + std::is_rvalue_reference, T2&&, mp11::mp_if< std::is_lvalue_reference, mp11::mp_if::type>, - const T2&, - T2&>, + const T2&, T2&>, mp11::mp_if, const T2, T2>>>; template diff --git a/include/boost/histogram/detail/utility.hpp b/include/boost/histogram/detail/utility.hpp index d8cc853a..99683eeb 100644 --- a/include/boost/histogram/detail/utility.hpp +++ b/include/boost/histogram/detail/utility.hpp @@ -41,11 +41,8 @@ inline void escape(std::ostream& os, const string_view s) { // the following is highly optimized code that runs in a hot loop; // please measure the performance impact of changes -inline void lin(std::size_t& out, - std::size_t& stride, - const int axis_size, - const int axis_shape, - int j) noexcept { +inline void lin(std::size_t& out, std::size_t& stride, const int axis_size, + const int axis_shape, int j) noexcept { BOOST_ASSERT_MSG(stride == 0 || (-1 <= j && j <= axis_size), "index must be in bounds for this algorithm"); j += (j < 0) * (axis_size + 2); // wrap around if j < 0 @@ -53,7 +50,8 @@ inline void lin(std::size_t& out, #ifndef _MSC_VER #pragma GCC diagnostic ignored "-Wstrict-overflow" #endif - stride *= (j < axis_shape) * axis_shape; // stride == 0 indicates out-of-range + stride *= + (j < axis_shape) * axis_shape; // stride == 0 indicates out-of-range } template @@ -84,8 +82,7 @@ inline void fill_storage(S& s, std::size_t idx) { template inline auto storage_get(const S& s, std::size_t idx, bool error) -> typename S::const_reference { - if (error) - throw std::out_of_range("bin index out of range"); + if (error) throw std::out_of_range("bin index out of range"); return s[idx]; } diff --git a/include/boost/histogram/dynamic_histogram.hpp b/include/boost/histogram/dynamic_histogram.hpp index ef2e9b06..07831492 100644 --- a/include/boost/histogram/dynamic_histogram.hpp +++ b/include/boost/histogram/dynamic_histogram.hpp @@ -54,7 +54,7 @@ template class histogram { static_assert(mp11::mp_size::value > 0, "at least one axis required"); - public: +public: using any_axis_type = mp11::mp_rename; using axes_type = std::vector; using element_type = typename Storage::element_type; @@ -62,15 +62,14 @@ class histogram { using const_iterator = iterator_over; using iterator = const_iterator; - public: +public: histogram() = default; histogram(const histogram&) = default; histogram(histogram&&) = default; histogram& operator=(const histogram&) = default; histogram& operator=(histogram&&) = default; - template > explicit histogram(Axis0&& axis0, Axis&&... axis) : axes_({any_axis_type(std::forward(axis0)), @@ -161,9 +160,7 @@ class histogram { "(did you use weight() in the wrong place?)"); std::size_t idx = 0, stride = 1; xlin<0>(idx, stride, std::forward(ts)...); - if (stride) { - detail::fill_storage(storage_, idx); - } + if (stride) { detail::fill_storage(storage_, idx); } } template @@ -183,9 +180,7 @@ class histogram { "fill arguments does not match histogram dimension"); std::size_t idx = 0, stride = 1; xlin<0>(idx, stride, std::forward(ts)...); - if (stride) { - detail::fill_storage(storage_, idx, std::move(w)); - } + if (stride) { detail::fill_storage(storage_, idx, std::move(w)); } } template @@ -260,8 +255,7 @@ class histogram { template histogram reduce_to(int n, Ts... ts) const { std::vector b(dim(), false); - for (const auto& i : {n, int(ts)...}) - b[i] = true; + for (const auto& i : {n, int(ts)...}) b[i] = true; return reduce_impl(b); } @@ -269,8 +263,7 @@ class histogram { template > histogram reduce_to(Iterator begin, Iterator end) const { std::vector b(dim(), false); - for (; begin != end; ++begin) - b[*begin] = true; + for (; begin != end; ++begin) b[*begin] = true; return reduce_impl(b); } @@ -280,7 +273,7 @@ class histogram { return const_iterator(*this, storage_.size()); } - private: +private: axes_type axes_; Storage storage_; mutable detail::index_cache index_cache_; @@ -402,9 +395,7 @@ class histogram { noexcept {} template - void xlin_get(mp11::mp_int, - std::size_t& idx, - std::size_t& stride, + void xlin_get(mp11::mp_int, std::size_t& idx, std::size_t& stride, T&& t) const { constexpr unsigned D = detail::mp_size::value - N; apply_visitor( @@ -434,7 +425,8 @@ class histogram { const auto a_size = a.size(); const auto a_shape = a.shape(); const auto j = detail::indirect_int_cast(*iter++); - stride *= (-1 <= j && j <= a_size); // set stride to zero, if j is invalid + stride *= + (-1 <= j && j <= a_size); // set stride to zero, if j is invalid detail::lin(idx, stride, a_size, a_shape, j); } } @@ -444,9 +436,7 @@ class histogram { noexcept {} template - void lin_get(mp11::mp_size_t, - std::size_t& idx, - std::size_t& stride, + void lin_get(mp11::mp_size_t, std::size_t& idx, std::size_t& stride, const T& t) const noexcept { constexpr long unsigned int D = detail::mp_size::value - N; const auto& a = axes_[D]; @@ -464,17 +454,14 @@ class histogram { auto axes_iter = axes_.begin(); auto n_iter = n.begin(); for (const auto& bi : b) { - if (bi) - axes.emplace_back(*axes_iter); + if (bi) axes.emplace_back(*axes_iter); *n_iter = axes_iter->shape(); ++axes_iter; ++n_iter; } histogram h(std::move(axes)); detail::index_mapper m(n, b); - do { - h.storage_.add(m.second, storage_[m.first]); - } while (m.next()); + do { h.storage_.add(m.second, storage_[m.first]); } while (m.next()); return h; } @@ -499,25 +486,22 @@ make_dynamic_histogram(Axis&&... axis) { template dynamic_histogram< - mp11::mp_set_push_back...>, - Storage> + mp11::mp_set_push_back...>, Storage> make_dynamic_histogram_with(Axis&&... axis) { return dynamic_histogram< - mp11::mp_set_push_back...>, Storage>( - std::forward(axis)...); + mp11::mp_set_push_back...>, + Storage>(std::forward(axis)...); } template > dynamic_histogram< detail::mp_set_union> make_dynamic_histogram(Iterator begin, Iterator end) { - return dynamic_histogram< - detail::mp_set_union>( - begin, end); + return dynamic_histogram>(begin, end); } -template > dynamic_histogram< detail::mp_set_union, diff --git a/include/boost/histogram/histogram_fwd.hpp b/include/boost/histogram/histogram_fwd.hpp index 9252dfd9..95001d7d 100644 --- a/include/boost/histogram/histogram_fwd.hpp +++ b/include/boost/histogram/histogram_fwd.hpp @@ -27,7 +27,8 @@ struct sqrt; struct pow; } // namespace transform -template +template class regular; template class circular; @@ -42,10 +43,8 @@ using types = mp11::mp_list, axis::regular, axis::regular, axis::regular, - axis::circular, - axis::variable, - axis::integer, - axis::category, + axis::circular, axis::variable, + axis::integer, axis::category, axis::category>; template diff --git a/include/boost/histogram/iterator.hpp b/include/boost/histogram/iterator.hpp index f653347a..1f0794f3 100644 --- a/include/boost/histogram/iterator.hpp +++ b/include/boost/histogram/iterator.hpp @@ -21,11 +21,10 @@ namespace histogram { template class iterator_over - : public iterator_facade, - typename Histogram::element_type, - random_access_traversal_tag, - typename Histogram::const_reference> { - public: + : public iterator_facade< + iterator_over, typename Histogram::element_type, + random_access_traversal_tag, typename Histogram::const_reference> { +public: iterator_over(const Histogram& h, std::size_t idx) : histogram_(h), idx_(idx) {} @@ -55,7 +54,7 @@ class iterator_over return histogram_.axis(dim)[idx(dim)]; } - private: +private: bool equal(const iterator_over& rhs) const noexcept { return &histogram_ == &rhs.histogram_ && idx_ == rhs.idx_; } diff --git a/include/boost/histogram/ostream_operators.hpp b/include/boost/histogram/ostream_operators.hpp index fde35639..0a2ef30d 100644 --- a/include/boost/histogram/ostream_operators.hpp +++ b/include/boost/histogram/ostream_operators.hpp @@ -34,7 +34,8 @@ inline std::ostream& operator<<(std::ostream& os, const histogram& h) { } template -inline std::ostream& operator<<(std::ostream& os, const weight_counter& x) { +inline std::ostream& operator<<(std::ostream& os, + const weight_counter& x) { os << "weight_counter(" << x.value() << ", " << x.variance() << ")"; return os; } diff --git a/include/boost/histogram/serialization.hpp b/include/boost/histogram/serialization.hpp index ab7a7156..001b11a3 100644 --- a/include/boost/histogram/serialization.hpp +++ b/include/boost/histogram/serialization.hpp @@ -41,14 +41,14 @@ struct serialize_helper { template template -void weight_counter::serialize(Archive& ar, unsigned /* version */) { +void weight_counter::serialize(Archive& ar, + unsigned /* version */) { ar& w; ar& w2; } template -void serialize(Archive& ar, - array_storage& store, +void serialize(Archive& ar, array_storage& store, unsigned /* version */) { ar& store.array_; } diff --git a/include/boost/histogram/static_histogram.hpp b/include/boost/histogram/static_histogram.hpp index 12c5f3ca..14a36ffd 100644 --- a/include/boost/histogram/static_histogram.hpp +++ b/include/boost/histogram/static_histogram.hpp @@ -39,7 +39,7 @@ class histogram { using axes_size = mp11::mp_size; static_assert(axes_size::value > 0, "at least one axis required"); - public: +public: using axes_type = mp11::mp_rename; using element_type = typename Storage::element_type; using const_reference = typename Storage::const_reference; @@ -52,8 +52,7 @@ class histogram { histogram& operator=(const histogram& rhs) = default; histogram& operator=(histogram&& rhs) = default; - template > explicit histogram(Axis0&& axis0, Axis&&... axis) : axes_(std::forward(axis0), std::forward(axis)...) { @@ -154,8 +153,7 @@ class histogram { "fill arguments do not match histogram dimension"); std::size_t idx = 0, stride = 1; xlin<0>(idx, stride, ts...); - if (stride) - detail::fill_storage(storage_, idx); + if (stride) detail::fill_storage(storage_, idx); } template @@ -172,8 +170,7 @@ class histogram { // case with one argument is ambiguous, is specialized below std::size_t idx = 0, stride = 1; xlin<0>(idx, stride, ts...); - if (stride) - detail::fill_storage(storage_, idx, std::move(w)); + if (stride) detail::fill_storage(storage_, idx, std::move(w)); } // TODO: remove as obsolete @@ -253,8 +250,9 @@ class histogram { auto reduce_to(mp11::mp_int, Ns...) const -> static_histogram, Ns...>, Storage> { - using HR = static_histogram, Ns...>, - Storage>; + using HR = + static_histogram, Ns...>, + Storage>; auto hr = HR(detail::make_sub_tuple, Ns...>(axes_)); const auto b = detail::bool_mask, Ns...>(dim(), true); @@ -264,9 +262,11 @@ class histogram { const_iterator begin() const noexcept { return const_iterator(*this, 0); } - const_iterator end() const noexcept { return const_iterator(*this, size()); } + const_iterator end() const noexcept { + return const_iterator(*this, size()); + } - private: +private: axes_type axes_; Storage storage_; mutable detail::index_cache index_cache_; @@ -350,13 +350,11 @@ class histogram { } template - void xlin_iter(mp11::mp_size_t<0>, std::size_t&, std::size_t&, Iterator) const - noexcept {} + void xlin_iter(mp11::mp_size_t<0>, std::size_t&, std::size_t&, + Iterator) const noexcept {} template - void xlin_iter(mp11::mp_size_t, - std::size_t& idx, - std::size_t& stride, + void xlin_iter(mp11::mp_size_t, std::size_t& idx, std::size_t& stride, Iterator iter) const { constexpr unsigned D = axes_size::value - N; const auto a_size = std::get(axes_).size(); @@ -380,13 +378,11 @@ class histogram { } template - void lin_iter(mp11::mp_size_t<0>, std::size_t&, std::size_t&, Iterator) const - noexcept {} + void lin_iter(mp11::mp_size_t<0>, std::size_t&, std::size_t&, + Iterator) const noexcept {} template - void lin_iter(mp11::mp_size_t, - std::size_t& idx, - std::size_t& stride, + void lin_iter(mp11::mp_size_t, std::size_t& idx, std::size_t& stride, Iterator iter) const noexcept { constexpr unsigned D = axes_size::value - N; const auto a_size = std::get(axes_).size(); @@ -402,9 +398,7 @@ class histogram { noexcept {} template - void xlin_get(mp11::mp_size_t, - std::size_t& idx, - std::size_t& stride, + void xlin_get(mp11::mp_size_t, std::size_t& idx, std::size_t& stride, T&& t) const { constexpr unsigned D = detail::mp_size::value - N; const auto a_size = std::get(axes_).size(); @@ -419,9 +413,7 @@ class histogram { noexcept {} template - void lin_get(mp11::mp_size_t, - std::size_t& idx, - std::size_t& stride, + void lin_get(mp11::mp_size_t, std::size_t& idx, std::size_t& stride, T&& t) const noexcept { constexpr unsigned D = detail::mp_size::value - N; const auto a_size = std::get(axes_).size(); @@ -437,9 +429,7 @@ class histogram { detail::shape_vector_visitor v(dim()); for_each_axis(v); detail::index_mapper m(v.shapes, b); - do { - h.storage_.add(m.second, storage_[m.first]); - } while (m.next()); + do { h.storage_.add(m.second, storage_[m.first]); } while (m.next()); } template diff --git a/include/boost/histogram/storage/adaptive_storage.hpp b/include/boost/histogram/storage/adaptive_storage.hpp index 9deb401c..4103b2fa 100644 --- a/include/boost/histogram/storage/adaptive_storage.hpp +++ b/include/boost/histogram/storage/adaptive_storage.hpp @@ -54,7 +54,7 @@ inline T* alloc(std::size_t s) { } class array_base { - public: +public: explicit array_base(const std::size_t s) : size(s) {} array_base() = default; array_base(const array_base&) = default; @@ -72,7 +72,7 @@ class array_base { template class array : public array_base { - public: +public: explicit array(const std::size_t s) : array_base(s), ptr(alloc(s)) { std::fill(begin(), end(), T(0)); } @@ -118,23 +118,19 @@ class array : public array_base { const T* begin() const { return ptr.get(); } const T* end() const { return ptr.get() + size; } - private: +private: std::unique_ptr ptr; }; template <> class array : public array_base { - public: +public: using array_base::array_base; }; -using any_array = variant, - array, - array, - array, - array, - array, - array>; +using any_array = + variant, array, array, array, + array, array, array>; template struct next_type; @@ -159,8 +155,7 @@ using next = typename next_type::type; template inline bool safe_increase(T& t) { - if (t == std::numeric_limits::max()) - return false; + if (t == std::numeric_limits::max()) return false; ++t; return true; } @@ -176,8 +171,7 @@ inline bool safe_assign(T& t, const U& u) { template inline bool safe_radd(T& t, const U& u) { - if (static_cast(std::numeric_limits::max() - t) < u) - return false; + if (static_cast(std::numeric_limits::max() - t) < u) return false; t += static_cast(u); return true; } @@ -185,8 +179,7 @@ inline bool safe_radd(T& t, const U& u) { // float rounding is a mess, the equal sign is necessary here template inline bool safe_radd(T& t, const double u) { - if ((std::numeric_limits::max() - t) <= u) - return false; + if ((std::numeric_limits::max() - t) <= u) return false; t += u; return true; } @@ -390,24 +383,22 @@ struct rmul_visitor : public static_visitor { } void operator()(array&) const {} void operator()(array& lhs) const { - for (std::size_t i = 0; i != lhs.size; ++i) - lhs[i] *= x; + for (std::size_t i = 0; i != lhs.size; ++i) lhs[i] *= x; } }; struct bicmp_visitor : public static_visitor { template bool operator()(const array& b1, const array& b2) const { - if (b1.size != b2.size) - return false; + if (b1.size != b2.size) return false; return std::equal(b1.begin(), b1.end(), b2.begin()); } template bool operator()(const array& b1, const array& b2) const { - if (b1.size != b2.size) - return false; - return std::all_of(b1.begin(), b1.end(), [](const T& t) { return t == 0; }); + if (b1.size != b2.size) return false; + return std::all_of(b1.begin(), b1.end(), + [](const T& t) { return t == 0; }); } template @@ -425,11 +416,12 @@ struct bicmp_visitor : public static_visitor { class adaptive_storage { using buffer_type = detail::any_array; - public: +public: using element_type = detail::wcount; using const_reference = element_type; - explicit adaptive_storage(std::size_t s) : buffer_(detail::array(s)) {} + explicit adaptive_storage(std::size_t s) + : buffer_(detail::array(s)) {} adaptive_storage() = default; adaptive_storage(const adaptive_storage&) = default; @@ -450,9 +442,7 @@ class adaptive_storage { adaptive_storage& operator=(const RHS& rhs) { // no check for self-assign needed, default operator above is better match const auto n = rhs.size(); - if (size() != n) { - buffer_ = detail::array(n); - } + if (size() != n) { buffer_ = detail::array(n); } using T = typename RHS::element_type; for (std::size_t i = 0; i < n; ++i) { apply_visitor(detail::assign_visitor(buffer_, i, rhs[i]), buffer_); @@ -477,7 +467,8 @@ class adaptive_storage { apply_visitor(detail::radd_visitor(buffer_, i, x.value()), buffer_); } else { - apply_visitor(detail::radd_visitor(buffer_, i, x), buffer_); + apply_visitor(detail::radd_visitor(buffer_, i, x), + buffer_); } } @@ -517,9 +508,9 @@ class adaptive_storage { template adaptive_storage& operator+=(const RHS& rhs) { for (std::size_t i = 0, n = size(); i < n; ++i) - apply_visitor( - detail::radd_visitor(buffer_, i, rhs[i]), - buffer_); + apply_visitor(detail::radd_visitor( + buffer_, i, rhs[i]), + buffer_); return *this; } @@ -529,7 +520,7 @@ class adaptive_storage { return *this; } - private: +private: buffer_type buffer_; friend class ::boost::python::access; diff --git a/include/boost/histogram/storage/array_storage.hpp b/include/boost/histogram/storage/array_storage.hpp index e2839dbc..ade9d079 100644 --- a/include/boost/histogram/storage/array_storage.hpp +++ b/include/boost/histogram/storage/array_storage.hpp @@ -24,7 +24,7 @@ namespace histogram { template class array_storage { - public: +public: using element_type = T; using const_reference = const T&; @@ -81,30 +81,29 @@ class array_storage { array_[i] += x; } - const_reference operator[](std::size_t i) const noexcept { return array_[i]; } + const_reference operator[](std::size_t i) const noexcept { + return array_[i]; + } template bool operator==(const array_storage& rhs) const noexcept { - if (size_ != rhs.size_) - return false; + if (size_ != rhs.size_) return false; return std::equal(array_.get(), array_.get() + size_, rhs.array_.get()); } template array_storage& operator+=(const S& rhs) noexcept { - for (std::size_t i = 0; i < size_; ++i) - add(i, rhs[i]); + for (std::size_t i = 0; i < size_; ++i) add(i, rhs[i]); return *this; } template array_storage& operator*=(const U& x) noexcept { - for (std::size_t i = 0; i < size_; ++i) - array_[i] *= x; + for (std::size_t i = 0; i < size_; ++i) array_[i] *= x; return *this; } - private: +private: std::size_t size_ = 0; std::unique_ptr array_; diff --git a/include/boost/histogram/storage/operators.hpp b/include/boost/histogram/storage/operators.hpp index dd4b140d..bd4e696f 100644 --- a/include/boost/histogram/storage/operators.hpp +++ b/include/boost/histogram/storage/operators.hpp @@ -12,22 +12,16 @@ namespace boost { namespace histogram { -template , +template , typename = detail::requires_storage> bool operator==(const S1& s1, const S2& s2) noexcept { - if (s1.size() != s2.size()) - return false; + if (s1.size() != s2.size()) return false; for (std::size_t i = 0, n = s1.size(); i < n; ++i) - if (!(s1[i] == s2[i])) - return false; + if (!(s1[i] == s2[i])) return false; return true; } -template , +template , typename = detail::requires_storage> bool operator!=(const S1& s1, const S2& s2) noexcept { return !operator==(s1, s2); diff --git a/include/boost/histogram/storage/weight_counter.hpp b/include/boost/histogram/storage/weight_counter.hpp index d4e052fe..dca79791 100644 --- a/include/boost/histogram/storage/weight_counter.hpp +++ b/include/boost/histogram/storage/weight_counter.hpp @@ -21,7 +21,7 @@ namespace histogram { /// Double counter which holds a sum of weights and a sum of squared weights template class weight_counter { - public: +public: /// Beware: For performance reasons counters are not initialized weight_counter() = default; weight_counter(const weight_counter&) = default; @@ -108,7 +108,7 @@ class weight_counter { return static_cast(w); } - private: +private: friend class ::boost::serialization::access; template diff --git a/src/python/axis.cpp b/src/python/axis.cpp index 1a426b05..5d76aa30 100644 --- a/src/python/axis.cpp +++ b/src/python/axis.cpp @@ -4,33 +4,34 @@ // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) -#include "utility.hpp" -#include -#include #include #include +#include #include +#include #include #include -#include #include +#include #include +#include "utility.hpp" #ifdef HAVE_NUMPY #include namespace np = boost::python::numpy; #endif -#include -#include -#include -#include #include +#include #include +#include +#include +#include namespace bp = boost::python; namespace bh = boost::histogram; namespace bha = boost::histogram::axis; -template bp::str generic_repr(const T &t) { +template +bp::str generic_repr(const T& t) { std::ostringstream os; os << t; return os.str().c_str(); @@ -56,19 +57,15 @@ generic_iterator make_generic_iterator(bp::object self) { } template -struct axis_value_view_to_python -{ - static PyObject* convert(const bha::value_view &i) - { +struct axis_value_view_to_python { + static PyObject* convert(const bha::value_view& i) { return bp::incref(bp::object(i.value()).ptr()); } }; template -struct axis_interval_view_to_python -{ - static PyObject* convert(const bha::interval_view &i) - { +struct axis_interval_view_to_python { + static PyObject* convert(const bha::interval_view& i) { return bp::incref(bp::make_tuple(i.lower(), i.upper()).ptr()); } }; @@ -96,10 +93,8 @@ bp::object variable_init(bp::tuple args, bp::dict kwargs) { if (k == "label") label = boost::string_view(bp::extract(v), bp::len(v)); else if (k == "uoflow") { - if (!bp::extract(v)) - uo = bha::uoflow::off; - } - else { + if (!bp::extract(v)) uo = bha::uoflow::off; + } else { std::stringstream s; s << "keyword " << k << " not recognized"; PyErr_SetString(PyExc_KeyError, s.str().c_str()); @@ -142,46 +137,51 @@ bp::object category_init(bp::tuple args, bp::dict kwargs) { return self.attr("__init__")(bha::category<>(c.begin(), c.end(), label)); } -template void axis_set_label(T& t, bp::str s) { - t.label({bp::extract(s)(), - static_cast(bp::len(s))}); +template +void axis_set_label(T& t, bp::str s) { + t.label( + {bp::extract(s)(), static_cast(bp::len(s))}); } -template bp::str axis_get_label(const T& t) { +template +bp::str axis_get_label(const T& t) { auto s = t.label(); return {s.data(), s.size()}; } -template bp::object axis_getitem(const A &a, int i) { +template +bp::object axis_getitem(const A& a, int i) { if (i < -1 * a.uoflow() || i >= a.size() + 1 * a.uoflow()) throw std::out_of_range("index out of bounds"); - return bp::make_tuple(a.lower(i), a.lower(i+1)); + return bp::make_tuple(a.lower(i), a.lower(i + 1)); } -template <> bp::object axis_getitem>(const bha::category<> &a, int i) { - if (i < 0 || i >= a.size()) - throw std::out_of_range("index out of bounds"); +template <> +bp::object axis_getitem>(const bha::category<>& a, int i) { + if (i < 0 || i >= a.size()) throw std::out_of_range("index out of bounds"); return bp::object(a.value(i)); } #ifdef HAVE_NUMPY -template bp::object axis_array_interface(const Axis& axis) { +template +bp::object axis_array_interface(const Axis& axis) { using T = typename std::decay::type; bp::dict d; - auto shape = bp::make_tuple(axis.size()+1); + auto shape = bp::make_tuple(axis.size() + 1); d["shape"] = shape; d["typestr"] = bp::dtype_typestr(); // make new array, and pass it to Python auto a = np::empty(shape, np::dtype::get_builtin()); auto buf = reinterpret_cast(a.get_data()); - for (auto i = 0; i < axis.size()+1; ++i) - buf[i] = axis.lower(i); + for (auto i = 0; i < axis.size() + 1; ++i) buf[i] = axis.lower(i); d["data"] = a; d["version"] = 3; return d; } -template <> bp::object axis_array_interface>(const bha::category<>& axis) { +template <> +bp::object axis_array_interface>( + const bha::category<>& axis) { bp::dict d; auto shape = bp::make_tuple(axis.size()); d["shape"] = shape; @@ -189,8 +189,7 @@ template <> bp::object axis_array_interface>(const bha::category // make new array, and pass it to Python auto a = np::empty(shape, np::dtype::get_builtin()); auto buf = reinterpret_cast(a.get_data()); - for (auto i = 0; i < axis.size(); ++i) - buf[i] = axis.value(i); + for (auto i = 0; i < axis.size(); ++i) buf[i] = axis.value(i); d["data"] = a; d["version"] = 3; return d; @@ -199,15 +198,16 @@ template <> bp::object axis_array_interface>(const bha::category template struct axis_suite : public bp::def_visitor> { - template static void visit(Class &cl) { - cl.add_property( - "shape", &T::shape, - "Number of bins, including over-/underflow bins if they are present."); - cl.add_property( - "label", axis_get_label, axis_set_label, - "Name or description for the axis."); - cl.def("index", &T::index, ":param float x: value" - "\n:returns: bin index for the passed value", + template + static void visit(Class& cl) { + cl.add_property("shape", &T::shape, + "Number of bins, including over-/underflow bins if they " + "are present."); + cl.add_property("label", axis_get_label, axis_set_label, + "Name or description for the axis."); + cl.def("index", &T::index, + ":param float x: value" + "\n:returns: bin index for the passed value", bp::args("self", "x")); cl.def("__len__", &T::size, ":returns: number of bins, excluding over-/underflow bins.", @@ -227,39 +227,35 @@ struct axis_suite : public bp::def_visitor> { }; template -bha::regular* regular_init( - unsigned bin, double lower, double upper, - bp::str pylabel, bool with_uoflow) -{ +bha::regular* regular_init(unsigned bin, double lower, + double upper, bp::str pylabel, + bool with_uoflow) { const auto uo = with_uoflow ? bha::uoflow::on : bha::uoflow::off; - return new bha::regular(bin, lower, upper, - {bp::extract(pylabel)(), - static_cast(bp::len(pylabel))}, + return new bha::regular( + bin, lower, upper, {bp::extract(pylabel)(), + static_cast(bp::len(pylabel))}, uo); } bha::regular* regular_pow_init( - unsigned bin, double lower, double upper, double power, - bp::str pylabel, bool with_uoflow) -{ + unsigned bin, double lower, double upper, double power, bp::str pylabel, + bool with_uoflow) { using namespace ::boost::python; const auto uo = with_uoflow ? bha::uoflow::on : bha::uoflow::off; return new bha::regular( - bin, lower, upper, - {extract(pylabel)(), - static_cast(bp::len(pylabel))}, + bin, lower, upper, {extract(pylabel)(), + static_cast(bp::len(pylabel))}, uo, power); } -bha::integer<>* integer_init(int lower, int upper, - bp::str pylabel, bool with_uoflow) -{ +bha::integer<>* integer_init(int lower, int upper, bp::str pylabel, + bool with_uoflow) { using namespace ::boost::python; const auto uo = with_uoflow ? bha::uoflow::on : bha::uoflow::off; return new bha::integer<>(lower, upper, - {extract(pylabel)(), - static_cast(bp::len(pylabel))}, - uo); + {extract(pylabel)(), + static_cast(bp::len(pylabel))}, + uo); } void register_axis_types() { @@ -268,32 +264,34 @@ void register_axis_types() { docstring_options dopt(true, true, false); class_("generic_iterator", init()) - .def("__iter__", &generic_iterator::self, return_internal_reference<>()) - .def("__next__", &generic_iterator::next) // Python3 - .def("next", &generic_iterator::next) // Python2 - ; + .def("__iter__", &generic_iterator::self, return_internal_reference<>()) + .def("__next__", &generic_iterator::next) // Python3 + .def("next", &generic_iterator::next) // Python2 + ; - class_>( - "regular", - "Axis for real-valued data and bins of equal width." - "\nBinning is a O(1) operation.", - no_init) - .def("__init__", make_constructor(regular_init, - default_call_policies(), - (arg("bin"), arg("lower"), arg("upper"), - arg("label") = "", arg("uoflow") = true))) + class_>("regular", + "Axis for real-valued data and bins of equal width." + "\nBinning is a O(1) operation.", + no_init) + .def("__init__", + make_constructor(regular_init, + default_call_policies(), + (arg("bin"), arg("lower"), arg("upper"), + arg("label") = "", arg("uoflow") = true))) .def(axis_suite>()); -#define BOOST_HISTOGRAM_PYTHON_REGULAR_CLASS(x) \ - class_>( \ - "regular_"#x, \ - "Axis for real-valued data and bins of equal width in "#x"-space." \ - "\nBinning is a O(1) operation.", \ - no_init) \ - .def("__init__", make_constructor(regular_init, \ - default_call_policies(), \ - (arg("bin"), arg("lower"), arg("upper"), \ - arg("label") = "", arg("uoflow") = true))) \ +#define BOOST_HISTOGRAM_PYTHON_REGULAR_CLASS(x) \ + class_>( \ + "regular_" #x, \ + "Axis for real-valued data and bins of equal width in " #x \ + "-space." \ + "\nBinning is a O(1) operation.", \ + no_init) \ + .def("__init__", \ + make_constructor(regular_init, \ + default_call_policies(), \ + (arg("bin"), arg("lower"), arg("upper"), \ + arg("label") = "", arg("uoflow") = true))) \ .def(axis_suite>()) BOOST_HISTOGRAM_PYTHON_REGULAR_CLASS(log); @@ -305,10 +303,11 @@ void register_axis_types() { "Axis for real-valued data and bins of equal width in power-space." "\nBinning is a O(1) operation.", no_init) - .def("__init__", make_constructor(regular_pow_init, - default_call_policies(), - (arg("bin"), arg("lower"), arg("upper"), arg("power"), - arg("label") = "", arg("uoflow") = true))) + .def("__init__", + make_constructor( + regular_pow_init, default_call_policies(), + (arg("bin"), arg("lower"), arg("upper"), arg("power"), + arg("label") = "", arg("uoflow") = true))) .def(axis_suite>()); class_>( @@ -320,8 +319,7 @@ void register_axis_types() { no_init) .def(init( (arg("self"), arg("bin"), arg("phase") = 0.0, - arg("perimeter") = bh::detail::two_pi, - arg("label") = ""))) + arg("perimeter") = bh::detail::two_pi, arg("label") = ""))) .def(axis_suite>()); class_>( @@ -331,7 +329,7 @@ void register_axis_types() { "\nthe problem domain allows it, prefer a regular axis.", no_init) .def("__init__", raw_function(variable_init)) - .def(init &>()) + .def(init&>()) .def(axis_suite>()); class_>( @@ -340,10 +338,10 @@ void register_axis_types() { "\nthat are one integer wide. Faster than a regular axis." "\nBinning is a O(1) operation.", no_init) - .def("__init__", make_constructor(integer_init, - default_call_policies(), - (arg("lower"), arg("upper"), arg("label") = "", - arg("uoflow") = true))) + .def("__init__", + make_constructor(integer_init, default_call_policies(), + (arg("lower"), arg("upper"), arg("label") = "", + arg("uoflow") = true))) .def(axis_suite>()); class_>( @@ -354,6 +352,6 @@ void register_axis_types() { "\nBinning is a O(1) operation.", no_init) .def("__init__", raw_function(category_init)) - .def(init &>()) + .def(init&>()) .def(axis_suite>()); } diff --git a/src/python/histogram.cpp b/src/python/histogram.cpp index 6319ac50..e3549e9c 100644 --- a/src/python/histogram.cpp +++ b/src/python/histogram.cpp @@ -4,18 +4,18 @@ // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) -#include "serialization_suite.hpp" -#include "utility.hpp" +#include #include -#include #include #include -#include +#include #include #include #include #include #include +#include "serialization_suite.hpp" +#include "utility.hpp" #ifdef HAVE_NUMPY #include namespace np = boost::python::numpy; @@ -44,8 +44,8 @@ public: using array = bh::detail::array; struct dtype_visitor : public boost::static_visitor { - list & shapes, & strides; - dtype_visitor(list &sh, list &st) : shapes(sh), strides(st) {} + list &shapes, &strides; + dtype_visitor(list& sh, list& st) : shapes(sh), strides(st) {} template str operator()(const array& /*unused*/) const { strides.append(sizeof(T)); @@ -85,26 +85,25 @@ public: // double array, fill it and pass it auto a = np::empty(tuple(shapes), np::dtype::get_builtin()); for (auto i = 0l, n = bp::len(shapes); i < n; ++i) - const_cast(a.get_strides())[i] = bp::extract(strides[i]); - auto *buf = (double *)a.get_data(); - for (auto i = 0ul; i < b.size; ++i) - buf[i] = static_cast(b[i]); + const_cast(a.get_strides())[i] = + bp::extract(strides[i]); + auto* buf = (double*)a.get_data(); + for (auto i = 0ul; i < b.size; ++i) buf[i] = static_cast(b[i]); return a; } }; - static object array_interface(const pyhistogram &self) { + static object array_interface(const pyhistogram& self) { dict d; list shapes; list strides; - auto &b = self.storage_.buffer_; + auto& b = self.storage_.buffer_; d["typestr"] = boost::apply_visitor(dtype_visitor(shapes, strides), b); for (auto i = 0u; i < self.dim(); ++i) { if (i) strides.append(strides[-1] * shapes[-1]); shapes.append(self.axis(i).shape()); } - if (self.dim() == 0) - shapes.append(0); + if (self.dim() == 0) shapes.append(0); d["shape"] = tuple(shapes); d["strides"] = tuple(strides); d["data"] = boost::apply_visitor(data_visitor(shapes, strides), b); @@ -117,7 +116,8 @@ public: } // namespace boost struct axis_visitor : public boost::static_visitor { - template bp::object operator()(const T &t) const { + template + bp::object operator()(const T& t) const { return bp::object(t); } }; @@ -126,9 +126,10 @@ struct axes_appender { bp::object obj; pyhistogram::axes_type& axes; bool& success; - axes_appender(bp::object o, pyhistogram::axes_type& a, - bool& s) : obj(o), axes(a), success(s) {} - template void operator()(const A&) const { + axes_appender(bp::object o, pyhistogram::axes_type& a, bool& s) + : obj(o), axes(a), success(s) {} + template + void operator()(const A&) const { if (success) return; bp::extract get_axis(obj); if (get_axis.check()) { @@ -138,9 +139,8 @@ struct axes_appender { } }; -bp::object histogram_axis(const pyhistogram &self, int i) { - if (i < 0) - i += self.dim(); +bp::object histogram_axis(const pyhistogram& self, int i) { + if (i < 0) i += self.dim(); if (i < 0 || i >= int(self.dim())) throw std::out_of_range("axis index out of range"); return boost::apply_visitor(axis_visitor(), self.axis(i)); @@ -163,8 +163,7 @@ bp::object histogram_init(bp::tuple args, bp::dict kwargs) { bp::object pa = args[i + 1]; bool success = false; boost::mp11::mp_for_each( - axes_appender(pa, axes, success) - ); + axes_appender(pa, axes, success)); if (!success) { std::string msg = "require an axis object, got "; msg += bp::extract(pa.attr("__class__"))(); @@ -204,8 +203,7 @@ struct fetcher { } const T& operator[](long i) const noexcept { - if (n > 0) - return carray[i]; + if (n > 0) return carray[i]; return value; } }; @@ -220,18 +218,20 @@ struct span { bp::object histogram_fill(bp::tuple args, bp::dict kwargs) { const auto nargs = bp::len(args); - pyhistogram &self = bp::extract(args[0]); + pyhistogram& self = bp::extract(args[0]); const unsigned dim = nargs - 1; if (dim != self.dim()) { - PyErr_SetString(PyExc_ValueError, "number of arguments and dimension do not match"); + PyErr_SetString(PyExc_ValueError, + "number of arguments and dimension do not match"); bp::throw_error_already_set(); } if (dim > BOOST_HISTOGRAM_AXIS_LIMIT) { PyErr_SetString(PyExc_RuntimeError, - bh::detail::cat("too many arguments, maximum is ", - BOOST_HISTOGRAM_AXIS_LIMIT).c_str()); + bh::detail::cat("too many arguments, maximum is ", + BOOST_HISTOGRAM_AXIS_LIMIT) + .c_str()); bp::throw_error_already_set(); } @@ -241,7 +241,8 @@ bp::object histogram_fill(bp::tuple args, bp::dict kwargs) { fetch[d].assign(args[1 + d]); if (fetch[d].n > 0) { if (n > 0 && fetch[d].n != n) { - PyErr_SetString(PyExc_ValueError, "lengths of sequences do not match"); + PyErr_SetString(PyExc_ValueError, + "lengths of sequences do not match"); bp::throw_error_already_set(); } n = fetch[d].n; @@ -261,7 +262,8 @@ bp::object histogram_fill(bp::tuple args, bp::dict kwargs) { fetch_weight.assign(kwargs.get("weight")); if (fetch_weight.n > 0) { if (n > 0 && fetch_weight.n != n) { - PyErr_SetString(PyExc_ValueError, "length of weight sequence does not match"); + PyErr_SetString(PyExc_ValueError, + "length of weight sequence does not match"); bp::throw_error_already_set(); } n = fetch_weight.n; @@ -275,22 +277,18 @@ bp::object histogram_fill(bp::tuple args, bp::dict kwargs) { for (auto i = 0l; i < n; ++i) self(bh::weight(fetch_weight[i]), fetch[0][i]); } else { - for (auto i = 0l; i < n; ++i) - self(fetch[0][i]); + for (auto i = 0l; i < n; ++i) self(fetch[0][i]); } } else { double v[BOOST_HISTOGRAM_AXIS_LIMIT]; if (fetch_weight.n >= 0) { for (auto i = 0l; i < n; ++i) { - for (auto d = 0u; d < dim; ++d) - v[d] = fetch[d][i]; + for (auto d = 0u; d < dim; ++d) v[d] = fetch[d][i]; self(bh::weight(fetch_weight[i]), span{v, dim}); } - } - else { + } else { for (auto i = 0l; i < n; ++i) { - for (auto d = 0u; d < dim; ++d) - v[d] = fetch[d][i]; + for (auto d = 0u; d < dim; ++d) v[d] = fetch[d][i]; self(span{v, dim}); } } @@ -318,20 +316,20 @@ bp::object histogram_getitem(const pyhistogram& self, bp::object args) { if (dim > BOOST_HISTOGRAM_AXIS_LIMIT) { PyErr_SetString(PyExc_RuntimeError, - bh::detail::cat("too many arguments, maximum is ", - BOOST_HISTOGRAM_AXIS_LIMIT).c_str()); + bh::detail::cat("too many arguments, maximum is ", + BOOST_HISTOGRAM_AXIS_LIMIT) + .c_str()); bp::throw_error_already_set(); } int idx[BOOST_HISTOGRAM_AXIS_LIMIT]; - for (unsigned i = 0; i < dim; ++i) - idx[i] = bp::extract(args[i]); + for (unsigned i = 0; i < dim; ++i) idx[i] = bp::extract(args[i]); return bp::object(self.at(span{idx, self.dim()})); } bp::object histogram_at(bp::tuple args, bp::dict kwargs) { - const pyhistogram & self = bp::extract(args[0]); + const pyhistogram& self = bp::extract(args[0]); if (kwargs) { PyErr_SetString(PyExc_RuntimeError, "no keyword arguments allowed"); @@ -343,14 +341,15 @@ bp::object histogram_at(bp::tuple args, bp::dict kwargs) { } bp::object histogram_reduce_to(bp::tuple args, bp::dict kwargs) { - const pyhistogram &self = bp::extract(args[0]); + const pyhistogram& self = bp::extract(args[0]); const unsigned nargs = bp::len(args) - 1; if (nargs > BOOST_HISTOGRAM_AXIS_LIMIT) { PyErr_SetString(PyExc_RuntimeError, - bh::detail::cat("too many arguments, maximum is ", - BOOST_HISTOGRAM_AXIS_LIMIT).c_str()); + bh::detail::cat("too many arguments, maximum is ", + BOOST_HISTOGRAM_AXIS_LIMIT) + .c_str()); bp::throw_error_already_set(); } @@ -360,84 +359,86 @@ bp::object histogram_reduce_to(bp::tuple args, bp::dict kwargs) { } int idx[BOOST_HISTOGRAM_AXIS_LIMIT]; - for (auto i = 0u; i < nargs; ++i) - idx[i] = bp::extract(args[1 + i]); + for (auto i = 0u; i < nargs; ++i) idx[i] = bp::extract(args[1 + i]); return bp::object(self.reduce_to(idx, idx + nargs)); } -std::string histogram_repr(const pyhistogram &h) { +std::string histogram_repr(const pyhistogram& h) { return bh::detail::cat(h); } -double element_value(const pyhistogram::element_type& b) { - return b.value(); -} +double element_value(const pyhistogram::element_type& b) { return b.value(); } double element_variance(const pyhistogram::element_type& b) { return b.variance(); } double element_getitem(const pyhistogram::element_type& e, int i) { - if (i < 0 || i > 1) - throw std::out_of_range("element_getitem"); + if (i < 0 || i > 1) throw std::out_of_range("element_getitem"); return i == 0 ? e.value() : e.variance(); } int element_len(const pyhistogram::element_type&) { return 2; } std::string element_repr(const pyhistogram::element_type& e) { - return bh::detail::cat("histogram.element(", e.value(), ", ", e.variance(), ")"); + return bh::detail::cat("histogram.element(", e.value(), ", ", e.variance(), + ")"); } void register_histogram() { bp::docstring_options dopt(true, true, false); - bp::scope s = bp::class_>( - "histogram", "N-dimensional histogram for real-valued data.", bp::no_init) - .def("__init__", bp::raw_function(histogram_init), - ":param axis args: axis objects" - "\nPass one or more axis objects to configure the histogram.") - // shadowed C++ ctors - .def(bp::init()) - // .def(bp::init()) + bp::scope s = + bp::class_>( + "histogram", "N-dimensional histogram for real-valued data.", + bp::no_init) + .def("__init__", bp::raw_function(histogram_init), + ":param axis args: axis objects" + "\nPass one or more axis objects to configure the histogram.") + // shadowed C++ ctors + .def(bp::init()) +// .def(bp::init()) #ifdef HAVE_NUMPY - .add_property("__array_interface__", &bp::access::array_interface) + .add_property("__array_interface__", &bp::access::array_interface) #endif - .add_property("dim", &pyhistogram::dim) - .def("axis", histogram_axis, bp::arg("i") = 0, - ":param int i: axis index" - "\n:return: corresponding axis object") - .def("__call__", bp::raw_function(histogram_fill), - ":param double args: values (number must match dimension)" - "\n:keyword double weight: optional weight" - "\n" - "\nIf Numpy support is enabled, 1d-arrays can be passed instead of" - "\nvalues, which must be equal in lenght. Arrays and values can" - "\nbe mixed arbitrarily in the same call.") - .def("__len__", &pyhistogram::size, - ":return: total number of bins, including under- and overflow") - .def("at", bp::raw_function(histogram_at), - ":param int args: indices of the bin (number must match dimension)" - "\n:return: bin content") - .def("__getitem__", histogram_getitem, - ":param int args: indices of the bin (number must match dimension)" - "\n:return: bin content") - .def("reduce_to", bp::raw_function(histogram_reduce_to), - ":param int args: indices of the axes in the reduced histogram" - "\n:return: reduced histogram with subset of axes") - .def("__iter__", bp::iterator()) - .def("__repr__", histogram_repr, - ":return: string representation of the histogram") - .def(bp::self == bp::self) - .def(bp::self != bp::self) - .def(bp::self += bp::self) - .def(bp::self *= double()) - .def(bp::self * double()) - .def(double() * bp::self) - .def(bp::self + bp::self) - .def_pickle(bh::serialization_suite()) - ; + .add_property("dim", &pyhistogram::dim) + .def("axis", histogram_axis, bp::arg("i") = 0, + ":param int i: axis index" + "\n:return: corresponding axis object") + .def( + "__call__", bp::raw_function(histogram_fill), + ":param double args: values (number must match dimension)" + "\n:keyword double weight: optional weight" + "\n" + "\nIf Numpy support is enabled, 1d-arrays can be passed " + "instead of" + "\nvalues, which must be equal in lenght. Arrays and values can" + "\nbe mixed arbitrarily in the same call.") + .def("__len__", &pyhistogram::size, + ":return: total number of bins, including under- and overflow") + .def("at", bp::raw_function(histogram_at), + ":param int args: indices of the bin (number must match " + "dimension)" + "\n:return: bin content") + .def("__getitem__", histogram_getitem, + ":param int args: indices of the bin (number must match " + "dimension)" + "\n:return: bin content") + .def("reduce_to", bp::raw_function(histogram_reduce_to), + ":param int args: indices of the axes in the reduced histogram" + "\n:return: reduced histogram with subset of axes") + .def("__iter__", bp::iterator()) + .def("__repr__", histogram_repr, + ":return: string representation of the histogram") + .def(bp::self == bp::self) + .def(bp::self != bp::self) + .def(bp::self += bp::self) + .def(bp::self *= double()) + .def(bp::self * double()) + .def(double() * bp::self) + .def(bp::self + bp::self) + .def_pickle(bh::serialization_suite()); bp::class_( "element", "Holds value and variance of bin count.", @@ -453,6 +454,5 @@ void register_histogram() { .def(bp::self + bp::self) .def(bp::self + double()) .def(double() + bp::self) - .def("__repr__", element_repr) - ; + .def("__repr__", element_repr); } diff --git a/src/python/module.cpp b/src/python/module.cpp index edf440cc..c57ea2e2 100644 --- a/src/python/module.cpp +++ b/src/python/module.cpp @@ -5,10 +5,10 @@ // or copy at http://www.boost.org/LICENSE_1_0.txt) #include -#include #include +#include #ifdef HAVE_NUMPY -# include +#include #endif void register_axis_types(); @@ -23,9 +23,7 @@ BOOST_PYTHON_MODULE(histogram) { #else current.attr("HAVE_NUMPY") = false; #endif - object axis_module = object( - borrowed(PyImport_AddModule("histogram.axis")) - ); + object axis_module = object(borrowed(PyImport_AddModule("histogram.axis"))); current.attr("axis") = axis_module; { scope current = axis_module; diff --git a/src/python/serialization_suite.hpp b/src/python/serialization_suite.hpp index 870b75a0..0e632708 100644 --- a/src/python/serialization_suite.hpp +++ b/src/python/serialization_suite.hpp @@ -31,11 +31,11 @@ namespace detail { class python_bytes_sink : public iostreams::sink { public: - python_bytes_sink(PyObject **pstr) : pstr_(pstr), len_(0), pos_(0) { + python_bytes_sink(PyObject** pstr) : pstr_(pstr), len_(0), pos_(0) { BOOST_ASSERT(*pstr == 0); } - std::streamsize write(const char *s, std::streamsize n) { + std::streamsize write(const char* s, std::streamsize n) { if (len_ == 0) { *pstr_ = PyBytes_FromStringAndSize(s, n); if (*pstr_ == 0) // no point trying to recover from allocation error @@ -47,7 +47,7 @@ public: if (_PyBytes_Resize(pstr_, len_) == -1) std::abort(); // no point trying to recover from allocation error } - char *b = PyBytes_AS_STRING(*pstr_); + char* b = PyBytes_AS_STRING(*pstr_); std::copy(s, s + n, b + pos_); } pos_ += n; @@ -55,17 +55,18 @@ public: } private: - PyObject **pstr_; + PyObject** pstr_; std::streamsize len_, pos_; }; } // namespace detail -template struct serialization_suite : python::pickle_suite { +template +struct serialization_suite : python::pickle_suite { static python::tuple getstate(python::object obj) { - PyObject *pobj = 0; + PyObject* pobj = 0; iostreams::stream os(&pobj); archive::text_oarchive oa(os); - oa << python::extract(obj)(); + oa << python::extract(obj)(); os.flush(); return python::make_tuple(obj.attr("__dict__"), python::object(python::handle<>(pobj))); @@ -81,7 +82,7 @@ template struct serialization_suite : python::pickle_suite { iostreams::stream is(PyBytes_AS_STRING(o.ptr()), PyBytes_Size(o.ptr())); archive::text_iarchive ia(is); - ia >> python::extract(obj)(); + ia >> python::extract(obj)(); } static bool getstate_manages_dict() { return true; } diff --git a/src/python/utility.hpp b/src/python/utility.hpp index 56722812..2ad168ca 100644 --- a/src/python/utility.hpp +++ b/src/python/utility.hpp @@ -8,22 +8,22 @@ #define _BOOST_HISTOGRAM_PYTHON_UTILITY_HPP_ #include -#include #include +#include namespace boost { namespace python { template str dtype_typestr() { - str s; - if (std::is_floating_point::value) - s = "|f"; - else if (std::is_integral::value) - s = std::is_unsigned::value ? "|u" : "|i"; - else - throw std::invalid_argument("T must be a builtin arithmetic type"); - s += str(sizeof(T)); - return s; + str s; + if (std::is_floating_point::value) + s = "|f"; + else if (std::is_integral::value) + s = std::is_unsigned::value ? "|u" : "|i"; + else + throw std::invalid_argument("T must be a builtin arithmetic type"); + s += str(sizeof(T)); + return s; } } // python } // boost diff --git a/test/adaptive_storage_test.cpp b/test/adaptive_storage_test.cpp index 4affe50a..bb23b761 100644 --- a/test/adaptive_storage_test.cpp +++ b/test/adaptive_storage_test.cpp @@ -190,8 +190,7 @@ void convert_array_storage_impl() { array_storage t(std::size_t(1)); t.increase(0); - while (t[0] < 1e20) - t.add(0, t[0]); + while (t[0] < 1e20) t.add(0, t[0]); auto d = aref; d = t; BOOST_TEST(d == t); diff --git a/test/axis_test.cpp b/test/axis_test.cpp index 4c534005..1f6a82c7 100644 --- a/test/axis_test.cpp +++ b/test/axis_test.cpp @@ -51,7 +51,8 @@ int main() { { axis::regular<> a{4, -2, 2}; BOOST_TEST_EQ(a[-1].lower(), -std::numeric_limits::infinity()); - BOOST_TEST_EQ(a[a.size()].upper(), std::numeric_limits::infinity()); + BOOST_TEST_EQ(a[a.size()].upper(), + std::numeric_limits::infinity()); axis::regular<> b; BOOST_TEST_NOT(a == b); b = a; @@ -149,7 +150,8 @@ int main() { { axis::variable<> a{-1, 0, 1}; BOOST_TEST_EQ(a[-1].lower(), -std::numeric_limits::infinity()); - BOOST_TEST_EQ(a[a.size()].upper(), std::numeric_limits::infinity()); + BOOST_TEST_EQ(a[a.size()].upper(), + std::numeric_limits::infinity()); axis::variable<> b; BOOST_TEST_NOT(a == b); b = a; @@ -258,7 +260,8 @@ int main() { a6 = a1; BOOST_TEST_EQ(a6, a1); axis::any, axis::integer<>> a7(axis::integer<>(0, 2)); - BOOST_TEST_THROWS(axis::any> a8(a7), std::invalid_argument); + BOOST_TEST_THROWS(axis::any> a8(a7), + std::invalid_argument); BOOST_TEST_THROWS(a4 = a7, std::invalid_argument); } @@ -288,14 +291,13 @@ int main() { axes.push_back(axis::regular( 2, 1, 10, "regular4", axis::uoflow::off, -0.5)); axes.push_back(axis::circular<>(4, 0.1, 1.0, "polar")); - axes.push_back(axis::variable<>({-1, 0, 1}, "variable", axis::uoflow::off)); + axes.push_back( + axis::variable<>({-1, 0, 1}, "variable", axis::uoflow::off)); axes.push_back(axis::category<>({A, B, C}, "category")); axes.push_back(axis::category({a, b}, "category2")); axes.push_back(axis::integer<>(-1, 1, "integer", axis::uoflow::off)); std::ostringstream os; - for (const auto& a : axes) { - os << a << "\n"; - } + for (const auto& a : axes) { os << a << "\n"; } const std::string ref = "regular(2, -1, 1, label='regular1')\n" "regular_log(2, 1, 10, label='regular2', uoflow=False)\n" @@ -343,7 +345,8 @@ int main() { std_vector1 = {axis::regular<>{2, -1, 1}, axis::variable<>{-1, 0, 1}, axis::category<>{A, B, C}}; - std::vector, axis::variable<>, axis::category<>>> + std::vector< + axis::any, axis::variable<>, axis::category<>>> std_vector2 = {axis::regular<>{2, -1, 1}, axis::variable<>{-1, 0, 1}, axis::category<>{{A, B, C}}}; @@ -365,8 +368,8 @@ int main() { std::make_tuple(axis::regular<>{2, -1, 1}, axis::variable<>{-1, 0, 1}, axis::category<>{{A, B}}); - auto tuple3 = - std::make_tuple(axis::regular<>{2, -1, 1}, axis::variable<>{-1, 0, 1}); + auto tuple3 = std::make_tuple(axis::regular<>{2, -1, 1}, + axis::variable<>{-1, 0, 1}); BOOST_TEST(detail::axes_equal(std_vector1, tuple1)); BOOST_TEST(detail::axes_equal(tuple1, std_vector1)); @@ -383,7 +386,8 @@ int main() { std_vector1 = {axis::regular<>{2, -1, 1}, axis::variable<>{-1, 0, 1}, axis::category<>{A, B, C}}; - std::vector, axis::variable<>, axis::category<>>> + std::vector< + axis::any, axis::variable<>, axis::category<>>> std_vector2 = {axis::regular<>{2, -2, 2}, axis::variable<>{-2, 0, 2}, axis::category<>{A, B}}; diff --git a/test/fail_histogram_dynamic_bin_2_test.cpp b/test/fail_histogram_dynamic_bin_2_test.cpp index 4318caaf..9993558a 100644 --- a/test/fail_histogram_dynamic_bin_2_test.cpp +++ b/test/fail_histogram_dynamic_bin_2_test.cpp @@ -3,6 +3,7 @@ using namespace boost::histogram; int main() { - auto h = make_dynamic_histogram(axis::integer<>(0, 2), axis::integer<>(0, 2)); + auto h = + make_dynamic_histogram(axis::integer<>(0, 2), axis::integer<>(0, 2)); h.at(std::make_pair(-2, 0)); } diff --git a/test/fail_histogram_dynamic_bin_3_test.cpp b/test/fail_histogram_dynamic_bin_3_test.cpp index f4a09ba1..26a5f896 100644 --- a/test/fail_histogram_dynamic_bin_3_test.cpp +++ b/test/fail_histogram_dynamic_bin_3_test.cpp @@ -3,6 +3,7 @@ using namespace boost::histogram; int main() { - auto h = make_dynamic_histogram(axis::integer<>(0, 2), axis::integer<>(0, 2)); + auto h = + make_dynamic_histogram(axis::integer<>(0, 2), axis::integer<>(0, 2)); h.at(std::vector({-2, 0})); } diff --git a/test/fail_histogram_static_bin_vector_out_of_bounds_test.cpp b/test/fail_histogram_static_bin_vector_out_of_bounds_test.cpp index 082ac181..e2ec5119 100644 --- a/test/fail_histogram_static_bin_vector_out_of_bounds_test.cpp +++ b/test/fail_histogram_static_bin_vector_out_of_bounds_test.cpp @@ -3,6 +3,7 @@ using namespace boost::histogram; int main() { - auto h = make_static_histogram(axis::integer<>(0, 2), axis::integer<>(0, 2)); + auto h = + make_static_histogram(axis::integer<>(0, 2), axis::integer<>(0, 2)); h.at(std::vector(-2, 0)); } diff --git a/test/histogram_test.cpp b/test/histogram_test.cpp index 0cce79fa..83535985 100644 --- a/test/histogram_test.cpp +++ b/test/histogram_test.cpp @@ -76,8 +76,8 @@ void run_tests() { // init_2 { - auto h = make_histogram(Type(), axis::regular<>{3, -1, 1}, - axis::integer<>{-1, 2}); + auto h = make_histogram( + Type(), axis::regular<>{3, -1, 1}, axis::integer<>{-1, 2}); BOOST_TEST_EQ(h.dim(), 2); BOOST_TEST_EQ(h.size(), 25); BOOST_TEST_EQ(h.axis(0_c).shape(), 5); @@ -89,9 +89,9 @@ void run_tests() { // init_3 { - auto h = make_histogram(Type(), axis::regular<>{3, -1, 1}, - axis::integer<>{-1, 2}, - axis::circular<>{3}); + auto h = make_histogram( + Type(), axis::regular<>{3, -1, 1}, axis::integer<>{-1, 2}, + axis::circular<>{3}); BOOST_TEST_EQ(h.dim(), 3); BOOST_TEST_EQ(h.size(), 75); auto h2 = make_histogram>( @@ -136,8 +136,9 @@ void run_tests() { h(0, 0); auto h2 = decltype(h)(h); BOOST_TEST(h2 == h); - auto h3 = static_histogram, axis::integer<>>, - array_storage>(h); + auto h3 = + static_histogram, axis::integer<>>, + array_storage>(h); BOOST_TEST_EQ(h3, h); } @@ -153,8 +154,9 @@ void run_tests() { // test self-assign h2 = h2; BOOST_TEST_EQ(h, h2); - auto h3 = static_histogram, axis::integer<>>, - array_storage>(); + auto h3 = + static_histogram, axis::integer<>>, + array_storage>(); h3 = h; BOOST_TEST_EQ(h, h3); } @@ -183,8 +185,8 @@ void run_tests() { // axis methods { enum { A = 3, B = 5 }; - auto a = make_histogram(Type(), - axis::regular<>(1, 1, 2, "foo")); + auto a = make_histogram( + Type(), axis::regular<>(1, 1, 2, "foo")); BOOST_TEST_EQ(a.axis().size(), 1); BOOST_TEST_EQ(a.axis().shape(), 3); BOOST_TEST_EQ(a.axis().index(1), 0); @@ -203,7 +205,8 @@ void run_tests() { b.axis().label("foo"); BOOST_TEST_EQ(b.axis().label(), "foo"); - auto c = make_histogram(Type(), axis::category<>({A, B})); + auto c = + make_histogram(Type(), axis::category<>({A, B})); BOOST_TEST_EQ(c.axis().size(), 2); BOOST_TEST_EQ(c.axis().shape(), 2); BOOST_TEST_EQ(c.axis().index(A), 0); @@ -227,7 +230,8 @@ void run_tests() { BOOST_TEST(c != b); BOOST_TEST(a == c); BOOST_TEST(c == a); - auto d = make_histogram(Type(), axis::regular<>(2, 0, 1)); + auto d = + make_histogram(Type(), axis::regular<>(2, 0, 1)); BOOST_TEST(c != d); BOOST_TEST(d != c); c(0); @@ -425,8 +429,8 @@ void run_tests() { // add_1 { auto a = make_histogram(Type(), axis::integer<>(0, 2)); - auto b = - make_histogram>(Type(), axis::integer<>(0, 2)); + auto b = make_histogram>(Type(), + axis::integer<>(0, 2)); a(0); // 1 0 b(1); // 0 1 auto a2 = a; @@ -477,8 +481,8 @@ void run_tests() { { auto a = make_histogram>(Type(), axis::integer<>(-1, 2)); - auto b = - make_histogram>(Type(), axis::integer<>(-1, 2)); + auto b = make_histogram>(Type(), + axis::integer<>(-1, 2)); a(-1); b(1); auto c = a; @@ -896,8 +900,8 @@ void run_mixed_tests() { { auto a = make_histogram(T1{}, axis::regular<>{3, 0, 3}, axis::integer<>(0, 2)); - auto b = make_histogram>(T2{}, axis::regular<>{3, 0, 3}, - axis::integer<>(0, 2)); + auto b = make_histogram>( + T2{}, axis::regular<>{3, 0, 3}, axis::integer<>(0, 2)); BOOST_TEST_EQ(a, b); auto b2 = make_histogram(T2{}, axis::integer<>{0, 3}, axis::integer<>(0, 2)); @@ -926,8 +930,8 @@ void run_mixed_tests() { { auto a = make_histogram(T1{}, axis::regular<>{3, 0, 3}, axis::integer<>(0, 2)); - auto b = make_histogram>(T2{}, axis::regular<>{3, 0, 3}, - axis::integer<>(0, 2)); + auto b = make_histogram>( + T2{}, axis::regular<>{3, 0, 3}, axis::integer<>(0, 2)); a(1, 1); BOOST_TEST_NE(a, b); b = a; diff --git a/test/index_mapper_test.cpp b/test/index_mapper_test.cpp index 22e8e6a1..e0612fe4 100644 --- a/test/index_mapper_test.cpp +++ b/test/index_mapper_test.cpp @@ -4,11 +4,11 @@ // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) -#include "utility.hpp" -#include #include #include +#include #include +#include "utility.hpp" using namespace boost::histogram::detail; diff --git a/test/meta_test.cpp b/test/meta_test.cpp index ad3574f2..a890bcea 100644 --- a/test/meta_test.cpp +++ b/test/meta_test.cpp @@ -135,7 +135,8 @@ int main() { BOOST_TEST_TRAIT_TRUE((std::is_same, long&>)); BOOST_TEST_TRAIT_TRUE( (std::is_same, const long&>)); - BOOST_TEST_TRAIT_TRUE((std::is_same, long&&>)); + BOOST_TEST_TRAIT_TRUE( + (std::is_same, long&&>)); } // mp_set_union @@ -153,8 +154,8 @@ int main() { BOOST_TEST_TRAIT_TRUE((std::is_same, std::tuple>)); BOOST_TEST_TRAIT_TRUE((std::is_same, std::tuple>)); BOOST_TEST_TRAIT_TRUE((std::is_same, std::tuple>)); - BOOST_TEST_TRAIT_TRUE( - (std::is_same, std::tuple>)); + BOOST_TEST_TRAIT_TRUE(( + std::is_same, std::tuple>)); BOOST_TEST_TRAIT_TRUE( (std::is_same, std::tuple>)); BOOST_TEST_TRAIT_TRUE( diff --git a/test/speed_cpp.cpp b/test/speed_cpp.cpp index 75f8e101..838c6310 100644 --- a/test/speed_cpp.cpp +++ b/test/speed_cpp.cpp @@ -6,12 +6,12 @@ #include #include +#include #include #include #include -#include #include -#include +#include using namespace boost::histogram; using boost::mp11::mp_list; @@ -21,12 +21,10 @@ std::unique_ptr random_array(unsigned n, int type) { std::default_random_engine gen(1); if (type) { // type == 1 std::normal_distribution<> d(0.5, 0.3); - for (unsigned i = 0; i < n; ++i) - r[i] = d(gen); + for (unsigned i = 0; i < n; ++i) r[i] = d(gen); } else { // type == 0 std::uniform_real_distribution<> d(0.0, 1.0); - for (unsigned i = 0; i < n; ++i) - r[i] = d(gen); + for (unsigned i = 0; i < n; ++i) r[i] = d(gen); } return r; } @@ -58,8 +56,7 @@ double compare_1d(unsigned n, int distrib) { for (unsigned k = 0; k < 20; ++k) { auto h = Histogram(axis::regular<>(100, 0, 1)); auto t = clock(); - for (unsigned i = 0; i < n; ++i) - h(r[i]); + for (unsigned i = 0; i < n; ++i) h(r[i]); t = clock() - t; best = std::min(best, double(t) / CLOCKS_PER_SEC); } @@ -73,10 +70,10 @@ double compare_2d(unsigned n, int distrib) { auto best = std::numeric_limits::max(); for (unsigned k = 0; k < 20; ++k) { - auto h = Histogram(axis::regular<>(100, 0, 1), axis::regular<>(100, 0, 1)); + auto h = + Histogram(axis::regular<>(100, 0, 1), axis::regular<>(100, 0, 1)); auto t = clock(); - for (unsigned i = 0; i < n / 2; ++i) - h(r[2 * i], r[2 * i + 1]); + for (unsigned i = 0; i < n / 2; ++i) h(r[2 * i], r[2 * i + 1]); t = clock() - t; best = std::min(best, double(t) / CLOCKS_PER_SEC); } @@ -136,9 +133,8 @@ int main() { else printf("normal distribution\n"); printf("hs_ss %.3f\n", - compare_1d< - static_histogram>, array_storage>>( - nfill, itype)); + compare_1d>, + array_storage>>(nfill, itype)); printf("hs_sd %.3f\n", compare_1d< static_histogram>, adaptive_storage>>( @@ -147,8 +143,8 @@ int main() { compare_1d>>( nfill, itype)); printf("hd_sd %.3f\n", - compare_1d>(nfill, - itype)); + compare_1d>( + nfill, itype)); } printf("2D\n"); @@ -169,8 +165,8 @@ int main() { compare_2d>>( nfill, itype)); printf("hd_sd %.3f\n", - compare_2d>(nfill, - itype)); + compare_2d>( + nfill, itype)); } printf("3D\n"); @@ -191,8 +187,8 @@ int main() { compare_3d>>( nfill, itype)); printf("hd_sd %.3f\n", - compare_3d>(nfill, - itype)); + compare_3d>( + nfill, itype)); } printf("6D\n"); @@ -215,7 +211,7 @@ int main() { compare_6d>>( nfill, itype)); printf("hd_sd %.3f\n", - compare_6d>(nfill, - itype)); + compare_6d>( + nfill, itype)); } } diff --git a/test/speed_gsl.cpp b/test/speed_gsl.cpp index c31c75dd..b333df2a 100644 --- a/test/speed_gsl.cpp +++ b/test/speed_gsl.cpp @@ -11,20 +11,18 @@ #include #include #include -#include #include +#include std::unique_ptr random_array(unsigned n, int type) { std::unique_ptr r(new double[n]); std::default_random_engine gen(1); if (type) { // type == 1 std::normal_distribution<> d(0.5, 0.3); - for (unsigned i = 0; i < n; ++i) - r[i] = d(gen); + for (unsigned i = 0; i < n; ++i) r[i] = d(gen); } else { // type == 0 std::uniform_real_distribution<> d(0.0, 1.0); - for (unsigned i = 0; i < n; ++i) - r[i] = d(gen); + for (unsigned i = 0; i < n; ++i) r[i] = d(gen); } return r; } @@ -37,8 +35,7 @@ void compare_1d(unsigned n, int distrib) { gsl_histogram* h = gsl_histogram_alloc(100); gsl_histogram_set_ranges_uniform(h, 0, 1); auto t = clock(); - for (unsigned i = 0; i < n; ++i) - gsl_histogram_increment(h, r[i]); + for (unsigned i = 0; i < n; ++i) gsl_histogram_increment(h, r[i]); t = clock() - t; best = std::min(best, double(t) / CLOCKS_PER_SEC); gsl_histogram_free(h); diff --git a/test/speed_root.cpp b/test/speed_root.cpp index 8a24bc28..dca1c6cf 100644 --- a/test/speed_root.cpp +++ b/test/speed_root.cpp @@ -13,20 +13,18 @@ #include #include #include -#include #include +#include std::unique_ptr random_array(unsigned n, int type) { std::unique_ptr r(new double[n]); std::default_random_engine gen(1); if (type) { // type == 1 std::normal_distribution<> d(0.5, 0.3); - for (unsigned i = 0; i < n; ++i) - r[i] = d(gen); + for (unsigned i = 0; i < n; ++i) r[i] = d(gen); } else { // type == 0 std::uniform_real_distribution<> d(0.0, 1.0); - for (unsigned i = 0; i < n; ++i) - r[i] = d(gen); + for (unsigned i = 0; i < n; ++i) r[i] = d(gen); } return r; } @@ -38,8 +36,7 @@ void compare_1d(unsigned n, int distrib) { for (unsigned k = 0; k < 20; ++k) { TH1I hroot("", "", 100, 0, 1); auto t = clock(); - for (unsigned i = 0; i < n; ++i) - hroot.Fill(r[i]); + for (unsigned i = 0; i < n; ++i) hroot.Fill(r[i]); t = clock() - t; best_root = std::min(best_root, double(t) / CLOCKS_PER_SEC); } @@ -53,8 +50,7 @@ void compare_2d(unsigned n, int distrib) { for (unsigned k = 0; k < 20; ++k) { TH2I hroot("", "", 100, 0, 1, 100, 0, 1); auto t = clock(); - for (unsigned i = 0; i < n / 2; ++i) - hroot.Fill(r[2 * i], r[2 * i + 1]); + for (unsigned i = 0; i < n / 2; ++i) hroot.Fill(r[2 * i], r[2 * i + 1]); t = clock() - t; best_root = std::min(best_root, double(t) / CLOCKS_PER_SEC); } @@ -87,9 +83,7 @@ void compare_6d(unsigned n, int distrib) { THnI hroot("", "", 6, &bin.front(), &min.front(), &max.front()); auto t = clock(); - for (unsigned i = 0; i < n / 6; ++i) { - hroot.Fill(r.get() + 6 * i); - } + for (unsigned i = 0; i < n / 6; ++i) { hroot.Fill(r.get() + 6 * i); } t = clock() - t; best_root = std::min(best_root, double(t) / CLOCKS_PER_SEC); } diff --git a/test/utility.hpp b/test/utility.hpp index b18a17da..c44a629b 100644 --- a/test/utility.hpp +++ b/test/utility.hpp @@ -22,8 +22,7 @@ namespace std { // never add to std, we only do it to get ADL working template ostream& operator<<(ostream& os, const vector& v) { os << "[ "; - for (const auto& x : v) - os << x << " "; + for (const auto& x : v) os << x << " "; os << "]"; return os; }