-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGridMapper.cpp
111 lines (91 loc) · 2.9 KB
/
GridMapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//---------------------------------------------------------------------------
#pragma hdrstop
#include <algorithm>
#include <numeric>
#include <iterator>
#include <boost/tuple/tuple.hpp>
#include "GridMapper.h"
using std::lower_bound;
using std::iota;
using std::for_each;
using std::stable_sort;
using std::begin;
using std::end;
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
void ViewRowGridMapper::ClearMap()
{
mapped_ = false;
view2Model_.clear();
model2view_.clear();
}
//---------------------------------------------------------------------------
int ViewRowGridMapper::Map( int RowCount )
{
ViewToModelCont View2Model( RowCount );
iota( begin( View2Model ), end( View2Model ), 0 );
BuildModelToView( View2Model );
view2Model_.swap( View2Model );
mapped_ = true;
return GetCount();
}
//---------------------------------------------------------------------------
template<size_t TupleFieldIdx>
struct ModelIndexCompare {
template<typename T, typename U>
bool operator()( T const & Tuple, U Value ) const {
return Tuple.template get<TupleFieldIdx>() < Value;
}
};
int ViewRowGridMapper::DoIdx2Num( int Idx ) const
{
if ( mapped_ ) {
ModelToViewCont::const_iterator It =
lower_bound(
begin( model2view_ ), end( model2view_ ), Idx,
ModelIndexCompare<(size_t)ModelToRowField::ModelIdx>()
);
if ( It != end( model2view_ ) &&
!( Idx < It->get<(int)ModelToRowField::ModelIdx>() ) )
{
return It->get<(size_t)ModelToRowField::ViewIdx>();
}
return -1;
}
else {
return Idx;
}
}
//---------------------------------------------------------------------------
int ViewRowGridMapper::DoNum2Idx( int ARow ) const
{
return mapped_ && ARow < static_cast<int>( view2Model_.size() ) ?
view2Model_[ARow]
:
ARow;
}
//---------------------------------------------------------------------------
template<typename C>
class CopyIotaType {
public:
explicit CopyIotaType( C& Cont ) : n_( 0 ), cont_( Cont ) {}
template<typename T>
void operator()( T const & Val ) {
cont_.push_back( boost::make_tuple( Val, n_++ ) );
}
private:
int n_;
C& cont_;
};
template<typename C>
inline CopyIotaType<C> CopyIota( C& Cont ) { return CopyIotaType<C>( Cont ); }
void ViewRowGridMapper::BuildModelToView( ViewToModelCont const & View2Model )
{
ModelToViewCont Model2View;
Model2View.reserve( View2Model.size() );
for_each( begin( View2Model ), end( View2Model ), CopyIota( Model2View ) );
stable_sort( begin( Model2View ), end( Model2View ) );
model2view_.swap( Model2View );
}
//---------------------------------------------------------------------------