42 template <u
int32 SYMBOL_SIZE, u
int32 K,
typename SymbolIterator,
typename IndexType>
49 const uint32 N_SYMBOLS = 1u << SYMBOL_SIZE;
51 IndexType counters[N_SYMBOLS] = { 0u };
53 const IndexType n = end -
begin;
55 for (IndexType i = 0; i < n; ++i)
57 const IndexType i_mod_K = i & (
K-1);
63 for (
uint32 c = 0; c < N_SYMBOLS; ++c)
64 occ[ k*N_SYMBOLS + c ] = counters[c];
68 ++counters[ begin[i] ];
74 for (
uint32 i = 0; i < N_SYMBOLS; ++i)
98 op1->x += (op2 & 0xff);
99 op1->y += (op2 >> 8 & 0xff);
100 op1->z += (op2 >> 16 & 0xff);
101 op1->w += (op2 >> 24);
106 op1->x += (op2 & 0xff);
107 op1->y += (op2 >> 8 & 0xff);
108 op1->z += (op2 >> 16 & 0xff);
109 op1->w += (op2 >> 24);
129 template <u
int32 N,
typename TextString,
typename T>
131 const TextString
text,
137 for (
uint32 j = begin; j < end; ++j)
138 x += occ::popc_nbit<N>( text[j], c );
145 template <u
int32 N,
typename TextString,
typename T>
147 const TextString
text,
154 for (
uint32 j = begin; j < end; ++j)
155 x += occ::popc_nbit<N>( text[j], c );
157 return x + occ::popc_nbit<N>( text[ end ], c, i );
163 template <u
int32 N,
typename TextString,
typename T,
typename W>
165 const TextString
text,
173 for (
uint32 j = begin; j < end; ++j)
174 x += occ::popc_nbit<N>( text[j], c );
176 last_mask = text[ end ];
177 return x + occ::popc_nbit<N>( last_mask, c, i );
184 template <u
int32 SYMBOL_SIZE_T, u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable>
192 template <u
int32 SYMBOL_SIZE_T, u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable>
198 template <u
int32 SYMBOL_SIZE_T, u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable,
typename WordType,
typename OccType>
201 template <u
int32 N,
typename T>
207 static const uint32 LOG_SYMS_PER_WORD = 4;
213 static const uint32 LOG_SYMS_PER_WORD = 5;
220 static const uint32 LOG_SYMS_PER_WORD = 3;
226 static const uint32 LOG_SYMS_PER_WORD = 4;
233 static const uint32 LOG_SYMS_PER_WORD = 2;
239 static const uint32 LOG_SYMS_PER_WORD = 3;
243 template <u
int32 SYMBOL_SIZE, u
int32 K,
typename TextStorage,
typename OccIterator,
typename CountTable,
typename word_type,
typename index_type>
244 struct dispatch_rank<SYMBOL_SIZE,
K,
PackedStream<TextStorage,
uint8,SYMBOL_SIZE,true,index_type>,OccIterator,CountTable,word_type,index_type>
251 static const uint32 SYMBOL_COUNT = 1u << SYMBOL_SIZE;
261 template <
typename T>
263 const TextStorage
text,
269 const uint32 ml = (range.x - kl*
K) >> LOG_SYMS_PER_WORD;
270 const uint32 mh = (range.y - kh*
K) >> LOG_SYMS_PER_WORD;
272 const word_type l_mod = ~word_type(range.x) & (SYMS_PER_WORD-1);
273 const word_type h_mod = ~word_type(range.y) & (SYMS_PER_WORD-1);
275 const uint32 offl = kl*(
K >> LOG_SYMS_PER_WORD);
278 uint32 xl = occ::popc_nbit<SYMBOL_SIZE>(
text, c, offl, offl + ml );
281 uint32 xh = (kl == kh) ? xl : 0u;
282 uint32 startm = (kl == kh) ? ml+1 : 0u;
284 const word_type mask = text[ offl + ml ];
285 xl += occ::popc_nbit<SYMBOL_SIZE>( mask, c, l_mod );
289 xh += occ::popc_nbit<SYMBOL_SIZE>( mask, c, (mh == ml) ? h_mod : 0u );
292 if (kl != kh || mh > ml)
294 const uint32 offh = kh*(
K >> LOG_SYMS_PER_WORD);
295 xh += occ::popc_nbit<SYMBOL_SIZE>(
text, c, offh + startm, offh + mh, h_mod );
297 return make_uint2( xl, xh );
303 if (i == index_type(-1))
307 const uint32 m = (i - k*
K) >> LOG_SYMS_PER_WORD;
308 const word_type i_mod = ~word_type(i) & (SYMS_PER_WORD-1);
311 const index_type out = dict.
m_occ[ k*SYMBOL_COUNT + c ];
313 const uint32 off = k*(
K >> LOG_SYMS_PER_WORD);
316 return out + occ::popc_nbit<SYMBOL_SIZE>( dict.
m_text.stream(), c, off, off + m, i_mod );
322 if (range.x == index_type(-1) && range.y == index_type(-1))
323 return make_vector( index_type(0), index_type(0) );
326 if (range.x == index_type(-1) || range.x == range.y)
328 const index_type r = run( dict, range.y, c );
336 const index_type outl = dict.
m_occ[ kl*SYMBOL_COUNT + c ];
337 const index_type outh = (kl == kh) ? outl : dict.
m_occ[ kh*SYMBOL_COUNT + c ];
339 const uint2 r = popcN( dict.
m_text.stream(), range, kl, kh, c );
352 const uint32 m = (i - k*
K) >> LOG_SYMS_PER_WORD;
357 const uint32 off = k*(
K >> LOG_SYMS_PER_WORD);
358 const uint32 x = occ::popc_nbit<2>( dict.
m_text.stream(), dict.
m_count_table, off, off + m, ~word_type(i) & (SYMS_PER_WORD-1) );
367 *out = run4( dict, i );
389 if (SYMBOL_SIZE == 4)
392 (*out)[0] = r.x; (*out)[1] = r.y; (*out)[2] = r.z; (*out)[3] = r.w;
396 for (
uint32 s = 0; s < SYMBOL_COUNT; ++s)
397 (*out)[s] =
rank( dict, i, s );
403 if (SYMBOL_SIZE == 4)
407 run4( dict, range, &l, &h );
409 (*outl)[0] = l.x; (*outl)[1] = l.y; (*outl)[2] = l.z; (*outl)[3] = l.w;
410 (*outh)[0] = h.x; (*outh)[1] = h.y; (*outh)[2] = h.z; (*outh)[3] = h.w;
414 for (
uint32 s = 0; s < SYMBOL_COUNT; ++s)
424 template <
typename TextStorage,
typename OccIterator,
typename CountTable>
440 template <
typename T>
442 const TextStorage
text,
447 const uint32 m = (i - k*
K) >> 4u;
448 const uint32 i_16 = ~i&15;
451 const uint4 masks = text[ k ];
453 if (m > 0) x += occ::popc_nbit<2>( masks.x, c );
454 if (m > 1) x += occ::popc_nbit<2>( masks.y, c );
455 if (m > 2) x += occ::popc_nbit<2>( masks.z, c );
458 return x + occ::popc_nbit<2>(
comp( masks, m ), c, i_16 );
464 template <
typename T>
466 const TextStorage
text,
472 const uint32 ml = (range.x - kl*
K) >> 4u;
473 const uint32 mh = (range.y - kh*
K) >> 4u;
475 const uint32 l_16 = ~range.x & 15;
476 const uint32 h_16 = ~range.y & 15;
479 const uint4 masks_l = text[ kl ];
480 const uint4 masks_h = (kl == kh) ? masks_l : text[ kh ];
484 if (ml > 0) xl += occ::popc_nbit<2>( masks_l.x, c );
485 if (ml > 1) xl += occ::popc_nbit<2>( masks_l.y, c );
486 if (ml > 2) xl += occ::popc_nbit<2>( masks_l.z, c );
488 uint32 xh = (kl == kh) ? xl : 0u;
489 uint32 startm = (kl == kh) ? ml : 0u;
492 if (mh > 0 && startm == 0) xh += occ::popc_nbit<2>( masks_h.x, c );
493 if (mh > 1 && startm <= 1) xh += occ::popc_nbit<2>( masks_h.y, c );
494 if (mh > 2 && startm <= 2) xh += occ::popc_nbit<2>( masks_h.z, c );
496 xl += occ::popc_nbit<2>(
comp( masks_l, ml ), c, l_16 );
497 xh += occ::popc_nbit<2>(
comp( masks_h, mh ), c, h_16 );
498 return make_uint2( xl, xh );
507 const uint32 k = i >> LOG_K;
512 return out +
popc( dict.
m_text.stream(), i, k, c );
519 return make_uint2(0u,0u);
522 if (range.x ==
uint32(-1) || range.x == range.y)
524 const uint32 r = run( dict, range.y, c );
525 return make_uint2( range.x ==
uint32(-1) ? 0u : r, r );
528 const uint32 kl = range.x >> LOG_K;
529 const uint32 kh = range.y >> LOG_K;
535 const uint2 r = popc2( dict.
m_text.stream(), range, kl, kh, c );
537 return make_uint2( outl + r.x, outh + r.y );
542 const uint32 k = i >> LOG_K;
545 uint4 r = dict.
m_occ[k];
556 *out = run4( dict, i );
561 const uint32 kl = range.x >> LOG_K;
562 const uint32 kh = range.y >> LOG_K;
565 *outl = dict.
m_occ[kl];
566 *outh = (kl == kh) ? *outl : dict.
m_occ[kh];
578 *out = run4( dict, i );
583 run4( dict, range, &outl->
data, &outh->
data );
588 template <u
int32 SYMBOL_SIZE_T, u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable,
typename IndexType>
592 typedef typename TextString::storage_type word_type;
593 typedef typename std::iterator_traits<OccIterator>::value_type occ_type;
600 template <u
int32 SYMBOL_SIZE_T, u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable,
typename IndexType>
604 typedef typename TextString::storage_type word_type;
605 typedef typename std::iterator_traits<OccIterator>::value_type occ_type;
612 template <u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable,
typename IndexType>
617 typedef typename TextString::storage_type word_type;
618 typedef typename std::iterator_traits<OccIterator>::value_type occ_type;
625 template <u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable>
630 typedef typename TextString::storage_type word_type;
631 typedef typename std::iterator_traits<OccIterator>::value_type occ_type;
634 dict, range, outl, outh );
638 template <u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable>
643 typedef typename TextString::storage_type word_type;
644 typedef typename std::iterator_traits<OccIterator>::value_type occ_type;
647 dict, range, outl, outh );
651 template <u
int32 SYMBOL_SIZE, u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable>
653 typename rank_dictionary<SYMBOL_SIZE,K,TextString,OccIterator,CountTable>::vector_type
658 typedef typename TextString::storage_type word_type;
659 typedef typename std::iterator_traits<OccIterator>::value_type occ_type;
670 template <u
int32 SYMBOL_SIZE, u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable>
677 typedef typename TextString::storage_type word_type;
678 typedef typename std::iterator_traits<OccIterator>::value_type occ_type;
685 template <u
int32 SYMBOL_SIZE, u
int32 K,
typename TextString,
typename OccIterator,
typename CountTable>
693 typedef typename TextString::storage_type word_type;
694 typedef typename std::iterator_traits<OccIterator>::value_type occ_type;
697 dict, range, outl, outh );