This file is indexed.

/usr/include/InsightToolkit/Review/Statistics/itkHistogram.txx is in libinsighttoolkit3-dev 3.20.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkHistogram.txx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __itkHistogram_txx
#define __itkHistogram_txx

#include "itkHistogram.h"
#include "itkNumericTraits.h"

namespace itk { 
namespace Statistics {

template< class TMeasurement, class TFrequencyContainer >
Histogram<TMeasurement, TFrequencyContainer >
::Histogram()
{
  this->m_ClipBinsAtEnds = true;
  this->m_FrequencyContainer = FrequencyContainerType::New();
  this->m_OffsetTable = OffsetTableType( this->GetMeasurementVectorSize() + 1 );
  for( unsigned int i = 0; i < this->GetMeasurementVectorSize() + 1; i++ )
    {
    this->m_OffsetTable[i] = itk::NumericTraits< InstanceIdentifier >::Zero;
    }
}

template< class TMeasurement, class TFrequencyContainer >
typename 
Histogram<TMeasurement, TFrequencyContainer >::InstanceIdentifier
Histogram<TMeasurement, TFrequencyContainer >
::Size() const
{
  if( this->GetMeasurementVectorSize() == 0 )
    {
    return itk::NumericTraits< InstanceIdentifier >::Zero;
    }
  InstanceIdentifier size = 1;
  for (unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++)
    {
    size *= m_Size[i];
    }
  return size;
}

template< class TMeasurement, class TFrequencyContainer >
const typename 
Histogram<TMeasurement, TFrequencyContainer >::SizeType &
Histogram<TMeasurement, TFrequencyContainer >
::GetSize() const
{
  return m_Size;
}

template< class TMeasurement, class TFrequencyContainer >
typename 
Histogram<TMeasurement, TFrequencyContainer >::SizeValueType
Histogram<TMeasurement, TFrequencyContainer >
::GetSize(unsigned int dimension) const
{
  return m_Size[dimension];
}

template< class TMeasurement, class TFrequencyContainer >
const typename 
Histogram<TMeasurement, TFrequencyContainer >::MeasurementType &
Histogram<TMeasurement, TFrequencyContainer >
::GetBinMin(unsigned int dimension, InstanceIdentifier nbin) const
{
  return m_Min[dimension][nbin];
}

template< class TMeasurement, 
          class TFrequencyContainer >
const typename 
Histogram<TMeasurement, TFrequencyContainer >::MeasurementType &
Histogram<TMeasurement, TFrequencyContainer >
::GetBinMax(unsigned int dimension, InstanceIdentifier nbin) const
{
  return m_Max[dimension][nbin];
}

template< class TMeasurement, class TFrequencyContainer >
void
Histogram<TMeasurement, TFrequencyContainer >
::SetBinMin( unsigned int dimension, InstanceIdentifier nbin,
                 MeasurementType min)
{
  m_Min[dimension][nbin] = min;
}

template< class TMeasurement, class TFrequencyContainer >
void
Histogram<TMeasurement, TFrequencyContainer >
::SetBinMax( unsigned int dimension, InstanceIdentifier nbin,
                 MeasurementType max)
{
  m_Max[dimension][nbin] = max;
}

template< class TMeasurement, class TFrequencyContainer >
const typename 
Histogram<TMeasurement, TFrequencyContainer >::BinMinVectorType &
Histogram<TMeasurement, TFrequencyContainer >
::GetDimensionMins(unsigned int dimension) const
{
  return m_Min[dimension];
}

template< class TMeasurement, class TFrequencyContainer >
const typename 
Histogram<TMeasurement, TFrequencyContainer >::BinMaxVectorType &
Histogram<TMeasurement, TFrequencyContainer >
::GetDimensionMaxs(unsigned int dimension) const
{
  return m_Max[dimension];
}

template< class TMeasurement, class TFrequencyContainer >
const typename 
Histogram<TMeasurement, TFrequencyContainer >::BinMinContainerType &
Histogram<TMeasurement, TFrequencyContainer >
::GetMins() const
{
  return m_Min;
}

template< class TMeasurement, class TFrequencyContainer >
const typename 
Histogram<TMeasurement, TFrequencyContainer >::BinMaxContainerType &
Histogram<TMeasurement, TFrequencyContainer >
::GetMaxs() const
{
  return m_Max;
}

template< class TMeasurement, class TFrequencyContainer >
typename 
Histogram<TMeasurement, TFrequencyContainer >::AbsoluteFrequencyType
Histogram<TMeasurement, TFrequencyContainer >
::GetFrequency( InstanceIdentifier id ) const
{
  return m_FrequencyContainer->GetFrequency(id);
}

template< class TMeasurement, class TFrequencyContainer >
bool 
Histogram<TMeasurement, TFrequencyContainer >
::SetFrequency( InstanceIdentifier id, AbsoluteFrequencyType value)
{
  return m_FrequencyContainer->SetFrequency(id, value);
}

template< class TMeasurement, class TFrequencyContainer >
bool 
Histogram<TMeasurement, TFrequencyContainer >
::IncreaseFrequency(InstanceIdentifier id, AbsoluteFrequencyType value)
{
  return m_FrequencyContainer->IncreaseFrequency(id, value);
}


template< class TMeasurement, class TFrequencyContainer >
void
Histogram<TMeasurement, TFrequencyContainer >
::Initialize(const SizeType &size)
{

  if( this->GetMeasurementVectorSize() == 0 )
    {
    itkExceptionMacro("MeasurementVectorSize is Zero. It should be set to a non-zero value before calling Initialize");
    }

  this->m_Size = size;
  
  // creates offset table which will be used for generation of
  // instance identifiers.
  InstanceIdentifier num = 1;
  
  this->m_OffsetTable.resize( this->GetMeasurementVectorSize() + 1 );

  this->m_OffsetTable[0] = num;
  for (unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++)
    {
    num *= m_Size[i];
    this->m_OffsetTable[i + 1] = num;
    }

  this->m_TempIndex.SetSize( this->GetMeasurementVectorSize() );

  m_NumberOfInstances = num;

  // adjust the sizes of min max value containers 
  unsigned int dim;
  m_Min.resize(this->GetMeasurementVectorSize());
  for ( dim = 0; dim < this->GetMeasurementVectorSize(); dim++)
    {
    m_Min[dim].resize(m_Size[dim]);
    } 

  m_Max.resize(this->GetMeasurementVectorSize());
  for ( dim = 0; dim < this->GetMeasurementVectorSize(); dim++)
    {
    m_Max[dim].resize(m_Size[dim]);
    } 

  // initialize auxiliary variables
  this->m_TempIndex.SetSize( this->GetMeasurementVectorSize() );
  this->m_TempMeasurementVector.SetSize( this->GetMeasurementVectorSize() );

  // initialize the frequency container
  m_FrequencyContainer->Initialize(this->m_OffsetTable[this->GetMeasurementVectorSize()]);
  this->SetToZero();
}

template< class TMeasurement, class TFrequencyContainer >
void 
Histogram<TMeasurement, TFrequencyContainer >
::SetToZero()
{
  m_FrequencyContainer->SetToZero();
}

template< class TMeasurement, class TFrequencyContainer >
void 
Histogram<TMeasurement, TFrequencyContainer >
::Initialize(const SizeType &size, MeasurementVectorType& lowerBound,
             MeasurementVectorType& upperBound)
{
  this->Initialize(size);

  float interval;
  for ( unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++)
    {
    if( size[i] > 0 )
      {
      interval = (float) (upperBound[i] - lowerBound[i]) 
                       / static_cast< MeasurementType >(size[i]);

      // Set the min vector and max vector
      for (unsigned int j = 0; j < static_cast<unsigned int>(size[i] - 1); j++)
        {
        this->SetBinMin(i, j, (MeasurementType)(lowerBound[i] +  
                                                ((float)j * interval)));
        this->SetBinMax(i, j, (MeasurementType)(lowerBound[i] +  
                                                (((float)j + 1) * interval)));
        }
      this->SetBinMin(i, size[i] - 1, 
                      (MeasurementType)(lowerBound[i] + 
                                        (((float) size[i] - 1) * interval)));
      this->SetBinMax(i, size[i] - 1, 
                      (MeasurementType)(upperBound[i]));
      }
    }
}

template< class TMeasurement, class TFrequencyContainer >
inline const typename Histogram<TMeasurement, TFrequencyContainer >::IndexType &
Histogram<TMeasurement, TFrequencyContainer >
::GetIndex(const MeasurementVectorType& measurement) const
{
  // Have this deprecated method call the un-deprecated one.. 
  this->GetIndex( measurement, m_TempIndex );
  return m_TempIndex;
}


/** */
template< class TMeasurement, class TFrequencyContainer >
bool Histogram<TMeasurement, TFrequencyContainer >
::GetIndex(const MeasurementVectorType & measurement,IndexType & index ) const
{
  // now using something similar to binary search to find
  // index.
  unsigned int dim;

  if( index.Size() != this->GetMeasurementVectorSize() )
    {
    index.SetSize( this->GetMeasurementVectorSize() );
    }
  
  int begin;
  int mid;
  int end;

  MeasurementType median;
  MeasurementType tempMeasurement;

  for (dim = 0; dim < this->GetMeasurementVectorSize(); dim++)
    {
    tempMeasurement = measurement[dim];
    begin = 0;
    if (tempMeasurement < m_Min[dim][begin])
      {
      // one of measurement is below the minimum
      // its ok if we extend the bins to infinity.. not ok if we don't
      if(!m_ClipBinsAtEnds)
        {
        index[dim] = (long) 0;
        continue;
        }
      else
        { // set an illegal value and return 0
        index[dim] = (long) m_Size[dim]; 
        return false;
        }
      }

    end = m_Min[dim].size() - 1;
    if (tempMeasurement >= m_Max[dim][end])
      {
      // one of measurement is above the maximum
      // its ok if we extend the bins to infinity.. not ok if we don't
      if(!m_ClipBinsAtEnds)
        {
        index[dim] = (long) m_Size[dim]-1;
        continue;
        }
      else
        { // set an illegal value and return 0
        index[dim] = (long) m_Size[dim]; 
        return false;
        }
      }

    // Binary search for the bin where this measurement could be
    mid = (end + 1) / 2;
    median = m_Min[dim][mid];

    while(true)
      {
      if (tempMeasurement < median )
        {
        end = mid - 1;
        } 
      else if (tempMeasurement > median)
        {
        // test whether it is inside the current bin by comparing to the max of this bin.
        if( tempMeasurement <  m_Max[dim][mid] && 
            tempMeasurement >= m_Min[dim][mid] )
          {
          index[dim] = mid;
          break;
          }
        // otherwise, continue binary search
        begin = mid + 1;
        }
      else
        {
        index[dim] = mid;
        break;
        }
      mid = begin + (end - begin) / 2;
      median = m_Min[dim][mid];
      } // end of while
    } // end of for()
  return true;
}


template< class TMeasurement, class TFrequencyContainer >
inline const typename Histogram<TMeasurement, TFrequencyContainer >::IndexType&
Histogram<TMeasurement, TFrequencyContainer >
::GetIndex( InstanceIdentifier id ) const
{
  InstanceIdentifier id2 = id;

  for (int i = this->GetMeasurementVectorSize() - 1; i > 0; i--)
    {
    m_TempIndex[i] = static_cast<IndexValueType>(id2 / this->m_OffsetTable[i]);
    id2 -= (m_TempIndex[i] * this->m_OffsetTable[i]);
    }
  m_TempIndex[0] = static_cast<IndexValueType>(id2);
  
  return m_TempIndex;
}


template< class TMeasurement, class TFrequencyContainer >
inline bool
Histogram<TMeasurement, TFrequencyContainer >
::IsIndexOutOfBounds(const IndexType &index) const
{
  for (unsigned int dim = 0; dim < this->GetMeasurementVectorSize(); dim++)
    {
    if (index[dim] < 0 || index[dim] >= static_cast<IndexValueType>(m_Size[dim]))
      {
      return true;
      }
    }
  return false;
}

template< class TMeasurement, class TFrequencyContainer >
inline typename Histogram<TMeasurement, TFrequencyContainer >::InstanceIdentifier
Histogram<TMeasurement, TFrequencyContainer >
::GetInstanceIdentifier(const IndexType &index) const
{
  InstanceIdentifier instanceId = 0;
  for (int i= this->GetMeasurementVectorSize() - 1; i > 0; i-- )
    {
    instanceId += index[i] * this->m_OffsetTable[i];
    }
  
  instanceId += index[0];
  
  return instanceId;
}


template< class TMeasurement, class TFrequencyContainer >
inline const typename Histogram<TMeasurement, TFrequencyContainer >::MeasurementType&
Histogram<TMeasurement, TFrequencyContainer >
::GetBinMinFromValue(const unsigned int dimension, const float value ) const
{
  // If the value is lower than any of min value in the Histogram,
  // it returns the lowest min value
  if ( value <= this->m_Min[dimension][0] )
    {
    return this->m_Min[dimension][0];
    }

  // If the value is higher than any of min value in the Histogram,
  // it returns the highest min value
  if ( value >= m_Min[dimension][m_Size[dimension]-1] )
    {
    return m_Min[dimension][this->m_Size[dimension]-1];
    }

  unsigned int binMinFromValue = 0;

  for ( unsigned int i = 0; i < this->m_Size[dimension]; i++ )
    {
    if (  (value >= this->m_Min[dimension][i])
          && (value <  this->m_Max[dimension][i])  )
      {
      binMinFromValue = i;
      }
    }

  return this->m_Min[dimension][binMinFromValue];
}

template< class TMeasurement, class TFrequencyContainer >
inline const typename Histogram< TMeasurement, TFrequencyContainer >::MeasurementType&
Histogram< TMeasurement, TFrequencyContainer >
::GetBinMaxFromValue(const unsigned int dimension, const float value ) const
{
  // If the value is lower than any of max value in the Histogram,
  // it returns the lowest max value
  if ( value <= this->m_Max[dimension][0] )
    {
    return this->m_Max[dimension][0];
    }

  // If the value is higher than any of max value in the Histogram,
  // it returns the highest max value
  if ( value >= m_Max[dimension][m_Size[dimension]-1] )
    {
    return m_Max[dimension][this->m_Size[dimension]-1];
    }

  unsigned int binMaxFromValue = 0;

  for ( unsigned int i = 0; i < this->m_Size[dimension]; i++ )
    {
    if (  (value >= this->m_Min[dimension][i])
          && (value <  this->m_Max[dimension][i])  )
      {
      binMaxFromValue = i;
      }
    }

  return this->m_Max[dimension][binMaxFromValue];
}

template< class TMeasurement, class TFrequencyContainer >
const typename 
Histogram< TMeasurement, TFrequencyContainer >::MeasurementVectorType&
Histogram< TMeasurement, TFrequencyContainer >
::GetHistogramMinFromIndex(const IndexType &index) const
{
  for( unsigned int i=0; i < this->GetMeasurementVectorSize(); i++ )
    {
    m_TempMeasurementVector[i] = this->GetBinMin(i, index[i]);
    }
  return m_TempMeasurementVector;
}

template< class TMeasurement, class TFrequencyContainer >
const typename 
Histogram< TMeasurement, TFrequencyContainer >::MeasurementVectorType&
Histogram< TMeasurement, TFrequencyContainer >
::GetHistogramMaxFromIndex(const IndexType &index) const
{
  for( unsigned int i=0; i < this->GetMeasurementVectorSize(); i++ )
    {
    m_TempMeasurementVector[i] = this->GetBinMax(i, index[i]);
    }
  return m_TempMeasurementVector;
}

template< class TMeasurement, class TFrequencyContainer >
inline const typename Histogram< TMeasurement, TFrequencyContainer >::MeasurementVectorType &
Histogram< TMeasurement, TFrequencyContainer >
::GetMeasurementVector(const IndexType &index) const
{
  for ( unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++)
    {
    MeasurementType value = (m_Min[i][index[i]] + m_Max[i][index[i]]);
    m_TempMeasurementVector[i] =  static_cast< MeasurementType >( value / 2.0 );
    }
  return m_TempMeasurementVector;
}

template< class TMeasurement, class TFrequencyContainer >
inline const typename Histogram< TMeasurement, TFrequencyContainer >::MeasurementVectorType &
Histogram< TMeasurement, TFrequencyContainer >
::GetMeasurementVector( InstanceIdentifier id) const
{
  return this->GetMeasurementVector( this->GetIndex(id) );
}

template< class TMeasurement, class TFrequencyContainer >
inline void
Histogram< TMeasurement, TFrequencyContainer >
::SetFrequency(const AbsoluteFrequencyType value) 
{
  typename Self::Iterator iter = this->Begin();
  typename Self::Iterator end = this->End();
  
  while ( iter != end )
    {
    iter.SetFrequency(value);
    ++iter;
    }
}

template< class TMeasurement, class TFrequencyContainer >
inline bool
Histogram< TMeasurement, TFrequencyContainer >
::SetFrequency(const IndexType &index, const AbsoluteFrequencyType value) 
{
  return this->SetFrequency( this->GetInstanceIdentifier(index), value);
}
  
template< class TMeasurement, class TFrequencyContainer >
inline bool
Histogram< TMeasurement, TFrequencyContainer >
::SetFrequency(const MeasurementVectorType &measurement, const AbsoluteFrequencyType value) 
{
  return this->SetFrequency( this->GetInstanceIdentifier(GetIndex(measurement)), value);
}

template< class TMeasurement, class TFrequencyContainer >
inline bool
Histogram< TMeasurement, TFrequencyContainer >
::IncreaseFrequency(const IndexType &index, const AbsoluteFrequencyType value)
{
  const bool result = 
      this->IncreaseFrequency( this->GetInstanceIdentifier(index), value);
  return result;
}
  
template< class TMeasurement, class TFrequencyContainer >
inline bool
Histogram< TMeasurement, TFrequencyContainer >
::IncreaseFrequency(const MeasurementVectorType &measurement, const AbsoluteFrequencyType value) 
{
  IndexType index;
  this->GetIndex( measurement, index );
  return this->IncreaseFrequency( this->GetInstanceIdentifier( index ), value );
}


template< class TMeasurement, class TFrequencyContainer >
inline typename Histogram< TMeasurement, TFrequencyContainer >::AbsoluteFrequencyType
Histogram< TMeasurement, TFrequencyContainer >
::GetFrequency(const IndexType &index) const
{
  return ( this->GetFrequency( this->GetInstanceIdentifier(index)) );
}

template< class TMeasurement, class TFrequencyContainer >
typename 
Histogram< TMeasurement, TFrequencyContainer >::MeasurementType 
Histogram< TMeasurement, TFrequencyContainer >
::GetMeasurement( InstanceIdentifier n, unsigned int dimension) const
{
  return static_cast< MeasurementType >((m_Min[dimension][n] + 
                                         m_Max[dimension][n]) / 2); 
}

template< class TMeasurement, class TFrequencyContainer >
typename 
Histogram< TMeasurement, TFrequencyContainer >::AbsoluteFrequencyType
Histogram< TMeasurement, TFrequencyContainer >
::GetFrequency( InstanceIdentifier n, unsigned int dimension) const
{
  InstanceIdentifier nextOffset = this->m_OffsetTable[dimension + 1];
  InstanceIdentifier current = this->m_OffsetTable[dimension] * n;
  InstanceIdentifier includeLength = this->m_OffsetTable[dimension];
  InstanceIdentifier include;
  InstanceIdentifier includeEnd;
  InstanceIdentifier last = this->m_OffsetTable[this->GetMeasurementVectorSize()];

  AbsoluteFrequencyType frequency = 0;
  while (current < last)
    {
    include = current;
    includeEnd = include + includeLength;
    while(include < includeEnd)
      {
      frequency += GetFrequency(include);
      include++;
      }
    current += nextOffset;
    }
  return frequency;
}

template< class TMeasurement, class TFrequencyContainer >
inline typename Histogram< TMeasurement, TFrequencyContainer >::TotalAbsoluteFrequencyType
Histogram< TMeasurement, TFrequencyContainer >
::GetTotalFrequency() const
{
  return m_FrequencyContainer->GetTotalFrequency();
}

template< class TMeasurement, class TFrequencyContainer >
double
Histogram< TMeasurement, TFrequencyContainer >
::Quantile(unsigned int dimension, double p) const
{
  InstanceIdentifier n;
  const unsigned int size = this->GetSize(dimension);
  double p_n_prev;
  double p_n;
  double f_n;
  double cumulated = 0;
  double totalFrequency = double( this->GetTotalFrequency() );
  double binProportion;
  double min, max, interval;

  if ( p < 0.5 )
    {
    n = 0;
    p_n = NumericTraits< double >::Zero;
    do 
      {
      f_n = this->GetFrequency(n, dimension);
      cumulated += f_n;
      p_n_prev = p_n;
      p_n = cumulated / totalFrequency;
      n++;
      } 
    while( n < size && p_n < p);

    binProportion = f_n / totalFrequency;

    min = double( this->GetBinMin(dimension, n - 1) );
    max = double( this->GetBinMax(dimension, n - 1) );
    interval = max - min;
    return min + ((p - p_n_prev) / binProportion) * interval;
    }
  else
    {
    n = size - 1;
    InstanceIdentifier m = NumericTraits< InstanceIdentifier >::Zero;
    p_n      = NumericTraits< double >::One;
    do 
      {
      f_n = this->GetFrequency(n, dimension);
      cumulated += f_n;
      p_n_prev = p_n;
      p_n = NumericTraits< double >::One - cumulated / totalFrequency;
      n--;
      m++;
      } 
    while( m < size && p_n > p);

    binProportion = f_n / totalFrequency;
    min = double( this->GetBinMin(dimension, n + 1) );
    max = double( this->GetBinMax(dimension, n + 1) );
    interval = max - min;
    return max - ((p_n_prev - p) / binProportion) * interval;
    }
}

template< class TMeasurement, class TFrequencyContainer >
void 
Histogram< TMeasurement, TFrequencyContainer >
::PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os,indent);

