00001
00032 #ifndef LL_LLSTL_H
00033 #define LL_LLSTL_H
00034
00035 #include <functional>
00036 #include <algorithm>
00037 #include <map>
00038 #include <vector>
00039 #include <set>
00040 #include <deque>
00041
00042
00043
00044 template <typename T1, typename T2>
00045 struct compare_pair_first
00046 {
00047 bool operator()(const std::pair<T1, T2>& a, const std::pair<T1, T2>& b) const
00048 {
00049 return a.first < b.first;
00050 }
00051 };
00052
00053 template <typename T1, typename T2>
00054 struct compare_pair_greater
00055 {
00056 bool operator()(const std::pair<T1, T2>& a, const std::pair<T1, T2>& b) const
00057 {
00058 if (!(a.first < b.first))
00059 return true;
00060 else if (!(b.first < a.first))
00061 return false;
00062 else
00063 return !(a.second < b.second);
00064 }
00065 };
00066
00067
00068 template <typename T>
00069 struct compare_pointer_contents
00070 {
00071 typedef const T* Tptr;
00072 bool operator()(const Tptr& a, const Tptr& b) const
00073 {
00074 return *a < *b;
00075 }
00076 };
00077
00078
00079
00080
00081
00082
00083 struct DeletePointer
00084 {
00085 template<typename T> void operator()(T* ptr) const
00086 {
00087 delete ptr;
00088 }
00089 };
00090 struct DeletePointerArray
00091 {
00092 template<typename T> void operator()(T* ptr) const
00093 {
00094 delete[] ptr;
00095 }
00096 };
00097
00098
00099
00100
00101
00102
00103 struct DeletePairedPointer
00104 {
00105 template<typename T> void operator()(T &ptr) const
00106 {
00107 delete ptr.second;
00108 }
00109 };
00110 struct DeletePairedPointerArray
00111 {
00112 template<typename T> void operator()(T &ptr) const
00113 {
00114 delete[] ptr.second;
00115 }
00116 };
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 template<typename T>
00138 struct DeletePointerFunctor : public std::unary_function<T*, bool>
00139 {
00140 bool operator()(T* ptr) const
00141 {
00142 delete ptr;
00143 return true;
00144 }
00145 };
00146
00147
00148 template<typename T>
00149 struct DeleteArrayFunctor : public std::unary_function<T*, bool>
00150 {
00151 bool operator()(T* ptr) const
00152 {
00153 delete[] ptr;
00154 return true;
00155 }
00156 };
00157
00158
00159
00160
00161
00162
00163 struct CopyNewPointer
00164 {
00165 template<typename T> T* operator()(const T* ptr) const
00166 {
00167 return new T(*ptr);
00168 }
00169 };
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 template <typename K, typename T>
00180 inline T* get_ptr_in_map(const std::map<K,T*>& inmap, const K& key)
00181 {
00182
00183 typedef typename std::map<K,T*>::const_iterator map_iter;
00184 map_iter iter = inmap.find(key);
00185 if(iter == inmap.end())
00186 {
00187 return NULL;
00188 }
00189 else
00190 {
00191 return iter->second;
00192 }
00193 };
00194
00195
00196 template <typename K, typename T>
00197 inline bool is_in_map(const std::map<K,T>& inmap, const K& key)
00198 {
00199 typedef typename std::map<K,T>::const_iterator map_iter;
00200 if(inmap.find(key) == inmap.end())
00201 {
00202 return false;
00203 }
00204 else
00205 {
00206 return true;
00207 }
00208 }
00209
00210
00211
00212
00213
00214 template <typename K, typename T>
00215 inline T get_if_there(const std::map<K,T>& inmap, const K& key, T default_value)
00216 {
00217
00218 typedef typename std::map<K,T>::const_iterator map_iter;
00219 map_iter iter = inmap.find(key);
00220 if(iter == inmap.end())
00221 {
00222 return default_value;
00223 }
00224 else
00225 {
00226 return iter->second;
00227 }
00228 };
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 template <typename T, typename Iter>
00240 inline Iter vector_replace_with_last(std::vector<T>& invec, Iter iter)
00241 {
00242 typename std::vector<T>::iterator last = invec.end(); --last;
00243 if (iter == invec.end())
00244 {
00245 return iter;
00246 }
00247 else if (iter == last)
00248 {
00249 invec.pop_back();
00250 return invec.end();
00251 }
00252 else
00253 {
00254 *iter = *last;
00255 invec.pop_back();
00256 return iter;
00257 }
00258 };
00259
00260
00261
00262
00263 template <typename T>
00264 inline bool vector_replace_with_last(std::vector<T>& invec, const T& val)
00265 {
00266 typename std::vector<T>::iterator iter = std::find(invec.begin(), invec.end(), val);
00267 if (iter != invec.end())
00268 {
00269 typename std::vector<T>::iterator last = invec.end(); --last;
00270 *iter = *last;
00271 invec.pop_back();
00272 return true;
00273 }
00274 return false;
00275 }
00276
00277
00278 template <typename T>
00279 inline T* vector_append(std::vector<T>& invec, S32 N)
00280 {
00281 U32 sz = invec.size();
00282 invec.resize(sz+N);
00283 return &(invec[sz]);
00284 }
00285
00286
00287 template <class InputIter, class Size, class Function>
00288 Function ll_for_n(InputIter first, Size n, Function f)
00289 {
00290 for ( ; n > 0; --n, ++first)
00291 f(*first);
00292 return f;
00293 }
00294
00295
00296 template <class InputIter, class Size, class OutputIter>
00297 OutputIter ll_copy_n(InputIter first, Size n, OutputIter result)
00298 {
00299 for ( ; n > 0; --n, ++result, ++first)
00300 *result = *first;
00301 return result;
00302 }
00303
00304
00305 template <class InputIter, class OutputIter, class Size, class UnaryOp>
00306 OutputIter ll_transform_n(
00307 InputIter first,
00308 Size n,
00309 OutputIter result,
00310 UnaryOp op)
00311 {
00312 for ( ; n > 0; --n, ++result, ++first)
00313 *result = op(*first);
00314 return result;
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 template <class _Pair>
00350 struct _LLSelect1st : public std::unary_function<_Pair, typename _Pair::first_type> {
00351 const typename _Pair::first_type& operator()(const _Pair& __x) const {
00352 return __x.first;
00353 }
00354 };
00355
00356 template <class _Pair>
00357 struct _LLSelect2nd : public std::unary_function<_Pair, typename _Pair::second_type>
00358 {
00359 const typename _Pair::second_type& operator()(const _Pair& __x) const {
00360 return __x.second;
00361 }
00362 };
00363
00364 template <class _Pair> struct llselect1st : public _LLSelect1st<_Pair> {};
00365 template <class _Pair> struct llselect2nd : public _LLSelect2nd<_Pair> {};
00366
00367
00368
00369
00370 template <class _Operation1, class _Operation2>
00371 class ll_unary_compose :
00372 public std::unary_function<typename _Operation2::argument_type,
00373 typename _Operation1::result_type>
00374 {
00375 protected:
00376 _Operation1 __op1;
00377 _Operation2 __op2;
00378 public:
00379 ll_unary_compose(const _Operation1& __x, const _Operation2& __y)
00380 : __op1(__x), __op2(__y) {}
00381 typename _Operation1::result_type
00382 operator()(const typename _Operation2::argument_type& __x) const {
00383 return __op1(__op2(__x));
00384 }
00385 };
00386
00387 template <class _Operation1, class _Operation2>
00388 inline ll_unary_compose<_Operation1,_Operation2>
00389 llcompose1(const _Operation1& __op1, const _Operation2& __op2)
00390 {
00391 return ll_unary_compose<_Operation1,_Operation2>(__op1, __op2);
00392 }
00393
00394 template <class _Operation1, class _Operation2, class _Operation3>
00395 class ll_binary_compose
00396 : public std::unary_function<typename _Operation2::argument_type,
00397 typename _Operation1::result_type> {
00398 protected:
00399 _Operation1 _M_op1;
00400 _Operation2 _M_op2;
00401 _Operation3 _M_op3;
00402 public:
00403 ll_binary_compose(const _Operation1& __x, const _Operation2& __y,
00404 const _Operation3& __z)
00405 : _M_op1(__x), _M_op2(__y), _M_op3(__z) { }
00406 typename _Operation1::result_type
00407 operator()(const typename _Operation2::argument_type& __x) const {
00408 return _M_op1(_M_op2(__x), _M_op3(__x));
00409 }
00410 };
00411
00412 template <class _Operation1, class _Operation2, class _Operation3>
00413 inline ll_binary_compose<_Operation1, _Operation2, _Operation3>
00414 llcompose2(const _Operation1& __op1, const _Operation2& __op2,
00415 const _Operation3& __op3)
00416 {
00417 return ll_binary_compose<_Operation1,_Operation2,_Operation3>
00418 (__op1, __op2, __op3);
00419 }
00420
00421
00422
00423 template <class _Operation>
00424 class llbinder1st :
00425 public std::unary_function<typename _Operation::second_argument_type,
00426 typename _Operation::result_type> {
00427 protected:
00428 _Operation op;
00429 typename _Operation::first_argument_type value;
00430 public:
00431 llbinder1st(const _Operation& __x,
00432 const typename _Operation::first_argument_type& __y)
00433 : op(__x), value(__y) {}
00434 typename _Operation::result_type
00435 operator()(const typename _Operation::second_argument_type& __x) const {
00436 return op(value, __x);
00437 }
00438 };
00439
00440 template <class _Operation, class _Tp>
00441 inline llbinder1st<_Operation>
00442 llbind1st(const _Operation& __oper, const _Tp& __x)
00443 {
00444 typedef typename _Operation::first_argument_type _Arg1_type;
00445 return llbinder1st<_Operation>(__oper, _Arg1_type(__x));
00446 }
00447
00448 template <class _Operation>
00449 class llbinder2nd
00450 : public std::unary_function<typename _Operation::first_argument_type,
00451 typename _Operation::result_type> {
00452 protected:
00453 _Operation op;
00454 typename _Operation::second_argument_type value;
00455 public:
00456 llbinder2nd(const _Operation& __x,
00457 const typename _Operation::second_argument_type& __y)
00458 : op(__x), value(__y) {}
00459 typename _Operation::result_type
00460 operator()(const typename _Operation::first_argument_type& __x) const {
00461 return op(__x, value);
00462 }
00463 };
00464
00465 template <class _Operation, class _Tp>
00466 inline llbinder2nd<_Operation>
00467 llbind2nd(const _Operation& __oper, const _Tp& __x)
00468 {
00469 typedef typename _Operation::second_argument_type _Arg2_type;
00470 return llbinder2nd<_Operation>(__oper, _Arg2_type(__x));
00471 }
00472
00473 #endif // LL_LLSTL_H