diff options
author | ylavic <ylavic.dev@gmail.com> | 2018-12-13 00:32:56 +0300 |
---|---|---|
committer | ylavic <ylavic.dev@gmail.com> | 2018-12-13 00:32:56 +0300 |
commit | eb6ee17d2dcc596806875b2167cf8bf54f37e425 (patch) | |
tree | f30b2c73b6c1cbe7503a9f8e2da5938a75dda38d /include/rapidjson/pointer.h | |
parent | 0e34ed43f40fa522de4e88e3d817882cef3b6c40 (diff) |
Speed up Pointer::operator<().
Speed is more important than alphabetical order (which makes few sense in
JSON in general, and with pointers especially). The use case is indexing
in std containers, i.e. O(log n) with rbtree, so the faster comparison
the better.
Diffstat (limited to 'include/rapidjson/pointer.h')
-rw-r--r-- | include/rapidjson/pointer.h | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index 97a376a5..51805a63 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -366,20 +366,21 @@ public: if (!rhs.IsValid()) return true; - const Token *lTok = tokens_, *const lEnd = lTok + tokenCount_, - *rTok = rhs.tokens_, *const rEnd = rTok + rhs.tokenCount_; - for (; lTok != lEnd && rTok != rEnd; ++lTok, ++rTok) { - if (lTok->index != rTok->index) - return lTok->index < rTok->index; - - if (lTok->length > rTok->length) - return std::memcmp(lTok->name, rTok->name, sizeof(Ch) * rTok->length) < 0; - - int comp = std::memcmp(lTok->name, rTok->name, sizeof(Ch) * lTok->length); - if (comp || lTok->length != rTok->length) - return comp <= 0; + if (tokenCount_ != rhs.tokenCount_) + return tokenCount_ < rhs.tokenCount_; + + for (size_t i = 0; i < tokenCount_; i++) { + if (tokens_[i].index != rhs.tokens_[i].index) + return tokens_[i].index < rhs.tokens_[i].index; + + if (tokens_[i].length != rhs.tokens_[i].length) + return tokens_[i].length < rhs.tokens_[i].length; + + if (int cmp = std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch) * tokens_[i].length)) + return cmp < 0; } - return rTok != rEnd; + + return false; } //@} |