Data structure to handle temporary attributes. First version

This commit is contained in:
ganovelli 2004-03-31 12:40:42 +00:00
parent 851c64717e
commit 48d93d6339
4 changed files with 635 additions and 0 deletions

View File

@ -0,0 +1,269 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* 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. *
* *
****************************************************************************/
#ifndef __VCGLIB_CAT__
#define __VCGLIB_CAT__
#include <vector>
#include <list>
#include <algorithm>
#include <assert.h>
#include <vcg/entries_allocation_table.h>
namespace vcg {
// CATBase: abstract base class for all the allocation tables
template <class STL_CONT>
class CATBase{
public:
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;
public:
// ID serves as a type trait.
static int & Id(){
static int id=0;
return id;
}
};
// CATEntry: first derivation templated on the type of entry
// It implements all the methods to trace and access TVector element
template <class STL_CONT, class ENTRY_TYPE>
class CATEntry: public CATBase<STL_CONT>{
public:
typedef STL_CONT::value_type VALUE_TYPE;
typedef ENTRY_TYPE EntryType;
CATEntry(){if(Id()==0){
Id() = CATBase<STL_CONT>::Id()+1;
CATBase<STL_CONT>::Id() = Id();
}
}
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
virtual void Resort( VALUE_TYPE* old_start, // resort the allocation table
VALUE_TYPE* new_start); // after a container was moved
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
}
static VALUE_TYPE *& Lower() {
static VALUE_TYPE * lower; // pointer to the first element
return lower; // of the last container accessed
}
static VALUE_TYPE *& Upper() {
static VALUE_TYPE * upper; // pointer to the first element
return upper; // if the container next to the last accessed
}
static std::list<ENTRY_TYPE>::iterator & Curr(){ // container that was last accessed
static std::list<ENTRY_TYPE>::iterator currEntry;
return currEntry;
}
static bool IsTheSameAsLast(VALUE_TYPE *pt); // true if pt is in the container
// that was accessed last
static void Update(VALUE_TYPE*); // set Upper() e Lower()
static std::list<ENTRY_TYPE>::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
static int id=0; // (used as type trait)
return id;
}
};
// --------------------------- CATEntry: implementation --------------------
template <class STL_CONT, class ENTRY_TYPE>
unsigned int CATEntry<STL_CONT,ENTRY_TYPE>::
Ord(VALUE_TYPE * pt)
{
Update(pt);
return (pt-Lower());
}
template <class STL_CONT, class ENTRY_TYPE>
std::list<ENTRY_TYPE>::iterator CATEntry<STL_CONT,ENTRY_TYPE>::
FindBase(const VALUE_TYPE * pt)
{
std::list<ENTRY_TYPE>::iterator
ite = AT().begin(),curr_base;
std::list<ENTRY_TYPE> tmp = AT();
for(;ite != AT().end();ite++)
if( pt < (*ite).Start())
return curr_base;
else
curr_base = ite;
return curr_base;
}
template <class STL_CONT, class ENTRY_TYPE>
bool CATEntry< STL_CONT, ENTRY_TYPE>::
IsTheSameAsLast(VALUE_TYPE * pt)
{
return ( UTD() && ( !(Lower()> pt)) && (pt < Upper()) );
}
template <class STL_CONT, class ENTRY_TYPE>
void CATEntry< STL_CONT, ENTRY_TYPE>::
Update(VALUE_TYPE * pt)
{
if(!IsTheSameAsLast(pt)){
std::list<ENTRY_TYPE>::iterator lower_ite;
lower_ite = FindBase(pt);
assert( lower_ite!=AT().end());
Lower() = (*lower_ite).Start();
if( (*lower_ite).Start() == AT().back().Start())
Upper() = (VALUE_TYPE *) 0xffffffff;
else
{
lower_ite++;
Upper() = (*lower_ite).Start();
}
Curr() = lower_ite;
UTD() = true;
}
}
template <class STL_CONT, class ENTRY_TYPE>
void CATEntry< STL_CONT, ENTRY_TYPE>::
Resort(VALUE_TYPE* old_start,VALUE_TYPE* new_start)
{
AT().sort();
UTD() = false;
}
template <class STL_CONT, class ENTRY_TYPE>
void CATEntry<STL_CONT, ENTRY_TYPE>::
Remove( const STL_CONT & c )
{
std::list<ENTRY_TYPE>::iterator ite;
for(ite = AT().begin(); ite != AT().end(); ++ite)
if((*ite).C() == &c)
{
AT().erase(ite);
break;
}
UTD() = false;
}
template <class STL_CONT, class ENTRY_TYPE>
void CATEntry<STL_CONT, ENTRY_TYPE>::
Remove(VALUE_TYPE * pt)
{
std::list<ENTRY_TYPE>::iterator lower_ite;
lower_ite = FindBase(pt);
AT().erase(lower_ite);
UTD() = false;
}
template <class STL_CONT, class ENTRY_TYPE>
void CATEntry<STL_CONT, ENTRY_TYPE>::
Insert( STL_CONT & c )
{
ENTRY_TYPE entry(c);
std::list<ENTRY_TYPE>::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 <class STL_CONT, class ENTRY_TYPE>
ENTRY_TYPE & CATEntry<STL_CONT, ENTRY_TYPE>::
GetEntry(STL_CONT::value_type*pt){
Update(pt);
return *Curr();
}
template <class STL_CONT, class ENTRY_TYPE>
void CATEntry<STL_CONT, ENTRY_TYPE>::
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...)
template <class STL_CONT,class ATTR_TYPE>
class CAT:public CATEntry<STL_CONT, EntryCAT<STL_CONT,ATTR_TYPE> >{
public:
typedef typename EntryCAT<STL_CONT,ATTR_TYPE> EntryType;
static ATTR_TYPE & Get(STL_CONT::value_type * pt);
};
//---------------------- CAT: implementation---------------------------------------------------
template <class STL_CONT, class ATTR_TYPE>
ATTR_TYPE & CAT<STL_CONT,ATTR_TYPE>::
Get(STL_CONT::value_type * pt)
{
int ord = Ord(pt);
return Curr()->Data()[ord];
}
//---------------------------------------------------------------------------------------------
};//end namespace vcg
#endif

