Commit 0e34ed43 authored by ylavic's avatar ylavic

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...
parent af17f196
......@@ -358,7 +358,7 @@ public:
//! 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 {
if (!IsValid())
......@@ -366,27 +366,20 @@ public:
if (!rhs.IsValid())
return true;
size_t i = 0, lCount = tokenCount_, rCount = rhs.tokenCount_;
for (;;) {
if (!rCount)
return false;
if (!lCount)
return true;
if (tokens_[i].index != rhs.tokens_[i].index)
return tokens_[i].index < rhs.tokens_[i].index;
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 (tokens_[i].length > rhs.tokens_[i].length)
return std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch) * rhs.tokens_[i].length) < 0;
if (lTok->length > rTok->length)
return std::memcmp(lTok->name, rTok->name, sizeof(Ch) * rTok->length) < 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++;
int comp = std::memcmp(lTok->name, rTok->name, sizeof(Ch) * lTok->length);
if (comp || lTok->length != rTok->length)
return comp <= 0;
}
return rTok != rEnd;
}
//@}
......
......@@ -1515,8 +1515,8 @@ TEST(Pointer, LessThan) {
"/e/f.g",
""
};
static struct {
const char *string;
static const struct {
const char *str;
bool valid;
} ordered_pointers[] = {
{ "", true },
......@@ -1537,24 +1537,29 @@ TEST(Pointer, LessThan) {
{ "/e/f~g", false },
{ "/e/f~~g", false }
};
MemoryPoolAllocator<> allocator;
typedef GenericPointer<Value, MemoryPoolAllocator<> > PooledPointer;
std::multiset<PooledPointer> set;
typedef MemoryPoolAllocator<> AllocatorType;
typedef GenericPointer<Value, AllocatorType> PointerType;
typedef std::multiset<PointerType> PointerSet;
AllocatorType allocator;
PointerSet set;
size_t i;
for (i = 0; i < sizeof(pointers) / sizeof(*pointers); ++i) {
set.insert(PooledPointer(pointers[i], &allocator));
EXPECT_EQ(sizeof(pointers) / sizeof(pointers[0]),
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;
for (std::set<PooledPointer>::iterator it = set.begin(); it != set.end(); ++it) {
EXPECT_TRUE(i < sizeof(ordered_pointers) / sizeof(*ordered_pointers));
for (PointerSet::iterator it = set.begin(); it != set.end(); ++it) {
EXPECT_TRUE(i < sizeof(ordered_pointers) / sizeof(ordered_pointers[0]));
EXPECT_EQ(it->IsValid(), ordered_pointers[i].valid);
if (it->IsValid()) {
std::stringstream ss;
OStreamWrapper os(ss);
EXPECT_TRUE(it->Stringify(os));
EXPECT_EQ(ss.str(), ordered_pointers[i].string);
EXPECT_EQ(ss.str(), ordered_pointers[i].str);
}
i++;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment