Skip to content

Commit

Permalink
styles: format
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Mar 15, 2024
1 parent 595cd8e commit 2463cdb
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ RemoveParentheses: Leave
RemoveSemicolon: false
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Leave
SeparateDefinitionBlocks: Always
ShortNamespaceLines: 1000
SortIncludes: CaseSensitive
# SortJavaStaticImport:
Expand Down
34 changes: 21 additions & 13 deletions benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ void do_benchmark(const std::string& content, const std::string& tag)
iteration_time_ms.reserve(10000);
auto start = std::chrono::steady_clock::now();
bool parsed = false;
while (true)
{
while (true) {
auto t0 = std::chrono::steady_clock::now();
auto opt = parser::parse(content);
parsed = opt.has_value();
Expand All @@ -34,10 +33,14 @@ void do_benchmark(const std::string& content, const std::string& tag)
auto loop_times = iteration_time_ms.size();
auto sum = std::accumulate(iteration_time_ms.begin(), iteration_time_ms.end(), 0.0);
double mean = sum / loop_times;
double stdev_cum = std::accumulate(iteration_time_ms.begin(), iteration_time_ms.end(), 0.0, [mean](auto a, auto b) {
auto off = b - mean;
return a + off * off;
});
double stdev_cum = std::accumulate(
iteration_time_ms.begin(),
iteration_time_ms.end(),
0.0,
[mean](auto a, auto b) {
auto off = b - mean;
return a + off * off;
});

double stdev = std::sqrt(stdev_cum / loop_times);
std::sort(iteration_time_ms.begin(), iteration_time_ms.end());
Expand All @@ -50,8 +53,8 @@ void do_benchmark(const std::string& content, const std::string& tag)
median = iteration_time_ms[loop_times / 2];
}

std::cout << tag << ", " << std::boolalpha << parsed << ", " << loop_times << ", " << mean << ", " << median << ", "
<< stdev << std::endl;
std::cout << tag << ", " << std::boolalpha << parsed << ", " << loop_times << ", " << mean
<< ", " << median << ", " << stdev << std::endl;
}

int main(int argc, char** argv)
Expand Down Expand Up @@ -80,19 +83,24 @@ int main(int argc, char** argv)
using namespace json::_packed_bytes;

do_benchmark<json::parser<std::string, std::string, packed_bytes_trait_none>>(
content, path.filename().string() + ", none");
content,
path.filename().string() + ", none");
do_benchmark<json::parser<std::string, std::string, packed_bytes_trait_uint32>>(
content, path.filename().string() + ", bits32");
content,
path.filename().string() + ", bits32");
do_benchmark<json::parser<std::string, std::string, packed_bytes_trait_uint64>>(
content, path.filename().string() + ", bits64");
content,
path.filename().string() + ", bits64");

if constexpr (packed_bytes_trait<16>::available) {
do_benchmark<json::parser<std::string, std::string, packed_bytes_trait<16>>>(
content, path.filename().string() + ", simd128");
content,
path.filename().string() + ", simd128");
}
if constexpr (packed_bytes_trait<32>::available) {
do_benchmark<json::parser<std::string, std::string, packed_bytes_trait<32>>>(
content, path.filename().string() + ", simd256");
content,
path.filename().string() + ", simd256");
}

do_benchmark<json::parser5<std::string>>(content, path.filename().string() + ", json5");
Expand Down
13 changes: 13 additions & 0 deletions include/common/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ class basic_array
: _array_data(std::make_move_iterator(arr.begin()), std::make_move_iterator(arr.end()))
{
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
basic_array(const jsonization_t& value)
: basic_array(value.to_json())
{
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_to_json_in_templ_spec<jsonization_t>::value, bool> = true>
Expand All @@ -65,17 +67,24 @@ class basic_array
~basic_array() noexcept = default;

bool empty() const noexcept { return _array_data.empty(); }

size_t size() const noexcept { return _array_data.size(); }

bool contains(size_t pos) const { return pos < _array_data.size(); }

bool exists(size_t pos) const { return contains(pos); }

const basic_value<string_t>& at(size_t pos) const;

string_t dumps(std::optional<size_t> indent = std::nullopt) const
{
return indent ? format(*indent) : to_string();
}

string_t to_string() const;

string_t format(size_t indent = 4) const { return format(indent, 0); }

template <typename value_t>
bool all() const;
template <typename value_t, template <typename...> typename collection_t = std::vector>
Expand Down Expand Up @@ -124,6 +133,7 @@ class basic_array

basic_array<string_t>& operator=(const basic_array<string_t>&) = default;
basic_array<string_t>& operator=(basic_array<string_t>&&) noexcept = default;

template <
typename value_t,
std::enable_if_t<std::is_convertible_v<value_t, basic_array<string_t>>, bool> = true>
Expand All @@ -133,6 +143,7 @@ class basic_array
}

bool operator==(const basic_array<string_t>& rhs) const;

bool operator!=(const basic_array<string_t>& rhs) const { return !(*this == rhs); }

template <
Expand All @@ -143,6 +154,7 @@ class basic_array
{
return as_collection<value_t, collection_t>();
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_from_json_in_member<jsonization_t, string_t>::value, bool> =
Expand All @@ -155,6 +167,7 @@ class basic_array
}
return dst;
}

