xtd 1.0.0
Loading...
Searching...
No Matches
range.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "index.hpp"
6#include "object.hpp"
7#include "usize.hpp"
8#include "usize_object.hpp"
9#include "npos.hpp"
10#include "linq/enumerable.hpp"
11#include <vector>
12
14namespace xtd {
28 class range : public xtd::object, xtd::iequatable<range> {
29 public:
31
34 using index_type = xtd::index;
35
39
41
44 range() noexcept = default;
48 range(const index_type& start, const index_type& end) noexcept : start_{start}, end_{end} {}
52 range(xtd::integer auto start, const xtd::integer auto end) noexcept : start_{start}, end_{end} {}
54
56
60 [[nodiscard]] constexpr auto end() const noexcept -> const index_type& {return end_;}
63 [[nodiscard]] constexpr auto start() const noexcept -> const index_type& {return start_;}
65
72 [[nodiscard]] auto equals(const object& obj) const noexcept -> bool override {return is<range>(obj) && equals(static_cast<const range& > (obj));}
76 [[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_);}
77
80 [[nodiscard]] auto get_hash_code() const noexcept -> size_type override {return hash_code::combine(start_, end_);}
81
84 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("{}..{}", start_, end_);}
86
92 [[nodiscard]] static auto all() noexcept -> xtd::range {return range {xtd::index::start, xtd::index::end};}
94
101 [[nodiscard]] static auto end_at(index_type end) noexcept -> xtd::range {return range {xtd::index::start, end};}
105 [[nodiscard]] static auto end_at(xtd::integer auto end) noexcept -> xtd::range {return range {xtd::index::start, xtd::index {end}};}
106
110 [[nodiscard]] static auto start_at(index_type start) noexcept -> xtd::range {return range {start, xtd::index::end};}
114 [[nodiscard]] static auto start_at(xtd::integer auto start) noexcept -> xtd::range {return range {xtd::index {start}, xtd::index::end};}
116
117 private:
118 index_type start_ = index_type {0};
119 index_type end_ = index_type {0};
120 };
121}
122
124template<xtd::iterable source_t>
126 //auto source_holder = enumerable_holder<source_t> {std::forward<source_t>(source)};
127 //return invoke_take_with_range(source_holder.get(), range);
128 return invoke_take_with_range(source, range);
129}
130
131template<typename source_t>
132requires(!requires (const xtd::raw_type<source_t>& source) {{source.size()} -> std::convertible_to<std::size_t>;})
133auto xtd::linq::enumerable::invoke_take_with_range(source_t&& source, const xtd::range& range) -> xtd::collections::generic::enumerable_generator<xtd::iterable_value_type<source_t>> {
134 //auto source_holder = enumerable_holder<source_t> {std::forward<source_t>(source)};
135 //auto result = list<xtd::iterable_value_type<source_t>> {source_holder.get()};
136 auto result = std::vector<xtd::iterable_value_type<source_t>> {source.begin(), source.end()};
137 return invoke_take_with_range(result, range);
138}
139
140template<typename source_t>
141requires(requires (const xtd::raw_type<source_t>& source) {{source.size()} -> std::convertible_to<std::size_t>;})
142auto xtd::linq::enumerable::invoke_take_with_range(source_t&& source, const xtd::range& range) -> xtd::collections::generic::enumerable_generator<xtd::iterable_value_type<source_t>> {
143 auto index = xtd::usize {0};
144 auto skip = range.start();
145 auto count = skip + range.end() - range.start();
146 //if (range.end() )
147 //auto source_holder = enumerable_holder<xtd::raw_type<source_t>> {std::forward<source_t>(source)};
148 //for (const auto& item : source_holder.get())
149 for (const auto& item : source) {
150 if (index++ < skip) continue;
151 if (index++ == count) break;
152 co_yield item;
153 }
154}
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
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:28
auto get_hash_code() const noexcept -> size_type override
Serves as a hash function for a particular type.
Definition range.hpp:80
auto to_string() const noexcept -> xtd::string override
Returns the string representation of the current Range object.
Definition range.hpp:84
xtd::index index_type
Represents the xtd::range index type.
Definition range.hpp:34
constexpr auto start() const noexcept -> const index_type &
Gets the inclusive start index of the Range.
Definition range.hpp:63
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:105
xtd::usize size_type
Represents the size type.
Definition range.hpp:37
auto equals(const object &obj) const noexcept -> bool override
Determines whether the specified object is equal to the current object.
Definition range.hpp:72
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:52
constexpr auto end() const noexcept -> const index_type &
Gets an Index that represents the exclusive end index of the range.
Definition range.hpp:60
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:114
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:110
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:76
range() noexcept=default
Instantiates a new xtd::range instance.
Definition integer.hpp:12
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
auto range(type_t count)
Generates a sequence of integral numbers within a specified range.
Definition range.hpp:39
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
std::remove_cvref_t< value_t > raw_type
Represents a raw type alias equivalent to std::remove_cvref_t<value_t>.
Definition raw_type.hpp:25
auto is(xtd::any value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:485
Contains xtd::index struct.
Contains xtd::linq::enumerable <type_t> class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
auto end() const -> const_iterator
Returns an iterator to the end.
Definition read_only_span.hpp:219
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
Contains xtd::usize type.
Contains xtd::usize_object alias.