This file is indexed.

/usr/include/vigra/array_vector.hxx is in libvigraimpex-dev 1.10.0+git20160211.167be93+dfsg-2+b5.

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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/************************************************************************/
/*                                                                      */
/*               Copyright 2002-2004 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_ARRAY_VECTOR_HXX
#define VIGRA_ARRAY_VECTOR_HXX

#include "error.hxx"
#include "memory.hxx"
#include "numerictraits.hxx"
#include <memory>
#include <algorithm>
#include <iosfwd>

#ifdef VIGRA_CHECK_BOUNDS
#define VIGRA_ASSERT_INSIDE(diff) \
  vigra_precondition(diff >= 0, "Index out of bounds");\
  vigra_precondition(diff < (difference_type)size_, "Index out of bounds");
#else
#define VIGRA_ASSERT_INSIDE(diff)
#endif

namespace vigra
{

template <class T, class Alloc = std::allocator<T> >
class ArrayVector;

/** Provide STL conforming interface for C-arrays.

    This template implements much of the functionality of <a href="http://www.sgi.com/tech/stl/Vector.html">std::vector</a>
    on top of a C-array. <tt>ArrayVectorView</tt> does not manage the memory
    it refers to (i.e. it does not allocate or deallocate any memory).
    Thus, if the underlying memory changes, all dependent <tt>ArrayVectorView</tt>
    objects are invalidated. This is especially important when <tt>ArrayVectorView</tt>
    is used as a base class for <tt>ArrayVector</tt>, where several functions
    (e.g. resize(), insert()) can allocate new memory and thus invalidate the
    dependent views. The rules what operations invalidate view objects are the
    same as the rules concerning standard iterators.

    <b>\#include</b> \<vigra/array_vector.hxx\><br>
    Namespace: vigra
*/
template <class T>
class ArrayVectorView
{
    typedef ArrayVectorView<T> this_type;

public:
        /** default constructor
        */
    typedef T value_type;
    typedef value_type & reference;
    typedef value_type const & const_reference;
    typedef value_type * pointer;
    typedef value_type const * const_pointer;
    typedef value_type * iterator;
    typedef value_type const * const_iterator;
    typedef std::size_t size_type;
    typedef std::ptrdiff_t difference_type;
    typedef std::reverse_iterator<iterator> reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

public:
        /** default constructor.
            View contains NULL pointer.
        */
    ArrayVectorView()
    : size_(0),
      data_(0)
    {}

        /** Construct for given array \a data of length \a size.
            <tt>data, data+size</tt> must form a valid range.
        */
    ArrayVectorView( size_type size, pointer const & data)
    : size_(size),
      data_(data)
    {}

        /** Copy constructor.
        */
    ArrayVectorView( this_type const & rhs )
    : size_(rhs.size_),
      data_(rhs.data_)
    {}

        /** Copy assignment. There are 3 cases:

            <ul>
            <li> When this <tt>ArrayVectorView</tt> does not point to valid data
                 (e.g. after default construction), it becomes a copy of \a rhs.
            <li> When the shapes of the two arrays match, the array contents
                 (not the pointers) are copied.
            <li> Otherwise, a <tt>PreconditionViolation</tt> exception is thrown.
            </ul>
        */
    ArrayVectorView & operator=( ArrayVectorView const & rhs );

        /** Copy assignment.
            When the shapes of the two arrays match, the array contents
            (not the pointers) are copied. Otherwise, a <tt>PreconditionViolation</tt>
            exception is thrown.
        */
    template <class U>
    this_type & operator=( ArrayVectorView<U> const & rhs )
    {
        copyImpl(rhs);
        return *this;
    }

        /** Overwrite all array elements with the value \a initial.
        */
    template <class U>
    void init(U const & initial)
    {
        std::fill(begin(), end(), initial);
    }

        /** Copy array elements.
            When the shapes of the two arrays match, the array contents
            (not the pointers) are copied. Otherwise, a <tt>PreconditionViolation</tt>
            exception is thrown.
        */
    void copy( this_type const & rhs )
    {
        if(data_ != rhs.data_)
            copyImpl(rhs);
    }

        /** Copy array elements.
            When the shapes of the two arrays match, the array contents
            (not the pointers) are copied. Otherwise, a <tt>PreconditionViolation</tt>
            exception is thrown.
        */
    template <class U>
    void copy( ArrayVectorView<U> const & rhs )
    {
        copyImpl(rhs);
    }

