static inline size_t handle_bucket(TSTNode<CharT>* node, unsigned char** strings, size_t pos, size_t depth) { static_assert(BucketNum < 3, "BucketNum < 3"); if (node->is_tst[BucketNum]) { pos = burst_traverse<BucketT>( static_cast<TSTNode<CharT>*>(node->buckets[BucketNum]), strings, pos, depth + (BucketNum==1)*sizeof(CharT)); } else if (node->buckets[BucketNum]) { BucketT* buck = static_cast<BucketT*>(node->buckets[BucketNum]); size_t bsize = buck->size(); std::copy(buck->begin(), buck->end(), strings+pos); delete buck; if (not is_middle_bucket(BucketNum)) { mkqsort(strings+pos, bsize, depth); } else if (not is_end(node->pivot)) { mkqsort(strings+pos, bsize, depth+sizeof(CharT)); } pos += bsize; } return pos; }
static inline void insert(TrieNode<CharT, BucketT>* root, unsigned char** strings, size_t n) { for (size_t i=0; i < n; ++i) { unsigned char* str = strings[i]; size_t depth = 0; CharT c = get_char<CharT>(str, 0); TrieNode<CharT, BucketT>* node = root; while (node->is_trie(c)) { assert(not is_end(c)); node = node->get_node(c); depth += sizeof(CharT); c = get_char<CharT>(str, depth); } BucketT* bucket = node->get_bucket(c); assert(bucket); bucket->push_back(str); if (is_end(c)) continue; if (bucket->size() > Threshold) { node->_buckets[c] = BurstImpl()(*bucket, depth+sizeof(CharT)); make_trie(node->_buckets[c]); delete bucket; } } }
static inline void burst_insert(TSTNode<CharT>* root, unsigned char** strings, size_t N) { for (size_t i=0; i < N; ++i) { unsigned char* str = strings[i]; size_t depth = 0; CharT c = get_char<CharT>(str, depth); TSTNode<CharT>* node = root; unsigned bucket = get_bucket(c, node->pivot); while (node->is_tst[bucket]) { if (is_middle_bucket(bucket)) { depth += sizeof(CharT); c = get_char<CharT>(str, depth); } node = static_cast<TSTNode<CharT>*>( node->buckets[bucket]); bucket = get_bucket(c, node->pivot); } BucketT* buck = static_cast<BucketT*>(node->buckets[bucket]); if (not buck) node->buckets[bucket] = buck = new BucketT; buck->push_back(str); if (is_middle_bucket(bucket) && is_end(node->pivot)) { continue; } if (buck->size() > sizeof(CharT)*Threshold and buck->size() == buck->capacity()) { if (is_middle_bucket(bucket)) { depth += sizeof(CharT); } CharT* oracle = static_cast<CharT*>( malloc(buck->size()*sizeof(CharT))); for (unsigned j=0; j < buck->size(); ++j) { oracle[j] = get_char<CharT>((*buck)[j], depth); } TSTNode<CharT>* new_node = BurstImpl()(*buck, oracle, depth); free(oracle); delete buck; node->buckets[bucket] = new_node; node->is_tst[bucket] = true; } } }