netgen/libsrc/general/table.cpp

255 lines
5.1 KiB
C++
Raw Normal View History

2009-01-13 04:40:13 +05:00
/**************************************************************************/
/* File: table.cpp */
/* Author: Joachim Schoeberl */
/* Date: 01. Jun. 95 */
/**************************************************************************/
/*
Abstract data type TABLE
*/
#include <mystdlib.h>
#include <myadt.hpp>
namespace netgen
{
//using namespace netgen;
BASE_TABLE :: BASE_TABLE (int size)
: data(size)
{
for (int i = 0; i < size; i++)
{
data[i].maxsize = 0;
data[i].size = 0;
data[i].col = NULL;
}
oneblock = NULL;
}
BASE_TABLE :: BASE_TABLE (const FlatArray<int> & entrysizes, int elemsize)
: data(entrysizes.Size())
{
2018-01-12 19:55:29 +05:00
size_t cnt = 0;
size_t n = entrysizes.Size();
2009-01-13 04:40:13 +05:00
2018-01-12 19:55:29 +05:00
for (size_t i = 0; i < n; i++)
2009-01-13 04:40:13 +05:00
cnt += entrysizes[i];
oneblock = new char[elemsize * cnt];
// mem_total_alloc_table += elemsize * cnt;
cnt = 0;
2018-01-12 19:55:29 +05:00
for (size_t i = 0; i < n; i++)
2009-01-13 04:40:13 +05:00
{
data[i].maxsize = entrysizes[i];
data[i].size = 0;
data[i].col = &oneblock[elemsize * cnt];
cnt += entrysizes[i];
}
}
BASE_TABLE :: ~BASE_TABLE ()
{
if (oneblock)
delete [] oneblock;
else
{
for (int i = 0; i < data.Size(); i++)
delete [] (char*)data[i].col;
}
}
void BASE_TABLE :: SetSize (int size)
{
for (int i = 0; i < data.Size(); i++)
delete [] (char*)data[i].col;
data.SetSize(size);
for (int i = 0; i < size; i++)
{
data[i].maxsize = 0;
data[i].size = 0;
data[i].col = NULL;
}
}
void BASE_TABLE :: ChangeSize (int size)
{
int oldsize = data.Size();
if (size == oldsize)
return;
if (size < oldsize)
for (int i = size; i < oldsize; i++)
delete [] (char*)data[i].col;
data.SetSize(size);
for (int i = oldsize; i < size; i++)
{
data[i].maxsize = 0;
data[i].size = 0;
data[i].col = NULL;
}
}
void BASE_TABLE :: IncSize2 (int i, int elsize)
{
#ifdef DEBUG
if (i < 0 || i >= data.Size())
{
MyError ("BASE_TABLE::Inc: Out of range");
return;
}
#endif
2011-07-15 03:36:19 +06:00
2009-01-13 04:40:13 +05:00
linestruct & line = data[i];
if (line.size == line.maxsize)
{
void * p = new char [(line.maxsize+5) * elsize];
memcpy (p, line.col, line.maxsize * elsize);
delete [] (char*)line.col;
line.col = p;
line.maxsize += 5;
}
line.size++;
}
2011-07-15 03:36:19 +06:00
void BASE_TABLE :: SetEntrySize2 (int i, int newsize, int elsize)
{
linestruct & line = data[i];
if (newsize > line.maxsize)
{
void * p = new char [newsize * elsize];
memcpy (p, line.col, min2 (newsize, line.size) * elsize);
delete [] (char*)line.col;
line.col = p;
}
line.size = newsize;
}
2009-01-13 04:40:13 +05:00
/*
void BASE_TABLE :: DecSize (int i)
{
#ifdef DEBUG
if (i < 0 || i >= data.Size())
{
MyError ("BASE_TABLE::Dec: Out of range");
return;
}
#endif
linestruct & line = data[i];
#ifdef DEBUG
if (line.size == 0)
{
MyError ("BASE_TABLE::Dec: EntrySize < 0");
return;
}
#endif
line.size--;
}
*/
void BASE_TABLE :: AllocateElementsOneBlock (int elemsize)
{
2018-01-12 19:55:29 +05:00
size_t cnt = 0;
size_t n = data.Size();
2009-01-13 04:40:13 +05:00
2018-01-12 19:55:29 +05:00
for (size_t i = 0; i < n; i++)
2009-01-13 04:40:13 +05:00
cnt += data[i].maxsize;
oneblock = new char[elemsize * cnt];
cnt = 0;
2018-01-12 19:55:29 +05:00
for (size_t i = 0; i < n; i++)
2009-01-13 04:40:13 +05:00
{
data[i].size = 0;
data[i].col = &oneblock[elemsize * cnt];
cnt += data[i].maxsize;
}
}
2018-01-12 19:55:29 +05:00
size_t BASE_TABLE :: AllocatedElements () const
2009-01-13 04:40:13 +05:00
{
2018-01-12 19:55:29 +05:00
size_t els = 0;
for (size_t i = 0; i < data.Size(); i++)
2009-01-13 04:40:13 +05:00
els += data[i].maxsize;
return els;
}
2018-01-12 19:55:29 +05:00
size_t BASE_TABLE :: UsedElements () const
2009-01-13 04:40:13 +05:00
{
2018-01-12 19:55:29 +05:00
size_t els = 0;
for (size_t i = 0; i < data.Size(); i++)
2009-01-13 04:40:13 +05:00
els += data[i].size;
return els;
}
void BASE_TABLE :: SetElementSizesToMaxSizes ()
{
for (int i = 0; i < data.Size(); i++)
data[i].size = data[i].maxsize;
}
2018-04-28 06:42:04 +05:00
2018-11-29 22:35:30 +05:00
void BASE_TABLE :: DoArchive (Archive & ar, int elemsize)
2018-04-28 06:42:04 +05:00
{
if (ar.Output())
{
size_t entries = 0, size = data.Size();
for (size_t i = 0; i < data.Size(); i++)
entries += data[i].size;
ar & size & entries;
for (size_t i = 0; i < data.Size(); i++)
{
ar & data[i].size;
ar.Do ((unsigned char*)data[i].col, data[i].size*elemsize);
2018-04-28 13:34:46 +05:00
/*
for (size_t j = 0; j < data[i].size*elemsize; j++)
ar & ((unsigned char*) data[i].col)[j];
cout << "write " << data[i].size*elemsize << " chars" << endl;
*/
2018-04-28 06:42:04 +05:00
}
}
else
{
size_t entries, size;
ar & size & entries;
data.SetSize(size);
oneblock = new char [entries*elemsize];
2018-04-28 13:34:46 +05:00
size_t cnt = 0;
for (size_t i = 0; i < size; i++)
2018-04-28 06:42:04 +05:00
{
ar & data[i].size;
2018-04-28 13:34:46 +05:00
data[i].col = oneblock+cnt;
2018-04-28 06:42:04 +05:00
data[i].maxsize = data[i].size;
2018-04-28 13:34:46 +05:00
ar.Do ((unsigned char*)(oneblock+cnt), data[i].size*elemsize);
cnt += data[i].size*elemsize;
2018-04-28 06:42:04 +05:00
}
}
}
2009-01-13 04:40:13 +05:00
}