22 #include "drwnVarUniverse.h"
28 #define DRWN_FACTOR_MAPPING_FULL
29 #undef DRWN_FACTOR_MAPPING_FULL
46 #ifdef DRWN_FACTOR_MAPPING_FULL
47 vector<int> _fullMapping;
62 _owner(&m), _dstIndx(0), _srcIndx(0) {
64 if (!_owner->identity()) {
65 #ifdef DRWN_FACTOR_MAPPING_FULL
66 if (_owner->_fullMapping.empty())
68 _assignment.resize(_owner->_targetCards.size(), 0);
73 _owner(&m), _dstIndx(indx), _srcIndx(0) {
75 if (_owner->identity()) {
80 #ifdef DRWN_FACTOR_MAPPING_FULL
82 if (!_owner->_fullMapping.empty()) {
83 _srcIndx = (_dstIndx < (int)_owner->_fullMapping.size()) ?
84 _owner->_fullMapping[_dstIndx] : 0;
89 _assignment.resize(_owner->_targetCards.size(), 0);
92 for (
unsigned i = 0; i < _assignment.size(); i++) {
93 int r = (int)(_dstIndx / stride);
95 _assignment[i] = r % _owner->_targetCards[i];
96 _srcIndx += r * _owner->_strideMapping[i];
97 stride *= _owner->_targetCards[i];
104 inline iterator() : _owner(NULL), _dstIndx(0), _srcIndx(0) { }
105 inline ~iterator() { }
107 inline iterator& operator=(
const iterator& it) {
108 _owner = it._owner; _dstIndx = it._dstIndx; _srcIndx = it._srcIndx;
109 _assignment = it._assignment;
112 inline bool operator==(
const iterator& it)
const {
113 return ((_owner == it._owner) && (_dstIndx == it._dstIndx));
115 inline bool operator!=(
const iterator& it)
const {
116 return (!
operator==(it));
118 inline bool operator<(
const iterator& it)
const {
119 return (_dstIndx < it._dstIndx);
121 inline bool operator>(
const iterator& it)
const {
122 return (_dstIndx > it._dstIndx);
124 inline bool operator<=(
const iterator& it)
const {
125 return (_dstIndx <= it._dstIndx);
127 inline bool operator>=(
const iterator& it)
const {
128 return (_dstIndx >= it._dstIndx);
130 inline const int& operator*()
const {
133 inline iterator& operator++() {
136 if (_owner->identity()) {
141 #ifdef DRWN_FACTOR_MAPPING_FULL
142 if (!_owner->_fullMapping.empty()) {
143 _srcIndx = (_dstIndx < (int)_owner->_fullMapping.size()) ?
144 _owner->_fullMapping[_dstIndx] : 0;
149 _srcIndx += _owner->_strideMapping[0];
150 for (
unsigned i = 1; i < _assignment.size(); i++) {
151 if (_assignment[i - 1] < _owner->_targetCards[i - 1])
153 _assignment[i - 1] = 0;
155 _srcIndx += _owner->_strideMapping[i];
161 inline iterator& operator--() {
164 if (_owner->identity()) {
169 #ifdef DRWN_FACTOR_MAPPING_FULL
170 if (!_owner->_fullMapping.empty()) {
171 _srcIndx = (_dstIndx >= 0) ? _owner->_fullMapping[_dstIndx] : 0;
176 _srcIndx -= _owner->_strideMapping[0];
177 for (
unsigned i = 1; i < _assignment.size(); i++) {
178 if (_assignment[i - 1] >= 0)
180 _assignment[i - 1] = _owner->_targetCards[i - 1] - 1;
182 _srcIndx -= _owner->_strideMapping[i];
187 inline iterator operator++(
int) {
188 iterator copy(*
this);
192 inline iterator operator--(
int) {
193 iterator copy(*
this);
197 inline const int& operator[](
int n)
const {
198 iterator copy(*_owner, _dstIndx + n);
202 friend class iterator;
214 if (srcVars.size() == 1) {
217 if (dstVars.size() == 1) {
218 DRWN_ASSERT(dstVars[0] == srcVars[0]);
224 unsigned k = dstVars.size();
225 for (
unsigned i = 0; i < dstVars.size(); i++) {
226 if (dstVars[i] == srcVars[0]) k = i;
229 DRWN_ASSERT(k != dstVars.size());
233 _targetCards.resize(2);
234 _strideMapping.resize(2);
236 _targetCards[1] = _targetSize / _targetCards[0];
237 _strideMapping[0] = 1;
238 _strideMapping[1] = -1 * _targetCards[0];
239 }
else if (k + 1 == dstVars.size()) {
240 _targetCards.resize(2);
241 _strideMapping.resize(2);
243 _targetCards[0] = _targetSize / _targetCards[1];
244 _strideMapping[0] = 0;
245 _strideMapping[1] = 1;
247 _targetCards.resize(3);
248 _strideMapping.resize(3);
250 for (
unsigned i = 0; i < k; i++) {
254 _targetCards[2] = _targetSize / (_targetCards[0] * _targetCards[1]);
255 _strideMapping[0] = 0;
256 _strideMapping[1] = 1;
257 _strideMapping[2] = -1 * _targetCards[1];
266 if (std::equal(srcVars.begin(), srcVars.end(), dstVars.begin())) {
267 for (
unsigned i = 0; i < dstVars.size(); i++) {
272 if (dstVars.size() > srcVars.size()) {
273 _targetCards.resize(2, 1);
274 _strideMapping.resize(2, 1);
275 for (
unsigned i = 0; i < srcVars.size(); i++) {
278 _targetCards[1] = _targetSize / _targetCards[0];
279 _strideMapping[1] = -1 * _targetCards[0];
287 if ((dstVars.size() > srcVars.size()) &&
288 (std::equal(srcVars.begin(), srcVars.end(), dstVars.end() - srcVars.size()))) {
289 for (
unsigned i = 0; i < dstVars.size(); i++) {
294 _targetCards.resize(2, 1);
295 _strideMapping.resize(2, 1);
296 for (
unsigned i = 0; i < srcVars.size(); i++) {
299 _targetCards[0] = _targetSize / _targetCards[1];
300 _strideMapping[0] = 0;
308 map<int, unsigned> varIndex;
310 for (
unsigned i = 0; i < srcVars.size(); i++) {
311 varIndex[srcVars[i]] = stride;
315 _targetCards.resize(dstVars.size(), 0);
316 _strideMapping.resize(dstVars.size(), 0);
317 for (
unsigned i = 0; i < dstVars.size(); i++) {
319 _targetSize *= _targetCards[i];
321 map<int, unsigned>::const_iterator v = varIndex.find(dstVars[i]);
322 if (v != varIndex.end()) {
323 _strideMapping[i] += v->second;
324 if (i + 1 < dstVars.size()) {
325 _strideMapping[i + 1] = -1 * _targetCards[i] * v->second;
330 #ifdef DRWN_FACTOR_MAPPING_FULL
331 _fullMapping = mapping();
337 inline bool empty()
const {
return (_targetSize == 0); }
342 inline bool identity()
const {
return _targetCards.empty(); }
346 iterator end()
const {
return iterator(*
this, _targetSize); }
351 m.reserve(_targetSize);
352 for (
iterator it = begin(); it != end(); ++it) {
bool identity() const
returns true if the mapping is identity (destiation variables and source variables are in the same or...
Definition: drwnTableFactorMapping.h:342
vector< int > mapping() const
returns the mapping of indexes from the source to the destiation
Definition: drwnTableFactorMapping.h:349
const drwnTableFactorMapping * _owner
the factor mapping that owns this iterator
Definition: drwnTableFactorMapping.h:56
bool empty() const
returns true if the mapping has size zero
Definition: drwnTableFactorMapping.h:337
iterator begin() const
iterators
Definition: drwnTableFactorMapping.h:345
vector< int > _assignment
current assignement to the variables in the target table
Definition: drwnTableFactorMapping.h:59
int varCardinality(int v) const
returns the cardinality of variable v (between 0 and _numVariables - 1)
Definition: drwnVarUniverse.h:51
drwnTableFactorMapping(const vector< int > &dstVars, const vector< int > &srcVars, const drwnVarUniversePtr &pUniverse)
dstVar is a superset of srcVar (and variables don't repeat)
Definition: drwnTableFactorMapping.h:209
drwnTableFactorMapping()
default constructor
Definition: drwnTableFactorMapping.h:206
bool operator<(const CvSize &r, const CvSize &s)
inequality operator for CvSize objects (allows partial sorting)
Definition: drwnOpenCVUtils.cpp:88
Creates a mapping between entries in two tables.
Definition: drwnTableFactorMapping.h:39
int _dstIndx
current index into the target table
Definition: drwnTableFactorMapping.h:57
vector< int > _strideMapping
stride mapping from source to target
Definition: drwnTableFactorMapping.h:45
vector< int > _targetCards
cardinality of each variable in the target
Definition: drwnTableFactorMapping.h:44
int _srcIndx
current index into the source table
Definition: drwnTableFactorMapping.h:58
int _targetSize
number of entries in the target table
Definition: drwnTableFactorMapping.h:41
iterator for indexing entries in the tables
Definition: drwnTableFactorMapping.h:52
bool operator==(const CvRect &r, const CvRect &s)
equality operator for CvRect objects
Definition: drwnOpenCVUtils.cpp:83