diff --git a/vcg/container_allocation_table.h b/vcg/container_allocation_table.h index ee81743c..577cceea 100644 --- a/vcg/container_allocation_table.h +++ b/vcg/container_allocation_table.h @@ -42,11 +42,11 @@ typedef STL_CONT::value_type VALUE_TYPE; virtual void Resort(VALUE_TYPE*,VALUE_TYPE*) =0; virtual void Remove(const STL_CONT&) = 0; virtual void AddDataElem(VALUE_TYPE*,int)=0; -virtual void Reserve(STL_CONT::value_type * pt,const int & rs)=0; -virtual void Resize(STL_CONT::value_type * pt,const int & rs)=0; +//virtual void Reserve(STL_CONT::value_type * pt,const int & rs)=0; +//virtual void Resize(STL_CONT::value_type * pt,const int & rs)=0; public: -// ID serve as a type trait. +// ID serves as a type trait. static int & Id(){ static int id=0; return id; @@ -59,6 +59,7 @@ template class CATEntry: public CATBase{ public: typedef STL_CONT::value_type VALUE_TYPE; +typedef ENTRY_TYPE EntryType; CATEntry(){if(Id()==0){ Id() = CATBase::Id()+1; @@ -68,8 +69,11 @@ CATEntry(){if(Id()==0){ static unsigned int Ord(VALUE_TYPE *); +static ENTRY_TYPE & GetEntry(STL_CONT::value_type*pt); + +static void Insert( STL_CONT & c ); // insert a vector to trace virtual void Remove( const STL_CONT & c); // remove the container c -static void Remove( VALUE_TYPE * v); // remove the container that contains v +static void Remove( VALUE_TYPE * v); // remove the container that contains v virtual void Resort( VALUE_TYPE* old_start, // resort the allocation table VALUE_TYPE* new_start); // after a container was moved @@ -105,6 +109,7 @@ static bool IsTheSameAsLast(VALUE_TYPE *pt); // true if pt is in the container static void Update(VALUE_TYPE*); // set Upper() e Lower() static std::list::iterator FindBase(const VALUE_TYPE * pt); // find the container that contains pt (naive) +virtual void AddDataElem(STL_CONT::value_type * pt,int n);// add n element to the auxiliary data public: static int & Id(){ // unique identifier of the istance @@ -191,8 +196,12 @@ void CATEntry:: Remove( const STL_CONT & c ) { std::list::iterator ite; -for(ite = AT().begin(); (*ite).C() != &c;++ite) - AT().erase(ite); +for(ite = AT().begin(); ite != AT().end(); ++ite) +if((*ite).C() == &c) + { + AT().erase(ite); + break; + } UTD() = false; } @@ -207,35 +216,46 @@ Remove(VALUE_TYPE * pt) UTD() = false; } +template +void CATEntry:: +Insert( STL_CONT & c ) +{ +ENTRY_TYPE entry(c); +std::list::iterator lower_ite,upper_ite; +upper_ite = std::lower_bound(AT().begin(), AT().end(), entry); +lower_ite = AT().insert(upper_ite,entry); +lower_ite->Reserve(c.capacity()); +lower_ite->Resize(c.size()); +UTD() = false; +} +template +ENTRY_TYPE & CATEntry:: +GetEntry(STL_CONT::value_type*pt){ +Update(pt); +return *Curr(); +} + +template +void CATEntry:: + +AddDataElem(STL_CONT::value_type * pt,int n) +{ +Update(pt); +Curr()->Push_back(n); +} + +//-------------------------------------------------------------------------------------------- // CAT: derivation of CATEntry for the case where the temporary data is unique for each type. // VERY IMPORTANT: there cannot be two vector of value with the same type of temporary datya // This class is used to implement optional core data (NormalOpt, CoordOpt etc...) -// For generic user-defined data see CATMulti template class CAT:public CATEntry >{ public: typedef typename EntryCAT EntryType; static ATTR_TYPE & Get(STL_CONT::value_type * pt); -static std::vector & Data(STL_CONT::value_type * pt); -static void Insert( STL_CONT & c); -virtual void Reserve(STL_CONT::value_type * pt,const int & rs); -virtual void Resize(STL_CONT::value_type * pt,const int & rs); -virtual void AddDataElem(STL_CONT::value_type * pt,int n);// add n element to the auxiliary data - }; - - +}; //---------------------- CAT: implementation--------------------------------------------------- -template -void CAT:: - -AddDataElem(STL_CONT::value_type * pt,int n) -{ -Update(pt); -for(int i = 0 ; i < n; ++i) - Curr()->push_back(); -} - template ATTR_TYPE & CAT:: @@ -244,73 +264,7 @@ Get(STL_CONT::value_type * pt) int ord = Ord(pt); return Curr()->Data()[ord]; } - -template -std::vector & CAT:: -Data(STL_CONT::value_type * pt) -{ -Update(pt); -return Curr()->Data(); -} - -template -void CAT:: - -Insert( STL_CONT & c ) -{ -EntryType entry(c); -std::list::iterator lower_ite,upper_ite; -upper_ite = std::lower_bound(AT().begin(), AT().end(), entry); -lower_ite = AT().insert(upper_ite,entry); -lower_ite->Data().reserve(c.capacity()); -lower_ite->Data().resize(c.size()); -UTD() = false; -} - -template -void CAT:: -Reserve(STL_CONT::value_type * pt,const int & rs) -{ -Update(pt); -Curr()->Data().reserve(rs); -} - -template -void CAT:: -Resize(STL_CONT::value_type * pt,const int & rs) -{ -Update(pt); -Curr()->Data().resize(rs); -} - -template -class CATMulti:public CATEntry{ -public: -typedef typename ENTRY_TYPE EntryType; -static void Insert( STL_CONT & c); -static ENTRY_TYPE & GetEntry(STL_CONT::value_type *); -virtual void Reserve(const int & rs){}; -virtual void Resize(const int & rs){}; -}; - -// --------------------------- CATMulti: implementation ------------------ -template -void CATMulti:: -Insert( STL_CONT & c ) -{ -ENTRY_TYPE entry(c); -std::list::iterator lower_ite,upper_ite; -upper_ite = std::lower_bound(AT().begin(), AT().end(), entry); -lower_ite = AT().insert(upper_ite,entry); -UTD() = false; -} - -template -ENTRY_TYPE & CATMulti:: -GetEntry(STL_CONT::value_type*pt){ -Update(pt); -return *Curr(); -} +//--------------------------------------------------------------------------------------------- };//end namespace vcg diff --git a/vcg/entries_allocation_table.h b/vcg/entries_allocation_table.h index 37f5da3b..e19e4883 100644 --- a/vcg/entries_allocation_table.h +++ b/vcg/entries_allocation_table.h @@ -35,7 +35,11 @@ struct EntryCATBase{ EntryCATBase(STL_CONT & _c):c(_c){}; STL_CONT::value_type * Start() const; const STL_CONT * C(); -virtual void push_back(){} +virtual void Push_back(const int &){}; + +virtual void Reserve(const int & s){}; +virtual void Resize(const int & s){}; + const bool operator < (const EntryCATBase & other) const; private: @@ -48,7 +52,11 @@ struct EntryCAT: public EntryCATBase{ typedef ATTR_TYPE attr_type; EntryCAT(STL_CONT & _c) : EntryCATBase(_c){}; std::vector & Data(){return data;} -void push_back(){ data.push_back(ATTR_TYPE());} +void Push_back(const int & n){ for(int i = 0; i < n ; ++i) data.push_back(ATTR_TYPE());} +virtual void Reserve(const int & s){data.reserve(s);}; +virtual void Resize(const int & s){data.resize(s);}; + + private: std::vector data; }; @@ -70,16 +78,53 @@ template } -// EntryCATMulti: entry type for generic data (matches with CATMulti) + + +struct WrapBase{ +virtual void Push_back(const int & n)=0; +virtual void Reserve(const int & n)=0; +virtual void Resize(const int & n)=0; + }; + +// (note) double hineritance is not necessary, just handy +template +struct Wrap: public WrapBase,std::vector{ + virtual void Push_back(const int & n){for (int i = 0 ; i < n; ++i) push_back( ATTR_TYPE());} + virtual void Reserve(const int & n){reserve(n);} + virtual void Resize(const int & n){resize(n);} + }; + + + +// EntryCATMulti: entry type for multiple user data template struct EntryCATMulti: public EntryCATBase{ + EntryCATMulti(STL_CONT & _c) : EntryCATBase(_c){}; -std::list< void * > & Data(){return data;} -void push_back(){} +std::list & Data(){return data;} +void push_back(const int & n ){ + std::list< void * >::iterator ite; + for(ite = data.begin(); ite != data.end(); ++ite) + (*ite)->Push_back(n); + } +virtual void Reserve(const int & n){ + std::list::iterator ite; + for(ite = data.begin(); ite != data.end(); ++ite) + (*ite)->Reserve(n); + }; +virtual void Resize(const int & n){ + std::list::iterator ite; + for(ite = data.begin(); ite != data.end(); ++ite) + (*ite)->Resize(n); + }; + private: - std::list< void * > data; + std::list< WrapBase * > data; }; + + + template class TempData{ public: @@ -90,7 +135,7 @@ public: std::vector * item; ATTR_TYPE & operator [](STL_CONT::value_type * v) { - int pos = CATMulti >::Ord(v); + int pos = CATEntry >::Ord(v); return (*item)[pos]; } }; diff --git a/vcg/traced_vector.h b/vcg/traced_vector.h index 3dc616e5..99e0148d 100644 --- a/vcg/traced_vector.h +++ b/vcg/traced_vector.h @@ -70,24 +70,24 @@ public: template TempData NewTempData(){ //CAT::Insert(*this) - CATMulti >::EntryType - entry = CATMulti >::GetEntry(&*begin()); - entry.Data().push_back(new std::vector); + CATEntry >::EntryType + entry = CATEntry >::GetEntry(&*begin()); + entry.Data().push_back(new Wrap< ATTR_TYPE>); - ((std::vector*)entry.Data().back())->reserve(capacity()); - ((std::vector*)entry.Data().back())->resize(size()); + ((Wrap*)entry.Data().back())->reserve(capacity()); + ((Wrap*)entry.Data().back())->resize(size()); - return TempData((std::vector*) entry.Data().back()); + return TempData((Wrap*) entry.Data().back()); } template void DeleteTempData(TempData & td){ //CAT::Insert(*this) - CATMulti >::EntryType - entry = CATMulti >::GetEntry(&*begin()); + CATEntry >::EntryType + entry = CATEntry >::GetEntry(&*begin()); - entry.Data().remove(td.Item()); - delete td.Item(); + entry.Data().remove((Wrap*)td.Item()); + delete ((Wrap*)td.Item()); }