| | | 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 | | |
| | 269 | 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); |
| | 269 | 15 | | _type = type; |
| | 269 | 16 | | _source = source; |
| | 269 | 17 | | _selector = selector; |
| | 269 | 18 | | } |
| | | 19 | | |
| | | 20 | | public override Iterator<TResult> Clone() => |
| | 60 | 21 | | new SpecSelectIterator<TObject, TResult>(_source, _type, _selector); |
| | | 22 | | |
| | | 23 | | public override bool MoveNext() |
| | | 24 | | { |
| | 790 | 25 | | var index = _state - 1; |
| | 790 | 26 | | var source = _source; |
| | 790 | 27 | | var type = _type; |
| | | 28 | | |
| | 1113 | 29 | | while (unchecked((uint)index < (uint)source.Length)) |
| | | 30 | | { |
| | 958 | 31 | | var item = source[index]; |
| | 958 | 32 | | index = _state++; |
| | 958 | 33 | | if (item.Type == type && item.Reference is TObject reference) |
| | | 34 | | { |
| | 635 | 35 | | _current = _selector(reference, item.Bag); |
| | 635 | 36 | | return true; |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | 155 | 40 | | Dispose(); |
| | 155 | 41 | | return false; |
| | | 42 | | } |
| | | 43 | | } |