| | 1 | | using System.Diagnostics; |
| | 2 | |
|
| | 3 | | namespace Pozitron.QuerySpecification; |
| | 4 | |
|
| | 5 | | internal sealed class SpecSelectIterator<TObject, TResult> : Iterator<TResult> |
| | 6 | | { |
| | 7 | | private readonly SpecItem[] _source; |
| | 8 | | private readonly Func<TObject, int, TResult> _selector; |
| | 9 | | private readonly int _type; |
| | 10 | |
|
| 204 | 11 | | public SpecSelectIterator(SpecItem[] source, int type, Func<TObject, int, TResult> selector) |
| | 12 | | { |
| | 13 | | Debug.Assert(source != null && source.Length > 0); |
| | 14 | | Debug.Assert(selector != null); |
| 204 | 15 | | _type = type; |
| 204 | 16 | | _source = source; |
| 204 | 17 | | _selector = selector; |
| 204 | 18 | | } |
| | 19 | |
|
| | 20 | | public override Iterator<TResult> Clone() => |
| 56 | 21 | | new SpecSelectIterator<TObject, TResult>(_source, _type, _selector); |
| | 22 | |
|
| | 23 | | public override bool MoveNext() |
| | 24 | | { |
| 611 | 25 | | var index = _state - 1; |
| 611 | 26 | | var source = _source; |
| 611 | 27 | | var type = _type; |
| | 28 | |
|
| 734 | 29 | | while (unchecked((uint)index < (uint)source.Length)) |
| | 30 | | { |
| 618 | 31 | | var item = source[index]; |
| 618 | 32 | | index = _state++; |
| 618 | 33 | | if (item.Type == type && item.Reference is TObject reference) |
| | 34 | | { |
| 495 | 35 | | _current = _selector(reference, item.Bag); |
| 495 | 36 | | return true; |
| | 37 | | } |
| | 38 | | } |
| | 39 | |
|
| 116 | 40 | | Dispose(); |
| 116 | 41 | | return false; |
| | 42 | | } |
| | 43 | | } |