        /** Swap array elements.
            When the shapes of the two arrays match, the array contents
            (not the pointers) are swapped. Otherwise, a <tt>PreconditionViolation</tt>
            exception is thrown.
        */
    void swapData(this_type rhs)
    {
        if(data_ != rhs.data_)
            swapDataImpl(rhs);
    }

        /** Swap array elements.
            When the shapes of the two arrays match, the array contents
            (not the pointers) are swapped. Otherwise, a <tt>PreconditionViolation</tt>
            exception is thrown.
        */
    template <class U>
    void swapData(ArrayVectorView<U> rhs)
    {
        swapDataImpl(rhs);
    }

        /** Construct <tt>ArrayVectorView</tt> referring to a subarray.
            \a begin and \a end must be a valid sub-range of the current array.
            Otherwise, a <tt>PreconditionViolation</tt>
            exception is thrown.
        */
    this_type subarray (size_type begin, size_type end) const
    {
        vigra_precondition(begin <= end && end <= size_,
                "ArrayVectorView::subarray(): Limits out of range.");
        return this_type(end-begin, data_ + begin);
    }

        /** Get contained const pointer to the data.
        */
    inline const_pointer data() const
    {
        return data_;
    }

        /** Get contained pointer to the data.
        */
    inline pointer data()
    {
        return data_;
    }

        /** Get const iterator referring to the first array element.
        */
    inline const_iterator begin() const
    {
        return data();
    }

        /** Get iterator referring to the first array element.
        */
    inline iterator begin()
    {
        return data();
    }

        /** Get const iterator pointing beyond the last array element.
        */
    inline const_iterator end() const
    {
        return data() + size();
    }

        /** Get iterator pointing beyond the last array element.
        */
    inline iterator end()
    {
        return data() + size();
    }

        /** Get const iterator referring to the first array element.
        */
    inline const_iterator cbegin() const
    {
        return data();
    }

        /** Get const iterator pointing beyond the last array element.
        */
    inline const_iterator cend() const
    {
        return data() + size();
    }

        /** Get reverse iterator referring to the last array element.
        */
    inline reverse_iterator rbegin()
    {
        return (reverse_iterator(end()));
    }

        /** Get const reverse iterator referring to the last array element.
        */
    inline const_reverse_iterator rbegin() const
    {
        return (const_reverse_iterator(end()));
    }

        /** Get reverse iterator pointing before the first array element.
        */
    inline reverse_iterator rend()
    {
        return (reverse_iterator(begin()));
    }

        /** Get const reverse iterator pointing before the first array element.
        */
    inline const_reverse_iterator rend() const
    {
        return (const_reverse_iterator(begin()));
    }

        /** Get const reverse iterator referring to the last array element.
        */
    inline const_reverse_iterator crbegin() const
    {
        return (const_reverse_iterator(end()));
    }

        /** Get const reverse iterator pointing before the first array element.
        */
    inline const_reverse_iterator crend() const
    {
        return (const_reverse_iterator(begin()));
    }

        /** Access first array element.
        */
    reference front()
    {
        return *data_;
    }

        /** Read first array element.
        */
    const_reference front() const
    {
        return *data_;
    }

        /** Access last array element.
        */
    reference back()
    {
        return data_[size_-1];
    }

        /** Read last array element.
        */
    const_reference back() const
    {
        return data_[size_-1];
    }

        /** Access array element \a i.
        */
    reference operator[]( difference_type i )
    {
        VIGRA_ASSERT_INSIDE(i);
        return data()[i];
    }

        /** Read array element \a i.
        */
    const_reference operator[]( difference_type i ) const
    {
        VIGRA_ASSERT_INSIDE(i);
        return data()[i];
    }

        /** Equivalent to <tt>size() == 0</tt>.
        */
    bool empty() const
    {
        return size_ == 0;
    }

        /** Number of elements in the array.
        */
    size_type size() const
    {
        return size_;
    }

        /** Check for element-wise equality of two array.
            Also returns <tt>false</tt> if the two arrays have different sizes.
        */
    template <class U>
    bool operator==(ArrayVectorView<U> const & rhs) const;

        /** check whether two arrays are not elementwise equal.
            Also returns <tt>true</tt> if the two arrays have different sizes.
         */
    template <class U>
    bool operator!=(ArrayVectorView<U> const & rhs) const
    {
        return !operator==(rhs);
    }

