array.* Aggiunta class TPointer_array : public TArray

Gestisce array di puntatori senza mai farne delete
assoc.*    Portata sizeof(TAssoc_array) da 2278 a 48 sostituendo
           l'array di TArray con un TArray di TArray.
           N.B. 883 e 10883 sono numeri primi.
config.*   Ottimizzata la scrittura dei pragrafi, vengono messi a dirty
           solo quando e' vero e non sempre!
execp.cpp  Tolto controllo dei parametri in quanto gia' effettuato
           dalla nuova funzione TFilename::name()
prefix.cpp Migliorato controllo sull'esistemnza dello studio nella
           TPrefix::set_study


git-svn-id: svn://10.65.10.50/trunk@5756 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
guy 1997-12-17 10:40:18 +00:00
parent cd5309e65b
commit e4e927843f
8 changed files with 196 additions and 95 deletions

View File

@ -111,8 +111,6 @@ TObject* TArray::pred_item( )
return ( _scanindex == -1)?NULL:_data[ _scanindex ]; return ( _scanindex == -1)?NULL:_data[ _scanindex ];
}; };
// @doc EXTERNAL // @doc EXTERNAL
// @mfunc Modifica la dimensione dell'array. // @mfunc Modifica la dimensione dell'array.
@ -548,6 +546,60 @@ void TArray::sort(
qsort(_data, items(), sizeof(TObject*), (qsortfunc)compare); qsort(_data, items(), sizeof(TObject*), (qsortfunc)compare);
} }
///////////////////////////////////////////////////////////
// TPointer_array
///////////////////////////////////////////////////////////
// @doc EXTERNAL
// @mfunc Rimuove uno o tutti (default) gli elementi
// @rdesc Ritorna uno dei seguenti parametri:
//
// @flag TRUE | Se l'operazione e' riuscita con successo
// @flag FALSE | Se il numero di elementi e' rimasto invariato
bool TPointer_array::destroy(
int index, // @parm Numero dell'elemento da eliminare (default -1)
bool pack) // @parm Se true richiama la funzione <mf TArray::pack> per compattare gli elementi dell'array (default FALSE)
// @comm Permette la rimozione di uno (index > 0 ) o di tutti (default) gli
// elementi dell'array assegnandone il valore NULL.
// <nl>Nel caso il parametro pack sia TRUE permette di rendere contigui
// tutti gli elementi diversi da NULL tramite la chiamata alla funzione
// <mf TArray::pack>.
{
if (data())
{
if (index < 0)
memset(data(), 0, size() * sizeof(TObject*));
else
remove(index);
}
return TArray::destroy(index, pack);
}
TPointer_array& TPointer_array::operator= (const TArray& a)
{
destroy();
if (size() < a.size())
resize(a.size());
for (int i = a.size()-1; i >= 0; i--)
add(a[i], i);
return *this;
}
int TPointer_array::add(const TObject& object, int index)
{
return TArray::add(&(TObject&)object, index);
}
int TPointer_array::insert(const TObject& object, int index, bool force)
{
return TArray::insert(&(TObject&)object, index, force);
}
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
// TBit_array // TBit_array
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////

View File

