Rework Pointer::operator<() loop.

I must be too dumb to understand the mess MSVC (32bit only) did with the
previous loop, and to figure out how it might have make it never end.
Anyway, hopefully any compiler can grok this new loop...
This commit is contained in:
ylavic 2018-12-11 00:40:05 +01:00
parent af17f196c6
commit 0e34ed43f4
2 changed files with 27 additions and 29 deletions

View File

@ -358,7 +358,7 @@ public:
//! Less than operator. //! Less than operator.
/*! /*!
\note Invalid pointers are never lesser than valid ones. \note Invalid pointers are always greater than valid ones.
*/ */
bool operator<(const GenericPointer& rhs) const { bool operator<(const GenericPointer& rhs) const {
if (!IsValid()) if (!IsValid())
@ -366,27 +366,20 @@ public:
if (!rhs.IsValid()) if (!rhs.IsValid())
return true; return true;
size_t i = 0, lCount = tokenCount_, rCount = rhs.tokenCount_; const Token *lTok = tokens_, *const lEnd = lTok + tokenCount_,
for (;;) { *rTok = rhs.tokens_, *const rEnd = rTok + rhs.tokenCount_;
if (!rCount) for (; lTok != lEnd && rTok != rEnd; ++lTok, ++rTok) {
return false; if (lTok->index != rTok->index)
if (!lCount) return lTok->index < rTok->index;
return true;
if (tokens_[i].index != rhs.tokens_[i].index) if (lTok->length > rTok->length)
return tokens_[i].index < rhs.tokens_[i].index; return std::memcmp(lTok->name, rTok->name, sizeof(Ch) * rTok->length) < 0;
if (tokens_[i].length > rhs.tokens_[i].length) int comp = std::memcmp(lTok->name, rTok->name, sizeof(Ch) * lTok->length);
return std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch) * rhs.tokens_[i].length) < 0; if (comp || lTok->length != rTok->length)
return comp <= 0;
int cmp = std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch) * tokens_[i].length);
if (cmp || tokens_[i].length != rhs.tokens_[i].length)
return cmp <= 0;
lCount--;
rCount--;
i++;
} }
return rTok != rEnd;
} }
//@} //@}

View File

@ -1515,8 +1515,8 @@ TEST(Pointer, LessThan) {
"/e/f.g", "/e/f.g",
"" ""
}; };
static struct { static const struct {
const char *string; const char *str;
bool valid; bool valid;
} ordered_pointers[] = { } ordered_pointers[] = {
{ "", true }, { "", true },
@ -1537,24 +1537,29 @@ TEST(Pointer, LessThan) {
{ "/e/f~g", false }, { "/e/f~g", false },
{ "/e/f~~g", false } { "/e/f~~g", false }
}; };
MemoryPoolAllocator<> allocator; typedef MemoryPoolAllocator<> AllocatorType;
typedef GenericPointer<Value, MemoryPoolAllocator<> > PooledPointer; typedef GenericPointer<Value, AllocatorType> PointerType;
std::multiset<PooledPointer> set; typedef std::multiset<PointerType> PointerSet;
AllocatorType allocator;
PointerSet set;
size_t i; size_t i;
for (i = 0; i < sizeof(pointers) / sizeof(*pointers); ++i) { EXPECT_EQ(sizeof(pointers) / sizeof(pointers[0]),
set.insert(PooledPointer(pointers[i], &allocator)); sizeof(ordered_pointers) / sizeof(ordered_pointers[0]));
for (i = 0; i < sizeof(pointers) / sizeof(pointers[0]); ++i) {
set.insert(PointerType(pointers[i], &allocator));
} }
i = 0; i = 0;
for (std::set<PooledPointer>::iterator it = set.begin(); it != set.end(); ++it) { for (PointerSet::iterator it = set.begin(); it != set.end(); ++it) {
EXPECT_TRUE(i < sizeof(ordered_pointers) / sizeof(*ordered_pointers)); EXPECT_TRUE(i < sizeof(ordered_pointers) / sizeof(ordered_pointers[0]));
EXPECT_EQ(it->IsValid(), ordered_pointers[i].valid); EXPECT_EQ(it->IsValid(), ordered_pointers[i].valid);
if (it->IsValid()) { if (it->IsValid()) {
std::stringstream ss; std::stringstream ss;
OStreamWrapper os(ss); OStreamWrapper os(ss);
EXPECT_TRUE(it->Stringify(os)); EXPECT_TRUE(it->Stringify(os));
EXPECT_EQ(ss.str(), ordered_pointers[i].string); EXPECT_EQ(ss.str(), ordered_pointers[i].str);
} }
i++; i++;
} }