Skip to content

Commit

Permalink
Removed some cxx 11 ifdefs for non constexpr variants + more wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaaserne committed Jan 27, 2025
1 parent 8d2b1ab commit dc2c899
Show file tree
Hide file tree
Showing 34 changed files with 504 additions and 542 deletions.
2 changes: 1 addition & 1 deletion include/Lz/c_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LZ_MODULE_EXPORT_SCOPE_BEGIN
* auto cstr = "Hello" | lz::c_string;
* ```
*/
detail::c_string_adaptor detail::c_string_adaptor::c_string{};
detail::c_string_adaptor c_string{};

#else

Expand Down
2 changes: 1 addition & 1 deletion include/Lz/chunk_if.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ LZ_MODULE_EXPORT_SCOPE_BEGIN
* }
* ```
*/
detail::chunk_if_adaptor<void> detail::chunk_if_adaptor<void>::chunk_if{};
detail::chunk_if_adaptor<void> chunk_if{};

/**
* @brief This adaptor is used to make chunks of the iterable, based on a condition returned by the function passed. The iterator
Expand Down
2 changes: 1 addition & 1 deletion include/Lz/chunks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LZ_MODULE_EXPORT_SCOPE_BEGIN
* auto chunked = vec | lz::chunks(3); // chunked = { {1, 2, 3}, {4, 5} }
* ```
*/
detail::chunks_adaptor detail::chunks_adaptor::chunks;
detail::chunks_adaptor chunks;

#else

Expand Down
2 changes: 1 addition & 1 deletion include/Lz/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ LZ_MODULE_EXPORT_SCOPE_BEGIN
* @param iterable The iterable to create a common view from.
* @return A common view object. Can be used in <algorithm> functions.
*/
detail::common_adaptor detail::common_adaptor::common{};
detail::common_adaptor common{};

#else

