|
|
@ -27,6 +27,7 @@ distribution.
|
|
|
|
#include "Export.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sstream>
|
|
|
|
#include <sstream>
|
|
|
|
//#include <ostream>
|
|
|
|
//#include <ostream>
|
|
|
|
namespace DFHack
|
|
|
|
namespace DFHack
|
|
|
@ -35,33 +36,42 @@ namespace DFHack
|
|
|
|
class BitArray
|
|
|
|
class BitArray
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
BitArray()
|
|
|
|
BitArray() : bits(NULL), size(0) {}
|
|
|
|
|
|
|
|
BitArray(const BitArray<T> &other) : bits(NULL), size(0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bits = NULL;
|
|
|
|
*this = other;
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~BitArray()
|
|
|
|
~BitArray()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(bits)
|
|
|
|
free(bits);
|
|
|
|
delete [] bits;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void clear_all ( void )
|
|
|
|
void clear_all ( void )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(bits)
|
|
|
|
if(bits)
|
|
|
|
memset(bits, 0, size);
|
|
|
|
memset(bits, 0, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void extend (T index)
|
|
|
|
void resize (unsigned newsize)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
uint32_t newsize = (index / 8) + 1;
|
|
|
|
if (newsize == size)
|
|
|
|
if (newsize <= size)
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
uint8_t *newbits = new uint8_t[newsize];
|
|
|
|
bits = (uint8_t*)realloc(bits, newsize);
|
|
|
|
memset(newbits, 0, newsize);
|
|
|
|
if (newsize > size)
|
|
|
|
memcpy(newbits, bits, size);
|
|
|
|
memset(bits+size, 0, newsize-size);
|
|
|
|
delete[] bits;
|
|
|
|
|
|
|
|
bits = newbits;
|
|
|
|
|
|
|
|
size = newsize;
|
|
|
|
size = newsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BitArray<T> &operator= (const BitArray<T> &other)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
resize(other.size);
|
|
|
|
|
|
|
|
memcpy(bits, other.bits, size);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void extend (T index)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
unsigned newsize = (index / 8) + 1;
|
|
|
|
|
|
|
|
if (newsize > size)
|
|
|
|
|
|
|
|
resize(newsize);
|
|
|
|
|
|
|
|
}
|
|
|
|
void set (T index, bool value = true)
|
|
|
|
void set (T index, bool value = true)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(!value)
|
|
|
|
if(!value)
|
|
|
@ -107,7 +117,7 @@ namespace DFHack
|
|
|
|
else return false;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// WARNING: this can truncate long bit arrays
|
|
|
|
/// WARNING: this can truncate long bit arrays
|
|
|
|
operator uint32_t ()
|
|
|
|
uint32_t as_int ()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(!bits)
|
|
|
|
if(!bits)
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
@ -147,4 +157,42 @@ namespace DFHack
|
|
|
|
uint8_t * bits;
|
|
|
|
uint8_t * bits;
|
|
|
|
uint32_t size;
|
|
|
|
uint32_t size;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T = int>
|
|
|
|
|
|
|
|
class DfArray
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
T *m_data;
|
|
|
|
|
|
|
|
unsigned short m_size;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
DfArray() : m_data(NULL), m_size(0) {}
|
|
|
|
|
|
|
|
~DfArray() { free(m_data); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DfArray(const DfArray<T> &other) : m_data(NULL), m_size(0) {
|
|
|
|
|
|
|
|
*this = other;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
T *data() { return m_data; }
|
|
|
|
|
|
|
|
const T *data() const { return m_data; }
|
|
|
|
|
|
|
|
unsigned size() const { return m_size; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
T& operator[] (unsigned i) { return m_data[i]; }
|
|
|
|
|
|
|
|
const T& operator[] (unsigned i) const { return m_data[i]; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void resize(unsigned new_size)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (new_size == m_size)
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_data = (T*)realloc(m_data, sizeof(T)*new_size);
|
|
|
|
|
|
|
|
if (new_size > m_size)
|
|
|
|
|
|
|
|
memset(m_data+sizeof(T)*m_size, 0, sizeof(T)*(new_size - m_size));
|
|
|
|
|
|
|
|
m_size = new_size;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DfArray &operator= (const DfArray<T> &other)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
resize(other.size());
|
|
|
|
|
|
|
|
memcpy(data(), other.data(), sizeof(T)*size());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|