37#include <forward_list>
38#include <boost/version.hpp>
39#include <boost/random/mersenne_twister.hpp>
40#include <boost/random/uniform_smallint.hpp>
41#include <boost/random/uniform_01.hpp>
42#include <boost/random/geometric_distribution.hpp>
43#include <boost/random/variate_generator.hpp>
45#include "DGtal/base/Common.h"
46#include "DGtal/base/LabelledMap.h"
50#define BOOST_MAJOR_VERSION (BOOST_VERSION / 100000)
51#define BOOST_MINOR_VERSION (( BOOST_VERSION / 100) % 1000)
52#define BOOST_SUBMINOR_VERSION (BOOST_VERSION % 100)
72template <
typename Value,
unsigned int L,
unsigned int X,
unsigned int Y>
74 Value _data[ L ][ X ][ Y ];
79 ArrayLXY(
Value invalid )
88 for (
unsigned int l = 0; l < L; ++l )
89 for (
unsigned int y = 0; y < Y; ++y )
90 for (
unsigned int x = 0; x < X; ++x )
91 setValue( _invalid, l, x, y );
95 const Value & value(
unsigned int l,
unsigned int x,
unsigned int y )
const
97 return _data[ l ][ x ][ y ];
100 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y )
102 if ( _data[ l ][ x ][ y ] != _invalid )
104 _data[ l ][ x ][ y ] = _invalid;
111 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
113 _data[ l ][ x ][ y ] = val;
116 void setValueNoNewLabel(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
118 _data[ l ][ x ][ y ] = val;
121 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const
123 return value( l, x, y ) != _invalid;
126 void getLabels( std::vector<unsigned int> & labels,
127 unsigned int x,
unsigned int y )
const
130 for (
unsigned int l = 0; l < L; ++l )
131 if ( hasLabel( l, x, y ) )
132 labels.push_back( l );
135 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const
138 for (
unsigned int l = 0; l < L; ++l )
139 if ( hasLabel( l, x, y ) ) ++nb;
143 void display ( ostream & ,
unsigned int ,
unsigned int ,
unsigned int )
147 unsigned long long area()
const
149 return L * X * Y *
sizeof(
Value );
160template <
typename Value,
unsigned int L,
unsigned int X,
unsigned int Y>
162 typedef typename std::map<unsigned int, Value> MyMap;
164 MyMap _data[ X ][ Y ];
175 for (
unsigned int y = 0; y < Y; ++y )
176 for (
unsigned int x = 0; x < X; ++x )
177 _data[ x ][ y ].clear();
181 const Value & value(
unsigned int l,
unsigned int x,
unsigned int y )
183 return _data[ x ][ y ][ l ];
186 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y )
188 return _data[ x ][ y ].erase( l );
192 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
194 _data[ x ][ y ][ l ] = val;
197 void setValueNoNewLabel(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
199 _data[ x ][ y ][ l ] = val;
202 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const
204 return _data[ x ][ y ].count( l ) != 0;
207 void getLabels( std::vector<unsigned int> & labels,
208 unsigned int x,
unsigned int y )
const
211 for (
ConstIterator it = _data[ x ][ y ].begin(), it_end = _data[ x ][ y ].end();
213 labels.push_back( (*it).first );
216 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const
218 return (
unsigned int)_data[ x ][ y ].size();
221 void display ( ostream & ,
unsigned int,
unsigned int ,
unsigned int )
226 unsigned long long area()
const
228 unsigned long long total = 0;
229 for (
unsigned int y = 0; y < Y; ++y )
230 for (
unsigned int x = 0; x < X; ++x )
232 unsigned int size = nbLabels( x, y );
233 total += ( size + 1 ) *
235 + 3 *
sizeof(
Value* )
255template <
typename Value,
unsigned int L,
unsigned int X,
unsigned int Y>
257 typedef typename std::pair<DGtal::uint16_t, Value> MyPair;
258 typedef typename std::forward_list<MyPair> MyList;
259 typedef typename MyList::iterator Iterator;
261 MyList _data[ X ][ Y ];
272 for (
unsigned int y = 0; y < Y; ++y )
273 for (
unsigned int x = 0; x < X; ++x )
274 _data[ x ][ y ].clear();
278 const Value & value(
unsigned int l,
unsigned int x,
unsigned int y )
280 MyList & list = _data[ x ][ y ];
281 Iterator it = list.begin(), it_end = list.end();
282 for ( ; it != it_end; ++it )
284 if ( it->first == l )
return it->second;
286 ASSERT ( it == it_end ) ;
287 list.emplace_front( std::make_pair( l,
Value() ) );
288 return list.front().second;
291 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y )
293 MyList & list = _data[ x ][ y ];
294 Iterator it_prev = list.before_begin();
295 Iterator it = list.begin(), it_end = list.end();
296 for ( ; it != it_end; ++it )
298 if ( it->first == l )
300 list.erase_after( it_prev );
309 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
311 MyList & list = _data[ x ][ y ];
312 Iterator it = list.begin(), it_end = list.end();
313 for ( ; it != it_end; ++it )
315 if ( it->first == l )
322 list.emplace_front( std::make_pair( l, val ) );
325 void setValueNoNewLabel(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
327 MyList & list = _data[ x ][ y ];
328 Iterator it = list.begin(), it_end = list.end();
329 for ( ; it != it_end; ++it )
331 if ( it->first == l )
338 list.emplace_front( std::make_pair( l, val ) );
341 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const
343 const MyList & list = _data[ x ][ y ];
345 for ( ; it != it_end; ++it )
347 if ( it->first == l )
return true;
352 void getLabels( std::vector<unsigned int> & labels,
353 unsigned int x,
unsigned int y )
const
356 const MyList & list = _data[ x ][ y ];
358 for ( ; it != it_end; ++it )
360 labels.push_back( (*it).first );
364 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const
366 const MyList & list = _data[ x ][ y ];
369 for ( ; it != it_end; ++it )
374 void display ( ostream & ,
unsigned int ,
unsigned int ,
unsigned int )
379 unsigned long long area()
const
381 unsigned long long total = 0;
382 for (
unsigned int y = 0; y < Y; ++y )
383 for (
unsigned int x = 0; x < X; ++x )
385 unsigned int size = nbLabels( x, y );
386 total +=
sizeof(
Value* )
410template <
typename Value,
unsigned int L,
unsigned int X,
unsigned int Y,
411 typename TWord,
unsigned int N,
unsigned int M >
412class ArrayXYOfLabelledMap {
414 MyLabelledMap _data[ X ][ Y ];
418 ArrayXYOfLabelledMap() {}
421 const Value & value(
unsigned int l,
unsigned int x,
unsigned int y )
const
423 return _data[ x ][ y ].
fastAt( l );
427 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
429 _data[ x ][ y ][ l ] = val;
433 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y )
435 return _data[ x ][ y ].erase( l );
439 void setValueNoNewLabel(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
441 _data[ x ][ y ].fastAt( l ) = val;
445 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const
447 return _data[ x ][ y ].count( l );
451 void getLabels( std::vector<unsigned int> & labels,
452 unsigned int x,
unsigned int y )
const
454 _data[ x ][ y ].labels().getLabels( labels );
458 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const
460 return _data[ x ][ y ].size();
462 inline void display ( ostream & ,
unsigned int ,
unsigned int x,
unsigned int y )
464 std::cerr << _data[ x ][ y ] << endl;
468 unsigned long long area()
const
470 unsigned long long total = 0;
471 for (
unsigned int y = 0; y < Y; ++y )
472 for (
unsigned int x = 0; x < X; ++x )
474 unsigned int size = nbLabels( x, y );
475 total +=
sizeof( MyLabelledMap );
477 total += ( 1 + ( size - N - 1 ) / M ) * ( M *
sizeof(
Value ) + 8 );
485#if (BOOST_MAJOR_VERSION >= 1 ) && (BOOST_MINOR_VERSION >= 47 )
486template <
typename MapLXY,
unsigned int L,
unsigned int X,
unsigned int Y>
488generateData( MapLXY & m,
double proba_no_label,
double proba_label )
490 boost::random::mt19937 rng;
492 boost::random::uniform_smallint<> diceL(0, L-1);
493 boost::random::uniform_01<> diceDouble;
494 boost::random::geometric_distribution<> diceNbLabels( proba_label );
496 std::cerr <<
"E(Y)=" << ( (1-proba_label)/proba_label )
497 <<
" Var(Y)=" << ( (1-proba_label)/(proba_label*proba_label) )
499 unsigned int total = 0;
500 for (
unsigned int y = 0; y < Y; ++y )
501 for (
unsigned int x = 0; x < X; ++x )
503 if ( diceDouble( rng ) >= proba_no_label )
505 unsigned int nb = diceNbLabels( rng );
506 for (
unsigned int i = 0; i < nb; ++i )
508 unsigned int l = diceL( rng );
509 double v = diceDouble( rng );
510 m.setValue( v, l, x, y );
515 std::cerr <<
"- " << total <<
" insertions." << endl;
520template <
typename MapLXY,
unsigned int L,
unsigned int X,
unsigned int Y>
526 boost::uniform_smallint<> diceL(0, L-1);
527 boost::uniform_01<> diceDouble;
528 boost::geometric_distribution<> nbLabelsDist( proba_label );
529 boost::variate_generator
531 boost::geometric_distribution<> > diceNbLabels( rng, nbLabelsDist);
533 std::cerr <<
"E(Y)=" << ( (1-proba_label)/proba_label )
534 <<
" Var(Y)=" << ( (1-proba_label)/(proba_label*proba_label) )
536 unsigned int total = 0;
537 for (
unsigned int y = 0; y < Y; ++y )
538 for (
unsigned int x = 0; x < X; ++x )
540 if ( diceDouble( rng ) >= proba_no_label )
542 unsigned int nb = diceNbLabels();
543 for (
unsigned int i = 0; i < nb; ++i )
545 unsigned int l = diceL( rng );
546 double v = diceDouble( rng );
547 m.setValue( v, l, x, y );
552 std::cerr <<
"- " << total <<
" insertions." << endl;
557template <
typename MapLXY,
unsigned int L,
unsigned int X,
unsigned int Y>
562 std::vector<unsigned int> labels;
563 for (
unsigned int y = 0; y < Y; ++y )
564 for (
unsigned int x = 0; x < X; ++x )
566 m.getLabels( labels, x, y );
567 for (
unsigned int i = 0; i < labels.size(); ++i )
568 sum += m.value( labels[ i ], x, y );
570 std::cerr <<
"- sum = " << sum <<
"." << endl;
574template <
typename MapLXY,
unsigned int L,
unsigned int X,
unsigned int Y>
579 for (
unsigned int y = 0; y < Y; ++y )
580 for (
unsigned int x = 0; x < X; ++x )
582 if ( m.hasLabel( l, x, y ) )
583 sum += m.value( l, x, y );
585 std::cerr <<
"- sum = " << sum <<
"." << endl;
589template <
typename MapLXY,
unsigned int L,
unsigned int X,
unsigned int Y>
593 unsigned int loc3 = 0;
594 for (
unsigned int y = 0; y < Y; ++y )
595 for (
unsigned int x = 0; x < X; ++x )
597 if ( ( m.hasLabel( l1, x, y ) )
598 && ( m.hasLabel( l2, x, y ) )
599 && ( m.hasLabel( l3, x, y ) ) )
602 std::cerr <<
"- " << loc3 <<
" places with " << l1 <<
", " << l2 <<
" ," << l3 << endl;
606template <
typename MapLXY,
unsigned int L,
unsigned int X,
unsigned int Y>
611 for (
unsigned int y = 0; y < Y; ++y )
612 for (
unsigned int x = 0; x < X; ++x )
614 nb += m.erase( l, x, y );
616 std::cerr <<
"- " << nb <<
" values deleted." << endl;
622 typedef double Value;
623 static const unsigned int X = 100;
624 static const unsigned int Y = 100;
625 static const unsigned int L = 16;
627 static const unsigned int N = 1;
628 static const unsigned int M = 5;
630 static const double PROBA_NO_LABEL = 0.75;
635 static const double PROBA_LABEL = 0.5;
637 typedef ArrayLXY<Value, L, X, Y> MyArrayLXY;
638 typedef ArrayXYOfMap<Value, L, X, Y> MyArrayXYOfMap;
639 typedef ArrayXYOfList<Value, L, X, Y> MyArrayXYOfList;
640 typedef ArrayXYOfLabelledMap<Value, L, X, Y, Word, N, M > MyArrayXYOfLabelledMap;
645 MyArrayLXY* arrayLXY =
new MyArrayLXY( -1.0 );
646 generateData< MyArrayLXY, L, X, Y> ( *arrayLXY, PROBA_NO_LABEL, PROBA_LABEL );
650 std::cerr << arrayLXY->area() <<
" bytes." << std::endl;
654 sumAllData< MyArrayLXY, L, X, Y> ( *arrayLXY );
658 sumOneData< MyArrayLXY, L, X, Y> ( *arrayLXY, 0 );
662 sumOneData< MyArrayLXY, L, X, Y> ( *arrayLXY, 15 );
666 locateThreeData< MyArrayLXY, L, X, Y> ( *arrayLXY, 3, 7, 8 );
670 eraseOneData< MyArrayLXY, L, X, Y> ( *arrayLXY, 9 );
681 MyArrayXYOfMap* arrayXYOfMap =
new MyArrayXYOfMap();
682 generateData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap, PROBA_NO_LABEL, PROBA_LABEL );
686 std::cerr << arrayXYOfMap->area() <<
" bytes." << std::endl;
690 sumAllData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap );
694 sumOneData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap, 0 );
698 sumOneData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap, 15 );
702 locateThreeData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap, 3, 7, 8 );
706 eraseOneData< MyArrayXYOfMap, L, X, Y> ( *arrayXYOfMap, 9 );
717 MyArrayXYOfList* arrayXYOfList =
new MyArrayXYOfList();
718 generateData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList, PROBA_NO_LABEL, PROBA_LABEL );
722 std::cerr << arrayXYOfList->area() <<
" bytes." << std::endl;
726 sumAllData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList );
730 sumOneData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList, 0 );
734 sumOneData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList, 15 );
738 locateThreeData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList, 3, 7, 8 );
742 eraseOneData< MyArrayXYOfList, L, X, Y> ( *arrayXYOfList, 9 );
746 delete arrayXYOfList;
751 trace.
beginBlock (
"---------- ArrayXYOfLabelledMap ---------------" );
753 MyArrayXYOfLabelledMap* arrayXYOfLabelledMap =
new MyArrayXYOfLabelledMap;
754 generateData< MyArrayXYOfLabelledMap, L, X, Y > ( *arrayXYOfLabelledMap, PROBA_NO_LABEL, PROBA_LABEL );
758 std::cerr << arrayXYOfLabelledMap->area() <<
" bytes." << std::endl;
762 sumAllData< MyArrayXYOfLabelledMap, L, X, Y> ( *arrayXYOfLabelledMap );
766 sumOneData< MyArrayXYOfLabelledMap, L, X, Y> ( *arrayXYOfLabelledMap, 0 );
770 sumOneData< MyArrayXYOfLabelledMap, L, X, Y> ( *arrayXYOfLabelledMap, 15 );
773 trace.
beginBlock (
"Locate places (3, 7, 8) in ArrayXYOfLabelledMap" );
774 locateThreeData< MyArrayXYOfLabelledMap, L, X, Y> ( *arrayXYOfLabelledMap, 3, 7, 8 );
778 eraseOneData< MyArrayXYOfLabelledMap, L, X, Y> ( *arrayXYOfLabelledMap, 9 );
782 delete arrayXYOfLabelledMap;
Aim: Represents a map label -> data, where the label is an integer between 0 and a constant L-1....
Data & fastAt(const Key &key)
void beginBlock(const std::string &keyword="")
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::uint8_t uint8_t
unsigned 8-bit integer.
void erase(VContainer1 &c1, LContainer2 &c2, unsigned int idx)
unsigned int generateData(MapLXY &m, double proba_no_label, double proba_label)
double sumAllData(MapLXY &m)
unsigned int eraseOneData(MapLXY &m, unsigned int l)
double sumOneData(MapLXY &m, unsigned int l)
unsigned int locateThreeData(MapLXY &m, unsigned int l1, unsigned int l2, unsigned int l3)
void display(ostream &out, const AContainer &C)