From d6d1febfad6698f55f077b7dd94330a46da83886 Mon Sep 17 00:00:00 2001 From: Thomas Grill Date: Mon, 18 Apr 2005 15:07:41 +0000 Subject: changed template parameter of TableMap fixed problems with symbol binding restructured TableMap type (doesn't own pointers any more) svn path=/trunk/; revision=2779 --- externals/grill/flext/source/flattr.cpp | 12 +++- externals/grill/flext/source/flattr_ed.cpp | 6 +- externals/grill/flext/source/flclass.h | 16 ++++- externals/grill/flext/source/flitem.cpp | 20 ++++-- externals/grill/flext/source/flmap.h | 99 ++++++++++++++++++------------ externals/grill/flext/source/flsupport.cpp | 50 +++++++-------- 6 files changed, 130 insertions(+), 73 deletions(-) (limited to 'externals/grill/flext') diff --git a/externals/grill/flext/source/flattr.cpp b/externals/grill/flext/source/flattr.cpp index 79a63ab3..1ae55ec4 100644 --- a/externals/grill/flext/source/flattr.cpp +++ b/externals/grill/flext/source/flattr.cpp @@ -43,6 +43,13 @@ flext_base::AttrDataCont::~AttrDataCont() } */ +flext_base::AttrDataCont::~AttrDataCont() { clear(); } + +void flext_base::AttrDataCont::clear() +{ + for(iterator it(*this); it; ++it) delete it.data(); + TablePtrMap::clear(); +} //! Add get and set attributes void flext_base::AddAttrib(ItemCont *aa,ItemCont *ma,const t_symbol *asym,metharg tp,methfun gfun,methfun sfun) @@ -167,7 +174,10 @@ bool flext_base::InitAttrib(int argc,const t_atom *argv) SetAttrib(tag,attr,a.GetInitValue()); */ AttrData *a = attrdata->find(tag); - if(!a) attrdata->insert(tag,a = new AttrData); + if(!a) { + AttrData *old = attrdata->insert(tag,a = new AttrData); + FLEXT_ASSERT(!old); + } a->SetInit(true); a->SetInitValue(nxt-cur-1,argv+cur+1); diff --git a/externals/grill/flext/source/flattr_ed.cpp b/externals/grill/flext/source/flattr_ed.cpp index d8883005..dc2cc9d1 100644 --- a/externals/grill/flext/source/flattr_ed.cpp +++ b/externals/grill/flext/source/flattr_ed.cpp @@ -723,8 +723,10 @@ bool flext_base::cb_AttrDialog(flext_base *th,int argc,const t_atom *argv) a.SetInit(true); a.SetInitValue(icnt,argv+ioffs); */ - if(!a) - th->attrdata->insert(aname,a = new AttrData); + if(!a) { + AttrData *old = th->attrdata->insert(aname,a = new AttrData); + FLEXT_ASSERT(!old); + } a->SetSave(sv == 2); a->SetInit(true); diff --git a/externals/grill/flext/source/flclass.h b/externals/grill/flext/source/flclass.h index cf11bdb8..bebd045f 100644 --- a/externals/grill/flext/source/flclass.h +++ b/externals/grill/flext/source/flclass.h @@ -659,7 +659,13 @@ protected: ~ItemSet(); }; */ - typedef TablePtrMapOwned ItemSet; + class ItemSet + :public TablePtrMap + { + public: + virtual ~ItemSet(); + virtual void clear(); + }; /*! This class holds hashed item entries \note not thread-safe! @@ -775,7 +781,13 @@ protected: ~AttrDataCont(); }; */ - typedef TablePtrMapOwned AttrDataCont; + class AttrDataCont + :public TablePtrMap + { + public: + virtual ~AttrDataCont(); + virtual void clear(); + }; // these outlet functions don't check for thread but send directly to the real-time system #if FLEXT_SYS == FLEXT_SYS_PD || FLEXT_SYS == FLEXT_SYS_MAX diff --git a/externals/grill/flext/source/flitem.cpp b/externals/grill/flext/source/flitem.cpp index 4cab1481..057f1d44 100755 --- a/externals/grill/flext/source/flitem.cpp +++ b/externals/grill/flext/source/flitem.cpp @@ -15,6 +15,14 @@ WARRANTIES, see the file, "license.txt," in this distribution. #include "flext.h" #include +flext_base::ItemSet::~ItemSet() { clear(); } + +void flext_base::ItemSet::clear() +{ + for(iterator it(*this); it; ++it) delete it.data(); + TablePtrMap::clear(); +} + flext_base::Item::~Item() { @@ -57,8 +65,10 @@ void flext_base::ItemCont::Add(Item *item,const t_symbol *tag,int inlet) if(!Contained(inlet)) Resize(inlet+2); ItemSet &set = GetInlet(inlet); Item *lst = set.find(tag); - if(!lst) - set.insert(tag,lst = item); + if(!lst) { + Item *old = set.insert(tag,lst = item); + FLEXT_ASSERT(!old); + } else for(;;) if(!lst->nxt) { lst->nxt = item; break; } @@ -76,8 +86,10 @@ bool flext_base::ItemCont::Remove(Item *item,const t_symbol *tag,int inlet,bool for(Item *prv = NULL; lit; prv = lit,lit = lit->nxt) { if(lit == item) { if(prv) prv->nxt = lit->nxt; - else if(lit->nxt) - set.insert(tag,lit->nxt); + else if(lit->nxt) { + Item *old = set.insert(tag,lit->nxt); + FLEXT_ASSERT(!old); + } else { Item *l = set.remove(tag); FLEXT_ASSERT(l == lit); diff --git a/externals/grill/flext/source/flmap.h b/externals/grill/flext/source/flmap.h index 57e806e1..1ca0267d 100644 --- a/externals/grill/flext/source/flmap.h +++ b/externals/grill/flext/source/flmap.h @@ -83,8 +83,9 @@ public: class FLEXT_SHARE TableAnyMap { protected: - virtual TableAnyMap *New(TableAnyMap *parent) = 0; - virtual void Free(void *ptr) = 0; + virtual TableAnyMap *_newmap(TableAnyMap *parent) = 0; + virtual void _delmap(TableAnyMap *map) = 0; +// virtual void Free(void *ptr) = 0; struct Data { void operator()(size_t k,void *v) { key = k,value = v; } @@ -96,32 +97,34 @@ protected: TableAnyMap(TableAnyMap *p,int sz,Data *dt) : tsize(sz),data(dt) - , n(0),count(0) + , n(0) +// , count(0) , parent(p),left(NULL),right(NULL) {} virtual ~TableAnyMap(); - int size() const { return count; } +// int size() const { return count; } - void insert(size_t k,void *t) + void *insert(size_t k,void *t) { // FLEXT_ASSERT(t); if(n) - _set(k,t); + return _set(k,t); else { data[n++](k,t); - ++count; +// ++count; + return NULL; } } 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 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(); + virtual void clear(); class FLEXT_SHARE iterator { @@ -155,52 +158,53 @@ protected: }; private: - void _init(size_t k,void *t) { data[0](k,t); n = count = 1; } + void _init(size_t k,void *t) { data[0](k,t); n = /*count =*/ 1; } - bool _toleft(size_t k,void *t) + void *_toleft(size_t k,void *t) { if(left) { - bool a = left->_set(k,t); - if(a) ++count; + void *a = left->_set(k,t); +// if(!a) ++count; return a; } else { - (left = New(this))->_init(k,t); - ++count; - return true; + (left = _newmap(this))->_init(k,t); + // ++count; + return NULL; } } - bool _toright(size_t k,void *t) + void *_toright(size_t k,void *t) { if(right) { - bool a = right->_set(k,t); - if(a) ++count; + void *a = right->_set(k,t); +// if(!a) ++count; return a; } else { - (right = New(this))->_init(k,t); - ++count; - return true; + (right = _newmap(this))->_init(k,t); +// ++count; + return NULL; } } - bool _toleft(Data &v) { return _toleft(v.key,v.value); } - bool _toright(Data &v) { return _toright(v.key,v.value); } + void *_toleft(Data &v) { return _toleft(v.key,v.value); } + void *_toright(Data &v) { return _toright(v.key,v.value); } - bool _set(size_t k,void *t); + void *_set(size_t k,void *t); void *_find(size_t k) const; void *_remove(size_t k); const int tsize; Data *const data; - int n,count; + int n; +// int count; TableAnyMap *parent,*left,*right; + //! return index of data item with key <= k + //! \note index can point past the last item! int _tryix(size_t k) const { - //! return index of data item with key <= k - // FLEXT_ASSERT(n); int ix = 0; { @@ -223,12 +227,12 @@ private: return ix; } - static void _eraseempty(TableAnyMap *&b) + void _eraseempty(TableAnyMap *&b) { // FLEXT_ASSERT(b); if(!b->n) { // remove empty branch - delete b; b = NULL; + _delmap(b); b = NULL; } } @@ -241,19 +245,29 @@ class TablePtrMap : TableAnyMap { public: - TablePtrMap(): TableAnyMap(NULL,N,slots) {} + TablePtrMap(): TableAnyMap(NULL,N,slots),count(0) {} virtual ~TablePtrMap() { clear(); } - inline void clear() { TableAnyMap::clear(); } + virtual void clear() { TableAnyMap::clear(); count = 0; } - inline int size() const { return TableAnyMap::size(); } + inline int size() const { return count; } - inline void insert(K k,T t) { TableAnyMap::insert(*(size_t *)&k,(void *)t); } + inline T insert(K k,T t) + { + void *d = TableAnyMap::insert(*(size_t *)&k,(void *)t); + if(!d) ++count; + return (T)d; + } 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); } +// inline void erase(K k) { TableAnyMap::erase(*(size_t *)&k); } + inline T remove(K k) + { + void *d = TableAnyMap::remove(*(size_t *)&k); + if(d) --count; + return (T)d; + } class iterator : TableAnyMap::iterator @@ -274,15 +288,18 @@ public: }; protected: - TablePtrMap(TableAnyMap *p): TableAnyMap(p,N,slots) {} + TablePtrMap(TableAnyMap *p): TableAnyMap(p,N,slots),count(0) {} - virtual TableAnyMap *New(TableAnyMap *parent) { return new TablePtrMap(parent); } + virtual TableAnyMap *_newmap(TableAnyMap *parent) { return new TablePtrMap(parent); } + virtual void _delmap(TableAnyMap *map) { delete (TablePtrMap *)map; } - virtual void Free(void *ptr) {} +// virtual void Free(void *ptr) {} + int count; Data slots[N]; }; +#if 0 template class TablePtrMapOwned : public TablePtrMap @@ -291,13 +308,15 @@ public: virtual ~TablePtrMapOwned() { TablePtrMapOwned::clear(); } protected: +/* virtual void Free(void *ptr) { // FLEXT_ASSERT(ptr); delete (T)ptr; } - +*/ }; +#endif //! @} // FLEXT_SUPPORT diff --git a/externals/grill/flext/source/flsupport.cpp b/externals/grill/flext/source/flsupport.cpp index 3d9b1b03..5b33352c 100644 --- a/externals/grill/flext/source/flsupport.cpp +++ b/externals/grill/flext/source/flsupport.cpp @@ -308,11 +308,11 @@ TableAnyMap::~TableAnyMap() { clear(); } void TableAnyMap::clear() { - if(left) { delete left; left = NULL; } - if(right) { delete right; right = NULL; } + if(left) { _delmap(left); left = NULL; } + if(right) { _delmap(right); right = NULL; } - for(int i = 0; i < n; ++i) Free(data[i].value); - count = n = 0; +// for(int i = 0; i < n; ++i) Free(data[i].value); + /*count = */n = 0; } /* @@ -328,7 +328,7 @@ int TableAnyMap::size() const } */ -bool TableAnyMap::_set(size_t k,void *t) +void *TableAnyMap::_set(size_t k,void *t) { FLEXT_ASSERT(n); @@ -341,29 +341,31 @@ bool TableAnyMap::_set(size_t k,void *t) return _toright(k,t); int ix = _tryix(k); + if(ix >= n) { + FLEXT_ASSERT(ix == n); + // after last entry + data[n++](k,t); +// ++count; + return NULL; + } + size_t dk = data[ix].key; if(k == dk) { // update data in existing slot (same key) - Free(data[ix].value); + void *a = data[ix].value; data[ix] = t; - return false; - } - else if(ix >= n) { - FLEXT_ASSERT(ix == n); - // after last entry - data[n++](k,t); - ++count; - return true; + return a; } else { // insert new slot by shifting the higher ones FLEXT_ASSERT(k < dk); - bool a; + void *a; if(n == tsize) a = _toright(data[tsize-1]); else { - ++n,++count; - a = true; + ++n; +// ++count; + a = NULL; } Data *tg = data+ix; @@ -385,7 +387,7 @@ void *TableAnyMap::_find(size_t k) const return right?right->_find(k):NULL; const int ix = _tryix(k); - return data[ix].key == k?data[ix].value:NULL; + return ix < n && data[ix].key == k?data[ix].value:NULL; } void *TableAnyMap::_remove(size_t k) @@ -398,7 +400,7 @@ void *TableAnyMap::_remove(size_t k) void *r = left?left->_remove(k):NULL; if(r) { _eraseempty(left); - --count; +// --count; } return r; } @@ -406,13 +408,13 @@ void *TableAnyMap::_remove(size_t k) void *r = right?right->_remove(k):NULL; if(r) { _eraseempty(right); - --count; +// --count; } return r; } const int ix = _tryix(k); - if(data[ix].key != k) + if(ix >= n || data[ix].key != k) return NULL; else { // found key in this map @@ -454,7 +456,7 @@ void *TableAnyMap::_remove(size_t k) --n; } - --count; +// --count; return ret; } } @@ -476,7 +478,7 @@ void TableAnyMap::_getbig(Data &dt) else dt = data[--n]; } - --count; +// --count; } void TableAnyMap::_getsmall(Data &dt) @@ -497,7 +499,7 @@ void TableAnyMap::_getsmall(Data &dt) else --n; } - --count; +// --count; } void TableAnyMap::iterator::forward() -- cgit v1.2.1