@ -92,7 +92,6 @@ class TArray : public TContainer
// @access:(INTERNAL) Private Member // @access:(INTERNAL) Private Member
{ {
// @cmember:(INTERNAL) Array di puntatori ad oggetti // @cmember:(INTERNAL) Array di puntatori ad oggetti
TObject** _data; TObject** _data;
// @cmember:(INTERNAL) Dimensione dell'array // @cmember:(INTERNAL) Dimensione dell'array
@ -107,7 +106,8 @@ class TArray : public TContainer
// @access Protected Member // @access Protected Member
protected: protected:
TObject** data() const { return _data; }
// @cmember Modifica la dimensione dell'array. // @cmember Modifica la dimensione dell'array.
void resize(int newdim); void resize(int newdim);
@ -176,9 +176,9 @@ public:
virtual int insert(TObject* obj, int index = 0, bool force = FALSE); virtual int insert(TObject* obj, int index = 0, bool force = FALSE);
// @cmember Aggiunge un oggetto all'array. L'oggetto viene duplicato // @cmember Aggiunge un oggetto all'array. L'oggetto viene duplicato
int add(const TObject& object, int index = -1) ; virtual int add(const TObject& object, int index = -1) ;
// @cmember Inserisce un oggetto alla posizione index // @cmember Inserisce un oggetto alla posizione index
int insert(const TObject& object, int index = 0, bool force = FALSE); virtual int insert(const TObject& object, int index = 0, bool force = FALSE);
// @cmember Elimina l'elemento nella posizione index dell'array // @cmember Elimina l'elemento nella posizione index dell'array
TObject* remove(int index, bool pack = FALSE); TObject* remove(int index, bool pack = FALSE);
// @cmember Elimina l'elemento nella posizione index dell'array // @cmember Elimina l'elemento nella posizione index dell'array
@ -206,6 +206,19 @@ inline TObject& TArray::operator[] (int index) const
return *o; return *o;
} }
class TPointer_array : public TArray
{
virtual bool destroy(int index = -1, bool pack = FALSE);
virtual int add(const TObject& object, int index);
virtual int insert(const TObject& object, int index = 0, bool force = FALSE);
virtual TPointer_array& operator= (const TArray& a);
TPointer_array() { }
TPointer_array(int size) : TArray(size) { }
TPointer_array(const TArray& a) { *this = a; }
virtual ~TPointer_array() { }
};
// @doc EXTERNAL // @doc EXTERNAL
// @class TBit_array | Come la classe <c TArray> ma i suoi elementi sono bit; // @class TBit_array | Come la classe <c TArray> ma i suoi elementi sono bit;

View File

