00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef _CSP_KERNEL_Globals_H
00031 #define _CSP_KERNEL_Globals_H
00032
00033
00034 CSP_NAMESPACE_BEGIN(csp);
00035
00036
00037
00038
00039
00040
00041
00042
00043 extern CSP_API bool g_predictable;
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 extern CSP_API bool g_debugging;
00055
00056
00076 template<class Container>
00077 inline int
00078 size_cmp(const Container& c1, const Container& c2)
00079 {
00080 typename Container::const_iterator i1 = c1.begin();
00081 typename Container::const_iterator i2 = c2.begin();
00082 typename Container::const_iterator e1 = c1.end();
00083 typename Container::const_iterator e2 = c2.end();
00084
00085 for (;; ++i1, ++i2)
00086 if (i1 == e1)
00087 return (i2 == e2) ? 0 : -1;
00088 else
00089 if (i2 == e2)
00090 return 1;
00091
00092
00093 return 0;
00094 }
00095
00096
00105 template<class Container>
00106 inline bool
00107 size_lt(const Container& c1, const Container& c2)
00108 {
00109 return size_cmp(c1, c2) == -1;
00110 }
00111
00112
00122 template<class Container>
00123 inline bool
00124 size_le(const Container& c1, const Container& c2)
00125 {
00126 return size_cmp(c1, c2) <= 0;
00127 }
00128
00129
00137 template<class Container>
00138 inline bool
00139 size_eq(const Container& c1, const Container& c2)
00140 {
00141 return size_cmp(c1, c2) == 0;
00142 }
00143
00144
00153 template<class Container>
00154 inline bool
00155 size_ge(const Container& c1, const Container& c2)
00156 {
00157 return size_cmp(c1, c2) >= 0;
00158 }
00159
00160
00169 template<class Container>
00170 inline bool
00171 size_gt(const Container& c1, const Container& c2)
00172 {
00173 return size_cmp(c1, c2) == 1;
00174 }
00175
00176
00186 template<class Container>
00187 inline int
00188 size_cmp(const Container& c, size_t n)
00189 {
00190 size_t cnt = 0;
00191 typename Container::const_iterator i = c.begin();
00192 typename Container::const_iterator e = c.end();
00193
00194 for (; i != e; ++i)
00195 if (++cnt > n)
00196 return 1;
00197
00198 return (cnt == n) ? 0 : -1;
00199 }
00200
00201
00209 template<class Container>
00210 inline bool
00211 size_lt(const Container& c, size_t n)
00212 {
00213 return size_cmp(c, n) == -1;
00214 }
00215
00216
00225 template<class Container>
00226 inline bool
00227 size_le(const Container& c, size_t n)
00228 {
00229 return size_cmp(c, n) <= 0;
00230 }
00231
00232
00240 template<class Container>
00241 inline bool
00242 size_eq(const Container& c, size_t n)
00243 {
00244 return size_cmp(c, n) == 0;
00245 }
00246
00247
00256 template<class Container>
00257 inline bool
00258 size_ge(const Container& c, size_t n)
00259 {
00260 return size_cmp(c, n) >= 0;
00261 }
00262
00263
00271 template<class Container>
00272 inline bool
00273 size_gt(const Container& c, size_t n)
00274 {
00275 return size_cmp(c, n) == 1;
00276 }
00277
00278
00290 template<class Container>
00291 inline int
00292 size_cmp(
00293 const Container& c,
00294 size_t n,
00295 size_t& counted,
00296 typename Container::const_iterator& i)
00297 {
00298 counted = 0;
00299 i = c.begin();
00300 typename Container::const_iterator e = c.end();
00301
00302 for (; i != e; ++i)
00303 if (++counted > n)
00304 {
00305 counted--;
00306 return 1;
00307 }
00308
00309 return (counted == n) ? 0 : -1;
00310 }
00311
00312
00321 template<class C, class O>
00322 inline bool
00323 member(const C& c, const O& o)
00324 {
00325 return find(c.begin(), c.end(), &o) != c.end();
00326 }
00327
00328
00333 template<class T>
00334 inline void
00335 merge(list<T*>& dest, list<T*>& src)
00336 {
00337 if (g_predictable)
00338 dest.merge(src, id_less<T>());
00339 else
00340 dest.splice(dest.begin(), src);
00341 }
00342
00343
00344 template<class T>
00345 inline void
00346 merge(list<T*>& dest, list<T*>& src, typename list<T*>::iterator i)
00347 {
00348 if (g_predictable)
00349 {
00350 id_less<const T*> lessThan;
00351 typename list<T*>::iterator j = dest.begin();
00352 typename list<T*>::iterator e = dest.end();
00353
00354 for (; (j != e) && lessThan(*j, *i); ++j)
00355 ;
00356 dest.splice(j, src, i);
00357 }
00358 else
00359 dest.splice(dest.end(), src, i);
00360 }
00361
00362
00363 template<class T>
00364 inline void
00365 insert(list<T*>& dest, T* element)
00366 {
00367 if (g_predictable)
00368 {
00369 id_less<const T*> lessThan;
00370 typename list<T*>::iterator j = dest.begin();
00371 typename list<T*>::iterator e = dest.end();
00372
00373 for (; (j != e) && lessThan(*j, element); ++j)
00374 ;
00375 dest.insert(j, element);
00376 }
00377 else
00378 dest.push_back(element);
00379 }
00380
00381
00388 template<class T>
00389 bool
00390 isSortedById(list<T*>& c)
00391 {
00392 id_t previous = NoId;
00393 typename list<T*>::const_iterator i = c.begin();
00394 typename list<T*>::const_iterator e = c.end();
00395
00396 for (; i != e; ++i)
00397 {
00398 id_t id = (*i)->id();
00399 if (id <= previous)
00400 return false;
00401 previous = id;
00402 }
00403
00404 return true;
00405 }
00406
00407
00408
00410 template<class Container>
00411 inline wstring
00412 print(const Container& c)
00413 {
00414 wostringstream wos;
00415 typename Container::const_iterator i = c.begin();
00416 typename Container::const_iterator e = c.end();
00417
00418 for (; i != e; ++i)
00419 wos << endl << L" " << **i;
00420
00421 return wos.str();
00422 }
00423
00424
00426 template<class Container>
00427 inline wstring
00428 printIds(const Container& c)
00429 {
00430 wostringstream wos;
00431 typename Container::const_iterator i = c.begin();
00432 typename Container::const_iterator e = c.end();
00433 typename Container::const_iterator next;
00434
00435 for (; i != e; i = next)
00436 {
00437 next = i;
00438 ++next;
00439 wos << (*i)->id();
00440 if (next != e)
00441 wos << L" ";
00442 }
00443
00444 return wos.str();
00445 }
00446
00447
00448 CSP_NAMESPACE_END(csp);
00449
00450
00451 #endif // _CSP_KERNEL_Globals_H
00452
00453
00454
00455