Esiste un metodo “hasNext” per un IEnumerator?
-
13-09-2019 - |
Domanda
Con Iterator
s Java, ho usato il metodo hasNext
per determinare se un'iterazione ha più elementi (senza consumare un elemento) -. Dunque, hasNext
è come un metodo "Peek
"
La mia domanda: c'è qualcosa come un "hasNext
" o il metodo "Peek
" con IEnumerator
s generici C # s '
Soluzione
No, purtroppo non c'è.
Il IEnumerator<T>
interfaccia solo espone i seguenti membri:
Metodi:
Dispose
MoveNext
Reset
Proprietà :
Current
Altri suggerimenti
No, ma in C # è possibile chiedere più volte per l'elemento corrente senza spostare a quello successivo. E 'solo un modo diverso di vedere le cose.
Non sarebbe anche è difficile scrivere una classe C # per prendere un IEnumerator
.NET stile e restituire un Iterator
in stile Java. Personalmente trovo lo stile .NET facile da usare nella maggior parte dei casi, ma ci andiamo:)
EDIT: Okay, questo è completamente testato, ma pensare che funzionerà. Lo fa almeno compilazione:)
using System;
using System.Collections;
using System.Collections.Generic;
// // Mimics Java's Iterable<T> interface
public interface IIterable<T>
{
IIterator<T> Iterator();
}
// Mimics Java's Iterator interface - but
// implements IDisposable for the sake of
// parity with IEnumerator.
public interface IIterator<T> : IDisposable
{
bool HasNext { get; }
T Next();
void Remove();
}
public sealed class EnumerableAdapter<T> : IIterable<T>
{
private readonly IEnumerable<T> enumerable;
public EnumerableAdapter(IEnumerable<T> enumerable)
{
this.enumerable = enumerable;
}
public IIterator<T> Iterator()
{
return new EnumeratorAdapter<T>(enumerable.GetEnumerator());
}
}
public sealed class EnumeratorAdapter<T> : IIterator<T>
{
private readonly IEnumerator<T> enumerator;
private bool fetchedNext = false;
private bool nextAvailable = false;
private T next;
public EnumeratorAdapter(IEnumerator<T> enumerator)
{
this.enumerator = enumerator;
}
public bool HasNext
{
get
{
CheckNext();
return nextAvailable;
}
}
public T Next()
{
CheckNext();
if (!nextAvailable)
{
throw new InvalidOperationException();
}
fetchedNext = false; // We've consumed this now
return next;
}
void CheckNext()
{
if (!fetchedNext)
{
nextAvailable = enumerator.MoveNext();
if (nextAvailable)
{
next = enumerator.Current;
}
fetchedNext = true;
}
}
public void Remove()
{
throw new NotSupportedException();
}
public void Dispose()
{
enumerator.Dispose();
}
}
public sealed class IterableAdapter<T> : IEnumerable<T>
{
private readonly IIterable<T> iterable;
public IterableAdapter(IIterable<T> iterable)
{
this.iterable = iterable;
}
public IEnumerator<T> GetEnumerator()
{
return new IteratorAdapter<T>(iterable.Iterator());
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public sealed class IteratorAdapter<T> : IEnumerator<T>
{
private readonly IIterator<T> iterator;
private bool gotCurrent = false;
private T current;
public IteratorAdapter(IIterator<T> iterator)
{
this.iterator = iterator;
}
public T Current
{
get
{
if (!gotCurrent)
{
throw new InvalidOperationException();
}
return current;
}
}
object IEnumerator.Current
{
get { return Current; }
}
public bool MoveNext()
{
gotCurrent = iterator.HasNext;
if (gotCurrent)
{
current = iterator.Next();
}
return gotCurrent;
}
public void Reset()
{
throw new NotSupportedException();
}
public void Dispose()
{
iterator.Dispose();
}
}
Gli enumeratori sono spesso pigramente valutati in modo hasNext ha poco senso.
No , solo MoveNext
, Reset
e Current
.
Si può anche provare con uno sguardo a questo Implementazione Peek a IEnumerator e IEnumerator <> . Si tratta di un metodo di estensione che aggiunge la funzionalità Peek per IEnumerator. Spero che sia d'aiuto. :)