Add post fix/prefix operator++ and arbitrary operator+-

This commit is contained in:
Doyle T 2018-07-24 20:44:01 +10:00
parent fc11e9ac0e
commit 71a1446fe0
2 changed files with 47 additions and 9 deletions

View File

@ -31,7 +31,6 @@ void DqnVHashTable_Test()
{ {
Block blocks[] = {{0}, {1}, {2}, {3}, {4}}; Block blocks[] = {{0}, {1}, {2}, {3}, {4}};
bool blockSeen[DQN_ARRAY_COUNT(blocks)] = {};
DqnVHashTable<Height, Block> table = {}; DqnVHashTable<Height, Block> table = {};
DQN_DEFER(table.Free()); DQN_DEFER(table.Free());
@ -42,6 +41,8 @@ void DqnVHashTable_Test()
table.Set(4, blocks[3]); table.Set(4, blocks[3]);
table.Set(5, blocks[4]); table.Set(5, blocks[4]);
{
bool blockSeen[DQN_ARRAY_COUNT(blocks)] = {};
isize blocksSeen = 0; isize blocksSeen = 0;
for (Block const &block : table) for (Block const &block : table)
{ {
@ -49,7 +50,39 @@ void DqnVHashTable_Test()
blockSeen[block.x] = true; blockSeen[block.x] = true;
blocksSeen++; blocksSeen++;
} }
DQN_ASSERT(blocksSeen == DQN_ARRAY_COUNT(blockSeen)); DQN_ASSERT(blocksSeen == DQN_ARRAY_COUNT(blockSeen));
Log(Status::Ok, "Auto iterator using prefix operator++");
}
{
bool blockSeen[DQN_ARRAY_COUNT(blocks)] = {};
isize blocksSeen = 0;
for (auto it = table.begin(); it != table.end();)
{
it = it + 1;
Block *block = it.GetCurrItem();
DQN_ASSERT(blockSeen[block->x] == false);
blockSeen[block->x] = true;
blocksSeen++;
}
DQN_ASSERT(blocksSeen == DQN_ARRAY_COUNT(blockSeen));
Log(Status::Ok, "Auto iterator using operator+");
}
{
bool blockSeen[DQN_ARRAY_COUNT(blocks)] = {};
isize blocksSeen = 0;
for (auto it = table.begin(); it != table.end(); it++)
{
Block *block = it.GetCurrItem();
DQN_ASSERT(blockSeen[block->x] == false);
blockSeen[block->x] = true;
blocksSeen++;
}
DQN_ASSERT(blocksSeen == DQN_ARRAY_COUNT(blockSeen));
Log(Status::Ok, "Auto iterator using postfix operator++");
}
} }
} }

9
dqn.h
View File

@ -2709,11 +2709,16 @@ DQN_VHASH_TABLE_TEMPLATE struct DqnVHashTable
bool operator!=(Iterator const &other) const { return !Equals(GetCurrEntry()->key, other.GetCurrEntry()->key); } bool operator!=(Iterator const &other) const { return !Equals(GetCurrEntry()->key, other.GetCurrEntry()->key); }
Item &operator* () const { return *GetCurrItem(); } Item &operator* () const { return *GetCurrItem(); }
Iterator operator++() { if (++indexInBucket >= GetCurrBucket()->entryIndex) { indexInBucket = 0; indexInIndexesOfUsedBuckets++; } return *this; } Iterator &operator++() { if (++indexInBucket >= GetCurrBucket()->entryIndex) { indexInBucket = 0; indexInIndexesOfUsedBuckets++; } return *this; }
Iterator &operator--() { if (--indexInBucket < 0) { indexInBucket = 0; indexInIndexesOfUsedBuckets = DQN_MAX(--indexInIndexesOfUsedBuckets, 0); } return *this; }
Iterator operator++(int) { Iterator result = *this; ++(*this); return result; }
Iterator operator--(int) { Iterator result = *this; --(*this); return result; }
Iterator operator+ (int offset) const { Iterator result = *this; DQN_FOR_EACH(i, offset) { (offset > 0) ? ++result : --result; } return result; } // TODO(doyle): Improve
Iterator operator- (int offset) const { Iterator result = *this; DQN_FOR_EACH(i, offset) { (offset > 0) ? --result : ++result; } return result; } // TODO(doyle): Improve
}; };
Iterator begin() { return Iterator(this); } Iterator begin() { return Iterator(this); }
Iterator end() { isize lastBucketIndex = indexesOfUsedBuckets[indexInIndexesOfUsedBuckets - 1]; isize lastIndexInBucket = DQN_MAX((buckets + lastBucketIndex)->entryIndex - 1, 0); return Iterator(this, DQN_MAX(lastBucketIndex, 0), DQN_MAX(lastIndexInBucket, 0)); } Iterator end() { isize lastBucketIndex = indexesOfUsedBuckets[indexInIndexesOfUsedBuckets - 1]; isize lastIndexInBucket = (buckets + lastBucketIndex)->entryIndex - 1; return Iterator(this, DQN_MAX(lastBucketIndex, 0), DQN_MAX(lastIndexInBucket, 0)); }
}; };
DQN_VHASH_TABLE_TEMPLATE void DQN_VHASH_TABLE_DECL::LazyInit(isize size) DQN_VHASH_TABLE_TEMPLATE void DQN_VHASH_TABLE_DECL::LazyInit(isize size)