Add post fix/prefix operator++ and arbitrary operator+-
This commit is contained in:
parent
fc11e9ac0e
commit
71a1446fe0
@ -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,14 +41,48 @@ void DqnVHashTable_Test()
|
|||||||
table.Set(4, blocks[3]);
|
table.Set(4, blocks[3]);
|
||||||
table.Set(5, blocks[4]);
|
table.Set(5, blocks[4]);
|
||||||
|
|
||||||
isize blocksSeen = 0;
|
|
||||||
for (Block const &block : table)
|
|
||||||
{
|
{
|
||||||
DQN_ASSERT(blockSeen[block.x] == false);
|
bool blockSeen[DQN_ARRAY_COUNT(blocks)] = {};
|
||||||
blockSeen[block.x] = true;
|
isize blocksSeen = 0;
|
||||||
blocksSeen++;
|
for (Block const &block : table)
|
||||||
|
{
|
||||||
|
DQN_ASSERT(blockSeen[block.x] == false);
|
||||||
|
blockSeen[block.x] = true;
|
||||||
|
blocksSeen++;
|
||||||
|
}
|
||||||
|
DQN_ASSERT(blocksSeen == DQN_ARRAY_COUNT(blockSeen));
|
||||||
|
Log(Status::Ok, "Auto iterator using prefix operator++");
|
||||||
}
|
}
|
||||||
|
|
||||||
DQN_ASSERT(blocksSeen == DQN_ARRAY_COUNT(blockSeen));
|
{
|
||||||
|
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
9
dqn.h
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user