]> git.lyx.org Git - lyx.git/blob - boost/boost/iterator_adaptors.hpp
update boost
[lyx.git] / boost / boost / 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 // 01 Feb 2002   Jeremy Siek
16 //      Added more comments in default_iterator_policies.
17 // 08 Jan 2001   David Abrahams
18 //      Moved concept checks into a separate class, which makes MSVC
19 //      better at dealing with them.
20 // 07 Jan 2001   David Abrahams
21 //      Choose proxy for operator->() only if the reference type is not a reference.
22 //      Updated workarounds for __MWERKS__ == 0x2406
23 // 20 Dec 2001   David Abrahams
24 //      Adjusted is_convertible workarounds for __MWERKS__ == 0x2406
25 // 03 Nov 2001   Jeremy Siek
26 //      Changed the named template parameter interface and internal.
27 // 04 Oct 2001   Jeremy Siek
28 //      Changed projection_iterator to not rely on the default reference,
29 //      working around a limitation of detail::iterator_traits.
30 // 04 Oct 2001   David Abrahams
31 //      Applied indirect_iterator patch from George A. Heintzelman <georgeh@aya.yale.edu>
32 //      Changed name of "bind" to "select" to avoid problems with MSVC.
33 // 26 Sep 2001   David Abrahams
34 //      Added borland bug fix
35 // 08 Mar 2001   Jeremy Siek
36 //      Added support for optional named template parameters.
37 // 19 Feb 2001   David Abrahams
38 //      Rolled back reverse_iterator_pair_generator again, as it doesn't
39 //      save typing on a conforming compiler.
40 // 18 Feb 2001   David Abrahams
41 //      Reinstated reverse_iterator_pair_generator
42 // 16 Feb 2001   David Abrahams
43 //      Add an implicit conversion operator to operator_arrow_proxy
44 //      as CW and BCC workarounds.
45 // 11 Feb 2001   David Abrahams
46 //      Switch to use of BOOST_STATIC_CONSTANT where possible
47 // 11 Feb 2001   Jeremy Siek
48 //      Removed workaround for older MIPSpro compiler. The workaround
49 //        was preventing the proper functionality of the underlying
50 //        iterator being carried forward into the iterator adaptor.
51 //        Also added is_bidirectional enum to avoid EDG compiler error.
52 // 11 Feb 2001   David Abrahams
53 //      Borland fixes up the wazoo. It finally works!
54 // 10 Feb 2001   David Abrahams
55 //      Removed traits argument from iterator_adaptor<> and switched to
56 //        explicit trait specification for maximum ease-of-use.
57 //      Added comments to detail::iterator_defaults<>
58 //      Began using detail::iterator_defaults<> unconditionally for code clarity
59 //      Changed uses of `Iterator' to `Base' where non-iterators can be used.
60 //
61 // 10 Feb 2001   David Abrahams
62 //      Rolled in supposed Borland fixes from John Maddock, but not seeing any
63 //        improvement yet
64 //      Changed argument order to indirect_ generator, for convenience in the
65 //        case of input iterators (where Reference must be a value type).
66 //      Removed derivation of filter_iterator_policies from
67 //        default_iterator_policies, since the iterator category is likely to be
68 //        reduced (we don't want to allow illegal operations like decrement).
69 //      Support for a simpler filter iterator interface.
70 //
71 // 09 Feb 2001   David Abrahams
72 //      Improved interface to indirect_ and reverse_ iterators
73 //      Rolled back Jeremy's new constructor for now; it was causing
74 //        problems with counting_iterator_test
75 //      Attempted fix for Borland
76 //
77 // 09 Feb 2001   Jeremy Siek
78 //      Added iterator constructor to allow const adaptor
79 //        from non-const adaptee.
80 //      Changed make_xxx to pass iterators by-value to
81 //        get arrays converted to pointers.
82 //      Removed InnerIterator template parameter from
83 //        indirect_iterator_generator.
84 //      Rearranged parameters for make_filter_iterator
85 //
86 // 07 Feb 2001   Jeremy Siek
87 //      Removed some const iterator adaptor generators.
88 //      Added make_xxx_iterator() helper functions for remaining
89 //        iterator adaptors.
90 //      Removed some traits template parameters where they
91 //        where no longer needed thanks to detail::iterator_traits.
92 //      Moved some of the compile-time logic into enums for
93 //      EDG compatibility.
94 //
95 // 07 Feb 2001  David Abrahams
96 //      Removed iterator_adaptor_pair_generator and
97 //        reverse_iterator_pair_generator (more such culling to come)
98 //      Improved comments
99 //      Changed all uses of std::iterator_traits as default arguments
100 //        to boost::detail::iterator_traits for improved utility in
101 //        non-generic contexts
102 //      Fixed naming convention of non-template parameter names
103 //
104 // 06 Feb 2001   David Abrahams
105 //      Produce operator-> proxy objects for InputIterators
106 //      Added static assertions to do some basic concept checks
107 //      Renamed single-type generators -> xxx_generator
108 //      Renamed const/nonconst iterator generators -> xxx_pair_generator
109 //      Added make_transform_iterator(iter, function)
110 //      The existence of boost::detail::iterator_traits allowed many
111 //        template arguments to be defaulted. Some arguments had to be
112 //        moved to accomplish it.
113 //
114 // 04 Feb 2001  MWERKS bug workaround, concept checking for proper
115 //              reference types (David Abrahams)
116
117 #ifndef BOOST_ITERATOR_ADAPTOR_DWA053000_HPP_
118 # define BOOST_ITERATOR_ADAPTOR_DWA053000_HPP_
119
120 # include <boost/iterator.hpp>
121 # include <boost/utility.hpp>
122 # include <boost/compressed_pair.hpp>
123 # include <boost/concept_check.hpp>
124 # include <boost/type.hpp>
125 # include <boost/static_assert.hpp>
126 # include <boost/type_traits.hpp>
127 # include <boost/detail/iterator.hpp>
128 # include <boost/detail/select_type.hpp>
129
130 // I was having some problems with VC6. I couldn't tell whether our hack for
131 // stock GCC was causing problems so I needed an easy way to turn it on and
132 // off. Now we can test the hack with various compilers and still have an
133 // "out" if it doesn't work. -dwa 7/31/00
134 # if __GNUC__ == 2 && __GNUC_MINOR__ <= 96 && !defined(__STL_USE_NAMESPACES)
135 #  define BOOST_RELOPS_AMBIGUITY_BUG 1
136 # endif
137
138 namespace boost {
139
140 //============================================================================
141 // Concept checking classes that express the requirements for iterator
142 // policies and adapted types. These classes are mostly for
143 // documentation purposes, and are not used in this header file. They
144 // merely provide a more succinct statement of what is expected of the
145 // iterator policies.
146
147 template <class Policies, class Adapted, class Traits>
148 struct TrivialIteratorPoliciesConcept
149 {
150   typedef typename Traits::reference reference;
151   void constraints() {
152     function_requires< AssignableConcept<Policies> >();
153     function_requires< DefaultConstructibleConcept<Policies> >();
154     function_requires< AssignableConcept<Adapted> >();
155     function_requires< DefaultConstructibleConcept<Adapted> >();
156
157     const_constraints();
158   }
159   void const_constraints() const {
160     reference r = p.dereference(x);
161     b = p.equal(x, x);
162     ignore_unused_variable_warning(r);
163   }
164   Policies p;
165   Adapted x;
166   mutable bool b;
167 };
168
169 // Add InputIteratorPoliciesConcept?
170
171 template <class Policies, class Adapted, class Traits>
172 struct ForwardIteratorPoliciesConcept
173 {
174   typedef typename Traits::iterator_category iterator_category;
175   void constraints() {
176     function_requires<
177       TrivialIteratorPoliciesConcept<Policies, Adapted, Traits>
178       >();
179
180     p.increment(x);
181     std::forward_iterator_tag t = iterator_category();
182     ignore_unused_variable_warning(t);
183   }
184   Policies p;
185   Adapted x;
186   iterator_category category;
187 };
188
189 template <class Policies, class Adapted, class Traits>
190 struct BidirectionalIteratorPoliciesConcept
191 {
192   typedef typename Traits::iterator_category iterator_category;
193   void constraints() {
194     function_requires<
195       ForwardIteratorPoliciesConcept<Policies, Adapted, Traits>
196       >();
197
198     p.decrement(x);
199     std::bidirectional_iterator_tag t = iterator_category();
200     ignore_unused_variable_warning(t);
201   }
202   Policies p;
203   Adapted x;
204 };
205
206 template <class Policies, class Adapted, class Traits>
207 struct RandomAccessIteratorPoliciesConcept
208 {
209   typedef typename Traits::difference_type DifferenceType;
210   typedef typename Traits::iterator_category iterator_category;
211   void constraints() {
212     function_requires<
213       BidirectionalIteratorPoliciesConcept<Policies, Adapted, Traits>
214       >();
215
216     p.advance(x, n);
217     std::random_access_iterator_tag t = iterator_category();
218     const_constraints();
219     ignore_unused_variable_warning(t);
220   }
221   void const_constraints() const {
222     n = p.distance(x, x);
223   }
224   Policies p;
225   Adapted x;
226   mutable DifferenceType n;
227   mutable bool b;
228 };
229
230
231 //============================================================================
232 // Default policies for iterator adaptors. You can use this as a base
233 // class if you want to customize particular policies.
234 struct default_iterator_policies
235 {
236     // Some of the member functions were defined static, but Borland
237     // got confused and thought they were non-const. Also, Sun C++
238     // does not like static function templates. 
239     //
240     // The reason some members were defined static is because there is
241     // not state (data members) needed by those members of the
242     // default_iterator_policies class. If your policies class member
243     // functions need to access state stored in the policies object,
244     // then the member functions should not be static (they can't be).
245
246     template <class Base>
247     void initialize(Base&)
248         { }
249
250     template <class IteratorAdaptor>
251     typename IteratorAdaptor::reference dereference(const IteratorAdaptor& x) const
252         { return *x.base(); }
253
254     template <class IteratorAdaptor>
255     void increment(IteratorAdaptor& x)
256         { ++x.base(); }
257
258     template <class IteratorAdaptor>
259     void decrement(IteratorAdaptor& x)
260         { --x.base(); }
261
262     template <class IteratorAdaptor, class DifferenceType>
263     void advance(IteratorAdaptor& x, DifferenceType n)
264         { x.base() += n; }
265
266     template <class IteratorAdaptor1, class IteratorAdaptor2>
267     typename IteratorAdaptor1::difference_type
268     distance(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const
269         { return y.base() - x.base(); }
270
271     template <class IteratorAdaptor1, class IteratorAdaptor2>
272     bool equal(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const
273         { return x.base() == y.base(); }
274 };
275
276 // putting the comparisons in a base class avoids the g++
277 // ambiguous overload bug due to the relops operators
278
279 #ifdef BOOST_RELOPS_AMBIGUITY_BUG
280 template <class Derived, class Base>
281 struct iterator_comparisons : Base { };
282
283 template <class D1, class D2, class Base1, class Base2>
284 inline bool operator==(const iterator_comparisons<D1,Base1>& xb,
285                        const iterator_comparisons<D2,Base2>& yb)
286 {
287         const D1& x = static_cast<const D1&>(xb);
288     const D2& y = static_cast<const D2&>(yb);
289     return x.policies().equal(x, y);
290 }
291
292 template <class D1, class D2, class Base1, class Base2>
293 inline bool operator!=(const iterator_comparisons<D1,Base1>& xb,
294                        const iterator_comparisons<D2,Base2>& yb)
295 {
296     const D1& x = static_cast<const D1&>(xb);
297     const D2& y = static_cast<const D2&>(yb);
298     return !x.policies().equal(x, y);
299 }
300
301 template <class D1, class D2, class Base1, class Base2>
302 inline bool operator<(const iterator_comparisons<D1,Base1>& xb,
303                       const iterator_comparisons<D2,Base2>& yb)
304 {
305     const D1& x = static_cast<const D1&>(xb);
306     const D2& y = static_cast<const D2&>(yb);
307     return x.policies().distance(y, x) < 0;
308 }
309
310 template <class D1, class D2, class Base1, class Base2>
311 inline bool operator>(const iterator_comparisons<D1,Base1>& xb,
312                       const iterator_comparisons<D2,Base2>& yb)
313 {
314     const D1& x = static_cast<const D1&>(xb);
315     const D2& y = static_cast<const D2&>(yb);
316     return x.policies().distance(y, x) > 0;
317 }
318
319 template <class D1, class D2, class Base1, class Base2>
320 inline bool operator>=(const iterator_comparisons<D1,Base1>& xb,
321                        const iterator_comparisons<D2,Base2>& yb)
322 {
323     const D1& x = static_cast<const D1&>(xb);
324     const D2& y = static_cast<const D2&>(yb);
325     return x.policies().distance(y, x) >= 0;
326 }
327
328 template <class D1, class D2, class Base1, class Base2>
329 inline bool operator<=(const iterator_comparisons<D1,Base1>& xb,
330                        const iterator_comparisons<D2,Base2>& yb)
331 {
332     const D1& x = static_cast<const D1&>(xb);
333     const D2& y = static_cast<const D2&>(yb);
334     return x.policies().distance(y, x) <= 0;
335 }
336 #endif
337
338 namespace detail {
339
340   // operator->() needs special support for input iterators to strictly meet the
341   // standard's requirements. If *i is not a reference type, we must still
342   // produce a (constant) lvalue to which a pointer can be formed. We do that by
343   // returning an instantiation of this special proxy class template.
344
345   template <class T>
346   struct operator_arrow_proxy
347   {
348       operator_arrow_proxy(const T& x) : m_value(x) {}
349       const T* operator->() const { return &m_value; }
350       // This function is needed for MWCW and BCC, which won't call operator->
351       // again automatically per 13.3.1.2 para 8
352       operator const T*() const { return &m_value; }
353       T m_value;
354   };
355
356   template <class Iter>
357   inline operator_arrow_proxy<typename Iter::value_type>
358   operator_arrow(const Iter& i, std::input_iterator_tag) {
359     typedef typename Iter::value_type value_t; // VC++ needs this typedef
360     return operator_arrow_proxy<value_t>(*i);
361   }
362
363   template <class Iter>
364   inline typename Iter::pointer
365   operator_arrow(const Iter& i, std::forward_iterator_tag) {
366     return &(*i);
367   }
368
369   template <class Value, class Reference, class Pointer>
370   struct operator_arrow_result_generator
371   {
372       typedef operator_arrow_proxy<Value> proxy;
373       // Borland chokes unless it's an actual enum (!)
374       enum { use_proxy = !boost::is_reference<Reference>::value };
375
376       typedef typename boost::detail::if_true<(use_proxy)>::template
377       then<
378         proxy,
379    // else
380         Pointer
381       >::type type;
382   };
383
384
385 # ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
386
387    // Select default pointer and reference types for adapted non-pointer
388    // iterators based on the iterator and the value_type. Poor man's partial
389    // specialization is in use here.
390    template <bool is_pointer>
391    struct iterator_defaults_select
392    {
393        template <class Iterator,class Value>
394        struct traits
395        {
396            // The assumption is that iterator_traits can deduce these types
397            // properly as long as the iterator is not a pointer.
398            typedef typename boost::detail::iterator_traits<Iterator>::pointer pointer;
399            typedef typename boost::detail::iterator_traits<Iterator>::reference reference;
400        };
401    };
402
403    // Select default pointer and reference types for adapted pointer iterators
404    // given a (possibly-const) value_type.
405    template <>
406    struct iterator_defaults_select<true>
407    {
408        template <class Iterator,class Value>
409        struct traits
410        {
411            typedef Value* pointer;
412            typedef Value& reference;
413        };
414    };
415
416    // Consolidate selection of the default pointer and reference type
417    template <class Iterator,class Value>
418    struct iterator_defaults
419    {
420        BOOST_STATIC_CONSTANT(bool, is_ptr = boost::is_pointer<Iterator>::value);
421
422        typedef typename iterator_defaults_select<is_ptr>::template traits<Iterator,Value> traits;
423        typedef typename traits::pointer pointer;
424        typedef typename traits::reference reference;
425    };
426 # else
427    template <class Iterator,class Value>
428    struct iterator_defaults : iterator_traits<Iterator>
429    {
430        // Trying to factor the common is_same expression into an enum or a
431        // static bool constant confused Borland.
432        typedef typename if_true<(
433                ::boost::is_same<Value,typename iterator_traits<Iterator>::value_type>::value
434            )>::template then<
435                 typename iterator_traits<Iterator>::pointer,
436                 Value*
437        >::type pointer;
438
439        typedef typename if_true<(
440                ::boost::is_same<Value,typename iterator_traits<Iterator>::value_type>::value
441            )>::template then<
442                 typename iterator_traits<Iterator>::reference,
443                 Value&
444        >::type reference;
445
446    };
447 # endif
448
449   //===========================================================================
450   // Specify the defaults for iterator_adaptor's template parameters
451
452   struct default_argument { };
453   // This class template is a workaround for MSVC.
454   struct dummy_default_gen {
455     template <class Base, class Traits>
456     struct select { typedef default_argument type; };
457   };
458   // This class template is a workaround for MSVC.
459   template <class Gen> struct default_generator {
460     typedef dummy_default_gen type;
461   };
462
463   struct default_value_type {
464     template <class Base, class Traits>
465     struct select {
466       typedef typename boost::detail::iterator_traits<Base>::value_type type;
467     };
468   };
469   template <> struct default_generator<default_value_type>
470   { typedef default_value_type type; }; // VC++ workaround
471
472   struct default_difference_type {
473     template <class Base, class Traits>
474     struct select {
475       typedef typename boost::detail::iterator_traits<Base>::difference_type type;
476     };
477   };
478   template <> struct default_generator<default_difference_type>
479   { typedef default_difference_type type; }; // VC++ workaround
480
481   struct default_iterator_category {
482     template <class Base, class Traits>
483     struct select {
484       typedef typename boost::detail::iterator_traits<Base>::iterator_category type;
485     };
486   };
487   template <> struct default_generator<default_iterator_category>
488   { typedef default_iterator_category type; }; // VC++ workaround
489
490   struct default_pointer {
491     template <class Base, class Traits>
492     struct select {
493       typedef typename Traits::value_type Value;
494       typedef typename boost::detail::iterator_defaults<Base,Value>::pointer
495         type;
496     };
497   };
498   template <> struct default_generator<default_pointer>
499   { typedef default_pointer type; }; // VC++ workaround
500
501   struct default_reference {
502     template <class Base, class Traits>
503     struct select {
504       typedef typename Traits::value_type Value;
505       typedef typename boost::detail::iterator_defaults<Base,Value>::reference
506         type;
507     };
508   };
509   template <> struct default_generator<default_reference>
510   { typedef default_reference type; }; // VC++ workaround
511
512 } // namespace detail
513
514
515   //===========================================================================
516   // Support for named template parameters
517
518 struct named_template_param_base { };
519
520 namespace detail {
521   struct value_type_tag { };
522   struct reference_tag { };
523   struct pointer_tag { };
524   struct difference_type_tag { };
525   struct iterator_category_tag { };
526
527   // avoid using std::pair because A or B might be a reference type, and g++
528   // complains about forming references to references inside std::pair
529   template <class A, class B>
530   struct cons_type {
531     typedef A first_type;
532     typedef B second_type;
533   };
534
535 } // namespace detail
536
537 template <class Value> struct value_type_is : public named_template_param_base
538 {
539   typedef detail::cons_type<detail::value_type_tag, Value> type;
540 };
541 template <class Reference> struct reference_is : public named_template_param_base
542 {
543   typedef detail::cons_type<detail::reference_tag, Reference> type;
544 };
545 template <class Pointer> struct pointer_is : public named_template_param_base
546 {
547   typedef detail::cons_type<detail::pointer_tag, Pointer> type;
548 };
549 template <class Difference> struct difference_type_is
550   : public named_template_param_base
551 {
552   typedef detail::cons_type<detail::difference_type_tag, Difference> type;
553 };
554 template <class IteratorCategory> struct iterator_category_is
555   : public named_template_param_base
556 {
557   typedef detail::cons_type<detail::iterator_category_tag, IteratorCategory> type;
558 };
559
560 namespace detail {
561
562   struct end_of_list { };
563
564   // Given an associative list, find the value with the matching key.
565   // An associative list is a list of key-value pairs. The list is
566   // built out of cons_type's and is terminated by end_of_list.
567
568 # if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) || defined(__BORLANDC__)
569   template <class AssocList, class Key>
570   struct find_param;
571
572   struct find_param_continue {
573     template <class AssocList, class Key2> struct select {
574       typedef typename AssocList::first_type Head;
575       typedef typename Head::first_type Key1;
576       typedef typename Head::second_type Value;
577       typedef typename if_true<(is_same<Key1, Key2>::value)>::template
578       then<Value,
579         typename find_param<typename AssocList::second_type, Key2>::type
580       >::type type;
581     };
582   };
583   struct find_param_end {
584     template <class AssocList, class Key>
585     struct select { typedef detail::default_argument type; };
586   };
587   template <class AssocList> struct find_param_helper1
588   { typedef find_param_continue type; };
589   template <> struct find_param_helper1<end_of_list>
590   { typedef find_param_end type; };
591
592   template <class AssocList, class Key>
593   struct find_param {
594     typedef typename find_param_helper1<AssocList>::type select1;
595     typedef typename select1::template select<AssocList, Key>::type type;
596   };
597 # else
598   template <class AssocList, class Key> struct find_param;
599
600   template <class Key>
601   struct find_param<end_of_list, Key> { typedef default_argument type; };
602
603   // Found a matching Key, return the associated Value
604   template <class Key, class Value, class Rest>
605   struct find_param<detail::cons_type< detail::cons_type<Key, Value>, Rest>, Key> {
606     typedef Value type;
607   };
608
609   // Non-matching keys, continue the search
610   template <class Key1, class Value, class Rest, class Key2>
611   struct find_param<detail::cons_type< detail::cons_type<Key1, Value>, Rest>, Key2> {
612     typedef typename find_param<Rest, Key2>::type type;
613   };
614 # endif
615
616   struct make_named_arg {
617     template <class Key, class Value>
618     struct select { typedef typename Value::type type; };
619   };
620   struct make_key_value {
621     template <class Key, class Value>
622     struct select { typedef detail::cons_type<Key, Value> type; };
623   };
624
625   template <class Value>
626   struct is_named_parameter
627   {
628       enum { value = is_convertible< typename add_reference< Value >::type, add_reference< named_template_param_base >::type >::value };
629   };
630
631 # if defined(__MWERKS__) && __MWERKS__ <= 0x2406 // workaround for broken is_convertible implementation
632   template <class T> struct is_named_parameter<value_type_is<T> > { enum { value = true }; };
633   template <class T> struct is_named_parameter<reference_is<T> > { enum { value = true }; };
634   template <class T> struct is_named_parameter<pointer_is<T> > { enum { value = true }; };
635   template <class T> struct is_named_parameter<difference_type_is<T> > { enum { value = true }; };
636   template <class T> struct is_named_parameter<iterator_category_is<T> > { enum { value = true }; };
637 # endif
638
639   template <class Key, class Value>
640   struct make_arg {
641 # ifdef __BORLANDC__
642     // Borland C++ doesn't like the extra indirection of is_named_parameter
643     typedef typename
644       if_true<(is_convertible<Value,named_template_param_base>::value)>::
645       template then<make_named_arg, make_key_value>::type Make;
646 # else
647     enum { is_named = is_named_parameter<Value>::value };
648     typedef typename if_true<(is_named)>::template
649       then<make_named_arg, make_key_value>::type Make;
650 # endif
651     typedef typename Make::template select<Key, Value>::type type;
652   };
653
654   // Mechanism for resolving the default argument for a template parameter.
655
656   template <class T> struct is_default { typedef type_traits::no_type type; };
657   template <> struct is_default<default_argument>
658   { typedef type_traits::yes_type type; };
659
660   struct choose_default {
661     template <class Arg, class DefaultGen, class Base, class Traits>
662     struct select {
663       typedef typename default_generator<DefaultGen>::type Gen;
664       typedef typename Gen::template select<Base,Traits>::type type;
665     };
666   };
667   struct choose_arg {
668     template <class Arg, class DefaultGen, class Base, class Traits>
669     struct select {
670       typedef Arg type;
671     };
672   };
673
674   template <class UseDefault>
675   struct choose_arg_or_default { typedef choose_arg type; };
676   template <> struct choose_arg_or_default<type_traits::yes_type> {
677     typedef choose_default type;
678   };
679
680   template <class Arg, class DefaultGen, class Base, class Traits>
681   class resolve_default {
682     typedef typename choose_arg_or_default<typename is_default<Arg>::type>::type
683       Selector;
684   public:
685     typedef typename Selector
686       ::template select<Arg, DefaultGen, Base, Traits>::type type;
687   };
688
689   template <class Base, class Value, class Reference, class Pointer,
690             class Category, class Distance>
691   class iterator_adaptor_traits_gen
692   {
693     // Form an associative list out of the template parameters
694     // If the argument is a normal parameter (not named) then make_arg
695     // creates a key-value pair. If the argument is a named parameter,
696     // then make_arg extracts the key-value pair defined inside the
697     // named parameter.
698     typedef detail::cons_type< typename make_arg<value_type_tag, Value>::type,
699       detail::cons_type<typename make_arg<reference_tag, Reference>::type,
700       detail::cons_type<typename make_arg<pointer_tag, Pointer>::type,
701       detail::cons_type<typename make_arg<iterator_category_tag, Category>::type,
702       detail::cons_type<typename make_arg<difference_type_tag, Distance>::type,
703                 end_of_list> > > > > ArgList;
704
705     // Search the list for particular parameters
706     typedef typename find_param<ArgList, value_type_tag>::type Val;
707     typedef typename find_param<ArgList, difference_type_tag>::type Diff;
708     typedef typename find_param<ArgList, iterator_category_tag>::type Cat;
709     typedef typename find_param<ArgList, pointer_tag>::type Ptr;
710     typedef typename find_param<ArgList, reference_tag>::type Ref;
711
712     typedef boost::iterator<Category, Value, Distance, Pointer, Reference>
713       Traits0;
714
715     // Compute the defaults if necessary
716     typedef typename resolve_default<Val, default_value_type, Base, Traits0>::type
717       value_type;
718     // if getting default value type from iterator_traits, then it won't be const
719     typedef typename resolve_default<Diff, default_difference_type, Base,
720       Traits0>::type difference_type;
721     typedef typename resolve_default<Cat, default_iterator_category, Base,
722       Traits0>::type iterator_category;
723
724     typedef boost::iterator<iterator_category, value_type, difference_type,
725       Pointer, Reference> Traits1;
726
727     // Compute the defaults for pointer and reference. This is done as a
728     // separate step because the defaults for pointer and reference depend
729     // on value_type.
730     typedef typename resolve_default<Ptr, default_pointer, Base, Traits1>::type
731       pointer;
732     typedef typename resolve_default<Ref, default_reference, Base, Traits1>::type
733       reference;
734
735   public:
736     typedef boost::iterator<iterator_category,
737       typename remove_const<value_type>::type,
738       difference_type, pointer, reference> type;
739   };
740
741   // This is really a partial concept check for iterators. Should it
742   // be moved or done differently?
743   template <class Category, class Value, class Difference, class Pointer, class Reference>
744   struct validator
745   {
746       BOOST_STATIC_CONSTANT(
747           bool, is_input_or_output_iter
748           = (boost::is_convertible<Category*,std::input_iterator_tag*>::value
749              | boost::is_convertible<Category*,std::output_iterator_tag*>::value));
750
751       // Iterators should satisfy one of the known categories
752       BOOST_STATIC_ASSERT(is_input_or_output_iter);
753
754       // Iterators >= ForwardIterator must produce real references
755       // as required by the C++ standard requirements in Table 74.
756       BOOST_STATIC_CONSTANT(
757           bool, forward_iter_with_real_reference
758           = ((!boost::is_convertible<Category*,std::forward_iterator_tag*>::value)
759              | boost::is_same<Reference,Value&>::value
760              | boost::is_same<Reference,typename add_const<Value>::type&>::value));
761
762       BOOST_STATIC_ASSERT(forward_iter_with_real_reference);
763   };
764 } // namespace detail
765
766
767
768 // This macro definition is only temporary in this file
769 # if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
770 #  define BOOST_ARG_DEPENDENT_TYPENAME typename
771 # else
772 #  define BOOST_ARG_DEPENDENT_TYPENAME
773 # endif
774
775 //============================================================================
776 //iterator_adaptor - Adapts a generic piece of data as an iterator. Adaptation
777 //      is especially easy if the data being adapted is itself an iterator
778 //
779 //   Base - the base (usually iterator) type being wrapped.
780 //
781 //   Policies - a set of policies determining how the resulting iterator
782 //      works.
783 //
784 //   Value - if supplied, the value_type of the resulting iterator, unless
785 //      const. If const, a conforming compiler strips constness for the
786 //      value_type. If not supplied, iterator_traits<Base>::value_type is used
787 //
788 //   Reference - the reference type of the resulting iterator, and in
789 //      particular, the result type of operator*(). If not supplied but
790 //      Value is supplied, Value& is used. Otherwise
791 //      iterator_traits<Base>::reference is used.
792 //
793 //   Pointer - the pointer type of the resulting iterator, and in
794 //      particular, the result type of operator->(). If not
795 //      supplied but Value is supplied, Value* is used. Otherwise
796 //      iterator_traits<Base>::pointer is used.
797 //
798 //   Category - the iterator_category of the resulting iterator. If not
799 //      supplied, iterator_traits<Base>::iterator_category is used.
800 //
801 //   Distance - the difference_type of the resulting iterator. If not
802 //      supplied, iterator_traits<Base>::difference_type is used.
803 template <class Base, class Policies,
804     class Value = ::boost::detail::default_argument,
805     class Reference = ::boost::detail::default_argument,
806     class Pointer = ::boost::detail::default_argument,
807     class Category = ::boost::detail::default_argument,
808     class Distance = ::boost::detail::default_argument
809          >
810 struct iterator_adaptor :
811 #ifdef BOOST_RELOPS_AMBIGUITY_BUG
812     iterator_comparisons<
813           iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance>,
814     typename detail::iterator_adaptor_traits_gen<Base,Value,Reference,Pointer,Category, Distance>::type
815  >
816 #else
817     detail::iterator_adaptor_traits_gen<Base,Value,Reference,Pointer,Category,Distance>::type
818 #endif
819 {
820     typedef iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance> self;
821  public:
822     typedef detail::iterator_adaptor_traits_gen<Base,Value,Reference,Pointer,Category,Distance> TraitsGen;
823     typedef typename TraitsGen::type Traits;
824
825     typedef typename Traits::difference_type difference_type;
826     typedef typename Traits::value_type value_type;
827     typedef typename Traits::pointer pointer;
828     typedef typename Traits::reference reference;
829     typedef typename Traits::iterator_category iterator_category;
830
831     typedef Base base_type;
832     typedef Policies policies_type;
833
834  private:
835     typedef detail::validator<
836         iterator_category,value_type,difference_type,pointer,reference
837         > concept_check;
838
839  public:
840     iterator_adaptor()
841     {
842     }
843
844     explicit
845     iterator_adaptor(const Base& it, const Policies& p = Policies())
846         : m_iter_p(it, p) {
847         policies().initialize(base());
848     }
849
850     template <class Iter2, class Value2, class Pointer2, class Reference2>
851     iterator_adaptor (
852         const iterator_adaptor<Iter2,Policies,Value2,Reference2,Pointer2,Category,Distance>& src)
853             : m_iter_p(src.base(), src.policies())
854     {
855         policies().initialize(base());
856     }
857
858 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(__BORLANDC__)
859     // This is required to prevent a bug in how VC++ generates
860     // the assignment operator for compressed_pair
861     iterator_adaptor& operator= (const iterator_adaptor& x) {
862         m_iter_p = x.m_iter_p;
863         return *this;
864     }
865 #endif
866     reference operator*() const {
867          return policies().dereference(*this);
868     }
869
870 #ifdef BOOST_MSVC
871 # pragma warning(push)
872 # pragma warning( disable : 4284 )
873 #endif
874
875     typename boost::detail::operator_arrow_result_generator<value_type,reference,pointer>::type
876     operator->() const
877         { return detail::operator_arrow(*this, iterator_category()); }
878
879 #ifdef BOOST_MSVC
880 # pragma warning(pop)
881 #endif
882
883     value_type operator[](difference_type n) const
884         { return *(*this + n); }
885
886     self& operator++() {
887 #if !defined(__MWERKS__) || __MWERKS__ >= 0x2405
888         policies().increment(*this);
889 #else
890         // Odd bug, MWERKS couldn't  deduce the type for the member template
891         // Workaround by explicitly specifying the type.
892         policies().increment<self>(*this);
893 #endif
894         return *this;
895     }
896
897     self operator++(int) { self tmp(*this); ++*this; return tmp; }
898
899     self& operator--() {
900 #if !defined(__MWERKS__) || __MWERKS__ >= 0x2405
901         policies().decrement(*this);
902 #else
903         policies().decrement<self>(*this);
904 #endif
905         return *this;
906     }
907
908     self operator--(int) { self tmp(*this); --*this; return tmp; }
909
910     self& operator+=(difference_type n) {
911         policies().advance(*this, n);
912         return *this;
913     }
914
915     self& operator-=(difference_type n) {
916         policies().advance(*this, -n);
917         return *this;
918     }
919
920     base_type const& base() const { return m_iter_p.first(); }
921
922     // Moved from global scope to avoid ambiguity with the operator-() which
923     // subtracts iterators from one another.
924     self operator-(difference_type x) const
925         { self result(*this); return result -= x; }
926 private:
927     compressed_pair<Base,Policies> m_iter_p;
928
929 public: // implementation details (too many compilers have trouble when these are private).
930     base_type& base() { return m_iter_p.first(); }
931     Policies& policies() { return m_iter_p.second(); }
932     const Policies& policies() const { return m_iter_p.second(); }
933 };
934
935 template <class Base, class Policies, class Value, class Reference, class Pointer,
936     class Category, class Distance1, class Distance2>
937 iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance1>
938 operator+(
939     iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance1> p,
940     Distance2 x)
941 {
942     return p += x;
943 }
944
945 template <class Base, class Policies, class Value, class Reference, class Pointer,
946     class Category, class Distance1, class Distance2>
947 iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance1>
948 operator+(
949     Distance2 x,
950     iterator_adaptor<Base,Policies,Value,Reference,Pointer,Category,Distance1> p)
951 {
952     return p += x;
953 }
954
955 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
956     class Reference1, class Reference2, class Pointer1, class Pointer2, class Category,
957     class Distance>
958 typename iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>::difference_type
959 operator-(
960     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
961     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
962 {
963   typedef typename iterator_adaptor<Iterator1,Policies,Value1,Reference1,
964     Pointer1,Category,Distance>::difference_type difference_type;
965   return x.policies().distance(y, x);
966 }
967
968 #ifndef BOOST_RELOPS_AMBIGUITY_BUG
969 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
970     class Reference1, class Reference2, class Pointer1, class Pointer2,
971     class Category, class Distance>
972 inline bool
973 operator==(
974     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
975     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
976 {
977     return x.policies().equal(x, y);
978 }
979
980 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
981     class Reference1, class Reference2, class Pointer1, class Pointer2,
982     class Category, class Distance>
983 inline bool
984 operator<(
985     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
986     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
987 {
988     return x.policies().distance(y, x) < 0;
989 }
990
991 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
992     class Reference1, class Reference2, class Pointer1, class Pointer2,
993     class Category, class Distance>
994 inline bool
995 operator>(
996     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
997     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
998 {
999     return x.policies().distance(y, x) > 0;
1000 }
1001
1002 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
1003     class Reference1, class Reference2, class Pointer1, class Pointer2,
1004     class Category, class Distance>
1005 inline bool
1006 operator>=(
1007     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
1008     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
1009 {
1010     return x.policies().distance(y, x) >= 0;
1011 }
1012
1013 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
1014     class Reference1, class Reference2, class Pointer1, class Pointer2,
1015     class Category, class Distance>
1016 inline bool
1017 operator<=(
1018     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
1019     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
1020 {
1021     return x.policies().distance(y, x) <= 0;
1022 }
1023
1024 template <class Iterator1, class Iterator2, class Policies, class Value1, class Value2,
1025     class Reference1, class Reference2, class Pointer1, class Pointer2,
1026     class Category, class Distance>
1027 inline bool
1028 operator!=(
1029     const iterator_adaptor<Iterator1,Policies,Value1,Reference1,Pointer1,Category,Distance>& x,
1030     const iterator_adaptor<Iterator2,Policies,Value2,Reference2,Pointer2,Category,Distance>& y)
1031 {
1032     return !x.policies().equal(x, y);
1033 }
1034 #endif
1035
1036 //=============================================================================
1037 // Transform Iterator Adaptor
1038 //
1039 // Upon deference, apply some unary function object and return the
1040 // result by value.
1041
1042 template <class AdaptableUnaryFunction>
1043 struct transform_iterator_policies : public default_iterator_policies
1044 {
1045     transform_iterator_policies() { }
1046     transform_iterator_policies(const AdaptableUnaryFunction& f) : m_f(f) { }
1047
1048     template <class IteratorAdaptor>
1049     typename IteratorAdaptor::reference
1050     dereference(const IteratorAdaptor& iter) const
1051         { return m_f(*iter.base()); }
1052
1053     AdaptableUnaryFunction m_f;
1054 };
1055
1056 template <class AdaptableUnaryFunction, class Iterator>
1057 class transform_iterator_generator
1058 {
1059     typedef typename AdaptableUnaryFunction::result_type value_type;
1060 public:
1061     typedef iterator_adaptor<Iterator,
1062       transform_iterator_policies<AdaptableUnaryFunction>,
1063         value_type, value_type, value_type*, std::input_iterator_tag>
1064       type;
1065 };
1066
1067 template <class AdaptableUnaryFunction, class Iterator>
1068 inline typename transform_iterator_generator<AdaptableUnaryFunction,Iterator>::type
1069 make_transform_iterator(
1070     Iterator base,
1071     const AdaptableUnaryFunction& f = AdaptableUnaryFunction())
1072 {
1073     typedef typename transform_iterator_generator<AdaptableUnaryFunction,Iterator>::type result_t;
1074     return result_t(base, f);
1075 }
1076
1077 //=============================================================================
1078 // Indirect Iterators Adaptor
1079
1080 // Given a pointer to pointers (or iterator to iterators),
1081 // apply a double dereference inside operator*().
1082 //
1083 // We use the term "outer" to refer to the first level iterator type
1084 // and "inner" to refer to the second level iterator type.  For
1085 // example, given T**, T* is the inner iterator type and T** is the
1086 // outer iterator type. Also, const T* would be the const inner
1087 // iterator.
1088
1089 // We tried to implement this with transform_iterator, but that required
1090 // using boost::remove_ref, which is not compiler portable.
1091
1092 struct indirect_iterator_policies : public default_iterator_policies
1093 {
1094     template <class IteratorAdaptor>
1095     typename IteratorAdaptor::reference dereference(const IteratorAdaptor& x) const
1096         { return **x.base(); }
1097 };
1098
1099 namespace detail {
1100 # if !defined(BOOST_MSVC) || BOOST_MSVC > 1300 // strangely instantiated even when unused! Maybe try a recursive template someday ;-)
1101   template <class T>
1102   struct traits_of_value_type {
1103       typedef typename boost::detail::iterator_traits<T>::value_type outer_value;
1104       typedef typename boost::detail::iterator_traits<outer_value>::value_type value_type;
1105       typedef typename boost::detail::iterator_traits<outer_value>::reference reference;
1106       typedef typename boost::detail::iterator_traits<outer_value>::pointer pointer;
1107   };
1108 # endif
1109 }
1110
1111 template <class OuterIterator,      // Mutable or Immutable, does not matter
1112           class Value
1113 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
1114                 = BOOST_ARG_DEPENDENT_TYPENAME detail::traits_of_value_type<
1115                         OuterIterator>::value_type
1116 #endif
1117           , class Reference
1118 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
1119                 = BOOST_ARG_DEPENDENT_TYPENAME detail::traits_of_value_type<
1120                         OuterIterator>::reference
1121 #else
1122                 = Value &
1123 #endif
1124           , class Category = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_traits<
1125                         OuterIterator>::iterator_category
1126           , class Pointer
1127 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
1128                 = BOOST_ARG_DEPENDENT_TYPENAME detail::traits_of_value_type<
1129                         OuterIterator>::pointer
1130 #else
1131                 = Value*
1132 #endif
1133          >
1134 struct indirect_iterator_generator
1135 {
1136     typedef iterator_adaptor<OuterIterator,
1137         indirect_iterator_policies,Value,Reference,Pointer,Category> type;
1138 };
1139
1140 template <class OuterIterator,      // Mutable or Immutable, does not matter
1141           class Value
1142 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
1143                 = BOOST_ARG_DEPENDENT_TYPENAME detail::traits_of_value_type<
1144                         OuterIterator>::value_type
1145 #endif
1146           , class Reference
1147 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
1148                 = BOOST_ARG_DEPENDENT_TYPENAME detail::traits_of_value_type<
1149                         OuterIterator>::reference
1150 #else
1151                 = Value &
1152 #endif
1153           , class ConstReference = const Value&
1154           , class Category = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_traits<
1155                 OuterIterator>::iterator_category
1156           , class Pointer
1157 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
1158                 = BOOST_ARG_DEPENDENT_TYPENAME detail::traits_of_value_type<
1159                         OuterIterator>::pointer
1160 #else
1161                 = Value*
1162 #endif
1163           , class ConstPointer = const Value*
1164            >
1165 struct indirect_iterator_pair_generator
1166 {
1167   typedef typename indirect_iterator_generator<OuterIterator,
1168     Value, Reference,Category,Pointer>::type iterator;
1169   typedef typename indirect_iterator_generator<OuterIterator,
1170     Value, ConstReference,Category,ConstPointer>::type const_iterator;
1171 };
1172
1173 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
1174 template <class OuterIterator>
1175 inline typename indirect_iterator_generator<OuterIterator>::type
1176 make_indirect_iterator(OuterIterator base)
1177 {
1178     typedef typename indirect_iterator_generator
1179         <OuterIterator>::type result_t;
1180     return result_t(base);
1181 }
1182 #endif
1183
1184 //=============================================================================
1185 // Reverse Iterators Adaptor
1186
1187 struct reverse_iterator_policies : public default_iterator_policies
1188 {
1189     template <class IteratorAdaptor>
1190     typename IteratorAdaptor::reference dereference(const IteratorAdaptor& x) const
1191         { return *boost::prior(x.base()); }
1192
1193     template <class BidirectionalIterator>
1194     void increment(BidirectionalIterator& x) const
1195         { --x.base(); }
1196
1197     template <class BidirectionalIterator>
1198     void decrement(BidirectionalIterator& x) const
1199         { ++x.base(); }
1200
1201     template <class BidirectionalIterator, class DifferenceType>
1202     void advance(BidirectionalIterator& x, DifferenceType n) const
1203         { x.base() -= n; }
1204
1205     template <class Iterator1, class Iterator2>
1206     typename Iterator1::difference_type distance(
1207         const Iterator1& x, const Iterator2& y) const
1208         { return x.base() - y.base(); }
1209
1210     template <class Iterator1, class Iterator2>
1211     bool equal(const Iterator1& x, const Iterator2& y) const
1212         { return x.base() == y.base(); }
1213 };
1214
1215 template <class BidirectionalIterator,
1216     class Value = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_traits<BidirectionalIterator>::value_type,
1217     class Reference = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_defaults<BidirectionalIterator,Value>::reference,
1218     class Pointer = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_defaults<BidirectionalIterator,Value>::pointer,
1219     class Category = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_traits<BidirectionalIterator>::iterator_category,
1220     class Distance = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_traits<BidirectionalIterator>::difference_type
1221          >
1222 struct reverse_iterator_generator
1223 {
1224     typedef iterator_adaptor<BidirectionalIterator,reverse_iterator_policies,
1225         Value,Reference,Pointer,Category,Distance> type;
1226 };
1227
1228 template <class BidirectionalIterator>
1229 inline typename reverse_iterator_generator<BidirectionalIterator>::type
1230 make_reverse_iterator(BidirectionalIterator base)
1231 {
1232     typedef typename reverse_iterator_generator<BidirectionalIterator>::type result_t;
1233     return result_t(base);
1234 }
1235
1236 //=============================================================================
1237 // Projection Iterators Adaptor
1238
1239 template <class AdaptableUnaryFunction>
1240 struct projection_iterator_policies : public default_iterator_policies
1241 {
1242     projection_iterator_policies() { }
1243     projection_iterator_policies(const AdaptableUnaryFunction& f) : m_f(f) { }
1244
1245     template <class IteratorAdaptor>
1246     typename IteratorAdaptor::reference dereference(IteratorAdaptor const& iter) const {
1247         return m_f(*iter.base());
1248     }
1249
1250     AdaptableUnaryFunction m_f;
1251 };
1252
1253 template <class AdaptableUnaryFunction, class Iterator>
1254 class projection_iterator_generator {
1255     typedef typename AdaptableUnaryFunction::result_type value_type;
1256     typedef projection_iterator_policies<AdaptableUnaryFunction> policies;
1257 public:
1258     typedef iterator_adaptor<Iterator,policies,value_type,value_type&,value_type*> type;
1259 };
1260
1261 template <class AdaptableUnaryFunction, class Iterator>
1262 class const_projection_iterator_generator {
1263     typedef typename AdaptableUnaryFunction::result_type value_type;
1264     typedef projection_iterator_policies<AdaptableUnaryFunction> policies;
1265 public:
1266     typedef iterator_adaptor<Iterator,policies,value_type,const value_type&,const value_type*> type;
1267 };
1268
1269 template <class AdaptableUnaryFunction, class Iterator, class ConstIterator>
1270 struct projection_iterator_pair_generator {
1271     typedef typename projection_iterator_generator<AdaptableUnaryFunction, Iterator>::type iterator;
1272     typedef typename const_projection_iterator_generator<AdaptableUnaryFunction, ConstIterator>::type const_iterator;
1273 };
1274
1275
1276 template <class AdaptableUnaryFunction, class Iterator>
1277 inline typename projection_iterator_generator<AdaptableUnaryFunction, Iterator>::type
1278 make_projection_iterator(
1279     Iterator iter,
1280     const AdaptableUnaryFunction& f = AdaptableUnaryFunction())
1281 {
1282     typedef typename projection_iterator_generator<AdaptableUnaryFunction, Iterator>::type result_t;
1283     return result_t(iter, f);
1284 }
1285
1286 template <class AdaptableUnaryFunction, class Iterator>
1287 inline typename const_projection_iterator_generator<AdaptableUnaryFunction, Iterator>::type
1288 make_const_projection_iterator(
1289     Iterator iter,
1290     const AdaptableUnaryFunction& f = AdaptableUnaryFunction())
1291 {
1292     typedef typename const_projection_iterator_generator<AdaptableUnaryFunction, Iterator>::type result_t;
1293     return result_t(iter, f);
1294 }
1295
1296 //=============================================================================
1297 // Filter Iterator Adaptor
1298
1299 template <class Predicate, class Iterator>
1300 class filter_iterator_policies
1301 {
1302 public:
1303     filter_iterator_policies() { }
1304
1305     filter_iterator_policies(const Predicate& p, const Iterator& end)
1306         : m_predicate(p), m_end(end) { }
1307
1308     void initialize(Iterator& x) {
1309         satisfy_predicate(x);
1310     }
1311
1312     // The Iter template argument is neccessary for compatibility with a MWCW
1313     // bug workaround
1314     template <class IteratorAdaptor>
1315     void increment(IteratorAdaptor& x) {
1316         ++x.base();
1317         satisfy_predicate(x.base());
1318     }
1319
1320     template <class IteratorAdaptor>
1321     typename IteratorAdaptor::reference dereference(const IteratorAdaptor& x) const
1322         { return *x.base(); }
1323
1324     template <class IteratorAdaptor1, class IteratorAdaptor2>
1325     bool equal(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const
1326         { return x.base() == y.base(); }
1327
1328  private:
1329     void satisfy_predicate(Iterator& iter);
1330     Predicate m_predicate;
1331     Iterator m_end;
1332 };
1333
1334 template <class Predicate, class Iterator>
1335 void filter_iterator_policies<Predicate,Iterator>::satisfy_predicate(
1336     Iterator& iter)
1337 {
1338     while (m_end != iter && !m_predicate(*iter))
1339         ++iter;
1340 }
1341
1342
1343
1344 namespace detail {
1345   // A type generator returning Base if T is derived from Base, and T otherwise.
1346   template <class Base, class T>
1347   struct reduce_to_base_class
1348   {
1349       typedef typename if_true<(
1350             ::boost::is_convertible<T*,Base*>::value
1351           )>::template then<Base,T>::type type;
1352   };
1353
1354   // "Steps down" the category of iterators below bidirectional so the category
1355   // can be used with filter iterators.
1356   template <class Iterator>
1357   struct non_bidirectional_category
1358   {
1359 # if !defined(__MWERKS__) || __MWERKS__ > 0x2406
1360       typedef typename reduce_to_base_class<
1361               std::forward_iterator_tag,
1362                    typename iterator_traits<Iterator>::iterator_category
1363       >::type type;
1364    private:
1365       // For some reason, putting this assertion in filter_iterator_generator fails inexplicably under MSVC
1366       BOOST_STATIC_CONSTANT(
1367           bool, is_bidirectional
1368           = (!boost::is_convertible<type*, std::bidirectional_iterator_tag*>::value));
1369       BOOST_STATIC_ASSERT(is_bidirectional);
1370 # else
1371       // is_convertible doesn't work with MWERKS
1372       typedef typename iterator_traits<Iterator>::iterator_category input_category;
1373   public:
1374       typedef typename if_true<(
1375           boost::is_same<input_category,std::random_access_iterator_tag>::value
1376           || boost::is_same<input_category,std::bidirectional_iterator_tag>::value
1377         )>::template then<
1378           std::forward_iterator_tag,
1379           input_category
1380       >::type type;
1381 # endif
1382   };
1383 }
1384
1385 template <class Predicate, class Iterator,
1386     class Value = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_traits<Iterator>::value_type,
1387     class Reference = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_defaults<Iterator,Value>::reference,
1388     class Pointer = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_defaults<Iterator,Value>::pointer,
1389     class Category = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::non_bidirectional_category<Iterator>::type,
1390     class Distance = BOOST_ARG_DEPENDENT_TYPENAME boost::detail::iterator_traits<Iterator>::difference_type
1391          >
1392 class filter_iterator_generator {
1393     BOOST_STATIC_CONSTANT(bool, is_bidirectional
1394         = (boost::is_convertible<Category*, std::bidirectional_iterator_tag*>::value));
1395 #if !defined(BOOST_MSVC)  || BOOST_MSVC > 1300 // I don't have any idea why this occurs, but it doesn't seem to hurt too badly.
1396     BOOST_STATIC_ASSERT(!is_bidirectional);
1397 #endif
1398     typedef filter_iterator_policies<Predicate,Iterator> policies_type;
1399  public:
1400     typedef iterator_adaptor<Iterator,policies_type,
1401         Value,Reference,Pointer,Category,Distance> type;
1402 };
1403
1404 // This keeps MSVC happy; it doesn't like to deduce default template arguments
1405 // for template function return types
1406 namespace detail {
1407   template <class Predicate, class Iterator>
1408   struct filter_generator {
1409     typedef typename boost::filter_iterator_generator<Predicate,Iterator>::type type;
1410   };
1411 }
1412
1413 template <class Predicate, class Iterator>
1414 inline typename detail::filter_generator<Predicate, Iterator>::type
1415 make_filter_iterator(Iterator first, Iterator last, const Predicate& p = Predicate())
1416 {
1417   typedef filter_iterator_generator<Predicate, Iterator> Gen;
1418   typedef filter_iterator_policies<Predicate,Iterator> policies_t;
1419   typedef typename Gen::type result_t;
1420   return result_t(first, policies_t(p, last));
1421 }
1422
1423 } // namespace boost
1424 # undef BOOST_ARG_DEPENDENT_TYPENAME
1425
1426
1427 #endif
1428
1429
1430