]> git.lyx.org Git - lyx.git/blob - boost/boost/multi_array/iterator_adaptors.hpp
major boost update
[lyx.git] / boost / boost / multi_array / iterator_adaptors.hpp
1 // (C) Copyright David Abrahams 2000. Permission to copy, use,
2 // modify, sell and distribute this software is granted provided this
3 // copyright notice appears in all copies. This software is provided
4 // "as is" without express or implied warranty, and with no claim as
5 // to its suitability for any purpose.
6 //
7 // (C) Copyright Jeremy Siek 2000. Permission to copy, use, modify,
8 // sell and distribute this software is granted provided this
9 // copyright notice appears in all copies. This software is provided
10 // "as is" without express or implied warranty, and with no claim as
11 // to its suitability for any purpose.
12 //
13 // Revision History:
14
15 // 27 Mar 2002  Ronald Garcia
16 //      Forked from the main tree iterator adaptors. Necessary to
17 //      allow iterator::operator->*() to work with multi_array iterators.
18 // 01 Feb 2002   Jeremy Siek
19 //      Added more comments in default_iterator_policies.
20 // 08 Jan 2001   David Abrahams
21 //      Moved concept checks into a separate class, which makes MSVC
22 //      better at dealing with them.
23 // 07 Jan 2001   David Abrahams
24 //      Choose proxy for operator->() only if the reference type is not a reference.
25 //      Updated workarounds for __MWERKS__ == 0x2406
26 // 20 Dec 2001   David Abrahams
27 //      Adjusted is_convertible workarounds for __MWERKS__ == 0x2406
28 // 03 Nov 2001   Jeremy Siek
29 //      Changed the named template parameter interface and internal.
30 // 04 Oct 2001   Jeremy Siek
31 //      Changed projection_iterator to not rely on the default reference,
32 //      working around a limitation of detail::iterator_traits.
33 // 04 Oct 2001   David Abrahams
34 //      Applied indirect_iterator patch from George A. Heintzelman <georgeh@aya.yale.edu>
35 //      Changed name of "bind" to "select" to avoid problems with MSVC.
36 // 26 Sep 2001   David Abrahams
37 //      Added borland bug fix
38 // 08 Mar 2001   Jeremy Siek
39 //      Added support for optional named template parameters.
40 // 19 Feb 2001   David Abrahams
41 //      Rolled back reverse_iterator_pair_generator again, as it doesn't
42 //      save typing on a conforming compiler.
43 // 18 Feb 2001   David Abrahams
44 //      Reinstated reverse_iterator_pair_generator
45 // 16 Feb 2001   David Abrahams
46 //      Add an implicit conversion operator to operator_arrow_proxy
47 //      as CW and BCC workarounds.
48 // 11 Feb 2001   David Abrahams
49 //      Switch to use of BOOST_STATIC_CONSTANT where possible
50 // 11 Feb 2001   Jeremy Siek
51 //      Removed workaround for older MIPSpro compiler. The workaround
52 //        was preventing the proper functionality of the underlying
53 //        iterator being carried forward into the iterator adaptor.
54 //        Also added is_bidirectional enum to avoid EDG compiler error.
55 // 11 Feb 2001   David Abrahams
56 //      Borland fixes up the wazoo. It finally works!
57 // 10 Feb 2001   David Abrahams
58 //      Removed traits argument from iterator_adaptor<> and switched to
59 //        explicit trait specification for maximum ease-of-use.
60 //      Added comments to detail::iterator_defaults<>
61 //      Began using detail::iterator_defaults<> unconditionally for code clarity
62 //      Changed uses of `Iterator' to `Base' where non-iterators can be used.
63 //
64 // 10 Feb 2001   David Abrahams
65 //      Rolled in supposed Borland fixes from John Maddock, but not seeing any
66 //        improvement yet
67 //      Changed argument order to indirect_ generator, for convenience in the
68 //        case of input iterators (where Reference must be a value type).
69 //      Removed derivation of filter_iterator_policies from
70 //        default_iterator_policies, since the iterator category is likely to be
71 //        reduced (we don't want to allow illegal operations like decrement).
72 //      Support for a simpler filter iterator interface.
73 //
74 // 09 Feb 2001   David Abrahams
75 //      Improved interface to indirect_ and reverse_ iterators
76 //      Rolled back Jeremy's new constructor for now; it was causing
77 //        problems with counting_iterator_test
78 //      Attempted fix for Borland
79 //
80 // 09 Feb 2001   Jeremy Siek
81 //      Added iterator constructor to allow const adaptor
82 //        from non-const adaptee.
83 //      Changed make_xxx to pass iterators by-value to
84 //        get arrays converted to pointers.
85 //      Removed InnerIterator template parameter from
86 //        indirect_iterator_generator.
87 //      Rearranged parameters for make_filter_iterator
88 //
89 // 07 Feb 2001   Jeremy Siek
90 //      Removed some const iterator adaptor generators.
91 //      Added make_xxx_iterator() helper functions for remaining
92 //        iterator adaptors.
93 //      Removed some traits template parameters where they
94 //        where no longer needed thanks to detail::iterator_traits.
95 //      Moved some of the compile-time logic into enums for
96 //      EDG compatibility.
97 //
98 // 07 Feb 2001  David Abrahams
99 //      Removed iterator_adaptor_pair_generator and
100 //        reverse_iterator_pair_generator (more such culling to come)
101 //      Improved comments
102 //      Changed all uses of std::iterator_traits as default arguments
103 //        to boost::detail::iterator_traits for improved utility in
104 //        non-generic contexts
105 //      Fixed naming convention of non-template parameter names
106 //
107 // 06 Feb 2001   David Abrahams
108 //      Produce operator-> proxy objects for InputIterators
109 //      Added static assertions to do some basic concept checks
110 //      Renamed single-type generators -> xxx_generator
111 //      Renamed const/nonconst iterator generators -> xxx_pair_generator
112 //      Added make_transform_iterator(iter, function)
113 //      The existence of boost::detail::iterator_traits allowed many
114 //        template arguments to be defaulted. Some arguments had to be
115 //        moved to accomplish it.
116 //
117 // 04 Feb 2001  MWERKS bug workaround, concept checking for proper
118 //              reference types (David Abrahams)
119
120 #ifndef BOOST_ITERATOR_ADAPTOR_RG032702_HPP_
121 # define BOOST_ITERATOR_ADAPTOR_RG032702_HPP_
122
123 # include <boost/iterator.hpp>
124 # include <boost/utility.hpp>
125 # include <boost/compressed_pair.hpp>
126 # include <boost/concept_check.hpp>
127 # include <boost/type.hpp>
128 # include <boost/static_assert.hpp>
129 # include <boost/type_traits.hpp>
130 # include <boost/detail/iterator.hpp>
131 # include <boost/detail/select_type.hpp>
132
133 // I was having some problems with VC6. I couldn't tell whether our hack for
134 // stock GCC was causing problems so I needed an easy way to turn it on and
135 // off. Now we can test the hack with various compilers and still have an
136 // "out" if it doesn't work. -dwa 7/31/00
137 # if __GNUC__ == 2 && __GNUC_MINOR__ <= 96 && !defined(__STL_USE_NAMESPACES)
138 #  define BOOST_RELOPS_AMBIGUITY_BUG 1
139 # endif
140
141 namespace boost {
142 namespace detail {
143 namespace multi_array {
144
145 //============================================================================
146 // Default policies for iterator adaptors. You can use this as a base
147 // class if you want to customize particular policies.
148 struct default_iterator_policies
149 {
150     // Some of the member functions were defined static, but Borland
151     // got confused and thought they were non-const. Also, Sun C++
152     // does not like static function templates. 
153     //
154     // The reason some members were defined static is because there is
155     // not state (data members) needed by those members of the
156     // default_iterator_policies class. If your policies class member
157     // functions need to access state stored in the policies object,
158     // then the member functions should not be static (they can't be).
159
160     template <class Base>
161     void initialize(Base&)
162         { }
163
164     template <class IteratorAdaptor>
165     typename IteratorAdaptor::reference dereference(const IteratorAdaptor& x) const
166         { return *x.base(); }
167
168     template <class IteratorAdaptor>
169     void increment(IteratorAdaptor& x)
170         { ++x.base(); }
171
172     template <class IteratorAdaptor>
173     void decrement(IteratorAdaptor& x)
174         { --x.base(); }
175
176     template <class IteratorAdaptor, class DifferenceType>
177     void advance(IteratorAdaptor& x, DifferenceType n)
178         { x.base() += n; }
179
180     template <class IteratorAdaptor1, class IteratorAdaptor2>
181     typename IteratorAdaptor1::difference_type
182     distance(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const
183         { return y.base() - x.base(); }
184
185     template <class IteratorAdaptor1, class IteratorAdaptor2>
186     bool equal(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const
187         { return x.base() == y.base(); }
188 };
189
190 // putting the comparisons in a base class avoids the g++
191 // ambiguous overload bug due to the relops operators
192
193 #ifdef BOOST_RELOPS_AMBIGUITY_BUG
194 template <class Derived, class Base>
195 struct iterator_comparisons : Base { };
196
197 template <class D1, class D2, class Base1, class Base2>
198 inline bool operator==(const iterator_comparisons<D1,Base1>& xb,
199                        const iterator_comparisons<D2,Base2>& yb)
200 {
201         const D1& x = static_cast<const D1&>(xb);
202     const D2& y = static_cast<const D2&>(yb);
203     return x.policies().equal(x, y);
204 }
205
206 template <class D1, class D2, class Base1, class Base2>
207 inline bool operator!=(const iterator_comparisons<D1,Base1>& xb,
208                        const iterator_comparisons<D2,Base2>& yb)
209 {
210     const D1& x = static_cast<const D1&>(xb);
211     const D2& y = static_cast<const D2&>(yb);
212     return !x.policies().equal(x, y);
213 }
214
215 template <class D1, class D2, class Base1, class Base2>
216 inline bool operator<(const iterator_comparisons<D1,Base1>& xb,
217                       const iterator_comparisons<D2,Base2>& yb)
218 {
219     const D1& x = static_cast<const D1&>(xb);
220     const D2& y = static_cast<const D2&>(yb);
221     return x.policies().distance(y, x) < 0;
222 }
223
224 template <class D1, class D2, class Base1, class Base2>
225 inline bool operator>(const iterator_comparisons<D1,Base1>& xb,
226                       const iterator_comparisons<D2,Base2>& yb)
227 {
228     const D1& x = static_cast<const D1&>(xb);
229     const D2& y = static_cast<const D2&>(yb);
230     return x.policies().distance(y, x) > 0;
231 }
232
233 template <class D1, class D2, class Base1, class Base2>
234 inline bool operator>=(const iterator_comparisons<D1,Base1>& xb,
235                        const iterator_comparisons<D2,Base2>& yb)
236 {
237     const D1& x = static_cast<const D1&>(xb);
238     const D2& y = static_cast<const D2&>(yb);
239     return x.policies().distance(y, x) >= 0;
240 }
241
242 template <class D1, class D2, class Base1, class Base2>
243 inline bool operator<=(const iterator_comparisons<D1,Base1>& xb,
244                        const iterator_comparisons<D2,Base2>& yb)
245 {
246     const D1& x = static_cast<const D1&>(xb);
247     const D2& y = static_cast<const D2&>(yb);
248     return x.policies().distance(y, x) <= 0;
249 }
250 #endif
251
252 namespace detail {
253
254   // operator->() needs special support for input iterators to strictly meet the
255   // standard's requirements. If *i is not a reference type, we must still
256   // produce a (constant) lvalue to which a pointer can be formed. We do that by
257   // returning an instantiation of this special proxy class template.
258
259   template <class T>
260   struct operator_arrow_proxy
261   {
262       operator_arrow_proxy(const T& x) : m_value(x) {}
263     // RG removed const below
264       T* operator->() const { return &m_value; }
265       // This function is needed for MWCW and BCC, which won't call operator->
266       // again automatically per 13.3.1.2 para 8
267
268     // RG - removed const below
269       operator T*() const { return &m_value; }
270       mutable T m_value;
271   };
272
273   template <class Iter>
274   inline operator_arrow_proxy<typename Iter::reference>
275   operator_arrow(const Iter& i, std::input_iterator_tag) {
276     // RG - THIS is the change I needed to make!
277     // My input iterators need to return proxy references rather than values
278     typedef typename Iter::reference value_t; // VC++ needs this typedef
279     return operator_arrow_proxy<value_t>(*i);
280   }
281
282   template <class Iter>
283   inline typename Iter::pointer
284   operator_arrow(const Iter& i, std::forward_iterator_tag) {
285     return &(*i);
286   }
287
288   template <class Value, class Reference, class Pointer>
289   struct operator_arrow_result_generator
290   {
291     //RG - another important change!
292       typedef operator_arrow_proxy<Reference> proxy;
293       // Borland chokes unless it's an actual enum (!)
294       enum { use_proxy = !boost::is_reference<Reference>::value };
295
296       typedef typename boost::detail::if_true<(use_proxy)>::template
297       then<
298         proxy,
299    // else
300         Pointer
301       >::type type;
302   };
303
304
305 # ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
306
307    // Select default pointer and reference types for adapted non-pointer
308    // iterators based on the iterator and the value_type. Poor man's partial
309    // specialization is in use here.
310    template <bool is_pointer>
311    struct iterator_defaults_select
312    {
313        template <class Iterator,class Value>
314        struct traits
315        {
316            // The assumption is that iterator_traits can deduce these types
317            // properly as long as the iterator is not a pointer.
318            typedef typename boost::detail::iterator_traits<Iterator>::pointer pointer;
319            typedef typename boost::detail::iterator_traits<Iterator>::reference reference;
320        };
321    };
322
323    // Select default pointer and reference types for adapted pointer iterators
324    // given a (possibly-const) value_type.
325    template <>
326    struct iterator_defaults_select<true>
327    {
328        template <class Iterator,class Value>
329        struct traits
330        {
331            typedef Value* pointer;
332            typedef Value& reference;
333        };
334    };
335
336    // Consolidate selection of the default pointer and reference type
337    template <class Iterator,class Value>
338    struct iterator_defaults
339    {
340        BOOST_STATIC_CONSTANT(bool, is_ptr = boost::is_pointer<Iterator>::value);
341
342        typedef typename iterator_defaults_select<is_ptr>::template traits<Iterator,Value> traits;
343        typedef typename traits::pointer pointer;
344        typedef typename traits::reference reference;
345    };
346 # else
347    template <class Iterator,class Value>
348    struct iterator_defaults : iterator_traits<Iterator>
349    {
350        // Trying to factor the common is_same expression into an enum or a
351        // static bool constant confused Borland.
352        typedef typename if_true<(
353                ::boost::is_same<Value,typename iterator_traits<Iterator>::value_type>::value
354            )>::template then<
355                 typename iterator_traits<Iterator>::pointer,
356                 Value*
357        >::type pointer;
358
359        typedef typename if_true<(
360                ::boost::is_same<Value,typename iterator_traits<Iterator>::value_type>::value
361            )>::template then<
362                 typename iterator_traits<Iterator>::reference,
363                 Value&
364        >::type reference;
365
366    };
367 # endif
368
369   //===========================================================================
370   // Specify the defaults for iterator_adaptor's template parameters
371
372   struct default_argument { };
373   // This class template is a workaround for MSVC.
374   struct dummy_default_gen {
375     template <class Base, class Traits>
376     struct select { typedef default_argument type; };
377   };
378   // This class template is a workaround for MSVC.
379   template <class Gen> struct default_generator {
380     typedef dummy_default_gen type;
381   };
382
383   struct default_value_type {
384     template <class Base, class Traits>
385     struct select {
386       typedef typename boost::detail::iterator_traits<Base>::value_type type;
387     };
388   };
389   template <> struct default_generator<default_value_type>
390   { typedef default_value_type type; }; // VC++ workaround
391
392   struct default_difference_type {
393     template <class Base, class Traits>
394     struct select {
395       typedef typename boost::detail::iterator_traits<Base>::difference_type type;
396     };
397   };
398   template <> struct default_generator<default_difference_type>
399   { typedef default_difference_type type; }; // VC++ workaround
400
401   struct default_iterator_category {
402     template <class Base, class Traits>
403     struct select {
404       typedef typename boost::detail::iterator_traits<Base>::iterator_category type;
405     };
406   };
407   template <> struct default_generator<default_iterator_category>
408   { typedef default_iterator_category type; }; // VC++ workaround
409
410   struct default_pointer {
411     template <class Base, class Traits>
412     struct select {
413       typedef typename Traits::value_type Value;
414       typedef typename boost::detail::multi_array::detail::iterator_defaults<Base,Value>::pointer
415         type;
416     };
417   };
418   template <> struct default_generator<default_pointer>
419   { typedef default_pointer type; }; // VC++ workaround
420
421   struct default_reference {
422     template <class Base, class Traits>
423     struct select {
424       typedef typename Traits::value_type Value;
425       typedef typename boost::detail::multi_array::detail::iterator_defaults<Base,Value>::reference
426         type;
427     };
428   };
429   template <> struct default_generator<default_reference>
430   { typedef default_reference type; }; // VC++ workaround
431
432 } // namespace detail
433
434
435   //===========================================================================
436   // Support for named template parameters
437
438 struct named_template_param_base { };
439
440 namespace detail {
441   struct value_type_tag { };
442   struct reference_tag { };
443   struct pointer_tag { };
444   struct difference_type_tag { };
445   struct iterator_category_tag { };
446
447   // avoid using std::pair because A or B might be a reference type, and g++
448   // complains about forming references to references inside std::pair
449   template <class A, class B>
450   struct cons_type {
451     typedef A first_type;
452     typedef B second_type;
453   };
454
455 } // namespace detail
456
457 template <class Value> struct value_type_is : public named_template_param_base
458 {
459   typedef detail::cons_type<detail::value_type_tag, Value> type;
460 };
461 template <class Reference> struct reference_is : public named_template_param_base
462 {
463   typedef detail::cons_type<detail::reference_tag, Reference> type;
464 };
465 template <class Pointer> struct pointer_is : public named_template_param_base
466 {
467   typedef detail::cons_type<detail::pointer_tag, Pointer> type;
468 };
469 template <class Difference> struct difference_type_is
470   : public named_template_param_base
471 {
472   typedef detail::cons_type<detail::difference_type_tag, Difference> type;
473 };
474 template <class IteratorCategory> struct iterator_category_is
475   : public named_template_param_base
476 {
477   typedef detail::cons_type<detail::iterator_category_tag, IteratorCategory> type;
478 };
479
480 namespace detail {
481
482   struct end_of_list { };
483
484   // Given an associative list, find the value with the matching key.
485   // An associative list is a list of key-value pairs. The list is
486   // built out of cons_type's and is terminated by end_of_list.
487
488 # if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) || defined(__BORLANDC__)
489   template <class AssocList, class Key>
490   struct find_param;
491
492   struct find_param_continue {
493     template <class AssocList, class Key2> struct select {
494       typedef typename AssocList::first_type Head;
495       typedef typename Head::first_type Key1;
496       typedef typename Head::second_type Value;
497       typedef typename if_true<(is_same<Key1, Key2>::value)>::template
498       then<Value,
499         typename find_param<typename AssocList::second_type, Key2>::type
500       >::type type;
501     };
502   };
503   struct find_param_end {
504     template <class AssocList, class Key>
505     struct select { typedef detail::default_argument type; };
506   };
507   template <class AssocList> struct find_param_helper1
508   { typedef find_param_continue type; };
509   template <> struct find_param_helper1<end_of_list>
510   { typedef find_param_end type; };
511
512   template <class AssocList, class Key>
513   struct find_param {
514     typedef typename find_param_helper1<AssocList>::type select1;
515     typedef typename select1::template select<AssocList, Key>::type type;
516   };
517 # else
518   template <class AssocList, class Key> struct find_param;
519
520   template <class Key>
521   struct find_param<end_of_list, Key> { typedef default_argument type; };
522
523   // Found a matching Key, return the associated Value
524   template <class Key, class Value, class Rest>
525   struct find_param<detail::cons_type< detail::cons_type<Key, Value>, Rest>, Key> {
526     typedef Value type;
527   };
528
529   // Non-matching keys, continue the search
530   template <class Key1, class Value, class Rest, class Key2>
531   struct find_param<detail::cons_type< detail::cons_type<Key1, Value>, Rest>, Key2> {
532     typedef typename find_param<Rest, Key2>::type type;
533   };
534 # endif
535
536   struct make_named_arg {
537     template <class Key, class Value>
538     struct select { typedef typename Value::type type; };
539   };
540   struct make_key_value {
541     template <class Key, class Value>
542     struct select { typedef detail::cons_type<Key, Value> type; };
543   };
544
545   template <class Value>
546   struct is_named_parameter
547   {
548       enum { value = is_convertible<Value, named_template_param_base>::value };
549   };
550
551 # if defined(__MWERKS__) && __MWERKS__ <= 0x2406 // workaround for broken is_convertible implementation
552   template <class T> struct is_named_parameter<value_type_is<T> > { enum { value = true }; };
553   template <class T> struct is_named_parameter<reference_is<T> > { enum { value = true }; };
554   template <class T> struct is_named_parameter<pointer_is<T> > { enum { value = true }; };
555   template <class T> struct is_named_parameter<difference_type_is<T> > { enum { value = true }; };
556   template <class T> struct is_named_parameter<iterator_category_is<T> > { enum { value = true }; };
557 # endif
558
559   template <class Key, class Value>
560   struct make_arg {
561 # ifdef __BORLANDC__
562     // Borland C++ doesn't like the extra indirection of is_named_parameter
563     typedef typename
564       if_true<(is_convertible<Value,named_template_param_base>::value)>::
565       template then<make_named_arg, make_key_value>::type Make;
566 # else
567     enum { is_named = is_named_parameter<Value>::value };
568     typedef typename if_true<(is_named)>::template
569       then<make_named_arg, make_key_value>::type Make;
570 # endif
571     typedef typename Make::template select<Key, Value>::type type;
572   };
573
574   // Mechanism for resolving the default argument for a template parameter.
575
576   template <class T> struct is_default { typedef type_traits::no_type type; };
577   template <> struct is_default<default_argument>
578   { typedef type_traits::yes_type type; };
579
580   struct choose_default {
581     template <class Arg, class DefaultGen, class Base, class Traits>
582     struct select {
583       typedef typename default_generator<DefaultGen>::type Gen;
584       typedef typename Gen::template select<Base,Traits>::type type;
585     };
586   };
587   struct choose_arg {
588     template <class Arg, class DefaultGen, class Base, class Traits>
589     struct select {
590       typedef Arg type;
591     };
592   };
593
594   template <class UseDefault>
595   struct choose_arg_or_default { typedef choose_arg type; };
596   template <> struct choose_arg_or_default<type_traits::yes_type> {
597     typedef choose_default type;
598   };
599
600   template <class Arg, class DefaultGen, class Base, class Traits>
601   class resolve_default {
602     typedef typename choose_arg_or_default<typename is_default<Arg>::type>::type
603       Selector;
604   public:
605     typedef typename Selector
606       ::template select<Arg, DefaultGen, Base, Traits>::type type;
607   };
608
609   template <class Base, class Value, class Reference, class Pointer,
610             class Category, class Distance>
611   class iterator_adaptor_traits_gen
612   {
613     // Form an associative list out of the template parameters
614     // If the argument is a normal parameter (not named) then make_arg
615     // creates a key-value pair. If the argument is a named parameter,
616     // then make_arg extracts the key-value pair defined inside the
617     // named parameter.
618     typedef detail::cons_type< typename make_arg<value_type_tag, Value>::type,
619       detail::cons_type<typename make_arg<reference_tag, Reference>::type,
620       detail::cons_type<typename make_arg<pointer_tag, Pointer>::type,
621       detail::cons_type<typename make_arg<iterator_category_tag, Category>::type,
622       detail::cons_type<typename make_arg<difference_type_tag, Distance>::type,
623                 end_of_list> > > > > ArgList;
624
625     // Search the list for particular parameters
626     typedef typename find_param<ArgList, value_type_tag>::type Val;
627     typedef typename find_param<ArgList, difference_type_tag>::type Diff;
628     typedef typename find_param<ArgList, iterator_category_tag>::type Cat;
629     typedef typename find_param<ArgList, pointer_tag>::type Ptr;
630     typedef typename find_param<ArgList, reference_tag>::type Ref;
631
632     typedef boost::iterator<Category, Value, Distance, Pointer, Reference>
633       Traits0;
634
635     // Compute the defaults if necessary
636     typedef typename resolve_default<Val, default_value_type, Base, Traits0>::type
637       value_type;
638     // if getting default value type from iterator_traits, then it won't be const
639     typedef typename resolve_default<Diff, default_difference_type, Base,
640       Traits0>::type difference_type;
641     typedef typename resolve_default<Cat, default_iterator_category, Base,
642       Traits0>::type iterator_category;
643
644     typedef boost::iterator<iterator_category, value_type, difference_type,
645       Pointer, Reference> Traits1;
646
647     // Compute the defaults for pointer and reference. This is done as a
648     // separate step because the defaults for pointer and reference depend
649     // on value_type.
650     typedef typename resolve_default<Ptr, default_pointer, Base, Traits1>::type
651       pointer;
652     typedef typename resolve_default<Ref, default_reference, Base, Traits1>::type
653       reference;
654
655   public:
656     typedef boost::iterator<iterator_category,
657       typename remove_const<value_type>::type,
658       difference_type, pointer, reference> type;
659   };
660
661   // This is really a partial concept check for iterators. Should it
662   // be moved or done differently?
663   template <class Category, class Value, class Difference, class Pointer, class Reference>
664   struct validator
665   {
666       BOOST_STATIC_CONSTANT(
667           bool, is_input_or_output_iter
668           = (boost::is_convertible<Category*,std::input_iterator_tag*>::value
669              | boost::is_convertible<Category*,std::output_iterator_tag*>::value));
670
671       // Iterators should satisfy one of the known categories
672       BOOST_STATIC_ASSERT(is_input_or_output_iter);
673
674       // Iterators >= ForwardIterator must produce real references
675       // as required by the C++ standard requirements in Table 74.
676       BOOST_STATIC_CONSTANT(
677           bool, forward_iter_with_real_reference
678           = ((!boost::is_convertible<Category*,std::forward_iterator_tag*>::value)
679              | boost::is_same<Reference,Value&>::value
680              | boost::is_same<Reference,typename add_const<Value>::type&>::value));
681
682       BOOST_STATIC_ASSERT(forward_iter_with_real_reference);
683   };
684 } // namespace detail
685
686
687
688 // This macro definition is only temporary in this file
689 # if !defined(BOOST_MSVC)
690 #  define BOOST_ARG_DEPENDENT_TYPENAME typename
691 # else
692 #  define BOOST_ARG_DEPENDENT_TYPENAME
693 # endif
694
695 //============================================================================
696 //iterator_adaptor - Adapts a generic piece of data as an iterator. Adaptation
697 //      is especially easy if the data being adapted is itself an iterator
698 //
699 //   Base - the base (usually iterator) type being wrapped.
700 //
701 //   Policies - a set of policies determining how the resulting iterator
702 //      works.
703 //
704 //   Value - if supplied, the value_type of the resulting iterator, unless
705 //      const. If const, a conforming compiler strips constness for the
706 //      value_type. If not supplied, iterator_traits<Base>::value_type is used
707 //
708 //   Reference - the reference type of the resulting iterator, and in
709 //      particular, the result type of operator*(). If not supplied but
710 //      Value is supplied, Value& is used. Otherwise
711 //      iterator_traits<Base>::reference is used.
712 //
713 //   Pointer - the pointer type of the resulting iterator, and in
714 //      particular, the result type of operator->(). If not
715 //      supplied but Value is supplied, Value* is used. Otherwise
716 //      iterator_traits<Base>::pointer is used.
717 //
718 //   Category - the iterator_category of the resulting iterator. If not
719 //      supplied, iterator_traits<Base>::iterator_category is used.
720 //
721 //   Distance - the difference_type of the resulting iterator. If not
722 //      supplied, iterator_traits<Base>::difference_type is used.
723 template <class Base, class Policies,
724     class Value = ::boost::detail::multi_array::detail::default_argument,
725     class Reference = ::boost::detail::multi_array::detail::default_argument,
726     class Pointer = ::boost::detail::multi_array::detail::default_argument,
727     class Category = ::boost::detail::multi_array::detail::default_argument,
728     class Distance = ::boost::detail::multi_array::detail::default_argument
729          >
730 struct iterator_adaptor :
731 #ifdef BOOST_RELOPS_AMBIGUITY_BUG
732     iterator_comparisons<
733           iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance>,
734     typename detail::iterator_adaptor_traits_gen<Base,Value,Reference,Pointer,Category, Distance>::type
735  >
736 #else
737     detail::iterator_adaptor_traits_gen<Base,Value,Reference,Pointer,Category,Distance>::type
738 #endif
739 {
740     typedef iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance> self;
741  public:
742     typedef detail::iterator_adaptor_traits_gen<Base,Value,Reference,Pointer,Category,Distance> TraitsGen;
743     typedef typename TraitsGen::type Traits;
744
745     typedef typename Traits::difference_type difference_type;
746     typedef typename Traits::value_type value_type;
747     typedef typename Traits::pointer pointer;
748     typedef typename Traits::reference reference;
749     typedef typename Traits::iterator_category iterator_category;
750
751     typedef Base base_type;
752     typedef Policies policies_type;
753
754  private:
755     typedef detail::validator<
756         iterator_category,value_type,difference_type,pointer,reference
757         > concept_check;
758
759  public:
760     iterator_adaptor()
761     {
762     }
763
764     explicit
765     iterator_adaptor(const Base& it, const Policies& p = Policies())
766         : m_iter_p(it, p) {
767         policies().initialize(base());
768     }
769
770     template <class Iter2, class Value2, class Pointer2, class Reference2>
771     iterator_adaptor (
772         const iterator_adaptor<Iter2,Policies,Value2,Reference2,Pointer2,Category,Distance>& src)
773             : m_iter_p(src.base(), src.policies())
774     {
775         policies().initialize(base());
776     }
777
778 #if defined(BOOST_MSVC) || defined(__BORLANDC__)
779     // This is required to prevent a bug in how VC++ generates
780     // the assignment operator for compressed_pairv
781     iterator_adaptor& operator= (const iterator_adaptor& x) {
782         m_iter_p = x.m_iter_p;
783         return *this;
784     }
785 #endif
786     reference operator*() const {
787          return policies().dereference(*this);
788     }
789
790 #ifdef BOOST_MSVC
791 # pragma warning(push)
792 # pragma warning( disable : 4284 )
793 #endif
794
795     typename boost::detail::multi_array::detail::operator_arrow_result_generator<value_type,reference,pointer>::type
796     operator->() const
797         { return detail::operator_arrow(*this, iterator_category()); }
798
799 #ifdef BOOST_MSVC
800 # pragma warning(pop)
801 #endif
802
803     value_type operator[](difference_type n) const
804         { return *(*this + n); }
805
806     self& operator++() {
807 #if !defined(__MWERKS__) || __MWERKS__ >= 0x2405
808         policies().increment(*this);
809 #else
810         // Odd bug, MWERKS couldn't  deduce the type for the member template
811         // Workaround by explicitly specifying the type.
812         policies().increment<self>(*this);
813 #endif
814         return *this;
815     }
816
817     self operator++(int) { self tmp(*this); ++*this; return tmp; }
818
819     self& operator--() {
820 #if !defined(__MWERKS__) || __MWERKS__ >= 0x2405
821         policies().decrement(*this);
822 #else
823         policies().decrement<self>(*this);
824 #endif
825         return *this;
826     }
827
828     self operator--(int) { self tmp(*this); --*this; return tmp; }
829
830     self& operator+=(difference_type n) {
831         policies().advance(*this, n);
832         return *this;
833     }
834
835     self& operator-=(difference_type n) {
836         policies().advance(*this, -n);
837         return *this;
838     }
839
840     base_type const& base() const { return m_iter_p.first(); }
841
842     // Moved from global scope to avoid ambiguity with the operator-() which
843     // subtracts iterators from one another.
844     self operator-(difference_type x) const
845         { self result(*this); return result -= x; }
846 private:
847     compressed_pair<Base,Policies> m_iter_p;
848
849 public: // implementation details (too many compilers have trouble when these are private).
850     base_type& base() { return m_iter_p.first(); }
851     Policies& policies() { return m_iter_p.second(); }
852     const Policies& policies() const { return m_iter_p.second(); }
853 };
854
855 template <class Base, class Policies, class Value, class Reference, class Pointer,
856     class Category, class Distance1, class Distance2>
857 iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance1>
858 operator+(
859     iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance1> p,
860     Distance2 x)
861 {
862     return p += x;
863 }
864
865 template <class Base, class Policies, class Value, class Reference, class Pointer,
866     class Category, class Distance1, class Distance2>
867 iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance1>
868 operator+(
869     Distance2 x,
870     iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance1> p)
871 {
872     return p += x;
873 }
874
875 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
876     class Reference1, class Reference2, class Pointer1, class Pointer2, class Category,
877     class Distance>
878 typename iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>::difference_type
879 operator-(
880     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
881     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
882 {
883   typedef typename iterator_adaptor<Iterator1,Policies,Value1,Reference1,
884     Pointer1,Category,Distance>::difference_type difference_type;
885   return x.policies().distance(y, x);
886 }
887
888 #ifndef BOOST_RELOPS_AMBIGUITY_BUG
889 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
890     class Reference1, class Reference2, class Pointer1, class Pointer2,
891     class Category, class Distance>
892 inline bool
893 operator==(
894     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
895     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
896 {
897     return x.policies().equal(x, y);
898 }
899
900 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
901     class Reference1, class Reference2, class Pointer1, class Pointer2,
902     class Category, class Distance>
903 inline bool
904 operator<(
905     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
906     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
907 {
908     return x.policies().distance(y, x) < 0;
909 }
910
911 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
912     class Reference1, class Reference2, class Pointer1, class Pointer2,
913     class Category, class Distance>
914 inline bool
915 operator>(
916     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
917     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
918 {
919     return x.policies().distance(y, x) > 0;
920 }
921
922 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
923     class Reference1, class Reference2, class Pointer1, class Pointer2,
924     class Category, class Distance>
925 inline bool
926 operator>=(
927     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
928     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
929 {
930     return x.policies().distance(y, x) >= 0;
931 }
932
933 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
934     class Reference1, class Reference2, class Pointer1, class Pointer2,
935     class Category, class Distance>
936 inline bool
937 operator<=(
938     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
939     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
940 {
941     return x.policies().distance(y, x) <= 0;
942 }
943
944 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
945     class Reference1, class Reference2, class Pointer1, class Pointer2,
946     class Category, class Distance>
947 inline bool
948 operator!=(
949     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
950     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
951 {
952     return !x.policies().equal(x, y);
953 }
954 #endif
955
956
957 } // namespace multi_array
958 } // namespace detail
959 } // namespace boost
960 # undef BOOST_ARG_DEPENDENT_TYPENAME
961
962
963 #endif
964
965
966