1 #ifndef _WDL_PTRLIST_INDEXED_H_
2 #define _WDL_PTRLIST_INDEXED_H_
4 #include "assocarray.h"
7 template<class T
> class WDL_IndexedPtrList
{
9 WDL_IndexedPtrList() { }
10 ~WDL_IndexedPtrList() { }
13 // do not _checkState(), GetSize() may be called while unlocked for estimation purposes
14 return m_list
.GetSize();
16 T
* const *GetList() const { _checkState(); return m_list
.GetList(); }
17 T
*Get(INT_PTR idx
) const { _checkState(); return m_list
.Get(idx
); }
18 int Find(const T
*p
) const
22 const int *ret
= m_index
.GetPtr((INT_PTR
)p
);
23 return ret
? *ret
: -1;
25 void Empty() { _checkState(); m_list
.Empty(); m_index
.DeleteAll(); }
26 void Empty(bool wantDelete
, void (*delfunc
)(void*)=NULL
) { _checkState(); m_list
.Empty(wantDelete
,delfunc
); m_index
.DeleteAll(); }
27 void Delete(int idx
, bool wantDelete
=false, void (*delfunc
)(void *)=NULL
)
30 T
*item
= m_list
.Get(idx
);
31 m_list
.Delete(idx
,wantDelete
,delfunc
);
34 m_index
.Delete((INT_PTR
)item
);
35 const int indexsz
= m_index
.GetSize();
36 WDL_ASSERT(m_list
.GetSize() == indexsz
);
39 for (int x
=0;x
<indexsz
;x
++)
41 int *val
= m_index
.EnumeratePtr(x
);
42 if (WDL_NORMALLY(val
))
44 WDL_ASSERT(*val
!= idx
);
45 if (*val
> idx
) (*val
)--;
51 void DeletePtr(const T
*p
) { Delete(Find(p
)); }
52 void DeletePtr(const T
*p
, bool wantDelete
, void (*delfunc
)(void *)=NULL
) { Delete(Find(p
),wantDelete
,delfunc
); }
56 WDL_ASSERT(Find(p
) < 0);
59 const int sz
= m_list
.GetSize();
61 m_index
.Insert((INT_PTR
)p
,sz
);
67 for (int x
= 0; x
< m_list
.GetSize(); x
++)
69 m_index
.AddUnsorted((INT_PTR
)m_list
.Get(x
),x
);
74 void Swap(int index1
, int index2
)
77 if (index1
!= index2
&&
78 WDL_NORMALLY(index1
>=0) &&
79 WDL_NORMALLY(index1
<m_list
.GetSize()) &&
80 WDL_NORMALLY(index2
>=0) &&
81 WDL_NORMALLY(index2
<m_list
.GetSize()))
83 T
**list
= m_list
.GetList();
88 m_index
.Insert((INT_PTR
)a
,index2
);
89 m_index
.Insert((INT_PTR
)b
,index1
);
92 void Insert(int index
, T
*p
)
95 if (!WDL_NORMALLY(p
)) return;
96 const int listsz
= m_list
.GetSize();
97 if (index
< 0) index
=0;
98 if (index
>= listsz
) { Add(p
); return; }
100 const int indexsz
= m_index
.GetSize();
101 WDL_ASSERT(listsz
==indexsz
);
103 for (int x
=0;x
<indexsz
;x
++)
105 int *val
= m_index
.EnumeratePtr(x
);
106 if (WDL_NORMALLY(val
))
108 if (*val
>= index
) (*val
)++;
109 WDL_ASSERT(*val
!= index
);
112 m_list
.Insert(index
,p
);
113 m_index
.Insert((INT_PTR
)p
,index
);
116 WDL_PtrList
<T
> m_list
;
117 WDL_PtrKeyedArray
<int> m_index
;
119 WDL_IndexedPtrList
<T
> &operator=(const WDL_IndexedPtrList
<T
> &cp
)
126 void _checkState() const
129 const int idxsz
= m_index
.GetSize(), listsz
= m_list
.GetSize();
130 WDL_ASSERT(idxsz
== listsz
);