View File

@ -0,0 +1,149 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* 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. *
* *
****************************************************************************/
#ifndef __VCGLIB_ENTRIES__
#define __VCGLIB_ENTRIES__
namespace vcg {
// EntryCATBase: base class for the entry of the allocation table
// templated over the container type
template <class STL_CONT>
struct EntryCATBase{
EntryCATBase(STL_CONT & _c):c(_c){};
STL_CONT::value_type * Start() const;
const STL_CONT * C();
virtual void Push_back(const int &){};
virtual void Reserve(const int & s){};
virtual void Resize(const int & s){};
const bool operator < (const EntryCATBase<STL_CONT> & other) const;
private:
STL_CONT & c;
};
//EntryCAT: entry for the case of optional core types (matches with CAT)
template <class STL_CONT,class ATTR_TYPE >
struct EntryCAT: public EntryCATBase<STL_CONT>{
typedef ATTR_TYPE attr_type;
EntryCAT(STL_CONT & _c) : EntryCATBase<STL_CONT>(_c){};
std::vector<ATTR_TYPE> & Data(){return data;}
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<ATTR_TYPE> data;
};
//----------------------EntryCAT: implementation ----------------------------------------
template <class STL_CONT>
const bool EntryCATBase<STL_CONT>:: operator < (const EntryCATBase<STL_CONT> & other) const{
return (Start() < other.Start());
}
template <class STL_CONT>
STL_CONT::value_type * EntryCATBase<STL_CONT>::Start()const {
return &(*(c.begin()));
}
template <class STL_CONT>
const STL_CONT * EntryCATBase<STL_CONT>::C(){
return &c;
}
// -----------------------------------------------------------------------------------------
// WrapBase: used to implement a list of pointers to std::vector of different types
// Wrap: derived from WrapBase (to take the function and from std::vector)
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 <class ATTR_TYPE>
struct Wrap: public WrapBase,std::vector<ATTR_TYPE>{
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 <class STL_CONT>
struct EntryCATMulti: public EntryCATBase<STL_CONT>{
EntryCATMulti(STL_CONT & _c) : EntryCATBase<STL_CONT>(_c){};
std::list<WrapBase * > & 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<WrapBase * >::iterator ite;
for(ite = data.begin(); ite != data.end(); ++ite)
(*ite)->Reserve(n);
};
virtual void Resize(const int & n){
std::list<WrapBase * >::iterator ite;
for(ite = data.begin(); ite != data.end(); ++ite)
(*ite)->Resize(n);
};
private:
std::list< WrapBase * > data;
};
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
// TempData implements a handle to one of the vector od data stored in EntryCATMulti
template <class STL_CONT, class ATTR_TYPE>
class TempData{
public:
TempData(std::vector<ATTR_TYPE> *d):item(d){};
typedef typename ATTR_TYPE attr_type;
std::vector<ATTR_TYPE> * Item(){return item;};
std::vector<ATTR_TYPE> * item;
ATTR_TYPE & operator [](STL_CONT::value_type * v)
{
int pos = CATEntry<STL_CONT, EntryCATMulti<STL_CONT> >::Ord(v);
return (*item)[pos];
}
};
//----------------------------------------------------------------------------------
}; // end namespace vcg
#endif

View File

