Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
IterableUtils.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
5
7
8 template <typename T, typename ELEMENT_COMPARE_EQUALS_TYPE>
9 optional<size_t> IndexOf_ (const Iterable<T>& c, ArgByValueType<T> item, const ELEMENT_COMPARE_EQUALS_TYPE& equalsComparer)
10 {
11 constexpr bool kUseApply_{true}; // I think apply faster due to single lock
12 if (kUseApply_) {
13 size_t n = 0;
14 return c.Find ([&n, item, equalsComparer] (ArgByValueType<T> ii) { return equalsComparer (ii, item) ? true : (n++, false); })
15 ? n
16 : optional<size_t>{};
17 }
18 else {
19 size_t n = 0;
20 for (const T& i : c) {
21 if (equalsComparer (i, item)) {
22 return n;
23 }
24 ++n;
25 }
26 return {};
27 }
28 }
29
30 template <typename T, typename ELEMENT_COMPARE_EQUALS_TYPE>
31 optional<size_t> IndexOf_ (const Iterable<T>& c, const Iterable<T>& rhs, const ELEMENT_COMPARE_EQUALS_TYPE& equalsComparer)
32 {
33 size_t n = 0;
34 for (auto i = c.begin (); i != c.end (); ++i, ++n) {
35 bool foundDiff = false;
36 auto ii = i;
37 for (const T& r : rhs) {
38 if (ii == c.end ()) {
39 return {};
40 }
41 if (not(equalsComparer (r, *ii))) {
42 foundDiff = true;
43 break;
44 }
45 ++ii;
46 }
47 if (not foundDiff) {
48 return n;
49 }
50 }
51 return {};
52 }
53}