public static IEnumerable<RepetitionFormIndex> ReversedRange(RepetitionFormIndex count) { for (int i = count.Index - 1; i >= 0; i--) yield return new RepetitionFormIndex(i); }
public void Add(ReadOnlyCollection<LeafIndex> selectedElements, ParseFormIndex element) { RepetitionFormIndex repetitionElementIndex = new RepetitionFormIndex(element, Repetition);// Contract.Assert((int)repetitionElementIndex <= selectedLeavesPerParseElement.Count, "Some elements have been skipped in parsing this repetition"); if ((int)repetitionElementIndex == selectedLeavesPerParseElement.Count) selectedLeavesPerParseElement.Add(new List<ReadOnlyCollection<LeafIndex>> { selectedElements }); else selectedLeavesPerParseElement[repetitionElementIndex].Add(selectedElements); }
internal void RemoveExtraneous(List<ReadOnlyCollection<LeafIndex>> matchedIndices, int achievedStretchCount, ParseFormIndex stretchStartIndex) { Contract.Requires(matchedIndices != null); Contract.Requires(0 <= achievedStretchCount && achievedStretchCount < MaximumAchievedRepetitionCount); //the repetition form index of the first element not to be truncated: var untruncatedCount = new RepetitionFormIndex(stretchStartIndex, this.Repetition); //asserts that the elements to be truncated have been parsed (that is, their 'selectedLeavesPerParseElement' is populated) Contract.Assert(Contract.ForAll(RepetitionFormIndex.ReversedRange(untruncatedCount), repetitionToTruncateIndex => selectedLeavesPerParseElement[repetitionToTruncateIndex].Count == this.MaximumAchievedRepetitionCount)); foreach (var repetitionToTruncateIndex in RepetitionFormIndex.ReversedRange(untruncatedCount)) { foreach (var leafCollectionToRemove in selectedLeavesPerParseElement[repetitionToTruncateIndex].Skip(achievedStretchCount)) { bool removed = matchedIndices.Remove(leafCollectionToRemove); Contract.Assert(removed, "It should have been present"); } selectedLeavesPerParseElement[repetitionToTruncateIndex].RemoveRange((int)untruncatedCount); } }