Skip to content

Commit 96dffac

Browse files
authored
Merge pull request #70 from iris-cpp/fix-keyed_database
Correctly specify map-related params in `ngram::keyed_database`
2 parents a1d6094 + eb25c51 commit 96dffac

3 files changed

Lines changed: 12 additions & 9 deletions

File tree

include/iris/ngram/database.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ class database
7474
store_.clear();
7575
}
7676

77-
template<class DocumentID = document_id>
78-
bool search(this auto const& db, search_query<CharT> const& query, search_result<DocumentID>& search_res)
77+
template<class DocumentID, class HashT, class EqualT>
78+
bool search(this auto const& db, search_query<CharT> const& query, search_result<DocumentID, HashT, EqualT>& search_res)
7979
{
8080
search_res.clear();
8181
if (query.empty()) return false;

include/iris/ngram/keyed_database.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#include <iris/ngram/database.hpp>
99
#include <iris/ngram/id.hpp>
1010

11-
#include <iris/hash.hpp>
12-
11+
#include <format>
1312
#include <unordered_map>
1413
#include <stdexcept>
1514
#include <memory>
@@ -25,7 +24,7 @@ class keyed_database : private database<CharT>
2524
public:
2625
using document_id_type = KeyT;
2726

28-
template<class KeyLikeT>
27+
template<class KeyLikeT = KeyT>
2928
requires std::is_constructible_v<KeyT, KeyLikeT>
3029
void add_document(KeyLikeT&& key_like, std::basic_string_view<CharT> const doc_text)
3130
{
@@ -34,8 +33,10 @@ class keyed_database : private database<CharT>
3433
if (!inserted) {
3534
if constexpr (std::is_pointer_v<KeyT>) {
3635
throwf<std::invalid_argument>("key `{}` already exists in keyed_database", static_cast<void const*>(it->first));
37-
} else {
36+
} else if constexpr (std::formattable<KeyT, char>) {
3837
throwf<std::invalid_argument>("key `{}` already exists in keyed_database", it->first);
38+
} else {
39+
throwf<std::invalid_argument>("key `???` already exists in keyed_database");
3940
}
4041
}
4142
assert(doc_id_to_key_.size() == detail::to_index(doc_id));
@@ -48,7 +49,7 @@ class keyed_database : private database<CharT>
4849
base_type::update_document(this->get_document_id(key_like), doc_text);
4950
}
5051

51-
template<class KeyLikeT>
52+
template<class KeyLikeT = KeyT>
5253
requires std::is_constructible_v<KeyT, KeyLikeT>
5354
void add_or_update_document(KeyLikeT&& key_like, std::basic_string_view<CharT> const doc_text)
5455
{

include/iris/ngram/search_result.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ struct [[nodiscard]] search_word_match
5959
std::vector<interval<int>> spans_;
6060
};
6161

62-
template<class DocumentID = document_id>
62+
template<class DocumentID = document_id, class HashT = std::hash<DocumentID>, class EqualT = std::equal_to<>>
6363
struct [[nodiscard]] search_result
6464
{
6565
using document_id_type = DocumentID;
66-
using map_type = std::unordered_map<DocumentID, std::vector<search_word_match>>;
66+
using hash_type = HashT;
67+
using equal_type = EqualT;
68+
using map_type = std::unordered_map<DocumentID, std::vector<search_word_match>, HashT, EqualT>;
6769

6870
search_result() = default;
6971

0 commit comments

Comments
 (0)