size_t for Array size

This commit is contained in:
Joachim Schöberl 2018-03-15 19:01:20 +01:00
parent 73a5381d2e
commit edf7861ee3
3 changed files with 38 additions and 12 deletions

View File

@ -78,18 +78,18 @@ namespace netgen
{ {
protected: protected:
/// the size /// the size
int size; size_t size;
/// the data /// the data
T * data; T * data;
public: public:
typedef T TELEM; typedef T TELEM;
/// provide size and memory /// provide size and memory
FlatArray (int asize, T * adata) FlatArray (size_t asize, T * adata)
: size(asize), data(adata) { ; } : size(asize), data(adata) { ; }
/// the size /// the size
int Size() const { return size; } size_t Size() const { return size; }
ArrayIterator<T,BASE,TIND> begin() const ArrayIterator<T,BASE,TIND> begin() const
{ return ArrayIterator<T,BASE,TIND> (*this, BASE); } { return ArrayIterator<T,BASE,TIND> (*this, BASE); }
@ -228,7 +228,7 @@ namespace netgen
using FlatArray<T,BASE,TIND>::data; using FlatArray<T,BASE,TIND>::data;
/// physical size of array /// physical size of array
int allocsize; size_t allocsize;
/// memory is responsibility of container /// memory is responsibility of container
bool ownmem; bool ownmem;
@ -242,7 +242,7 @@ namespace netgen
ownmem = 1; ownmem = 1;
} }
explicit Array(int asize) explicit Array(size_t asize)
: FlatArray<T, BASE, TIND> (asize, new T[asize]) : FlatArray<T, BASE, TIND> (asize, new T[asize])
{ {
allocsize = asize; allocsize = asize;
@ -286,7 +286,7 @@ namespace netgen
} }
/// Change logical size. If necessary, do reallocation. Keeps contents. /// Change logical size. If necessary, do reallocation. Keeps contents.
void SetSize(int nsize) void SetSize(size_t nsize)
{ {
if (nsize > allocsize) if (nsize > allocsize)
ReSize (nsize); ReSize (nsize);
@ -294,7 +294,7 @@ namespace netgen
} }
/// Change physical size. Keeps logical size. Keeps contents. /// Change physical size. Keeps logical size. Keeps contents.
void SetAllocSize (int nallocsize) void SetAllocSize (size_t nallocsize)
{ {
if (nallocsize > allocsize) if (nallocsize > allocsize)
ReSize (nallocsize); ReSize (nallocsize);
@ -398,16 +398,16 @@ namespace netgen
private: private:
/// resize array, at least to size minsize. copy contents /// resize array, at least to size minsize. copy contents
void ReSize (int minsize) void ReSize (size_t minsize)
{ {
int nsize = 2 * allocsize; size_t nsize = 2 * allocsize;
if (nsize < minsize) nsize = minsize; if (nsize < minsize) nsize = minsize;
if (data) if (data)
{ {
T * p = new T[nsize]; T * p = new T[nsize];
int mins = (nsize < size) ? nsize : size; size_t mins = (nsize < size) ? nsize : size;
// memcpy (p, data, mins * sizeof(T)); // memcpy (p, data, mins * sizeof(T));
#if defined(__GNUG__) && __GNUC__ < 5 && !defined(__clang__) #if defined(__GNUG__) && __GNUC__ < 5 && !defined(__clang__)
@ -448,7 +448,7 @@ namespace netgen
// double mem[(S*sizeof(T)+7) / 8]; // double mem[(S*sizeof(T)+7) / 8];
public: public:
/// Generate array of logical and physical size asize /// Generate array of logical and physical size asize
explicit ArrayMem(int asize = 0) explicit ArrayMem(size_t asize = 0)
: Array<T> (S, static_cast<T*> (static_cast<void*>(&mem[0]))) : Array<T> (S, static_cast<T*> (static_cast<void*>(&mem[0])))
{ {
size = asize; size = asize;
@ -470,7 +470,7 @@ namespace netgen
ArrayMem & operator= (const FlatArray<T> & a2) ArrayMem & operator= (const FlatArray<T> & a2)
{ {
this->SetSize (a2.Size()); this->SetSize (a2.Size());
for (int i = 0; i < size; i++) for (size_t i = 0; i < size; i++)
(*this)[i] = a2[i]; (*this)[i] = a2[i];
return *this; return *this;
} }

View File

@ -114,6 +114,18 @@ MyStr::MyStr(int i)
strcpy(str, buffer); strcpy(str, buffer);
} }
MyStr::MyStr(unsigned int i)
{
char buffer[32];
sprintf(buffer, "%d", i);
length = unsigned(strlen(buffer));
if (length > SHORTLEN)
str = new char[length + 1];
else
str = shortstr;
strcpy(str, buffer);
}
MyStr::MyStr(void * p) MyStr::MyStr(void * p)
{ {
char buffer[32]; char buffer[32];
@ -139,6 +151,18 @@ MyStr::MyStr(long l)
strcpy(str, buffer); strcpy(str, buffer);
} }
MyStr::MyStr(unsigned long l)
{
char buffer[32];
sprintf(buffer, "%ld", l);
length = unsigned(strlen(buffer));
if (length > SHORTLEN)
str = new char[length + 1];
else
str = shortstr;
strcpy(str, buffer);
}
MyStr::MyStr(double d) MyStr::MyStr(double d)
{ {
char buffer[32]; char buffer[32];

View File

@ -50,8 +50,10 @@ public:
MyStr(char); MyStr(char);
MyStr(const MyStr &); MyStr(const MyStr &);
MyStr(int); MyStr(int);
MyStr(unsigned);
MyStr(void *); MyStr(void *);
MyStr(long); MyStr(long);
MyStr(unsigned long);
MyStr(double); MyStr(double);
MyStr(const Point3d& p); MyStr(const Point3d& p);
MyStr(const Vec3d& p); MyStr(const Vec3d& p);