Aggiunto costruttore TArray(TArray&) che duplica tutto il contenuto

git-svn-id: svn://10.65.10.50/trunk@217 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
villa 1994-09-12 08:02:05 +00:00
parent 2b1c61fb33
commit 946f6a4514
2 changed files with 530 additions and 521 deletions

View File

@ -1,53 +1,52 @@
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <array.h> #include <array.h>
#include <strings.h> #include <strings.h>
void TArray::resize(int arraysize) void TArray::resize(int arraysize)
{ {
CHECK(arraysize > size(), "Can't reduce array size."); CHECK(arraysize > size(), "Can't reduce array size.");
TObject** newdata = new TObject* [arraysize]; TObject** newdata = new TObject* [arraysize];
int i = 0; int i = 0;
if (_data != NULL) if (_data != NULL)
for (i = 0; i < size(); i++) newdata[i] = _data[i]; for (i = 0; i < size(); i++) newdata[i] = _data[i];
while (i < arraysize) newdata[i++] = NULL; while (i < arraysize) newdata[i++] = NULL;
if (_data != NULL) delete _data; if (_data != NULL) delete _data;
_size = arraysize; _size = arraysize;
_data = newdata; _data = newdata;
} }
bool TArray::destroy(int index, bool pack) bool TArray::destroy(int index, bool pack)
{ {
const int old = _items; const int old = _items;
if (index < 0) if (index < 0)
{ {
for (int i = size(); i--;) if (_data[i] != NULL) for (int i = size(); i--;) if (_data[i] != NULL)
{ {
delete _data[i]; delete _data[i];
_data[i] = NULL; _data[i] = NULL;
} }
_items = 0; _items = 0;
} }
else else
{ {
TObject* o = remove(index, pack); TObject* o = remove(index, pack);
if (o) delete o; if (o) delete o;
} }
return _items < old; return _items < old;
} }
TArray::TArray(int arraysize) : _size(0), _items(0), _data(NULL) TArray::TArray(int arraysize) : _size(0), _items(0), _data(NULL)
{ {
@ -58,162 +57,170 @@ TArray::TArray() : _size(0), _items(0), _data(NULL)
{ {
} }
TArray::TArray(const TArray& a)
{
if (a.size()) resize(a.size());
for (_items = 0; _items < a.items(); _items++)
_data[_items] = a[_items].dup();
}
TArray::~TArray() TArray::~TArray()
{ {
if (ok()) if (ok())
{ {
destroy(); destroy();
delete _data; delete _data;
} }
} }
const char* TArray::class_name() const const char* TArray::class_name() const
{ {
return "Array"; return "Array";
} }
word TArray::class_id() const word TArray::class_id() const
{ {
return CLASS_ARRAY; return CLASS_ARRAY;
} }
void TArray::print_on(ostream& out) const void TArray::print_on(ostream& out) const
{ {
for (int i = 0; i < size(); i++) for (int i = 0; i < size(); i++)
{ {
out.width(4); out.width(4);
out << i << ' '; out << i << ' ';
out.width(); out.width();
if (_data[i] != NULL) out << *_data[i]; if (_data[i] != NULL) out << *_data[i];
else out << "(null)"; else out << "(null)";
out << endl; out << endl;
} }
} }
bool TArray::ok() const bool TArray::ok() const
{ {
return(size() != 0 && (_data != NULL)); return(size() != 0 && (_data != NULL));
} }
int TArray::add(TObject* object, int index) int TArray::add(TObject* object, int index)
{ {
if (index < 0) for (index = 0; index < size() && _data[index]; index++); if (index < 0) for (index = 0; index < size() && _data[index]; index++);
if (index >= size()) resize(3*index/2 + 1); if (index >= size()) resize(3*index/2 + 1);
if (_data[index] != NULL) if (_data[index] != NULL)
{ {
#ifdef DBG #ifdef DBG
if (object == _data[index]) if (object == _data[index])
{ {
error_box("Iu ar traing tu overrait en arrei obgiect: %d", index); error_box("Iu ar traing tu overrait en arrei obgiect: %d", index);
return index; return index;
} }
#endif #endif
delete _data[index]; delete _data[index];
_items--; _items--;
} }
_data[index] = object; _data[index] = object;
if (object != NULL) _items++; if (object != NULL) _items++;
return index; return index;
} }
int TArray::insert(TObject* object, int index) int TArray::insert(TObject* object, int index)
{ {
if (objptr(index)) if (objptr(index))
{ {
if (items() == size()) add(NULL); if (items() == size()) add(NULL);
for (int i = size()-1; i >= index; i--) for (int i = size()-1; i >= index; i--)
_data[i] = _data[i-1]; _data[i] = _data[i-1];
_data[index] = NULL; _data[index] = NULL;
} }
return add(object, index); return add(object, index);
} }
int TArray::add(const TObject& object, int index) int TArray::add(const TObject& object, int index)
{ {
TObject* objptr = object.dup(); TObject* objptr = object.dup();
return add(objptr, index); return add(objptr, index);
} }
int TArray::insert(const TObject& object, int index) int TArray::insert(const TObject& object, int index)
{ {
TObject* objptr = object.dup(); TObject* objptr = object.dup();
return insert(objptr, index); return insert(objptr, index);
} }
TObject* TArray::remove(int index, bool dopack) TObject* TArray::remove(int index, bool dopack)
{ {
TObject* o = objptr(index); TObject* o = objptr(index);
if (o) if (o)
{ {
_data[index] = NULL; _data[index] = NULL;
_items--; _items--;
} }
if (dopack) pack(); if (dopack) pack();
return o; return o;
} }
void TArray::swap(int i1, int i2) void TArray::swap(int i1, int i2)
{ {
TObject* o1 = remove(i1, FALSE); TObject* o1 = remove(i1, FALSE);
TObject* o2 = remove(i2, FALSE); TObject* o2 = remove(i2, FALSE);
if (o1) add(o1, i2); if (o1) add(o1, i2);
if (o2) add(o2, i1); if (o2) add(o2, i1);
} }
int TArray::last() const int TArray::last() const
{ {
for (int last = size(); --last >= 0; ) for (int last = size(); --last >= 0; )
if (_data[last]) break; if (_data[last]) break;
return last; return last;
} }
void TArray::pack() void TArray::pack()
{ {
int next = 0; int next = 0;
for (int i = 0; i < size(); i++) for (int i = 0; i < size(); i++)
{ {
if (_data[i] != NULL) if (_data[i] != NULL)
{ {
if (next < i) if (next < i)
{ {
_data[next] = _data[i]; _data[next] = _data[i];
_data[i] = NULL; _data[i] = NULL;
} }
next++; next++;
} }
} }
} }
HIDDEN int sortable_compare(const TObject** o1, const TObject** o2) HIDDEN int sortable_compare(const TObject** o1, const TObject** o2)
{ {
const TSortable* s1 = (const TSortable*)*o1; const TSortable* s1 = (const TSortable*)*o1;
const TSortable* s2 = (const TSortable*)*o2; const TSortable* s2 = (const TSortable*)*o2;
return s1->compare(*s2); return s1->compare(*s2);
} }
void TArray::sort(COMPARE_FUNCTION compare) void TArray::sort(COMPARE_FUNCTION compare)
{ {
typedef int (*qsortfunc)(const void*, const void*); typedef int (*qsortfunc)(const void*, const void*);
if (compare == NULL) compare = sortable_compare; if (compare == NULL) compare = sortable_compare;
pack(); pack();
qsort(_data, items(), sizeof(TObject*), (qsortfunc)compare); qsort(_data, items(), sizeof(TObject*), (qsortfunc)compare);
} }
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
@ -222,173 +229,173 @@ void TArray::sort(COMPARE_FUNCTION compare)
TBit_array::TBit_array(long size) : _bit(NULL), _size(0) TBit_array::TBit_array(long size) : _bit(NULL), _size(0)
{ {
if (size) resize(index(size)); if (size) resize(index(size));
} }
void TBit_array::copy(const TBit_array& ba) void TBit_array::copy(const TBit_array& ba)
{ {
if (_bit) if (_bit)
{ {
delete _bit; delete _bit;
_bit = NULL; _bit = NULL;
_size = 0; _size = 0;
} }
resize(ba._size-1); resize(ba._size-1);
memcpy(_bit, ba._bit, _size); memcpy(_bit, ba._bit, _size);
} }
TBit_array::TBit_array(const TBit_array& ba) : _bit(NULL), _size(0) TBit_array::TBit_array(const TBit_array& ba) : _bit(NULL), _size(0)
{ copy(ba); } { copy(ba); }
TBit_array& TBit_array::operator=(const TBit_array& ba) TBit_array& TBit_array::operator=(const TBit_array& ba)
{ {
copy(ba); copy(ba);
return *this; return *this;
} }
TBit_array::~TBit_array() TBit_array::~TBit_array()
{ {
if (_bit) delete _bit; if (_bit) delete _bit;
} }
// Certified 100% // Certified 100%
void TBit_array::set() void TBit_array::set()
{ {
if (_bit) memset(_bit, 0xFF, _size); if (_bit) memset(_bit, 0xFF, _size);
} }
// Certified 100% // Certified 100%
void TBit_array::reset() void TBit_array::reset()
{ {
if (_bit) memset(_bit, 0x0, _size); if (_bit) memset(_bit, 0x0, _size);
} }
// Certified 99% // Certified 99%
void TBit_array::resize(word size) void TBit_array::resize(word size)
{ {
word oldsize = _size; word oldsize = _size;
byte* oldbit = _bit; byte* oldbit = _bit;
_size = size+1; _size = size+1;
_bit = new byte[_size]; _bit = new byte[_size];
reset(); reset();
if (oldsize) if (oldsize)
{ {
memcpy(_bit, oldbit, oldsize); memcpy(_bit, oldbit, oldsize);
delete oldbit; delete oldbit;
} }
} }
bool TBit_array::operator[] (long n) const bool TBit_array::operator[] (long n) const
{ {
const word i = index(n); const word i = index(n);
if (i >= _size) return FALSE; if (i >= _size) return FALSE;
return (_bit[i] & mask(n)) != 0; return (_bit[i] & mask(n)) != 0;
} }
// Certified 99% // Certified 99%
TBit_array& TBit_array::operator|= (const TBit_array& ba) TBit_array& TBit_array::operator|= (const TBit_array& ba)
{ {
CHECK(_size >= ba._size, "TBit_array |=: right operand too big"); CHECK(_size >= ba._size, "TBit_array |=: right operand too big");
for (word i = 0; i < _size; i++) for (word i = 0; i < _size; i++)
_bit[i] |= ba._bit[i]; _bit[i] |= ba._bit[i];
return *this; return *this;
} }
// Certified 99% // Certified 99%
void TBit_array::set(long n) void TBit_array::set(long n)
{ {
const word i = index(n); const word i = index(n);
if (i >= _size) resize(i); if (i >= _size) resize(i);
_bit[i] |= mask(n); _bit[i] |= mask(n);
} }
// Certified 99% // Certified 99%
void TBit_array::reset(long n) void TBit_array::reset(long n)
{ {
const word i = index(n); const word i = index(n);
if (i < _size) if (i < _size)
_bit[i] &= ~mask(n); _bit[i] &= ~mask(n);
} }
// Certified 99% // Certified 99%
void TBit_array::not(long n) void TBit_array::not(long n)
{ {
const word i = index(n); const word i = index(n);
if (i >= _size) resize(i); if (i >= _size) resize(i);
_bit[i] ^= mask(n); _bit[i] ^= mask(n);
} }
// Certified 50% // Certified 50%
long TBit_array::ones() const long TBit_array::ones() const
{ {
long one = 0; long one = 0;
for (word i = 0; i < _size; i++) for (word i = 0; i < _size; i++)
{ {
const byte b = _bit[i]; const byte b = _bit[i];
if (b) if (b)
{ {
for (byte m = 1; m; m <<= 1) for (byte m = 1; m; m <<= 1)
if (b & m) one++; if (b & m) one++;
} }
} }
return one; return one;
} }
// Certified 90% // Certified 90%
long TBit_array::last_one() const long TBit_array::last_one() const
{ {
for (word i = _size; i--;) for (word i = _size; i--;)
{ {
const byte b = _bit[i]; const byte b = _bit[i];
if (b) if (b)
{ {
for (byte j = 8; j--;) for (byte j = 8; j--;)
if ((1<<j) & b) return (long(i)<<3) + j; if ((1<<j) & b) return (long(i)<<3) + j;
} }
} }
return -1; return -1;
} }
// Certified 90% // Certified 90%
long TBit_array::first_one() const long TBit_array::first_one() const
{ {
for (word i = 0; i < _size; i++) for (word i = 0; i < _size; i++)
{ {
const byte b = _bit[i]; const byte b = _bit[i];
if (b) if (b)
{ {
for (byte j = 0; j < 8; j++) for (byte j = 0; j < 8; j++)
if ((1<<j) & b) return (long(i)<<3)+j; if ((1<<j) & b) return (long(i)<<3)+j;
} }
} }
return -1; return -1;
} }
bool TBit_array::ok() const bool TBit_array::ok() const
{ {
return _bit != NULL && _size > 0; return _bit != NULL && _size > 0;
} }
void TBit_array::set(const char* numbers) void TBit_array::set(const char* numbers)
{ {
TToken_string s(numbers, ' '); TToken_string s(numbers, ' ');
for (const char* n = s.get(0); n; n = s.get()) for (const char* n = s.get(0); n; n = s.get())
if (isdigit(*n)) set(atol(n)); if (isdigit(*n)) set(atol(n));
} }
void TBit_array::print_on(ostream& out) const void TBit_array::print_on(ostream& out) const
{ {
const long last = _size<<3; const long last = _size<<3;
for (long i = 1; i < last; i++) for (long i = 1; i < last; i++)
if (operator[](i)) out << ' ' << i; if (operator[](i)) out << ' ' << i;
} }

View File

@ -17,108 +17,110 @@ typedef (*COMPARE_FUNCTION)(const TObject**, const TObject**);
class TArray : public TObject class TArray : public TObject
{ {
TObject** _data; // Array of pointers to objects TObject** _data; // Array of pointers to objects
int _size; // Size of the array int _size; // Size of the array
int _items; // Number of items in the array int _items; // Number of items in the array
protected: protected:
// @FPROT // @FPROT
void resize(int newdim); // Estende l'array void resize(int newdim); // Estende l'array
public: public:
// @FPUB // @FPUB
TArray(int arraysize); // Crea un array (chiama resize) TArray(int arraysize); // Crea un array (chiama resize)
TArray(); // Crea un array (non chiama resize) TArray(); // Crea un array (non chiama resize)
virtual ~TArray() ; TArray(const TArray&); // copia tutto e duplica gli elementi
virtual const char* class_name() const ; // Ritorna il nome della classe // (casino se non hanno dup() definita)
virtual word class_id() const ; // Ritorna il class-id virtual ~TArray() ;
virtual void print_on(ostream& out) const ; // Stampa un array virtual const char* class_name() const ; // Ritorna il nome della classe
virtual bool ok() const ; // Ok se l'array non e' vuoto virtual word class_id() const ; // Ritorna il class-id
virtual void print_on(ostream& out) const ; // Stampa un array
virtual bool ok() const ; // Ok se l'array non e' vuoto
int size() const { return _size; } // Ritorna grandezza dell'array int size() const { return _size; } // Ritorna grandezza dell'array
int items() const { return _items; } // Ritorna numero di oggetti nell'array int items() const { return _items; } // Ritorna numero di oggetti nell'array
int last() const; // Ritorna l'indice dell'ultimo oggetto int last() const; // Ritorna l'indice dell'ultimo oggetto
TObject& operator[] (int index) const ; // [] ritorna l'oggetto puntato da index TObject& operator[] (int index) const ; // [] ritorna l'oggetto puntato da index
TObject* objptr(int index) const ; // Ritorna l'oggetto di posto [index] TObject* objptr(int index) const ; // Ritorna l'oggetto di posto [index]
virtual bool destroy(int index = -1, bool pack = FALSE); // Rimuove uno o tutti gli elementi (default) virtual bool destroy(int index = -1, bool pack = FALSE); // Rimuove uno o tutti gli elementi (default)
virtual int add(TObject* obj, int index = -1) ; // Aggiunge un oggetto ad un array. virtual int add(TObject* obj, int index = -1) ; // Aggiunge un oggetto ad un array.
virtual int insert(TObject* obj, int index = 0); virtual int insert(TObject* obj, int index = 0);
int add(const TObject& object, int index = -1) ; // Aggiunge un oggetto all'array. L'oggetto viene duplicato int add(const TObject& object, int index = -1) ; // Aggiunge un oggetto all'array. L'oggetto viene duplicato
int insert(const TObject& object, int index = 0); int insert(const TObject& object, int index = 0);
TObject* remove(int index, bool pack = FALSE); TObject* remove(int index, bool pack = FALSE);
void swap(int i1, int i2); void swap(int i1, int i2);
void pack(); // Rende contigui tutti gli elementi non nulli void pack(); // Rende contigui tutti gli elementi non nulli
void sort(COMPARE_FUNCTION = NULL); // Ordina i TObject (TSortable per default) void sort(COMPARE_FUNCTION = NULL); // Ordina i TObject (TSortable per default)
}; };
// @FIN // @FIN
inline TObject* TArray::objptr(int index) const inline TObject* TArray::objptr(int index) const
{ {
return (index < _size && index >= 0) ? _data[index] : NULL; return (index < _size && index >= 0) ? _data[index] : NULL;
} }
// @FIN // @FIN
inline TObject& TArray::operator[] (int index) const inline TObject& TArray::operator[] (int index) const
{ {
TObject* o = objptr(index); TObject* o = objptr(index);
#ifdef DBG #ifdef DBG
if (o == NULL) if (o == NULL)
fatal_box("Can't access NULL array item: %d of %d", index, size()); fatal_box("Can't access NULL array item: %d of %d", index, size());
#endif #endif
return *o; return *o;
} }
class TBit_array; class TBit_array;
class TBit class TBit
{ {
TBit_array* _a; TBit_array* _a;
long _b; long _b;
public: public:
TBit(TBit_array* a, word b) : _a(a), _b(b) {} TBit(TBit_array* a, word b) : _a(a), _b(b) {}
bool operator =(bool on); bool operator =(bool on);
operator bool(); operator bool();
}; };
class TBit_array : public TObject class TBit_array : public TObject
{ {
word _size; word _size;
byte* _bit; byte* _bit;
protected: protected:
virtual bool ok() const; virtual bool ok() const;
virtual void print_on(ostream& out) const; virtual void print_on(ostream& out) const;
void resize(word size); void resize(word size);
void copy(const TBit_array& ba); void copy(const TBit_array& ba);
word index(long n) const { return word(n >> 3); } word index(long n) const { return word(n >> 3); }
byte mask(long n) const { return 1 << (n & 0x7); } byte mask(long n) const { return 1 << (n & 0x7); }
public: public:
TBit_array(long size = 0); TBit_array(long size = 0);
TBit_array(const TBit_array& ba); TBit_array(const TBit_array& ba);
~TBit_array(); ~TBit_array();
TBit_array& operator=(const TBit_array& ba); TBit_array& operator=(const TBit_array& ba);
bool operator[] (long n) const; bool operator[] (long n) const;
TBit_array& operator |=(const TBit_array& b); TBit_array& operator |=(const TBit_array& b);
// TBit operator[] (long n) { return TBit(this, n); } // TBit operator[] (long n) { return TBit(this, n); }
long first_one() const; long first_one() const;
long last_one() const; long last_one() const;
long ones() const; long ones() const;
void set(long n); void set(long n);
void reset(long n); void reset(long n);
void not(long n); void not(long n);
void set(long n, bool on) { on ? set(n) : reset(n); } void set(long n, bool on) { on ? set(n) : reset(n); }
void set(); void set();
void reset(); void reset();
void set(const char* numbers); void set(const char* numbers);
}; };
inline bool TBit::operator =(bool on) { _a->set(_b, on); return on; } inline bool TBit::operator =(bool on) { _a->set(_b, on); return on; }