forked from tcbrindle/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into pr/set-algorithms
- Loading branch information
Showing
51 changed files
with
1,462 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
// Copyright (c) 2023 Tristan Brindle (tcbrindle at gmail dot com) | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include <flux.hpp> | ||
|
||
#include <array> | ||
#include <iostream> | ||
#include <string_view> | ||
|
||
using namespace std::string_view_literals; | ||
|
||
int main() | ||
{ | ||
std::array const array{"alpha"sv, "bravo"sv, "charlie"sv, "delta"sv, "echo"sv}; | ||
|
||
auto long_words = flux::drop(array, 2); | ||
|
||
// We can use the cursors() adaptor to iterate over the cursors of the | ||
// sequence (in this case integer indices) and use those to read from the | ||
// original sequence | ||
for (auto idx : flux::cursors(long_words)) { | ||
std::cout << idx << ": " << long_words[idx] << '\n'; | ||
} | ||
// prints | ||
// 2: charlie | ||
// 3: delta | ||
// 4: echo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
// Copyright (c) 2023 Tristan Brindle (tcbrindle at gmail dot com) | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include <flux.hpp> | ||
|
||
#include <array> | ||
#include <cassert> | ||
#include <string_view> | ||
|
||
using namespace std::string_view_literals; | ||
|
||
int main() | ||
{ | ||
// flux::repeat(val) is a random-access sequence which endlessly repeats | ||
// the given value | ||
auto seq = flux::repeat(3); | ||
|
||
auto cursor = flux::first(seq); | ||
assert(flux::read_at(seq, cursor) == 3); | ||
// fast-forward the cursor a lot... | ||
cursor = flux::next(seq, cursor, 1'000'000); | ||
assert(flux::read_at(seq, cursor) == 3); // still returning 3! | ||
|
||
// We could use the take adaptor to make a repeat sequence finite... | ||
auto taken = flux::take(seq, 5); | ||
assert(flux::equal(taken, std::array{3, 3, 3, 3, 3})); | ||
|
||
// ...but it's easier to use repeat(val, count) instead | ||
auto police = flux::repeat("hello"sv, 3); | ||
assert(flux::equal(police, std::array{"hello", "hello", "hello"})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
|
||
// Copyright (c) 2023 Tristan Brindle (tcbrindle at gmail dot com) | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef FLUX_OP_CURSORS_HPP_INCLUDED | ||
#define FLUX_OP_CURSORS_HPP_INCLUDED | ||
|
||
#include <flux/core.hpp> | ||
|
||
namespace flux { | ||
|
||
namespace detail { | ||
|
||
template <typename Base> | ||
struct cursors_adaptor : inline_sequence_base<cursors_adaptor<Base>> { | ||
private: | ||
Base base_; | ||
|
||
public: | ||
constexpr explicit cursors_adaptor(decays_to<Base> auto&& base) | ||
: base_(FLUX_FWD(base)) | ||
{} | ||
|
||
struct flux_sequence_traits { | ||
|
||
static inline constexpr bool is_infinite = infinite_sequence<Base>; | ||
|
||
static constexpr auto first(auto& self) -> decltype(flux::first(self.base_)) | ||
{ | ||
return flux::first(self.base_); | ||
} | ||
|
||
static constexpr auto is_last(auto& self, cursor_t<Base> const& cur) -> bool | ||
{ | ||
return flux::is_last(self.base_, cur); | ||
} | ||
|
||
static constexpr auto inc(auto& self, cursor_t<Base>& cur) -> void | ||
{ | ||
flux::inc(self.base_, cur); | ||
} | ||
|
||
static constexpr auto read_at(auto&, cursor_t<Base> const& cur) | ||
-> cursor_t<Base> | ||
{ | ||
return cur; | ||
} | ||
|
||
static constexpr auto dec(auto& self, cursor_t<Base>& cur) -> void | ||
requires bidirectional_sequence<Base> | ||
{ | ||
flux::dec(self.base_, cur); | ||
} | ||
|
||
static constexpr auto inc(auto& self, cursor_t<Base>& cur, distance_t offset) -> void | ||
requires random_access_sequence<Base> | ||
{ | ||
flux::inc(self.base_, cur, offset); | ||
} | ||
|
||
static constexpr auto distance(auto& self, cursor_t<Base> const& from, | ||
cursor_t<Base> const& to) -> distance_t | ||
requires random_access_sequence<Base> | ||
{ | ||
return flux::distance(self.base_, from, to); | ||
} | ||
|
||
static constexpr auto last(auto& self) -> cursor_t<Base> | ||
requires bounded_sequence<Base> | ||
{ | ||
return flux::last(self.base_); | ||
} | ||
|
||
static constexpr auto size(auto& self) -> distance_t | ||
requires sized_sequence<Base> | ||
{ | ||
return flux::size(self.base_); | ||
} | ||
}; | ||
}; | ||
|
||
struct cursors_fn { | ||
template <adaptable_sequence Seq> | ||
requires multipass_sequence<Seq> | ||
[[nodiscard]] | ||
constexpr auto operator()(Seq&& seq) const -> sequence auto | ||
{ | ||
return cursors_adaptor<std::decay_t<Seq>>(FLUX_FWD(seq)); | ||
} | ||
}; | ||
|
||
} // namespace detail | ||
|
||
inline constexpr auto cursors = detail::cursors_fn{}; | ||
|
||
template <typename D> | ||
constexpr auto inline_sequence_base<D>::cursors() && | ||
requires multipass_sequence<D> | ||
{ | ||
return flux::cursors(std::move(derived())); | ||
} | ||
|
||
} // namespace flux | ||
|
||
#endif // FLUX_OP_CURSORS_HPP_INCLUDED |
Oops, something went wrong.