BDE 4.39.x Production Release
Loading...
Searching...
No Matches
ball_categorymanager_radixtree

Detailed Description

Provide a space-efficient associative container for string keys.

Outline

Purpose

Provide a space-efficient associative container for string keys.

Classes

Description

This component implements ball::CategoryManager_RadixTree, a space-efficient associative container that stores key-value pairs where keys are strings (or string-like types). A radix tree (also known as a compressed trie or prefix tree) achieves space efficiency by sharing common prefixes among keys, making it particularly suitable for storing large sets of strings with common prefixes.

Features

The ball::CategoryManager_RadixTree provides the following features:

Usage

This section illustrates intended use of this component.

Example 1: Basic Usage

Suppose we want to store a mapping of words to their definitions. Using ball::CategoryManager_RadixTree allows us to efficiently store many words that share common prefixes.

First, we create a radix tree:

Definition ball_categorymanager_radixtree.h:434

Then, we insert some words and their definitions:

dictionary.emplace("car", "a road vehicle with four wheels");
dictionary.emplace("card", "a piece of stiff paper");
dictionary.emplace("care", "the provision of what is needed");
dictionary.emplace("cat", "a small domesticated carnivorous mammal");
EmplaceResult emplace(const bsl::string_view &key, Args &&... args)
Definition ball_categorymanager_radixtree.h:1577

Next, we can look up definitions:

dictionary.find("car");
assert(definition.has_value());
assert(definition->get() == "a road vehicle with four wheels");
OptValueRef find(const bsl::string_view &key)
Definition ball_categorymanager_radixtree.h:1654
Definition bslstl_optional.h:2043

We can also check if a key exists:

assert(dictionary.contains("card"));
assert(!dictionary.contains("dog"));
bool contains(const bsl::string_view &key) const
Definition ball_categorymanager_radixtree.h:1787

We can find the longest prefix of a key that exists in the tree:

bsl::string_view prefix = dictionary.findLongestCommonPrefix(&optValue,
"cards");
assert(prefix == "card");
assert(optValue.has_value() && optValue->get() == "a piece of stiff paper");
prefix = dictionary.findLongestCommonPrefix(&optValue, "carpet");
assert(prefix == "car");
assert(optValue.has_value()
&& optValue->get() == "a road vehicle with four wheels");
prefix = dictionary.findLongestCommonPrefix(&optValue, "dog");
assert(prefix == "");
assert(!optValue.has_value());
bsl::string_view findLongestCommonPrefix(OptValueRef *value, const bsl::string_view &key)
Definition ball_categorymanager_radixtree.h:1691
Definition bslstl_stringview.h:471

We can run a functor for all entries:

struct Printer {
void operator()(const bsl::string_view& key,
const bsl::string& value) const {
bsl::cout << key << ": " << value << bsl::endl;
}
};
dictionary.forEach(Printer());
void forEach(const t_FUNCTOR &functor)
Definition ball_categorymanager_radixtree.h:1713
Definition bslstl_string.h:1252

We can run a functor for all entries with a given prefix:

struct PrefixCollector {
PrefixCollector(bsl::vector<bsl::string> *vec) : d_vec_p(vec) {}
void operator()(const bsl::string_view& key,
const bsl::string& value) const {
d_vec_p->push_back(bsl::string(key));
}
};
PrefixCollector collector(&found);
dictionary.forEachPrefix("car", collector);
assert(found.size() == 3);
bool foundCar = false;
bool foundCard = false;
bool foundCare = false;
for (bsl::size_t i = 0; i < found.size(); ++i) {
if (found[i] == "car") {
foundCar = true;
} else if (found[i] == "card") {
foundCard = true;
} else if (found[i] == "care") {
foundCare = true;
}
}
assert(foundCar);
assert(foundCard);
assert(foundCare);
size_type forEachPrefix(const bsl::string_view &prefix, const t_FUNCTOR &functor)
Definition ball_categorymanager_radixtree.h:1725
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:3019
Definition bslstl_vector.h:1120
void push_back(const VALUE_TYPE &value)
Definition bslstl_vector.h:4343

We can also mutate values for all entries with a given prefix:

struct SuffixAppender {
void operator()(const bsl::string_view&, bsl::string& value) const {
value += "!";
}
};
dictionary.forEachPrefix("car", SuffixAppender());
assert(dictionary.find("car")->get() ==
"a road vehicle with four wheels!");
assert(dictionary.find("card")->get() == "a piece of stiff paper!");
assert(dictionary.find("care")->get() ==
"the provision of what is needed!");

Finally, we can remove entries:

dictionary.erase("card"); assert(!dictionary.contains("card"));
bool erase(const bsl::string_view &key)
Definition ball_categorymanager_radixtree.h:1600