Skip to content

Commit 8cd83d4

Browse files
committed
Refactor enumerable : with xtd concepts and enumerable_generator
1 parent 256aa66 commit 8cd83d4

4 files changed

Lines changed: 82 additions & 73 deletions

File tree

src/xtd.core/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,6 @@ add_sources(
17271727
"src/xtd/io/string_writer.cpp"
17281728
"src/xtd/io/text_reader.cpp"
17291729
"src/xtd/io/text_writer.cpp"
1730-
"src/xtd/linq/enumerable.cpp"
17311730
"src/xtd/literals/byte.cpp"
17321731
"src/xtd/literals/char32.cpp"
17331732
"src/xtd/literals/double.cpp"

src/xtd.core/include/xtd/linq/enumerable.hpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "enumerable_.hpp" // contains xtd::linq::enumerable <type_t> class definition
66
#include "../collections/generic/extensions/enumerable.hpp"
77
#include "../collections/generic/enumerable_generator.hpp"
8+
#include "../nullopt.hpp"
89
#include "from.hpp"
910

1011
/// @cond
@@ -170,3 +171,68 @@ auto xtd::linq::enumerable::average(source_t&& source) -> double {
170171
if (count == 0) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation);
171172
return average / count;
172173
}
174+
175+
template<xtd::forward_iterable source_t>
176+
requires std::same_as<typename xtd::raw_type<source_t>::value_type, xtd::optional<xtd::decimal>>
177+
auto xtd::linq::enumerable::average(source_t&& source) -> xtd::optional<xtd::decimal> {
178+
auto average = .0l;
179+
auto count = 0;
180+
for (auto item : source) {
181+
if (!item) continue;
182+
average += *item;
183+
++count;
184+
}
185+
return count == 0 ? xtd::nullopt : std::make_optional(average / count);
186+
}
187+
188+
template<xtd::forward_iterable source_t>
189+
requires std::same_as<typename xtd::raw_type<source_t>::value_type, xtd::optional<double>>
190+
auto xtd::linq::enumerable::average(source_t&& source) -> xtd::optional<double> {
191+
auto average = .0;
192+
auto count = 0;
193+
for (auto item : source) {
194+
if (!item) continue;
195+
average += *item;
196+
++count;
197+
}
198+
return count == 0 ? xtd::nullopt : std::make_optional(average / count);
199+
}
200+
201+
template<xtd::forward_iterable source_t>
202+
requires std::same_as<typename xtd::raw_type<source_t>::value_type, xtd::optional<xtd::single>>
203+
auto xtd::linq::enumerable::average(source_t&& source) -> xtd::optional<xtd::single> {
204+
auto average = .0f;
205+
auto count = 0;
206+
for (auto item : source) {
207+
if (!item) continue;
208+
average += *item;
209+
++count;
210+
}
211+
return count == 0 ? xtd::nullopt : std::make_optional(average / count);
212+
}
213+
214+
template<xtd::forward_iterable source_t>
215+
requires std::same_as<typename xtd::raw_type<source_t>::value_type, xtd::optional<xtd::int32>>
216+
auto xtd::linq::enumerable::average(source_t&& source) -> xtd::optional<double> {
217+
auto average = .0;
218+
auto count = 0;
219+
for (auto item : source) {
220+
if (!item) continue;
221+
average += *item;
222+
++count;
223+
}
224+
return count == 0 ? xtd::nullopt : std::make_optional(average / count);
225+
}
226+
227+
template<xtd::forward_iterable source_t>
228+
requires std::same_as<typename xtd::raw_type<source_t>::value_type, xtd::optional<xtd::int64>>
229+
auto xtd::linq::enumerable::average(source_t&& source) -> xtd::optional<double> {
230+
auto average = .0;
231+
auto count = 0;
232+
for (auto item : source) {
233+
if (!item) continue;
234+
average += *item;
235+
++count;
236+
}
237+
return count == 0 ? xtd::nullopt : std::make_optional(average / count);
238+
}

