public Enumerator(Enumerable enumerable, EnumerationDirection enumerationDirection) { switch (enumerationDirection) { case EnumerationDirection.FIFO: _reset = () => enumerable.FirstNode; _moveNext = () => _current.Next; break; case EnumerationDirection.LIFO: _reset = () => enumerable.LastNode; _moveNext = () => _current.Previous; break; default: throw new ArgumentOutOfRangeException(nameof(enumerationDirection)); } ResetMoveNext(); }
public void IsOppositeOf_AllPossibleCombinations_Result(EnumerationDirection oneDirection, EnumerationDirection otherDirection, bool result) { // Act var isOppositeOf = oneDirection.IsOppositeOf(otherDirection); // Assert Assert.That(isOppositeOf, Is.EqualTo(result)); }
public static bool IsForward(this EnumerationDirection direction) { // Argument must be valid enum constant Requires(Enum.IsDefined(typeof(EnumerationDirection), direction), EnumMustBeDefined); return(direction == EnumerationDirection.Forwards); }
public DepthFirstTreeNodeEnumerator(TreeNodeType root, EnumerationDirection direction = EnumerationDirection.Forward) { Completed = false; Direction = direction; CurrentItem = InvalidItem; Root = root; ProgressStack = new Stack <IList <TreeNodeType> >(); }
public static bool IsOppositeOf(this EnumerationDirection direction, EnumerationDirection otherDirection) { // Argument must be valid enum constant Requires(Enum.IsDefined(typeof(EnumerationDirection), direction), EnumMustBeDefined); // Argument must be valid enum constant Requires(Enum.IsDefined(typeof(EnumerationDirection), otherDirection), EnumMustBeDefined); return(direction != otherDirection); }
/// <summary> /// Force the specified direction /// </summary> private void SetDirection(EnumerationDirection direction) { Check.Require(IsReadingSequential, StorageResources.CannotChangeReadingDirectionWhenNonSequential); bool hadData = this.HasItem; _direction = direction; _timeComparison = TimeComparer.GetComparer(direction); if (!Backwards) { _getFirstItemToReadFromFileName = (f) => f.FirstItemTimestamp; _getLastItemToReadFromFileName = (f) => f.LastItemTimestamp; } else { _getFirstItemToReadFromFileName = (f) => f.LastItemTimestamp; _getLastItemToReadFromFileName = (f) => f.FirstItemTimestamp; } if (IsDataLoaded && _listReader.Direction != direction) { _listReader = _listReader.Reverse(); Check.Ensure(_listReader.Direction == Direction); } int moveCount = 0; if (IsDataLoaded) { // in sequential mode and when data is loaded iterator points to the data file to load next, so need to move by 2 // (skipping currently loaded file) moveCount = 2; } else { // data is not loaded; 2 cases: 1) seek found file with data beyond seek time and 2) seek time is contained in the file if (!NextFileFound || IsNextFileToBeReadInFull) { // case 1 moveCount = 1; } // case 2: next file remains valid because will have to read some data from it in the opposite direction } _fileIterator.Backwards = Backwards; for (int n = 0; n < moveCount; ++n) { MoveIteratorToNextFile(); } if (!hadData) { _position.SetEmpty(direction); } }
public static EnumerationDirection Opposite(this EnumerationDirection direction) { // Argument must be valid enum constant Requires(Enum.IsDefined(typeof(EnumerationDirection), direction), EnumMustBeDefined); // Result is the opposite direction Ensures(direction == EnumerationDirection.Forwards ? Result <EnumerationDirection>() == EnumerationDirection.Backwards : direction == EnumerationDirection.Backwards && Result <EnumerationDirection>() == EnumerationDirection.Forwards); return(direction.IsForward() ? EnumerationDirection.Backwards : EnumerationDirection.Forwards); }
private void ChangeDirection(EnumerationDirection newDirection, bool force) { Check.DoAssertLambda(CanChangeDirection , () => new InvalidOperationException(StorageResources.CannotChangeReadingDirectionWhenNonSequential)); if (newDirection != Direction || force) { _offlineReaderComparer.Direction = newDirection; _onlineReaderComparer.Direction = newDirection; LinkedList <RepositoryFolderReader> onlineQueue = new LinkedList <RepositoryFolderReader>(_onlineQueue); LinkedList <RepositoryFolderReader> exhaustedReaders = new LinkedList <RepositoryFolderReader>(_exhaustedReaders); LinkedList <RepositoryFolderReader> offlineQueue = new LinkedList <RepositoryFolderReader>(_offlineQueue); _exhaustedReaders.Clear(); _onlineQueue.Clear(); _offlineQueue.Clear(); // first, reverse all online readers foreach (RepositoryFolderReader reader in onlineQueue) { reader.Direction = newDirection; if (!reader.HasMoreItemsInCurrentFile) { AddReaderToOfflineQueue(reader); } else { // make reader top; it has data loaded and we need to preserve position _onlineQueue.AddFirst(reader); // get ready to start from last read item MoveTopReaderToNextItem(); } } // next, reverse offline and exhausted readers foreach (RepositoryFolderReader reader in offlineQueue.Concat(exhaustedReaders)) { reader.Direction = newDirection; AddPositionedOfflineReaderToAQueue(reader); } _position.Direction = newDirection; SyncOfflineQueueForReading(); } Invariant(); }
public IEnumerator <NodeValueType> BreadthFirstNodeValueEnumerator(EnumerationDirection direction) { IEnumerator <NodeValueType> result; if (direction == EnumerationDirection.Forward) { result = ForwardBreadthFirstNodeValueEnumerator(); } else if (direction == EnumerationDirection.Reverse) { result = ReverseBreadthFirstNodeValueEnumerator(); } else { throw new NotSupportedException(String.Format("Enumeration direction '{0}' is not supported.", direction)); } return(result); }
public ExpectedDirectedCollectionValue(SCG.IEnumerable <T> items, SCG.IEqualityComparer <T> equalityComparer, bool allowsNull, Func <T> chooseFunction = null, EnumerationDirection direction = EnumerationDirection.Forwards) : base(items, equalityComparer, allowsNull, chooseFunction) { #region Code Contracts // Argument must be non-null Requires(items != null, ArgumentMustBeNonNull); // Argument must be non-null Requires(equalityComparer != null, ArgumentMustBeNonNull); // All items must be non-null if collection disallows null values Requires(allowsNull || ForAll(items, item => item != null), ItemsMustBeNonNull); // Argument must be valid enum constant Requires(Enum.IsDefined(typeof(EnumerationDirection), direction), EnumMustBeDefined); #endregion Direction = direction; }
/// <summary> /// Create new instance /// </summary> internal RepositoryReader(IRepository repository) { Repository = repository; _seekStatusEvent = new FastSmartWeakEvent <EventHandler <PositionRestoreStatusEventArgs> >(); _readers = new Dictionary <string, RepositoryFolderReader>(); _onlineQueue = new LinkedList <RepositoryFolderReader>(); _offlineQueue = new LinkedList <RepositoryFolderReader>(); _exhaustedReaders = new LinkedList <RepositoryFolderReader>(); _onlineReaderComparer = new ReaderComparerByCurrentItem(); _offlineReaderComparer = new ReaderComparerByNextFile(); _position = new ReadingPosition(); EnumerationDirection defaultDirection = EnumerationDirection.Forwards; ChangeDirection(defaultDirection, true); Reset(); }
public IEnumeratorInfo <T> GetEnumerator(EnumerationDirection enumerationDirection) => new Enumerator(this, enumerationDirection);
public DepthFirstTreeNodeEnumerator(EnumerationDirection direction = EnumerationDirection.Forward) : this(InvalidItem, direction) { }
/// <summary> /// Update position when starting reading from the specified timestamp. /// </summary> /// <param name="seekTime"> /// Time used to seek the reader /// </param> /// <param name="direction"> /// Reading direction (chronologically); <see cref="IRepositoryReader.Direction"/> /// </param> public void Update(DateTime seekTime, EnumerationDirection direction) { Direction = direction; Time = seekTime; }
private bool IsBackwards(EnumerationDirection direction) { return(direction == EnumerationDirection.Backwards); }
public void Seek(DateTime seekTimestamp, EnumerationDirection direction) { _iterator.Seek(seekTimestamp, IsBackwards(direction)); }