Skip to content

Commit e724f2a

Browse files
committed
Merge branch 'develop'
2 parents 1733f2e + 9c19bcb commit e724f2a

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

Runtime/DataTypes/RangeEnumerator.cs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,22 @@ namespace JUtils
2424
/// }
2525
/// }
2626
/// }
27-
/// </code></example>>
27+
/// </code></example>
2828
public static class RangeExtensions
2929
{
30-
public static RangeEnumerator GetEnumerator(this Range range) => new (range);
31-
public static RangeEnumerator GetEnumerator(this int end) => new (0, end);
30+
public static RangeEnumerator GetEnumerator(this Range range)
31+
{
32+
return new RangeEnumerator(range);
33+
}
34+
35+
36+
public static RangeEnumerator GetEnumerator(this int end)
37+
{
38+
return new RangeEnumerator(0, end);
39+
}
3240
}
33-
34-
41+
42+
3543
/// <summary>
3644
/// Allows for complex enumerations for Range types
3745
/// </summary>
@@ -40,7 +48,7 @@ public class RangeEnumerator : IEnumerator<int>
4048
private readonly int _startIndex;
4149
private readonly int _direction = 1;
4250
private readonly int _end;
43-
51+
4452
private int _index;
4553

4654

@@ -51,21 +59,15 @@ internal RangeEnumerator(Range range)
5159
_direction = -1;
5260
_startIndex = _index = range.Start.Value;
5361
_end = range.End.Value;
62+
} else {
63+
_startIndex = _index = range.Start.Value - 1;
64+
_end = range.End.Value - 1;
5465
}
55-
else {
56-
_startIndex = _index = range.Start.Value-1;
57-
_end = range.End.Value-1;
58-
}
59-
60-
}
61-
62-
else if (range.End.IsFromEnd) {
66+
} else if (range.End.IsFromEnd) {
6367
_direction = -1;
64-
_startIndex = _index = range.Start.Value+1;
68+
_startIndex = _index = range.Start.Value + 1;
6569
_end = range.End.Value;
66-
}
67-
68-
else {
70+
} else {
6971
_startIndex = _index = range.Start.Value - 1;
7072
_end = range.End.Value;
7173
}
@@ -80,13 +82,21 @@ internal RangeEnumerator(int start, int end)
8082
{
8183
if (start > end) throw new ArgumentException("Start must not be greater than end");
8284

83-
_startIndex = _index = start-1;
84-
_end = end-1;
85+
_startIndex = _index = start - 1;
86+
_end = end - 1;
87+
}
88+
89+
90+
public bool MoveNext()
91+
{
92+
return _direction == 1 ? _index++ < _end : _index-- > _end;
8593
}
8694

8795

88-
public bool MoveNext() => _direction == 1 ? _index++ < _end : _index --> _end;
89-
public void Reset() => _index = _startIndex;
96+
public void Reset()
97+
{
98+
_index = _startIndex;
99+
}
90100

91101

92102
public int Current => _index;

Runtime/Extensions/Enumerable.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using System.Linq;
54
using JetBrains.Annotations;
@@ -107,5 +106,21 @@ public static T Furthest<T>(this IEnumerable<T> self, Vector3 position) where T
107106

108107
return nearestComponent;
109108
}
109+
110+
111+
/// <summary>
112+
/// Try get an specific element in the list based on a predicate
113+
/// </summary>
114+
public static bool TryGet<T>(this IEnumerable<T> self, out T result, Predicate<T> predicate)
115+
{
116+
foreach (T item in self) {
117+
if (!predicate(item)) continue;
118+
result = item;
119+
return true;
120+
}
121+
122+
result = default;
123+
return false;
124+
}
110125
}
111126
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{
22
"name": "jutils",
33
"displayName": "JUtils",
4-
"version": "1.10.3",
4+
"version": "1.10.4",
55
"description": "A unity utilities library, this contains many handy extensions, components and data structures with custom editors",
66
"license": "LGPL-3.0",
77
"author": {
88
"name": "Jeroen van de Geest",
99
"url": "https://jeroenvdg.com"
1010
},
11+
"dependencies": {
12+
"com.unity.editorcoroutines": "1.0.0"
13+
},
1114
"hideInEditor": false
1215
}

0 commit comments

Comments
 (0)