@ -1,5 +1,19 @@
#include <assoc.h> #include <assoc.h>
// @ccost:(INTERNAL) HASH_SIZE | 10883 | Dimensione della tabella hash
const int HASH_SIZE = 10883;
TArray& TAssoc_array::bucket(int index)
{
TArray* arr = (TArray*)_bucket.objptr(index);
if (arr == NULL)
{
arr = new TArray;
_bucket.add(arr, index);
}
return *arr;
}
// @doc EXTERNAL // @doc EXTERNAL
// @mfunc Cerca l'oggetto con chiave <p k> // @mfunc Cerca l'oggetto con chiave <p k>
@ -19,7 +33,7 @@ THash_object* TAssoc_array::_lookup(
{ {
const TFixed_string key(k); const TFixed_string key(k);
const word hv = key.hash() % HASH_SIZE; const word hv = key.hash() % HASH_SIZE;
TArray& arr = _data[hv]; TArray& arr = bucket(hv);
THash_object* o = NULL; THash_object* o = NULL;
isnew = FALSE; isnew = FALSE;
@ -48,65 +62,59 @@ THash_object* TAssoc_array::_lookup(
void TAssoc_array::destroy() void TAssoc_array::destroy()
{ {
for (int i = 0; i < HASH_SIZE; i++) _bucket.destroy();
_data[i].destroy(); _cnt = _row = _col = 0;
_cnt = _row = _col = 0; _rowitem = _colitem = 0;
} }
TObject* TAssoc_array::first_item( ) TObject* TAssoc_array::first_item()
{ {
_rowitem = 0; _rowitem = _colitem = 0;
_colitem = 0; return succ_item();
return succ_item( );
} }
TObject* TAssoc_array::last_item( ) TObject* TAssoc_array::last_item()
{ {
_rowitem = HASH_SIZE - 1; _rowitem = _bucket.last();
while( _rowitem >= 0 && _data[ _rowitem ].items() == 0 )
_rowitem --;
if( _rowitem < 0 ) if( _rowitem < 0 )
return NULL; return NULL;
const TArray* arr = &_data[_rowitem]; _colitem = bucket(_rowitem).items() - 1;
_colitem = arr->items( ) - 1;
return pred_item( ); return pred_item( );
} }
TObject* TAssoc_array::succ_item()
TObject* TAssoc_array::succ_item( )
{ {
const TArray* arr = &_data[_rowitem]; const TArray* arr = (const TArray*)_bucket.objptr(_rowitem);
while (_rowitem < HASH_SIZE)
for(;_rowitem < HASH_SIZE;)
{ {
if ((int)_colitem < arr->items()) if (arr && (int)_colitem < arr->items())
break; break;
arr = &_data[++_rowitem]; _rowitem = _bucket.succ(_rowitem);
_colitem = 0; if (_rowitem < HASH_SIZE)
{
arr = (TArray*)_bucket.objptr(_rowitem);
_colitem = 0;
}
} }
if (_rowitem == HASH_SIZE) if (_rowitem >= HASH_SIZE)
return NULL; return NULL;
THash_object* o = (THash_object*)arr->objptr(_colitem++); THash_object* o = (THash_object*)arr->objptr(_colitem++);
return (o == NULL || o->_obj == NULL) ? NULL : o->_obj; return (o == NULL || o->_obj == NULL) ? NULL : o->_obj;
} }
TObject* TAssoc_array::pred_item()
TObject* TAssoc_array::pred_item( )
{ {
const TArray* arr = &_data[_rowitem]; const TArray* arr = (const TArray*)_bucket.objptr(_rowitem);
while (_rowitem >= 0)
for(;_rowitem >= 0;)
{ {
if ((int)_colitem >= 0 ) if (arr && (int)_colitem >= 0)
break; break;
_rowitem --; _rowitem = _bucket.pred(_rowitem);
while( _rowitem >= 0 && _data[ _rowitem ].items( ) == 0 ) if (_rowitem >= 0)
_rowitem --; {
if (_rowitem < 0 ) arr = (TArray*)_bucket.objptr(_rowitem);
return NULL; _colitem = arr->items()-1;
arr = &_data[ _rowitem ]; }
_colitem = arr->items( ) - 1;
} }
if (_rowitem < 0 ) if (_rowitem < 0 )
return NULL; return NULL;
@ -139,9 +147,7 @@ bool TAssoc_array::add(
// definita <mf TObject::dup> // definita <mf TObject::dup>
{ {
bool isnew = FALSE; bool isnew = FALSE;
THash_object* o = _lookup(key,isnew,TRUE); THash_object* o = _lookup(key,isnew,TRUE);
if (!isnew) if (!isnew)
{ {
if (force) if (force)
@ -157,13 +163,13 @@ bool TAssoc_array::add(
} }
bool TAssoc_array::add(const char* key, const TObject& obj, bool force) bool TAssoc_array::add(const char* key, const TObject& obj, bool force)
{ {
// Non inserire l'Hash_object se non lo trovi (ci pensa la add sotto)
bool isnew = FALSE; bool isnew = FALSE;
THash_object* o = _lookup(key,isnew,FALSE); // Non inserire l'Hash_object se non lo trovi (ci pensa la add sotto) THash_object* o = _lookup(key,isnew,FALSE);
if (!isnew && !force) if (!isnew && !force)
return TRUE; return TRUE;
return add(key,obj.dup(),force);
return add(key,obj.dup(),force);
} }
// @doc EXTERNAL // @doc EXTERNAL
@ -181,9 +187,8 @@ bool TAssoc_array::remove(
{ {
const TFixed_string key(k); const TFixed_string key(k);
const word hv = key.hash() % HASH_SIZE; const word hv = key.hash() % HASH_SIZE;
TArray& arr = _data[hv]; TArray& arr = bucket(hv);
THash_object* o = NULL; THash_object* o = NULL;
for (int i = 0; i < arr.items(); i++) for (int i = 0; i < arr.items(); i++)
{ {
THash_object* ob = (THash_object*)&arr[i]; THash_object* ob = (THash_object*)&arr[i];
@ -193,7 +198,11 @@ bool TAssoc_array::remove(
break; break;
} }
if (o != NULL) if (o != NULL)
{ arr.destroy(i,TRUE); _cnt--; return TRUE; } {
arr.destroy(i,TRUE);
_cnt--;
return TRUE;
}
return FALSE; return FALSE;
} }
@ -228,13 +237,10 @@ TObject& TAssoc_array::find(
// @rdesc Se l'oggetto esiste ne ritorna il puntatore, altrimenti ritorna NULL // @rdesc Se l'oggetto esiste ne ritorna il puntatore, altrimenti ritorna NULL
TObject* TAssoc_array::objptr( TObject* TAssoc_array::objptr(
const char* key) const // @parm Chiave dell'oggetto da ritornare const char* key) const // @parm Chiave dell'oggetto da ritornare
{ {
bool isnew = FALSE; bool isnew = FALSE;
THash_object* o = ((TAssoc_array *)this)->_lookup(key,isnew); THash_object* o = ((TAssoc_array*)this)->_lookup(key,isnew);
if (o != NULL) return o ? o->_obj : NULL;
return o->_obj;
return NULL;
} }
// @doc EXTERNAL // @doc EXTERNAL
@ -260,21 +266,22 @@ bool TAssoc_array::is_key(
// @rdesc Ritorna il puntatore all'oggetto (se diverso da NULL), altrimenti // @rdesc Ritorna il puntatore all'oggetto (se diverso da NULL), altrimenti
// ritorna error object // ritorna error object
TObject* TAssoc_array::get() TObject* TAssoc_array::get()
// @xref <mf TAssoc_array::get_hashobj> // @xref <mf TAssoc_array::get_hashobj>
{ {
const TArray* arr = &_data[_row]; const TArray* arr = (const TArray*)_bucket.objptr(_row);
while(_row < HASH_SIZE)
for(;_row < HASH_SIZE;)
{ {
if ((int)_col < arr->items()) if (arr && (int)_col < arr->items())
break; break;
arr = &_data[++_row]; _row = _bucket.succ(_row);
arr = (const TArray*)_bucket.objptr(_row);
_col = 0; _col = 0;
} }
if (_row == HASH_SIZE) if (_row >= HASH_SIZE)
{ _row = 0; return NULL; } {
_row = 0;
return NULL;
}
THash_object* o = (THash_object*)arr->objptr(_col++); THash_object* o = (THash_object*)arr->objptr(_col++);
return (o == NULL || o->_obj == NULL) ? &error : o->_obj; return (o == NULL || o->_obj == NULL) ? &error : o->_obj;
} }
@ -291,18 +298,20 @@ THash_object* TAssoc_array::get_hashobj()
// //
// @xref <mf TAssoc_array::get> // @xref <mf TAssoc_array::get>
{ {
const TArray* arr = &_data[_row]; const TArray* arr = (const TArray*)_bucket.objptr(_row);
while(_row < HASH_SIZE)
for(;_row < HASH_SIZE;)
{ {
if ((int)_col < arr->items()) if (arr && (int)_col < arr->items())
break; break;
arr = &_data[++_row]; _row = _bucket.succ(_row);
arr = (const TArray*)_bucket.objptr(_row);
_col = 0; _col = 0;
} }
if (_row == HASH_SIZE) if (_row >= HASH_SIZE)
{ _row = 0; return NULL; } {
_row = 0;
return NULL;
}
return (THash_object*)arr->objptr(_col++); return (THash_object*)arr->objptr(_col++);
} }
@ -311,7 +320,8 @@ THash_object* TAssoc_array::get_hashobj()
// TString_array passato // TString_array passato
int TAssoc_array::get_keys(TString_array& kl, bool add_values) int TAssoc_array::get_keys(TString_array& kl, bool add_values)
{ {
kl.destroy(); restart(); kl.destroy();
restart();
THash_object* o = NULL; THash_object* o = NULL;
TString tmp(80); TString tmp(80);
while (o = get_hashobj()) while (o = get_hashobj())
@ -335,14 +345,12 @@ int TAssoc_array::get_keys(TString_array& kl, bool add_values)
// //
TAssoc_array & TAssoc_array::copy( TAssoc_array & TAssoc_array::copy(
const TAssoc_array & a) // @parm Array associativo sorgente const TAssoc_array & a) // @parm Array associativo sorgente
{ {
destroy(); destroy();
TAssoc_array& from = (TAssoc_array&)a; TAssoc_array& from = (TAssoc_array&)a;
from.restart(); from.restart();
for (THash_object* obj = from.get_hashobj(); obj; obj = from.get_hashobj()) for (THash_object* obj = from.get_hashobj(); obj; obj = from.get_hashobj())
add(obj->key(), obj->obj(), TRUE); add(obj->key(), obj->obj(), TRUE);
return * this; return * this;
} }

View File

@ -55,8 +55,6 @@ class TAssoc_array : public TContainer
//@access:(INTERNAL) Private Member //@access:(INTERNAL) Private Member
{ {
// @ccost:(INTERNAL) HASH_SIZE | 113 | Dimensione della tabella hash
enum { HASH_SIZE = 113 };
// @cmember:(INTERNAL) Numero di oggetti contenuti nella tabella // @cmember:(INTERNAL) Numero di oggetti contenuti nella tabella
word _cnt; word _cnt;
// @cmember:(INTERNAL) Numero di righe della tabella hash // @cmember:(INTERNAL) Numero di righe della tabella hash
@ -68,11 +66,12 @@ class TAssoc_array : public TContainer
// @cmember:(INTERNAL) Numero di colonne della tabella hash per i metodi _item // @cmember:(INTERNAL) Numero di colonne della tabella hash per i metodi _item
int _colitem; int _colitem;
// @cmember:(INTERNAL) Array contenente i dati veri e propri // @cmember:(INTERNAL) Array contenente i dati veri e propri
TArray _data[HASH_SIZE]; TArray _bucket;
// @access Protected member // @access Protected member
protected: protected:
TArray& bucket(int index);
// @cmember Cerca l'oggetto con chiave k // @cmember Cerca l'oggetto con chiave k
THash_object* _lookup(const char* k, bool& isnew, bool insert = FALSE); THash_object* _lookup(const char* k, bool& isnew, bool insert = FALSE);
// @cmember Copia tutto l'array associativo e ne duplica gli elementi // @cmember Copia tutto l'array associativo e ne duplica gli elementi
@ -137,14 +136,14 @@ public:
// @cmember Mette chiavi e opzionalmente valori (come stringa) nel <c TString_array> passato // @cmember Mette chiavi e opzionalmente valori (come stringa) nel <c TString_array> passato
int get_keys(TString_array& kl, bool add_values = FALSE); int get_keys(TString_array& kl, bool add_values = FALSE);
// @cmember Operatore di assegnamento tra array associativi // @cmember Operatore di assegnamento tra array associativi
TAssoc_array & operator= (const TAssoc_array & a) TAssoc_array& operator= (const TAssoc_array & a)
{ return copy(a); } { return copy(a); }
// @cmember Costruttore // @cmember Costruttore
TAssoc_array() : _cnt(0), _row(0), _col(0) TAssoc_array() : _cnt(0), _row(0), _col(0)
{} {}
// @cmember Costruttore. Copia tutto l'array associativo e ne duplica gli elementi // @cmember Costruttore. Copia tutto l'array associativo e ne duplica gli elementi
TAssoc_array(const TAssoc_array & a) : _cnt(0), _row(0), _col(0) TAssoc_array(const TAssoc_array& a) : _cnt(0), _row(0), _col(0)
{ copy(a); } { copy(a); }
// @cmember Distruttore // @cmember Distruttore
virtual ~TAssoc_array() virtual ~TAssoc_array()
@ -156,5 +155,4 @@ public:
for (THash_object* __obj = __ass.get_hashobj(); \ for (THash_object* __obj = __ass.get_hashobj(); \
__obj && (__str = (const TString&)__obj->obj(), __key = __obj->key()); \ __obj && (__str = (const TString&)__obj->obj(), __key = __obj->key()); \
__obj = __ass.get_hashobj()) __obj = __ass.get_hashobj())
#endif #endif

View File

@ -437,6 +437,7 @@ bool TConfig::set(
if (section && *section) if (section && *section)
set_paragraph(section); set_paragraph(section);
/*
const bool itwas = exist(var, index); const bool itwas = exist(var, index);
if (itwas && !force) if (itwas && !force)
@ -447,6 +448,37 @@ bool TConfig::set(
_data.add(vvar, new TString(value), force); _data.add(vvar, new TString(value), force);
_dirty = TRUE; _dirty = TRUE;
} }
*/
TString256 key(var);
if (index >= 0)
key << '(' << index << ')';
TString* val = (TString*)_data.objptr(key);
const bool itwas = val != NULL;
if (itwas && !force)
error_box("Tentativo di ridefinizione simbolo: %s", (const char*)key);
else
{
if (itwas)
{
const TFixed_string str(value);
// Se la variabile esisteva ed aveva un valore diverso ...
if (*val != str && !(str.blank() && val->empty()))
{
*val = str; // ... allora la sostituisco ...
val->trim();
_dirty = TRUE; // ... e metto a dirty.
}
}
else
{
// Se la variabile non esisteva allora la aggiungo e metto a dirty.
val = new TString(value);
val->trim();
_data.add(key, val, TRUE);
_dirty = TRUE;
}
}
return itwas; return itwas;
} }
@ -547,7 +579,7 @@ int TConfig::list_variables(TString_array& vl, bool value, const char* section)
return vl.items(); return vl.items();
} }
const TAssoc_array& TConfig::list_variables(const char* section) TAssoc_array& TConfig::list_variables(const char* section)
{ {
set_paragraph(section); set_paragraph(section);
return _data; return _data;

View File

@ -140,7 +140,7 @@ public:
// @cmember Ritorna l'intero array delle variabili della sezione // @cmember Ritorna l'intero array delle variabili della sezione
// eventualmente specificata da <p section> // eventualmente specificata da <p section>
const TAssoc_array& list_variables(const char* section = NULL); TAssoc_array& list_variables(const char* section = NULL);
// @cmember Ritorna il nome del file di configurazione // @cmember Ritorna il nome del file di configurazione
const TFilename& name() const { return _file; } const TFilename& name() const { return _file; }

View File

@ -65,11 +65,7 @@ word TExternal_app::run(
{ {
TFilename path(_path); TFilename path(_path);
TString name(path.name()); const TString name(path.name());
int p = name.find(' ');
if (p >=0)
name = name.left(p);
if (utente == TRUE) // utente puo' essere 0 = No, 1 = Si, 3 = Forzatura if (utente == TRUE) // utente puo' essere 0 = No, 1 = Si, 3 = Forzatura
{ {

View File

@ -351,8 +351,10 @@ const char* TPrefix::get_studio() const
} }
bool TPrefix::set_studio(const char* study, long ditta) bool TPrefix::set_studio(const char* study, long ditta)
{ {
if (!fexist(study)) TFilename dirtest(study);
dirtest.add("com/dir.gen");
if (!fexist(dirtest))
return FALSE; return FALSE;
const TString old_study(__ptprf); const TString old_study(__ptprf);