File: multi_range.h

package info (click to toggle)
etlcpp 20.44.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,692 kB
  • sloc: cpp: 302,710; ansic: 11,683; sh: 1,420; asm: 301; python: 281; makefile: 16
file content (529 lines) | stat: -rw-r--r-- 16,168 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
///\file

/******************************************************************************
The MIT License(MIT)

Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com

Copyright(c) 2020 John Wellbelove

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef ETL_MULTI_LOOP_INCLUDED
#define ETL_MULTI_LOOP_INCLUDED

#include "platform.h"
#include "nullptr.h"
#include "functional.h"
#include "exception.h"
#include "error_handler.h"

namespace etl
{
  //***************************************************************************
  /// Exception for the multi_range.
  //***************************************************************************
  class multi_range_exception : public etl::exception
  {
  public:

    multi_range_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
      : exception(reason_, file_name_, line_number_)
    {
    }
  };

  //***************************************************************************
  /// Circular reference exception.
  //***************************************************************************
  class multi_range_circular_reference : public etl::multi_range_exception
  {
  public:

    multi_range_circular_reference(string_type file_name_, numeric_type line_number_)
      : etl::multi_range_exception(ETL_ERROR_TEXT("multi_range:circular reference", ETL_MULTI_LOOP_FILE_ID"A"), file_name_, line_number_)
    {
    }
  };

  //***************************************************************************
  /// The base class for multi_range.
  //***************************************************************************
  class imulti_range
  {
  public:

    //***************************************************************************
    /// Insert after this range.
    //***************************************************************************
    imulti_range& insert(imulti_range& inner_range)
    {
      ETL_ASSERT(is_valid(inner_range), ETL_ERROR(multi_range_circular_reference));

      // Remember what the next range was.
      imulti_range* next = inner;

      // Append the new range
      inner = &inner_range;

      // Link to the original next range.
      inner_range.set_last(next);

      return *this;
    }

    //***************************************************************************
    /// Append to the most inner range.
    //***************************************************************************
    imulti_range& append(imulti_range& inner_range)
    {
      ETL_ASSERT(is_valid(inner_range), ETL_ERROR(multi_range_circular_reference));

      if (inner != ETL_NULLPTR)
      {
        inner->append(inner_range);
      }
      else
      {
        inner = &inner_range;
      }

      return *this;
    }

    //***************************************************************************
    /// Unlinks this range from its inner.
    //***************************************************************************
    void detach()
    {
      inner = ETL_NULLPTR;
    }

    //***************************************************************************
    /// Unlinks this and all inner ranges.
    //***************************************************************************
    void detach_all()
    {
      if (inner != ETL_NULLPTR)
      {
        inner->detach_all();
      }

      detach();
    }

    //***************************************************************************
    ///
    //***************************************************************************
    bool completed() const
    {
      return has_completed;
    }

    //***************************************************************************
    /// Gets the total number of ranges, from this range inclusive.
    //***************************************************************************
    size_t number_of_ranges() const
    {
      size_t count = 1UL;

      imulti_range* p_range = inner;

      while (p_range != ETL_NULLPTR)
      {
        ++count;
        p_range = p_range->inner;
      }

      return count;
    }

    //***************************************************************************
    /// Gets the total number of iterations over all ranges, from this range inclusive.
    //***************************************************************************
    size_t number_of_iterations()
    {
      size_t count = 0UL;

      for (start(); !completed(); next())
      {
        ++count;
      }

      return count;
    }

    //***************************************************************************
    /// Pure virtual functions.
    //***************************************************************************
    virtual void next() = 0;
    virtual void start() = 0;

  protected:

    //***************************************************************************
    /// Constructor
    //***************************************************************************
    imulti_range()
      : has_completed(true)
      , inner(ETL_NULLPTR)
    {
    }

    //***************************************************************************
    /// Checks that there are no circular references.
    //***************************************************************************
    bool is_valid(imulti_range& inner_range)
    {
      imulti_range* range = &inner_range;

      while (range != ETL_NULLPTR)
      {
        if (range == this)
        {
          return false;
        }

        range = range->inner;
      }

      return true;
    }

    //***************************************************************************
    /// Set the inner range of the last linked range.
    //***************************************************************************
    void set_last(imulti_range* next)
    {
      // Find the last range.
      imulti_range* range = this;

      while (range->inner != ETL_NULLPTR)
      {
        range = range->inner;
      }

      range->inner = next;
    }

    bool has_completed;
    imulti_range* inner;
  };

  //***************************************************************************
  /// multi_range
  /// \tparam T The type to range over.
  //***************************************************************************
  template <typename T>
  class multi_range : public imulti_range
  {
  public:

    typedef T        value_type;
    typedef const T& const_reference;

    //***************************************************************************
    ///
    //***************************************************************************
    struct step_type
    {
      typedef T value_type;

      virtual void operator()(value_type& value) = 0;
    };

    //***************************************************************************
    ///
    //***************************************************************************
    struct forward_step : public step_type
    {
      typedef T value_type;

      virtual void operator()(value_type& value)
      {
        ++value;
      }
    };

    //***************************************************************************
    ///
    //***************************************************************************
    struct forward_step_by : public step_type
    {
      typedef T value_type;

      explicit forward_step_by(const value_type& step_value_)
        : step_value(step_value_)
      {
      }

      virtual void operator()(value_type& value)
      {
        value += step_value;
      }

      const value_type step_value;
    };

    //***************************************************************************
    ///
    //***************************************************************************
    struct reverse_step : public step_type
    {
      typedef T value_type;

      virtual void operator()(value_type& value)
      {
        --value;
      }
    };

    //***************************************************************************
    ///
    //***************************************************************************
    struct reverse_step_by : public step_type
    {
      typedef T value_type;

      explicit reverse_step_by(const value_type& step_value_)
        : step_value(step_value_)
      {
      }

      virtual void operator()(value_type& value)
      {
        value -= step_value;
      }

      const value_type step_value;
    };

    //***************************************************************************
    ///
    //***************************************************************************
    struct compare_type
    {
      typedef T value_type;

      virtual bool operator()(const value_type& lhs, const value_type& rhs) const = 0;
    };

    //***************************************************************************
    ///
    //***************************************************************************
    struct not_equal_compare : public compare_type
    {
      typedef T value_type;

      virtual bool operator()(const value_type& lhs, const value_type& rhs) const ETL_OVERRIDE
      {
        return etl::not_equal_to<value_type>()(lhs, rhs);
      }
    };

    //***************************************************************************
    ///
    //***************************************************************************
    struct less_than_compare : public compare_type
    {
      typedef T value_type;

      virtual bool operator()(const value_type& lhs, const value_type& rhs) const ETL_OVERRIDE
      {
        return etl::less<value_type>()(lhs, rhs);
      }
    };

    //***************************************************************************
    ///
    //***************************************************************************
    struct greater_than_compare : public compare_type
    {
      typedef T value_type;

      virtual bool operator()(const value_type& lhs, const value_type& rhs) const ETL_OVERRIDE
      {
        return etl::greater<value_type>()(lhs, rhs);
      }
    };

    //***************************************************************************
    /// Constructor
    /// \param first The starting value of the range.
    /// \param last  The terminating value of the range. Equal to the last required value + 1.
    //***************************************************************************
    multi_range(value_type first_,
                value_type last_)
      : first(first_)
      , last(last_)
      , current(first_)
      , p_stepper(&default_stepper)
      , p_compare(&default_compare)
    {
    }

    //***************************************************************************
    /// Constructor
    /// \param first The starting value of the range.
    /// \param last  The terminating value of the range. Equal to the last required value + 1.
    //***************************************************************************
    multi_range(value_type first_,
                value_type last_,
                step_type& stepper_)
      : first(first_)
      , last(last_)
      , current(first_)
      , p_stepper(&stepper_)
      , p_compare(&default_compare)
    {
    }

    //***************************************************************************
    /// Constructor
    /// \param first The starting value of the range.
    /// \param last  The terminating value of the range.
    //***************************************************************************
    multi_range(value_type    first_,
                value_type    last_,
                compare_type& compare_)
      : first(first_)
      , last(last_)
      , current(first_)
      , p_stepper(&default_stepper)
      , p_compare(&compare_)
    {
    }

    //***************************************************************************
    /// Constructor
    /// \param first The starting value of the range.
    /// \param last  The terminating value of the range. Equal to the last required value + 1.
    //***************************************************************************
    multi_range(value_type    first_,
                value_type    last_,
                step_type&    stepper_,
                compare_type& compare_)
      : first(first_)
      , last(last_)
      , current(first_)
      , p_stepper(&stepper_)
      , p_compare(&compare_)
    {
    }

    //***************************************************************************
    /// Get a const reference to the starting value of the range.
    //***************************************************************************
    const_reference begin()
    {
      return first;
    }

    //***************************************************************************
    /// Get a const reference to the terminating value of the range.
    /// Equal to the last required value + 1.
    //***************************************************************************
    const_reference end()
    {
      return last;
    }

    //***************************************************************************
    /// Initialises the ranges to the starting values.
    //***************************************************************************
    void start() ETL_OVERRIDE
    {
      if (inner != ETL_NULLPTR)
      {
        inner->start();
      }

      current = first;
      has_completed = !(*p_compare)(current, last); // Check for null range.
    }

    //***************************************************************************
    /// Step to the next logical values in the ranges.
    //***************************************************************************
    void next() ETL_OVERRIDE
    {
      has_completed = false;

      if (inner != ETL_NULLPTR)
      {
        inner->next();

        if (inner->completed())
        {
          has_completed = step();
        }
      }
      else
      {
        has_completed = step();
      }
    }

    //***************************************************************************
    /// Returns a const reference to the current range value.
    //***************************************************************************
    const_reference value() const
    {
      return current;
    }

  private:

    //***************************************************************************
    /// Increments the current range value.
    //***************************************************************************
    bool step()
    {
      (*p_stepper)(current);

      const bool has_rolled_over = !(*p_compare)(current, last);

      if (has_rolled_over)
      {
        current = first;
      }

      return has_rolled_over;
    }

    multi_range() ETL_DELETE;
    multi_range(const multi_range&) ETL_DELETE;
    multi_range& operator =(const multi_range&) ETL_DELETE;

    value_type first;   ///< The first value of the range.
    value_type last;    ///< The terminating value of the range.
    value_type current; ///< The current value of the range.

    step_type*   p_stepper;
    forward_step default_stepper;

    compare_type*     p_compare;
    not_equal_compare default_compare;
  };
}

#endif