This file is indexed.

/usr/include/vigra/accumulator-grammar.hxx is in libvigraimpex-dev 1.10.0+dfsg-11ubuntu2.

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
747
748
749
/************************************************************************/
/*                                                                      */
/*               Copyright 2011-2012 by Ullrich Koethe                  */
/*                                                                      */
/*    This file is part of the VIGRA computer vision library.           */
/*    The VIGRA Website is                                              */
/*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
/*    Please direct questions, bug reports, and contributions to        */
/*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
/*        vigra@informatik.uni-hamburg.de                               */
/*                                                                      */
/*    Permission is hereby granted, free of charge, to any person       */
/*    obtaining a copy of this software and associated documentation    */
/*    files (the "Software"), to deal in the Software without           */
/*    restriction, including without limitation the rights to use,      */
/*    copy, modify, merge, publish, distribute, sublicense, and/or      */
/*    sell copies of the Software, and to permit persons to whom the    */
/*    Software is furnished to do so, subject to the following          */
/*    conditions:                                                       */
/*                                                                      */
/*    The above copyright notice and this permission notice shall be    */
/*    included in all copies or substantial portions of the             */
/*    Software.                                                         */
/*                                                                      */
/*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
/*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
/*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
/*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
/*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
/*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
/*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
/*    OTHER DEALINGS IN THE SOFTWARE.                                   */
/*                                                                      */
/************************************************************************/

#ifndef VIGRA_ACCUMULATOR_GRAMMAR_HXX
#define VIGRA_ACCUMULATOR_GRAMMAR_HXX

#ifdef _MSC_VER
#pragma warning (disable: 4503)
#endif

#include "config.hxx"
#include "metaprogramming.hxx"

namespace vigra {

namespace acc {

/**************************************************************************/
/*                                                                        */
/*                      irreducible basic accumulators                    */
/*                                                                        */
/**************************************************************************/
    
   
class CoordinateSystem;                        // returns an identity matrix of appropriate size

template <unsigned N> class PowerSum;          // sum over powers of values
template <unsigned N> class AbsPowerSum;       // sum over powers of absolute values

class Skewness;                                // skewness
class UnbiasedSkewness;                        // unbiased estimator for skewness
class Kurtosis;                                // excess kurtosis
class UnbiasedKurtosis;                        // unbiased estimator for excess kurtosis
class FlatScatterMatrix;                       // flattened upper-triangular part of the scatter matrix
class ScatterMatrixEigensystem;                // eigenvalues and eigenvectors of the scatter matrix

    // in all histogram classes: set bin count at runtime if BinCount == 0
template <int BinCount> class IntegerHistogram;      // use data values directly as bin indices
template <int BinCount> class UserRangeHistogram;    // set min/max explicitly at runtime
template <int BinCount> class AutoRangeHistogram;    // get min/max from accumulators
template <int BinCount> class GlobalRangeHistogram;  // like AutoRangeHistogram, but use global min/max rather than region min/max

class Minimum;                                 // minimum
class Maximum;                                 // maximum
template <class Hist> class StandardQuantiles; // compute (min, 10%, 25%, 50%, 75%, 90%, max) quantiles from 
                                               // min/max accumulators and given histogram

class ArgMinWeight;                            // store the value (or coordinate) where weight was minimal
class ArgMaxWeight;                            // store the value (or coordinate) where weight was maximal