@ -0,0 +1,65 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* 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. *
* *
****************************************************************************/
#ifndef __VCGLIB_SIMPLE__
#define __VCGLIB_SIMPLE__
#include <vector>
namespace vcg {
template <class STL_CONT, class ATTR_TYPE>
class SimpleTempData{
public:
typedef typename ATTR_TYPE attr_type;
STL_CONT& c;
std::vector<ATTR_TYPE> data;
SimpleTempData(STL_CONT &_c):c(_c){};
// access to data
ATTR_TYPE & operator[](const STL_CONT::value_type *v){return data[v-&*c.begin()];}
ATTR_TYPE & operator[](const int & i){return data[i];}
// start temporary attribute
void Start(){data.reserve(c.capacity());data.resize(c.size());}
// stop temporary attribute
void Stop(){data.clear();}
// update temproary data size
bool UpdateSize(){
if(data.size() != c.size())
{
data.resize(c.size());
return false;
}
return true;
}
};
}; // end namespace vcg
#endif

View File

@ -0,0 +1,152 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* 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. *
* *
****************************************************************************/
#ifndef __VCGLIB_TRACED_VECTOR__
#define __VCGLIB_TRACED_VECTOR__
#include <vcg/container_allocation_table.h>
#include <assert.h>
namespace vcg {
template <class VALUE_TYPE>
class TVector: public std::vector<VALUE_TYPE>{
typedef TVector<VALUE_TYPE> THIS_TYPE;
public:
TVector():std::vector<VALUE_TYPE>(){reserve(1);}
~TVector();
std::list < CATBase<THIS_TYPE>* > attributes;
// override di tutte le funzioni che possono spostare
// l'allocazione in memoria del container
void push_back(const VALUE_TYPE & v);
void pop_back();
void resize(const unsigned int & size);
void reserve(const unsigned int & size);
template <class ATTR_TYPE>
void EnableAttribute(){
CAT<THIS_TYPE,ATTR_TYPE> * cat = new CAT<THIS_TYPE,ATTR_TYPE>();
cat->Insert(*this);
attributes.push_back(cat);
}
template <class ATTR_TYPE>
void DisableAttribute(){
std::list < CATBase<THIS_TYPE> * >::iterator ia;
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
if((*ia)->Id() == CAT<THIS_TYPE,ATTR_TYPE>::Id())
{
(*ia)->Remove(*this);
delete (*ia);
attributes.erase(ia);
break;
}
}
template <class ATTR_TYPE>
TempData<THIS_TYPE,ATTR_TYPE> NewTempData(){
//CAT<THIS_TYPE,EntryCATMulti>::Insert(*this)
CATEntry<THIS_TYPE,EntryCATMulti<THIS_TYPE> >::EntryType
entry = CATEntry<THIS_TYPE,EntryCATMulti<THIS_TYPE> >::GetEntry(&*begin());
entry.Data().push_back(new Wrap< ATTR_TYPE>);
((Wrap<ATTR_TYPE>*)entry.Data().back())->reserve(capacity());
((Wrap<ATTR_TYPE>*)entry.Data().back())->resize(size());
return TempData<THIS_TYPE,ATTR_TYPE>((Wrap<ATTR_TYPE>*) entry.Data().back());
}
template <class ATTR_TYPE>
void DeleteTempData(TempData<THIS_TYPE,ATTR_TYPE> & td){
//CAT<THIS_TYPE,EntryCATMulti>::Insert(*this)
CATEntry<THIS_TYPE,EntryCATMulti<THIS_TYPE> >::EntryType
entry = CATEntry<THIS_TYPE,EntryCATMulti<THIS_TYPE> >::GetEntry(&*begin());
entry.Data().remove((Wrap<ATTR_TYPE>*)td.Item());
delete ((Wrap<ATTR_TYPE>*)td.Item());
}
private:
VALUE_TYPE * old_start;
void Update();
};
template <class VALUE_TYPE>
void TVector<VALUE_TYPE>::push_back(const VALUE_TYPE & v){
std::vector<VALUE_TYPE>::push_back(v);
std::list < CATBase<THIS_TYPE> * >::iterator ia;
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
(*ia)->AddDataElem(&(*(this->begin())),1);
Update();
}
template <class VALUE_TYPE>
void TVector<VALUE_TYPE>::pop_back(){
std::vector<VALUE_TYPE>::pop_back();
Update();
}
template <class VALUE_TYPE>
void TVector<VALUE_TYPE>::resize(const unsigned int & size){
std::vector<VALUE_TYPE>::resize(size);
std::list < CATBase<THIS_TYPE> * >::iterator ia;
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
(*ia)->
Update();
}
template <class VALUE_TYPE>
void TVector<VALUE_TYPE>::reserve(const unsigned int & size){
std::vector<VALUE_TYPE>::reserve(size);
Update();
}
template <class VALUE_TYPE>
void TVector<VALUE_TYPE>::
Update(){
std::list < CATBase<THIS_TYPE> * >::iterator ia;
if(&(*begin()) != old_start)
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
(*ia)->Resort(old_start,&(*begin()));
old_start = &(*begin());
}
template <class VALUE_TYPE>
TVector<VALUE_TYPE>::~TVector(){
std::list < CATBase<THIS_TYPE> * >::iterator ia;
for(ia = attributes.begin(); ia != attributes.end(); ++ia)
{
(*ia)->Remove(*this);
delete *ia;
}
}
}; // end namespace
#endif