        /** check whether the given point is in the array range.
         */
    bool isInside (difference_type const & p) const
    {
        return p >= 0 && p < size_;
    }

  protected:

    template <class U>
    void copyImpl(const ArrayVectorView <U>& rhs);

    void copyImpl(const ArrayVectorView & rhs);

    template <class U>
    void swapDataImpl(const ArrayVectorView <U>& rhs);

    size_type size_;
    pointer data_;
};

template <class T>
ArrayVectorView<T> & ArrayVectorView<T>::operator=( ArrayVectorView<T> const & rhs )
{
    if(data_ == 0)
    {
        size_ = rhs.size_;
        data_ = rhs.data_;
    }
    else if(data_ != rhs.data_)
        copyImpl(rhs);
    return *this;
}

template <class T>
template <class U>
bool ArrayVectorView<T>::operator==(ArrayVectorView<U> const & rhs) const
{
    if(size() != rhs.size())
        return false;
    for(size_type k=0; k<size(); ++k)
        if(data_[k] != rhs[k])
            return false;
    return true;
}

template <class T>
void
ArrayVectorView <T>::copyImpl(const ArrayVectorView & rhs)
{
    vigra_precondition (size() == rhs.size(),
        "ArrayVectorView::copy(): shape mismatch.");
    if(size() == 0)  // needed because MSVC debug assertions in std::copy() may fire
        return;      // "invalid address: data_ == NULL" even when nothing is to be copied
    // use copy() or copy_backward() according to possible overlap of this and rhs
    if(data_ <= rhs.data())
    {
        std::copy(rhs.begin(), rhs.end(), begin());
    }
    else
    {
        std::copy_backward(rhs.begin(), rhs.end(), end());
    }
}

template <class T>
template <class U>
void
ArrayVectorView <T>::copyImpl(const ArrayVectorView <U>& rhs)
{
    vigra_precondition (size() == rhs.size(),
        "ArrayVectorView::copy(): shape mismatch.");
    std::copy(rhs.begin(), rhs.end(), begin());
}

template <class T>
template <class U>
void
ArrayVectorView <T>::swapDataImpl(const ArrayVectorView <U>& rhs)
{
    vigra_precondition (size () == rhs.size() (),
        "ArrayVectorView::swapData(): size mismatch.");

    // check for overlap
    if(data_ + size_ <= rhs.data_ || rhs.data_ + size_ <= data_)
    {
        for(size_type k=0; k<size_; ++k)
            std::swap(data_[k], rhs.data_[k]);
    }
    else
    {
        ArrayVector<T> t(*this);
        copyImpl(rhs);
        rhs.copyImpl(t);
    }
}


