Add SmallIntegerSet.empty.

This commit is contained in:
Sönke Ludwig 2017-06-10 10:27:20 +02:00
parent 13a0adb967
commit abf8587078
No known key found for this signature in database
GPG key ID: D95E8DB493EE314C

View file

@ -180,8 +180,11 @@ struct SmallIntegerSet(V : size_t)
{ {
private { private {
uint[][4] m_bits; uint[][4] m_bits;
size_t m_count;
} }
@property bool empty() const { return m_count == 0; }
void insert(V i) void insert(V i)
{ {
foreach (j; 0 .. m_bits.length) { foreach (j; 0 .. m_bits.length) {
@ -189,6 +192,7 @@ struct SmallIntegerSet(V : size_t)
i /= 32; i /= 32;
if (i >= m_bits[j].length) if (i >= m_bits[j].length)
m_bits[j].length = nextPOT(i+1); m_bits[j].length = nextPOT(i+1);
if (j == 0 && !(m_bits[j][i] & b)) m_count++;
m_bits[j][i] |= b; m_bits[j][i] |= b;
} }
} }
@ -199,6 +203,7 @@ struct SmallIntegerSet(V : size_t)
uint b = 1u << (i%32); uint b = 1u << (i%32);
i /= 32; i /= 32;
if (!m_bits[j][i]) break; if (!m_bits[j][i]) break;
if (j == 0 && m_bits[j][i] & b) m_count--;
m_bits[j][i] &= ~b; m_bits[j][i] &= ~b;
if (m_bits[j][i]) break; if (m_bits[j][i]) break;
} }
@ -237,10 +242,13 @@ unittest {
SmallIntegerSet!uint set; SmallIntegerSet!uint set;
bool[uint] controlset; bool[uint] controlset;
assert(set.empty);
foreach (i; ints) { foreach (i; ints) {
set.insert(i); set.insert(i);
controlset[i] = true; controlset[i] = true;
} }
assert(!set.empty);
foreach (jidx, j; ints) { foreach (jidx, j; ints) {
size_t cnt = 0; size_t cnt = 0;
@ -256,6 +264,7 @@ unittest {
set.remove(j); set.remove(j);
controlset.remove(j); controlset.remove(j);
} }
assert(set.empty);
foreach (i; set) assert(false); foreach (i; set) assert(false);
} }