Skip to content

Commit b50cb3c

Browse files
authored
Add div_pos_int_ceil and split_evenly (#299)
1 parent 0b5d720 commit b50cb3c

File tree

7 files changed

+75
-0
lines changed

7 files changed

+75
-0
lines changed

include/fplus/curry_instances.autogenerated_defines

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ fplus_curry_define_fn_2(insert_at_idx)
394394
fplus_curry_define_fn_1(partition)
395395
fplus_curry_define_fn_1(split_at_idxs)
396396
fplus_curry_define_fn_1(split_every)
397+
fplus_curry_define_fn_1(split_evenly)
397398
fplus_curry_define_fn_2(split_by_token)
398399
fplus_curry_define_fn_1(run_length_encode_by)
399400
fplus_curry_define_fn_0(run_length_encode)

include/fplus/fwd_instances.autogenerated_defines

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ fplus_fwd_define_fn_2(insert_at_idx)
394394
fplus_fwd_define_fn_1(partition)
395395
fplus_fwd_define_fn_1(split_at_idxs)
396396
fplus_fwd_define_fn_1(split_every)
397+
fplus_fwd_define_fn_1(split_evenly)
397398
fplus_fwd_define_fn_2(split_by_token)
398399
fplus_fwd_define_fn_1(run_length_encode_by)
399400
fplus_fwd_define_fn_0(run_length_encode)
@@ -693,6 +694,7 @@ fplus_fwd_flip_define_fn_1(split_at_idx)
693694
fplus_fwd_flip_define_fn_1(partition)
694695
fplus_fwd_flip_define_fn_1(split_at_idxs)
695696
fplus_fwd_flip_define_fn_1(split_every)
697+
fplus_fwd_flip_define_fn_1(split_evenly)
696698
fplus_fwd_flip_define_fn_1(run_length_encode_by)
697699
fplus_fwd_flip_define_fn_1(span)
698700
fplus_fwd_flip_define_fn_1(aperture)

include/fplus/numeric.hpp

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

836+
// API search type: div_pos_int_ceil : (a, a) -> a
837+
// Positive 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+
{
842+
static_assert(std::is_integral<X>::value, "type must be integral");
843+
static_assert(!std::is_signed<X>::value, "type must be unsigned");
844+
return numerator / denominator + (numerator % denominator != 0);
845+
}
846+
836847
// API search type: histogram_using_intervals : ([(a, a)], [a]) -> [((a, a), Int)]
837848
// fwd bind count: 1
838849
// Generate a histogram of a sequence with given bins.

include/fplus/split.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,24 @@ ContainerOut split_every(std::size_t n, const ContainerIn& xs)
492492
xs);
493493
}
494494

495+
// API search type: split_evenly : (Int, [a]) -> [[a]]
496+
// fwd bind count: 1
497+
// Split a sequence into n similarly-sized chunks.
498+
// split_evenly(2, [0,1,2,3,4]) == [[0,1,2],[3,4]]
499+
template <typename ContainerIn,
500+
typename ContainerOut = std::vector<ContainerIn>>
501+
ContainerOut split_evenly(std::size_t n, const ContainerIn& xs)
502+
{
503+
const std::size_t every_n = div_pos_int_ceil(size_of_cont(xs), n);
504+
return split_at_idxs<
505+
std::vector<std::size_t>,
506+
ContainerIn,
507+
ContainerOut>(
508+
numbers_step<std::size_t>(
509+
every_n, size_of_cont(xs), every_n),
510+
xs);
511+
}
512+
495513
// API search type: split_by_token : ([a], Bool, [a]) -> [[a]]
496514
// fwd bind count: 2
497515
// Split a sequence at every segment matching a token.

include_all_in_one/include/fplus/fplus.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8351,6 +8351,17 @@ std::function<X(X)> divide_by(const X& x)
83518351
};
83528352
}
83538353

8354+
// API search type: div_pos_int_ceil : (a, a) -> a
8355+
// Positive 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+
{
8360+
static_assert(std::is_integral<X>::value, "type must be integral");
8361+
static_assert(!std::is_signed<X>::value, "type must be unsigned");
8362+
return numerator / denominator + (numerator % denominator != 0);
8363+
}
8364+
83548365
// API search type: histogram_using_intervals : ([(a, a)], [a]) -> [((a, a), Int)]
83558366
// fwd bind count: 1
83568367
// Generate a histogram of a sequence with given bins.
@@ -10762,6 +10773,24 @@ ContainerOut split_every(std::size_t n, const ContainerIn& xs)
1076210773
xs);
1076310774
}
1076410775

