using Iterator.Collections; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Iterator.Iterators { internal class AlphabeticalOrderIterator : Iterator { private WordsCollection _collection; private int _position = -1; private bool _reverse = false; public AlphabeticalOrderIterator(WordsCollection collection, bool reverse = false) { _collection = collection; _reverse = reverse; if (_reverse) { _position = collection.GetItems().Count; } } public override object Current() { return _collection.GetItems()[_position]; } public override int Key() { return _position; } public override bool MoveNext() { int updatedPosition = _position + (_reverse ? -1: 1); if (updatedPosition < 0 || updatedPosition >= _collection.GetItems().Count) return false; _position = updatedPosition; return true; } public override void Reset() { _position = _reverse ? _collection.GetItems().Count - 1 : 0; } } }