Skip to content

Commit 055f1fa

Browse files
committed
Add less than operator to Pointer.
Allows to sort pointers in (std-)containers and/or index by them.
1 parent 66eb606 commit 055f1fa

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

include/rapidjson/pointer.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,39 @@ class GenericPointer {
356356
*/
357357
bool operator!=(const GenericPointer& rhs) const { return !(*this == rhs); }
358358

359+
//! Less than operator.
360+
/*!
361+
\note Invalid pointers are never lesser than valid ones.
362+
*/
363+
bool operator<(const GenericPointer& rhs) const {
364+
if (!IsValid())
365+
return false;
366+
if (!rhs.IsValid())
367+
return true;
368+
369+
size_t i = 0, lCount = tokenCount_, rCount = rhs.tokenCount_;
370+
for (;;) {
371+
if (!rCount)
372+
return false;
373+
if (!lCount)
374+
return true;
375+
376+
if (tokens_[i].index != rhs.tokens_[i].index)
377+
return tokens_[i].index < rhs.tokens_[i].index;
378+
379+
if (tokens_[i].length > rhs.tokens_[i].length)
380+
return std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch) * rhs.tokens_[i].length) < 0;
381+
382+
int cmp = std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch) * tokens_[i].length);
383+
if (cmp || tokens_[i].length != rhs.tokens_[i].length)
384+
return cmp <= 0;
385+
386+
lCount--;
387+
rCount--;
388+
i++;
389+
}
390+
}
391+
359392
//@}
360393

361394
//!@name Stringify

0 commit comments

Comments
 (0)