From 4fbff7d1402142a74e578450fb6afb0c8524f417 Mon Sep 17 00:00:00 2001 From: Florian Weber Date: Sun, 26 Apr 2015 14:55:06 +0200 Subject: [PATCH] simplify value_t::operator< by using a lookup-table --- src/json.hpp | 80 +++++++---------------------------------------- src/json.hpp.re2c | 80 +++++++---------------------------------------- 2 files changed, 22 insertions(+), 138 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index cf5befe7f..b294e8e15 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -11,6 +11,7 @@ #define NLOHMANN_JSON_HPP #include +#include #include #include #include @@ -202,76 +203,17 @@ class basic_json */ friend bool operator<(const value_t lhs, const value_t rhs) { - // no type is smaller than itself - if (lhs == rhs) - { - return false; - } - - switch (lhs) - { - case (value_t::null): - { - // nulls are smaller than all other types - return true; + std::array order = {{ + 0, // null + 3, // object + 4, // array + 5, // string + 1, // boolean + 2, // integer + 2 // float } - - case (value_t::boolean): - { - // only nulls are smaller than booleans - return (rhs != value_t::null); - } - - case (value_t::number_float): - case (value_t::number_integer): - { - switch (rhs) - { - // numbers are smaller than objects, arrays, and string - case (value_t::object): - case (value_t::array): - case (value_t::string): - { - return true; - } - - default: - { - return false; - } - } - } - - case (value_t::object): - { - switch (rhs) - { - // objects are smaller than arrays and string - case (value_t::array): - case (value_t::string): - { - return true; - } - - default: - { - return false; - } - } - } - - case (value_t::array): - { - // arrays are smaller than strings - return (rhs == value_t::string); - } - - default: - { - // a string is not smaller than any other types - return false; - } - } + }; + return order[static_cast(lhs)] < order[static_cast(rhs)]; } diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index ae91d735c..b50eb2a93 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -11,6 +11,7 @@ #define NLOHMANN_JSON_HPP #include +#include #include #include #include @@ -202,76 +203,17 @@ class basic_json */ friend bool operator<(const value_t lhs, const value_t rhs) { - // no type is smaller than itself - if (lhs == rhs) - { - return false; - } - - switch (lhs) - { - case (value_t::null): - { - // nulls are smaller than all other types - return true; + std::array order = {{ + 0, // null + 3, // object + 4, // array + 5, // string + 1, // boolean + 2, // integer + 2 // float } - - case (value_t::boolean): - { - // only nulls are smaller than booleans - return (rhs != value_t::null); - } - - case (value_t::number_float): - case (value_t::number_integer): - { - switch (rhs) - { - // numbers are smaller than objects, arrays, and string - case (value_t::object): - case (value_t::array): - case (value_t::string): - { - return true; - } - - default: - { - return false; - } - } - } - - case (value_t::object): - { - switch (rhs) - { - // objects are smaller than arrays and string - case (value_t::array): - case (value_t::string): - { - return true; - } - - default: - { - return false; - } - } - } - - case (value_t::array): - { - // arrays are smaller than strings - return (rhs == value_t::string); - } - - default: - { - // a string is not smaller than any other types - return false; - } - } + }; + return order[static_cast(lhs)] < order[static_cast(rhs)]; }