Expand Down
4 changes: 0 additions & 4 deletions include/Lz/detail/adaptors/c_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
namespace lz {
namespace detail {
struct c_string_adaptor {
#ifdef LZ_HAS_CXX_11
static c_string_adaptor c_string;
#endif

using adaptor = c_string_adaptor;

template<class C>
Expand Down
8 changes: 0 additions & 8 deletions include/Lz/detail/adaptors/chunk_if.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ namespace lz {
namespace detail {
template<class ValueType>
struct chunk_if_adaptor {
#ifdef LZ_HAS_CXX_11
static chunk_if_adaptor<ValueType> chunk_if;
#endif

using adaptor = chunk_if_adaptor<ValueType>;

template<LZ_CONCEPT_ITERABLE Iterable, class UnaryPredicate>
Expand All @@ -32,10 +28,6 @@ struct chunk_if_adaptor {

template<>
struct chunk_if_adaptor<void> {
#ifdef LZ_HAS_CXX_11
static chunk_if_adaptor<void> chunk_if;
#endif

using adaptor = chunk_if_adaptor<void>;

template<LZ_CONCEPT_ITERABLE Iterable, class UnaryPredicate>
Expand Down
4 changes: 0 additions & 4 deletions include/Lz/detail/adaptors/chunks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ namespace lz {
namespace detail {

struct chunks_adaptor {
#ifdef LZ_HAS_CXX_11
static chunks_adaptor chunks;
#endif

using adaptor = chunks_adaptor;

template<LZ_CONCEPT_ITERABLE Iterable>
Expand Down
4 changes: 0 additions & 4 deletions include/Lz/detail/adaptors/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
namespace lz {
namespace detail {
struct common_adaptor {
#ifdef LZ_HAS_CXX_11
static common_adaptor common;
#endif

using adaptor = common_adaptor;

template<LZ_CONCEPT_ITERABLE Iterable>
Expand Down
4 changes: 0 additions & 4 deletions include/Lz/detail/adaptors/drop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
namespace lz {
namespace detail {
struct drop_adaptor {
#ifdef LZ_HAS_CXX_11
static drop_adaptor drop;
#endif

using adaptor = drop_adaptor;

template<LZ_CONCEPT_ITERABLE Iterable>
Expand Down
4 changes: 0 additions & 4 deletions include/Lz/detail/adaptors/drop_while.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
namespace lz {
namespace detail {
struct drop_while_adaptor {
#ifdef LZ_HAS_CXX_11
static drop_while_adaptor drop_while;
#endif

using adaptor = drop_while_adaptor;

template<class Iterable, class UnaryPredicate>
Expand Down
4 changes: 0 additions & 4 deletions include/Lz/detail/adaptors/enumerate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
namespace lz {
namespace detail {
struct enumerate_adaptor {
#ifdef LZ_HAS_CXX_11
static enumerate_adaptor enumerate;
#endif

using adaptor = enumerate_adaptor;

template<LZ_CONCEPT_ITERABLE Iterable, class IntType = int>
Expand Down
4 changes: 0 additions & 4 deletions include/Lz/detail/adaptors/except.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
namespace lz {
namespace detail {
struct except_adaptor {
#ifndef LZ_HAS_CXX_11
static except_adaptor except;
#endif

using adaptor = except_adaptor;

template<LZ_CONCEPT_ITERABLE Iterable1, LZ_CONCEPT_ITERABLE Iterable2,
Expand Down
6 changes: 1 addition & 5 deletions include/Lz/detail/adaptors/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

namespace lz {
namespace detail {
struct map_adaptor {
#ifdef LZ_HAS_CXX_11
static map_adaptor map;
#endif

struct map_adaptor {
using adaptor = map_adaptor;

template<class Iterable, class Function>
Expand Down
151 changes: 151 additions & 0 deletions include/Lz/detail/adaptors/random.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#pragma once

#ifndef LZ_RANDOM_ADAPTOR_HPP
#define LZ_RANDOM_ADAPTOR_HPP

#include <Lz/detail/iterables/random.hpp>
#include <Lz/detail/concepts.hpp>
#include <random>

namespace lz {
namespace detail {
template<std::size_t N>
class seed_sequence {
public:
using result_type = std::seed_seq::result_type;

private:
using seed_array = std::array<result_type, N>;
seed_array _seed;

template<class Iter>
LZ_CONSTEXPR_CXX_20 void create(Iter begin, Iter end) {
using value_type = val_t<Iter>;
std::transform(begin, end, _seed.begin(), [](const value_type val) { return static_cast<result_type>(val); });
}

result_type T(const result_type x) const {
return x ^ (x >> 27u);
}

public:
constexpr seed_sequence() = default;

explicit seed_sequence(std::random_device& rd) {
std::generate(_seed.begin(), _seed.end(), [&rd]() { return static_cast<result_type>(rd()); });
}

template<class T>
LZ_CONSTEXPR_CXX_20 seed_sequence(std::initializer_list<T> values) {
create(values.begin(), values.end());
}

template<class Iter>
LZ_CONSTEXPR_CXX_20 seed_sequence(Iter first, Iter last) {
create(first, last);
}

seed_sequence(const seed_sequence&) = delete;
seed_sequence& operator=(const seed_sequence&) = delete;

template<class Iter>
LZ_CONSTEXPR_CXX_20 void generate(Iter begin, Iter end) const {
if (begin == end) {
return;
}

using iter_value_type = val_t<Iter>;

std::fill(begin, end, 0x8b8b8b8b);
const auto n = static_cast<std::size_t>(end - begin);
constexpr auto s = N;
const std::size_t m = std::max(s + 1, n);
const std::size_t t = (n >= 623) ? 11 : (n >= 68) ? 7 : (n >= 39) ? 5 : (n >= 7) ? 3 : (n - 1) / 2;
const std::size_t p = (n - t) / 2;
const std::size_t q = p + t;

auto mask = static_cast<iter_value_type>(1) << 31;
mask <<= 1;
mask -= 1;

for (std::size_t k = 0; k < m - 1; k++) {
const std::size_t k_mod_n = k % n;
const std::size_t k_plus_mod_n = (k + p) % n;
const result_type r1 = 1664525 * T(begin[k_mod_n] ^ begin[k_plus_mod_n] ^ begin[(k - 1) % n]);

result_type r2;
if (k == 0) {
r2 = static_cast<result_type>((r1 + s) & mask);
}
else if (k <= s) {
r2 = static_cast<result_type>((r1 + k_mod_n + _seed[k - 1]) & mask);
}
else {
r2 = static_cast<result_type>((r1 + k_mod_n) & mask);
}

begin[k_plus_mod_n] += (r1 & mask);
begin[(k + q) % n] += (r2 & mask);
begin[k_mod_n] = r2;
}

for (std::size_t k = m; k < m + n - 1; k++) {
const std::size_t k_mod_n = k % n;
const std::size_t k_plus_mod_n = (k + p) % n;
const result_type r3 = 1566083941 * T(begin[k_mod_n] + begin[k_plus_mod_n] + begin[(k - 1) % n]);
const auto r4 = static_cast<result_type>((r3 - k_mod_n) & mask);

begin[k_plus_mod_n] ^= (r3 & mask);
begin[(k + q) % n] ^= (r4 & mask);
begin[k_mod_n] = r4;
}
}

template<class Iter>
LZ_CONSTEXPR_CXX_20 void param(Iter output_iter) const {
std::copy(_seed.begin(), _seed.end(), output_iter);
}

static constexpr std::size_t size() {
return N;
}
};

inline std::mt19937 create_mt_engine() {
std::random_device rd;
seed_sequence<8> seed_seq{rd};
return std::mt19937{seed_seq};
}

template<bool UseSentinel>
struct random_adaptor {
using adaptor = random_adaptor<UseSentinel>;

template<class Generator, class Distribution>
LZ_NODISCARD constexpr random_iterable<typename Distribution::result_type, Distribution, Generator, UseSentinel>
operator()(const Distribution& distribution, Generator& generator, const std::size_t amount) const {
return { distribution, generator, static_cast<std::ptrdiff_t>(amount) };
}

template<class Integral>
LZ_NODISCARD
enable_if<std::is_integral<Integral>::value, random_iterable<Integral, std::uniform_int_distribution<Integral>, std::mt19937, UseSentinel>>
operator()(const Integral min, const Integral max, const std::size_t amount) const {
static std::mt19937 gen = create_mt_engine();
std::uniform_int_distribution<Integral> dist(min, max);
return (*this)(dist, gen, amount);
}

template<class Floating>
LZ_NODISCARD
enable_if<std::is_floating_point<Floating>::value, random_iterable<Floating, std::uniform_real_distribution<Floating>, std::mt19937, UseSentinel>>
random(const Floating min, const Floating max, const std::size_t amount = (std::numeric_limits<std::size_t>::max)()) {
static std::mt19937 gen = create_mt_engine();
std::uniform_real_distribution<Floating> dist(min, max);
return (*this)(dist, gen, amount);
}
};
} // namespace detail
} // namespace lz

#endif
8 changes: 2 additions & 6 deletions include/Lz/detail/adaptors/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@
namespace lz {
namespace detail {
struct range_adaptor {
#ifdef LZ_HAS_CXX_11
static const range_adaptor range;
#endif

using adaptor = range_adaptor;

template<class Arithmetic>
LZ_NODISCARD constexpr range_iterable<Arithmetic>
range(const Arithmetic start, const Arithmetic end, const Arithmetic step) const noexcept {
operator()(const Arithmetic start, const Arithmetic end, const Arithmetic step) const noexcept {
return { start, end, step };
}

template<class Arithmetic>
LZ_NODISCARD constexpr range_iterable<Arithmetic>
range(const Arithmetic end) const noexcept {
operator()(const Arithmetic end) const noexcept {
return { 0, end, 1 };
}
};
Expand Down
27 changes: 27 additions & 0 deletions include/Lz/detail/adaptors/take.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#ifndef LZ_TAKE_ADAPTOR_HPP
#define LZ_TAKE_ADAPTOR_HPP

#include <Lz/detail/iterables/take.hpp>
#include <Lz/detail/adaptors/fn_args_holder.hpp>

namespace lz {
namespace detail {
struct take_adaptor {
using adaptor = take_adaptor;

template<class Iterable>
constexpr take_iterable<Iterable> operator()(Iterable&& iterable, const diff_iterable_t<Iterable> n) const {
return { std::forward<Iterable>(iterable), n };
}

template<class DiffT>
constexpr fn_args_holder<adaptor, DiffT> operator()(const DiffT n) const {
return { n };
}
};
} // namespace detail
} // namespace lz

#endif // LZ_TAKE_ADAPTOR_HPP
32 changes: 32 additions & 0 deletions include/Lz/detail/adaptors/take_while.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#ifndef LZ_TAKE_WHILE_ADAPTOR
#define LZ_TAKE_WHILE_ADAPTOR


#include <Lz/detail/iterables/take_while.hpp>
#include <Lz/detail/adaptors/fn_args_holder.hpp>
#include <Lz/detail/concepts.hpp>

namespace lz {
namespace detail {
struct take_while_adaptor {
using adaptor = take_while_adaptor;

// TODO add LZ_CONCEPT_ITERABLE to all adaptors
template<LZ_CONCEPT_ITERABLE Iterable, class UnaryPredicate>
LZ_NODISCARD constexpr take_while_iterable<Iterable, decay_t<UnaryPredicate>>
operator()(Iterable&& iterable, UnaryPredicate&& unary_predicate) const {
return { std::forward<Iterable>(iterable), std::forward<UnaryPredicate>(unary_predicate) };
}

template<class UnaryPredicate>
LZ_NODISCARD constexpr fn_args_holder<adaptor, decay_t<UnaryPredicate>>
operator()(UnaryPredicate&& unary_predicate) const {
return { *this, std::forward<UnaryPredicate>(unary_predicate) };
}
};

} // namespace detail
} // namespace lz
#endif // LZ_TAKE_WHILE_ADAPTOR
Loading

0 comments on commit dc2c899

Please sign in to comment.