Skip to content

Commit abfa9c8

Browse files
committed
Add div_pos_int_ceil function
1 parent 14750f7 commit abfa9c8

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

include/fplus/numeric.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,14 @@ std::function<X(X)> divide_by(const X& x)
833833
};
834834
}
835835

836+
// API search type: div_pos_int_ceil : (a, a) -> a
837+
// Integer division, but rounding up instead of down.
838+
// div_pos_int_ceil(5, 3) == 2
839+
template<typename X>
840+
static auto div_pos_int_ceil(X numerator, X denominator) {
841+
return numerator / denominator + (numerator % denominator != 0);
842+
}
843+
836844
// API search type: histogram_using_intervals : ([(a, a)], [a]) -> [((a, a), Int)]
837845
// fwd bind count: 1
838846
// Generate a histogram of a sequence with given bins.

include/fplus/split.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ template <typename ContainerIn,
500500
typename ContainerOut = std::vector<ContainerIn>>
501501
ContainerOut split_evenly(std::size_t n, const ContainerIn& xs)
502502
{
503-
const std::size_t every_n = size_of_cont(xs) / n;
503+
const std::size_t every_n = div_pos_int_ceil(size_of_cont(xs), n);
504504
return split_at_idxs<
505505
std::vector<std::size_t>,
506506
ContainerIn,

include_all_in_one/include/fplus/fplus.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8351,6 +8351,14 @@ std::function<X(X)> divide_by(const X& x)
83518351
};
83528352
}
83538353

8354+
// API search type: div_pos_int_ceil : (a, a) -> a
8355+
// Integer division, but rounding up instead of down.
8356+
// div_pos_int_ceil(5, 3) == 2
8357+
template<typename X>
8358+
static auto div_pos_int_ceil(X numerator, X denominator) {
8359+
return numerator / denominator + (numerator % denominator != 0);
8360+
}
8361+
83548362
// API search type: histogram_using_intervals : ([(a, a)], [a]) -> [((a, a), Int)]
83558363
// fwd bind count: 1
83568364
// Generate a histogram of a sequence with given bins.
@@ -10770,7 +10778,7 @@ template <typename ContainerIn,
1077010778
typename ContainerOut = std::vector<ContainerIn>>
1077110779
ContainerOut split_evenly(std::size_t n, const ContainerIn& xs)
1077210780
{
10773-
const std::size_t every_n = size_of_cont(xs) / n;
10781+
const std::size_t every_n = div_pos_int_ceil(size_of_cont(xs), n);
1077410782
return split_at_idxs<
1077510783
std::vector<std::size_t>,
1077610784
ContainerIn,

test/numeric_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ TEST_CASE("numeric_test - ceil_to_int_mult")
286286
REQUIRE_EQ(ceil_to_int_mult(1, 1), 1);
287287
}
288288

289+
TEST_CASE("numeric_test - div_pos_int_ceil")
290+
{
291+
using namespace fplus;
292+
REQUIRE_EQ(div_pos_int_ceil(5, 3), 2);
293+
}
294+
289295
TEST_CASE("numeric_test - reference_interval")
290296
{
291297
using namespace fplus;

0 commit comments

Comments
 (0)