From 946f6a4514cfdc2067946e608498c5886f0a8467 Mon Sep 17 00:00:00 2001 From: villa Date: Mon, 12 Sep 1994 08:02:05 +0000 Subject: [PATCH] Aggiunto costruttore TArray(TArray&) che duplica tutto il contenuto git-svn-id: svn://10.65.10.50/trunk@217 c028cbd2-c16b-5b4b-a496-9718f37d4682 --- include/array.cpp | 795 +++++++++++++++++++++++----------------------- include/array.h | 256 +++++++-------- 2 files changed, 530 insertions(+), 521 deletions(-) diff --git a/include/array.cpp b/include/array.cpp index 2fff9de90..6900738a4 100755 --- a/include/array.cpp +++ b/include/array.cpp @@ -1,394 +1,401 @@ -#include -#include - -#include -#include - -void TArray::resize(int arraysize) - -{ - CHECK(arraysize > size(), "Can't reduce array size."); - - TObject** newdata = new TObject* [arraysize]; - - int i = 0; - if (_data != NULL) - for (i = 0; i < size(); i++) newdata[i] = _data[i]; - - while (i < arraysize) newdata[i++] = NULL; - - if (_data != NULL) delete _data; - - _size = arraysize; - _data = newdata; -} - - -bool TArray::destroy(int index, bool pack) -{ - const int old = _items; - - if (index < 0) - { - for (int i = size(); i--;) if (_data[i] != NULL) - { - delete _data[i]; - _data[i] = NULL; - } - _items = 0; - } - else - { - TObject* o = remove(index, pack); - if (o) delete o; - } - - return _items < old; -} - - - -TArray::TArray(int arraysize) : _size(0), _items(0), _data(NULL) - -{ - if (arraysize) resize(arraysize); -} - -TArray::TArray() : _size(0), _items(0), _data(NULL) -{ -} - -TArray::~TArray() - -{ - if (ok()) - { - destroy(); - delete _data; - } -} - - -const char* TArray::class_name() const - -{ - return "Array"; -} - - -word TArray::class_id() const - -{ - return CLASS_ARRAY; -} - - -void TArray::print_on(ostream& out) const - -{ - for (int i = 0; i < size(); i++) - { - out.width(4); - out << i << ' '; - out.width(); - if (_data[i] != NULL) out << *_data[i]; - else out << "(null)"; - out << endl; - } -} - - -bool TArray::ok() const - -{ - return(size() != 0 && (_data != NULL)); -} - - -int TArray::add(TObject* object, int index) -{ - if (index < 0) for (index = 0; index < size() && _data[index]; index++); - if (index >= size()) resize(3*index/2 + 1); - - if (_data[index] != NULL) - { -#ifdef DBG - if (object == _data[index]) - { - error_box("Iu ar traing tu overrait en arrei obgiect: %d", index); - return index; - } -#endif - delete _data[index]; - _items--; - } - - _data[index] = object; - if (object != NULL) _items++; - - return index; -} - - -int TArray::insert(TObject* object, int index) -{ - if (objptr(index)) - { - if (items() == size()) add(NULL); - for (int i = size()-1; i >= index; i--) - _data[i] = _data[i-1]; - _data[index] = NULL; - } - return add(object, index); -} - -int TArray::add(const TObject& object, int index) -{ - TObject* objptr = object.dup(); - return add(objptr, index); -} - -int TArray::insert(const TObject& object, int index) -{ - TObject* objptr = object.dup(); - return insert(objptr, index); -} - -TObject* TArray::remove(int index, bool dopack) -{ - TObject* o = objptr(index); - if (o) - { - _data[index] = NULL; - _items--; - } - if (dopack) pack(); - return o; -} - -void TArray::swap(int i1, int i2) -{ - TObject* o1 = remove(i1, FALSE); - TObject* o2 = remove(i2, FALSE); - if (o1) add(o1, i2); - if (o2) add(o2, i1); -} - -int TArray::last() const -{ - for (int last = size(); --last >= 0; ) - if (_data[last]) break; - return last; -} - - -void TArray::pack() -{ - int next = 0; - for (int i = 0; i < size(); i++) - { - if (_data[i] != NULL) - { - if (next < i) - { - _data[next] = _data[i]; - _data[i] = NULL; - } - next++; - } - } -} - - -HIDDEN int sortable_compare(const TObject** o1, const TObject** o2) -{ - const TSortable* s1 = (const TSortable*)*o1; - const TSortable* s2 = (const TSortable*)*o2; - return s1->compare(*s2); -} - -void TArray::sort(COMPARE_FUNCTION compare) -{ - typedef int (*qsortfunc)(const void*, const void*); - if (compare == NULL) compare = sortable_compare; - - pack(); - qsort(_data, items(), sizeof(TObject*), (qsortfunc)compare); -} - -/////////////////////////////////////////////////////////// -// TBit_array -/////////////////////////////////////////////////////////// - -TBit_array::TBit_array(long size) : _bit(NULL), _size(0) -{ - if (size) resize(index(size)); -} - -void TBit_array::copy(const TBit_array& ba) -{ - if (_bit) - { - delete _bit; - _bit = NULL; - _size = 0; - } - resize(ba._size-1); - memcpy(_bit, ba._bit, _size); -} - -TBit_array::TBit_array(const TBit_array& ba) : _bit(NULL), _size(0) -{ copy(ba); } - -TBit_array& TBit_array::operator=(const TBit_array& ba) -{ - copy(ba); - return *this; -} - - -TBit_array::~TBit_array() -{ - if (_bit) delete _bit; -} - -// Certified 100% -void TBit_array::set() -{ - if (_bit) memset(_bit, 0xFF, _size); -} - -// Certified 100% -void TBit_array::reset() -{ - if (_bit) memset(_bit, 0x0, _size); -} - -// Certified 99% -void TBit_array::resize(word size) -{ - word oldsize = _size; - byte* oldbit = _bit; - - _size = size+1; - _bit = new byte[_size]; - reset(); - - if (oldsize) - { - memcpy(_bit, oldbit, oldsize); - delete oldbit; - } -} - - -bool TBit_array::operator[] (long n) const -{ - const word i = index(n); - if (i >= _size) return FALSE; - return (_bit[i] & mask(n)) != 0; -} - -// Certified 99% -TBit_array& TBit_array::operator|= (const TBit_array& ba) -{ - CHECK(_size >= ba._size, "TBit_array |=: right operand too big"); - - for (word i = 0; i < _size; i++) - _bit[i] |= ba._bit[i]; - - return *this; -} - -// Certified 99% -void TBit_array::set(long n) -{ - const word i = index(n); - if (i >= _size) resize(i); - _bit[i] |= mask(n); -} - -// Certified 99% -void TBit_array::reset(long n) -{ - const word i = index(n); - if (i < _size) - _bit[i] &= ~mask(n); -} - -// Certified 99% -void TBit_array::not(long n) -{ - const word i = index(n); - if (i >= _size) resize(i); - _bit[i] ^= mask(n); -} - -// Certified 50% -long TBit_array::ones() const -{ - long one = 0; - for (word i = 0; i < _size; i++) - { - const byte b = _bit[i]; - if (b) - { - for (byte m = 1; m; m <<= 1) - if (b & m) one++; - } - } - return one; -} - -// Certified 90% -long TBit_array::last_one() const -{ - for (word i = _size; i--;) - { - const byte b = _bit[i]; - if (b) - { - for (byte j = 8; j--;) - if ((1< 0; -} - - -void TBit_array::set(const char* numbers) -{ - TToken_string s(numbers, ' '); - for (const char* n = s.get(0); n; n = s.get()) - if (isdigit(*n)) set(atol(n)); -} - - -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; -} - - +#include +#include +#include +#include + + +void TArray::resize(int arraysize) + +{ + CHECK(arraysize > size(), "Can't reduce array size."); + + TObject** newdata = new TObject* [arraysize]; + + int i = 0; + if (_data != NULL) + for (i = 0; i < size(); i++) newdata[i] = _data[i]; + + while (i < arraysize) newdata[i++] = NULL; + + if (_data != NULL) delete _data; + + _size = arraysize; + _data = newdata; +} + + +bool TArray::destroy(int index, bool pack) +{ + const int old = _items; + + if (index < 0) + { + for (int i = size(); i--;) if (_data[i] != NULL) + { + delete _data[i]; + _data[i] = NULL; + } + _items = 0; + } + else + { + TObject* o = remove(index, pack); + if (o) delete o; + } + + return _items < old; +} + + +TArray::TArray(int arraysize) : _size(0), _items(0), _data(NULL) + +{ + if (arraysize) resize(arraysize); +} + +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() + +{ + if (ok()) + { + destroy(); + delete _data; + } +} + + +const char* TArray::class_name() const + +{ + return "Array"; +} + + +word TArray::class_id() const + +{ + return CLASS_ARRAY; +} + + +void TArray::print_on(ostream& out) const + +{ + for (int i = 0; i < size(); i++) + { + out.width(4); + out << i << ' '; + out.width(); + if (_data[i] != NULL) out << *_data[i]; + else out << "(null)"; + out << endl; + } +} + + +bool TArray::ok() const + +{ + return(size() != 0 && (_data != NULL)); +} + + +int TArray::add(TObject* object, int index) +{ + if (index < 0) for (index = 0; index < size() && _data[index]; index++); + if (index >= size()) resize(3*index/2 + 1); + + if (_data[index] != NULL) + { +#ifdef DBG + if (object == _data[index]) + { + error_box("Iu ar traing tu overrait en arrei obgiect: %d", index); + return index; + } +#endif + delete _data[index]; + _items--; + } + + _data[index] = object; + if (object != NULL) _items++; + + return index; +} + + +int TArray::insert(TObject* object, int index) +{ + if (objptr(index)) + { + if (items() == size()) add(NULL); + for (int i = size()-1; i >= index; i--) + _data[i] = _data[i-1]; + _data[index] = NULL; + } + return add(object, index); +} + +int TArray::add(const TObject& object, int index) +{ + TObject* objptr = object.dup(); + return add(objptr, index); +} + +int TArray::insert(const TObject& object, int index) +{ + TObject* objptr = object.dup(); + return insert(objptr, index); +} + +TObject* TArray::remove(int index, bool dopack) +{ + TObject* o = objptr(index); + if (o) + { + _data[index] = NULL; + _items--; + } + if (dopack) pack(); + return o; +} + +void TArray::swap(int i1, int i2) +{ + TObject* o1 = remove(i1, FALSE); + TObject* o2 = remove(i2, FALSE); + if (o1) add(o1, i2); + if (o2) add(o2, i1); +} + +int TArray::last() const +{ + for (int last = size(); --last >= 0; ) + if (_data[last]) break; + return last; +} + + +void TArray::pack() +{ + int next = 0; + for (int i = 0; i < size(); i++) + { + if (_data[i] != NULL) + { + if (next < i) + { + _data[next] = _data[i]; + _data[i] = NULL; + } + next++; + } + } +} + + +HIDDEN int sortable_compare(const TObject** o1, const TObject** o2) +{ + const TSortable* s1 = (const TSortable*)*o1; + const TSortable* s2 = (const TSortable*)*o2; + return s1->compare(*s2); +} + +void TArray::sort(COMPARE_FUNCTION compare) +{ + typedef int (*qsortfunc)(const void*, const void*); + if (compare == NULL) compare = sortable_compare; + + pack(); + qsort(_data, items(), sizeof(TObject*), (qsortfunc)compare); +} + +/////////////////////////////////////////////////////////// +// TBit_array +/////////////////////////////////////////////////////////// + +TBit_array::TBit_array(long size) : _bit(NULL), _size(0) +{ + if (size) resize(index(size)); +} + +void TBit_array::copy(const TBit_array& ba) +{ + if (_bit) + { + delete _bit; + _bit = NULL; + _size = 0; + } + resize(ba._size-1); + memcpy(_bit, ba._bit, _size); +} + +TBit_array::TBit_array(const TBit_array& ba) : _bit(NULL), _size(0) +{ copy(ba); } + +TBit_array& TBit_array::operator=(const TBit_array& ba) +{ + copy(ba); + return *this; +} + + +TBit_array::~TBit_array() +{ + if (_bit) delete _bit; +} + +// Certified 100% +void TBit_array::set() +{ + if (_bit) memset(_bit, 0xFF, _size); +} + +// Certified 100% +void TBit_array::reset() +{ + if (_bit) memset(_bit, 0x0, _size); +} + +// Certified 99% +void TBit_array::resize(word size) +{ + word oldsize = _size; + byte* oldbit = _bit; + + _size = size+1; + _bit = new byte[_size]; + reset(); + + if (oldsize) + { + memcpy(_bit, oldbit, oldsize); + delete oldbit; + } +} + + +bool TBit_array::operator[] (long n) const +{ + const word i = index(n); + if (i >= _size) return FALSE; + return (_bit[i] & mask(n)) != 0; +} + +// Certified 99% +TBit_array& TBit_array::operator|= (const TBit_array& ba) +{ + CHECK(_size >= ba._size, "TBit_array |=: right operand too big"); + + for (word i = 0; i < _size; i++) + _bit[i] |= ba._bit[i]; + + return *this; +} + +// Certified 99% +void TBit_array::set(long n) +{ + const word i = index(n); + if (i >= _size) resize(i); + _bit[i] |= mask(n); +} + +// Certified 99% +void TBit_array::reset(long n) +{ + const word i = index(n); + if (i < _size) + _bit[i] &= ~mask(n); +} + +// Certified 99% +void TBit_array::not(long n) +{ + const word i = index(n); + if (i >= _size) resize(i); + _bit[i] ^= mask(n); +} + +// Certified 50% +long TBit_array::ones() const +{ + long one = 0; + for (word i = 0; i < _size; i++) + { + const byte b = _bit[i]; + if (b) + { + for (byte m = 1; m; m <<= 1) + if (b & m) one++; + } + } + return one; +} + +// Certified 90% +long TBit_array::last_one() const +{ + for (word i = _size; i--;) + { + const byte b = _bit[i]; + if (b) + { + for (byte j = 8; j--;) + if ((1< 0; +} + + +void TBit_array::set(const char* numbers) +{ + TToken_string s(numbers, ' '); + for (const char* n = s.get(0); n; n = s.get()) + if (isdigit(*n)) set(atol(n)); +} + + +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; +} + + diff --git a/include/array.h b/include/array.h index 64179fdb9..9c1fa09cb 100755 --- a/include/array.h +++ b/include/array.h @@ -1,127 +1,129 @@ -#ifndef __ARRAY_H -#define __ARRAY_H - -#ifndef __OBJECT_H -#include -#endif - -// @M -#ifndef NULL -#define NULL 0L -#endif -// @END - -class TArray; - -typedef (*COMPARE_FUNCTION)(const TObject**, const TObject**); - -class TArray : public TObject -{ - TObject** _data; // Array of pointers to objects - int _size; // Size of the array - int _items; // Number of items in the array - -protected: -// @FPROT - void resize(int newdim); // Estende l'array - -public: -// @FPUB - TArray(int arraysize); // Crea un array (chiama resize) - TArray(); // Crea un array (non chiama resize) - virtual ~TArray() ; - virtual const char* class_name() const ; // Ritorna il nome della classe - 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 items() const { return _items; } // Ritorna numero di oggetti nell'array - int last() const; // Ritorna l'indice dell'ultimo oggetto - - TObject& operator[] (int index) const ; // [] ritorna l'oggetto puntato da 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 int add(TObject* obj, int index = -1) ; // Aggiunge un oggetto ad un array. - 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 insert(const TObject& object, int index = 0); - TObject* remove(int index, bool pack = FALSE); - 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 -{ - return (index < _size && index >= 0) ? _data[index] : NULL; -} - -// @FIN -inline TObject& TArray::operator[] (int index) const -{ - TObject* o = objptr(index); -#ifdef DBG - if (o == NULL) - fatal_box("Can't access NULL array item: %d of %d", index, size()); -#endif - return *o; -} - -class TBit_array; - -class TBit -{ - TBit_array* _a; - long _b; - -public: - TBit(TBit_array* a, word b) : _a(a), _b(b) {} - bool operator =(bool on); - operator bool(); -}; - -class TBit_array : public TObject -{ - word _size; - byte* _bit; - -protected: - virtual bool ok() const; - virtual void print_on(ostream& out) const; - - void resize(word size); - void copy(const TBit_array& ba); - word index(long n) const { return word(n >> 3); } - byte mask(long n) const { return 1 << (n & 0x7); } - -public: - TBit_array(long size = 0); - TBit_array(const TBit_array& ba); - ~TBit_array(); - - TBit_array& operator=(const TBit_array& ba); - bool operator[] (long n) const; - TBit_array& operator |=(const TBit_array& b); -// TBit operator[] (long n) { return TBit(this, n); } - long first_one() const; - long last_one() const; - long ones() const; - - void set(long n); - void reset(long n); - void not(long n); - - void set(long n, bool on) { on ? set(n) : reset(n); } - 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); } - -#endif +#ifndef __ARRAY_H +#define __ARRAY_H + +#ifndef __OBJECT_H +#include +#endif + +// @M +#ifndef NULL +#define NULL 0L +#endif +// @END + +class TArray; + +typedef (*COMPARE_FUNCTION)(const TObject**, const TObject**); + +class TArray : public TObject +{ + TObject** _data; // Array of pointers to objects + int _size; // Size of the array + int _items; // Number of items in the array + +protected: + // @FPROT + void resize(int newdim); // Estende l'array + +public: + // @FPUB + TArray(int arraysize); // Crea un array (chiama resize) + TArray(); // Crea un array (non chiama resize) + TArray(const TArray&); // copia tutto e duplica gli elementi + // (casino se non hanno dup() definita) + virtual ~TArray() ; + virtual const char* class_name() const ; // Ritorna il nome della classe + 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 items() const { return _items; } // Ritorna numero di oggetti nell'array + int last() const; // Ritorna l'indice dell'ultimo oggetto + + TObject& operator[] (int index) const ; // [] ritorna l'oggetto puntato da 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 int add(TObject* obj, int index = -1) ; // Aggiunge un oggetto ad un array. + 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 insert(const TObject& object, int index = 0); + TObject* remove(int index, bool pack = FALSE); + 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 +{ + return (index < _size && index >= 0) ? _data[index] : NULL; +} + +// @FIN +inline TObject& TArray::operator[] (int index) const +{ + TObject* o = objptr(index); +#ifdef DBG + if (o == NULL) + fatal_box("Can't access NULL array item: %d of %d", index, size()); +#endif + return *o; +} + +class TBit_array; + +class TBit +{ + TBit_array* _a; + long _b; + +public: + TBit(TBit_array* a, word b) : _a(a), _b(b) {} + bool operator =(bool on); + operator bool(); +}; + +class TBit_array : public TObject +{ + word _size; + byte* _bit; + +protected: + virtual bool ok() const; + virtual void print_on(ostream& out) const; + + void resize(word size); + void copy(const TBit_array& ba); + word index(long n) const { return word(n >> 3); } + byte mask(long n) const { return 1 << (n & 0x7); } + +public: + TBit_array(long size = 0); + TBit_array(const TBit_array& ba); + ~TBit_array(); + + TBit_array& operator=(const TBit_array& ba); + bool operator[] (long n) const; + TBit_array& operator |=(const TBit_array& b); + // TBit operator[] (long n) { return TBit(this, n); } + long first_one() const; + long last_one() const; + long ones() const; + + void set(long n); + void reset(long n); + void not(long n); + + void set(long n, bool on) { on ? set(n) : reset(n); } + 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); } + +#endif