32 template <
typename symbol_type>
42 symbol_type
operator() (
const symbol_type c)
const {
return (c >>
i) & 1u; }
49 template <
typename WaveletTreeType,
typename OccIterator>
61 const WaveletTreeType _tree,
62 const OccIterator _occ) :
78 const uint32 word =
tree.bits().stream()[ base_index ];
79 const uint32 word_occ = popc_nbit<1u>( word, 1u, ~i & 31u );
81 return base_occ + word_occ;
85 const WaveletTreeType
tree;
86 const OccIterator
occ;
128 template <
typename system_tag,
typename string_iterator,
typename index_type,
typename symbol_type>
130 const index_type string_len,
141 out_tree.
resize( string_len, symbol_size );
148 if (equal<system_tag,device_tag>())
151 thrust::copy(
string,
string + string_len, sorted_string.begin() );
155 sort_buffers.keys[1] =
raw_pointer( sorted_string ) + string_len;
160 for (
uint32 i = 0; i < symbol_size; ++i)
162 const uint32 bit = symbol_size - i - 1u;
168 thrust::device_ptr<symbol_type>( sort_buffers.current_keys() ),
170 out_tree.
bits() + string_len * i );
173 sort_enactor.
sort( string_len, sort_buffers, bit, symbol_size );
177 sorted_keys = sort_buffers.selector ?
178 sorted_string.begin() + string_len :
179 sorted_string.begin();
184 thrust::copy(
string,
string + string_len, sorted_string.begin() );
187 for (
uint32 i = 0; i < symbol_size; ++i)
189 const uint32 bit = symbol_size - i - 1u;
195 sorted_string.begin(),
197 out_tree.
bits() + string_len * i );
200 nvbio::transform<system_tag>(
202 sorted_string.begin(),
203 sorted_string.begin() + string_len,
207 thrust::sort_by_key( sorted_string.begin() + string_len, sorted_string.begin() + string_len, sorted_string.begin() );
211 sorted_keys = sorted_string.begin();
223 const uint32 n_symbols = 1u << symbol_size;
231 nvbio::upper_bound<system_tag>(
233 thrust::make_counting_iterator<index_type>(0u),
236 leaves.begin() + 1u );
252 std::stack<node_type> stack;
253 stack.push( node_type( 0u, 0u, std::make_pair( 0u, n_symbols ), std::make_pair( 0u, string_len ) ) );
260 while (!stack.empty())
263 node_type node = stack.top();
266 index_range i_range = node.i_range;
267 symbol_range s_range = node.s_range;
270 if (s_range.second - s_range.first == 1)
274 const uint32 l_node_index = node.index*2u + 1u;
275 const uint32 r_node_index = node.index*2u + 2u;
276 const uint32 s_split = (s_range.second + s_range.first)/2u;
277 const uint32 i_split = h_leaves[ s_split ];
280 h_splits[ node.index ] = index_type( i_split );
283 h_lookups[ node.index ] = index_type( i_range.first + node.level * string_len );
286 stack.push( node_type( node.level + 1u, r_node_index, std::make_pair( s_split, s_range.second ), std::make_pair( i_split, i_range.second ) ) );
287 stack.push( node_type( node.level + 1u, l_node_index, std::make_pair( s_range.first, s_split ), std::make_pair( i_range.first, i_split ) ) );
291 thrust::copy( h_splits.begin(), h_splits.begin() + n_symbols - 1u, out_tree.
splits() );
299 typedef typename bit_iterator::storage_iterator words_iterator;
301 const words_iterator words = out_tree.
bits().stream();
310 out_tree.
occ() + n_symbols,
311 thrust::plus<index_type>(),
317 out_tree.
occ() + n_symbols );
320 nvbio::transform<system_tag>(
326 out_tree.
occ()[ n_symbols ] = 0u;
332 template <
typename BitStreamIterator,
typename IndexIterator,
typename SymbolType>
336 return text( *
this, i );
341 template <
typename BitStreamIterator,
typename IndexIterator,
typename SymbolType>
346 const uint32 n_nodes = 1u << symbol_size();
351 const uint32 global_node_begin = node_begin + l * size();
354 const index_type zeros = global_node_begin - ones;
359 global_node_begin + r,
360 size() * (l+1u) - 1u );
361 const index_type global_index_mod = ~global_index & 31u;
363 const index_type base_index = global_index / 32u;
364 const index_type base_occ = b ? m_occ[ base_index + n_nodes ] :
365 base_index*32u - m_occ[ base_index + n_nodes ];
367 const uint32 word = bits().stream()[ base_index ];
368 const uint32 word_occ = popc_nbit<1u>( word, b, global_index_mod );
370 return base_occ + word_occ - offset;
380 template <
typename BitStreamIterator,
typename IndexIterator,
typename SymbolType>
394 index_type range_lo = 0u;
395 index_type range_hi = tree.
size();
399 for (
uint32 l = 0; l < symbol_size; ++l)
402 if (range_lo == range_hi)
406 const uint32 b = (c >> (symbol_size - l - 1u)) & 1u;
409 r = r ? tree.
rank( l, node, range_lo, r-1, b ) : 0u;
412 const uint32 child = node*2u + 1u;
439 template <
typename BitStreamIterator,
typename IndexIterator,
typename SymbolType>
441 typename WaveletTree<BitStreamIterator,IndexIterator,SymbolType>::range_type
448 rank( tree, range.x, c ),
449 rank( tree, range.y, c ) );
455 template <
typename BitStreamIterator,
typename IndexIterator,
typename IndexType,
typename SymbolType>
464 IndexType range_lo = 0u;
470 for (
uint32 l = 0; l < symbol_size; ++l)
473 const uint32 b = tree.
bits()[ r + range_lo + string_len*l ];
476 c |= b << (symbol_size - l - 1u);
479 r = r ? tree.
rank( l, node, range_lo, r-1, b ) : 0u;
482 const uint32 child = node*2u + 1u;
487 range_lo = tree.
splits()[ node ];