1 #ifndef __bitcache_h_11A6BC64_8CC3_447B_AD83_56BCF4FF0083__
2 #define __bitcache_h_11A6BC64_8CC3_447B_AD83_56BCF4FF0083__
8 #define _ASSERTE(x) (0)
11 #if !defined(_NO_TRANSFORM_SEQUENCES) || (!_NO_TRANSFORM_SEQUENCES)
12 #define _USE_TRANSFORM_SEQUENCES 1
14 #define _USE_TRANSFORM_SEQUENCES 0
17 #if _USE_TRANSFORM_SEQUENCES
18 #include <boost/mpl/begin_end.hpp>
19 #include <boost/mpl/next_prior.hpp>
25 typedef unsigned char BYTE;
26 typedef unsigned long long bitunit;
27 typedef std::vector<bitunit> ADVEC;
29 typedef std::vector<bitunit> SetBitVec;
31 template<
typename T>
struct LoadValVec {
typedef std::vector<T> type; };
36 template<
unsigned int x>
44 static const unsigned int Pos = 0;
48 template<
unsigned int x>
51 static const bitunit Value = (
TurnOnAllBits<(x-1)>::Value | ((bitunit)1 << (x-1)));
56 static const bitunit Value = (bitunit)1;
63 nByteSizeInBits =
sizeof(BYTE)*8,
64 nBytesPerBitUnit =
sizeof(bitunit),
65 nBitsPerBitUnit = nBytesPerBitUnit * 8,
66 nBitUnitModuloMask = nBitsPerBitUnit - 1,
75 template<
unsigned int size>
inline size_t _bitsum(
const BYTE* pb) { _ASSERTE(
"Not Implemented: _bitsum(BYTE*)" == NULL);
return -1; }
77 template<
typename T>
inline size_t bitsum(T bu) {
return _bitsum<sizeof(T)>((
const BYTE*)&bu); }
81 struct Null {
template<
typename T>
inline static T tr(T b) {
return b; } };
82 struct Invert{
template<
typename T>
inline static T tr(T b) {
return ~b; } };
83 struct And {
template<
typename T>
inline static T tr(T a, T b) {
return a & b; } };
84 struct Or {
template<
typename T>
inline static T tr(T a, T b) {
return a | b; } };
88 struct Equal {
template<
typename T1,
typename T2>
inline static bool pr(T1 t1, T2 t2) {
return t1 == t2; } };
89 struct Less {
template<
typename T1,
typename T2>
inline static bool pr(T1 t1, T2 t2) {
return t1 < t2; } };
90 struct Great {
template<
typename T1,
typename T2>
inline static bool pr(T1 t1, T2 t2) {
return t1 > t2; } };
94 struct toBitCount {
template<
typename T>
inline static size_t red(
size_t res, T bu) {
return res + bitsum(bu); } };
95 struct toMinVal {
template<
typename T>
inline static T red(T res, T bu) {
return bu < res ? bu : res; } };
96 struct toMaxVal {
template<
typename T>
inline static T red(T res, T bu) {
return bu > res ? bu : res; } };
97 struct toSum {
template<
typename T>
inline static T red(T res, T val){
return res + val; } };
100 #if _USE_TRANSFORM_SEQUENCES
103 template<
typename _tBegin,
typename _tEnd>
106 template<
typename outType,
typename inType>
109 template<
typename outType,
typename inType1,
typename inType2>
113 template<
typename _tEnd>
114 struct _s<_tEnd, _tEnd>
116 template<
typename outType,
typename inType>
117 static inline outType _apply(inType data) {
return data; }
120 template<
typename TrSeq,
typename outType,
typename inType>
123 template<
typename TrSeq,
typename outType,
typename inType1,
typename inType2>
124 inline outType _exec(inType1 in1, inType2 in2) {
return _s< typename boost::mpl::begin< TrSeq >::type,
typename boost::mpl::end< TrSeq >::type>::template _apply<outType>(in1, in2); }
132 template<
typename T1,
typename T2>
struct areTypesSame {
enum { Result =
false }; };
133 template<
typename T>
struct areTypesSame<T, T> {
enum { Result =
true }; };
135 template<
typename yesType,
typename noType,
bool cond=true>
struct select {
typedef yesType ResultType; };
136 template<
typename yesType,
typename noType>
struct select<yesType, noType, false> {
typedef noType ResultType; };
137 template<
typename outType,
typename inType>
142 template<
typename outType,
typename inType1,
typename inType2>
145 typedef typename select<outType,
148 >::ResultType ResultType;
153 template<
typename Seq,
typename outType = _detail::_unspecified_type>
struct TrSeq
155 template<
typename inType>
156 inline static typename _detail::selectValidType<outType, inType>::ResultType tr(inType b) {
return _detail::_exec<Seq, typename _detail::selectValidType<outType, inType>::ResultType>(b); }
158 template<
typename inType1,
typename inType2>
159 inline static typename _detail::selectValidType2<outType, inType1, inType2>::ResultType tr(inType1 in1, inType2 in2) {
return _detail::_exec<Seq, typename _detail::selectValidType2<outType, inType1, inType2>::ResultType>(in1, in2); }
175 #endif // _USE_TRANSFORM_SEQUENCES
177 template<
typename _Tr,
typename TOut,
typename _TIn1,
typename _TIn2>
183 typename _TIn1::const_pointer ptr1;
184 typename _TIn2::const_pointer ptr2;
185 inline TOut operator*() {
return _Tr::tr(*ptr1, *ptr2); }
186 inline const_pointer& operator++() { ++ptr1; ++ptr2;
return *
this; }
188 inline const_pointer(
const typename _TIn1::const_pointer &p1,
const typename _TIn2::const_pointer &p2) : ptr1(p1), ptr2(p2) { }
195 inline TRVEC(
const _TIn1& in1,
const _TIn1& in2) : _start(in1.data(), in2.data()), _size(in1.size()) { }
197 inline size_t size()
const {
return _size; }
199 template<
typename _Tr,
typename TOut,
typename TIn1,
typename TIn2>
200 inline TRVEC<_Tr, TOut, TIn1, TIn2> trvec(
const TIn1& in1,
const TIn2& in2) {
return TRVEC<_Tr, TOut, TIn1, TIn2>(in1, in2); }
204 template<
class _Transform,
typename _VecIn,
typename _VecOut>
205 inline void transform(
const _VecIn& in, _VecOut& out)
207 _ASSERTE(in.size() <= out.size());
209 typename _VecOut::pointer pbOut = out.data();
210 typename _VecIn::const_pointer pbIn = in.data();
211 typename _VecIn::const_pointer
const pbInLast = pbIn + in.size();
213 while(pbIn != pbInLast)
215 *pbOut++ = _Transform::tr(*pbIn++);
219 template<
class _Transform,
typename _vecIn1,
typename _vecIn2,
typename _vecOut>
220 inline void transform(
const _vecIn1& in1,
const _vecIn2& in2, _vecOut& out)
222 _ASSERTE(in1.size() <= in2.size());
223 _ASSERTE(in1.size() <= out.size());
225 typename _vecOut::pointer pbOut = out.data();
226 typename _vecIn1::const_pointer pbIn1 = in1.data();
227 typename _vecIn2::const_pointer pbIn2 = in2.data();
228 typename _vecIn1::const_pointer
const pbIn1Last = pbIn1 + in1.size();
230 while(pbIn1 != pbIn1Last)
232 *pbOut++ = _Transform::tr(*pbIn1++, *pbIn2++);
236 template<
class _Tr1,
class _Tr2,
class _BinTr,
typename _vecT1,
typename _vecT2,
typename _vecOut>
237 inline void zip(
const _vecT1& in1,
const _vecT2& in2, _vecOut& out)
239 _ASSERTE(in1.size() == in2.size() == out.size());
241 typename _vecOut::pointer pbOut = out.data();
242 typename _vecT1::const_pointer pbIn1 = in1.data();
243 typename _vecT2::const_pointer pbIn2 = in2.data();
245 for(
size_t i=0, nMax = in1.size(); i < nMax; ++i)
247 *pbOut++ = _BinTr::tr(_Tr1::tr(*pbIn1++), _Tr2::tr(*pbIn2++));
251 template<
class _Red,
class _Tr,
typename Result,
typename _vecT>
252 inline Result reduce(
const _vecT& in,
const Result& init = 0)
255 typename _vecT::const_pointer pbIn = in.data();
256 typename _vecT::const_pointer
const pbInLast = pbIn + in.size();
258 while(pbIn != pbInLast)
259 res = _Red::red(res, _Tr::tr(*pbIn++));
263 template<
class _Red,
class _Tr1,
class _Tr2,
typename Result,
typename _vecT1,
typename _vecT2>
264 inline Result reduce(
const _vecT1& vec1,
const _vecT2& vec2,
const Result& init=0)
266 _ASSERTE(vec1.size() == vec2.size());
269 typename _vecT1::const_pointer pbVec1 = vec1.data();
270 typename _vecT2::const_pointer pbVec2 = vec2.data();
272 for(
size_t i=0, nMax = vec1.size(); i < nMax; ++i)
273 res = _Red::red(res, _Tr1::tr(*pbVec1++), _Tr2::tr(*pbVec2++));
277 template<
class _Transform,
class _Red,
typename Result,
typename _vecT1,
typename _vecT2>
278 inline Result transform_reduce(
const _vecT1& vec1,
const _vecT2& vec2,
const Result& init=0)
281 typename _vecT1::const_pointer pbVec1 = vec1.data();
282 typename _vecT2::const_pointer pbVec2 = vec2.data();
284 for(
size_t i=0, nMax = vec1.size(); i < nMax; ++i)
285 res = _Red::red(res, _Transform::tr(*pbVec1++, *pbVec2++));
289 template<
class _Transform,
class _Red,
typename Result,
typename _vecT1,
typename _vecT2,
typename _vecOut>
290 inline Result transform_reduce(
const _vecT1& vec1,
const _vecT2& vec2, _vecOut& vecOut,
const Result& init=0)
293 typename _vecT1::const_pointer pbVec1 = vec1.data();
294 typename _vecT2::const_pointer pbVec2 = vec2.data();
295 typename _vecOut::pointer pvecOut = vecOut.data();
297 for(
size_t i=0, nMax = vec1.size(); i < nMax; ++i)
298 res = _Red::red(res, *pvecOut++ = _Transform::tr(*pbVec1++, *pbVec2++));
302 template<
class _Tr1,
class _Tr2,
class _BinTr,
class _Red,
typename Result,
typename _vecT1,
typename _vecT2,
typename _vecOut>
303 inline Result zip_reduce(
const _vecT1& in1,
const _vecT2& in2, _vecOut& out,
const Result& init=0)
305 _ASSERTE(in1.size() == in2.size() == out.size());
308 typename _vecOut::pointer pbOut = out.data();
309 typename _vecT2::const_pointer pbIn1 = in1.data();
310 typename _vecT1::const_pointer pbIn2 = in2.data();
312 for(
size_t i=0, nMax = in1.size(); i < nMax; ++i)
313 res = _Red::red(res, *pbOut++ = _BinTr::tr(_Tr1::tr(*pbIn1++), _Tr2::tr(*pbIn2++)));
317 template<
class _Tr1,
class _Tr2,
class _Pred,
typename _vecT1,
typename _vecT2>
318 inline bool comp(
const _vecT1& vec1,
const _vecT2& vec2)
320 typename _vecT1::const_pointer pbVec1 = vec1.data();
321 typename _vecT2::const_pointer pbVec2 = vec2.data();
322 const size_t nMax = vec1.size();
324 while(i < nMax && _Pred::pr(_Tr1::tr(*pbVec1++), _Tr2::tr(*pbVec2++))) ++i;
328 template<
class _Tr1,
class _Tr2,
class _Pred,
typename _vecT1,
typename _vecT2,
typename TOut>
329 inline bool comp_set(
const _vecT1& vec1,
const _vecT2& vec2, TOut& out)
331 typename _vecT1::const_pointer pbVec1 = vec1.data();
332 typename _vecT2::const_pointer pbVec2 = vec2.data();
333 const size_t nMax = vec1.size();
335 while(i < nMax && _Pred::pr(_Tr1::tr(*pbVec1++), _Tr2::tr(*pbVec2++), out)) ++i;
339 template<
typename vecT,
typename Result,
typename _FindFn>
340 inline bool scan(
const vecT& in, Result& res, _FindFn _found)
342 typename vecT::const_pointer pVec = in.data();
343 for(
size_t i=0, nMax = in.size(); i < nMax; ++i)
345 if(_found(res = *pVec++)) {
return true; }
350 template<
typename _Red,
typename vecT,
typename Result,
typename _FindFn>
351 inline Result scan_reduce(
const vecT& in, Result& res, _FindFn _found)
353 typename vecT::const_pointer pVec = in.data();
354 for(
size_t i=0, nMax = in.size(); i < nMax; ++i, ++pVec)
356 const typename vecT::value_type val = *pVec;
357 if(_found(val)) {
return res; }
358 res = _Red::red(res, val);
363 typedef signed char BITID;
364 inline const BITID* GetOnBitPositions(BYTE b)
366 static const BITID OnBitPositions[256][9] = {
367 {-1,-1,-1,-1,-1,-1,-1,-1,-1,},
368 {0, -1,-1,-1,-1,-1,-1,-1,-1,},
369 {1, -1,-1,-1,-1,-1,-1,-1,-1,},
370 {0, 1, -1,-1,-1,-1,-1,-1,-1,},
371 {2, -1,-1,-1,-1,-1,-1,-1,-1,},
372 {0, 2, -1,-1,-1,-1,-1,-1,-1,},
373 {1, 2, -1,-1,-1,-1,-1,-1,-1,},
374 {0, 1, 2, -1,-1,-1,-1,-1,-1,},
375 {3, -1,-1,-1,-1,-1,-1,-1,-1,},
376 {0, 3, -1,-1,-1,-1,-1,-1,-1,},
377 {1, 3, -1,-1,-1,-1,-1,-1,-1,},
378 {0, 1, 3, -1,-1,-1,-1,-1,-1,},
379 {2, 3, -1,-1,-1,-1,-1,-1,-1,},
380 {0, 2, 3, -1,-1,-1,-1,-1,-1,},
381 {1, 2, 3, -1,-1,-1,-1,-1,-1,},
382 {0, 1, 2, 3, -1,-1,-1,-1,-1,},
383 {4, -1,-1,-1,-1,-1,-1,-1,-1,},
384 {0, 4, -1,-1,-1,-1,-1,-1,-1,},
385 {1, 4, -1,-1,-1,-1,-1,-1,-1,},
386 {0, 1, 4, -1,-1,-1,-1,-1,-1,},
387 {2, 4, -1,-1,-1,-1,-1,-1,-1,},
388 {0, 2, 4, -1,-1,-1,-1,-1,-1,},
389 {1, 2, 4, -1,-1,-1,-1,-1,-1,},
390 {0, 1, 2, 4, -1,-1,-1,-1,-1,},
391 {3, 4, -1,-1,-1,-1,-1,-1,-1,},
392 {0, 3, 4, -1,-1,-1,-1,-1,-1,},
393 {1, 3, 4, -1,-1,-1,-1,-1,-1,},
394 {0, 1, 3, 4, -1,-1,-1,-1,-1,},
395 {2, 3, 4, -1,-1,-1,-1,-1,-1,},
396 {0, 2, 3, 4, -1,-1,-1,-1,-1,},
397 {1, 2, 3, 4, -1,-1,-1,-1,-1,},
398 {0, 1, 2, 3, 4, -1,-1,-1,-1,},
399 {5, -1,-1,-1,-1,-1,-1,-1,-1,},
400 {0, 5, -1,-1,-1,-1,-1,-1,-1,},
401 {1, 5, -1,-1,-1,-1,-1,-1,-1,},
402 {0, 1, 5, -1,-1,-1,-1,-1,-1,},
403 {2, 5, -1,-1,-1,-1,-1,-1,-1,},
404 {0, 2, 5, -1,-1,-1,-1,-1,-1,},
405 {1, 2, 5, -1,-1,-1,-1,-1,-1,},
406 {0, 1, 2, 5, -1,-1,-1,-1,-1,},
407 {3, 5, -1,-1,-1,-1,-1,-1,-1,},
408 {0, 3, 5, -1,-1,-1,-1,-1,-1,},
409 {1, 3, 5, -1,-1,-1,-1,-1,-1,},
410 {0, 1, 3, 5, -1,-1,-1,-1,-1,},
411 {2, 3, 5, -1,-1,-1,-1,-1,-1,},
412 {0, 2, 3, 5, -1,-1,-1,-1,-1,},
413 {1, 2, 3, 5, -1,-1,-1,-1,-1,},
414 {0, 1, 2, 3, 5, -1,-1,-1,-1,},
415 {4, 5, -1,-1,-1,-1,-1,-1,-1,},
416 {0, 4, 5, -1,-1,-1,-1,-1,-1,},
417 {1, 4, 5, -1,-1,-1,-1,-1,-1,},
418 {0, 1, 4, 5, -1,-1,-1,-1,-1,},
419 {2, 4, 5, -1,-1,-1,-1,-1,-1,},
420 {0, 2, 4, 5, -1,-1,-1,-1,-1,},
421 {1, 2, 4, 5, -1,-1,-1,-1,-1,},
422 {0, 1, 2, 4, 5, -1,-1,-1,-1,},
423 {3, 4, 5, -1,-1,-1,-1,-1,-1,},
424 {0, 3, 4, 5, -1,-1,-1,-1,-1,},
425 {1, 3, 4, 5, -1,-1,-1,-1,-1,},
426 {0, 1, 3, 4, 5, -1,-1,-1,-1,},
427 {2, 3, 4, 5, -1,-1,-1,-1,-1,},
428 {0, 2, 3, 4, 5, -1,-1,-1,-1,},
429 {1, 2, 3, 4, 5, -1,-1,-1,-1,},
430 {0, 1, 2, 3, 4, 5, -1,-1,-1,},
431 {6, -1,-1,-1,-1,-1,-1,-1,-1,},
432 {0, 6, -1,-1,-1,-1,-1,-1,-1,},
433 {1, 6, -1,-1,-1,-1,-1,-1,-1,},
434 {0, 1, 6, -1,-1,-1,-1,-1,-1,},
435 {2, 6, -1,-1,-1,-1,-1,-1,-1,},
436 {0, 2, 6, -1,-1,-1,-1,-1,-1,},
437 {1, 2, 6, -1,-1,-1,-1,-1,-1,},
438 {0, 1, 2, 6, -1,-1,-1,-1,-1,},
439 {3, 6, -1,-1,-1,-1,-1,-1,-1,},
440 {0, 3, 6, -1,-1,-1,-1,-1,-1,},
441 {1, 3, 6, -1,-1,-1,-1,-1,-1,},
442 {0, 1, 3, 6, -1,-1,-1,-1,-1,},
443 {2, 3, 6, -1,-1,-1,-1,-1,-1,},
444 {0, 2, 3, 6, -1,-1,-1,-1,-1,},
445 {1, 2, 3, 6, -1,-1,-1,-1,-1,},
446 {0, 1, 2, 3, 6, -1,-1,-1,-1,},
447 {4, 6, -1,-1,-1,-1,-1,-1,-1,},
448 {0, 4, 6, -1,-1,-1,-1,-1,-1,},
449 {1, 4, 6, -1,-1,-1,-1,-1,-1,},
450 {0, 1, 4, 6, -1,-1,-1,-1,-1,},
451 {2, 4, 6, -1,-1,-1,-1,-1,-1,},
452 {0, 2, 4, 6, -1,-1,-1,-1,-1,},
453 {1, 2, 4, 6, -1,-1,-1,-1,-1,},
454 {0, 1, 2, 4, 6, -1,-1,-1,-1,},
455 {3, 4, 6, -1,-1,-1,-1,-1,-1,},
456 {0, 3, 4, 6, -1,-1,-1,-1,-1,},
457 {1, 3, 4, 6, -1,-1,-1,-1,-1,},
458 {0, 1, 3, 4, 6, -1,-1,-1,-1,},
459 {2, 3, 4, 6, -1,-1,-1,-1,-1,},
460 {0, 2, 3, 4, 6, -1,-1,-1,-1,},
461 {1, 2, 3, 4, 6, -1,-1,-1,-1,},
462 {0, 1, 2, 3, 4, 6, -1,-1,-1,},
463 {5, 6, -1,-1,-1,-1,-1,-1,-1,},
464 {0, 5, 6, -1,-1,-1,-1,-1,-1,},
465 {1, 5, 6, -1,-1,-1,-1,-1,-1,},
466 {0, 1, 5, 6, -1,-1,-1,-1,-1,},
467 {2, 5, 6, -1,-1,-1,-1,-1,-1,},
468 {0, 2, 5, 6, -1,-1,-1,-1,-1,},
469 {1, 2, 5, 6, -1,-1,-1,-1,-1,},
470 {0, 1, 2, 5, 6, -1,-1,-1,-1,},
471 {3, 5, 6, -1,-1,-1,-1,-1,-1,},
472 {0, 3, 5, 6, -1,-1,-1,-1,-1,},
473 {1, 3, 5, 6, -1,-1,-1,-1,-1,},
474 {0, 1, 3, 5, 6, -1,-1,-1,-1,},
475 {2, 3, 5, 6, -1,-1,-1,-1,-1,},
476 {0, 2, 3, 5, 6, -1,-1,-1,-1,},
477 {1, 2, 3, 5, 6, -1,-1,-1,-1,},
478 {0, 1, 2, 3, 5, 6, -1,-1,-1,},
479 {4, 5, 6, -1,-1,-1,-1,-1,-1,},
480 {0, 4, 5, 6, -1,-1,-1,-1,-1,},
481 {1, 4, 5, 6, -1,-1,-1,-1,-1,},
482 {0, 1, 4, 5, 6, -1,-1,-1,-1,},
483 {2, 4, 5, 6, -1,-1,-1,-1,-1,},
484 {0, 2, 4, 5, 6, -1,-1,-1,-1,},
485 {1, 2, 4, 5, 6, -1,-1,-1,-1,},
486 {0, 1, 2, 4, 5, 6, -1,-1,-1,},
487 {3, 4, 5, 6, -1,-1,-1,-1,-1,},
488 {0, 3, 4, 5, 6, -1,-1,-1,-1,},
489 {1, 3, 4, 5, 6, -1,-1,-1,-1,},
490 {0, 1, 3, 4, 5, 6, -1,-1,-1,},
491 {2, 3, 4, 5, 6, -1,-1,-1,-1,},
492 {0, 2, 3, 4, 5, 6, -1,-1,-1,},
493 {1, 2, 3, 4, 5, 6, -1,-1,-1,},
494 {0, 1, 2, 3, 4, 5, 6, -1,-1,},
495 {7, -1,-1,-1,-1,-1,-1,-1,-1,},
496 {0, 7, -1,-1,-1,-1,-1,-1,-1,},
497 {1, 7, -1,-1,-1,-1,-1,-1,-1,},
498 {0, 1, 7, -1,-1,-1,-1,-1,-1,},
499 {2, 7, -1,-1,-1,-1,-1,-1,-1,},
500 {0, 2, 7, -1,-1,-1,-1,-1,-1,},
501 {1, 2, 7, -1,-1,-1,-1,-1,-1,},
502 {0, 1, 2, 7, -1,-1,-1,-1,-1,},
503 {3, 7, -1,-1,-1,-1,-1,-1,-1,},
504 {0, 3, 7, -1,-1,-1,-1,-1,-1,},
505 {1, 3, 7, -1,-1,-1,-1,-1,-1,},
506 {0, 1, 3, 7, -1,-1,-1,-1,-1,},
507 {2, 3, 7, -1,-1,-1,-1,-1,-1,},
508 {0, 2, 3, 7, -1,-1,-1,-1,-1,},
509 {1, 2, 3, 7, -1,-1,-1,-1,-1,},
510 {0, 1, 2, 3, 7, -1,-1,-1,-1,},
511 {4, 7, -1,-1,-1,-1,-1,-1,-1,},
512 {0, 4, 7, -1,-1,-1,-1,-1,-1,},
513 {1, 4, 7, -1,-1,-1,-1,-1,-1,},
514 {0, 1, 4, 7, -1,-1,-1,-1,-1,},
515 {2, 4, 7, -1,-1,-1,-1,-1,-1,},
516 {0, 2, 4, 7, -1,-1,-1,-1,-1,},
517 {1, 2, 4, 7, -1,-1,-1,-1,-1,},
518 {0, 1, 2, 4, 7, -1,-1,-1,-1,},
519 {3, 4, 7, -1,-1,-1,-1,-1,-1,},
520 {0, 3, 4, 7, -1,-1,-1,-1,-1,},
521 {1, 3, 4, 7, -1,-1,-1,-1,-1,},
522 {0, 1, 3, 4, 7, -1,-1,-1,-1,},
523 {2, 3, 4, 7, -1,-1,-1,-1,-1,},
524 {0, 2, 3, 4, 7, -1,-1,-1,-1,},
525 {1, 2, 3, 4, 7, -1,-1,-1,-1,},
526 {0, 1, 2, 3, 4, 7, -1,-1,-1,},
527 {5, 7, -1,-1,-1,-1,-1,-1,-1,},
528 {0, 5, 7, -1,-1,-1,-1,-1,-1,},
529 {1, 5, 7, -1,-1,-1,-1,-1,-1,},
530 {0, 1, 5, 7, -1,-1,-1,-1,-1,},
531 {2, 5, 7, -1,-1,-1,-1,-1,-1,},
532 {0, 2, 5, 7, -1,-1,-1,-1,-1,},
533 {1, 2, 5, 7, -1,-1,-1,-1,-1,},
534 {0, 1, 2, 5, 7, -1,-1,-1,-1,},
535 {3, 5, 7, -1,-1,-1,-1,-1,-1,},
536 {0, 3, 5, 7, -1,-1,-1,-1,-1,},
537 {1, 3, 5, 7, -1,-1,-1,-1,-1,},
538 {0, 1, 3, 5, 7, -1,-1,-1,-1,},
539 {2, 3, 5, 7, -1,-1,-1,-1,-1,},
540 {0, 2, 3, 5, 7, -1,-1,-1,-1,},
541 {1, 2, 3, 5, 7, -1,-1,-1,-1,},
542 {0, 1, 2, 3, 5, 7, -1,-1,-1,},
543 {4, 5, 7, -1,-1,-1,-1,-1,-1,},
544 {0, 4, 5, 7, -1,-1,-1,-1,-1,},
545 {1, 4, 5, 7, -1,-1,-1,-1,-1,},
546 {0, 1, 4, 5, 7, -1,-1,-1,-1,},
547 {2, 4, 5, 7, -1,-1,-1,-1,-1,},
548 {0, 2, 4, 5, 7, -1,-1,-1,-1,},
549 {1, 2, 4, 5, 7, -1,-1,-1,-1,},
550 {0, 1, 2, 4, 5, 7, -1,-1,-1,},
551 {3, 4, 5, 7, -1,-1,-1,-1,-1,},
552 {0, 3, 4, 5, 7, -1,-1,-1,-1,},
553 {1, 3, 4, 5, 7, -1,-1,-1,-1,},
554 {0, 1, 3, 4, 5, 7, -1,-1,-1,},
555 {2, 3, 4, 5, 7, -1,-1,-1,-1,},
556 {0, 2, 3, 4, 5, 7, -1,-1,-1,},
557 {1, 2, 3, 4, 5, 7, -1,-1,-1,},
558 {0, 1, 2, 3, 4, 5, 7, -1,-1,},
559 {6, 7, -1,-1,-1,-1,-1,-1,-1,},
560 {0, 6, 7, -1,-1,-1,-1,-1,-1,},
561 {1, 6, 7, -1,-1,-1,-1,-1,-1,},
562 {0, 1, 6, 7, -1,-1,-1,-1,-1,},
563 {2, 6, 7, -1,-1,-1,-1,-1,-1,},
564 {0, 2, 6, 7, -1,-1,-1,-1,-1,},
565 {1, 2, 6, 7, -1,-1,-1,-1,-1,},
566 {0, 1, 2, 6, 7, -1,-1,-1,-1,},
567 {3, 6, 7, -1,-1,-1,-1,-1,-1,},
568 {0, 3, 6, 7, -1,-1,-1,-1,-1,},
569 {1, 3, 6, 7, -1,-1,-1,-1,-1,},
570 {0, 1, 3, 6, 7, -1,-1,-1,-1,},
571 {2, 3, 6, 7, -1,-1,-1,-1,-1,},
572 {0, 2, 3, 6, 7, -1,-1,-1,-1,},
573 {1, 2, 3, 6, 7, -1,-1,-1,-1,},
574 {0, 1, 2, 3, 6, 7, -1,-1,-1,},
575 {4, 6, 7, -1,-1,-1,-1,-1,-1,},
576 {0, 4, 6, 7, -1,-1,-1,-1,-1,},
577 {1, 4, 6, 7, -1,-1,-1,-1,-1,},
578 {0, 1, 4, 6, 7, -1,-1,-1,-1,},
579 {2, 4, 6, 7, -1,-1,-1,-1,-1,},
580 {0, 2, 4, 6, 7, -1,-1,-1,-1,},
581 {1, 2, 4, 6, 7, -1,-1,-1,-1,},
582 {0, 1, 2, 4, 6, 7, -1,-1,-1,},
583 {3, 4, 6, 7, -1,-1,-1,-1,-1,},
584 {0, 3, 4, 6, 7, -1,-1,-1,-1,},
585 {1, 3, 4, 6, 7, -1,-1,-1,-1,},
586 {0, 1, 3, 4, 6, 7, -1,-1,-1,},
587 {2, 3, 4, 6, 7, -1,-1,-1,-1,},
588 {0, 2, 3, 4, 6, 7, -1,-1,-1,},
589 {1, 2, 3, 4, 6, 7, -1,-1,-1,},
590 {0, 1, 2, 3, 4, 6, 7, -1,-1,},
591 {5, 6, 7, -1,-1,-1,-1,-1,-1,},
592 {0, 5, 6, 7, -1,-1,-1,-1,-1,},
593 {1, 5, 6, 7, -1,-1,-1,-1,-1,},
594 {0, 1, 5, 6, 7, -1,-1,-1,-1,},
595 {2, 5, 6, 7, -1,-1,-1,-1,-1,},
596 {0, 2, 5, 6, 7, -1,-1,-1,-1,},
597 {1, 2, 5, 6, 7, -1,-1,-1,-1,},
598 {0, 1, 2, 5, 6, 7, -1,-1,-1,},
599 {3, 5, 6, 7, -1,-1,-1,-1,-1,},
600 {0, 3, 5, 6, 7, -1,-1,-1,-1,},
601 {1, 3, 5, 6, 7, -1,-1,-1,-1,},
602 {0, 1, 3, 5, 6, 7, -1,-1,-1,},
603 {2, 3, 5, 6, 7, -1,-1,-1,-1,},
604 {0, 2, 3, 5, 6, 7, -1,-1,-1,},
605 {1, 2, 3, 5, 6, 7, -1,-1,-1,},
606 {0, 1, 2, 3, 5, 6, 7, -1,-1,},
607 {4, 5, 6, 7, -1,-1,-1,-1,-1,},
608 {0, 4, 5, 6, 7, -1,-1,-1,-1,},
609 {1, 4, 5, 6, 7, -1,-1,-1,-1,},
610 {0, 1, 4, 5, 6, 7, -1,-1,-1,},
611 {2, 4, 5, 6, 7, -1,-1,-1,-1,},
612 {0, 2, 4, 5, 6, 7, -1,-1,-1,},
613 {1, 2, 4, 5, 6, 7, -1,-1,-1,},
614 {0, 1, 2, 4, 5, 6, 7, -1,-1,},
615 {3, 4, 5, 6, 7, -1,-1,-1,-1,},
616 {0, 3, 4, 5, 6, 7, -1,-1,-1,},
617 {1, 3, 4, 5, 6, 7, -1,-1,-1,},
618 {0, 1, 3, 4, 5, 6, 7, -1,-1,},
619 {2, 3, 4, 5, 6, 7, -1,-1,-1,},
620 {0, 2, 3, 4, 5, 6, 7, -1,-1,},
621 {1, 2, 3, 4, 5, 6, 7, -1,-1,},
622 {0, 1, 2, 3, 4, 5, 6, 7, -1,},
624 return OnBitPositions[b];
627 template<
class _Transform,
class _Fn>
628 inline void for_each_bit_uncond(
const ADVEC& advec, _Fn _op)
630 const bitunit* pBitUnit = &advec[0];
631 const BYTE* pByte = (BYTE*)pBitUnit;
632 for(
size_t i=0, nBitOffset=0, nByteCount = mTraits::nBytesPerBitUnit * advec.size(); i < nByteCount; ++i, ++pByte, nBitOffset += mTraits::nByteSizeInBits)
634 const BITID* pBitID = GetOnBitPositions(_Transform::tr(*pByte));
637 _op((
size_t)*pBitID + nBitOffset);
643 template<
class _Transform,
class _Fn>
644 inline void for_each_bit_uncond(
const ADVEC& advec1,
const ADVEC& advec2, _Fn _op)
646 _ASSERTE(advec1.size() <= advec2.size());
647 const bitunit* pBitUnit1 = &advec1[0];
const BYTE* pByte1 = (BYTE*)pBitUnit1;
648 const bitunit* pBitUnit2 = &advec2[0];
const BYTE* pByte2 = (BYTE*)pBitUnit2;
649 for(
size_t i=0, nBitOffset=0, nByteCount = mTraits::nBytesPerBitUnit * advec1.size(); i < nByteCount; ++i, ++pByte1, ++pByte2, nBitOffset += mTraits::nByteSizeInBits)
651 const BITID* pBitID = GetOnBitPositions(_Transform::tr(*pByte1, *pByte2));
654 _op((
size_t)*pBitID + nBitOffset);
660 template<
class _Transform,
class _Fn>
661 inline bool for_each_setbit_while(
const ADVEC& advec, _Fn _op)
663 const bitunit* pBitUnit = &advec[0];
664 const BYTE* pByte = (BYTE*)pBitUnit;
665 for(
size_t i=0, nBitOffset=0, nByteCount = mTraits::nBytesPerBitUnit * advec.size(); i < nByteCount; ++i, ++pByte, nBitOffset += mTraits::nByteSizeInBits)
667 const BITID* pBitID = GetOnBitPositions(_Transform::tr(*pByte));
670 if(_op((
size_t)*pBitID + nBitOffset) ==
false)
return false;
677 template<
typename vecType,
class _Fn>
678 inline void for_each_unit(vecType& advec, _Fn _op)
680 typename vecType::value_type* pbu = &advec[0];
681 for(
size_t i=0, nMax = advec.size(); i < nMax; ++i)
685 template<
typename _vecIn,
typename _vecOut,
class _Fn>
686 inline void for_each_unit_output(
const _vecIn& in, _vecOut& out, _Fn _op)
688 typename _vecOut::value_type* pbuOut = &out[0];
689 const typename _vecIn::value_type* pbuIn = &in[0];
690 for(
size_t i=0, nMax=in.size(); i < nMax; ++i)
691 *pbuOut++ = _op(*pbuIn++);
694 template<
typename TLoadValVec,
class _Fn>
695 inline void for_each_pair(
const TLoadValVec& vec, _Fn _op)
697 const typename TLoadValVec::value_type* pVali = &vec[0];
698 for(
size_t i=0, nMax = vec.size(), iMax = nMax-1; i < iMax; ++i, ++pVali)
700 const typename TLoadValVec::value_type* pValj = pVali+1;
701 for(
size_t j=i+1; j < nMax; ++j)
702 _op(*pVali, *pValj++);
706 template<
typename TLoadValVec,
typename TLoadValMap,
class _Fn>
707 inline void for_each_pair_output(
const TLoadValVec& vecIn, TLoadValMap& mapOut ,_Fn _op)
709 const typename TLoadValVec::value_type* pVali = &vecIn[0];
710 for(
size_t i=0, nMax = vecIn.size(), iMax = nMax-1; i < iMax; ++i, ++pVali)
712 typename TLoadValMap::value_type::value_type* pOut = &mapOut[i][i+1];
713 const typename TLoadValVec::value_type* pValj = pVali+1;
714 for(
size_t j=i+1; j < nMax; ++j)
715 *pOut++ = _op(*pVali, *pValj++);
719 template<
typename TLoadValVec1,
typename TLoadValVec2,
class _Fn>
720 inline void for_each_pair(
const TLoadValVec1& vec1,
const TLoadValVec2& vec2, _Fn _op)
722 const typename TLoadValVec1::value_type* pVal1 = &vec1[0];
723 for(
size_t i=0, iMax = vec1.size(); i < iMax; ++i, ++pVal1)
725 const typename TLoadValVec2::value_type* pVal2 = &vec2[0];
726 for(
size_t j=0, jMax = vec2.size(); j < jMax; ++j)
727 _op(*pVal1, *pVal2++);
732 template<
typename _vecIn,
typename _vecOut>
733 inline void make_copy(
const _vecIn& in, _vecOut& out)
735 transform<Tr::Null>(in, out);
738 template<
typename _vecIn,
typename _vecOut>
739 inline void make_inverted_copy(
const _vecIn& in, _vecOut& out)
741 transform<Tr::Invert>(in, out);
744 template<
typename _vecT>
745 inline void invert(_vecT& in)
747 make_inverted_copy(in, in);
750 template<
typename _vecT1,
typename _vecT2>
751 bool is_equal(
const _vecT1& vec1,
const _vecT2& vec2)
753 return comp<Tr::Null, Tr::Null, Pr::Equal>(vec1, vec2);
756 template<
class _Fn,
typename _vecT>
757 inline void for_each_setbit(
const _vecT& advec, _Fn _op)
759 for_each_bit_uncond<Tr::Null>(advec, _op);
762 template<
class _Fn,
typename _vecT>
763 inline void for_each_unsetbit(
const _vecT& advec, _Fn _op)
765 for_each_bit_uncond<Tr::Invert>(advec, _op);
769 inline void setbit(ADVEC& advec,
size_t nBitPos)
771 const size_t nBitUnitIndex = nBitPos >> mTraits::nBitUnitDivShiftBits;
772 const size_t nBitOffset = nBitPos & mTraits::nBitUnitModuloMask;
774 _ASSERTE((nBitPos % mTraits::nBitsPerBitUnit) == nBitOffset);
775 _ASSERTE((nBitPos / mTraits::nBitsPerBitUnit) == nBitUnitIndex);
777 advec[nBitUnitIndex] |= ((bitunit)1 << nBitOffset);
781 inline void unsetbit(ADVEC& advec,
size_t nBitPos)
783 const size_t nBitUnitIndex = nBitPos >> mTraits::nBitUnitDivShiftBits;
784 const size_t nBitOffset = nBitPos & mTraits::nBitUnitModuloMask;
786 _ASSERTE((nBitPos % mTraits::nBitsPerBitUnit) == nBitOffset);
787 _ASSERTE((nBitPos / mTraits::nBitsPerBitUnit) == nBitUnitIndex);
789 advec[nBitUnitIndex] &= ~((bitunit)1 << nBitOffset);
792 inline void setbits(ADVEC& advec,
size_t nBitPosStart,
size_t nBitPosEnd)
794 const size_t nBitUnitIndexEnd = nBitPosEnd >> mTraits::nBitUnitDivShiftBits;
795 const size_t nBitOffsetEnd = nBitPosEnd & mTraits::nBitUnitModuloMask;
796 const size_t nBitOffset = nBitPosStart & mTraits::nBitUnitModuloMask;
797 size_t nBitUnitIndex = nBitPosStart >> mTraits::nBitUnitDivShiftBits;
799 if(nBitUnitIndex == nBitUnitIndexEnd)
801 advec[nBitUnitIndex] |= (((bitunit)mTraits::nBitUnitAllBitsOn << nBitOffset) & ((bitunit)mTraits::nBitUnitAllBitsOn >> (bitunit)(mTraits::nBitsPerBitUnit-(nBitOffsetEnd+1))));
805 advec[nBitUnitIndex] |= ((bitunit)mTraits::nBitUnitAllBitsOn << nBitOffset);
807 while(++nBitUnitIndex < nBitUnitIndexEnd)
808 advec[nBitUnitIndex] |= ((bitunit)mTraits::nBitUnitAllBitsOn);
810 advec[nBitUnitIndex] |= ((bitunit)mTraits::nBitUnitAllBitsOn >> (bitunit)(mTraits::nBitsPerBitUnit-(nBitOffsetEnd+1)));
813 inline void unsetbits(ADVEC& advec,
size_t nBitPosStart,
size_t nBitPosEnd)
815 const size_t nBitUnitIndexEnd = nBitPosEnd >> mTraits::nBitUnitDivShiftBits;
816 const size_t nBitOffsetEnd = nBitPosEnd & mTraits::nBitUnitModuloMask;
817 const size_t nBitOffset = nBitPosStart & mTraits::nBitUnitModuloMask;
818 size_t nBitUnitIndex = nBitPosStart >> mTraits::nBitUnitDivShiftBits;
820 if(nBitUnitIndex == nBitUnitIndexEnd)
822 advec[nBitUnitIndex] &= ~(((bitunit)mTraits::nBitUnitAllBitsOn << nBitOffset) & ((bitunit)mTraits::nBitUnitAllBitsOn >> (bitunit)(mTraits::nBitsPerBitUnit-(nBitOffsetEnd+1))));
826 advec[nBitUnitIndex] &= ~((bitunit)mTraits::nBitUnitAllBitsOn << nBitOffset);
828 while(++nBitUnitIndex < nBitUnitIndexEnd)
829 advec[nBitUnitIndex] &= ~((bitunit)mTraits::nBitUnitAllBitsOn);
831 advec[nBitUnitIndex] &= ~((bitunit)mTraits::nBitUnitAllBitsOn >> (bitunit)(mTraits::nBitsPerBitUnit-(nBitOffsetEnd+1)));
834 inline void unsetbits(ADVEC& advec,
size_t nBitPosStart)
836 unsetbits(advec, nBitPosStart, (advec.size() * mTraits::nBitsPerBitUnit) - 1);
839 inline void setallbits(ADVEC& advec)
841 bitunit* pbu = &advec[0];
842 const bitunit*
const pbuLast = pbu + advec.size();
843 while(pbu != pbuLast) *pbu++ = (bitunit) mTraits::nBitUnitAllBitsOn;
846 inline void unsetallbits(ADVEC& advec)
848 bitunit* pbu = &advec[0];
849 const bitunit*
const pbuLast = pbu + advec.size();
850 while(pbu != pbuLast) *pbu++ = (bitunit) 0;
853 inline bool isbitset(
const ADVEC& advec,
size_t nBitPos)
855 const size_t nBitUnitIndex = nBitPos >> mTraits::nBitUnitDivShiftBits;
856 const size_t nBitOffset = nBitPos & mTraits::nBitUnitModuloMask;
858 _ASSERTE((nBitPos % mTraits::nBitsPerBitUnit) == nBitOffset);
859 _ASSERTE((nBitPos / mTraits::nBitsPerBitUnit) == nBitUnitIndex);
861 return (advec[nBitUnitIndex] & ((bitunit)1 << nBitOffset)) != 0 ;
864 inline size_t getfirstsetbit(
const ADVEC& advec)
866 const bitunit* pBitUnit = &advec[0];
868 size_t nBitOffset=0, byteIndex =0;
870 while(*pBitUnit == 0)
872 ++pBitUnit, nBitOffset += mTraits::nBitsPerBitUnit;
874 _ASSERTE(byteIndex < mTraits::nBytesPerBitUnit * advec.size());
875 byteIndex += mTraits::nBytesPerBitUnit;
879 const BYTE* pByte = (BYTE*)pBitUnit;
880 while(*pByte == 0) { ++pByte; nBitOffset += mTraits::nByteSizeInBits; }
882 const BITID* pBitID = GetOnBitPositions(*pByte); _ASSERTE(isbitset(advec, *pBitID + nBitOffset) ==
true);
883 return *pBitID + nBitOffset;
888 inline size_t bitsum(BYTE b) {
static const char *
const _Bitsperbyte =
889 "\0\1\1\2\1\2\2\3\1\2\2\3\2\3\3\4"
890 "\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
891 "\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
892 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
893 "\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
894 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
895 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
896 "\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
897 "\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
898 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
899 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
900 "\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
901 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
902 "\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
903 "\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
904 "\4\5\5\6\5\6\6\7\5\6\6\7\6\7\7\x8";
return _Bitsperbyte[b]; }
906 inline size_t _bitsum<1>(
const BYTE* pb) {
return bitsum(pb[0]); }
908 inline size_t _bitsum<2>(
const BYTE* pb) {
return bitsum(pb[0]) + bitsum(pb[1]); }
910 inline size_t _bitsum<4>(
const BYTE* pb) {
return bitsum(pb[0]) + bitsum(pb[1]) + bitsum(pb[2]) + bitsum(pb[3]); }
912 inline size_t _bitsum<8>(
const BYTE* pb) {
return bitsum(pb[0]) + bitsum(pb[1]) + bitsum(pb[2]) + bitsum(pb[3]) + bitsum(pb[4]) + bitsum(pb[5]) + bitsum(pb[6]) + bitsum(pb[7]); }
914 inline size_t _bitsum<16>(
const BYTE* pb){
return bitsum(pb[0]) + bitsum(pb[1]) + bitsum(pb[2]) + bitsum(pb[3]) + bitsum(pb[4]) + bitsum(pb[5]) + bitsum(pb[6]) + bitsum(pb[7]) + bitsum(pb[8]) + bitsum(pb[9]) + bitsum(pb[10]) + bitsum(pb[11]) + bitsum(pb[12]) + bitsum(pb[13]) + bitsum(pb[14]) + bitsum(pb[15]); }
917 inline size_t bitsum(
const std::vector<T>& advec)
919 return reduce<Red::toBitCount, Tr::Null> (advec, (size_t)0);
925 inline ADVEC gen_seq(T first, T end)
927 ADVEC seq(end - first);
928 bitunit* pbu = &seq[0];
933 template<
typename _vecType,
typename T>
934 inline void gen_seq(_vecType& vec, T first, T step=1)
936 typename _vecType::iterator iter = vec.begin();
937 typename _vecType::iterator iterEnd = vec.end();
938 for(; iter != iterEnd; ++iter, first += step)
948 inline std::string toString(
const SetBitVec& vec)
950 char numBuf[mTraits::nBitsPerBitUnit+1];
951 std::string str(
"{");
952 for_each_setbit(vec, [&](
size_t id) { sprintf(numBuf,
"%llu,", (uintptr_t)
id); str += numBuf; } );
957 inline std::string toString(
const T& obj)
959 char numBuf[mTraits::nBitsPerBitUnit+1];
960 std::string str(
"{");
961 std::for_each(obj.cbegin(), obj.cend(), [&](T::value_type val) { sprintf(numBuf,
"%llu,", (uintptr_t)val); str += numBuf; });
995 #endif // __bitcache_h_11A6BC64_8CC3_447B_AD83_56BCF4FF0083__