Include upper_bound and lower_bound implementation

This commit is contained in:
2024-07-02 18:54:07 +02:00
parent c488ace719
commit d90e21ec4c
10 changed files with 782 additions and 412 deletions

View File

@@ -1,17 +1,52 @@
#include "Discretizer.h"
namespace mdlp {
// The next to templates have been taken to have the chance to customize them to match
// np.searchsorted that is used in scikit-learn KBinsDiscretizer
// Code Taken from https://cplusplus.com/reference/algorithm/upper_bound/?kw=upper_bound
template <class ForwardIterator, class T>
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it;
typename iterator_traits<ForwardIterator>::difference_type count, step;
count = std::distance(first, last);
while (count > 0) {
it = first; step = count / 2; std::advance(it, step);
if (!(val < *it)) // or: if (!comp(val,*it)), for version (2)
{
first = ++it; count -= step + 1;
} else count = step;
}
return first;
}
// Code Taken from https://cplusplus.com/reference/algorithm/lower_bound/?kw=lower_bound
template <class ForwardIterator, class T>
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it;
typename iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first, last);
while (count > 0) {
it = first; step = count / 2; advance(it, step);
if (*it < val) { // or: if (comp(*it,val)), for version (2)
first = ++it;
count -= step + 1;
} else count = step;
}
return first;
}
labels_t& Discretizer::transform(const samples_t& data)
{
discretizedData.clear();
discretizedData.reserve(data.size());
// CutPoints always have more than two items
// CutPoints always have at least two items
// Have to ignore first and last cut points provided
auto first = cutPoints.begin() + 1;
auto last = cutPoints.end() - 1;
auto bound = direction == bound_dir_t::LEFT ? my_lower_bound<std::vector<float>::iterator, float> : my_upper_bound<std::vector<float>::iterator, float>;
for (const precision_t& item : data) {
auto upper = std::lower_bound(first, last, item);
int number = upper - first;
auto pos = bound(first, last, item);
int number = pos - first;
/*
OJO
*/