Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
Range.h
浏览该文件的文档.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
20
21#pragma once
22
23#include <sese/Config.h>
24
25namespace sese {
26
29template<class TYPE>
30class Range {
31public:
35 explicit Range(TYPE count) {
36 this->beginNumber = 1;
37 this->endNumber = count;
38 }
39
44 Range(TYPE begin, TYPE end) {
45 if (begin > end) {
46 std::swap(begin, end);
47 }
48
49 this->beginNumber = begin;
50 this->endNumber = end;
51 }
52
54 class Iterator {
55 public:
56 using difference_type = int64_t;
57 using value_type = TYPE;
58 using pointer = const TYPE *;
59 using reference = const TYPE &;
60 using iterator_category = std::forward_iterator_tag;
61
62 explicit Iterator(TYPE cur_number) : curNumber(cur_number) {}
63
65 this->curNumber += 1;
66 return *this;
67 }
68
69 Iterator operator++(int) { // NOLINT
70 Iterator iterator = *this;
71 iterator.curNumber += 1;
72 return iterator;
73 }
74
75 bool operator==(Iterator iterator) const {
76 return this->curNumber == iterator.curNumber;
77 }
78
79 bool operator!=(Iterator iterator) const {
80 return this->curNumber != iterator.curNumber;
81 }
82
83 TYPE operator*() const {
84 return this->curNumber;
85 }
86
87 private:
88 TYPE curNumber = 0;
89 };
90
93 public:
94 using difference_type = int64_t;
95 using value_type = TYPE;
96 using pointer = const TYPE *;
97 using reference = const TYPE &;
98 using iterator_category = std::forward_iterator_tag;
99
100 explicit ReverseIterator(TYPE cur_number) : curNumber(cur_number) {}
101
103 this->curNumber -= 1;
104 return *this;
105 }
106
108 Iterator iterator = *this;
109 iterator.curNumber -= 1;
110 return iterator;
111 }
112
113 bool operator==(ReverseIterator iterator) const {
114 return this->curNumber == iterator.curNumber;
115 }
116
117 bool operator!=(ReverseIterator iterator) const {
118 return this->curNumber != iterator.curNumber;
119 }
120
121 TYPE operator*() const {
122 return this->curNumber;
123 }
124
125 private:
126 TYPE curNumber = 0;
127 };
128
130 return Iterator(beginNumber);
131 }
132
134 return Iterator(endNumber + 1);
135 }
136
140
144
148 bool exist(const TYPE &num) const {
149 return this->beginNumber <= num && this->endNumber >= num;
150 }
151
152private:
153 TYPE beginNumber = 0;
154 TYPE endNumber = 0;
155};
156} // namespace sese