/** Replacement for <tt>std::vector</tt>.

    This template implements the same functionality as <tt><a href="http://www.sgi.com/tech/stl/Vector.html">std::vector</a></tt> (see there for detailed documentation).
    However, it gives two useful guarantees, that <tt>std::vector</tt> fails
    to provide:

    <ul>
    <li>The memory is always allocated as one contiguous piece.</li>
    <li>The iterator is always a <TT>T *</TT> </li>
    </ul>

    This means that memory managed by <tt>ArrayVector</tt> can be passed
    to algorithms that expect raw memory. This is especially important
    when legacy or C code has to be called, but it is also useful for certain
    optimizations.

    Moreover, <tt>ArrayVector</tt> is derived from <tt>ArrayVectorView</tt> so that one
    can create views of the array (in particular, subarrays). This implies another
    important difference to <tt>std::vector</tt>: the indexing operator
    (<tt>ArrayVector::operator[]</tt>) takes <tt>signed</tt> indices. In this way,
    an <tt>ArrayVectorView</tt> can be used with negative indices:

    \code
    ArrayVector<int> data(100);
    ArrayVectorView<int> view = data.subarray(50, 100);

    view[-50] = 1; // valid access
    \endcode

    Refer to the documentation of <tt>std::vector</tt> for a detailed
    description of <tt>ArrayVector</tt> functionality.

    <b>\#include</b> \<vigra/array_vector.hxx\><br>
    Namespace: vigra
*/
template <class T, class Alloc /* = std::allocator<T> */ >
class ArrayVector
: public ArrayVectorView<T>
{
    typedef ArrayVector<T, Alloc> this_type;
    enum { minimumCapacity = 2, resizeFactor = 2 };

public:
    typedef ArrayVectorView<T> view_type;
    typedef typename view_type::value_type value_type;
    typedef typename view_type::reference reference;
    typedef typename view_type::const_reference const_reference;
    typedef typename view_type::pointer pointer;
    typedef typename view_type::const_pointer const_pointer;
    typedef typename view_type::iterator iterator;
    typedef typename view_type::const_iterator const_iterator;
    typedef typename view_type::size_type size_type;
    typedef typename view_type::difference_type difference_type;
    typedef typename view_type::reverse_iterator reverse_iterator;
    typedef typename view_type::const_reverse_iterator const_reverse_iterator;
    typedef Alloc        allocator_type;

public:
    ArrayVector()
    : view_type(),
      capacity_(minimumCapacity),
      alloc_(Alloc())
    {
        this->data_ = reserve_raw(capacity_);
    }

    explicit ArrayVector(Alloc const & alloc)
    : view_type(),
      capacity_(minimumCapacity),
      alloc_(alloc)
    {
        this->data_ = reserve_raw(capacity_);
    }

    explicit ArrayVector( size_type size, Alloc const & alloc = Alloc())
    : view_type(),
      alloc_(alloc)
    {
        initImpl(size, value_type(), VigraTrueType());
    }

    ArrayVector( size_type size, value_type const & initial, Alloc const & alloc = Alloc())
    : view_type(),
      alloc_(alloc)
    {
        initImpl(size, initial, VigraTrueType());
    }


    ArrayVector( this_type const & rhs )
    : view_type(),
      alloc_(rhs.alloc_)
    {
        initImpl(rhs.begin(), rhs.end(), VigraFalseType());
    }

    template <class U>
    explicit ArrayVector( ArrayVectorView<U> const & rhs, Alloc const & alloc = Alloc() )
    : view_type(),
      alloc_(alloc)
    {
        initImpl(rhs.begin(), rhs.end(), VigraFalseType());
    }

    template <class InputIterator>
    ArrayVector(InputIterator i, InputIterator end)
    {
        initImpl(i, end, typename NumericTraits<InputIterator>::isIntegral());
    }

    template <class InputIterator>
    ArrayVector(InputIterator i, InputIterator end, Alloc const & alloc)
    : alloc_(alloc)
    {
        initImpl(i, end, typename NumericTraits<InputIterator>::isIntegral());
    }

    this_type & operator=( this_type const & rhs )
    {
        if(this == &rhs)
            return *this;
        if(this->size_ == rhs.size_)
            this->copyImpl(rhs);
        else
        {
            ArrayVector t(rhs);
            this->swap(t);
        }
        return *this;
    }

    template <class U>
    this_type & operator=( ArrayVectorView<U> const & rhs);

    ~ArrayVector()
    {
        deallocate(this->data_, this->size_, this->capacity_);
    }

    void pop_back();

    void push_back( value_type const & t );

    iterator insert(iterator p, value_type const & v);

    iterator insert(iterator p, size_type n, value_type const & v);

    template <class InputIterator>
    iterator insert(iterator p, InputIterator i, InputIterator iend);

    iterator erase(iterator p);

    iterator erase(iterator p, iterator q);

    void clear();

    pointer reserveImpl( bool dealloc, size_type new_capacity );

    pointer reserveImpl( bool dealloc);

    void reserve()
    {
        reserveImpl(true);
    }

    void reserve( size_type new_capacity )
    {
        reserveImpl(true, new_capacity);
    }

    void resize( size_type new_size, value_type const & initial );

    void resize( size_type new_size )
    {
        resize(new_size, value_type());
    }

    size_type capacity() const
    {
        return capacity_;
    }

    void swap(this_type & rhs);

  private:

    void deallocate(pointer data, size_type size, size_type capacity);

    pointer reserve_raw(size_type capacity);

    void initImpl( size_type size, value_type const & initial, VigraTrueType /*isIntegral*/);

    template <class Iter>
    void initImpl( Iter i, Iter end, VigraFalseType /*isIntegral*/);

    template <class Iter>
    void initImpl( Iter i, Iter end, Error_NumericTraits_not_specialized_for_this_case)
    {
        initImpl(i, end, VigraFalseType());
    }

    size_type capacity_;
    Alloc alloc_;
};

