CircularIndex: optimize further for non-power-of-two sizes

- Improve count() for non-power-of-two sizes by handling wrap-around
- Improve full() to use always use count(), which is cheaper in both
  scenarios now
pull/283/head
Yuri D'Elia 2023-07-22 14:54:47 +02:00
parent 6d621d4e81
commit a807184e74
1 changed files with 4 additions and 10 deletions

View File

@ -28,9 +28,7 @@ public:
/// @returns true if full
inline bool full() const {
// alternative without wrap-around logic:
// return tail != head && mask(tail) == mask(head);
return ((index_t)(head - tail) % (size * 2)) == size;
return count() == size;
}
/// Reset the indexes to empty
@ -67,13 +65,9 @@ public:
if constexpr (size_is_power2)
return head - tail;
else {
index_t i = tail;
index_t c = 0;
while (i != head) {
i = next(i);
++c;
}
return c;
return head >= tail
? (head - tail)
: (size * 2 + head) - tail;
}
}