DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LabelledMap.ih
1 
30 
31 #include <cstdlib>
33 
35 // IMPLEMENTATION of inline methods.
37 
39 // ----------------------- Standard services ------------------------------
40 
42 // class LabelledMap::BlockIterator
43 //-----------------------------------------------------------------------------
44 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
47 {}
48 //-----------------------------------------------------------------------------
49 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
52  : myIdx( 0 ), myDatas( 0 )
53 {}
54 //-----------------------------------------------------------------------------
55 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
57 BlockIterator( const Self & other)
58  : myIdx( other.myIdx ), myNbDatas( other.myNbDatas ),
59  myDatas( other.myDatas ), myNext( other.myNext )
60 {}
61 //-----------------------------------------------------------------------------
62 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
64 BlockIterator( __FirstBlock & block, unsigned int idx, unsigned int size )
65 {
66  ASSERT( idx <= size );
67  if ( size <= N+1 )
68  {
69  if ( idx <= N )
70  {
71  myIdx = idx;
72  myNbDatas = N + 1;
73  myDatas = block.datas;
74  myNext = 0;
75  }
76  else
77  { // end iterator.
78  myIdx = 0;
79  myNbDatas = 0;
80  myDatas = 0;
81  myNext = 0;
82  }
83  }
84  else
85  {
86  ASSERT( block.data.nextBlock != 0 );
87  myNext = block.data.nextBlock;
88  if ( idx < N )
89  {
90  myIdx = idx;
91  myNbDatas = N;
92  myDatas = block.datas;
93  }
94  else
95  {
96  idx -= N;
97  while ( idx >= M )
98  {
99  idx -= M;
100  myNext = ( myNext != 0 ) ? myNext->next : 0;
101  }
102  if ( myNext == 0 )
103  {
104  myIdx = 0;
105  myNbDatas = 0;
106  myDatas = 0;
107  }
108  else
109  {
110  myIdx = idx;
111  myNbDatas = M;
112  myDatas = myNext->datas;
113  }
114  }
115  }
116 }
117 //-----------------------------------------------------------------------------
118 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
119 inline
122 operator=( const Self & other )
123 {
124  if ( this != &other )
125  {
126  myIdx = other.myIdx;
127  myNbDatas = other.myNbDatas;
128  myDatas = other.myDatas;
129  myNext = other.myNext;
130  }
131  return *this;
132 }
133 //-----------------------------------------------------------------------------
134 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
135 inline
138 operator*() const
139 {
140  ASSERT( myDatas != 0 );
141  return myDatas[ myIdx ];
142 }
143 //-----------------------------------------------------------------------------
144 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
145 inline
148 operator->() const
149 {
150  ASSERT( myDatas != 0 );
151  return myDatas + myIdx;
152 }
153 //-----------------------------------------------------------------------------
154 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
155 inline
159 {
160  return this->operator+=( 1 );
161 }
162 //-----------------------------------------------------------------------------
163 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
164 inline
168 {
169  Self tmp( *this );
170  this->operator++();
171  return tmp;
172 }
173 //-----------------------------------------------------------------------------
174 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
175 inline
176 bool
178 operator==( const Self & other ) const
179 {
180  return ( myDatas == other.myDatas ) && ( myIdx == other.myIdx );
181 }
182 //-----------------------------------------------------------------------------
183 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
184 inline
185 bool
187 operator!=( const Self & other ) const
188 {
189  return ( myDatas != other.myDatas ) || ( myIdx != other.myIdx );
190 }
191 //-----------------------------------------------------------------------------
192 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
193 inline
197 {
198  myIdx += n;
199  while ( myIdx >= myNbDatas )
200  {
201  if ( myNext == 0 )
202  {
203  myDatas = 0;
204  myIdx = 0;
205  break;
206  }
207  myIdx -= myNbDatas;
208  myDatas = myNext->datas;
209  myNbDatas = M;
210  myNext = myNext->next;
211  }
212  return *this;
213 }
214 //-----------------------------------------------------------------------------
215 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
216 inline
220 {
221  Self tmp( *this );
222  tmp += n;
223  return *tmp;
224 }
225 
227 // class LabelledMap::BlockConstIterator
228 //-----------------------------------------------------------------------------
229 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
232 {}
233 //-----------------------------------------------------------------------------
234 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
237  : myIdx( 0 ), myDatas( 0 )
238 {}
239 //-----------------------------------------------------------------------------
240 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
242 BlockConstIterator( const Self & other)
243  : myIdx( other.myIdx ), myNbDatas( other.myNbDatas ),
244  myDatas( other.myDatas ), myNext( other.myNext )
245 {}
246 //-----------------------------------------------------------------------------
247 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
249 BlockConstIterator( const __FirstBlock & block, unsigned int idx, unsigned int size )
250 {
251  ASSERT( idx <= size );
252  if ( size <= N+1 )
253  {
254  if ( idx <= N )
255  {
256  myIdx = idx;
257  myNbDatas = N + 1;
258  myDatas = block.datas;
259  myNext = 0;
260  }
261  else
262  { // end iterator.
263  myIdx = 0;
264  myNbDatas = 0;
265  myDatas = 0;
266  myNext = 0;
267  }
268  }
269  else
270  {
271  ASSERT( block.data.nextBlock != 0 );
272  myNext = block.data.nextBlock;
273  if ( idx < N )
274  {
275  myIdx = idx;
276  myNbDatas = N;
277  myDatas = block.datas;
278  }
279  else
280  {
281  idx -= N;
282  while ( idx >= M )
283  {
284  idx -= M;
285  myNext = ( myNext != 0 ) ? myNext->next : 0;
286  }
287  if ( myNext == 0 )
288  {
289  myIdx = 0;
290  myNbDatas = 0;
291  myDatas = 0;
292  }
293  else
294  {
295  myIdx = idx;
296  myNbDatas = M;
297  myDatas = myNext->datas;
298  }
299  }
300  }
301 }
302 //-----------------------------------------------------------------------------
303 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
304 inline
307 operator=( const Self & other )
308 {
309  if ( this != &other )
310  {
311  myIdx = other.myIdx;
312  myNbDatas = other.myNbDatas;
313  myDatas = other.myDatas;
314  myNext = other.myNext;
315  }
316  return *this;
317 }
318 //-----------------------------------------------------------------------------
319 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
320 inline
323 operator*() const
324 {
325  ASSERT( myDatas != 0 );
326  return myDatas[ myIdx ];
327 }
328 //-----------------------------------------------------------------------------
329 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
330 inline
333 operator->() const
334 {
335  ASSERT( myDatas != 0 );
336  return myDatas + myIdx;
337 }
338 //-----------------------------------------------------------------------------
339 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
340 inline
344 {
345  return this->operator+=( 1 );
346 }
347 //-----------------------------------------------------------------------------
348 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
349 inline
353 {
354  Self tmp( *this );
355  this->operator++();
356  return tmp;
357 }
358 //-----------------------------------------------------------------------------
359 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
360 inline
361 bool
363 operator==( const Self & other ) const
364 {
365  return ( myDatas == other.myDatas ) && ( myIdx == other.myIdx );
366 }
367 //-----------------------------------------------------------------------------
368 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
369 inline
370 bool
372 operator!=( const Self & other ) const
373 {
374  return ( myDatas != other.myDatas ) || ( myIdx != other.myIdx );
375 }
376 //-----------------------------------------------------------------------------
377 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
378 inline
382 {
383  myIdx += n;
384  while ( myIdx >= myNbDatas )
385  {
386  if ( myNext == 0 )
387  {
388  myDatas = 0;
389  myIdx = 0;
390  break;
391  }
392  myIdx -= myNbDatas;
393  myDatas = myNext->datas;
394  myNbDatas = M;
395  myNext = myNext->next;
396  }
397  return *this;
398 }
399 //-----------------------------------------------------------------------------
400 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
401 inline
405 {
406  Self tmp( *this );
407  tmp += n;
408  return *tmp;
409 }
410 
412 // class LabelledMap::ConstIterator
413 //-----------------------------------------------------------------------------
414 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
415 inline
418  : myLabelsIt( lIt ), myBlockIt( bIt )
419 {}
420 //-----------------------------------------------------------------------------
421 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
422 inline
425 {}
426 //-----------------------------------------------------------------------------
427 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
428 inline
431 {}
432 //-----------------------------------------------------------------------------
433 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
434 inline
436 ConstIterator( const ConstIterator & other )
437  : myLabelsIt( other.myLabelsIt ), myBlockIt( other.myBlockIt )
438 {}
439 //-----------------------------------------------------------------------------
440 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
441 inline
444 operator= ( const Self & other )
445 {
446  if ( this != &other )
447  {
448  myLabelsIt = other.myLabelsIt;
449  myBlockIt = other.myBlockIt;
450  }
451  return *this;
452 }
453 //-----------------------------------------------------------------------------
454 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
455 inline
458 operator*() const
459 {
460  return std::make_pair( *myLabelsIt, *myBlockIt );
461 }
462 //-----------------------------------------------------------------------------
463 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
464 inline
467 operator->() const
468 {
469  // Warning: not thread-safe.
470  static Value __static_tmp;
471  __static_tmp = this->operator*();
472  return &__static_tmp;
473 }
474 //-----------------------------------------------------------------------------
475 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
476 inline
480 {
481  ++myLabelsIt;
482  ++myBlockIt;
483  return *this;
484 }
485 //-----------------------------------------------------------------------------
486 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
487 inline
491 {
492  Self tmp( *this );
493  this->operator++();
494  return tmp;
495 }
496 //-----------------------------------------------------------------------------
497 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
498 inline
499 bool
501 operator==( const Self & other ) const
502 {
503  return myLabelsIt == other.myLabelsIt;
504 }
505 //-----------------------------------------------------------------------------
506 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
507 inline
508 bool
510 operator!=( const Self & other ) const
511 {
512  return myLabelsIt != other.myLabelsIt;
513 }
514 //-----------------------------------------------------------------------------
515 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
516 inline
519 _data() const
520 {
521  return const_cast<Data&>( *myBlockIt );
522 }
523 //-----------------------------------------------------------------------------
524 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
525 inline
528 _const_data() const
529 {
530  return *myBlockIt;
531 }
532 
534 // class LabelledMap
535 //-----------------------------------------------------------------------------
536 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
537 inline
540 { // default constructor of myLabels and myFirstBlock is automatically called.
541 }
542 //-----------------------------------------------------------------------------
543 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
544 inline
547 {
548  blockClear( size() );
549 }
550 //-----------------------------------------------------------------------------
551 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
552 inline
554 LabelledMap( const LabelledMap & other )
555  : myFirstBlock( other.myFirstBlock )
556 {
557  unsigned int s = N + 1; // there is one more stored data in the last block.
558  const __AnyBlock* nextBlock = other.myFirstBlock.data.nextBlock;
559  __AnyBlock** currentPointer = & myFirstBlock.data.nextBlock;
560  myLabels = other.myLabels;
561  while ( s < size() )
562  {
563  *currentPointer = new __AnyBlock( *nextBlock );
564  s += M;
565  currentPointer = & ( currentPointer->data.nextBlock );
566  }
567 }
568 //-----------------------------------------------------------------------------
569 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
570 template <typename InputIterator>
571 inline
573 LabelledMap( InputIterator first, InputIterator last )
574 { // default constructor of myLabels and myFirstBlock is automatically called.
575  BOOST_CONCEPT_ASSERT(( boost::InputIterator< InputIterator > ));
576  for ( ; first != last; ++first )
577  {
578  this->operator[]( first->first ) = first->second;
579  }
580 }
581 //-----------------------------------------------------------------------------
582 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
583 inline
586 operator=( const LabelledMap & other )
587 {
588  if ( this != &other )
589  {
590  blockClear( size() );
591  myLabels = other.myLabels;
592  unsigned int theSize = other.size();
593  myFirstBlock = other.myFirstBlock;
594  // there is one more stored data in the last block.
595  unsigned int s = N + 1;
596  const __AnyBlock* nextBlock = other.myFirstBlock.data.nextBlock;
597  __AnyBlock** currentPointer = & myFirstBlock.data.nextBlock;
598  while ( s < theSize )
599  {
600  *currentPointer = new __AnyBlock( *nextBlock );
601  s += M;
602  currentPointer = & ( (*currentPointer)->next );
603  }
604  }
605  return *this;
606 }
607 //-----------------------------------------------------------------------------
608 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
609 inline
610 void
613 {
614  blockClear( size() );
615  myLabels.reset();
616 }
617 //-----------------------------------------------------------------------------
618 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
619 inline
620 void
622 blockClear( unsigned int theSize )
623 {
624  if ( theSize != N+1 )
625  {
626  __AnyBlock* nextBlock = myFirstBlock.data.nextBlock;
627  while ( nextBlock != 0 )
628  {
629  __AnyBlock* ptr = nextBlock;
630  nextBlock = nextBlock->next;
631  delete ptr;
632  }
633  }
634  myFirstBlock.data.nextBlock = 0;
635 }
636 //-----------------------------------------------------------------------------
637 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
638 inline
641 labels() const
642 {
643  return myLabels;
644 }
645 //-----------------------------------------------------------------------------
646 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
647 inline
650 size() const
651 {
652  return myLabels.count();
653 }
654 //-----------------------------------------------------------------------------
655 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
656 inline
657 bool
659 empty() const
660 {
661  return size() == 0;
662 }
663 //-----------------------------------------------------------------------------
664 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
665 inline
668 max_size() const
669 {
670  return L; //SizeType( -1 ) / (2*sizeof( Data ));
671 }
672 //-----------------------------------------------------------------------------
673 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
674 inline
677 capacity() const
678 {
679  return ( size() <= (N+1) )
680  ? N+1
681  : ( 1 + ( size() - 1 - N ) / M ) * M + N;
682 }
683 //-----------------------------------------------------------------------------
684 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
685 inline
688 key_comp() const
689 {
690  return KeyCompare();
691 }
692 //-----------------------------------------------------------------------------
693 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
694 inline
697 value_comp() const
698 {
699  return ValueCompare();
700 }
701 //-----------------------------------------------------------------------------
702 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
703 inline
704 void
706 swap( Self & other )
707 {
708  std::swap( myLabels, other.myLabels );
709  std::swap( myFirstBlock, other.myFirstBlock );
710 }
711 //-----------------------------------------------------------------------------
712 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
713 inline
716 count( const Key & key ) const
717 {
718  return myLabels.test( key ) ? 1 : 0;
719 }
720 //-----------------------------------------------------------------------------
721 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
722 inline
725 blockAt( unsigned int idx ) const
726 {
727  ASSERT( idx < size() );
728  if ( idx < N )
729  return myFirstBlock.datas[ idx ];
730  else if ( ( idx == N ) && ( size() == N+1 ) )
731  return myFirstBlock.data.lastData;
732  const __AnyBlock* ptr = myFirstBlock.data.nextBlock;
733  idx -= N;
734  while ( idx >= M )
735  {
736  idx -= M;
737  ptr = ptr->next;
738  }
739  ASSERT( ptr != 0 );
740  return ptr->datas[ idx ];
741 }
742 //-----------------------------------------------------------------------------
743 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
744 inline
747 blockAt( unsigned int idx )
748 {
749  ASSERT( idx < size() );
750  if ( idx < N )
751  return myFirstBlock.datas[ idx ];
752  else if ( ( idx == N ) && ( size() == N+1 ) )
753  return myFirstBlock.data.lastData;
754  __AnyBlock* ptr = myFirstBlock.data.nextBlock;
755  idx -= N;
756  while ( idx >= M )
757  {
758  idx -= M;
759  ptr = ptr->next;
760  }
761  ASSERT( ptr != 0 );
762  return ptr->datas[ idx ];
763 }
764 //-----------------------------------------------------------------------------
765 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
766 inline
769 operator[]( const Key & key ) const
770 {
771  ASSERT( key < L );
772  bool exists = myLabels.test( key );
773  if ( ! exists )
774  {
775  unsigned int block_size = size();
776  myLabels.set( key ); // must be done before so that 'index' works.
777  return blockInsert( myLabels.index( key ), block_size, Data() );
778  }
779  else
780  {
781  return blockAt( myLabels.index( key ) );
782  }
783 }
784 //-----------------------------------------------------------------------------
785 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
786 inline
789 operator[]( const Key & key )
790 {
791  ASSERT( key < L );
792  bool exists = myLabels.test( key );
793  if ( ! exists )
794  {
795  unsigned int block_size = size();
796  myLabels.set( key ); // must be done before so that 'index' works.
797  return blockInsert( myLabels.index( key ), block_size, Data() );
798  }
799  else
800  {
801  return blockAt( myLabels.index( key ) );
802  }
803 }
804 //-----------------------------------------------------------------------------
805 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
806 inline
809 fastAt( const Key & key ) const
810 {
811  ASSERT( key < L );
812  ASSERT( myLabels.test( key ) );
813  return blockAt( myLabels.index( key ) );
814 }
815 //-----------------------------------------------------------------------------
816 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
817 inline
820 fastAt( const Key & key )
821 {
822  ASSERT( key < L );
823  ASSERT( myLabels.test( key ) );
824  return blockAt( myLabels.index( key ) );
825 }
826 //-----------------------------------------------------------------------------
827 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
828 inline
829 void
831 erase( Iterator position )
832 {
833  Key key = (*position).first;
834  ASSERT( myLabels.test( key ) );
835  blockErase( myLabels.index( key ) );
836  myLabels.reset( key );
837 }
838 //-----------------------------------------------------------------------------
839 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
840 inline
843 erase( Key key )
844 {
845  if ( myLabels.test( key ) )
846  {
847  blockErase( myLabels.index( key ) );
848  myLabels.reset( key );
849  return 1;
850  }
851  return 0;
852 }
853 //-----------------------------------------------------------------------------
854 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
855 inline
856 void
858 erase( Iterator first, Iterator last )
859 {
860  for ( ; first != last; ++first )
861  erase( first );
862 }
863 
864 //-----------------------------------------------------------------------------
865 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
866 inline
867 std::pair<typename DGtal::LabelledMap<TData, L, TWord, N, M>::Iterator, bool>
869 insert( const Value & val )
870 {
871  ASSERT( val.first < L );
872  bool exists = myLabels.test( val.first );
873  if ( ! exists )
874  {
875  unsigned int block_size = size();
876  myLabels.set( val.first ); // must be done before so that 'index' works.
877  blockInsert( myLabels.index( val.first ), block_size, val.second );
878  }
879  Iterator position = begin();
880  while ( (*position).first != val.first )
881  {
882  // std::cerr << "[" << (*position).first << "]" << std::endl;
883  ++position;
884  }
885  return std::make_pair( position, exists );
886 }
887 //-----------------------------------------------------------------------------
888 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
889 inline
892 insert ( Iterator position, const Value & val )
893 {
894  ASSERT( val.first < L );
895  if ( ! myLabels.test( val.first ) )
896  {
897  unsigned int block_size = size();
898  myLabels.set( val.first ); // must be done before so that 'index' works.
899  blockInsert( myLabels.index( val.first ), block_size, val.second );
900  }
901  position = begin();
902  while ( (*position).first != val.first )
903  {
904  //std::cerr << "[" << (*position).first << "]" << std::endl;
905  ++position;
906  }
907  return position;
908 }
909 //-----------------------------------------------------------------------------
910 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
911 template <typename InputIterator>
912 inline
913 void
915 insert( InputIterator first, InputIterator last )
916 {
917  BOOST_CONCEPT_ASSERT(( boost::InputIterator< InputIterator > ));
918  for ( ; first != last; ++first )
919  {
920  Key k = first->first;
921  if ( ! myLabels.test( k ) )
922  {
923  unsigned int block_size = size();
924  myLabels.set( k ); // must be done before so that 'index' works.
925  blockInsert( myLabels.index( k ), block_size, first->second );
926  }
927  }
928 }
929 //-----------------------------------------------------------------------------
930 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
931 inline
932 std::pair< typename DGtal::LabelledMap<TData, L, TWord, N, M>::Iterator,
935 equal_range( const Key & x )
936 {
937  Iterator it = begin(), it_end = end();
938  for ( ; it != it_end; ++it )
939  {
940  if ( (*it).first == x )
941  {
942  // JOL: g++ does not compile correctly:
943  // return std::make_pair( it, ++it );
944  it_end = it; ++it_end;
945  return std::make_pair( it, it_end );
946  }
947  else if ( (*it).first > x ) return std::make_pair( it, it );
948  }
949  return std::make_pair( it_end, it_end );
950 }
951 //-----------------------------------------------------------------------------
952 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
953 inline
954 std::pair< typename DGtal::LabelledMap<TData, L, TWord, N, M>::ConstIterator,
957 equal_range( const Key & x ) const
958 {
959  ConstIterator it = begin(), it_end = end();
960  for ( ; it != it_end; ++it )
961  {
962  if ( (*it).first == x ) return std::make_pair( it, ++it );
963  else if ( (*it).first > x ) return std::make_pair( it, it );
964  }
965  return std::make_pair( it_end, it_end );
966 }
967 //-----------------------------------------------------------------------------
968 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
969 inline
972 find( const Key & x )
973 {
974  Iterator it = begin(), it_end = end();
975  for ( ; it != it_end; ++it )
976  {
977  if ( (*it).first == x ) return it;
978  else if ( (*it).first > x ) return it_end;
979  }
980  return it_end;
981 }
982 //-----------------------------------------------------------------------------
983 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
984 inline
987 find( const Key & x ) const
988 {
989  ConstIterator it = begin(), it_end = end();
990  for ( ; it != it_end; ++it )
991  {
992  if ( (*it).first == x ) return it;
993  else if ( (*it).first > x ) return it_end;
994  }
995  return it_end;
996 }
997 //-----------------------------------------------------------------------------
998 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
999 inline
1002 lower_bound( const Key & x )
1003 {
1004  Iterator it = begin(), it_end = end();
1005  for ( ; it != it_end; ++it )
1006  {
1007  if ( (*it).first >= x ) return it;
1008  }
1009  return it_end;
1010 }
1011 //-----------------------------------------------------------------------------
1012 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1013 inline
1016 lower_bound( const Key & x ) const
1017 {
1018  ConstIterator it = begin(), it_end = end();
1019  for ( ; it != it_end; ++it )
1020  {
1021  if ( (*it).first >= x ) return it;
1022  }
1023  return it_end;
1024 }
1025 //-----------------------------------------------------------------------------
1026 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1027 inline
1030 upper_bound( const Key & x )
1031 {
1032  Iterator it = begin(), it_end = end();
1033  for ( ; it != it_end; ++it )
1034  {
1035  if ( (*it).first > x ) return it;
1036  }
1037  return it_end;
1038 }
1039 //-----------------------------------------------------------------------------
1040 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1041 inline
1044 upper_bound( const Key & x ) const
1045 {
1046  ConstIterator it = begin(), it_end = end();
1047  for ( ; it != it_end; ++it )
1048  {
1049  if ( (*it).first > x ) return it;
1050  }
1051  return it_end;
1052 }
1053 
1054 //-----------------------------------------------------------------------------
1055 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1056 inline
1059 blockInsert( unsigned int idx, unsigned int block_size, const Data & data )
1060 {
1061  ASSERT( idx <= block_size ); // end is ok.
1062  return myFirstBlock.insert( idx, block_size, data );
1063 }
1064 //-----------------------------------------------------------------------------
1065 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1066 inline
1067 void
1069 blockErase( unsigned int idx )
1070 {
1071  ASSERT( idx < size() ); // end is not ok.
1072  myFirstBlock.erase( idx, size() );
1073 }
1074 //-----------------------------------------------------------------------------
1075 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1076 inline
1080 {
1081  const Self* ptr = (const Self *) this;
1082  return Iterator( myLabels.begin(), ptr->blockBegin() );
1083 }
1084 //-----------------------------------------------------------------------------
1085 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1086 inline
1090 {
1091  const Self* ptr = (const Self *) this;
1092  return Iterator( myLabels.end(), ptr->blockEnd() );
1093 }
1094 //-----------------------------------------------------------------------------
1095 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1096 inline
1099 begin() const
1100 {
1101  return ConstIterator( myLabels.begin(), blockBegin() );
1102 }
1103 //-----------------------------------------------------------------------------
1104 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1105 inline
1108 end() const
1109 {
1110  return ConstIterator( myLabels.end(), blockEnd() );
1111 }
1112 //-----------------------------------------------------------------------------
1113 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1114 inline
1118 {
1119  return BlockIterator( myFirstBlock, 0, size() );
1120 }
1121 //-----------------------------------------------------------------------------
1122 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1123 inline
1127 {
1128  SizeType s = size();
1129  return BlockIterator( myFirstBlock, s, s );
1130 }
1131 //-----------------------------------------------------------------------------
1132 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1133 inline
1136 blockBegin() const
1137 {
1138  return BlockConstIterator( myFirstBlock, 0, size() );
1139 }
1140 //-----------------------------------------------------------------------------
1141 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1142 inline
1145 blockEnd() const
1146 {
1147  SizeType s = size();
1148  return BlockConstIterator( myFirstBlock, s, s );
1149 }
1150 
1152 // Interface - public :
1153 
1158 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1159 inline
1160 void
1162 selfDisplay( std::ostream & out ) const
1163 {
1164  if ( size() == 0 ) out << "()";
1165  else
1166  {
1167  ConstIterator it = begin();
1168  ConstIterator it_end = end();
1169  out << "( ";
1170  out << "(" << (*it).first << "," << (*it).second << ")";
1171  ++it;
1172  for ( ; it != it_end; ++it )
1173  {
1174  out << ",(" << (*it).first << "," << (*it).second << ")";
1175  }
1176  out << " )";
1177  }
1178  // {
1179  // BlockConstIterator it = blockBegin();
1180  // BlockConstIterator it_end = blockEnd();
1181  // BlockConstIterator it_last = it;
1182  // out << "(";
1183  // out << *it;
1184  // ++it;
1185  // for ( ; it != it_end; ++it )
1186  // {
1187  // out << ( ( it_last.myDatas == it.myDatas ) ? ',' : ';' );
1188  // out << *it;
1189  // it_last = it;
1190  // }
1191  // out << ")";
1192  // }
1193 }
1194 
1199 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1200 inline
1201 bool
1203 {
1204  return true;
1205 }
1206 
1207 
1208 
1210 // Implementation of inline functions //
1211 
1212 template <typename TData, unsigned int L, typename TWord, unsigned int N, unsigned int M>
1213 inline
1214 std::ostream&
1215 DGtal::operator<< ( std::ostream & out,
1216  const LabelledMap<TData, L, TWord, N, M> & object )
1217 {
1218  object.selfDisplay( out );
1219  return out;
1220 }
1221 
1222 template <typename TData>
1223 std::pair< unsigned int, unsigned int >
1225 ( unsigned int L, double prob_no_data, double prob_one_data )
1226 {
1227  unsigned int sL = 2 * ( ( ( L - 1 ) / 16 ) + 1 ); // word of 16bits
1228  // std::cerr << "L=" << L << " sL=" << sL << std::endl;
1229  LabelledMapMemFunctor F( prob_one_data, prob_no_data, sL, sizeof( TData ),
1230  sizeof( TData* ), 2 );
1231  double m = -1.0;
1232  unsigned int Nopt = 0;
1233  unsigned int Mopt = 0;
1234  for ( unsigned int N = 1; N < L; ++N )
1235  for ( unsigned int M = 2; M < (L-N); ++M )
1236  {
1237  // std::cerr << "Fmem(" << N << "," << M << ")="
1238  // << F.fctNM( N, M ) << std::endl;
1239  if ( ( m < 0.0 ) || ( F.fctNM( N, M ) < m ) )
1240  {
1241  Nopt = N; Mopt = M;
1242  m = F.fctNM( Nopt, Mopt );
1243  }
1244  }
1245  return std::make_pair( Nopt, Mopt );
1246 }
1247 
1248 // //
1250 
1251