    // FIXME: not yet implemented
template <unsigned NDim> class MultiHistogram; // multi-dimensional histogram
                                               // (always specify number of bins at runtime)

class Centralize;                              // cache centralized values
class PrincipalProjection;                     // cache values after principal projection
    // FIXME: not yet implemented
class Whiten;                                  // cache values after whitening
class RangeMapping;                            // map value from [min, max] to another range and cache result (e.g. for histogram creation)

template <int INDEX>  class DataArg;           // specifiy the index of the data member in a CoupledHandle
template <int INDEX>  class WeightArg;         // specifiy the index of the weight member in a CoupledHandle
template <int INDEX>  class LabelArg;          // specifiy the index of the label member in a CoupledHandle
template <int INDEX>  class CoordArg;          // specifiy the index of the coord member in a CoupledHandle

/* 
Quantiles other than minimum and maximum require more thought:
--------------------------------------------------------------
 * Exact quantiles can be found in time O(n) using recursive partitioning (quickselect),
   but this requires the data (or an auxiliary index array) to be re-arranged.
 * Exact quantiles can be found in time O(k*n) using recursive histogram refinement,
   were k = O(log(n)/log(BinCount)) is the expected number of required passes over the 
   data, provided that bins are filled approximately evenly. If data can be re-arranged,
   such that we remember the items corresponding to each bin, running time reduces to O(n) 
   (this is Tibshirani's 'binmedian' algorithm). For the median, Tibshirani proves that
   the initial histogram only needs to cover the interval [Mean-StdDev, Mean+StdDev].
 * Both strategies can be combined: perform k passes to reduce bin size to about 
   n/(BinCount)^k, and then perform quickselect on an auxiliary array of size 
   O(n/(BinCount)^k) which has been filled during the final pass.
 * Good approximate results can be obtained by early stopping of histogram refinement
   (Tibshirani's 'binapprox' algorithm). A 2-pass algorithm for the median achieves
   accuracy of StdDev/BinCount: Mean and StdDev are computed during pass 1, 
   and a histogram over [Mean-StdDev, Mean+StdDev] during pass 2.
 * A 1-pass approximation method is described in Chen et al. However, it assumes that
   samples arrive in random order which is usually not true in image data.
*/

/**************************************************************************/
/*                                                                        */
/*                  modifiers for composite accumulators                  */
/*                                                                        */
/**************************************************************************/

   // data normalization w.r.t. number of samples
template <class A>    class DivideByCount;       //  A / count
template <class A>    class RootDivideByCount;   //  sqrt(A / count)
template <class A>    class DivideUnbiased;      //  A / (count - 1)  
template <class A>    class RootDivideUnbiased;  //  sqrt(A / (count - 1))

    // data access
template <class A> class Coord;           // use pixel coordinate instead of pixel value (index 0 of CoupledHandle)
template <class A> class Weighted;        // use (value, weight) pairs (index 1 and 2 of CoupledHandle)
template <class A> class CoordWeighted;   // use (coord, weight) pairs(index 0 and end of CoupledHandle)
template <class A> class DataFromHandle;  // extract data from index 1 of a CoupledHandle

    // data preparation
template <class A> class Central;    // subtract mean
template <class A> class Principal;  // subtract mean and rotate to principal coordinates

