-
Notifications
You must be signed in to change notification settings - Fork 7
Simplify proximity_counter by inheritance from bin_counter #125
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: main
Are you sure you want to change the base?
Changes from all commits
f979740
4289021
812b571
c35635d
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 |
|---|---|---|
|
|
@@ -210,12 +210,44 @@ CML_BinCounter::insert(double value) | |
| // Consider only values below upper edge and only if model passed sanity | ||
| // check. | ||
| if (bins_ready && value <= bins[nbin-1].bin_ceil) { | ||
| for (int ii = static_cast<int>(nbin) - 1; ii >= 0; ii--) { | ||
| const auto bin_index = static_cast<size_t>(ii); | ||
| if (value >= bins[bin_index].bin_floor) { | ||
| bins[bin_index].count++; | ||
| //ii-- is a post decrement. tests ii > 0, then decrements. | ||
| //so index ii runs from nbin - 1 to 0. | ||
| for (auto bin = bins.rbegin(), end = bins.rend(); bin != end; ++bin) { | ||
| if (value >= bin->bin_floor) { | ||
| bin->count++; | ||
| return; | ||
| } | ||
| } | ||
|
Minsoo-Kang-space marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| /***************************************************************************** | ||
| Method: apply_tolerance | ||
| Purpose: | ||
| Modifies the specified bin edges, shifting them down slightly. | ||
| The lower bin edge behaves as a closed end, so a value is in the bin if it | ||
| is >= lower_edge. To allow for numerical rounding / truncation, it may be | ||
| desirable in some circumstances to extend that boundary. | ||
| For example, if the boundaries are set at {0, 1, 2} then a value of 0.9999999 | ||
| would be binned into the lower bin. It may be desirable to include values | ||
| arbitrarily close to 1.0 into the upper bin and doing so requires lowering | ||
| the bin edge to accommodate that desired allowance. | ||
| *****************************************************************************/ | ||
| void | ||
| CML_BinCounter::apply_tolerance(double tol) | ||
|
Contributor
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. This function needs to be tested. |
||
| { | ||
| if (!bins_ready) { | ||
| CMLMessage::error( __FILE__,__LINE__, | ||
| "Cannot apply bin tolerance, bins not ready\n"); | ||
| return; | ||
| } | ||
|
|
||
| if (bins[0].bin_floor != std::numeric_limits<double>::lowest()) { | ||
| bins[0].bin_floor -= tol; | ||
| } | ||
| for (size_t ii = 0; ii < nbin; ii++) { | ||
| bins[ii].bin_ceil = | ||
| bins[ii+1].bin_floor = bins[ii].bin_ceil - tol; | ||
| } | ||
|
Comment on lines
+246
to
+252
Contributor
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. This is an out-of-bounds read when All this loop is intending to do is to decrement the floor and ceiling of bins which aren't the first or last bin by // Double-check, this probably only works if nbin >= 2
for (auto bin = std::next(bins.begin()), end = std::prev(bins.end()); bin != end; ++bin) {
bin->bin_floor -= tol;
bin->bin_ceil -= tol;
}
bins[0].bin_ceil -= tol;
bins[nbin - 1].bin_floor -= tol;
if (bins[0].bin_floor != std::numeric_limits<double>::lowest()) {
bins[0].bin_floor -= tol;
bins[nbin - 1].bin_ceil -= tol;
}Regardless, there's definitely future work here to better determine if the dataset has overflow bins or not. Comparing against the lowest double should be a static analysis warning and it's not obvious to someone reading this function why you're comparing against |
||
| } | ||
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.
This isn't necessary. This line can be removed.