xtd 1.0.0
Loading...
Searching...
No Matches
range.hpp
Go to the documentation of this file.
1
4#pragma once
7#include "index.hpp"
8#include "index.hpp"
9#include "object.hpp"
10#include "usize.hpp"
11#include "usize_object.hpp"
12#include "npos.hpp"
13#include "linq/enumerable.hpp"
14#include <vector>
15
17namespace xtd {
38 class range : public xtd::object, xtd::iequatable<range> {
39 public:
41
45
49
51
65
66
68
71 range() noexcept = default;
75 range(const index_type& start, const index_type& end) noexcept : start_{start}, end_{end} {}
79 range(xtd::integer auto start, const xtd::integer auto end) noexcept : start_{start}, end_{end} {}
81
83
87 [[nodiscard]] constexpr auto end() const noexcept -> const index_type& {return end_;}
90 [[nodiscard]] constexpr auto start() const noexcept -> const index_type& {return start_;}
92
94
99 [[nodiscard]] auto equals(const object& obj) const noexcept -> bool override {return is<range>(obj) && equals(static_cast<const range& > (obj));}
103 [[nodiscard]] auto equals(const range& value) const noexcept -> bool override {return xtd::collections::generic::helpers::equator<index_type> {}(start_, value.start_) && xtd::collections::generic::helpers::equator<index_type> {}(end_, value.end_);}
104
113 auto start = start_.get_offset(length);
114 auto end = end_.get_offset(length);
116 return {.start = start, .length = end - start};
117 }
118
121 [[nodiscard]] auto get_hash_code() const noexcept -> size_type override {return hash_code::combine(start_, end_);}
122
125 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("{}..{}", start_, end_);}
127
129
133 [[nodiscard]] static auto all() noexcept -> xtd::range {return range {xtd::index::start, xtd::index::end};}
135
137
142 [[nodiscard]] static auto end_at(index_type end) noexcept -> xtd::range {return range {xtd::index::start, end};}
146 [[nodiscard]] static auto end_at(xtd::integer auto end) noexcept -> xtd::range {return range {xtd::index::start, xtd::index {end}};}
147
151 [[nodiscard]] static auto parse(const xtd::string& value) -> xtd::range {
152 auto result = range {};
154 return result;
155 }
156
160 [[nodiscard]] static auto start_at(index_type start) noexcept -> xtd::range {return range {start, xtd::index::end};}
164 [[nodiscard]] static auto start_at(xtd::integer auto start) noexcept -> xtd::range {return range {xtd::index {start}, xtd::index::end};}
165
170 [[nodiscard]] static auto try_parse(const xtd::string& value, xtd::range& result) noexcept -> bool {
171 auto indexes = value.split('.');
172 if (indexes.length() != 3 && !xtd::string::is_empty(indexes[1])) return false;
173 if (!xtd::index::try_parse(indexes[0], result.start_)) return false;
174 if (!xtd::index::try_parse(indexes[2], result.end_)) return false;
175 return true;
176 }
177
178
179 private:
180 index_type start_ = index_type {0};
181 index_type end_ = index_type {0};
182 };
183}
184
185#include "literals/range.hpp"
186
188template<typename char_t, typename traits_t, typename allocator_t>
190 //return xtd::basic_read_only_string_view<type_t>(self(), range);
191 auto [start, length] = range.get_offset_and_length(size());
192 return xtd::basic_read_only_string_view<char_t> {chars_.begin() + start, chars_.begin() + start + length};
193}
194
195template<typename char_t, typename traits_t, typename allocator_t>
197 return operator [](range);
198}
199
200template<xtd::iterable source_t>
202 if (range.start().is_from_end() || range.end().is_from_end()) {
203 auto safe_source = std::vector<xtd::iterable_value_type<source_t>> {source.begin(), source.end()};
204 for (const auto& item : xtd::span<xtd::iterable_value_type<source_t>>{safe_source, range})
205 co_yield item;
206 } else {
207 auto index = xtd::usize {0};
208 auto skip = range.start().value();
209 auto end = range.end().value();
210 //auto source_holder = enumerable_holder<xtd::raw_type<source_t>> {std::forward<source_t>(source)};
211 //for (const auto& item : source_holder.get())
212 for (const auto& item : source) {
213 if (index++ < skip) continue;
214 if (index - 1 == end) break;
215 co_yield item;
216 }
217 }
218}
Contains xtd::basic_string_view alias.
auto operator[](xtd::usize index) const -> const_reference
Returns a reference to the character at specified location index.
Definition basic_string.hpp:1378
auto operator()(xtd::usize index) const -> const_reference
Returns a reference to the character at specified location index.
Definition basic_string.hpp:1396
static auto is_empty(const xtd::basic_string< value_type, traits_type, allocator_type > &string) noexcept -> bool
Definition basic_string.hpp:1231
Represents an enumerable generator that supports deferred, lazy iteration over a collection of a spec...
Definition enumerable_generator.hpp:44
static auto combine(args_t... values) noexcept -> xtd::usize
Combines values into a hash code.
Definition hash_code.hpp:70
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
static auto take(source_t &&source, xtd::usize count) -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
Returns a specified number of contiguous elements from the start of a sequence.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:40
Represents a range that has start and end indexes.
Definition range.hpp:38
auto get_hash_code() const noexcept -> size_type override
Serves as a hash function for a particular type.
Definition range.hpp:121
auto to_string() const noexcept -> xtd::string override
Returns the string representation of the current Range object.
Definition range.hpp:125
static auto end_at(index_type end) noexcept -> xtd::range
Creates a xtd::range object starting from the first element in the collection to a specified end inde...
Definition range.hpp:142
xtd::index index_type
Represents the xtd::range index type.
Definition range.hpp:44
constexpr auto start() const noexcept -> const index_type &
Gets the inclusive start index of the Range.
Definition range.hpp:90
static auto end_at(xtd::integer auto end) noexcept -> xtd::range
Creates a xtd::range object starting from the first element in the collection to a specified end inde...
Definition range.hpp:146
auto get_offset_and_length(size_type length) const -> offset_and_length
Calculates the start offset and length of the range object using a collection length.
Definition range.hpp:112
xtd::usize size_type
Represents the size type.
Definition range.hpp:47
static auto all() noexcept -> xtd::range
Gets a xtd::range object that starts from the first element to the end.
Definition range.hpp:133
auto equals(const object &obj) const noexcept -> bool override
Determines whether the specified object is equal to the current object.
Definition range.hpp:99
static auto try_parse(const xtd::string &value, xtd::range &result) noexcept -> bool
Converts the string to xtd::range equivalent. A return value indicates whether the conversion succeed...
Definition range.hpp:170
range(xtd::integer auto start, const xtd::integer auto end) noexcept
Instantiates a new xtd::range instance with the specified starting and ending indexes.
Definition range.hpp:79
constexpr auto end() const noexcept -> const index_type &
Gets an Index that represents the exclusive end index of the range.
Definition range.hpp:87
static auto start_at(xtd::integer auto start) noexcept -> xtd::range
Creates a new xtd::range object starting from a specified start index to the end of the collection.
Definition range.hpp:164
static auto start_at(index_type start) noexcept -> xtd::range
Creates a new xtd::range object starting from a specified start index to the end of the collection.
Definition range.hpp:160
static auto parse(const xtd::string &value) -> xtd::range
Converts the string to xtd::range equivalent.
Definition range.hpp:151
auto equals(const range &value) const noexcept -> bool override
Indicates whether the current object is equal to another object of the same type.
Definition range.hpp:103
range() noexcept=default
Instantiates a new xtd::range instance.
Represents a non-owning view over a contiguous sequence of objects.
Definition span.hpp:62
Definition integer.hpp:12
xtd::raw_type< decltype(*std::begin(std::declval< iterable_t & >()))> iterable_value_type
Represents the orward iterable value type.
Definition iterable_value_type.hpp:29
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
@ format
The format is not valid.
Definition exception_case.hpp:51
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
auto range(type_t count)
Generates a sequence of integral numbers within a specified range.
Definition range.hpp:39
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
std::basic_string_view< char_t, traits_t > basic_read_only_string_view
Represents an object that can refer to a constant contiguous sequence of char_t with the first elemen...
Definition basic_read_only_string_view.hpp:16
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto is(xtd::any value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:485
@ start
Starting of a logical operation.
Definition trace_event_type.hpp:37
@ end
The END key.
Definition console_key.hpp:42
Contains xtd::index struct.
Contains xtd::linq::enumerable <type_t> class.
Contains xtd::range suffixes.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
constexpr auto length() const noexcept -> size_type
Returns the length of the current read_only_span.
Definition read_only_span.hpp:209
constexpr auto size() const noexcept -> size_type
Returns the number of elements.
Definition read_only_span.hpp:213
Contains xtd::npos constant.
Contains xtd::object class.
Implements a function object for performing comparisons. Unless specialised, invokes operator== on ty...
Definition equator.hpp:39
Represents a type that can be used to index a collection either from the beginning or the end.
Definition index.hpp:38
static const index start
Represents the index of the first valid element in a collection.
Definition index.hpp:172
static const index end
Represents a value that is not a valid position in a collection.
Definition index.hpp:136
static auto try_parse(const xtd::string &value, xtd::index &result) noexcept -> bool
Converts the string to xtd::index equivalent. A return value indicates whether the conversion succeed...
Represents a offset and length.
Definition range.hpp:59
size_type length
Represents a length.
Definition range.hpp:63
size_type start
Represents a offset.
Definition range.hpp:61
Contains xtd::helpers::throw_helper class.
Contains xtd::usize type.
Contains xtd::usize_object alias.