    // FIXME: not implemented yet
template <class A> class Whitened;   // transform to principal coordinates and scale to unit variance

template <class A> class Global;     // compute statistic A globally rather than per region

/**************************************************************************/
/*                                                                        */
/*                   alias names for important features                   */
/*                                                                        */
/**************************************************************************/

/** \brief Alias. Count. */
typedef PowerSum<0>                                 Count;
/** \brief Alias. Sum. */
typedef PowerSum<1>                                 Sum;
/** \brief Alias. Sum of squares. */
typedef PowerSum<2>                                 SumOfSquares;

/** \brief Alias. Mean. */
typedef DivideByCount<Sum>                          Mean;
/** \brief Alias. Root mean square. */
typedef RootDivideByCount<SumOfSquares>             RootMeanSquares;

// desired pseudocode (unfortunately not legal in C++)
//
//     template <unsigned N> 
//     typedef DivideByCount<PowerSum<N> >          Moment;
//
// actual definition (desired behavior is realised by rules below)
//
/** \brief Alias. Moment<N>. */
template <unsigned N> class                         Moment;  
/** \brief Alias. CentralMoment<N>. */
template <unsigned N> class                         CentralMoment;  

/** \brief Alias. Sum of squared differences. */
typedef Central<PowerSum<2> >                       SumOfSquaredDifferences;
/** \brief Alias. Sum of squared differences. */
typedef SumOfSquaredDifferences                     SSD;

/** \brief Alias. Variance. */
typedef DivideByCount<Central<PowerSum<2> > >       Variance;
/** \brief Alias. Standard deviation. */
typedef RootDivideByCount<Central<PowerSum<2> > >   StdDev;
/** \brief Alias. Unbiased variance. */
typedef DivideUnbiased<Central<PowerSum<2> > >      UnbiasedVariance;
/** \brief Alias. Unbiased standard deviation. */
typedef RootDivideUnbiased<Central<PowerSum<2> > >  UnbiasedStdDev;

/** \brief Alias. Covariance. */
typedef DivideByCount<FlatScatterMatrix>            Covariance;
/** \brief Alias. Unbiased covariance. */
typedef DivideUnbiased<FlatScatterMatrix>           UnbiasedCovariance;
/** \brief Alias. Covariance eigensystem. */
typedef DivideByCount<ScatterMatrixEigensystem>     CovarianceEigensystem;

/** \brief Alias. Absolute sum. */
typedef AbsPowerSum<1>                              AbsSum;
/** \brief Alias. Sum of absolute differences. */
typedef Central<AbsSum>                             SumOfAbsDifferences;
/** \brief Alias. Mean absolute deviation. */
typedef DivideByCount<SumOfAbsDifferences>          MeanAbsoluteDeviation;

/** \brief Alias. Region center. */
typedef Coord<Mean>                                 RegionCenter;
/** \brief Alias. Region radii. */
typedef Coord<Principal<StdDev> >                   RegionRadii;
/** \brief Alias. Region axes. */
typedef Coord<Principal<CoordinateSystem> >         RegionAxes;

/** \brief Alias. Center of mass. */
typedef Weighted<RegionCenter>                      CenterOfMass;
/** \brief Alias. Moments of inertia. */
typedef Weighted<Coord<Principal<Variance> > >      MomentsOfInertia;
/** \brief Alias. Axes of inertia. */
typedef Weighted<RegionAxes>                        AxesOfInertia;

/**************************************************************************/
/*                                                                        */
/*                        Tag standardization rules                       */
/*                                                                        */
/**************************************************************************/

namespace acc_detail {

template <class A>
struct ModifierRule
{
    typedef A type;
};

} // namespace acc_detail

template <class A>
struct Error___Tag_modifiers_of_same_kind_must_not_be_combined;

    // apply rules as long as the Tag type changes ...
template <class A, class S=typename acc_detail::ModifierRule<A>::type>
struct StandardizeTag
{
    typedef typename StandardizeTag<S>::type type;
};

    // ... and stop otherwise ...
template <class A>
struct StandardizeTag<A, A>
{
    typedef A type;
};

    // ... or fail when the tag spec was non-conforming 
template <class A, class B>
struct StandardizeTag<A, Error___Tag_modifiers_of_same_kind_must_not_be_combined<B> >
    : public Error___Tag_modifiers_of_same_kind_must_not_be_combined<B>
{};

namespace acc_detail {

    // Assign priorities to modifiers to determine their standard order (by ascending priority).
    // SubstitutionMask determines which modifiers must be automatically transferred to dependencies.
enum { MinPriority = 1, 
       AccumulatorPriority = 32,
       PrepareDataPriority = 16,
       NormalizePriority = 8,
       AccessDataPriority = 4,
       WeightingPriority = 2,
       GlobalPriority = 1,
       MaxPriority = 32,
       SubstitutionMask = PrepareDataPriority | AccessDataPriority | WeightingPriority | GlobalPriority };

template <class A>
struct ModifierPriority
{
    static const int value = AccumulatorPriority;
};

#define VIGRA_MODIFIER_PRIORITY(MODIFIER, VALUE) \
template <class A> \
struct ModifierPriority<MODIFIER<A> > \
{ \
    static const int value = VALUE; \
};

VIGRA_MODIFIER_PRIORITY(Global, GlobalPriority)

VIGRA_MODIFIER_PRIORITY(Weighted, WeightingPriority)

VIGRA_MODIFIER_PRIORITY(Coord, AccessDataPriority)
VIGRA_MODIFIER_PRIORITY(DataFromHandle, AccessDataPriority)

VIGRA_MODIFIER_PRIORITY(DivideByCount, NormalizePriority)
VIGRA_MODIFIER_PRIORITY(RootDivideByCount, NormalizePriority)
VIGRA_MODIFIER_PRIORITY(DivideUnbiased, NormalizePriority)
VIGRA_MODIFIER_PRIORITY(RootDivideUnbiased, NormalizePriority)

VIGRA_MODIFIER_PRIORITY(Central, PrepareDataPriority)
VIGRA_MODIFIER_PRIORITY(Principal, PrepareDataPriority)
VIGRA_MODIFIER_PRIORITY(Whitened, PrepareDataPriority)

