Skip to content

Commit

Permalink
fix json
Browse files Browse the repository at this point in the history
  • Loading branch information
wjr-z committed Oct 4, 2024
1 parent e8debd6 commit cb0f4be
Show file tree
Hide file tree
Showing 16 changed files with 2,661 additions and 2,342 deletions.
52 changes: 35 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
cmake_minimum_required(VERSION 3.14)
project(wjr
LANGUAGES CXX
)
project(wjr LANGUAGES CXX)

if(NOT(CMAKE_SIZEOF_VOID_P EQUAL 8))
message(FATAL_ERROR "Not support non 64-bit currently, CMake will exit.")
Expand All @@ -17,6 +15,7 @@ endif()

option(WJR_ENABLE_ASSEMBLY "Link with assembly by using NASM" OFF)
option(WJR_DISABLE_EXCEPTIONS "Disable exceptions" ON)
option(WJR_DISABLE_CXX_20 "Disable C++ 20 even if it's supported." ON)

if (DEFINED WJR_DEBUG_LEVEL AND (NOT DEFINED WJR_DEBUG_LEVEL_DEBUG))
set(WJR_DEBUG_LEVEL_DEBUG_TMP ${WJR_DEBUG_LEVEL})
Expand Down Expand Up @@ -92,7 +91,7 @@ if(WJR_ENABLE_ASSEMBLY)
list(APPEND WJR_ASSEMBLY_LIBS wjr_asm wjr_add_n wjr_sub_n)
list(APPEND WJR_COMPILE_DEFINITIONS WJR_ENABLE_ASSEMBLY)

message("enable ${WJR_ARCH} assembly")
message("Enable ${WJR_ARCH} assembly")
endif()
endif()

Expand Down Expand Up @@ -133,7 +132,18 @@ else()
endif()
endif()

