refactor: backport to MinGW 11

main
mandlm 2024-03-23 13:43:58 +01:00
parent 1441a869c2
commit 961a6bdf6c
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7
2 changed files with 16 additions and 4 deletions

View File

@ -8,9 +8,11 @@
class SearchTreeNode { class SearchTreeNode {
private: private:
WordRefList words_; WordRefList words_;
std::unordered_map<char, SearchTreeNode> children_; std::unordered_map<char, SearchTreeNode *> children_;
public: public:
~SearchTreeNode();
void insert(std::string_view partial_word, const std::string *original_word); void insert(std::string_view partial_word, const std::string *original_word);
const SearchTreeNode *find(std::string_view search_term) const; const SearchTreeNode *find(std::string_view search_term) const;

View File

@ -2,12 +2,22 @@
using std::string, std::string_view; using std::string, std::string_view;
SearchTreeNode::~SearchTreeNode() {
for (auto [_, child] : children_) {
delete child;
}
}
void SearchTreeNode::insert(string_view partial_word, void SearchTreeNode::insert(string_view partial_word,
const string *original_word) { const string *original_word) {
if (partial_word.empty()) { if (partial_word.empty()) {
words_.push_back(original_word); words_.push_back(original_word);
} else { } else {
children_[partial_word.front()].insert( const auto &initial = partial_word.front();
if (!children_.contains(initial)) {
children_.insert({initial, new SearchTreeNode});
}
children_[partial_word.front()]->insert(
string_view(partial_word).substr(1, partial_word.length()), string_view(partial_word).substr(1, partial_word.length()),
original_word); original_word);
} }
@ -20,7 +30,7 @@ const SearchTreeNode *SearchTreeNode::find(string_view search_term) const {
auto child = children_.find(search_term.front()); auto child = children_.find(search_term.front());
if (child != children_.cend()) { if (child != children_.cend()) {
return child->second.find(search_term.substr(1, search_term.length())); return child->second->find(search_term.substr(1, search_term.length()));
} }
return nullptr; return nullptr;
@ -29,7 +39,7 @@ const SearchTreeNode *SearchTreeNode::find(string_view search_term) const {
WordRefList SearchTreeNode::words() const { WordRefList SearchTreeNode::words() const {
WordRefList results(words_); WordRefList results(words_);
for (const auto &child : children_) { for (const auto &child : children_) {
auto child_words = child.second.words(); auto child_words = child.second->words();
std::move(child_words.begin(), child_words.end(), std::move(child_words.begin(), child_words.end(),
std::back_inserter(results)); std::back_inserter(results));
} }