This file is indexed.

/usr/include/ITK-4.5/itkStatisticsAlgorithm.hxx is in libinsighttoolkit4-dev 4.5.0-3.

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
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef __itkStatisticsAlgorithm_hxx
#define __itkStatisticsAlgorithm_hxx

#include "itkStatisticsAlgorithm.h"
#include "itkNumericTraits.h"

namespace itk
{
namespace Statistics
{
namespace Algorithm
{
template< typename TSize >
inline TSize FloorLog(TSize size)
{
  TSize k;

  for ( k = 0; size != 1; size >>= 1 )
    {
    ++k;
    }

  return k;
}

/** The endIndex should points one point after the last elements if multiple
 * partitionValue exist in the sample the return index will points the middle
 * of such values. Implemented following the description of the partition
 * algorithm in the QuickSelect entry of the Wikipedia.
 * http://en.wikipedia.org/wiki/Selection_algorithm. */
template< typename TSubsample >
inline int Partition(TSubsample *sample,
                     unsigned int activeDimension,
                     int beginIndex, int endIndex,
                     const typename TSubsample::MeasurementType partitionValue)
{
  typedef typename TSubsample::MeasurementType SampleMeasurementType;

  int moveToFrontIndex = beginIndex;
  int moveToBackIndex = endIndex - 1;

  //
  // Move to the back all entries whose activeDimension component is equal to
  // the partitionValue.
  //
  while ( moveToFrontIndex < moveToBackIndex )
    {
    //
    //  Find the first element (from the back) that is in the wrong side of the
    // partition.
    //
    while ( moveToBackIndex >= beginIndex )
      {
      if ( sample->GetMeasurementVectorByIndex(moveToBackIndex)[activeDimension] != partitionValue )
        {
        break;
        }
      moveToBackIndex--;
      }

    //
    //  Find the first element (from the front) that is in the wrong side of the
    // partition.
    //
    while ( moveToFrontIndex < endIndex )
      {
      if ( sample->GetMeasurementVectorByIndex(moveToFrontIndex)[activeDimension] == partitionValue )
        {
        break;
        }
      moveToFrontIndex++;
      }

    if ( moveToFrontIndex < moveToBackIndex )
      {
      //
      // Swap them to place them in the correct side of the partition
      //
      sample->Swap(moveToBackIndex, moveToFrontIndex);
      }
    }

  //
  // Now, ignore the section at the end with all the values equal to the
  // partition value,
  // and start swapping entries that are in the wrong side of the partition.
  //
  moveToFrontIndex = beginIndex;
  moveToBackIndex  = endIndex - 1;
  while ( moveToFrontIndex < moveToBackIndex )
    {
    //
    //  Find the first element (from the back) that is in the wrong side of the
    // partition.
    //
    while ( moveToBackIndex >= beginIndex )
      {
      if ( sample->GetMeasurementVectorByIndex(moveToBackIndex)[activeDimension] < partitionValue )
        {
        break;
        }
      moveToBackIndex--;
      }

    //
    //  Find the first element (from the front) that is in the wrong side of the
    // partition.
    //
    while ( moveToFrontIndex < endIndex )
      {
      if ( sample->GetMeasurementVectorByIndex(moveToFrontIndex)[activeDimension] > partitionValue )
        {
        break;
        }
      moveToFrontIndex++;
      }

    if ( moveToFrontIndex < moveToBackIndex )
      {
      //
      // Swap them to place them in the correct side of the partition
      //
      sample->Swap(moveToBackIndex, moveToFrontIndex);
      }
    }

  //
  // Take all the entries with activeDimension component equal to
  // partitionValue, that were placed at the end of the list, and move them to
  // the interface between smaller and larger values.
  //
  int beginOfSectionEqualToPartition = moveToFrontIndex;
  moveToBackIndex = endIndex - 1;
  while ( moveToFrontIndex < moveToBackIndex )
    {
    //
    //  Find the first element (from the back) that is in the wrong side of the
    // partition.
    //
    while ( moveToBackIndex >= beginIndex )
      {
      if ( sample->GetMeasurementVectorByIndex(moveToBackIndex)[activeDimension] == partitionValue )
        {
        break;
        }
      moveToBackIndex--;
      }

    //
    //  Find the first element (from the front) that is in the wrong side of the
    // partition.
    //
    while ( moveToFrontIndex < endIndex )
      {
      if ( sample->GetMeasurementVectorByIndex(moveToFrontIndex)[activeDimension] != partitionValue )
        {
        break;
        }
      moveToFrontIndex++;
      }

    if ( moveToFrontIndex < moveToBackIndex )
      {
      //
      // Swap them to place them in the correct side of the partition
      //
      sample->Swap(moveToBackIndex, moveToFrontIndex);
      }
    }
  int endOfSectionEqualToPartition = moveToFrontIndex - 1;

  int storeIndex = ( beginOfSectionEqualToPartition + endOfSectionEqualToPartition ) / 2;

  const SampleMeasurementType pivotValue  = sample->GetMeasurementVectorByIndex(storeIndex)[activeDimension];
  if ( pivotValue != partitionValue )
    {
    // The partition was done using a value that is not present in the sample.
    // Therefore we must now find the largest value of the left section and
    // swap it to the boundary between smaller and larger than the
    // partitionValue.
    for ( int kk = beginIndex; kk < storeIndex; kk++ )
      {
      SampleMeasurementType nodeValue      = sample->GetMeasurementVectorByIndex(kk)[activeDimension];
      SampleMeasurementType boundaryValue  = sample->GetMeasurementVectorByIndex(storeIndex)[activeDimension];
      if ( nodeValue > boundaryValue )
        {
        sample->Swap(kk, storeIndex);
        }
      }
    }

  return storeIndex;
}

template< typename TValue >
inline TValue MedianOfThree(const TValue a,
                            const TValue b,
                            const TValue c)
{
  if ( a < b )
    {
    if ( b < c )
      {
      return b;
      }
    else if ( a < c )
      {
      return c;
      }
    else
      {
      return a;
      }
    }
  else if ( a < c )
    {
    return a;
    }
  else if ( b < c )
    {
    return c;
    }
  else
    {
    return b;
    }
}

template< typename TSample >
inline void FindSampleBound(const TSample *sample,
                            typename TSample::ConstIterator begin,
                            typename TSample::ConstIterator end,
                            typename TSample::MeasurementVectorType & min,
                            typename TSample::MeasurementVectorType & max)
{
  typedef typename TSample::MeasurementVectorSizeType MeasurementVectorSizeType;

  const MeasurementVectorSizeType Dimension = sample->GetMeasurementVectorSize();
  if ( Dimension == 0 )
    {
    itkGenericExceptionMacro(
      << "Length of a sample's measurement vector hasn't been set.");
    }
  // Sanity check
  MeasurementVectorTraits::Assert(max, Dimension,
                                  "Length mismatch StatisticsAlgorithm::FindSampleBound");
  MeasurementVectorTraits::Assert(min, Dimension,
                                  "Length mismatch StatisticsAlgorithm::FindSampleBound");

  if ( sample->Size() == 0 )
    {
    itkGenericExceptionMacro(
      << "Attempting to compute bounds of a sample list containing no\
       measurement vectors");
    }

  unsigned int dimension;
  typename TSample::MeasurementVectorType temp;

  min = max = temp = begin.GetMeasurementVector();
  while ( true )
    {
    for ( dimension = 0; dimension < Dimension; dimension++ )
      {
      if ( temp[dimension] < min[dimension] )
        {
        min[dimension] = temp[dimension];
        }
      else if ( temp[dimension] > max[dimension] )
        {
        max[dimension] = temp[dimension];
        }
      }
    ++begin;
    if ( begin == end )
      {
      break;
      }
    temp = begin.GetMeasurementVector();
    }   // end of while
}

/** The endIndex should points one point after the last elements. */
template< typename TSubsample >
inline void
FindSampleBoundAndMean(const TSubsample *sample,
                       int beginIndex,
                       int endIndex,
                       typename TSubsample::MeasurementVectorType & min,
                       typename TSubsample::MeasurementVectorType & max,
                       typename TSubsample::MeasurementVectorType & mean)
{
  typedef typename TSubsample::MeasurementType       MeasurementType;
  typedef typename TSubsample::MeasurementVectorType MeasurementVectorType;

  typedef typename TSubsample::MeasurementVectorSizeType MeasurementVectorSizeType;
  const MeasurementVectorSizeType Dimension = sample->GetMeasurementVectorSize();
  if ( Dimension == 0 )
    {
    itkGenericExceptionMacro(
      << "Length of a sample's measurement vector hasn't been set.");
    }

  Array< double > sum(Dimension);

  MeasurementVectorSizeType dimension;
  MeasurementVectorType     temp;
  NumericTraits<MeasurementVectorType>::SetLength(temp, Dimension);
  NumericTraits<MeasurementVectorType>::SetLength(mean, Dimension);

  min = max = temp = sample->GetMeasurementVectorByIndex(beginIndex);
  double frequencySum = sample->GetFrequencyByIndex(beginIndex);
  sum.Fill(0.0);

  while ( true )
    {
    for ( dimension = 0; dimension < Dimension; dimension++ )
      {
      if ( temp[dimension] < min[dimension] )
        {
        min[dimension] = temp[dimension];
        }
      else if ( temp[dimension] > max[dimension] )
        {
        max[dimension] = temp[dimension];
        }
      sum[dimension] += temp[dimension];
      }
    ++beginIndex;
    if ( beginIndex == endIndex )
      {
      break;
      }
    temp = sample->GetMeasurementVectorByIndex(beginIndex);
    frequencySum += sample->GetFrequencyByIndex(beginIndex);
    }   // end of while

  for ( unsigned int i = 0; i < Dimension; i++ )
    {
    mean[i] = (MeasurementType)( sum[i] / frequencySum );
    }
}

/** The endIndex should point one point after the last elements.  Note that kth
 * is an index in a different scale than [beginIndex,endIndex].  For example,
 * it is possible to feed this function with beginIndex=15, endIndex=23, and
 * kth=3, since we can ask for the element 3rd in the range [15,23]. */

template< typename TSubsample >
inline typename TSubsample::MeasurementType
QuickSelect(TSubsample *sample,
            unsigned int activeDimension,
            int beginIndex,
            int endIndex,
            int kth,
            typename TSubsample::MeasurementType medianGuess)
{
  typedef typename TSubsample::MeasurementType SampleMeasurementType;

  int begin = beginIndex;
  int end = endIndex - 1;
  int kthIndex = kth + beginIndex;

  SampleMeasurementType tempMedian;

  //
  // Select a pivot value
  //
  if ( medianGuess != NumericTraits< SampleMeasurementType >::NonpositiveMin() )
    {
    tempMedian = medianGuess;
    }
  else
    {
    const int             length = end - begin;
    const int             middle = begin + length / 2;
    const SampleMeasurementType v1 = sample->GetMeasurementVectorByIndex(begin)[activeDimension];
    const SampleMeasurementType v2 = sample->GetMeasurementVectorByIndex(end)[activeDimension];
    const SampleMeasurementType v3 = sample->GetMeasurementVectorByIndex(middle)[activeDimension];
    tempMedian = MedianOfThree< SampleMeasurementType >(v1, v2, v3);
    }

  while ( true )
    {
    // Partition expects the end argument to be one past-the-end of the array.
    // The index pivotNewIndex returned by Partition is in the range
    // [begin,end].
    int pivotNewIndex =
      Partition< TSubsample >(sample, activeDimension, begin, end + 1, tempMedian);

    if ( kthIndex == pivotNewIndex )
      {
      break;
      }

    if ( kthIndex < pivotNewIndex )
      {
      end = pivotNewIndex - 1;
      }
    else
      {
      begin = pivotNewIndex + 1;
      }

    if ( begin > end )
      {
      break;
      }

    const int             length = end - begin;
    const SampleMeasurementType v1 = sample->GetMeasurementVectorByIndex(begin)[activeDimension];
    const SampleMeasurementType v2 = sample->GetMeasurementVectorByIndex(end)[activeDimension];

    // current partition has only 1 or 2 elements
    if ( length < 2 )
      {
      if ( v2 < v1 )
        {
        sample->Swap(begin, end);
        }
      break;
      }

    const int             middle = begin + length / 2;
    const SampleMeasurementType v3 = sample->GetMeasurementVectorByIndex(middle)[activeDimension];
    tempMedian = MedianOfThree< SampleMeasurementType >(v1, v2, v3);
    }

  return sample->GetMeasurementVectorByIndex(kthIndex)[activeDimension];
}

template< typename TSubsample >
inline typename TSubsample::MeasurementType
QuickSelect(TSubsample *sample,
            unsigned int activeDimension,
            int beginIndex,
            int endIndex,
            int kth)
{
  typedef typename TSubsample::MeasurementType SampleMeasurementType;
  SampleMeasurementType medianGuess = NumericTraits< SampleMeasurementType >::NonpositiveMin();
  return QuickSelect< TSubsample >(sample, activeDimension,
                                   beginIndex, endIndex, kth, medianGuess);
}


template< typename TSubsample >
inline int UnguardedPartition(TSubsample *sample,
                              unsigned int activeDimension,
                              int beginIndex,
                              int endIndex,
                              typename TSubsample::MeasurementType pivotValue)
{
  typedef typename TSubsample::MeasurementType MeasurementType;
  while ( true )
    {
    MeasurementType beginValue =
      sample->GetMeasurementVectorByIndex(beginIndex)[activeDimension];

    while ( beginValue < pivotValue )
      {
      ++beginIndex;

      beginValue = sample->GetMeasurementVectorByIndex(beginIndex)[activeDimension];
      }

    --endIndex;

    MeasurementType endValue =
      sample->GetMeasurementVectorByIndex(endIndex)[activeDimension];

    while ( pivotValue < endValue )
      {
      --endIndex;
      endValue = sample->GetMeasurementVectorByIndex(endIndex)[activeDimension];
      }

    if ( !( beginIndex < endIndex ) )
      {
      return beginIndex;
      }

    sample->Swap(beginIndex, endIndex);

    ++beginIndex;
    }
}
template< typename TSubsample >
inline typename TSubsample::MeasurementType
NthElement(TSubsample *sample,
           unsigned int activeDimension,
           int beginIndex,
           int endIndex,
           int nth)
{
  typedef typename TSubsample::MeasurementType MeasurementType;

  const int nthIndex = beginIndex + nth;

  int beginElement = beginIndex;
  int endElement   = endIndex;

  while ( endElement - beginElement > 3 )
    {
    const int begin = beginElement;
    const int end   = endElement - 1;
    const int length = endElement - beginElement;
    const int middle = beginElement + length / 2;

    const MeasurementType v1 = sample->GetMeasurementVectorByIndex(begin)[activeDimension];
    const MeasurementType v2 = sample->GetMeasurementVectorByIndex(end)[activeDimension];
    const MeasurementType v3 = sample->GetMeasurementVectorByIndex(middle)[activeDimension];

    const MeasurementType tempMedian = MedianOfThree< MeasurementType >(v1, v2, v3);

    int cut = UnguardedPartition(sample, activeDimension, beginElement,  endElement, tempMedian);

    if ( cut <= nthIndex )
      {
      beginElement = cut;
      }
    else
      {
      endElement = cut;
      }
    }

  InsertSort(sample, activeDimension, beginElement,  endElement);

  return sample->GetMeasurementVectorByIndex(nthIndex)[activeDimension];
}

template< typename TSubsample >
inline void InsertSort(TSubsample *sample,
                       unsigned int activeDimension,
                       int beginIndex,
                       int endIndex)
{
  int backwardSearchBegin;
  int backwardIndex;

  for ( backwardSearchBegin = beginIndex + 1;
        backwardSearchBegin < endIndex;
        backwardSearchBegin++ )
    {
    backwardIndex = backwardSearchBegin;
    while ( backwardIndex > beginIndex )
      {
      typedef typename TSubsample::MeasurementType SampleMeasurementType;
      const SampleMeasurementType value1 = sample->GetMeasurementVectorByIndex(backwardIndex)[activeDimension];
      const SampleMeasurementType value2 = sample->GetMeasurementVectorByIndex(backwardIndex - 1)[activeDimension];

      if ( value1 < value2 )
        {
        sample->Swap(backwardIndex, backwardIndex - 1);
        }
      else
        {
        break;
        }
      --backwardIndex;
      }
    }
}

template< typename TSubsample >
inline void DownHeap(TSubsample *sample,
                     unsigned int activeDimension,
                     int beginIndex, int endIndex, int node)
{
  int currentNode = node;
  int leftChild;
  int rightChild;
  int largerChild;

  typedef typename TSubsample::MeasurementType SampleMeasurementType;
  SampleMeasurementType currentNodeValue =
    sample->GetMeasurementVectorByIndex(currentNode)[activeDimension];
  SampleMeasurementType leftChildValue;
  SampleMeasurementType rightChildValue;
  SampleMeasurementType largerChildValue;

  while ( true )
    {
    // location of first child
    largerChild = leftChild =
                    beginIndex + 2 * ( currentNode - beginIndex ) + 1;
    rightChild = leftChild + 1;
    if ( leftChild > endIndex - 1 )
      {
      // leaf node
      return;
      }

    largerChildValue = rightChildValue = leftChildValue =
                                           sample->GetMeasurementVectorByIndex(leftChild)[activeDimension];

    if ( rightChild < endIndex )
      {
      rightChildValue =
        sample->GetMeasurementVectorByIndex(rightChild)[activeDimension];
      }

    if ( leftChildValue < rightChildValue )
      {
      largerChild = rightChild;
      largerChildValue = rightChildValue;
      }

    if ( largerChildValue <= currentNodeValue )
      {
      // the node satisfies heap property
      return;
      }
    // move down current node value to the larger child
    sample->Swap(currentNode, largerChild);
    currentNode = largerChild;
    }
}

template< typename TSubsample >
inline void HeapSort(TSubsample *sample,
                     unsigned int activeDimension,
                     int beginIndex,
                     int endIndex)
{
  // construct a heap
  int node;

  for ( node = beginIndex + ( endIndex - beginIndex ) / 2 - 1;
        node >= beginIndex; node-- )
    {
    DownHeap< TSubsample >(sample, activeDimension,
                           beginIndex, endIndex, node);
    }

  // sort
  int newEndIndex;
  for ( newEndIndex = endIndex - 1; newEndIndex >= beginIndex;
        --newEndIndex )
    {
    sample->Swap(beginIndex, newEndIndex);
    DownHeap< TSubsample >(sample, activeDimension,
                           beginIndex, newEndIndex, beginIndex);
    }
}

template< typename TSubsample >
inline void IntrospectiveSortLoop(TSubsample *sample,
                                  unsigned int activeDimension,
                                  int beginIndex,
                                  int endIndex,
                                  int depthLimit,
                                  int sizeThreshold)
{
  typedef typename TSubsample::MeasurementType SampleMeasurementType;

  int length = endIndex - beginIndex;
  int cut;
  while ( length > sizeThreshold )
    {
    if ( depthLimit == 0 )
      {
      HeapSort< TSubsample >(sample, activeDimension,
                             beginIndex, endIndex);
      return;
      }

    --depthLimit;
    cut = Partition< TSubsample >( sample, activeDimension,
                                   beginIndex, endIndex,
                                   MedianOfThree< SampleMeasurementType >
                                     (sample->GetMeasurementVectorByIndex(beginIndex)[activeDimension],
                                     sample->GetMeasurementVectorByIndex(beginIndex + length / 2)[activeDimension],
                                     sample->GetMeasurementVectorByIndex(endIndex - 1)[activeDimension]) );
    IntrospectiveSortLoop< TSubsample >(sample, activeDimension,
                                        cut, endIndex,
                                        depthLimit, sizeThreshold);
    endIndex = cut;
    length = endIndex - beginIndex;
    }
}

template< typename TSubsample >
inline void IntrospectiveSort(TSubsample *sample,
                              unsigned int activeDimension,
                              int beginIndex,
                              int endIndex,
                              int sizeThreshold)
{
  IntrospectiveSortLoop< TSubsample >(sample, activeDimension, beginIndex, endIndex,
                                      2 * FloorLog(endIndex - beginIndex), sizeThreshold);
  InsertSort< TSubsample >(sample, activeDimension, beginIndex, endIndex);
}
} // end of namespace Algorithm
} // end of namespace Statistics
} // end of namespace itk

#endif // #ifndef __itkStatisticsAlgorithm_hxx