37#if __GXX_EXPERIMENTAL_CXX0X__ && ( __GNUC__ >= 4 ) && ( __GNUC_MINOR__ >= 6 )
38#include <forward_list>
40#include <boost/version.hpp>
41#include <boost/random/mersenne_twister.hpp>
42#include <boost/random/uniform_smallint.hpp>
43#include <boost/random/uniform_01.hpp>
44#include <boost/random/geometric_distribution.hpp>
45#include <boost/random/variate_generator.hpp>
48#include "DGtal/base/Common.h"
49#include "DGtal/base/LabelledMap.h"
53#define BOOST_MAJOR_VERSION (BOOST_VERSION / 100000)
54#define BOOST_MINOR_VERSION (( BOOST_VERSION / 100) % 1000)
55#define BOOST_SUBMINOR_VERSION (BOOST_VERSION % 100)
75template <
typename Value>
90 :
L( _L ),
X( _X ),
Y( _Y )
104 for (
unsigned int l = 0; l <
L; ++l )
105 for (
unsigned int x = 0; x <
X; ++x )
106 for (
unsigned int y = 0; y <
Y; ++y )
111 size_t offset(
unsigned int l,
unsigned int x,
unsigned int y )
const
113 return ( ( l *
X ) + x ) *
Y + y;
111 size_t offset(
unsigned int l,
unsigned int x,
unsigned int y )
const {
…}
116 const Value &
value(
unsigned int l,
unsigned int x,
unsigned int y )
const
116 const Value &
value(
unsigned int l,
unsigned int x,
unsigned int y )
const {
…}
121 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y )
123 size_t offs =
offset( l, x, y );
121 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y ) {
…}
133 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
133 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y ) {
…}
143 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const
143 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const {
…}
149 unsigned int x,
unsigned int y )
const
152 for (
unsigned int l = 0; l <
L; ++l )
154 labels.push_back( l );
157 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const
160 for (
unsigned int l = 0; l <
L; ++l )
157 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const {
…}
165 void display ( ostream & ,
unsigned int ,
unsigned int ,
unsigned int )
165 void display ( ostream & ,
unsigned int ,
unsigned int ,
unsigned int ) {
…}
169 unsigned long long area()
const
182template <
typename Value>
184 typedef typename std::map<unsigned int, Value>
MyMap;
189 const unsigned int L;
190 const unsigned int X;
191 const unsigned int Y;
199 :
L( _L ),
X( _X ),
Y( _Y )
209 size_t offset(
unsigned int x,
unsigned int y )
const
209 size_t offset(
unsigned int x,
unsigned int y )
const {
…}
217 for (
unsigned int x = 0; x <
X; ++x )
218 for (
unsigned int y = 0; y <
Y; ++y )
223 const Value &
value(
unsigned int l,
unsigned int x,
unsigned int y )
223 const Value &
value(
unsigned int l,
unsigned int x,
unsigned int y ) {
…}
228 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y )
230 return static_cast<unsigned int>(
_data[
offset( x, y ) ].erase( l ));
228 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y ) {
…}
234 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
234 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y ) {
…}
244 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const
244 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const {
…}
250 unsigned int x,
unsigned int y )
const
256 labels.push_back( (*it).first );
259 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const
261 return static_cast<unsigned int>(
_data[
offset( x, y ) ].size());
259 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const {
…}
264 void display ( ostream & ,
unsigned int ,
unsigned int ,
unsigned int )
264 void display ( ostream & ,
unsigned int ,
unsigned int ,
unsigned int ) {
…}
269 unsigned long long area()
const
271 unsigned long long total = 0;
272 for (
unsigned int y = 0; y <
Y; ++y )
273 for (
unsigned int x = 0; x <
X; ++x )
275 unsigned int size =
nbLabels( x, y );
276 total += ( size + 1 ) *
278 + 3 *
sizeof(
Value* )
288#if __GXX_EXPERIMENTAL_CXX0X__ && ( __GNUC__ >= 4 ) && ( __GNUC_MINOR__ >= 6 )
300template <
typename Value>
301class DynArrayXYOfList {
302 typedef typename std::pair<uint16_t, Value> MyPair;
303 typedef typename std::forward_list<MyPair> MyList;
304 typedef typename MyList::iterator Iterator;
307 typedef Value ValueType;
308 const unsigned int L;
309 const unsigned int X;
310 const unsigned int Y;
318 DynArrayXYOfList(
unsigned int _L,
unsigned int _X,
unsigned int _Y )
319 :
L( _L ), X( _X ), Y( _Y )
321 _data =
new MyList[ X * Y ];
329 size_t offset(
unsigned int x,
unsigned int y )
const
337 for (
unsigned int y = 0; y < Y; ++y )
338 for (
unsigned int x = 0; x < X; ++x )
339 _data[ offset( x, y ) ].clear();
343 const Value & value(
unsigned int l,
unsigned int x,
unsigned int y )
345 MyList & list = _data[ offset( x, y ) ];
346 Iterator it = list.begin(), it_end = list.end();
347 for ( ; it != it_end; ++it )
349 if ( it->first == l )
return it->second;
351 ASSERT(it == it_end);
352 list.emplace_front( std::make_pair( l,
Value() ) );
353 return list.front().second;
356 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y )
358 MyList & list = _data[ offset( x, y ) ];
359 Iterator it_prev = list.before_begin();
360 Iterator it = list.begin(), it_end = list.end();
361 for ( ; it != it_end; ++it )
363 if ( it->first == l )
365 list.erase_after( it_prev );
374 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
376 MyList & list = _data[ offset( x, y ) ];
377 Iterator it = list.begin(), it_end = list.end();
378 for ( ; it != it_end; ++it )
380 if ( it->first == l )
387 list.emplace_front( std::make_pair( l, val ) );
390 void setValueNoNewLabel(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
392 MyList & list = _data[ offset( x, y ) ];
393 Iterator it = list.begin(), it_end = list.end();
394 for ( ; it != it_end; ++it )
396 if ( it->first == l )
403 list.emplace_front( std::make_pair( l, val ) );
406 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const
408 const MyList & list = _data[ offset( x, y ) ];
410 for ( ; it != it_end; ++it )
412 if ( it->first == l )
return true;
417 void getLabels( std::vector<unsigned int> & labels,
418 unsigned int x,
unsigned int y )
const
421 const MyList & list = _data[ offset( x, y ) ];
423 for ( ; it != it_end; ++it )
425 labels.push_back( (*it).first );
429 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const
431 const MyList & list = _data[ offset( x, y ) ];
434 for ( ; it != it_end; ++it )
439 void display ( ostream & ,
unsigned int ,
unsigned int ,
unsigned int )
444 unsigned long long area()
const
446 unsigned long long total = 0;
447 for (
unsigned int y = 0; y < Y; ++y )
448 for (
unsigned int x = 0; x < X; ++x )
450 unsigned int size = nbLabels( x, y );
451 total +=
sizeof(
Value* )
476template <
typename Value,
unsigned int L,
477 typename TWord,
unsigned int N,
unsigned int M >
482 const unsigned int X;
483 const unsigned int Y;
501 size_t offset(
unsigned int x,
unsigned int y )
const
501 size_t offset(
unsigned int x,
unsigned int y )
const {
…}
509 for (
unsigned int y = 0; y <
Y; ++y )
510 for (
unsigned int x = 0; x <
X; ++x )
515 const Value &
value(
unsigned int l,
unsigned int x,
unsigned int y )
const
515 const Value &
value(
unsigned int l,
unsigned int x,
unsigned int y )
const {
…}
521 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y )
521 void setValue(
const Value & val,
unsigned int l,
unsigned int x,
unsigned int y ) {
…}
527 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y )
527 unsigned int erase(
unsigned int l,
unsigned int x,
unsigned int y ) {
…}
539 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const
539 bool hasLabel(
unsigned int l,
unsigned int x,
unsigned int y )
const {
…}
546 unsigned int x,
unsigned int y )
const
548 _data[
offset( x, y ) ].labels().getLabels( labels );
552 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const
554 return static_cast<unsigned int>(
_data[
offset( x, y ) ].size());
552 unsigned int nbLabels(
unsigned int x,
unsigned int y )
const {
…}
556 inline void display ( ostream & ,
unsigned int ,
unsigned int x,
unsigned int y )
556 inline void display ( ostream & ,
unsigned int ,
unsigned int x,
unsigned int y ) {
…}
562 unsigned long long area()
const
564 unsigned long long total = 0;
565 for (
unsigned int y = 0; y <
Y; ++y )
566 for (
unsigned int x = 0; x <
X; ++x )
568 unsigned int size =
nbLabels( x, y );
571 total += ( 1 + ( size - N - 1 ) / M ) * ( M *
sizeof(
Value ) + 8 );
620#if (BOOST_MAJOR_VERSION >= 1 ) && (BOOST_MINOR_VERSION >= 47 )
621template <
typename MapLXY>
623generateData( MapLXY & m,
unsigned int L,
double proba_no_label,
double proba_label )
625 boost::random::mt19937 rng;
627 boost::random::uniform_smallint<> diceL(0,
L-1);
628 boost::random::uniform_01<> diceDouble;
629 boost::random::geometric_distribution<> diceNbLabels( proba_label );
631 std::cerr <<
"E(Y)=" << ( (1-proba_label)/proba_label )
632 <<
" Var(Y)=" << ( (1-proba_label)/(proba_label*proba_label) )
634 unsigned int X = m.X;
635 unsigned int Y = m.Y;
636 unsigned int total = 0;
637 for (
unsigned int y = 0; y < Y; ++y )
638 for (
unsigned int x = 0; x < X; ++x )
640 if ( diceDouble( rng ) >= proba_no_label )
642 unsigned int nb = diceNbLabels( rng );
643 for (
unsigned int i = 0; i < nb; ++i )
645 unsigned int l = diceL( rng );
646 double v = diceDouble( rng );
647 m.setValue( v, l, x, y );
652 std::cerr <<
"- " << total <<
" insertions." << endl;
657template <
typename MapLXY>
659generateData( MapLXY & m,
unsigned int L,
double proba_no_label,
double proba_label )
663 boost::uniform_smallint<> diceL(0,
L-1);
664 boost::uniform_01<> diceDouble;
665 boost::geometric_distribution<> nbLabelsDist( proba_label );
666 boost::variate_generator
668 boost::geometric_distribution<> > diceNbLabels( rng, nbLabelsDist);
670 std::cerr <<
"E(Y)=" << ( (1-proba_label)/proba_label )
671 <<
" Var(Y)=" << ( (1-proba_label)/(proba_label*proba_label) )
673 unsigned int X = m.X;
674 unsigned int Y = m.Y;
675 unsigned int total = 0;
676 for (
unsigned int y = 0; y < Y; ++y )
677 for (
unsigned int x = 0; x < X; ++x )
679 if ( diceDouble( rng ) >= proba_no_label )
681 unsigned int nb = diceNbLabels();
682 for (
unsigned int i = 0; i < nb; ++i )
684 unsigned int l = diceL( rng );
685 double v = diceDouble( rng );
686 m.setValue( v, l, x, y );
691 std::cerr <<
"- " << total <<
" insertions." << endl;
696template <
typename MapLXY>
700 unsigned int X = m.X;
701 unsigned int Y = m.Y;
703 std::vector<unsigned int> labels;
704 for (
unsigned int y = 0; y < Y; ++y )
705 for (
unsigned int x = 0; x < X; ++x )
707 m.getLabels( labels, x, y );
708 for (
unsigned int i = 0; i < labels.size(); ++i )
709 sum += m.value( labels[ i ], x, y );
711 std::cerr <<
"- sum = " << sum <<
"." << endl;
715template <
typename MapLXY>
719 unsigned int X = m.X;
720 unsigned int Y = m.Y;
722 for (
unsigned int y = 0; y < Y; ++y )
723 for (
unsigned int x = 0; x < X; ++x )
725 if ( m.hasLabel( l, x, y ) )
726 sum += m.value( l, x, y );
728 std::cerr <<
"- sum = " << sum <<
"." << endl;
732template <
typename MapLXY>
734locateThreeData( MapLXY & m,
unsigned int l1,
unsigned int l2,
unsigned int l3 )
736 unsigned int X = m.X;
737 unsigned int Y = m.Y;
738 unsigned int loc3 = 0;
739 for (
unsigned int y = 0; y < Y; ++y )
740 for (
unsigned int x = 0; x < X; ++x )
742 if ( ( m.hasLabel( l1, x, y ) )
743 && ( m.hasLabel( l2, x, y ) )
744 && ( m.hasLabel( l3, x, y ) ) )
747 std::cerr <<
"- " << loc3 <<
" places with " << l1 <<
", " << l2 <<
" ," << l3 << endl;
751template <
typename MapLXY>
755 unsigned int X = m.X;
756 unsigned int Y = m.Y;
758 for (
unsigned int y = 0; y < Y; ++y )
759 for (
unsigned int x = 0; x < X; ++x )
761 nb += m.erase( l, x, y );
766template <
typename MapLXY>
767void generateStatsMultiMapXY(
const string & name,
769 const unsigned int L,
770 const unsigned int X,
771 const unsigned int Y,
772 double PROBA_NO_LABEL,
775 typedef typename MapLXY::ValueType
Value;
777 string msg =
"Generating statistics for " + name;
778 trace.beginBlock ( msg.c_str() );
779 trace.beginBlock (
"Generating data." );
781 long tgen =
trace.endBlock();
783 trace.beginBlock (
"Memory usage." );
784 unsigned long long mem = m.area();
785 trace.info() << mem <<
" bytes." << std::endl;
786 long tmem =
trace.endBlock();
788 trace.beginBlock (
"Sum all values." );
790 trace.info() <<
"- sum_all = " << sumAll <<
"." << endl;
791 long tsumall =
trace.endBlock();
793 trace.beginBlock (
"Sum label 0 values." );
795 trace.info() <<
"- sum_0 = " << sum0 <<
"." << endl;
796 long tsum0 =
trace.endBlock();
798 trace.beginBlock (
"Sum label 15 values." );
800 trace.info() <<
"- sum_15 = " << sum15 <<
"." << endl;
801 long tsum15 =
trace.endBlock();
803 trace.beginBlock (
"Locate places (3, 7, 8)." );
805 trace.info() <<
"- " << nbThree <<
" places (3, 7, 8) detected." << endl;
806 long tthree =
trace.endBlock();
808 trace.beginBlock (
"Erase label 9." );
810 trace.info() <<
"- " << nbErased <<
" values deleted." << endl;
811 long terase9 =
trace.endBlock();
813 trace.beginBlock (
"Clear data" );
815 long tclear =
trace.endBlock();
818 std::cout << name <<
" " <<
L <<
" " << X <<
" " << Y
819 <<
" " << PROBA_NO_LABEL <<
" " << PROBA_LABEL
820 <<
" " <<
sizeof(
Value) <<
" " <<
sizeof(
Value*) <<
" " << mem
821 <<
" " << tgen <<
" " << tmem
822 <<
" " << tsumall <<
" " << tsum0 <<
" " << tsum15
823 <<
" " << tthree <<
" " << terase9 <<
" " << tclear
824 <<
" " << sumAll <<
" " << sum0 <<
" " << sum15
825 <<
" " << nbThree <<
" " << nbErased
829#define GENERATE_STATS_NM( Word, N , M ) \
831 typedef DynArrayXYOfLabelledMap<Value,L,Word,N,M> MyArrayXYOfLabelledMap; \
832 MyArrayXYOfLabelledMap* anArrayXYOfLabelledMap = new MyArrayXYOfLabelledMap( X, Y ); \
833 generateStatsMultiMapXY( "DynArrayXYOfLabelledMap<double," #Word "," #N "," #M ">", \
834 *anArrayXYOfLabelledMap, L, X, Y, PROB_NO_DATA, PROB_ONE_DATA ); \
835 delete anArrayXYOfLabelledMap; \
842int main(
int ,
char** )
844 typedef double Value;
845 static const unsigned int L = 16;
846 trace.info()<<
"Test several multi-map structures and compute some statistics." <<std::endl <<
"With parameter: "<<std::endl
847 <<
"\t tested image size: 500 500"<<std::endl
848 <<
"Probability that there is no data at all at an image position (Bernouilli distribution): 0.5"
849 <<
"Probability for the geometric distribution of the number of data per image position (E(Y)=(1-p)/p, Var(Y)=(1-p)/p^2 :0.5"<< std::endl;
853 unsigned int X = 100;
854 unsigned int Y = 100;
856 double PROB_NO_DATA = 0.5;
861 double PROB_ONE_DATA = 0.5;
864 MyArrayLXY* anArrayLXY =
new MyArrayLXY(
L, X, Y, -1.0 );
865 generateStatsMultiMapXY(
"DynArrayLXY<double>",
866 *anArrayLXY,
L, X, Y, PROB_NO_DATA, PROB_ONE_DATA );
870 MyArrayXYOfMap* anArrayXYOfMap =
new MyArrayXYOfMap(
L, X, Y );
871 generateStatsMultiMapXY(
"DynArrayXYOfMap<double>",
872 *anArrayXYOfMap,
L, X, Y, PROB_NO_DATA, PROB_ONE_DATA );
873 delete anArrayXYOfMap;
875#if __GXX_EXPERIMENTAL_CXX0X__ && ( __GNUC__ >= 4 ) && ( __GNUC_MINOR__ >= 6 )
876 typedef DynArrayXYOfList<Value> MyArrayXYOfList;
877 MyArrayXYOfList* anArrayXYOfList =
new MyArrayXYOfList(
L, X, Y );
878 generateStatsMultiMapXY(
"DynArrayXYOfList<double>",
879 *anArrayXYOfList,
L, X, Y, PROB_NO_DATA, PROB_ONE_DATA );
880 delete anArrayXYOfList;
Aim: Represents a map label -> data, where the label is an integer between 0 and a constant L-1....
bool hasLabel(unsigned int l, unsigned int x, unsigned int y) const
unsigned long long area() const
size_t offset(unsigned int l, unsigned int x, unsigned int y) const
void setValue(const Value &val, unsigned int l, unsigned int x, unsigned int y)
void getLabels(std::vector< unsigned int > &labels, unsigned int x, unsigned int y) const
void setValueNoNewLabel(const Value &val, unsigned int l, unsigned int x, unsigned int y)
unsigned int erase(unsigned int l, unsigned int x, unsigned int y)
const Value & value(unsigned int l, unsigned int x, unsigned int y) const
unsigned int nbLabels(unsigned int x, unsigned int y) const
void display(ostream &, unsigned int, unsigned int, unsigned int)
DynArrayLXY(unsigned int _L, unsigned int _X, unsigned int _Y, Value invalid)
size_t offset(unsigned int x, unsigned int y) const
~DynArrayXYOfLabelledMap()
unsigned long long area() const
void setValue(const Value &val, unsigned int l, unsigned int x, unsigned int y)
void setValueNoNewLabel(const Value &val, unsigned int l, unsigned int x, unsigned int y)
void getLabels(std::vector< unsigned int > &labels, unsigned int x, unsigned int y) const
unsigned int erase(unsigned int l, unsigned int x, unsigned int y)
void display(ostream &, unsigned int, unsigned int x, unsigned int y)
const Value & value(unsigned int l, unsigned int x, unsigned int y) const
unsigned int nbLabels(unsigned int x, unsigned int y) const
bool hasLabel(unsigned int l, unsigned int x, unsigned int y) const
LabelledMap< Value, L, TWord, N, M > MyLabelledMap
DynArrayXYOfLabelledMap(unsigned int _X, unsigned int _Y)
void getLabels(std::vector< unsigned int > &labels, unsigned int x, unsigned int y) const
unsigned int nbLabels(unsigned int x, unsigned int y) const
size_t offset(unsigned int x, unsigned int y) const
void setValue(const Value &val, unsigned int l, unsigned int x, unsigned int y)
bool hasLabel(unsigned int l, unsigned int x, unsigned int y) const
unsigned int erase(unsigned int l, unsigned int x, unsigned int y)
unsigned long long area() const
void setValueNoNewLabel(const Value &val, unsigned int l, unsigned int x, unsigned int y)
DynArrayXYOfMap(unsigned int _L, unsigned int _X, unsigned int _Y)
std::map< unsigned int, Value > MyMap
void display(ostream &, unsigned int, unsigned int, unsigned int)
const Value & value(unsigned int l, unsigned int x, unsigned int y)
MyMap::const_iterator ConstIterator
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
std::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)