From 71a1446fe0be8df923b5f057658585031709ff5b Mon Sep 17 00:00:00 2001 From: Doyle T Date: Tue, 24 Jul 2018 20:44:01 +1000 Subject: [PATCH] Add post fix/prefix operator++ and arbitrary operator+- --- DqnVHashTable.cpp | 47 ++++++++++++++++++++++++++++++++++++++++------- dqn.h | 9 +++++++-- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/DqnVHashTable.cpp b/DqnVHashTable.cpp index 8b5713e..f3f9156 100644 --- a/DqnVHashTable.cpp +++ b/DqnVHashTable.cpp @@ -31,7 +31,6 @@ void DqnVHashTable_Test() { Block blocks[] = {{0}, {1}, {2}, {3}, {4}}; - bool blockSeen[DQN_ARRAY_COUNT(blocks)] = {}; DqnVHashTable table = {}; DQN_DEFER(table.Free()); @@ -42,14 +41,48 @@ void DqnVHashTable_Test() table.Set(4, blocks[3]); table.Set(5, blocks[4]); - isize blocksSeen = 0; - for (Block const &block : table) { - DQN_ASSERT(blockSeen[block.x] == false); - blockSeen[block.x] = true; - blocksSeen++; + bool blockSeen[DQN_ARRAY_COUNT(blocks)] = {}; + isize blocksSeen = 0; + 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++"); + } } } diff --git a/dqn.h b/dqn.h index 22bd5d0..1cd2f47 100644 --- a/dqn.h +++ b/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); } 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 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)