Skip to content

Commit

Permalink
Add a bit vector class, and throw out a memleak.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alcaro committed May 14, 2015
1 parent ce4c19c commit ef0ed51
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
68 changes: 68 additions & 0 deletions array.h
Expand Up @@ -197,3 +197,71 @@ template<typename T> class array : public arrayview<T> {
free(this->items);
}
};


template<> class array<bool> {
protected:
class null_only;

uint8_t* bits;
size_t nbits;

class entry {
array<bool>& parent;
size_t index;

public:
operator bool() const { return parent.get(index); }
entry& operator=(bool val) { parent.set(index, val); return *this; }

entry(array<bool>& parent, size_t index) : parent(parent), index(index) {}
};
friend class entry;

bool get(size_t n) const
{
if (n >= nbits) return false;
return bits[n/8]>>(n&7) & 1;
}

void set(size_t n, bool val)
{
if (n >= nbits)
{
size_t prevbytes = bitround((nbits+7)/8);
size_t newbytes = bitround((n+8)/8);
if (newbytes > prevbytes)
{
bits = realloc(bits, newbytes);
memset(bits+prevbytes, 0, newbytes-prevbytes);
}
nbits = n+1;
}
uint8_t& byte = bits[n/8];
byte &=~ (1<<(n&7));
byte |= (val<<(n&7));
}

public:
bool operator[](size_t n) const { return get(n); }
entry operator[](size_t n) { return entry(*this, n); }

size_t len() const { return nbits; }
void reset()
{
free(this->bits);
this->bits = NULL;
this->nbits = 0;
}

array()
{
this->bits = NULL;
this->nbits = 0;
}

~array()
{
free(this->bits);
}
};
1 change: 1 addition & 0 deletions fifo.h
Expand Up @@ -110,6 +110,7 @@ fifo()
for (size_t i=head;i<buflen;i++) items[i].~T();
for (size_t i=0;i<tail;i++) items[i].~T();
}
free(items);
}

};
1 change: 1 addition & 0 deletions minir.h
Expand Up @@ -173,6 +173,7 @@ namespace minir {
io_is_core,// This is the core, the most important device. Output only.
io_core, // Must see or emit all I/O from the core; both input, frames, and audio/video.
io_user, // Takes input from, or sends output to, the user, and must not be optimized out if unused.
io_file, // Reads or writes a file.
io_multi, // Everything beyond this point repeats an arbitrary number of times.
io_thread, // Events are not guaranteed dispatched on the input thread; the device manager may need to buffer the events. Output only.

Expand Down
21 changes: 20 additions & 1 deletion tests.cpp
Expand Up @@ -229,12 +229,31 @@ static void test_hashset()
}
}

static void test_bitarray()
{
{
array<bool> a;
for (int i=0;i<=7;i++) assert(!a[i]);
a[7]=true;
for (int i=0;i<=6;i++) assert(!a[i]);
assert(a[7]);
for (int i=8;i<=15;i++) assert(!a[i]);

for (int i=0;i<=7;i++) a[i]=true;
a[15]=true;
for (int i=0;i<=7;i++) assert(a[i]);
for (int i=8;i<=14;i++) assert(!a[i]);
assert(a[15]);
}
}

int main()
{
//test_multiint();
//test_sort();
//test_fifo();
//test_endian();
test_hashset();
//test_hashset();
//test_bitarray();
}
#endif

0 comments on commit ef0ed51

Please sign in to comment.