Skip to content

Commit

Permalink
chore: update meojson
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Dec 5, 2023
1 parent 9f77ef4 commit 5ec31e8
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 162 deletions.
166 changes: 95 additions & 71 deletions 3rdparty/include/meojson/bitops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,111 @@

#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
#include <bit>
namespace json::__bitops {
using std::countl_one;
using std::countr_one;
using std::countl_zero;
using std::countr_zero;
inline constexpr bool is_little_endian() {
return std::endian::native == std::endian::little;
}
namespace json::_bitops
{
using std::countl_one;
using std::countl_zero;
using std::countr_one;
using std::countr_zero;
inline constexpr bool is_little_endian()
{
return std::endian::native == std::endian::little;
}
}
#else
#include <cstdint>
namespace json::__bitops {
namespace json::_bitops
{
#if defined(__GNUC__) || defined(__clang__)
inline constexpr int countl_zero(uint32_t x) {
if constexpr (sizeof(uint32_t) == sizeof(unsigned int))
return x == 0 ? 32 : __builtin_clz(x);
if constexpr (sizeof(uint32_t) == sizeof(unsigned long))
return x == 0 ? 32 : __builtin_clzl(x);
return x == 0 ? 32 : __builtin_clzll(x);
}
inline constexpr int countr_zero(uint32_t x) {
if constexpr (sizeof(uint32_t) == sizeof(unsigned int))
return x == 0 ? 32 : __builtin_ctz(x);
if constexpr (sizeof(uint32_t) == sizeof(unsigned long))
return x == 0 ? 32 : __builtin_ctzl(x);
return x == 0 ? 32 : __builtin_ctzll(x);
}
inline constexpr int countl_zero(uint64_t x) {
return x == 0 ? 64 : __builtin_clzll(x);
}
inline constexpr int countr_zero(uint64_t x) {
return x == 0 ? 64 : __builtin_ctzll(x);
}
inline constexpr int countl_zero(uint32_t x)
{
if constexpr (sizeof(uint32_t) == sizeof(unsigned int)) return x == 0 ? 32 : __builtin_clz(x);
if constexpr (sizeof(uint32_t) == sizeof(unsigned long)) return x == 0 ? 32 : __builtin_clzl(x);
return x == 0 ? 32 : __builtin_clzll(x);
}
inline constexpr int countr_zero(uint32_t x)
{
if constexpr (sizeof(uint32_t) == sizeof(unsigned int)) return x == 0 ? 32 : __builtin_ctz(x);
if constexpr (sizeof(uint32_t) == sizeof(unsigned long)) return x == 0 ? 32 : __builtin_ctzl(x);
return x == 0 ? 32 : __builtin_ctzll(x);
}
inline constexpr int countl_zero(uint64_t x)
{
return x == 0 ? 64 : __builtin_clzll(x);
}
inline constexpr int countr_zero(uint64_t x)
{
return x == 0 ? 64 : __builtin_ctzll(x);
}
#elif defined(_MSC_VER)
#ifdef __AVX2__
// lzcnt intrinsics is not constexpr
inline int countl_zero(uint32_t x) {
return __lzcnt(x);
}
inline int countr_zero(uint32_t x) {
return _tzcnt_u32(x);
}
inline int countl_zero(uint64_t x) {
return (int)__lzcnt64(x);
}
inline int countr_zero(uint64_t x) {
return (int)_tzcnt_u64(x);
}
// lzcnt intrinsics is not constexpr
inline int countl_zero(uint32_t x)
{
return __lzcnt(x);
}
inline int countr_zero(uint32_t x)
{
return _tzcnt_u32(x);
}
inline int countl_zero(uint64_t x)
{
return (int)__lzcnt64(x);
}
inline int countr_zero(uint64_t x)
{
return (int)_tzcnt_u64(x);
}
#else
inline constexpr int countl_zero(uint32_t x) {
unsigned long index = 0;
return _BitScanReverse(&index, x) ? 31 - index : 32;
}
inline constexpr int countr_zero(uint32_t x) {
unsigned long index = 0;
return _BitScanForward(&index, x) ? index : 32;
}
inline constexpr int countl_zero(uint64_t x) {
unsigned long index = 0;
return _BitScanReverse64(&index, x) ? 63 - index : 64;
}
inline constexpr int countr_zero(uint64_t x) {
unsigned long index = 0;
return _BitScanForward64(&index, x) ? index : 64;
}
inline constexpr int countl_zero(uint32_t x)
{
unsigned long index = 0;
return _BitScanReverse(&index, x) ? 31 - index : 32;
}
inline constexpr int countr_zero(uint32_t x)
{
unsigned long index = 0;
return _BitScanForward(&index, x) ? index : 32;
}
inline constexpr int countl_zero(uint64_t x)
{
unsigned long index = 0;
return _BitScanReverse64(&index, x) ? 63 - index : 64;
}
inline constexpr int countr_zero(uint64_t x)
{
unsigned long index = 0;
return _BitScanForward64(&index, x) ? index : 64;
}
#endif // __AVX2__
#else // compiler
#else // compiler
#error "bring your own bit counting implementation"
#endif
inline int countl_one(uint32_t x) { return countl_zero(~x); }
inline int countr_one(uint32_t x) { return countr_zero(~x); }
inline int countl_one(uint64_t x) { return countl_zero(~x); }
inline int countr_one(uint64_t x) { return countr_zero(~x); }
inline int countl_one(uint32_t x)
{
return countl_zero(~x);
}
inline int countr_one(uint32_t x)
{
return countr_zero(~x);
}
inline int countl_one(uint64_t x)
{
return countl_zero(~x);
}
inline int countr_one(uint64_t x)
{
return countr_zero(~x);
}

// no constexpr endian awareness before C++20
inline bool is_little_endian() {
union {
uint32_t u32;
uint8_t u8;
} u = { 0x01020304 };
return u.u8 == 4;
}
// no constexpr endian awareness before C++20
inline bool is_little_endian()
{
union {
uint32_t u32;
uint8_t u8;
} u = { 0x01020304 };
return u.u8 == 4;
}
} // namespace json::_bitops
#endif // C++20
47 changes: 46 additions & 1 deletion 3rdparty/include/meojson/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ class basic_value
return format(indent, 0);
}

template <typename value_t>
bool all() const;
template <typename value_t, template <typename...> typename vector_t = std::vector>
vector_t<value_t> to_vector() const;
template <typename value_t, template <typename...> typename map_t = std::map>
Expand Down Expand Up @@ -313,6 +315,8 @@ class basic_array
{
return format(indent, 0);
}
template <typename value_t>
bool all() const;
template <typename value_t, template <typename...> typename vector_t = std::vector>
vector_t<value_t> to_vector() const;

Expand Down Expand Up @@ -425,6 +429,8 @@ class basic_object
{
return format(indent, 0);
}
template <typename value_t>
bool all() const;
template <typename value_t, template <typename...> typename map_t = std::map>
map_t<string_t, value_t> to_map() const;

Expand Down Expand Up @@ -488,7 +494,7 @@ class basic_object
// ****************************

template <typename string_t = default_string_t, typename parsing_t = void,
typename accel_traits = packed_bytes_trait_max>
typename accel_traits = _packed_bytes::packed_bytes_trait_max>
class parser
{
public:
Expand Down Expand Up @@ -1152,6 +1158,21 @@ inline string_t basic_value<string_t>::format(size_t indent, size_t indent_times
}
}

template <typename string_t>
template <typename value_t>
inline bool basic_value<string_t>::all() const
{
if (is_array()) {
return as_array().template all<value_t>();
}
else if (is_object()) {
return as_object().template all<value_t>();
}
else {
return false;
}
}

template <typename string_t>
template <typename value_t, template <typename...> typename vector_t>
inline vector_t<value_t> basic_value<string_t>::to_vector() const
Expand Down Expand Up @@ -1432,6 +1453,18 @@ inline string_t basic_array<string_t>::format(size_t indent, size_t indent_times
return str;
}

template <typename string_t>
template <typename value_t>
inline bool basic_array<string_t>::all() const
{
for (const auto& elem : _array_data) {
if (!elem.template is<value_t>()) {
return false;
}
}
return true;
}

namespace _to_vector_helper
{
template <typename T>
Expand Down Expand Up @@ -1772,6 +1805,18 @@ inline string_t basic_object<string_t>::format(size_t indent, size_t indent_time
return str;
}

template <typename string_t>
template <typename value_t>
inline bool basic_object<string_t>::all() const
{
for (const auto& [_, val] : _object_data) {
if (!val.template is<value_t>()) {
return false;
}
}
return true;
}

template <typename string_t>
template <typename value_t, template <typename...> typename map_t>
inline map_t<string_t, value_t> basic_object<string_t>::to_map() const
Expand Down
Loading

0 comments on commit 5ec31e8

Please sign in to comment.