    // explicitly set priority for base accumulators that look like modifiers
VIGRA_MODIFIER_PRIORITY(StandardQuantiles, AccumulatorPriority)

#undef VIGRA_MODIFIER_PRIORITY

    // check if the tag A contains a modifier with TARGET_PRIORITY
template <class A, int TARGET_PRIORITY, int PRIORITY=ModifierPriority<A>::value>
struct HasModifierPriority
{
    typedef VigraFalseType type;
    static const bool value = false;
};

template <class A, int TARGET_PRIORITY>
struct HasModifierPriority<A, TARGET_PRIORITY, TARGET_PRIORITY>
{
    typedef VigraTrueType type;
    static const bool value = true;
};

template <class A, template <class> class B, int TARGET_PRIORITY, int PRIORITY>
struct HasModifierPriority<B<A>, TARGET_PRIORITY, PRIORITY>
: public HasModifierPriority<A, TARGET_PRIORITY>
{};

template <class A, template <class> class B, int TARGET_PRIORITY>
struct HasModifierPriority<B<A>, TARGET_PRIORITY, TARGET_PRIORITY>
{
    typedef VigraTrueType type;
    static const bool value = true;
};

    // three-way compare 
template <class A, class B>
struct ModifierCompare
{
    static const int p1 = ModifierPriority<A>::value;
    static const int p2 = ModifierPriority<B>::value;
    static const int value = p1 < p2 
                                ? -1
                                : p2 < p1
                                     ? 1
                                     : 0;
};

template <class A>
struct ModifierCompareToInner;

template <class A, template <class> class B>
struct ModifierCompareToInner<B<A> >
: public ModifierCompare<B<A>, A>
{};

    // sort modifiers by ascending priority
template <class A, int compare=ModifierCompareToInner<A>::value>
struct ModifierOrder;

    // do nothing if the order is correct (compare == -1)
template <class A>
struct ModifierOrder<A, -1>
{
    typedef A type;
};

    // fail if there are two modifiers with the same priority (compare == 0)
template <class A, template <class> class B, template <class> class C>
struct ModifierOrder<C<B<A> >, 0>
{
    typedef Error___Tag_modifiers_of_same_kind_must_not_be_combined<C<B<A> > > type;
};

    // sort if the order is reversed (compare == 1)
template <class A, template <class> class B, template <class> class C>
struct ModifierOrder<C<B<A> >, 1>
{
    typedef B<C<A> > type;
};

#define VIGRA_CLEANUP_DATA_PREPARATION_MODIFIERS(OUTER, INNER, RESULT) \
template <class A> \
struct ModifierOrder<OUTER<INNER<A > >, 0> \
{ \
    typedef RESULT<A > type; \
};

    // drop duplicates
VIGRA_CLEANUP_DATA_PREPARATION_MODIFIERS(Central, Central, Central)
VIGRA_CLEANUP_DATA_PREPARATION_MODIFIERS(Principal, Principal, Principal)
VIGRA_CLEANUP_DATA_PREPARATION_MODIFIERS(Whitened, Whitened, Whitened)

