51 #include "../geometry/geometry.h"
52 #include "../config.h"
57 template<
typename INT>
58 struct count_iterator : std::iterator<std::random_access_iterator_tag,INT,INT,INT,INT&>{
61 count_iterator(
const count_iterator&) =
default;
62 count_iterator( INT cur ):
cur_(cur) {}
64 count_iterator& operator++(){
68 count_iterator& operator--(){
72 count_iterator operator++(
int){
73 count_iterator ret = *
this;
77 count_iterator operator--(
int){
78 count_iterator ret = *
this;
82 count_iterator
operator+(INT n)
const {
return count_iterator(
cur_+n); }
83 count_iterator& operator+=(INT n) {
cur_ +=n;
return *
this; }
84 count_iterator
operator-(INT n)
const {
return count_iterator(
cur_-n); }
86 INT
operator-(count_iterator&rhs)
const {
return cur_-rhs.cur_; }
91 bool operator!=( count_iterator rhs )
const {
92 return cur_ != rhs.cur_;
94 bool operator==( count_iterator rhs )
const {
95 return !operator!=(rhs);
99 template<
typename INT>
100 count_iterator<INT>
operator+(INT n, count_iterator<INT> rhs) {
return count_iterator<INT>(rhs.cur_+n); }
101 template<
int depth>
struct set_map_{
102 static void apply(
int offset,
int lda[],
int lh[][2],
103 const std::vector<std::size_t>& displs, std::vector<std::size_t>& map ) {
104 for(
int i=lh[depth][0]; i<lh[depth][1]; ++i )
108 template<>
struct set_map_<0>{
109 static void apply(
int offset,
int[] ,
int lh[][2],
110 const std::vector<std::size_t>& displs, std::vector<std::size_t>& map ) {
111 typedef count_iterator<std::size_t> iter;
112 map.insert(map.end(), iter(displs[offset+lh[0][0]]), iter(displs[offset+lh[0][1]]));
117 template<
typename T,
typename CONFIG>
120 template<
typename T,
typename CONFIG>
121 struct bin_iterator : std::iterator<std::forward_iterator_tag,std::pair<typename CONFIG::point_type,T> > {
122 using P = std::pair<typename CONFIG::point_type,T>;
123 static const int D = CONFIG::D;
150 template<
typename T,
typename CONFIG>
152 using P = std::pair<typename CONFIG::point_type,T>;
153 static const int D = CONFIG::D;
158 bin_range(
int lda_[],
int lh_[][2],
const std::vector<std::size_t>& d_,
const std::vector<P>& v_ )
160 for(
int i=0; i<
D; ++i ){
162 lh[i][0] = lh_[i][0];
163 lh[i][1] = lh_[i][1];
175 template<
typename T,
typename CONFIG>
178 for(
int i=0; i<
D-1; ++i )
count[i] =
range.lh[i+1][0];
181 template<
typename T,
typename CONFIG>
184 if( ++index == high ){
187 if( ++count[i] != range.lh[i+1][1] )
goto VALIDATE;
188 count[i] = range.lh[i+1][0];
196 template<
typename T,
typename CONFIG>
199 std::size_t offset = 0;
200 for(
int i=0; i<D-1; ++i ) offset += range.lda[i+1]*(range.lh[i+1][1]-1);
201 index = high = range.displs[offset+range.lh[0][1]];
203 template<
typename T,
typename CONFIG>
206 if( count[D-2] == range.lh[D-1][1] )
return;
207 std::size_t offset = 0;
208 for(
int i=0; i<D-1; ++i ) offset += range.lda[i+1]*count[i];
209 index = range.displs[offset+range.lh[0][0]];
210 high = range.displs[offset+range.lh[0][1]];
213 template<
typename T,
typename CONFIG>
215 template<
typename T,
typename CONFIG>
219 template<
typename CONFIG>
222 using point_type =
typename CONFIG::point_type;
223 static const int D = CONFIG::D;
224 static const bool QUIET = CONFIG::QUIET;
226 std::vector<std::size_t> displs;
230 std::size_t n[CONFIG::D];
231 typename CONFIG::REAL h;
232 using REAL =
typename CONFIG::REAL;
235 bin_t( std::vector<std::pair<point_type,T> >& val ){
243 min =
max = val[0].first;
244 for( std::size_t i=1; i<val.size(); ++i ){
245 point_type p = val[i].first;
246 for( std::size_t i=0; i<D; ++i ) {
253 REAL vol = std::abs(
max[0]-min[0]);
259 REAL vol_multi = vol;
260 for(
int i=1; i<D; ++i ){
261 vol_multi =
max[i]-min[i];
263 vol_multi =
static_cast<REAL
>(1);
270 vol =
static_cast<REAL
>(0);
272 h = std::pow(
static_cast<REAL
>(6)*vol/
static_cast<REAL
>(val.size()),1.0/D);
275 h =
static_cast<REAL
>(1);
276 if(val.size() > 1 && !QUIET)
277 std::cout <<
"MUI Warning [bin.h]: Bin support size fixed to 1.0, check interface dimensionality or problem decomposition." << std::endl;
281 for(
int i=0; i<D; ++i ) {
282 n[i] =
static_cast<size_t>(std::ceil((
max[i]-min[i])/h));
283 n[i] =
static_cast<size_t>(
std::max( n[i], std::size_t(1) ));
288 std::vector<std::size_t> index(val.size()+1,0);
289 std::vector<std::size_t> counts(nn,0);
290 for( std::size_t i=0; i<val.size(); ++i ) {
291 index[i] = get_index_(val[i].first);
294 displs.resize(nn+1,0);
295 std::partial_sum(counts.begin(),counts.end(), displs.begin()+1);
298 std::vector<std::pair<point_type,T> > v(val.size());
299 for( std::size_t i=0; i<val.size(); ++i ) v[counts[index[i]]++] = val[i];
304 std::vector<std::size_t> map;
307 if( initialize_query_(bx,lda,lh) )
return map;
308 map.reserve(lda[D-1]*12);
317 initialize_query_(bx,lda,lh);
324 if(dim_size == 0) dim_size = 1.0;
332 for(
int i=1; i<CONFIG::D; ++i ) lda[i] = lda[i-1]*n[i-1];
333 for(
int i=0; i<CONFIG::D; ++i ) {
334 lh[i][0] =
static_cast<int>(
std::max(std::floor((bx.
get_min()[i]-min[i])/h),
typename CONFIG::REAL(0)));
335 lh[i][1] =
static_cast<int>(
std::min(std::ceil((bx.
get_max()[i]-min[i])/h),
typename CONFIG::REAL(n[i])));
336 if( lh[i][0] >= lh[i][1] ){
344 inline std::size_t get_index_(
const point_type& pt )
const {
345 std::size_t
m = 1, ret=0;
346 for(
int i=0; i<D; ++i ) {
347 std::size_t d =
static_cast<size_t>((std::floor((pt[i]-min[i])/h)));
348 d =
static_cast<size_t>(
std::min(d,n[i]-1));
INT cur_
Definition: bin.h:59
Definition: geometry.h:188
coordinate_type get_min() const
Definition: geometry.h:202
coordinate_type get_max() const
Definition: geometry.h:204
u u u u u u min
Definition: dim.h:289
u u m
Definition: dim.h:281
bool almost_equal(T x, T y)
Definition: util.h:128
vexpr_sub< E1, E2, SCALAR, D > operator-(vexpr< E1, SCALAR, D > const &u, vexpr< E2, SCALAR, D > const &v)
Definition: point.h:290
vexpr_add< E1, E2, SCALAR, D > operator+(vexpr< E1, SCALAR, D > const &u, vexpr< E2, SCALAR, D > const &v)
Definition: point.h:285
vexpr_mul< E1, E2, SCALAR, D > operator*(vexpr< E1, SCALAR, D > const &u, vexpr< E2, SCALAR, D > const &v)
Definition: point.h:300
SCALAR max(vexpr< E, SCALAR, D > const &u)
Definition: point.h:350
vexpr_apply1< E, OP, SCALAR, D > apply(vexpr< E, SCALAR, D > const &u, OP const &op)
Definition: point.h:392
SCALAR norm(vexpr< E, SCALAR, D > const &u)
Definition: point.h:386
const P & operator*() const
Definition: bin.h:214
bool operator==(const bin_iterator &rhs) const
Definition: bin.h:139
bin_iterator(const bin_range< T, CONFIG > &range_, int)
Definition: bin.h:125
bin_iterator operator++(int)
Definition: bin.h:130
const bin_range< T, CONFIG > & range
Definition: bin.h:144
const P * operator->() const
Definition: bin.h:216
bin_iterator & operator=(const bin_iterator &)=default
std::pair< typename CONFIG::point_type, T > P
Definition: bin.h:122
std::size_t index
Definition: bin.h:147
int count[D-1]
Definition: bin.h:145
void invalidate()
Definition: bin.h:197
bool operator!=(const bin_iterator &rhs) const
Definition: bin.h:138
bin_iterator & operator++()
Definition: bin.h:182
std::size_t high
Definition: bin.h:146
static const int D
Definition: bin.h:123
bin_iterator(const bin_range< T, CONFIG > &range_)
Definition: bin.h:176
void validate()
Definition: bin.h:204
bin_iterator(const bin_iterator &)=default
const std::vector< std::size_t > & displs
Definition: bin.h:171
int lh[D][2]
Definition: bin.h:170
bin_range(const bin_range &)=default
int lda[D]
Definition: bin.h:169
static const int D
Definition: bin.h:153
iterator begin() const
Definition: bin.h:155
bin_range(int lda_[], int lh_[][2], const std::vector< std::size_t > &d_, const std::vector< P > &v_)
Definition: bin.h:158
iterator end() const
Definition: bin.h:156
const std::vector< P > & value
Definition: bin.h:172
bin_range & operator=(const bin_range &)=default
bin_iterator< T, CONFIG > iterator
Definition: bin.h:154
std::pair< typename CONFIG::point_type, T > P
Definition: bin.h:152
REAL domain_size()
Definition: bin.h:321
bin_t(std::vector< std::pair< point_type, T > > &val)
Definition: bin.h:235
std::vector< std::size_t > query(const geometry::box< CONFIG > &bx) const
Definition: bin.h:303
bin_range< T, CONFIG > query2(const geometry::box< CONFIG > &bx, const std::vector< std::pair< point_type, T > > &v) const
Definition: bin.h:314