template <class T, class Alloc>
template <class U>
ArrayVector<T, Alloc> & ArrayVector<T, Alloc>::operator=( ArrayVectorView<U> const & rhs )
{
    if(this->size_ == rhs.size())
        this->copyImpl(rhs);
    else
    {
        ArrayVector t(rhs);
        this->swap(t);
    }
    return *this;
}

template <class T, class Alloc>
inline void ArrayVector<T, Alloc>::pop_back()
{
    --this->size_;
    alloc_.destroy(this->data_ + this->size_);
}

template <class T, class Alloc>
inline void ArrayVector<T, Alloc>::push_back( value_type const & t )
{
	size_type old_capacity = this->capacity_;
	pointer old_data = reserveImpl(false);
    alloc_.construct(this->data_ + this->size_, t);
    // deallocate old data _after_ construction of new element, so that
    // 't' can refer to the old data as in 'push_back(front())'
    deallocate(old_data, this->size_, old_capacity);
    ++this->size_;
}

template <class T, class Alloc>
inline void ArrayVector<T, Alloc>::clear()
{
    detail::destroy_n(this->data_, this->size_);
    this->size_ = 0;
}

template <class T, class Alloc>
typename ArrayVector<T, Alloc>::iterator
ArrayVector<T, Alloc>::insert(iterator p, value_type const & v)
{
    difference_type pos = p - this->begin();
    if(p == this->end())
    {
        push_back(v);
        p = this->begin() + pos;
    }
    else
    {
        T lastElement = this->back();
        push_back(lastElement);
        p = this->begin() + pos;
        std::copy_backward(p, this->end() - 2, this->end() - 1);
        *p = v;
    }
    return p;
}

template <class T, class Alloc>
typename ArrayVector<T, Alloc>::iterator
ArrayVector<T, Alloc>::insert(iterator p, size_type n, value_type const & v)
{
    difference_type pos = p - this->begin();
    size_type new_size = this->size() + n;
    if(new_size > capacity_)
    {
        size_type new_capacity = std::max(new_size, resizeFactor*capacity_);
        pointer new_data = reserve_raw(new_capacity);
        try
        {
            std::uninitialized_copy(this->begin(), p, new_data);
            std::uninitialized_fill(new_data + pos, new_data + pos + n, v);
            std::uninitialized_copy(p, this->end(), new_data + pos + n);
        }
        catch(...)
        {
            alloc_.deallocate(new_data, new_capacity);
            throw;
        }
        deallocate(this->data_, this->size_, this->capacity_);
        capacity_ = new_capacity;
        this->data_ = new_data;
    }
    else if(pos + n > this->size_)
    {
        size_type diff = pos + n - this->size_;
        std::uninitialized_copy(p, this->end(), this->end() + diff);
        std::uninitialized_fill(this->end(), this->end() + diff, v);
        std::fill(p, this->end(), v);
    }
    else
    {
        size_type diff = this->size_ - (pos + n);
        std::uninitialized_copy(this->end() - n, this->end(), this->end());
        std::copy_backward(p, p + diff, this->end());
        std::fill(p, p + n, v);
    }
    this->size_ = new_size;
    return this->begin() + pos;
}

template <class T, class Alloc>
template <class InputIterator>
typename ArrayVector<T, Alloc>::iterator
ArrayVector<T, Alloc>::insert(iterator p, InputIterator i, InputIterator iend)
{
    size_type n = std::distance(i, iend);
    size_type pos = p - this->begin();
    size_type new_size = this->size() + n;
    if(new_size > capacity_)
    {
        size_type new_capacity = std::max(new_size, resizeFactor*capacity_);
        pointer new_data = reserve_raw(new_capacity);
        try
        {
            std::uninitialized_copy(this->begin(), p, new_data);
            std::uninitialized_copy(i, iend, new_data + pos);
            std::uninitialized_copy(p, this->end(), new_data + pos + n);
        }
        catch(...)
        {
            alloc_.deallocate(new_data, new_capacity);
            throw;
        }
        deallocate(this->data_, this->size_, this->capacity_);
        capacity_ = new_capacity;
        this->data_ = new_data;
    }
    else if(pos + n > this->size_)
    {
        size_type diff = pos + n - this->size_;
        std::uninitialized_copy(p, this->end(), this->end() + diff);
        InputIterator split = i;
        std::advance(split, n - diff);
        std::uninitialized_copy(split, iend, this->end());
        std::copy(i, split, p);
    }
    else
    {
        size_type diff = this->size_ - (pos + n);
        std::uninitialized_copy(this->end() - n, this->end(), this->end());
        std::copy_backward(p, p + diff, this->end());
        std::copy(i, iend, p);
    }
    this->size_ = new_size;
    return this->begin() + pos;
}

