Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
19c2a15
added the move algorithm on line 1265 of algorithm and created a new …
ahanamuk Jun 8, 2020
354e48b
added cmake file
ahanamuk Jun 8, 2020
09f6c9f
passing all tests for ranges move algorithm
ahanamuk Jun 8, 2020
61375a9
updated move algo tests to check that we actually move elems, not jus…
ahanamuk Jun 9, 2020
68640ed
enabled constexpr, cleaned up move algo test
ahanamuk Jun 9, 2020
5277552
removed iostream unused header
ahanamuk Jun 9, 2020
45fb14d
mainly updated formatting/comments based on feedback
ahanamuk Jun 10, 2020
80e349e
Delete cmake
ahanamuk Jun 10, 2020
d4f91b0
modified a test
ahanamuk Jun 11, 2020
a685197
Merge branch 'move_algo' of https://github.com/ahanamuk/STL
ahanamuk Jun 11, 2020
c9f134f
initial fill algo
ahanamuk Jun 11, 2020
93ff947
Passing all fill tests
ahanamuk Jun 11, 2020
be947f1
added a test case
ahanamuk Jun 11, 2020
5cb7bfc
minor changes/formatting updates to move
ahanamuk Jun 12, 2020
9170077
updated clang formatting
ahanamuk Jun 12, 2020
d4fbaac
initial fill algo
ahanamuk Jun 11, 2020
064684e
comitting untracked stuff
ahanamuk Jun 16, 2020
91040f9
generate initial commit
ahanamuk Jun 16, 2020
dd51ae0
minor changes
ahanamuk Jun 16, 2020
62689da
Merge branch 'master' into generate_algo
CaseyCarter Jun 16, 2020
e2c8965
generate_n tests added
ahanamuk Jun 16, 2020
cc7220a
similar changes as casey made in fill_n
ahanamuk Jun 16, 2020
f7cf046
Merge pull request #2 from CaseyCarter/generate
ahanamuk Jun 16, 2020
e61aa5f
generate and generate_n tests passing now
ahanamuk Jun 17, 2020
9c8acb3
minor change
ahanamuk Jun 17, 2020
63a57a4
improved style
ahanamuk Jun 22, 2020
b0fb033
added copyright banner
ahanamuk Jun 22, 2020
1c7b54d
minor deindented algo
ahanamuk Jun 22, 2020
e13beda
tried to write a better test for generate but it threw a bunch of err…
ahanamuk Jun 23, 2020
b8a91e3
trimmed white space
ahanamuk Jun 23, 2020
947f2dd
made the function being passed into generate be non-constant
ahanamuk Jun 23, 2020
c51d3a0
++i instead of i++
ahanamuk Jun 24, 2020
9b579b0
Merge branch 'master' into generate_algo
CaseyCarter Jun 24, 2020
d954405
Run clang-format after merging.
StephanTLavavej Jun 25, 2020
3bb5a62
fixed toda condition and lambda function testing
ahanamuk Jun 25, 2020
8cf5aa7
Merge remote-tracking branch 'origin/master' into generate
CaseyCarter Jul 2, 2020
37b38d9
tiny fixes:
CaseyCarter Jul 2, 2020
a985e79
added test for 0 and -1 for generate_n
ahanamuk Jul 6, 2020
9756d7b
Merge remote-tracking branch 'origin/master' into generate
CaseyCarter Jul 7, 2020
b2d281a
Review comments; modernize
CaseyCarter Jul 8, 2020
053cda7
Review comments
CaseyCarter Jul 8, 2020
40ddeac
Apply suggestions from code review
CaseyCarter Jul 9, 2020
81b8b70
Miya's 11th hour save
CaseyCarter Jul 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions stl/inc/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -3169,6 +3169,74 @@ namespace ranges {
};

inline constexpr _Fill_n_fn fill_n{_Not_quite_object::_Construct_tag{}};

// VARIABLE ranges::generate
class _Generate_fn : private _Not_quite_object {
public:
using _Not_quite_object::_Not_quite_object;

// clang-format off
template <input_or_output_iterator _Out, sentinel_for<_Out> _Se, copy_constructible _Fn>
requires invocable<_Fn&> && indirectly_writable<_Out, invoke_result_t<_Fn&>>
constexpr _Out operator()(_Out _First, _Se _Last, _Fn _Gen) const {
_Adl_verify_range(_First, _Last);
_Seek_wrapped(_First, _Generate_unchecked(_Get_unwrapped(_STD move(_First)),
_Get_unwrapped(_STD move(_Last)), _Pass_fn(_Gen)));
return _First;
}

template <class _Rng, copy_constructible _Fn>
requires invocable<_Fn&> && output_range<_Rng, invoke_result_t<_Fn&>>
constexpr borrowed_iterator_t<_Rng> operator()(_Rng&& _Range, _Fn _Gen) const {
auto _First = _RANGES begin(_Range);
_Seek_wrapped(
_First, _Generate_unchecked(_Get_unwrapped(_STD move(_First)), _Uend(_Range), _Pass_fn(_Gen)));
return _First;
}
// clang-format on
private:
template <class _Out, class _Se, class _Fn>
_NODISCARD static constexpr _Out _Generate_unchecked(_Out _First, const _Se _Last, _Fn _Gen) {
_STL_INTERNAL_STATIC_ASSERT(input_or_output_iterator<_Out>);
_STL_INTERNAL_STATIC_ASSERT(sentinel_for<_Se, _Out>);
_STL_INTERNAL_STATIC_ASSERT(copy_constructible<_Fn>);
_STL_INTERNAL_STATIC_ASSERT(invocable<_Fn&>);
_STL_INTERNAL_STATIC_ASSERT(indirectly_writable<_Out, invoke_result_t<_Fn&>>);

for (; _First != _Last; ++_First) {
*_First = _Gen();
}

return _First;
}
};

