summaryrefslogtreecommitdiffstats
blob: 79bc707e4d41fca85dc66afeffe3153383358ca7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*=============================================================================
   Copyright (c) 2006 Eric Niebler

   Use, modification and distribution is subject to the Boost Software
   License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
   http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#ifndef FUSION_SEGMENTED_ITERATOR_EAN_05032006_1027
#define FUSION_SEGMENTED_ITERATOR_EAN_05032006_1027

#include <boost/mpl/if.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/not.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/fusion/support/tag_of.hpp>
#include <boost/fusion/support/is_sequence.hpp>
#include <boost/fusion/view/filter_view.hpp>
#include <boost/fusion/container/list/cons.hpp> // for nil
#include <boost/fusion/container/generation/make_cons.hpp>
#include <boost/fusion/iterator/advance.hpp>
#include <boost/fusion/iterator/distance.hpp>
#include <boost/fusion/sequence/intrinsic/ext_/segments.hpp>
#include <boost/fusion/support/ext_/is_segmented.hpp>

namespace boost { namespace fusion
{
    struct fusion_sequence_tag;

    namespace detail
    {
        using mpl::_;
        using mpl::not_;

        ////////////////////////////////////////////////////////////////////////////
        template<typename Sequence>
        struct is_empty
          : result_of::equal_to<
                typename result_of::begin<Sequence>::type
              , typename result_of::end<Sequence>::type
            >
        {};

        template<typename Sequence>
        struct is_empty<Sequence &>
          : is_empty<Sequence>
        {};

        ////////////////////////////////////////////////////////////////////////////
        struct not_is_empty_pred
        {
            template<typename Sequence>
            struct apply
              : not_<is_empty<Sequence> >
            {};
        };

        struct segmented_range_tag;

        ////////////////////////////////////////////////////////////////////////////
        template<typename Sequence, typename Index, bool IsSegmented>
        struct segmented_range
          : sequence_base<segmented_range<Sequence, Index, IsSegmented> >
        {
            BOOST_MPL_ASSERT_NOT((is_reference<Sequence>));
            typedef mpl::bool_<IsSegmented> is_segmented;
            typedef segmented_range_tag fusion_tag;
            typedef fusion_sequence_tag tag; // this gets picked up by MPL
            typedef mpl::true_ is_view;

            // If this is a range of segments, skip over the empty ones
            typedef typename mpl::if_<
                is_segmented
              , filter_view<Sequence, not_is_empty_pred>
              , Sequence
            >::type sequence_non_ref_type;

            typedef typename mpl::if_<
                traits::is_view<sequence_non_ref_type>
              , sequence_non_ref_type
              , sequence_non_ref_type &
            >::type sequence_type;

            typedef
                typename fusion::result_of::advance<
                    typename fusion::result_of::begin<sequence_non_ref_type>::type
                  , Index
                >::type
            iterator_type;

            typedef typename traits::category_of<sequence_non_ref_type>::type category;

            explicit segmented_range(Sequence &sequence_)
              : sequence(sequence_type(sequence_))
            {}

            segmented_range(sequence_type sequence_, int)
              : sequence(sequence_)
            {}

            iterator_type where_() const
            {
                return fusion::advance<Index>(
                    fusion::begin(const_cast<sequence_non_ref_type &>(this->sequence))
                );
            }

            sequence_type sequence;

        private:
            segmented_range &operator =(segmented_range const &);
        };
    }

    namespace extension
    {
        template<>
        struct is_segmented_impl<detail::segmented_range_tag>
        {
            template<typename Sequence>
            struct apply
              : Sequence::is_segmented
            {};
        };

        template<>
        struct size_impl<detail::segmented_range_tag>
        {
            template<typename Sequence>
            struct apply
              : mpl::int_<
                    result_of::distance<
                        typename Sequence::iterator_type
                      , typename result_of::end<typename Sequence::sequence_non_ref_type>::type
                    >::value
                >
            {};
        };