src/xtd.core/include/xtd/linq/enumerable_.hpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,24 +276,34 @@ namespace xtd {
276276
/// @brief Computes the average of a sequence of optional xtd::decimal values.
277277
/// @param source A sequence of optional xtd::decimal values to calculate the average of.
278278
/// @return The average of the sequence of values, or xtd::nullopt if the source sequence is empty or contains only values that are xtd::nullopt.
279-
[[nodiscard]] static auto average(const ienumerable<xtd::optional<xtd::decimal >>& source) noexcept -> xtd::optional<xtd::decimal>;
279+
template<xtd::forward_iterable source_t>
280+
requires std::same_as<typename xtd::raw_type<source_t>::value_type, xtd::optional<xtd::decimal>>
281+
[[nodiscard]] static auto average(source_t&& source) -> xtd::optional<xtd::decimal>;
280282
/// @brief Computes the average of a sequence of optional double values.
281283
/// @param source A sequence of optional double values to calculate the average of.
282284
/// @return The average of the sequence of values, or xtd::nullopt if the source sequence is empty or contains only values that are xtd::nullopt.
283-
[[nodiscard]] static auto average(const ienumerable<xtd::optional<double >>& source) noexcept -> xtd::optional<double>;
285+
template<xtd::forward_iterable source_t>
286+
requires std::same_as<typename xtd::raw_type<source_t>::value_type, xtd::optional<double>>
287+
[[nodiscard]] static auto average(source_t&& source) -> xtd::optional<double>;
284288
/// @brief Computes the average of a sequence of optional float values.
285289
/// @param source A sequence of optional float values to calculate the average of.
286290
/// @return The average of the sequence of values, or xtd::nullopt if the source sequence is empty or contains only values that are xtd::nullopt.
287-
[[nodiscard]] static auto average(const ienumerable<xtd::optional<float >>& source) noexcept -> xtd::optional<float>;
291+
template<xtd::forward_iterable source_t>
292+
requires std::same_as<typename xtd::raw_type<source_t>::value_type, xtd::optional<xtd::single>>
293+
[[nodiscard]] static auto average(source_t&& source) -> xtd::optional<xtd::single>;
288294
/// @brief Computes the average of a sequence of optional xtd::int32 values.
289295
/// @param source A sequence of optional xtd::int32 values to calculate the average of.
290296
/// @return The average of the sequence of values, or xtd::nullopt if the source sequence is empty or contains only values that are xtd::nullopt.
291-
[[nodiscard]] static auto average(const ienumerable<xtd::optional<xtd::int32 >>& source) noexcept -> xtd::optional<double>;
297+
template<xtd::forward_iterable source_t>
298+
requires std::same_as<typename xtd::raw_type<source_t>::value_type, xtd::optional<xtd::int32>>
299+
[[nodiscard]] static auto average(source_t&& source) -> xtd::optional<double>;
292300
/// @brief Computes the average of a sequence of optional xtd::int64 values.
293301
/// @param source A sequence of optional xtd::int64 values to calculate the average of.
294302
/// @return The average of the sequence of values, or xtd::nullopt if the source sequence is empty or contains only values that are xtd::nullopt.
295-
[[nodiscard]] static auto average(const ienumerable<xtd::optional<xtd::int64 >>& source) noexcept -> xtd::optional<double>;
296-
303+
template<xtd::forward_iterable source_t>
304+
requires std::same_as<typename xtd::raw_type<source_t>::value_type, xtd::optional<xtd::int64>>
305+
[[nodiscard]] static auto average(source_t&& source) -> xtd::optional<double>;
306+
297307
/// @brief Casts the elements of an xtd::collections::generic::ienumerable to the specified type.
298308
/// @tparam result_t The type of the resulting value.
299309
/// @tparam source_t The type of the elements of source.

src/xtd.core/src/xtd/linq/enumerable.cpp

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)