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:
- Space-efficient storage of string keys with common prefixes
- O(k) insertion, lookup, and removal where k is the key length
- Support for custom value types
- Visitor pattern for traversing all key-value pairs
- Full allocator support for value-semantic behavior
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:
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:
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:
"cards");
assert(prefix == "card");
assert(optValue.has_value() && optValue->get() == "a piece of stiff paper");
assert(prefix == "car");
assert(optValue.has_value()
&& optValue->get() == "a road vehicle with four wheels");
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 {
bsl::cout << key << ": " << value << bsl::endl;
}
};
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 collector(&found);
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 {
value += "!";
}
};
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:
bool erase(const bsl::string_view &key)
Definition ball_categorymanager_radixtree.h:1600