10776+
// API search type: split_evenly : (Int, [a]) -> [[a]]
10777+
// fwd bind count: 1
10778+
// Split a sequence into n similarly-sized chunks.
10779+
// split_evenly(2, [0,1,2,3,4]) == [[0,1,2],[3,4]]
10780+
template <typename ContainerIn,
10781+
typename ContainerOut = std::vector<ContainerIn>>
10782+
ContainerOut split_evenly(std::size_t n, const ContainerIn& xs)
10783+
{
10784+
const std::size_t every_n = div_pos_int_ceil(size_of_cont(xs), n);
10785+
return split_at_idxs<
10786+
std::vector<std::size_t>,
10787+
ContainerIn,
10788+
ContainerOut>(
10789+
numbers_step<std::size_t>(
10790+
every_n, size_of_cont(xs), every_n),
10791+
xs);
10792+
}
10793+
1076510794
// API search type: split_by_token : ([a], Bool, [a]) -> [[a]]
1076610795
// fwd bind count: 2
1076710796
// Split a sequence at every segment matching a token.
@@ -15094,6 +15123,7 @@ fplus_curry_define_fn_2(insert_at_idx)
1509415123
fplus_curry_define_fn_1(partition)
1509515124
fplus_curry_define_fn_1(split_at_idxs)
1509615125
fplus_curry_define_fn_1(split_every)
15126+
fplus_curry_define_fn_1(split_evenly)
1509715127
fplus_curry_define_fn_2(split_by_token)
1509815128
fplus_curry_define_fn_1(run_length_encode_by)
1509915129
fplus_curry_define_fn_0(run_length_encode)
@@ -15702,6 +15732,7 @@ fplus_fwd_define_fn_2(insert_at_idx)
1570215732
fplus_fwd_define_fn_1(partition)
1570315733
fplus_fwd_define_fn_1(split_at_idxs)
1570415734
fplus_fwd_define_fn_1(split_every)
15735+
fplus_fwd_define_fn_1(split_evenly)
1570515736
fplus_fwd_define_fn_2(split_by_token)
1570615737
fplus_fwd_define_fn_1(run_length_encode_by)
1570715738
fplus_fwd_define_fn_0(run_length_encode)
@@ -16001,6 +16032,7 @@ fplus_fwd_flip_define_fn_1(split_at_idx)
1600116032
fplus_fwd_flip_define_fn_1(partition)
1600216033
fplus_fwd_flip_define_fn_1(split_at_idxs)
1600316034
fplus_fwd_flip_define_fn_1(split_every)
16035+
fplus_fwd_flip_define_fn_1(split_evenly)
1600416036
fplus_fwd_flip_define_fn_1(run_length_encode_by)
1600516037
fplus_fwd_flip_define_fn_1(span)
1600616038
fplus_fwd_flip_define_fn_1(aperture)

test/numeric_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ 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+
const std::uint64_t numerator = 5;
293+
const std::uint64_t denominator = 3;
294+
REQUIRE_EQ(div_pos_int_ceil(numerator, denominator), 2);
295+
}
296+
289297
TEST_CASE("numeric_test - reference_interval")
290298
{
291299
using namespace fplus;

test/split_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ TEST_CASE("split_test - split functions")
9191
REQUIRE_EQ(split_at_idx(2, xs), std::make_pair(IntVector({ 1, 2 }), IntVector({ 2, 3, 2 })));
9292
REQUIRE_EQ(partition(is_even_int, xs), std::make_pair(IntVector({ 2, 2, 2 }), IntVector({ 1, 3 })));
9393
REQUIRE_EQ(split_every(2, xs), IntVectors({ { 1, 2 }, { 2, 3 }, { 2 } }));
94+
REQUIRE_EQ(split_evenly(1, xs), IntVectors({ { 1, 2, 2, 3, 2 } }));
95+
REQUIRE_EQ(split_evenly(2, xs), IntVectors({ { 1, 2, 2 }, { 3, 2 } }));
96+
REQUIRE_EQ(split_evenly(3, xs), IntVectors({ { 1, 2 }, { 2, 3 }, { 2 } }));
9497
auto splittedAt1And3 = split_at_idxs(IdxVector({ 1, 3 }), xs);
9598
IntVectors splittedAt1And3Dest = { IntVector({ 1 }), IntVector({ 2, 2 }), IntVector({ 3, 2 }) };
9699
REQUIRE_EQ(splittedAt1And3, splittedAt1And3Dest);

0 commit comments

Comments
 (0)