    // the strongest data preparation modifier takes precendence
VIGRA_CLEANUP_DATA_PREPARATION_MODIFIERS(Principal, Central, Principal)
VIGRA_CLEANUP_DATA_PREPARATION_MODIFIERS(Whitened, Central, Whitened)
VIGRA_CLEANUP_DATA_PREPARATION_MODIFIERS(Whitened, Principal, Whitened)

    // Coord takes precendence over DataFromHandle
VIGRA_CLEANUP_DATA_PREPARATION_MODIFIERS(DataFromHandle, Coord, Coord)

#undef VIGRA_CLEANUP_DATA_PREPARATION_MODIFIERS

    // drop duplicates
template <class A, template <class> class B>
struct ModifierRule<B<B<A> > >
{
    typedef B<A> type;
};

template <class A, int PRIORITY=ModifierPriority<A>::value>
struct RecurseModifier;

template <class A, template <class> class B, int PRIORITY>
struct RecurseModifier<B<A>, PRIORITY>
{
    typedef typename ModifierOrder<B<typename StandardizeTag<A>::type> >::type type;
};

template <class A, template <class> class B>
struct RecurseModifier<B<A>, AccumulatorPriority>
{
    typedef B<A> type;
};

    // recurse down the modifier chain, but only of B is actually a modifier,
    // and not a templated base accumulator (i.e. do not recurse if B's
    // priority is 'AccumulatorPriority')
template <class A, template <class> class B>
struct ModifierRule<B<A> >
: public RecurseModifier<B<A> >
{};

    // reduce the SOURCE modifier to the TARGET modifier,
    // using the given TEMPLATE arguments
    // (this is a work-around for the lack of templated typedef in C++)
#define VIGRA_REDUCE_MODFIER(TEMPLATE, SOURCE, TARGET) \
template <TEMPLATE > \
struct ModifierRule<SOURCE > \
{ \
    typedef TARGET type; \
};

#define VIGRA_VOID

    // centralizing doesn't change the CoordinateSystem
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Central<CoordinateSystem>, CoordinateSystem)
    // whitened CoordinateSystem are the same as principal CoordinateSystem
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Whitened<CoordinateSystem>, Principal<CoordinateSystem>)

    // counting modified data is the same as counting data, except for weighted data and global counting
VIGRA_REDUCE_MODFIER(template <class> class A, A<Count>, Count)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Weighted<Count>, Weighted<Count>)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, CoordWeighted<Count>, Weighted<Count>)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Global<Count>, Global<Count>)

    // reduce aliases that typedef can't handle
VIGRA_REDUCE_MODFIER(unsigned N, Moment<N>, DivideByCount<PowerSum<N> >)
VIGRA_REDUCE_MODFIER(unsigned N, CentralMoment<N>, DivideByCount<Central<PowerSum<N> > >)
VIGRA_REDUCE_MODFIER(class A, CoordWeighted<A>, Weighted<Coord<A> >)

    // reduce statistics that are inherently centered
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Central<Centralize>, Centralize)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Central<Skewness>, Skewness)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Central<Kurtosis>, Kurtosis)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Central<FlatScatterMatrix>, FlatScatterMatrix)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Central<ScatterMatrixEigensystem>, ScatterMatrixEigensystem)

VIGRA_REDUCE_MODFIER(VIGRA_VOID, Principal<Centralize>, PrincipalProjection)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Whitened<Centralize>, Whiten)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Principal<PrincipalProjection>, PrincipalProjection)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Whitened<PrincipalProjection>, Whiten)
VIGRA_REDUCE_MODFIER(VIGRA_VOID, Whitened<Whiten>, Whiten)

    // reduce even absolute powers to plain powers
template <unsigned N>
struct ModifierRule<AbsPowerSum<N> >
{
    typedef typename IfBool<(N % 2 == 0), PowerSum<N>, AbsPowerSum<N> >::type type;
};

#undef VIGRA_VOID
#undef VIGRA_REDUCE_MODFIER

template <class A>
struct ShouldBeWeighted
{
    typedef VigraFalseType type;
    static const bool value = false;
};

