You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.3 KiB

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;
}
}
}