template <
typename jsonization_t,
std::enable_if_t<
Expand Down
1 change: 1 addition & 0 deletions include/common/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class exception : public std::exception
{
public:
exception() = default;

exception(const std::string& msg)
: _what(msg)
{
Expand Down
13 changes: 13 additions & 0 deletions include/common/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ class basic_object
: _object_data(std::make_move_iterator(map.begin()), std::make_move_iterator(map.end()))
{
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
basic_object(const jsonization_t& value)
: basic_object(value.to_json())
{
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_to_json_in_templ_spec<jsonization_t>::value, bool> = true>
Expand All @@ -64,17 +66,24 @@ class basic_object
~basic_object() = default;

bool empty() const noexcept { return _object_data.empty(); }

size_t size() const noexcept { return _object_data.size(); }

bool contains(const string_t& key) const;

bool exists(const string_t& key) const { return contains(key); }

const basic_value<string_t>& at(const string_t& key) const;

string_t dumps(std::optional<size_t> indent = std::nullopt) const
{
return indent ? format(*indent) : to_string();
}

string_t to_string() const;

string_t format(size_t indent = 4) const { return format(indent, 0); }

template <typename value_t>
bool all() const;
template <typename value_t, template <typename...> typename map_t = std::map>
Expand Down Expand Up @@ -116,6 +125,7 @@ class basic_object

basic_object<string_t>& operator=(const basic_object<string_t>&) = default;
basic_object<string_t>& operator=(basic_object<string_t>&&) = default;

template <
typename value_t,
std::enable_if_t<std::is_convertible_v<value_t, basic_object<string_t>>, bool> = true>
Expand All @@ -125,6 +135,7 @@ class basic_object
}

bool operator==(const basic_object<string_t>& rhs) const;

bool operator!=(const basic_object<string_t>& rhs) const { return !(*this == rhs); }

template <
Expand All @@ -135,6 +146,7 @@ class basic_object
{
return as_map<value_t, map_t>();
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_from_json_in_member<jsonization_t, string_t>::value, bool> =
Expand All @@ -147,6 +159,7 @@ class basic_object
}
return dst;
}

template <
typename jsonization_t,
std::enable_if_t<
Expand Down
32 changes: 32 additions & 0 deletions include/common/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class basic_value
: basic_value(basic_array<string_t>(std::forward<collection_t>(collection)))
{
}

template <
typename map_t,
std::enable_if_t<
Expand All @@ -96,6 +97,7 @@ class basic_value
: basic_value(value.to_json())
{
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_to_json_in_templ_spec<jsonization_t>::value, bool> = true>
Expand All @@ -118,13 +120,21 @@ class basic_value
~basic_value();

bool valid() const noexcept { return _type != value_type::invalid; }

bool empty() const noexcept { return is_null(); }

bool is_null() const noexcept { return _type == value_type::null; }

bool is_number() const noexcept { return _type == value_type::number; }

bool is_boolean() const noexcept { return _type == value_type::boolean; }

bool is_string() const noexcept { return _type == value_type::string; }

bool is_array() const noexcept { return _type == value_type::array; }

bool is_object() const noexcept { return _type == value_type::object; }

template <typename value_t>
bool is() const noexcept;

Expand All @@ -133,9 +143,13 @@ class basic_value

bool contains(const string_t& key) const;
bool contains(size_t pos) const;

bool exists(const string_t& key) const { return contains(key); }

bool exists(size_t pos) const { return contains(pos); }

value_type type() const noexcept { return _type; }

const basic_value<string_t>& at(size_t pos) const;
const basic_value<string_t>& at(const string_t& key) const;

Expand Down Expand Up @@ -185,12 +199,15 @@ class basic_value
{
return indent ? format(*indent) : to_string();
}

// return raw string
string_t to_string() const;

string_t format(size_t indent = 4) const { return format(indent, 0); }

basic_value<string_t>& operator=(const basic_value<string_t>& rhs);
basic_value<string_t>& operator=(basic_value<string_t>&&) noexcept;

template <
typename value_t,
std::enable_if_t<std::is_convertible_v<value_t, basic_value<string_t>>, bool> = true>
Expand All @@ -200,6 +217,7 @@ class basic_value
}

bool operator==(const basic_value<string_t>& rhs) const;

bool operator!=(const basic_value<string_t>& rhs) const { return !(*this == rhs); }

const basic_value<string_t>& operator[](size_t pos) const;
Expand All @@ -224,18 +242,29 @@ class basic_value
basic_value<string_t>& operator+=(basic_array<string_t>&& rhs);

explicit operator bool() const { return as_boolean(); }

explicit operator int() const { return as_integer(); }

explicit operator unsigned() const { return as_unsigned(); }

explicit operator long() const { return as_long(); }

explicit operator unsigned long() const { return as_unsigned_long(); }

explicit operator long long() const { return as_long_long(); }

explicit operator unsigned long long() const { return as_unsigned_long_long(); }

explicit operator float() const { return as_float(); }

explicit operator double() const { return as_double(); }

explicit operator long double() const { return as_long_double(); }

explicit operator string_t() const { return as_string(); }

explicit operator basic_array<string_t>() const { return as_array(); }

explicit operator basic_object<string_t>() const { return as_object(); }

template <
Expand All @@ -246,6 +275,7 @@ class basic_value
{
return as_collection<value_t, collection_t>();
}

template <
typename value_t,
template <typename...> typename map_t = std::map,
Expand All @@ -254,6 +284,7 @@ class basic_value
{
return as_map<value_t, map_t>();
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_from_json_in_member<jsonization_t, string_t>::value, bool> =
Expand All @@ -266,6 +297,7 @@ class basic_value
}
return dst;
}

template <
typename jsonization_t,
std::enable_if_t<
Expand Down
Loading

0 comments on commit 2463cdb

Please sign in to comment.