template <>
struct ShouldBeWeighted<ArgMinWeight>
{
    typedef VigraTrueType type;
    static const bool value = true;
};

template <>
struct ShouldBeWeighted<ArgMaxWeight>
{
    typedef VigraTrueType type;
    static const bool value = true;
};

template <class A, template <class> class B>
struct ShouldBeWeighted<B<A> >
: public ShouldBeWeighted<A>
{};

} // namespace acc_detail

template <class A>
struct IsCoordinateFeature
{
    typedef VigraFalseType type;
    static const bool value = false;
};

template <class A, template <class> class B>
struct IsCoordinateFeature<B<A> >
{
    typedef typename IsCoordinateFeature<A>::type type;
    static const bool value = IsCoordinateFeature<A>::value;
};

template <class A>
struct IsCoordinateFeature<Coord<A> >
{
    typedef VigraTrueType type;
    static const bool value = true;
};

template <class A>
struct IsPrincipalFeature
{
    typedef VigraFalseType type;
    static const bool value = false;
};

template <class A, template <class> class B>
struct IsPrincipalFeature<B<A> >
{
    typedef typename IsPrincipalFeature<A>::type type;
    static const bool value = IsPrincipalFeature<A>::value;
};

template <class A>
struct IsPrincipalFeature<Principal<A> >
{
    typedef VigraTrueType type;
    static const bool value = true;
};

template <class A>
struct IsPrincipalFeature<Whitened<A> >
{
    typedef VigraTrueType type;
    static const bool value = true;
};

/**************************************************************************/
/*                                                                        */
/*                           Tag transfer rules                           */
/*                                                                        */
/**************************************************************************/

namespace acc_detail {

template <class A>
struct DefaultModifier;

template <class A>
struct ModifierPriority<DefaultModifier<A> >
{
    static const int value = ModifierPriority<A>::value << 1;
};

template <class A, int TargetPriority, int Priority=ModifierPriority<A>::value>
struct InsertDefaultModifier
{
    typedef DefaultModifier<typename InsertDefaultModifier<A, (TargetPriority << 1)>::type> type;
};

template <class A, int TargetPriority>
struct InsertDefaultModifier<A, TargetPriority, TargetPriority>
{
    typedef A type;
};

template <class A, int TargetPriority, int Priority=ModifierPriority<A>::value>
struct TagLongForm;

template <class A, int TargetPriority>
struct TagLongForm<A, TargetPriority, MaxPriority>
{
    typedef typename InsertDefaultModifier<A, TargetPriority>::type type;
};

template <class A, template <class> class B, int TargetPriority>
struct TagLongForm<B<A>, TargetPriority, MaxPriority>
{
    typedef typename InsertDefaultModifier<B<A>, TargetPriority>::type type;
};

template <class A, template <class> class B, int TargetPriority, int Priority>
struct TagLongForm<B<A>, TargetPriority, Priority>
{
    typedef typename TagLongForm<A, (Priority << 1)>::type Inner;
    typedef typename InsertDefaultModifier<B<Inner>, TargetPriority>::type type;
};

template <class A, template <class> class B, int TargetPriority>
struct TagLongForm<B<A>, TargetPriority, TargetPriority>
{
    typedef typename TagLongForm<A, (TargetPriority << 1)>::type Inner;
    typedef B<Inner> type;
};

template <class A>
struct LongModifierRule
{
    typedef A type;
};

    // apply rules as long as the Tag type changes ...
template <class A, class S=typename LongModifierRule<A>::type>
struct StandardizeTagLongForm
{
    typedef typename StandardizeTagLongForm<S>::type type;
};