  os << indent << "OffsetTable: " <<  std::endl;
  for(unsigned int i=0; i < this->m_OffsetTable.size(); i++)
    {
    os << this->m_OffsetTable[i] << "  ";
    }
  os << std::endl;
  os << indent << "ClipBinsAtEnds: " <<  
    itk::NumericTraits<bool>::PrintType( this->GetClipBinsAtEnds() ) << std::endl;
  os << indent << "FrequencyContainerPointer: " << m_FrequencyContainer
     << std::endl;
}

template< class TMeasurement, class TFrequencyContainer >
void 
Histogram< TMeasurement, TFrequencyContainer >
::Graft( const DataObject *thatObject )
{
  this->Superclass::Graft(thatObject);

  const Self *thatConst = dynamic_cast< const Self * >(thatObject);
  if (thatConst)
    {
    Self *that = const_cast< Self * >(thatConst); 
    this->m_Size                  = that->m_Size;
    this->m_OffsetTable           = that->m_OffsetTable;
    this->m_FrequencyContainer    = that->m_FrequencyContainer;
    this->m_NumberOfInstances     = that->m_NumberOfInstances;
    this->m_Min                   = that->m_Min;
    this->m_Max                   = that->m_Max;
    this->m_TempMeasurementVector = that->m_TempMeasurementVector;
    this->m_TempIndex             = that->m_TempIndex;
    this->m_ClipBinsAtEnds        = that->m_ClipBinsAtEnds;
    }
}

} // end of namespace Statistics 
} // end of namespace itk 

#endif