inline constexpr _Generate_fn generate{_Not_quite_object::_Construct_tag{}};

// VARIABLE ranges::generate_n
class _Generate_n_fn : private _Not_quite_object {
public:
using _Not_quite_object::_Not_quite_object;

// clang-format off
template <input_or_output_iterator _Out, copy_constructible _Fn>
requires invocable<_Fn&> && indirectly_writable<_Out, invoke_result_t<_Fn&>>
constexpr _Out operator()(_Out _First, iter_difference_t<_Out> _Count, _Fn _Gen) const {
if (_Count > 0) {
auto _UFirst = _Get_unwrapped_n(_STD move(_First), _Count);
do {
*_UFirst = _Gen();
++_UFirst;
} while (--_Count > 0);

_Seek_wrapped(_First, _STD move(_UFirst));
}

return _First;
}
};

inline constexpr _Generate_n_fn generate_n{_Not_quite_object::_Construct_tag{}};
} // namespace ranges
#endif // __cpp_lib_concepts

Expand Down
2 changes: 2 additions & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ tests\P0896R4_ranges_alg_find_if
tests\P0896R4_ranges_alg_find_if_not
tests\P0896R4_ranges_alg_for_each
tests\P0896R4_ranges_alg_for_each_n
tests\P0896R4_ranges_alg_generate
tests\P0896R4_ranges_alg_generate_n
tests\P0896R4_ranges_alg_heap
tests\P0896R4_ranges_alg_is_permutation
tests\P0896R4_ranges_alg_is_sorted
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_generate/env.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\concepts_matrix.lst
50 changes: 50 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_generate/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <algorithm>
#include <cassert>
#include <concepts>
#include <ranges>

#include <range_algorithm_support.hpp>

using namespace std;

constexpr auto iota_gen = [val = 0]() mutable { return val++; };

// Validate dangling story
STATIC_ASSERT(same_as<decltype(ranges::generate(borrowed<false>{}, iota_gen)), ranges::dangling>);
STATIC_ASSERT(same_as<decltype(ranges::generate(borrowed<true>{}, iota_gen)), int*>);

struct instantiator {
template <ranges::output_range<const int&> Out>
static constexpr void call() {
using ranges::generate, ranges::iterator_t;

{
int output[] = {13, 42, 1367};
Out out_wrapper{output};
auto result = generate(out_wrapper, iota_gen);
STATIC_ASSERT(same_as<decltype(result), iterator_t<Out>>);
assert(result == out_wrapper.end());
for (int i = 0; i < 3; ++i) {
assert(i == output[i]);
}
}
{
int output[] = {13, 42, 1367};
Out out_wrapper{output};
auto result = generate(out_wrapper.begin(), out_wrapper.end(), iota_gen);
STATIC_ASSERT(same_as<decltype(result), iterator_t<Out>>);
assert(result == out_wrapper.end());
for (int i = 0; i < 3; ++i) {
assert(i == output[i]);
}
}
}
};

int main() {
STATIC_ASSERT((test_out<instantiator, int>(), true));
test_out<instantiator, int>();
}
4 changes: 4 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_generate_n/env.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\concepts_matrix.lst
53 changes: 53 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_generate_n/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <algorithm>
#include <cassert>
#include <concepts>
#include <ranges>

#include <range_algorithm_support.hpp>

using namespace std;

struct instantiator {
template <ranges::output_range<const int&> Out>
static constexpr void call() {
using ranges::generate_n, ranges::equal, ranges::iterator_t;

const auto iota_gen = [val = 0]() mutable { return val++; };

{
int output[] = {13, 42, 1367};
Out out_wrapper{output};
auto result = generate_n(out_wrapper.begin(), ranges::distance(output), iota_gen);
STATIC_ASSERT(same_as<decltype(result), iterator_t<Out>>);
assert(result == out_wrapper.end());
for (int i = 0; i < 3; ++i) {
assert(i == output[i]);
}
}

constexpr int expected_output[] = {13, 42, 1367};
int output[] = {13, 42, 1367};
{
Out out_wrapper{output};
auto result = generate_n(out_wrapper.begin(), 0, iota_gen);
STATIC_ASSERT(same_as<decltype(result), iterator_t<Out>>);
assert(result.peek() == output);
assert(equal(output, expected_output));
}
{
Out out_wrapper{output};
auto result = generate_n(out_wrapper.begin(), -1, iota_gen);
STATIC_ASSERT(same_as<decltype(result), iterator_t<Out>>);
assert(result.peek() == output);
assert(equal(output, expected_output));
}
}
};

int main() {
STATIC_ASSERT((test_out<instantiator, int>(), true));
test_out<instantiator, int>();
}