00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00036
00037
00038 #ifndef _LIST2_H_
00039 #define _LIST2_H_
00040
00041 #include "Mbo.h"
00042
00043 #include <list>
00044 #include <stdexcept>
00045
00046 namespace std
00047 {
00048
00069 template <class _Ty, class _Alloc = allocator<_Ty> >
00070 class list2 : public list<_Ty, _Alloc>
00071 {
00072 typedef list2<_Ty, _Alloc> _Myt;
00073 typedef list<_Ty, _Alloc> _Mybase;
00074 public:
00075
00080 virtual void limit(
00081 _IN size_type _Limit
00082 )
00083 {
00084 if (size() > _Limit)
00085 {
00086 resize(_Limit);
00087 }
00088 }
00089
00098 pair<size_type, bool> position(
00099 _IN const _Ty &_Val
00100 ) const
00101 {
00102 size_type _Index = 0;
00103 for (const_iterator iter = begin(); iter != end(); ++iter, ++_Index)
00104 {
00105 if (*iter == _Val)
00106 {
00107 return pair<size_type, bool>(_Index, true);
00108 }
00109 }
00110 return pair<size_type, bool>(-1, false);
00111 }
00112
00119 bool contains(
00120 _IN const _Ty &_Val
00121 ) const
00122 {
00123 return std::find(begin(), end(), _Val) != end();
00124 }
00125
00133 bool contains_no(
00134 _IN const _Ty &_Val
00135 ) const
00136 {
00137 return std::find(begin(), end(), _Val) == end();
00138 }
00139
00146 virtual bool add_if_new(
00147 _IN const _Ty &_Val
00148 )
00149 {
00150 if (!contains(_Val))
00151 {
00152 push_back(_Val);
00153 return true;
00154 }
00155 return false;
00156 }
00157
00162 operator bool()
00163 {
00164 return size() > 0;
00165 }
00166
00171 reference at(
00172 _IN size_type _Off
00173 )
00174 {
00175 if (size() <= _Off)
00176 {
00177 _Xran();
00178 }
00179 iterator it = begin();
00180 while(_Off--) ++it;
00181 return *it;
00182 }
00183
00188 const_reference at(
00189 _IN size_type _Off
00190 ) const
00191 {
00192 if (size() <= _Off)
00193 {
00194 _Xran();
00195 }
00196 const_iterator it = begin();
00197 while(_Off--) ++it;
00198 return *it;
00199 }
00200
00205 reference operator[] (
00206 _IN size_type _Off
00207 )
00208 {
00209 return at(_Off);
00210 }
00211
00216 const_reference operator[] (
00217 _IN size_type _Off
00218 ) const
00219 {
00220 return at(_Off);
00221 }
00222
00228 static list2<_Ty, _Alloc> __cdecl dyn_create(
00229 _IN size_type _Count,
00230 ...
00231 )
00232 {
00233 _Myt _List;
00234 va_list _Args;
00235
00236 va_start(_Args, _Count);
00237 while(_Count--)
00238 {
00239 _List.push_back(va_arg(_Args, const _Ty));
00240 }
00241 va_end(_Args);
00242 return _List;
00243 }
00244
00245 protected:
00246
00249 void _Xran() const
00250 {
00251 _THROW(out_of_range, "invalid list2<T> subscript");
00252 }
00253
00254 };
00255
00271 template < class _Ty_a, class _Alloc_a
00272 , class _Ty_b, class _Alloc_b
00273 >
00274 list2<_Ty_a, _Alloc_a> & operator += (
00275 _IN list2<_Ty_a, _Alloc_a> & container,
00276 _IN const list<_Ty_b, _Alloc_b> & values
00277 )
00278 {
00279
00280 for (list2<_Ty_b, _Alloc_b>::const_iterator it = values.begin(); it != values.end(); ++it)
00281 {
00282 container.push_back((_Ty_a)*it);
00283 }
00284 return container;
00285 };
00286
00302 template < class _Ty_a, class _Alloc_a
00303 , class _Ty_b, class _Alloc_b
00304 >
00305 list2<_Ty_a, _Alloc_a> & operator << (
00306 _IN list2<_Ty_a, _Alloc_a> & container,
00307 _IN const list<_Ty_b, _Alloc_b> & values
00308 )
00309 {
00310 container.clear();
00311 container += values;
00312 return container;
00313 };
00314
00315 };
00316
00317 #endif // _LIST2_H_