target_compile_features(wjr PUBLIC cxx_std_17)
if (NOT WJR_DISABLE_CXX_20 AND "cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
message("Enable C++ 20.")
set (WJR_USE_CXX_20 ON)
else()
set (WJR_USE_CXX_20 OFF)
endif()

if (WJR_USE_CXX_20)
target_compile_features(wjr PUBLIC cxx_std_20)
else()
target_compile_features(wjr PUBLIC cxx_std_17)
endif()

target_compile_options(wjr
PUBLIC $<$<COMPILE_LANGUAGE:CXX>:${WJR_CXX_FLAGS}$<SEMICOLON>>
Expand All @@ -149,29 +159,37 @@ target_compile_definitions(wjr
PUBLIC $<$<COMPILE_LANGUAGE:CXX>: ${WJR_COMPILE_DEFINITIONS}$<SEMICOLON>>
)

add_subdirectory(third-party/boost/config)
add_subdirectory(third-party/boost/predef)
add_subdirectory(third-party/boost/type_traits)
add_subdirectory(third-party/boost/preprocessor)
if (WIN32)
add_subdirectory(third-party/boost/winapi)
endif()
add_subdirectory(third-party/boost/atomic)

if(WJR_DISABLE_EXCEPTIONS)
target_compile_definitions(wjr
PUBLIC $<$<COMPILE_LANGUAGE:CXX>: -DWJR_DISABLE_EXCEPTIONS >
)
endif()

target_compile_definitions(boost_atomic PUBLIC BOOST_NO_EXCEPTIONS)
if (WJR_USE_CXX_20)
set(WJR_ATOMIC_LIBS)
else()
add_subdirectory(third-party/boost/config)
add_subdirectory(third-party/boost/predef)
add_subdirectory(third-party/boost/type_traits)
add_subdirectory(third-party/boost/preprocessor)
if (WIN32)
add_subdirectory(third-party/boost/winapi)
endif()
add_subdirectory(third-party/boost/atomic)

if(WJR_DISABLE_EXCEPTIONS)
target_compile_definitions(boost_atomic PUBLIC BOOST_NO_EXCEPTIONS)
endif()

set(WJR_ATOMIC_LIBS Boost::atomic)
endif()

target_link_libraries(wjr
PUBLIC
${WJR_LIBS}
${WJR_ASSEMBLY_LIBS}
Boost::atomic
${WJR_ATOMIC_LIBS}
)

set(WJR_PCH PUBLIC ${WJR_INCLUDE_DIR}/wjr/biginteger.hpp)
set(WJR_PCH PUBLIC ${WJR_INCLUDE_DIR}/wjr/assert.hpp ${WJR_INCLUDE_DIR}/wjr/biginteger.hpp)
target_precompile_headers(wjr PUBLIC ${WJR_PCH})
46 changes: 24 additions & 22 deletions include/wjr/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@
#include <wjr/type_traits.hpp>

namespace wjr {

#if defined(__cpp_lib_constexpr_algorithms)
namespace constant {
#if defined(__cpp_lib_algorithms)

template <typename InputIt, typename OutputIt>
constexpr OutputIt constexpr_copy(InputIt first, InputIt last, OutputIt dst) {
constexpr OutputIt 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) {
constexpr OutputIt 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) {
constexpr OutputIt 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) {
constexpr Iter inplace_merge(Iter first, Iter mid, Iter last, Pred pred) {
return std::inplace_merge(first, mid, last, pred);
}

template <typename Iter, typename Pred>
constexpr void constexpr_sort(Iter first, Iter last, Pred pred) {
constexpr void 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) {
constexpr OutputIt copy(InputIt first, InputIt last, OutputIt dst) {
for (; first != last; (void)++first, (void)++dst) {
*dst = *first;
}
Expand All @@ -52,7 +52,7 @@ constexpr OutputIt constexpr_copy(InputIt first, InputIt last, OutputIt dst) {
}

template <typename InputIt, typename OutputIt>
constexpr OutputIt constexpr_move_backward(InputIt first, InputIt last, OutputIt dst) {
constexpr OutputIt move_backward(InputIt first, InputIt last, OutputIt dst) {
while (first != last) {
*(--dst) = std::move(*(--last));
}
Expand All @@ -61,8 +61,8 @@ constexpr OutputIt constexpr_move_backward(InputIt first, InputIt last, OutputIt
}

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) {
constexpr OutputIt 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) {
Expand All @@ -87,23 +87,23 @@ constexpr OutputIt constexpr_merge(InputIt1 first1, InputIt1 last1, InputIt2 fir
}
}

dst = constexpr_copy(first1, last1, dst);
dst = constexpr_copy(first2, last2, dst);
dst = copy(first1, last1, dst);
dst = 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) {
constexpr void __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);
move_backward(first, mid, ++hole);
*first = std::move(tmp);
} else {
Iter prev = mid;
Expand All @@ -126,24 +126,26 @@ constexpr void __constexpr_insertion_sort(Iter first, Iter last, Pred pred) {
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);
constexpr void __sort_impl(Iter first, Iter last, Pred pred) {
__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);
constexpr void sort(Iter first, Iter last, Pred pred) {
algorithm_detail::__sort_impl(first, last, pred);
}

#endif

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

} // namespace constant

template <typename CharT, typename Traits>
WJR_PURE WJR_INTRINSIC_INLINE bool
starts_with(std::basic_string_view<CharT, Traits> str,
Expand Down
45 changes: 18 additions & 27 deletions include/wjr/concurrency/spin_mutex.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef WJR_CONCURRENCY_SPIN_MUTEX_HPP__
#define WJR_CONCURRENCY_SPIN_MUTEX_HPP__

#include <atomic>
#include <thread>

#include <wjr/assert.hpp>
#include <wjr/concurrency/atomic.hpp>
#include <wjr/concurrency/pause.hpp>

namespace wjr {
Expand All @@ -15,47 +15,38 @@ namespace wjr {
*
*/
class spin_mutex {
static constexpr int fast_lock_threshold = 4;
static constexpr int pause_per_lock_threshold = 8;
static constexpr int yield_per_pause_threshold = 12;
static constexpr unsigned int yield_threshold = 16;

public:
spin_mutex() = default;
spin_mutex(const spin_mutex &) = delete;
spin_mutex &operator=(const spin_mutex &) = delete;

WJR_INTRINSIC_INLINE bool try_lock() {
return !m_flag.test_and_set(std::memory_order_acquire);
return !m_flag.test_and_set(memory_order_acquire);
}

void lock() {
if (WJR_UNLIKELY(!try_lock())) {
for (int i = 0; i < fast_lock_threshold; ++i) {
if (try_lock()) {
return;
}
unsigned int pause_cnt = 0;
do {
if (WJR_LIKELY(try_lock())) {
return;
}

do {
int yield_cnt = yield_per_pause_threshold;

do {
pause_intrinsic();

int puase_cnt = pause_per_lock_threshold;
do {
if (try_lock()) {
return;
}
} while (--puase_cnt);
} while (--yield_cnt);
if (WJR_LIKELY(try_lock())) {
return;
}

++pause_cnt;
if (WJR_UNLIKELY(!(pause_cnt & (yield_threshold - 1)))) {
std::this_thread::yield();
} while (!try_lock());
}
} else {
pause_intrinsic();
}
} while (true);
}

void unlock() { m_flag.clear(std::memory_order_release); }
void unlock() { m_flag.clear(memory_order_release); }

#if WJR_DEBUG_LEVEL > 2
~spin_mutex() {
Expand All @@ -65,7 +56,7 @@ class spin_mutex {
#endif

private:
std::atomic_flag m_flag = ATOMIC_FLAG_INIT;
atomic_flag m_flag = ATOMIC_FLAG_INIT;
};

} // namespace wjr
Expand Down
28 changes: 28 additions & 0 deletions include/wjr/container/constexpr_map.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef WJR_CONTAINER_CONSTEXPR_MAP_HPP__
#define WJR_CONTAINER_CONSTEXPR_MAP_HPP__

#include <array>
#include <string_view>

#include <wjr/type_traits.hpp>

namespace wjr {

namespace constant {

template <size_t KeySize>
class string_map {
public:
using key_type = std::string_view;

constexpr string_map(const key_type (&keys)[KeySize]) noexcept : m_keys(keys) {}

private:
std::array<key_type, KeySize> m_keys;
};

} // namespace constant

} // namespace wjr

#endif // WJR_CONTAINER_CONSTEXPR_MAP_HPP__
2 changes: 2 additions & 0 deletions include/wjr/json/detail.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ struct basic_value {
basic_value(array_t, void *ptr) noexcept : m_ptr(ptr), m_type(value_t::array) {}

void set(null_t) noexcept { m_type = value_t::null; }
void set(null_t, std::nullptr_t) noexcept { m_type = value_t::null; }

void set(boolean_t, bool f) noexcept {
m_boolean = f;
m_type = value_t::boolean;
Expand Down
Loading

0 comments on commit cb0f4be

Please sign in to comment.