-
Notifications
You must be signed in to change notification settings - Fork 0
/
inrange.hpp
133 lines (115 loc) · 4.81 KB
/
inrange.hpp
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
// The MIT License (MIT)
// Copyright (c) 2023 Anans
// 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.
#pragma once
#ifndef __IN_RANGE__
#define __IN_RANGE__
namespace inrange
{
// 惰性求值列表中的仿迭代器
class CounterIterator
{
public:
using counter_t = long long int;
private:
counter_t number;
const counter_t step; // 包含符号的步长
public:
constexpr CounterIterator(const counter_t, const counter_t);
constexpr CounterIterator &operator++();
constexpr CounterIterator operator++(int);
constexpr counter_t operator*() const;
constexpr bool operator==(const CounterIterator &) const;
constexpr bool operator!=(const CounterIterator &) const;
~CounterIterator() = default;
};
constexpr CounterIterator::CounterIterator(const counter_t _number, const counter_t _step)
: step(_step), number(_number) {}
constexpr CounterIterator &CounterIterator::operator++()
{
number += step;
return *this;
}
constexpr CounterIterator CounterIterator::operator++(int)
{
auto ret = *this;
number += step;
return ret;
}
constexpr CounterIterator::counter_t CounterIterator::operator*() const
{
return number;
}
constexpr bool CounterIterator::operator==(const CounterIterator &another) const
{
return (number - another.number > 0) ^ (number - step - another.number > 0);
}
constexpr bool CounterIterator::operator!=(const CounterIterator &another) const
{
return !((*this) == another);
}
// 惰性求值列表,实现了一个仿序列
class LazyEvaluationList
{
public:
using value_type = CounterIterator::counter_t;
using iterator = CounterIterator;
using const_iterator = const CounterIterator;
using step_t = unsigned int; // 不带符号的步长类型
private:
const_iterator begin_iter, end_iter;
public:
constexpr LazyEvaluationList(const value_type, const value_type, const step_t);
constexpr iterator begin() const;
constexpr iterator end() const;
constexpr const_iterator &cbegin() const;
constexpr const_iterator &cend() const;
~LazyEvaluationList() = default;
};
constexpr LazyEvaluationList::LazyEvaluationList(const value_type _begin_num,
const value_type _end_num,
const step_t _step_long)
: begin_iter(_begin_num, (_begin_num <= _end_num ? _step_long : -CounterIterator::counter_t(_step_long))),
end_iter(_end_num, (_begin_num <= _end_num ? _step_long : -CounterIterator::counter_t(_step_long))) {}
// note:如果不转换为有符号的较宽类型,则步长可能溢出
constexpr LazyEvaluationList::iterator LazyEvaluationList::begin() const
{
return begin_iter;
}
constexpr LazyEvaluationList::iterator LazyEvaluationList::end() const
{
return end_iter;
}
constexpr LazyEvaluationList::const_iterator &LazyEvaluationList::cbegin() const
{
return begin_iter;
}
constexpr LazyEvaluationList::const_iterator &LazyEvaluationList::cend() const
{
return end_iter;
}
}
// for接口
constexpr inline inrange::LazyEvaluationList in(const inrange::LazyEvaluationList::value_type begin,
const inrange::LazyEvaluationList::value_type end,
const inrange::LazyEvaluationList::step_t step = 1)
{
return inrange::LazyEvaluationList(begin, end, step);
}
#endif