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

View File

@ -1,127 +1,129 @@
#ifndef __ARRAY_H #ifndef __ARRAY_H
#define __ARRAY_H #define __ARRAY_H
#ifndef __OBJECT_H #ifndef __OBJECT_H
#include <object.h> #include <object.h>
#endif #endif
// @M // @M
#ifndef NULL #ifndef NULL
#define NULL 0L #define NULL 0L
#endif #endif
// @END // @END
class TArray; class TArray;
typedef (*COMPARE_FUNCTION)(const TObject**, const TObject**); 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
int size() const { return _size; } // Ritorna grandezza dell'array virtual bool ok() const ; // Ok se l'array non e' vuoto
int items() const { return _items; } // Ritorna numero di oggetti nell'array
int last() const; // Ritorna l'indice dell'ultimo oggetto int size() const { return _size; } // Ritorna grandezza dell'array
int items() const { return _items; } // Ritorna numero di oggetti nell'array
TObject& operator[] (int index) const ; // [] ritorna l'oggetto puntato da index int last() const; // Ritorna l'indice dell'ultimo oggetto
TObject* objptr(int index) const ; // Ritorna l'oggetto di posto [index]
TObject& operator[] (int index) const ; // [] ritorna l'oggetto puntato da index
virtual bool destroy(int index = -1, bool pack = FALSE); // Rimuove uno o tutti gli elementi (default) TObject* objptr(int index) const ; // Ritorna l'oggetto di posto [index]
virtual int add(TObject* obj, int index = -1) ; // Aggiunge un oggetto ad un array.
virtual int insert(TObject* obj, int index = 0); 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.
int add(const TObject& object, int index = -1) ; // Aggiunge un oggetto all'array. L'oggetto viene duplicato virtual int insert(TObject* obj, int index = 0);
int insert(const TObject& object, int index = 0);
TObject* remove(int index, bool pack = FALSE); int add(const TObject& object, int index = -1) ; // Aggiunge un oggetto all'array. L'oggetto viene duplicato
void swap(int i1, int i2); int insert(const TObject& object, int index = 0);
void pack(); // Rende contigui tutti gli elementi non nulli TObject* remove(int index, bool pack = FALSE);
void sort(COMPARE_FUNCTION = NULL); // Ordina i TObject (TSortable per default) void swap(int i1, int i2);
}; void pack(); // Rende contigui tutti gli elementi non nulli
void sort(COMPARE_FUNCTION = NULL); // Ordina i TObject (TSortable per default)
// @FIN };
inline TObject* TArray::objptr(int index) const
{ // @FIN
return (index < _size && index >= 0) ? _data[index] : NULL; inline TObject* TArray::objptr(int index) const
} {
return (index < _size && index >= 0) ? _data[index] : NULL;
// @FIN }
inline TObject& TArray::operator[] (int index) const
{ // @FIN
TObject* o = objptr(index); inline TObject& TArray::operator[] (int index) const
#ifdef DBG {
if (o == NULL) TObject* o = objptr(index);
fatal_box("Can't access NULL array item: %d of %d", index, size()); #ifdef DBG
#endif if (o == NULL)
return *o; fatal_box("Can't access NULL array item: %d of %d", index, size());
} #endif
return *o;
class TBit_array; }
class TBit class TBit_array;
{
TBit_array* _a; class TBit
long _b; {
TBit_array* _a;
public: long _b;
TBit(TBit_array* a, word b) : _a(a), _b(b) {}
bool operator =(bool on); public:
operator bool(); TBit(TBit_array* a, word b) : _a(a), _b(b) {}
}; bool operator =(bool on);
operator bool();
class TBit_array : public TObject };
{
word _size; class TBit_array : public TObject
byte* _bit; {
word _size;
protected: byte* _bit;
virtual bool ok() const;
virtual void print_on(ostream& out) const; protected:
virtual bool ok() const;
void resize(word size); virtual void print_on(ostream& out) const;
void copy(const TBit_array& ba);
word index(long n) const { return word(n >> 3); } void resize(word size);
byte mask(long n) const { return 1 << (n & 0x7); } void copy(const TBit_array& ba);
word index(long n) const { return word(n >> 3); }
public: byte mask(long n) const { return 1 << (n & 0x7); }
TBit_array(long size = 0);
TBit_array(const TBit_array& ba); public:
~TBit_array(); TBit_array(long size = 0);
TBit_array(const TBit_array& ba);
TBit_array& operator=(const TBit_array& ba); ~TBit_array();
bool operator[] (long n) const;
TBit_array& operator |=(const TBit_array& b); TBit_array& operator=(const TBit_array& ba);
// TBit operator[] (long n) { return TBit(this, n); } bool operator[] (long n) const;
long first_one() const; TBit_array& operator |=(const TBit_array& b);
long last_one() const; // TBit operator[] (long n) { return TBit(this, n); }
long ones() const; long first_one() const;
long last_one() const;
void set(long n); long ones() const;
void reset(long n);
void not(long n); void set(long n);
void reset(long n);
void set(long n, bool on) { on ? set(n) : reset(n); } void not(long n);
void set();
void reset(); void set(long n, bool on) { on ? set(n) : reset(n); }
void set(const char* numbers); void set();
}; void reset();
void set(const char* numbers);
inline bool TBit::operator =(bool on) { _a->set(_b, on); return on; } };
inline TBit::operator bool() { return _a->operator[](_b); }
inline bool TBit::operator =(bool on) { _a->set(_b, on); return on; }
#endif inline TBit::operator bool() { return _a->operator[](_b); }
#endif