Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
wjr-z committed Aug 10, 2024
1 parent 8300f28 commit 40d3f35
Show file tree
Hide file tree
Showing 14 changed files with 734 additions and 67 deletions.
134 changes: 134 additions & 0 deletions include/wjr/algorithm.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,146 @@
#ifndef WJR_FORMAT_ALGORITHM_HPP__
#define WJR_FORMAT_ALGORITHM_HPP__

#include <algorithm>
#include <string_view>

#include <wjr/iterator/detail.hpp>
#include <wjr/type_traits.hpp>

namespace wjr {

#if defined(__cpp_lib_constexpr_algorithms)

template <typename InputIt, typename OutputIt>
constexpr OutputIt constexpr_copy(InputIt first, InputIt last, OutputIt dst) {
return std::copy(first, last, dst);
}

template <typename InputIt, typename OutputIt>
constexpr OutputIt constexpr_move_backward(InputIt first, InputIt last, OutputIt dst) {
return std::move_backward(first, last, dst);
}

template <typename InputIt1, typename InputIt2, typename OutputIt, typename Pred>
constexpr OutputIt constexpr_merge(InputIt1 first1, InputIt1 last1, InputIt2 first2,
InputIt2 last2, OutputIt dst, Pred pred) {
return std::merge(first1, last1, first2, last2, dst, pred);
}

template <typename Iter, typename Pred>
constexpr Iter constexpr_inplace_merge(Iter first, Iter mid, Iter last, Pred pred) {
return std::inplace_merge(first1, last1, first2, last2, dst, pred);
}

template <typename Iter, typename Pred>
constexpr void constexpr_sort(Iter first, Iter last, Pred pred) {
std::sort(first, last, pred);
}

#else

template <typename InputIt, typename OutputIt>
constexpr OutputIt constexpr_copy(InputIt first, InputIt last, OutputIt dst) {
for (; first != last; (void)++first, (void)++dst) {
*dst = *first;
}

return dst;
}

template <typename InputIt, typename OutputIt>
constexpr OutputIt constexpr_move_backward(InputIt first, InputIt last, OutputIt dst) {
while (first != last) {
*(--dst) = std::move(*(--last));
}

return dst;
}

template <typename InputIt1, typename InputIt2, typename OutputIt, typename Pred>
constexpr OutputIt constexpr_merge(InputIt1 first1, InputIt1 last1, InputIt2 first2,
InputIt2 last2, OutputIt dst, Pred pred) {
const auto count1 = std::distance(first1, last1);
const auto count2 = std::distance(first2, last2);
if (first1 != last1 && first2 != last2) {
for (;;) {
if (pred(*first2, *first1)) {
*dst = *first2;
++dst;
++first2;

if (first2 == last2) {
break;
}
} else {
*dst = *first1;
++dst;
++first1;

if (first1 == last1) {
break;
}
}
}
}

dst = constexpr_copy(first1, last1, dst);
dst = constexpr_copy(first2, last2, dst);
return dst;
}

namespace algorithm_detail {

template <typename Iter, typename Pred>
constexpr void __constexpr_insertion_sort(Iter first, Iter last, Pred pred) {
if (first != last) {
for (Iter mid = first; ++mid != last;) {
iterator_reference_t<Iter> ref = *mid;

if (pred(ref, *first)) {
iterator_value_t<Iter> tmp = std::move(ref);
Iter hole = mid;
constexpr_move_backward(first, mid, ++hole);
*first = std::move(tmp);
} else {
Iter prev = mid;
if (pred(ref, *--prev)) {
iterator_value_t<Iter> tmp = std::move(ref);
*mid = *prev;

Iter pprev = prev;
for (; pred(tmp, *--pprev); prev = pprev) {
*prev = std::move(*pprev);
}

*prev = std::move(tmp);
}
}
}
}
}

inline constexpr size_t __insertion_sort_threshold = in_place_max;

template <typename Iter, typename Pred>
constexpr void __constexpr_sort_impl(Iter first, Iter last, Pred pred) {
__constexpr_insertion_sort(first, last, pred);
}

} // namespace algorithm_detail

template <typename Iter, typename Pred>
constexpr void constexpr_sort(Iter first, Iter last, Pred pred) {
algorithm_detail::__constexpr_sort_impl(first, last, pred);
}

#endif

template <typename Iter>
constexpr void constexpr_sort(Iter first, Iter last) {
constexpr_sort(first, last, std::less<>{});
}

template <typename CharT, typename Traits>
WJR_PURE WJR_INTRINSIC_INLINE bool
starts_with(std::basic_string_view<CharT, Traits> str,
Expand Down
8 changes: 4 additions & 4 deletions include/wjr/container/generic/bplus_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class inline_key {
~inline_key() = default;

constexpr inline_key(reference value) noexcept(
std::is_nothrow_constructible_v<aligned_storage<T>, reference>)
std::is_nothrow_constructible_v<uninitialized<T>, reference>)
: m_storage(value) {}

constexpr reference operator*() const noexcept { return *m_storage; }
Expand All @@ -72,7 +72,7 @@ class inline_key {

private:
// no need to check
aligned_storage<T> m_storage;
uninitialized<T> m_storage;
};

template <typename T>
Expand Down Expand Up @@ -102,7 +102,7 @@ class inline_key<T, false> {
};

template <typename T>
struct is_possible_inline_key : std::is_trivially_copyable<aligned_storage<T>> {};
struct is_possible_inline_key : std::is_trivially_copyable<uninitialized<T>> {};

template <typename T>
inline constexpr bool is_possible_inline_key_v = is_possible_inline_key<T>::value;
Expand Down Expand Up @@ -630,7 +630,7 @@ class basic_bplus_tree {
const const_iterator iter = __search<true>(key);
const auto pos = iter.get_pos();
const bool inserted =
pos == 0 || key_comp()(*iter.get_leaf()->m_values[pos - 1], key);
pos == 0 || key_comp()(iter.get_leaf()->__get_key(pos - 1), key);
return {iter, inserted};
}

Expand Down
8 changes: 8 additions & 0 deletions include/wjr/container/generic/constexpr_tree.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef WJR_CONTAINER_GERNERIC_CONSTEXPR_TREE_HPP__
#define WJR_CONTAINER_GERNERIC_CONSTEXPR_TREE_HPP__

#include <wjr/algorithm.hpp>

namespace wjr {} // namespace wjr

#endif // WJR_CONTAINER_GERNERIC_CONSTEXPR_TREE_HPP__
7 changes: 0 additions & 7 deletions include/wjr/json/detail.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,12 @@ enum class value_t : uint8_t {
};

using number_unsigned_t = integral_constant<value_t, value_t::number_unsigned>;

using number_signed_t = integral_constant<value_t, value_t::number_signed>;

using number_float_t = integral_constant<value_t, value_t::number_float>;

using null_t = integral_constant<value_t, value_t::null>;

using boolean_t = integral_constant<value_t, value_t::boolean>;

using string_t = integral_constant<value_t, value_t::string>;

using object_t = integral_constant<value_t, value_t::object>;

using array_t = integral_constant<value_t, value_t::array>;

struct basic_value {
Expand Down
Loading

0 comments on commit 40d3f35

Please sign in to comment.