/// <summary> /// Returns a new Query where each new element in the sequence is an instance of the PrevPair struct. /// The value field of the pair will point to an element in the current sequence, and the prev field will /// point to an element that comes 'offset' elements before the current element. If 'includeStart' is true, /// the sequence will also include elements that have no previous element. /// /// For example, with an offset of 2 and with includeStart as true, the sequence: /// A, B, C, D, E, F /// is transformed into: /// (A,_) (B,_) (C,A) (D,B) (E,C) (F,D) /// </summary> public static Query <PrevPair <T> > WithPrevious <T>(this Query <T> query, int offset = 1, bool includeStart = false) { using (var slice = query.Deconstruct()) { int resultCount = includeStart ? slice.Count : Mathf.Max(0, slice.Count - offset); var dstArray = ArrayPool <PrevPair <T> > .Spawn(resultCount); int dstIndex = 0; if (includeStart) { for (int i = 0; i < Mathf.Min(slice.Count, offset); i++) { dstArray[dstIndex++] = new PrevPair <T>() { value = slice[i], prev = default(T), hasPrev = false }; } } for (int i = offset; i < slice.Count; i++) { dstArray[dstIndex++] = new PrevPair <T>() { value = slice[i], prev = slice[i - offset], hasPrev = true }; } return(new Query <PrevPair <T> >(dstArray, resultCount)); } }
public bool TryGetNext(out PrevPair <SourceType> t) { top: SourceType value; if (_mainOp.TryGetNext(out value)) { if (_offsetLeft > 0) { _offsetLeft--; if (!_includeStart) { goto top; } t = new PrevPair <SourceType>() { value = value, prev = default(SourceType), hasPrev = false }; } else { SourceType prev; _delayedOp.TryGetNext(out prev); t = new PrevPair <SourceType>() { value = value, prev = prev, hasPrev = true }; } return(true); } else { t = default(PrevPair <SourceType>); return(false); } }