]> git.donarmstrong.com Git - rsem.git/blob - boost/fusion/iterator/detail/segmented_next_impl.hpp
Updated boost to v1.55.0
[rsem.git] / boost / fusion / iterator / detail / segmented_next_impl.hpp
1 /*=============================================================================
2     Copyright (c) 2011 Eric Niebler
3
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #if !defined(BOOST_FUSION_SEGMENTED_ITERATOR_NEXT_IMPL_HPP_INCLUDED)
8 #define BOOST_FUSION_SEGMENTED_ITERATOR_NEXT_IMPL_HPP_INCLUDED
9
10 #include <boost/type_traits/add_const.hpp>
11 #include <boost/type_traits/remove_reference.hpp>
12 #include <boost/fusion/iterator/equal_to.hpp>
13 #include <boost/fusion/container/list/cons_fwd.hpp>
14 #include <boost/fusion/iterator/next.hpp>
15 #include <boost/fusion/iterator/deref.hpp>
16
17 namespace boost { namespace fusion
18 {
19     template <typename First, typename Second>
20     struct iterator_range;
21
22     template <typename Context>
23     struct segmented_iterator;
24
25     namespace detail
26     {
27         template <typename Sequence, typename Stack>
28         struct segmented_begin_impl;
29
30         //bool is_invalid(stack)
31         //{
32         //  return empty(car(stack));
33         //}
34
35         template <typename Stack>
36         struct is_invalid
37           : result_of::equal_to<
38                 typename Stack::car_type::begin_type,
39                 typename Stack::car_type::end_type
40             >
41         {};
42
43         ////Advance the first iterator in the seq at the
44         ////top of a stack of iterator ranges. Return the
45         ////new stack.
46         //auto pop_front_car(stack)
47         //{
48         //  return cons(iterator_range(next(begin(car(stack))), end(car(stack))), cdr(stack));
49         //}
50
51         template <typename Stack>
52         struct pop_front_car
53         {
54             typedef 
55                 iterator_range<
56                     typename result_of::next<
57                         typename Stack::car_type::begin_type
58                     >::type
59                   , typename Stack::car_type::end_type
60                 >
61             car_type;
62             
63             typedef
64                 cons<car_type, typename Stack::cdr_type>
65             type;
66
67             static type call(Stack const & stack)
68             {
69                 return type(
70                     car_type(fusion::next(stack.car.first), stack.car.last),
71                     stack.cdr);
72             }
73         };
74
75         template <
76             typename Stack,
77             typename Next   = typename pop_front_car<Stack>::type,
78             bool IsInvalid  = is_invalid<Next>::value,
79             int StackSize   = Stack::size::value>
80         struct segmented_next_impl_recurse;
81
82         // Handle the case where the top of the stack has no usable 
83         //auto segmented_next_impl_recurse3(stack)
84         //{
85         //  if (size(stack) == 1)
86         //    return cons(iterator_range(end(car(stack)), end(car(stack))), nil_);
87         //  else
88         //    return segmented_next_impl_recurse(stack.cdr);
89         //}
90
91         template <
92             typename Stack,
93             int StackSize = Stack::size::value>
94         struct segmented_next_impl_recurse3
95         {
96             typedef segmented_next_impl_recurse<typename Stack::cdr_type> impl;
97             typedef typename impl::type type;
98
99             static type call(Stack const & stack)
100             {
101                 return impl::call(stack.cdr);
102             }
103         };
104
105         template <typename Stack>
106         struct segmented_next_impl_recurse3<Stack, 1>
107         {
108             typedef typename Stack::car_type::end_type end_type;
109             typedef iterator_range<end_type, end_type> range_type;
110             typedef cons<range_type> type;
111
112             static type call(Stack const & stack)
113             {
114                 return type(range_type(stack.car.last, stack.car.last));
115             }
116         };
117
118         //auto segmented_next_impl_recurse2(stack)
119         //{
120         //  auto res = segmented_begin_impl(front(car(stack)), stack);
121         //  if (is_invalid(res))
122         //    return segmented_next_impl_recurse3(stack);
123         //  else
124         //    return res;
125         //}
126
127         template <
128             typename Stack,
129             typename Sequence  =
130                 typename remove_reference<
131                     typename add_const<
132                         typename result_of::deref<
133                             typename Stack::car_type::begin_type
134                         >::type
135                     >::type
136                 >::type,
137             typename Result =
138                 typename segmented_begin_impl<Sequence, Stack>::type,
139             bool IsInvalid  =
140                 is_invalid<Result>::value>
141         struct segmented_next_impl_recurse2
142         {
143             typedef segmented_next_impl_recurse3<Stack> impl;
144             typedef typename impl::type type;
145
146             static type call(Stack const & stack)
147             {
148                 return impl::call(stack);
149             }
150         };
151
152         template <typename Stack, typename Sequence, typename Result>
153         struct segmented_next_impl_recurse2<Stack, Sequence, Result, false>
154         {
155             typedef Result type;
156
157             static type call(Stack const & stack)
158             {
159                 return segmented_begin_impl<Sequence, Stack>::call(*stack.car.first, stack);
160             }
161         };
162
163         //auto segmented_next_impl_recurse(stack)
164         //{
165         //  auto next = pop_front_car(stack);
166         //  if (is_invalid(next))
167         //    if (1 == size(stack))
168         //      return next;
169         //    else
170         //      return segmented_next_impl_recurse(cdr(stack));
171         //  else
172         //    return segmented_next_impl_recurse2(next)
173         //}
174
175         template <typename Stack, typename Next, bool IsInvalid, int StackSize>
176         struct segmented_next_impl_recurse
177         {
178             typedef
179                 typename segmented_next_impl_recurse<typename Stack::cdr_type>::type
180             type;
181
182             static type call(Stack const& stack)
183             {
184                 return segmented_next_impl_recurse<typename Stack::cdr_type>::call(stack.cdr);
185             }
186         };
187
188         template <typename Stack, typename Next>
189         struct segmented_next_impl_recurse<Stack, Next, true, 1>
190         {
191             typedef Next type;
192
193             static type call(Stack const & stack)
194             {
195                 return pop_front_car<Stack>::call(stack);
196             }
197         };
198
199         template <typename Stack, typename Next, int StackSize>
200         struct segmented_next_impl_recurse<Stack, Next, false, StackSize>
201         {
202             typedef segmented_next_impl_recurse2<Next> impl;
203             typedef typename impl::type type;
204
205             static type call(Stack const & stack)
206             {
207                 return impl::call(pop_front_car<Stack>::call(stack));
208             }
209         };
210
211         //auto segmented_next_impl(stack)
212         //{
213         //  // car(stack) is a seq of values, not a seq of segments
214         //  auto next = pop_front_car(stack);
215         //  if (is_invalid(next))
216         //    return segmented_next_impl_recurse(cdr(next));
217         //  else
218         //    return next;
219         //}
220
221         template <
222             typename Stack,
223             typename Next   = typename pop_front_car<Stack>::type,
224             bool IsInvalid  = is_invalid<Next>::value>
225         struct segmented_next_impl_aux
226         {
227             typedef segmented_next_impl_recurse<typename Stack::cdr_type> impl;
228             typedef typename impl::type type;
229
230             static type call(Stack const & stack)
231             {
232                 return impl::call(stack.cdr);
233             }
234         };
235
236         template <typename Stack, typename Next>
237         struct segmented_next_impl_aux<Stack, Next, false>
238         {
239             typedef Next type;
240
241             static type call(Stack const & stack)
242             {
243                 return pop_front_car<Stack>::call(stack);
244             }
245         };
246
247         template <typename Stack>
248         struct segmented_next_impl
249           : segmented_next_impl_aux<Stack>
250         {};
251     }
252 }}
253
254 #endif