2004-03-31 14:40:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
2016-06-13 07:29:25 +02:00
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
2004-03-31 14:40:42 +02:00
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
* for more details. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2005-07-07 15:33:52 +02:00
|
|
|
|
2004-04-01 00:36:44 +02:00
|
|
|
|
2004-03-31 14:40:42 +02:00
|
|
|
#ifndef __VCGLIB_CAT__
|
|
|
|
#define __VCGLIB_CAT__
|
|
|
|
|
|
|
|
#include <vector>
|
2010-06-21 17:52:18 +02:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2004-03-31 14:40:42 +02:00
|
|
|
#include <list>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <assert.h>
|
2004-04-01 00:33:38 +02:00
|
|
|
#include <vcg/container/entries_allocation_table.h>
|
2004-03-31 14:40:42 +02:00
|
|
|
|
|
|
|
namespace vcg {
|
2005-07-07 15:33:52 +02:00
|
|
|
/*@{*/
|
2010-06-21 17:52:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-07-07 15:33:52 +02:00
|
|
|
/*!
|
2008-03-17 12:25:27 +01:00
|
|
|
* CATBase is the abstract class for all the allocation tables. These table keep track of
|
|
|
|
* where the traced vector (see traced_ector.h) are kept in memory.
|
2005-07-07 15:33:52 +02:00
|
|
|
* The goal is to know (given a pointer to a memory location), which is the vector the pointed
|
|
|
|
* element is in
|
|
|
|
*
|
|
|
|
*/
|
2004-03-31 14:40:42 +02:00
|
|
|
|
2010-06-21 17:52:18 +02:00
|
|
|
struct CATBaseBase{
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT>
|
2010-06-21 17:52:18 +02:00
|
|
|
class CATBase: public CATBaseBase{
|
2004-03-31 14:40:42 +02:00
|
|
|
public:
|
2008-03-17 12:25:27 +01:00
|
|
|
typedef typename STL_CONT::value_type ValueType;
|
2004-03-31 14:40:42 +02:00
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
virtual void Resort(ValueType*,ValueType*) =0;
|
2004-03-31 14:40:42 +02:00
|
|
|
virtual void Remove(const STL_CONT&) = 0;
|
2004-04-05 20:20:50 +02:00
|
|
|
virtual void AddDataElem(ValueType*,int)=0;
|
2005-10-15 18:21:49 +02:00
|
|
|
virtual void Resize(ValueType*,int)=0;
|
2004-03-31 14:40:42 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
// ID serves as a type trait.
|
|
|
|
static int & Id(){
|
|
|
|
static int id=0;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-07-07 15:33:52 +02:00
|
|
|
/// CATEntry: first derivation templated on the type of entry
|
2006-12-03 19:01:01 +01:00
|
|
|
/// It implements all the methods to trace and access vector_occ's elements
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
class CATEntry: public CATBase<STL_CONT>{
|
|
|
|
public:
|
2008-03-17 12:25:27 +01:00
|
|
|
typedef typename STL_CONT::value_type ValueType;
|
|
|
|
typedef ENTRY_TYPE EntryType;
|
2004-03-31 14:40:42 +02:00
|
|
|
|
|
|
|
CATEntry(){if(Id()==0){
|
|
|
|
Id() = CATBase<STL_CONT>::Id()+1;
|
|
|
|
CATBase<STL_CONT>::Id() = Id();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-08 22:28:57 +02:00
|
|
|
static unsigned int Ord(const ValueType *);
|
2005-07-06 17:28:11 +02:00
|
|
|
static ENTRY_TYPE & GetEntry(typename STL_CONT::value_type*pt);
|
2004-03-31 14:40:42 +02:00
|
|
|
|
2004-04-01 00:33:38 +02:00
|
|
|
static void Insert( STL_CONT & c,bool cond=false ); // insert a vector to trace
|
|
|
|
virtual void Remove( const STL_CONT & c); // remove the container c
|
2005-07-06 17:28:11 +02:00
|
|
|
static void RemoveIfEmpty( const STL_CONT & c); // remove the container c
|
|
|
|
static void Remove( ValueType * v); // remove the container that contains v
|
2004-03-31 14:40:42 +02:00
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
virtual void Resort( ValueType* old_start, // resort the allocation table
|
|
|
|
ValueType* new_start); // after a container was moved
|
2004-03-31 14:40:42 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
static std::list<ENTRY_TYPE >& AT(){ // tallocation table
|
|
|
|
static std::list<ENTRY_TYPE > allocation_table;
|
|
|
|
return allocation_table;
|
|
|
|
}
|
|
|
|
static bool & UTD(){
|
|
|
|
static bool upToDate; // true if Lower() and Upper()
|
|
|
|
return upToDate; // are up to date
|
|
|
|
}
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
static ValueType *& Lower() {
|
|
|
|
static ValueType * lower; // pointer to the first element
|
2004-03-31 14:40:42 +02:00
|
|
|
return lower; // of the last container accessed
|
|
|
|
}
|
2004-04-05 20:20:50 +02:00
|
|
|
static ValueType *& Upper() {
|
|
|
|
static ValueType * upper; // pointer to the first element
|
2004-03-31 14:40:42 +02:00
|
|
|
return upper; // if the container next to the last accessed
|
|
|
|
}
|
|
|
|
|
2005-07-06 17:28:11 +02:00
|
|
|
static typename std::list<ENTRY_TYPE>::iterator & Curr(){ // container that was last accessed
|
2008-03-17 12:25:27 +01:00
|
|
|
static typename std::list<ENTRY_TYPE>::iterator currEntry;
|
2004-03-31 14:40:42 +02:00
|
|
|
return currEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-08 22:28:57 +02:00
|
|
|
static bool IsTheSameAsLast(const ValueType *pt); // true if pt is in the container
|
2004-03-31 14:40:42 +02:00
|
|
|
// that was accessed last
|
2006-06-08 22:28:57 +02:00
|
|
|
static void Update(const ValueType*); // set Upper() e Lower()
|
2005-07-06 17:28:11 +02:00
|
|
|
static typename std::list<ENTRY_TYPE>::iterator FindBase(const ValueType * pt);
|
2004-03-31 14:40:42 +02:00
|
|
|
// find the container that contains pt (naive)
|
2005-07-06 17:28:11 +02:00
|
|
|
virtual void AddDataElem(typename STL_CONT::value_type * pt,int n);// add n element to the auxiliary data
|
2005-10-15 18:21:49 +02:00
|
|
|
virtual void Resize(typename STL_CONT::value_type * pt,int n);// resize the auxiliary data
|
2004-03-31 14:40:42 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
static int & Id(){ // unique identifier of the istance
|
|
|
|
static int id=0; // (used as type trait)
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------- CATEntry: implementation --------------------
|
2005-10-15 18:21:49 +02:00
|
|
|
// derivazione fatta per i membri Occ (Optional Component Compact)
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
unsigned int CATEntry<STL_CONT,ENTRY_TYPE>::
|
|
|
|
|
2006-06-08 22:28:57 +02:00
|
|
|
Ord(const ValueType * pt)
|
2004-03-31 14:40:42 +02:00
|
|
|
{
|
|
|
|
Update(pt);
|
|
|
|
return (pt-Lower());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2005-07-06 17:28:11 +02:00
|
|
|
typename std::list<ENTRY_TYPE>::iterator CATEntry<STL_CONT,ENTRY_TYPE>::
|
2004-03-31 14:40:42 +02:00
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
FindBase(const ValueType * pt)
|
2004-03-31 14:40:42 +02:00
|
|
|
{
|
2008-03-17 12:25:27 +01:00
|
|
|
typename std::list<ENTRY_TYPE>::iterator ite,curr_base,_;
|
2004-04-01 00:33:38 +02:00
|
|
|
ite = AT().begin();
|
|
|
|
curr_base = AT().end();
|
2004-03-31 14:40:42 +02:00
|
|
|
|
|
|
|
for(;ite != AT().end();ite++)
|
|
|
|
if( pt < (*ite).Start())
|
|
|
|
return curr_base;
|
|
|
|
else
|
|
|
|
curr_base = ite;
|
|
|
|
|
|
|
|
return curr_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
bool CATEntry< STL_CONT, ENTRY_TYPE>::
|
|
|
|
|
2006-06-08 22:28:57 +02:00
|
|
|
IsTheSameAsLast(const ValueType * pt)
|
2004-03-31 14:40:42 +02:00
|
|
|
{
|
|
|
|
return ( UTD() && ( !(Lower()> pt)) && (pt < Upper()) );
|
|
|
|
}
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
void CATEntry< STL_CONT, ENTRY_TYPE>::
|
|
|
|
|
2006-06-08 22:28:57 +02:00
|
|
|
Update(const ValueType * pt)
|
2004-03-31 14:40:42 +02:00
|
|
|
{
|
|
|
|
if(!IsTheSameAsLast(pt)){
|
2008-03-17 12:25:27 +01:00
|
|
|
typename std::list<ENTRY_TYPE>::iterator lower_ite,upper_ite;
|
2004-03-31 14:40:42 +02:00
|
|
|
lower_ite = FindBase(pt);
|
|
|
|
|
|
|
|
assert( lower_ite!=AT().end());
|
|
|
|
|
|
|
|
Lower() = (*lower_ite).Start();
|
|
|
|
if( (*lower_ite).Start() == AT().back().Start())
|
2004-04-05 20:20:50 +02:00
|
|
|
Upper() = (ValueType *) 0xffffffff;
|
2004-03-31 14:40:42 +02:00
|
|
|
else
|
|
|
|
{
|
2005-10-15 18:21:49 +02:00
|
|
|
upper_ite = lower_ite; ++upper_ite;
|
|
|
|
Upper() = (*upper_ite).Start();
|
2004-03-31 14:40:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Curr() = lower_ite;
|
|
|
|
UTD() = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
void CATEntry< STL_CONT, ENTRY_TYPE>::
|
2004-04-05 20:20:50 +02:00
|
|
|
Resort(ValueType* old_start,ValueType* new_start)
|
2004-03-31 14:40:42 +02:00
|
|
|
{
|
|
|
|
AT().sort();
|
|
|
|
UTD() = false;
|
|
|
|
}
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
|
|
|
|
|
|
|
Remove( const STL_CONT & c )
|
|
|
|
{
|
2008-03-17 12:25:27 +01:00
|
|
|
typename std::list<ENTRY_TYPE>::iterator ite;
|
2004-03-31 14:40:42 +02:00
|
|
|
for(ite = AT().begin(); ite != AT().end(); ++ite)
|
2004-04-01 00:33:38 +02:00
|
|
|
if((*ite).C() == &c)
|
|
|
|
{
|
|
|
|
AT().erase(ite);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
UTD() = false;
|
|
|
|
}
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-04-01 00:33:38 +02:00
|
|
|
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
|
|
|
|
|
|
|
RemoveIfEmpty( const STL_CONT & c )
|
|
|
|
{
|
2008-03-17 12:25:27 +01:00
|
|
|
typename std::list<ENTRY_TYPE>::iterator ite;
|
2004-04-01 00:33:38 +02:00
|
|
|
for(ite = AT().begin(); ite != AT().end(); ++ite)
|
|
|
|
if((*ite).C() == &c)
|
|
|
|
if(!(*ite).Empty())
|
|
|
|
AT().erase(ite);
|
2004-03-31 14:40:42 +02:00
|
|
|
UTD() = false;
|
|
|
|
}
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
Remove(ValueType * pt)
|
2004-03-31 14:40:42 +02:00
|
|
|
{
|
2008-03-17 12:25:27 +01:00
|
|
|
typename std::list<ENTRY_TYPE>::iterator lower_ite;
|
2004-03-31 14:40:42 +02:00
|
|
|
lower_ite = FindBase(pt);
|
|
|
|
AT().erase(lower_ite);
|
|
|
|
UTD() = false;
|
2004-04-01 00:33:38 +02:00
|
|
|
|
2004-03-31 14:40:42 +02:00
|
|
|
}
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
|
|
|
|
2004-04-01 00:33:38 +02:00
|
|
|
Insert( STL_CONT & c,bool cond )
|
2004-03-31 14:40:42 +02:00
|
|
|
{
|
|
|
|
ENTRY_TYPE entry(c);
|
2008-03-17 12:25:27 +01:00
|
|
|
typename std::list<ENTRY_TYPE>::iterator lower_ite,upper_ite;
|
2006-12-03 19:01:01 +01:00
|
|
|
upper_ite = FindBase( c.Pointer2begin());
|
2004-04-01 00:33:38 +02:00
|
|
|
bool isIn = (upper_ite != AT().end());
|
|
|
|
if(isIn){
|
|
|
|
if((*upper_ite).C() != &c )
|
|
|
|
++upper_ite;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2004-03-31 14:40:42 +02:00
|
|
|
lower_ite = AT().insert(upper_ite,entry);
|
|
|
|
lower_ite->Reserve(c.capacity());
|
|
|
|
lower_ite->Resize(c.size());
|
|
|
|
UTD() = false;
|
|
|
|
}
|
2004-04-01 00:33:38 +02:00
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
ENTRY_TYPE & CATEntry<STL_CONT, ENTRY_TYPE>::
|
2005-07-06 17:28:11 +02:00
|
|
|
GetEntry(typename STL_CONT::value_type*pt){
|
2004-03-31 14:40:42 +02:00
|
|
|
Update(pt);
|
|
|
|
return *Curr();
|
|
|
|
}
|
|
|
|
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
|
|
|
|
2005-07-06 17:28:11 +02:00
|
|
|
AddDataElem(typename STL_CONT::value_type * pt,int n)
|
2004-03-31 14:40:42 +02:00
|
|
|
{
|
|
|
|
Update(pt);
|
|
|
|
Curr()->Push_back(n);
|
|
|
|
}
|
|
|
|
|
2005-10-15 18:21:49 +02:00
|
|
|
|
|
|
|
template <typename STL_CONT, class ENTRY_TYPE>
|
|
|
|
void CATEntry<STL_CONT, ENTRY_TYPE>::
|
|
|
|
|
|
|
|
Resize(typename STL_CONT::value_type * pt,int n)
|
|
|
|
{
|
|
|
|
Update(pt);
|
|
|
|
Curr()->Resize(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-31 14:40:42 +02:00
|
|
|
//--------------------------------------------------------------------------------------------
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT,class ATTR_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
class CAT:public CATEntry<STL_CONT, EntryCAT<STL_CONT,ATTR_TYPE> >{
|
2008-03-17 12:25:27 +01:00
|
|
|
typedef typename STL_CONT::value_type ValueType;
|
|
|
|
typedef CATEntry<STL_CONT, EntryCAT<STL_CONT,ATTR_TYPE> > TT;
|
2004-03-31 14:40:42 +02:00
|
|
|
public:
|
2006-06-08 22:28:57 +02:00
|
|
|
static ATTR_TYPE & Get(const ValueType * pt);
|
2005-10-15 18:21:49 +02:00
|
|
|
static CAT<STL_CONT,ATTR_TYPE> * New();
|
|
|
|
static CAT<STL_CONT,ATTR_TYPE> *& Instance(){ static CAT<STL_CONT,ATTR_TYPE> * instance=NULL; return instance;}
|
2004-03-31 14:40:42 +02:00
|
|
|
};
|
|
|
|
//---------------------- CAT: implementation---------------------------------------------------
|
2004-04-05 20:20:50 +02:00
|
|
|
template <typename STL_CONT, class ATTR_TYPE>
|
2004-03-31 14:40:42 +02:00
|
|
|
ATTR_TYPE & CAT<STL_CONT,ATTR_TYPE>::
|
|
|
|
|
2006-06-08 22:28:57 +02:00
|
|
|
Get(const ValueType * pt)
|
2004-03-31 14:40:42 +02:00
|
|
|
{
|
|
|
|
int ord = Ord(pt);
|
2008-03-17 12:25:27 +01:00
|
|
|
return TT::Curr()->Data()[ord];
|
2004-03-31 14:40:42 +02:00
|
|
|
}
|
2005-10-15 18:21:49 +02:00
|
|
|
|
2010-06-21 17:52:18 +02:00
|
|
|
struct Env{
|
|
|
|
|
|
|
|
static std::map< std::string,CATBaseBase *> & TypeNameBounds(){
|
|
|
|
static std::map< std::string,CATBaseBase *> ntb; return ntb;}
|
|
|
|
|
|
|
|
template <class TYPE_1,class TYPE_2>
|
|
|
|
static CAT<TYPE_1,TYPE_2> * newCAT(){
|
|
|
|
std::string n = std::string(typeid(TYPE_1).name())+std::string(typeid(TYPE_2).name());
|
|
|
|
std::map< std::string,CATBaseBase *>::iterator ti = TypeNameBounds().find(n);
|
|
|
|
if(ti == TypeNameBounds().end()){
|
|
|
|
CAT<TYPE_1,TYPE_2> * res = new CAT<TYPE_1,TYPE_2>();
|
|
|
|
TypeNameBounds().insert(std::pair<std::string,CATBaseBase*>(n,res));
|
|
|
|
return res;
|
|
|
|
}
|
2011-10-25 12:03:10 +02:00
|
|
|
else
|
|
|
|
return (CAT<TYPE_1,TYPE_2> *) (*ti).second;
|
2010-06-21 17:52:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-10-15 18:21:49 +02:00
|
|
|
template <typename STL_CONT, class ATTR_TYPE>
|
|
|
|
CAT<STL_CONT,ATTR_TYPE> * CAT<STL_CONT,ATTR_TYPE>::
|
|
|
|
New(){
|
|
|
|
if(Instance()==NULL)
|
|
|
|
{
|
2010-06-21 17:52:18 +02:00
|
|
|
// Instance() = new CAT<STL_CONT,ATTR_TYPE>();
|
|
|
|
Instance() = Env::newCAT<STL_CONT,ATTR_TYPE>();
|
2005-10-15 18:21:49 +02:00
|
|
|
}
|
|
|
|
return Instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-31 14:40:42 +02:00
|
|
|
};//end namespace vcg
|
|
|
|
|
2008-03-17 12:25:27 +01:00
|
|
|
#endif
|