        template<>
        struct segments_impl<detail::segmented_range_tag>
        {
            template<typename Sequence>
            struct apply
            {
                typedef Sequence &type;
                static type call(Sequence &seq)
                {
                    return seq;
                }
            };
        };

        template<>
        struct begin_impl<detail::segmented_range_tag>
        {
            template<typename Sequence>
            struct apply
            {
                typedef typename Sequence::iterator_type type;
                static type call(Sequence &seq)
                {
                    return seq.where_();
                }
            };
        };

        template<>
        struct end_impl<detail::segmented_range_tag>
        {
            template<typename Sequence>
            struct apply
            {
                typedef typename Sequence::sequence_non_ref_type sequence;
                typedef typename result_of::end<sequence>::type type;

                static type call(Sequence &seq)
                {
                    return fusion::end(seq.sequence);
                }
            };
        };
    }

    namespace detail
    {
        ///////////////////////////////////////////////////////////////////////
        template<typename Range>
        struct range_next;

        template<typename Sequence, typename Index, bool IsSegmented>
        struct range_next<segmented_range<Sequence, Index, IsSegmented> >
        {
            typedef typename mpl::next<Index>::type index_type;
            typedef segmented_range<Sequence, index_type, IsSegmented> type;

            static type call(segmented_range<Sequence, Index, IsSegmented> const &rng)
            {
                return type(rng.sequence, 0);
            }
        };

        ///////////////////////////////////////////////////////////////////////
        template<typename Cons>
        struct is_range_next_empty
          : is_empty<typename range_next<typename Cons::car_type>::type>
        {};

        template<>
        struct is_range_next_empty<nil>
          : mpl::true_
        {};

        ///////////////////////////////////////////////////////////////////////
        template<typename Sequence, bool IsSegmented = traits::is_segmented<Sequence>::value>
        struct as_segmented_range
        {
            typedef typename result_of::segments<Sequence>::type segments;
            typedef typename remove_reference<segments>::type sequence;
            typedef segmented_range<sequence, mpl::int_<0>, true> type;

            static type call(Sequence &seq)
            {
                segments segs(fusion::segments(seq));
                return type(segs);
            }
        };

        template<typename Sequence>
        struct as_segmented_range<Sequence, false>
        {
            typedef typename remove_reference<Sequence>::type sequence;
            typedef segmented_range<sequence, mpl::int_<0>, false> type;

            static type call(Sequence &seq)
            {
                return type(seq);
            }
        };

        template<typename Sequence, typename Index, bool IsSegmented>
        struct as_segmented_range<segmented_range<Sequence, Index, IsSegmented>, IsSegmented>
        {
            typedef segmented_range<Sequence, Index, IsSegmented> type;
            static type &call(type &seq)
            {
                return seq;
            }
        };

        ///////////////////////////////////////////////////////////////////////
        template<
            typename Sequence
          , typename State = nil
          , bool IsSegmented = traits::is_segmented<Sequence>::value
        >
        struct push_segments
        {
            typedef typename as_segmented_range<Sequence>::type range;
            typedef typename result_of::begin<range>::type begin;
            typedef typename result_of::deref<begin>::type next_ref;
            typedef typename remove_reference<next_ref>::type next;
            typedef push_segments<next, cons<range, State> > push;
            typedef typename push::type type;

            static type call(Sequence &seq, State const &state)
            {
                range rng(as_segmented_range<Sequence>::call(seq));
                next_ref nxt(*fusion::begin(rng));
                return push::call(nxt, fusion::make_cons(rng, state));
            }
        };

        template<typename Sequence, typename State>
        struct push_segments<Sequence, State, false>
        {
            typedef typename as_segmented_range<Sequence>::type range;
            typedef cons<range, State> type;

            static type call(Sequence &seq, State const &state)
            {
                range rng(as_segmented_range<Sequence>::call(seq));
                return fusion::make_cons(rng, state);
            }
        };

