Update of /cvsroot/pure-data/externals/grill/flext/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28823/source
Modified Files: flattr.cpp flclass.h flitem.cpp fllib.cpp flmap.h flmeth.cpp flprefix.h flstdc.h flsupport.cpp flsupport.h Log Message: optimized AtomList functions moved FLEXT_SHARE definition smaller changes to TableMap enhancements and more features for TableMap type new: FLEXT_WARN, FLEXT_ERROR macros
Index: flmap.h =================================================================== RCS file: /cvsroot/pure-data/externals/grill/flext/source/flmap.h,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** flmap.h 25 Mar 2005 04:52:21 -0000 1.10 --- flmap.h 31 Mar 2005 03:52:38 -0000 1.11 *************** *** 95,158 **** };
! TableAnyMap(TableAnyMap *p,int sz,Data *dt,bool o) ! : owned(o),tsize(sz),data(dt) ! , n(0),parent(p),left(NULL),right(NULL) {}
virtual ~TableAnyMap();
! int size() const;
void insert(size_t k,void *t) { ! FLEXT_ASSERT(t); ! if(n) _set(k,t); ! else data[n++](k,t); ! } ! ! void *find(size_t k) { return n?_find(k):NULL; } ! ! void clear(); ! ! void _toleft(size_t k,void *t) ! { ! if(left) ! left->_set(k,t); ! else { ! left = New(this); ! left->data[0](k,t); ! left->n = 1; ! } ! } ! ! void _toright(size_t k,void *t) ! { ! if(right) ! right->_set(k,t); else { ! right = New(this); ! right->data[0](k,t); ! right->n = 1; } }
! void _toleft(Data &v) { _toleft(v.key,v.value); } ! void _toright(Data &v) { _toright(v.key,v.value); }
! void _set(size_t k,void *t); ! void *_find(size_t k);
! const bool owned; ! const int tsize; ! Data *const data; ! int n; ! TableAnyMap *parent,*left,*right;
! class iterator { public: iterator(): map(NULL) {} ! iterator(TableAnyMap &m): map(&m),ix(0) { leftmost(); } iterator(iterator &it): map(it.map),ix(it.ix) {}
--- 95,132 ---- };
! TableAnyMap(TableAnyMap *p,int sz,Data *dt) ! : tsize(sz),data(dt) ! , n(0),count(0) ! , parent(p),left(NULL),right(NULL) {}
virtual ~TableAnyMap();
! int size() const { return count; }
void insert(size_t k,void *t) { ! // FLEXT_ASSERT(t); ! if(n) ! _set(k,t); else { ! data[n++](k,t); ! ++count; } }
! void *find(size_t k) const { return n?_find(k):NULL; }
! void erase(size_t k) { if(n) { void *s = _remove(k); if(s) Free(s); } }
! void *remove(size_t k) { return n?_remove(k):NULL; }
+ void clear();
! class FLEXT_SHARE iterator { public: iterator(): map(NULL) {} ! iterator(const TableAnyMap &m): map(&m),ix(0) { leftmost(); } iterator(iterator &it): map(it.map),ix(it.ix) {}
*************** *** 171,175 **** { // search smallest branch (go left as far as possible) ! TableAnyMap *nmap; while((nmap = map->left) != NULL) map = nmap; } --- 145,149 ---- { // search smallest branch (go left as far as possible) ! const TableAnyMap *nmap; while((nmap = map->left) != NULL) map = nmap; } *************** *** 177,193 **** void forward();
! TableAnyMap *map; int ix; }; - };
! template <typename K,typename T,int N = 8,bool O = false> ! class TableMap : TableAnyMap { public: ! TableMap(): TableAnyMap(NULL,N,slots,O) {} ! virtual ~TableMap() { clear(); }
inline void clear() { TableAnyMap::clear(); } --- 151,247 ---- void forward();
! const TableAnyMap *map; int ix; };
+ private: + void _init(size_t k,void *t) { data[0](k,t); n = count = 1; }
! bool _toleft(size_t k,void *t) ! { ! if(left) { ! bool a = left->_set(k,t); ! if(a) ++count; ! return a; ! } ! else { ! (left = New(this))->_init(k,t); ! ++count; ! return true; ! } ! } ! ! bool _toright(size_t k,void *t) ! { ! if(right) { ! bool a = right->_set(k,t); ! if(a) ++count; ! return a; ! } ! else { ! (right = New(this))->_init(k,t); ! ++count; ! return true; ! } ! } ! ! bool _toleft(Data &v) { return _toleft(v.key,v.value); } ! bool _toright(Data &v) { return _toright(v.key,v.value); } ! ! bool _set(size_t k,void *t); ! void *_find(size_t k) const; ! void *_remove(size_t k); ! ! const int tsize; ! Data *const data; ! int count,n; ! TableAnyMap *parent,*left,*right; ! ! int _tryix(size_t k) const ! { ! //! return index of data item with key <= k ! ! // FLEXT_ASSERT(n); ! int ix = 0; ! { ! int b = n; ! while(ix != b) { ! const int c = (ix+b)/2; ! const size_t dk = data[c].key; ! if(k == dk) ! return c; ! else if(k < dk) ! b = c; ! else if(ix < c) ! ix = c; ! else { ! ix = b; ! break; ! } ! } ! } ! return ix; ! } ! ! static void _eraseempty(TableAnyMap *&b) ! { ! // FLEXT_ASSERT(b); ! if(!b->n) { ! // remove empty branch ! delete b; b = NULL; ! } ! } ! ! void _getsmall(Data &dt); ! void _getbig(Data &dt); ! }; ! ! template <typename K,typename T,int N = 8> ! class TablePtrMap : TableAnyMap { public: ! TablePtrMap(): TableAnyMap(NULL,N,slots) {} ! virtual ~TablePtrMap() { clear(); }
inline void clear() { TableAnyMap::clear(); } *************** *** 197,201 **** inline void insert(K k,T *t) { TableAnyMap::insert(*(size_t *)&k,t); }
! inline T *find(K k) { return (T *)TableAnyMap::find(*(size_t *)&k); }
class iterator --- 251,258 ---- inline void insert(K k,T *t) { TableAnyMap::insert(*(size_t *)&k,t); }
! inline T *find(K k) const { return (T *)TableAnyMap::find(*(size_t *)&k); } ! ! inline void erase(K k) { TableAnyMap::erase(*(size_t *)&k); } ! inline T *remove(K k) { return (T *)TableAnyMap::remove(*(size_t *)&k); }
class iterator *************** *** 204,208 **** public: iterator() {} ! iterator(TableMap &m): TableAnyMap::iterator(m) {} iterator(iterator &it): TableAnyMap::iterator(it) {}
--- 261,265 ---- public: iterator() {} ! iterator(const TablePtrMap &m): TableAnyMap::iterator(m) {} iterator(iterator &it): TableAnyMap::iterator(it) {}
*************** *** 218,229 ****
protected: ! TableMap(TableAnyMap *p): TableAnyMap(p,N,slots,O) {}
! virtual TableAnyMap *New(TableAnyMap *parent) { return new TableMap(parent); } ! virtual void Free(void *ptr) { delete (T *)ptr; }
Data slots[N]; };
//! @} // FLEXT_SUPPORT --- 275,302 ----
protected: ! TablePtrMap(TableAnyMap *p): TableAnyMap(p,N,slots) {}
! virtual TableAnyMap *New(TableAnyMap *parent) { return new TablePtrMap(parent); } ! ! virtual void Free(void *ptr) {}
Data slots[N]; };
+ template <typename K,typename T,int N = 8> + class TablePtrMapOwned + : public TablePtrMap<K,T,N> + { + public: + virtual ~TablePtrMapOwned() { clear(); } + + protected: + virtual void Free(void *ptr) + { + // FLEXT_ASSERT(ptr); + delete (T *)ptr; + } + + };
//! @} // FLEXT_SUPPORT
Index: flstdc.h =================================================================== RCS file: /cvsroot/pure-data/externals/grill/flext/source/flstdc.h,v retrieving revision 1.33 retrieving revision 1.34 diff -C2 -d -r1.33 -r1.34 *** flstdc.h 22 Mar 2005 04:56:28 -0000 1.33 --- flstdc.h 31 Mar 2005 03:52:38 -0000 1.34 *************** *** 291,313 ****
- /* Set the right calling convention (and exporting) for the OS */ - - #ifdef _MSC_VER - #ifdef FLEXT_SHARED - // for compiling a shared flext library FLEXT_EXPORTS must be defined - #ifdef FLEXT_EXPORTS - #define FLEXT_SHARE __declspec(dllexport) - #else - #define FLEXT_SHARE __declspec(dllimport) - #endif - #else - #define FLEXT_SHARE - #endif - #define FLEXT_EXT __declspec(dllexport) - #else // other OS's - #define FLEXT_SHARE - #define FLEXT_EXT - #endif - // ----- disable attribute editor for PD version < devel_0_36 or 0.37 #ifndef PD_MAJOR_VERSION --- 291,294 ----
Index: flclass.h =================================================================== RCS file: /cvsroot/pure-data/externals/grill/flext/source/flclass.h,v retrieving revision 1.54 retrieving revision 1.55 diff -C2 -d -r1.54 -r1.55 *** flclass.h 16 Mar 2005 04:56:29 -0000 1.54 --- flclass.h 31 Mar 2005 03:52:38 -0000 1.55 *************** *** 660,664 **** }; */ ! typedef TableMap<const t_symbol *,Item,8,true> ItemSet;
/*! This class holds hashed item entries --- 660,664 ---- }; */ ! typedef TablePtrMapOwned<const t_symbol *,Item,8> ItemSet;
/*! This class holds hashed item entries *************** *** 776,780 **** }; */ ! typedef TableMap<const t_symbol *,AttrData,8,true> AttrDataCont;
// these outlet functions don't check for thread but send directly to the real-time system --- 776,780 ---- }; */ ! typedef TablePtrMapOwned<const t_symbol *,AttrData,8> AttrDataCont;
// these outlet functions don't check for thread but send directly to the real-time system
Index: flitem.cpp =================================================================== RCS file: /cvsroot/pure-data/externals/grill/flext/source/flitem.cpp,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** flitem.cpp 15 Mar 2005 04:56:35 -0000 1.16 --- flitem.cpp 31 Mar 2005 03:52:38 -0000 1.17 *************** *** 87,92 **** if(lit == item) { if(prv) prv->nxt = lit->nxt; ! else set.insert(tag,lit->nxt); ! lit->nxt = NULL; if(free) delete lit; --- 87,95 ---- if(lit == item) { if(prv) prv->nxt = lit->nxt; ! else if(lit->nxt) ! set.insert(tag,lit->nxt); ! else ! set.erase(tag); ! lit->nxt = NULL; if(free) delete lit; *************** *** 106,110 **** // --- class item lists (methods and attributes) ----------------
! typedef TableMap<flext_base::t_classid,flext_base::ItemCont,64> ClassMap;
static ClassMap classarr[2]; --- 109,113 ---- // --- class item lists (methods and attributes) ----------------
! typedef TablePtrMap<flext_base::t_classid,flext_base::ItemCont,64> ClassMap;
static ClassMap classarr[2];
Index: flsupport.h =================================================================== RCS file: /cvsroot/pure-data/externals/grill/flext/source/flsupport.h,v retrieving revision 1.85 retrieving revision 1.86 diff -C2 -d -r1.85 -r1.86 *** flsupport.h 22 Mar 2005 04:56:29 -0000 1.85 --- flsupport.h 31 Mar 2005 03:52:38 -0000 1.86 *************** *** 379,383 **** #endif //! Check for symbol and get string ! static const char *GetAString(const t_symbol *s,const char *def = "") { return s?GetString(s):def; }
// --- atom stuff ---------------------------------------- --- 379,383 ---- #endif //! Check for symbol and get string ! static const char *GetAString(const t_symbol *s,const char *def = NULL) { return s?GetString(s):def; }
// --- atom stuff ---------------------------------------- *************** *** 461,464 **** --- 461,466 ---- static const char *GetString(const t_atom &a) { t_symbol *s = GetSymbol(a); return s?GetString(s):NULL; } //! Check for a string and get its value + static const char *GetAString(const t_atom &a,const char *def = NULL) { return IsSymbol(a)?GetAString(GetSymbol(a),def):def; } + //! Check for a string and get its value static void GetAString(const t_atom &a,char *buf,size_t szbuf); //! Set the atom to represent a string
Index: flmeth.cpp =================================================================== RCS file: /cvsroot/pure-data/externals/grill/flext/source/flmeth.cpp,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** flmeth.cpp 16 Mar 2005 04:56:30 -0000 1.16 --- flmeth.cpp 31 Mar 2005 03:52:38 -0000 1.17 *************** *** 90,94 **** void flext_base::ListMethods(AtomList &la,int inlet) const { ! typedef TableMap<int,t_symbol,32> MethList; MethList list[2];
--- 90,94 ---- void flext_base::ListMethods(AtomList &la,int inlet) const { ! typedef TablePtrMap<int,t_symbol,32> MethList; MethList list[2];
Index: flsupport.cpp =================================================================== RCS file: /cvsroot/pure-data/externals/grill/flext/source/flsupport.cpp,v retrieving revision 1.47 retrieving revision 1.48 diff -C2 -d -r1.47 -r1.48 *** flsupport.cpp 25 Mar 2005 04:52:21 -0000 1.47 --- flsupport.cpp 31 Mar 2005 03:52:38 -0000 1.48 *************** *** 311,322 **** if(left) { delete left; left = NULL; } if(right) { delete right; right = NULL; } ! if(owned) ! for(int i = 0; i < n; ++i) { ! FLEXT_ASSERT(data[i].value); ! Free(data[i].value); ! } ! n = 0; }
int TableAnyMap::size() const { --- 311,320 ---- if(left) { delete left; left = NULL; } if(right) { delete right; right = NULL; } ! ! for(int i = 0; i < n; ++i) Free(data[i].value); ! count = n = 0; }
+ /* int TableAnyMap::size() const { *************** *** 326,333 **** if(right) sz += right->size(); } return sz; }
! void TableAnyMap::_set(size_t k,void *t) { FLEXT_ASSERT(n); --- 324,333 ---- if(right) sz += right->size(); } + FLEXT_ASSERT(sz == count); return sz; } + */
! bool TableAnyMap::_set(size_t k,void *t) { FLEXT_ASSERT(n); *************** *** 336,374 **** // fall through } ! else if(k < data[0].key) { ! _toleft(k,t); ! return; ! } ! else if(k > data[tsize-1].key) { ! _toright(k,t); ! return; ! } ! ! int ix = 0; ! { ! int b = n; ! while(ix != b) { ! const int c = (ix+b)/2; ! const size_t dk = data[c].key; ! if(k == dk) { ! ix = c; ! break; ! } ! else if(k < dk) ! b = c; ! else if(ix < c) ! ix = c; ! else { ! ix = b; ! break; ! } ! } ! }
size_t dk = data[ix].key; if(k == dk) { // update data in existing slot (same key) ! if(owned) Free(data[ix].value); data[ix] = t; } else if(ix >= n) { --- 336,351 ---- // fall through } ! else if(k < data[0].key) ! return _toleft(k,t); ! else if(k > data[tsize-1].key) ! return _toright(k,t);
+ int ix = _tryix(k); size_t dk = data[ix].key; if(k == dk) { // update data in existing slot (same key) ! Free(data[ix].value); data[ix] = t; + return false; } else if(ix >= n) { *************** *** 376,395 **** // after last entry data[n++](k,t); } else { // insert new slot by shifting the higher ones FLEXT_ASSERT(k < dk); if(n == tsize) ! _toright(data[tsize-1]); ! else ! ++n;
Data *tg = data+ix; for(Data *d = data+n-1; d > tg; d--) d[0] = d[-1]; (*tg)(k,t); } }
! void *TableAnyMap::_find(size_t k) { FLEXT_ASSERT(n); --- 353,378 ---- // after last entry data[n++](k,t); + ++count; + return true; } else { // insert new slot by shifting the higher ones FLEXT_ASSERT(k < dk); + bool a; if(n == tsize) ! a = _toright(data[tsize-1]); ! else { ! ++n,++count; ! a = true; ! }
Data *tg = data+ix; for(Data *d = data+n-1; d > tg; d--) d[0] = d[-1]; (*tg)(k,t); + return a; } }
! void *TableAnyMap::_find(size_t k) const { FLEXT_ASSERT(n); *************** *** 402,431 **** return right?right->_find(k):NULL;
! //! return index of data item with key <= k
FLEXT_ASSERT(n); ! int ix = 0; ! { ! int b = n; ! while(ix != b) { ! const int c = (ix+b)/2; ! const size_t dk = data[c].key; ! if(k == dk) ! return data[c].value; ! else if(k < dk) ! b = c; ! else if(ix < c) ! ix = c; ! else { ! ix = b; ! break; ! } } }
! if(data[ix].key == k) ! return data[ix].value; ! else return NULL; }
--- 385,502 ---- return right?right->_find(k):NULL;
! const int ix = _tryix(k); ! return data[ix].key == k?data[ix].value:NULL; ! }
+ void *TableAnyMap::_remove(size_t k) + { FLEXT_ASSERT(n); ! if(n < tsize) { ! // fall through ! } ! else if(k < data[0].key) { ! void *r = left?left->_remove(k):NULL; ! if(r) { ! _eraseempty(left); ! --count; } + return r; + } + else if(k > data[n-1].key) { + void *r = right?right->_remove(k):NULL; + if(r) { + _eraseempty(right); + --count; + } + return r; }
! const int ix = _tryix(k); ! if(data[ix].key != k) return NULL; + else { + // found key in this map + void *ret = data[ix].value; + + Data dt; + bool fnd,ins = false; + if(n >= tsize) { + // if this table is full get fill-in elements from branches + if(left) { + // try to get biggest element from left branch + left->_getbig(dt); + _eraseempty(left); + fnd = true,ins = true; + } + else if(right) { + // try to get smallest element from right branch + right->_getsmall(dt); + _eraseempty(right); + fnd = true; + } + else + fnd = false; + } + else fnd = false; + + if(ins) { + // insert smaller element from left + for(int i = ix; i; --i) data[i] = data[i-1]; + data[0] = dt; + } + else { + // shift elements + for(int i = ix+1; i < n; ++i) data[i-1] = data[i]; + // insert bigger element from right or reduce table size + if(fnd) + data[n-1] = dt; + else + --n; + } + + --count; + return ret; + } + } + + void TableAnyMap::_getbig(Data &dt) + { + FLEXT_ASSERT(n); + + if(right) { + right->_getbig(dt); + _eraseempty(right); + } + else { + if(left) { + for(int i = n; i; --i) data[i] = data[i-1]; + left->_getbig(data[0]); + _eraseempty(left); + } + else + dt = data[--n]; + } + --count; + } + + void TableAnyMap::_getsmall(Data &dt) + { + FLEXT_ASSERT(n); + + if(left) { + left->_getsmall(dt); + _eraseempty(left); + } + else { + dt = data[0]; + for(int i = 1; i < n; ++i) data[i-1] = data[i]; + if(right) { + right->_getsmall(data[n-1]); + _eraseempty(right); + } + else + --n; + } + --count; }
Index: flattr.cpp =================================================================== RCS file: /cvsroot/pure-data/externals/grill/flext/source/flattr.cpp,v retrieving revision 1.30 retrieving revision 1.31 diff -C2 -d -r1.30 -r1.31 *** flattr.cpp 15 Mar 2005 04:56:35 -0000 1.30 --- flattr.cpp 31 Mar 2005 03:52:38 -0000 1.31 *************** *** 104,108 **** void flext_base::ListAttrib(AtomList &la) const { ! typedef TableMap<int,t_symbol,32> AttrList; AttrList list[2];
--- 104,108 ---- void flext_base::ListAttrib(AtomList &la) const { ! typedef TablePtrMap<int,t_symbol,32> AttrList; AttrList list[2];
Index: flprefix.h =================================================================== RCS file: /cvsroot/pure-data/externals/grill/flext/source/flprefix.h,v retrieving revision 1.32 retrieving revision 1.33 diff -C2 -d -r1.32 -r1.33 *** flprefix.h 13 Mar 2005 04:56:39 -0000 1.32 --- flprefix.h 31 Mar 2005 03:52:38 -0000 1.33 *************** *** 373,376 **** --- 373,397 ---- #endif
+ + /* Set the right calling convention (and exporting) for the OS */ + + #ifdef _MSC_VER + #ifdef FLEXT_SHARED + // for compiling a shared flext library FLEXT_EXPORTS must be defined + #ifdef FLEXT_EXPORTS + #define FLEXT_SHARE __declspec(dllexport) + #else + #define FLEXT_SHARE __declspec(dllimport) + #endif + #else + #define FLEXT_SHARE + #endif + #define FLEXT_EXT __declspec(dllexport) + #else // other OS's + #define FLEXT_SHARE + #define FLEXT_EXT + #endif + + // std namespace #ifdef __MWERKS__
Index: fllib.cpp =================================================================== RCS file: /cvsroot/pure-data/externals/grill/flext/source/fllib.cpp,v retrieving revision 1.31 retrieving revision 1.32 diff -C2 -d -r1.31 -r1.32 *** fllib.cpp 15 Mar 2005 04:56:36 -0000 1.31 --- fllib.cpp 31 Mar 2005 03:52:38 -0000 1.32 *************** *** 127,131 ****
! typedef TableMap<const t_symbol *,libclass,8> LibMap;
static LibMap libnames; --- 127,131 ----
! typedef TablePtrMap<const t_symbol *,libclass,8> LibMap;
static LibMap libnames;