template <class T, class Alloc>
typename ArrayVector<T, Alloc>::iterator
ArrayVector<T, Alloc>::erase(iterator p)
{
    std::copy(p+1, this->end(), p);
    pop_back();
    return p;
}

template <class T, class Alloc>
typename ArrayVector<T, Alloc>::iterator
ArrayVector<T, Alloc>::erase(iterator p, iterator q)
{
    std::copy(q, this->end(), p);
    difference_type eraseCount = q - p;
    detail::destroy_n(this->end() - eraseCount, eraseCount);
    this->size_ -= eraseCount;
    return p;
}

template <class T, class Alloc>
typename ArrayVector<T, Alloc>::pointer
ArrayVector<T, Alloc>::reserveImpl( bool dealloc, size_type new_capacity)
{
    if(new_capacity <= capacity_)
        return 0;
    pointer new_data = reserve_raw(new_capacity),
            old_data = this->data_;
    if(this->size_ > 0)
        std::uninitialized_copy(old_data, old_data+this->size_, new_data);
    this->data_ = new_data;
    if(!dealloc)
    {
        this->capacity_ = new_capacity;
        return old_data;
    }
    deallocate(old_data, this->size_, this->capacity_);
    this->capacity_ = new_capacity;
    return 0;
}

template <class T, class Alloc>
inline typename ArrayVector<T, Alloc>::pointer
ArrayVector<T, Alloc>::reserveImpl(bool dealloc)
{
    if(capacity_ == 0)
        return reserveImpl(dealloc, minimumCapacity);
    else if(this->size_ == capacity_)
        return reserveImpl(dealloc, resizeFactor*capacity_);
    else
        return 0;
}

template <class T, class Alloc>
inline void
ArrayVector<T, Alloc>::resize( size_type new_size, value_type const & initial)
{
    if(new_size < this->size_)
        erase(this->begin() + new_size, this->end());
    else if(this->size_ < new_size)
    {
        insert(this->end(), new_size - this->size(), initial);
    }
}

template <class T, class Alloc>
inline void
ArrayVector<T, Alloc>::initImpl( size_type size, value_type const & initial, VigraTrueType /*isIntegral*/)
{
    this->size_ = size;
    capacity_ = size;
    this->data_ = reserve_raw(capacity_);
    if(this->size_ > 0)
        std::uninitialized_fill(this->data_, this->data_+this->size_, initial);
}

template <class T, class Alloc>
template <class Iter>
inline void
ArrayVector<T, Alloc>::initImpl( Iter i, Iter end, VigraFalseType /*isIntegral*/)
{
    this->size_ = std::distance(i, end);
    capacity_ = this->size_;
    this->data_ = reserve_raw(capacity_);
    if(this->size_ > 0)
        detail::uninitializedCopy(i, end, this->data_);
}

template <class T, class Alloc>
inline void
ArrayVector<T, Alloc>::swap(this_type & rhs)
{
    std::swap(this->size_, rhs.size_);
    std::swap(capacity_, rhs.capacity_);
    std::swap(this->data_, rhs.data_);
}

template <class T, class Alloc>
inline void
ArrayVector<T, Alloc>::deallocate(pointer data, size_type size, size_type capacity)
{
    if(data)
    {
        detail::destroy_n(data, size);
        alloc_.deallocate(data, capacity);
    }
}

template <class T, class Alloc>
inline typename ArrayVector<T, Alloc>::pointer
ArrayVector<T, Alloc>::reserve_raw(size_type capacity)
{
    pointer data = 0;
    if(capacity)
    {
        data = alloc_.allocate(capacity);
    }
    return data;
}

} // namespace vigra

namespace std {

template <class T>
ostream & operator<<(ostream & s, vigra::ArrayVectorView<T> const & a)
{
    for(std::size_t k=0; k<a.size()-1; ++k)
        s << a[k] << ", ";
    if(a.size())
            s << a.back();
    return s;
}

} // namespace std

#undef VIGRA_ASSERT_INSIDE
#endif /* VIGRA_ARRAY_VECTOR_HXX */