        ///////////////////////////////////////////////////////////////////////
        template<typename State, bool IsEmpty = is_range_next_empty<State>::value>
        struct pop_segments
        {
            typedef range_next<typename State::car_type> next;
            typedef push_segments<typename next::type, typename State::cdr_type> push;
            typedef typename push::type type;

            static type call(State const &state)
            {
                typename next::type rng(next::call(state.car));
                return push::call(rng, state.cdr);
            }
        };

        template<typename State>
        struct pop_segments<State, true>
        {
            typedef pop_segments<typename State::cdr_type> pop;
            typedef typename pop::type type;

            static type call(State const &state)
            {
                return pop::call(state.cdr);
            }
        };

        template<>
        struct pop_segments<nil, true>
        {
            typedef nil type;

            static type call(nil const &)
            {
                return nil();
            }
        };
    } // namespace detail

    struct segmented_iterator_tag;

    ////////////////////////////////////////////////////////////////////////////
    template<typename Cons>
    struct segmented_iterator
      : fusion::iterator_base<segmented_iterator<Cons> >
    {
        typedef segmented_iterator_tag fusion_tag;
        typedef fusion::forward_traversal_tag category;

        typedef Cons cons_type;
        typedef typename Cons::car_type car_type;
        typedef typename Cons::cdr_type cdr_type;

        explicit segmented_iterator(Cons const &c)
          : cons_(c)
        {}

        cons_type const &cons() const { return this->cons_; };
        car_type const &car() const { return this->cons_.car; };
        cdr_type const &cdr() const { return this->cons_.cdr; };

    private:
        Cons cons_;
    };

    ///////////////////////////////////////////////////////////////////////////
    template<typename Sequence>
    struct segmented_begin
    {
        typedef typename detail::push_segments<Sequence> push;
        typedef segmented_iterator<typename push::type> type;

        static type call(Sequence &seq)
        {
            return type(push::call(seq, nil()));
        }
    };

    ///////////////////////////////////////////////////////////////////////////
    template<typename Sequence>
    struct segmented_end
    {
        typedef segmented_iterator<nil> type;

        static type call(Sequence &)
        {
            return type(nil());
        }
    };

    namespace extension
    {
        template<>
        struct value_of_impl<segmented_iterator_tag>
        {
            template<typename Iterator>
            struct apply
            {
                typedef typename result_of::begin<typename Iterator::car_type>::type begin;
                typedef typename result_of::value_of<begin>::type type;
            };
        };

        template<>
        struct deref_impl<segmented_iterator_tag>
        {
            template<typename Iterator>
            struct apply
            {
                typedef typename result_of::begin<typename Iterator::car_type>::type begin;
                typedef typename result_of::deref<begin>::type type;

                static type call(Iterator const &it)
                {
                    return *fusion::begin(it.car());
                }
            };
        };

        // discards the old head, expands the right child of the new head
        // and pushes the result to the head of the list.

        template<>
        struct next_impl<segmented_iterator_tag>
        {
            template<
                typename Iterator
              , bool IsSegmentDone = detail::is_range_next_empty<Iterator>::value
            >
            struct apply
            {
                typedef typename Iterator::cdr_type cdr_type;
                typedef detail::range_next<typename Iterator::car_type> next;
                typedef segmented_iterator<cons<typename next::type, cdr_type> > type;

                static type call(Iterator const &it)
                {
                    return type(fusion::make_cons(next::call(it.car()), it.cdr()));
                }
            };

            template<typename Iterator>
            struct apply<Iterator, true> // segment done, move to next segment
            {
                typedef typename Iterator::cdr_type cdr_type;
                typedef typename detail::pop_segments<cdr_type> pop;
                typedef segmented_iterator<typename pop::type> type;

                static type call(Iterator const &it)
                {
                    return type(pop::call(it.cdr()));
                }
            };
        };
    }
}} // namespace boost::fusion

#endif