-
Notifications
You must be signed in to change notification settings - Fork 725
feat(candidate): allow arbitrary per-candidate data #1092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
dba7e4c
f90a85e
1558b24
093dda0
4661ec2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,11 +41,29 @@ class Candidate { | |
| void set_end(size_t end) { end_ = end; } | ||
| void set_quality(double quality) { quality_ = quality; } | ||
|
|
||
| // per-candidate data | ||
| void set_data(const string& data_tag, an<void> datum = nullptr) { | ||
| data_[data_tag] = datum; | ||
| } | ||
| an<void> get_data(const string& data_tag) const { | ||
| auto it = data_.find(data_tag); | ||
| return it != data_.end() ? it->second : nullptr; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another proof: null pointer and absence are equal. |
||
| } | ||
| vector<string> get_data_keys() const { | ||
| vector<string> keys; | ||
|
ksqsf marked this conversation as resolved.
|
||
| keys.reserve(data_.size()); | ||
| for (const auto& pair : data_) { | ||
| keys.push_back(pair.first); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check non-null? Null pointers should be treated as absent. |
||
| } | ||
| return keys; | ||
| } | ||
|
Comment on lines
+44
to
+59
|
||
|
|
||
| private: | ||
| string type_; | ||
| size_t start_ = 0; | ||
| size_t end_ = 0; | ||
| double quality_ = 0.; | ||
| hash_map<string, an<void>> data_; | ||
| }; | ||
|
|
||
| using CandidateQueue = list<of<Candidate>>; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Using void-erased pointers for arbitrary data is unsafe and forces consumers to manage casting/lifetimes. If feasible for the codebase standard, consider std::any (or an existing project-wide any/variant) for type-safe storage while still allowing arbitrary data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
anyis safer but is not naturally nullable, andoptional<any>seems to have too many overheads.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Null pointers sounds like absence of record / deletion, as you compared it to
optional<any>.