| 1 |
// |
| 2 |
// $Id$ |
| 3 |
// |
| 4 |
// |
| 5 |
// Original author: Witold Wolski <[email protected]> |
| 6 |
// |
| 7 |
// Copyright : ETH Zurich |
| 8 |
// |
| 9 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 |
// you may not use this file except in compliance with the License. |
| 11 |
// You may obtain a copy of the License at |
| 12 |
// |
| 13 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
// |
| 15 |
// Unless required by applicable law or agreed to in writing, software |
| 16 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
// See the License for the specific language governing permissions and |
| 19 |
// limitations under the License. |
| 20 |
// |
| 21 |
|
| 22 |
|
| 23 |
#ifndef BIN1D_H |
| 24 |
#define BIN1D_H |
| 25 |
|
| 26 |
#include <vector> |
| 27 |
#include <algorithm> |
| 28 |
#include <functional> |
| 29 |
#include <vector> |
| 30 |
#include <utility> |
| 31 |
#include <stdexcept> |
| 32 |
#include <boost/cstdint.hpp> |
| 33 |
|
| 34 |
namespace ralab |
| 35 |
{ |
| 36 |
namespace base |
| 37 |
{ |
| 38 |
namespace resample |
| 39 |
{ |
| 40 |
typedef boost::int32_t int32_t; |
| 41 |
typedef boost::int64_t int64_t; |
| 42 |
|
| 43 |
//Class returns the index a value belongs too.... |
| 44 |
struct Bin1D |
| 45 |
{ |
| 46 |
std::vector<double> breaks_; // boundaries |
| 47 |
double * begbreaks_; |
| 48 |
double * endbreaks_; |
| 49 |
public: |
| 50 |
Bin1D():breaks_(),begbreaks_(0),endbreaks_(0){} |
| 51 |
|
| 52 |
Bin1D( |
| 53 |
std::vector<double> & breaks // breaks |
| 54 |
):breaks_(), begbreaks_(0), endbreaks_(0) |
| 55 |
{ |
| 56 |
this->setBreaks(breaks); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
Bin1D( |
| 61 |
const Bin1D & rhs // breaks |
| 62 |
):breaks_(), begbreaks_(0), endbreaks_(0) |
| 63 |
{ |
| 64 |
this->setBreaks(rhs.breaks_); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
void setBreaks(const std::vector<double> & breaks){ |
| 69 |
breaks_.assign( breaks.begin(), breaks.end() ); |
| 70 |
std::sort(breaks_.begin(),breaks_.end()); |
| 71 |
reset(); |
| 72 |
} |
| 73 |
|
| 74 |
void reset(){ |
| 75 |
begbreaks_ = &breaks_[0]; |
| 76 |
endbreaks_ = begbreaks_ + breaks_.size(); |
| 77 |
} |
| 78 |
|
| 79 |
void getBreaks(std::vector<double> & breaks) const{ |
| 80 |
breaks.assign(begbreaks_,endbreaks_); |
| 81 |
} |
| 82 |
|
| 83 |
const std::vector<double> & getBreaks() const{ |
| 84 |
return breaks_; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
bool inRange(double dat) const{ |
| 89 |
return (dat > breaks_.front() && dat < breaks_.back()); |
| 90 |
} |
| 91 |
|
| 92 |
std::size_t operator()(double dat) const |
| 93 |
{ |
| 94 |
double * it2 = std::lower_bound(begbreaks_,endbreaks_,dat); |
| 95 |
size_t ub = std::distance(begbreaks_,it2); |
| 96 |
return ub; |
| 97 |
} |
| 98 |
|
| 99 |
void operator()(double dat1, |
| 100 |
double dat2, |
| 101 |
std::vector<int32_t> & idx, |
| 102 |
std::vector<double> & dist |
| 103 |
) const |
| 104 |
{ |
| 105 |
double * it1 = std::lower_bound(begbreaks_,endbreaks_,dat1); |
| 106 |
double * it2 = std::lower_bound(begbreaks_,endbreaks_,dat2); |
| 107 |
|
| 108 |
size_t ub1 = std::distance(begbreaks_,it1); |
| 109 |
size_t ub2 = std::distance(begbreaks_,it2); |
| 110 |
int64_t n = static_cast<int64_t>(ub2-ub1)+1; |
| 111 |
idx.resize(n); |
| 112 |
dist.resize(n); |
| 113 |
|
| 114 |
if(ub1 == ub2 ){ |
| 115 |
idx[0] = ub1-1; |
| 116 |
dist[0] = dat2 - dat1; |
| 117 |
return; |
| 118 |
} |
| 119 |
else{ |
| 120 |
for(int64_t i = 0; it1 != (it2+1) ; i++, it1++){ |
| 121 |
idx[i] = ub1 + i - 1; |
| 122 |
if(i == 0) |
| 123 |
{ |
| 124 |
dist[i] = *(it1) - dat1; |
| 125 |
} |
| 126 |
else if( i < n - 1 ) |
| 127 |
{ |
| 128 |
dist[i] = *(it1) - *(it1-1); |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
dist[i] = dat2 - *(it1-1); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
}//resample |
| 141 |
}//base |
| 142 |
}//ralab |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
#endif // BUCKET1D_H |