    // ... and stop otherwise ...
template <class A>
struct StandardizeTagLongForm<A, A>
{
    typedef A type;
};

template <class A, template <class> class B>
struct LongModifierRule<B<A> >
{
    typedef B<typename LongModifierRule<A>::type> type;
};

template <class A>
struct LongModifierRule<DefaultModifier<A> >
{
    typedef A type;
};

#define VIGRA_DROP_DATA_PREPARATION_MODIFIERS(SOURCE, TARGET) \
template <> \
struct LongModifierRule<SOURCE > \
{ \
    typedef TARGET type; \
};

VIGRA_DROP_DATA_PREPARATION_MODIFIERS(Central<Sum>, Sum)
VIGRA_DROP_DATA_PREPARATION_MODIFIERS(Principal<Sum>, Sum)
VIGRA_DROP_DATA_PREPARATION_MODIFIERS(Whitened<Sum>, Sum)
VIGRA_DROP_DATA_PREPARATION_MODIFIERS(Principal<FlatScatterMatrix>, FlatScatterMatrix)
VIGRA_DROP_DATA_PREPARATION_MODIFIERS(Whitened<FlatScatterMatrix>, FlatScatterMatrix)
VIGRA_DROP_DATA_PREPARATION_MODIFIERS(Principal<ScatterMatrixEigensystem>, ScatterMatrixEigensystem)
VIGRA_DROP_DATA_PREPARATION_MODIFIERS(Whitened<ScatterMatrixEigensystem>, ScatterMatrixEigensystem)

#undef VIGRA_DROP_DATA_PREPARATION_MODIFIERS

template <class A>
struct CheckSubstitutionFlag
{
    static const bool value = (ModifierPriority<A>::value & SubstitutionMask) != 0;
};

template <class A, class B, 
          bool substitute=CheckSubstitutionFlag<A>::value>
struct SubstituteModifiers;

template <class A, class B>
struct SubstituteModifiers<A, B, false>
{
    typedef B type;
};

template <class A, template <class> class AA, class B, template <class> class BB>
struct SubstituteModifiers<AA<A>, BB<B>, true>
{
    typedef AA<typename SubstituteModifiers<A, B>::type> type;
};

template <class A, class B, template <class> class BB>
struct SubstituteModifiers<DefaultModifier<A>, BB<B>, true>
{
    typedef BB<typename SubstituteModifiers<A, B>::type> type;
};

template <class A, template <class> class AA, class B, template <class> class BB>
struct SubstituteModifiers<AA<A>, BB<B>, false>
{
    typedef BB<typename SubstituteModifiers<A, B>::type> type;
};

} // namespace acc_detail

template <class A, class B>
struct TransferModifiers
{
    typedef typename StandardizeTag<A>::type StdA;
    typedef typename StandardizeTag<B>::type StdB;
    typedef typename acc_detail::TagLongForm<StdA, acc_detail::MinPriority>::type AA;
    typedef typename acc_detail::TagLongForm<StdB, acc_detail::MinPriority>::type BB;
    typedef typename acc_detail::SubstituteModifiers<AA, BB>::type AB;
    typedef typename acc_detail::StandardizeTagLongForm<AB>::type StdAB;
    typedef typename StandardizeTag<StdAB>::type type;
};

template <class A, class HEAD, class TAIL>
struct TransferModifiers<A, TypeList<HEAD, TAIL> >
{
    typedef TypeList<typename TransferModifiers<A, HEAD>::type,
                     typename TransferModifiers<A, TAIL>::type> type;
};

template <class A>
struct TransferModifiers<A, void>
{
    typedef void type;
};

template <class TargetTag, class A=typename TargetTag::Dependencies>
struct StandardizeDependencies
#ifndef DOXYGEN
: public StandardizeDependencies<TargetTag, typename A::type>
#endif
{};

template <class TargetTag, class HEAD, class TAIL>
struct StandardizeDependencies<TargetTag, TypeList<HEAD, TAIL> >
{
    typedef typename StandardizeTag<TargetTag>::type Target;
    typedef typename TransferModifiers<Target, TypeList<HEAD, TAIL> >::type type;
};

template <class TargetTag>
struct StandardizeDependencies<TargetTag, void>
{
    typedef void type;
};

}} // namespace vigra::acc

#endif // VIGRA_ACCUMULATOR_GRAMMAR_HXX