EnumeratorWrapper.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. namespace SharpCifs.Util.Sharpen
  4. {
  5. internal class EnumeratorWrapper<T> : Iterator<T>
  6. {
  7. object _collection;
  8. IEnumerator<T> _e;
  9. T _lastVal;
  10. bool _more;
  11. bool _copied;
  12. public EnumeratorWrapper (object collection, IEnumerator<T> e)
  13. {
  14. this._e = e;
  15. this._collection = collection;
  16. _more = e.MoveNext ();
  17. }
  18. public override bool HasNext ()
  19. {
  20. return _more;
  21. }
  22. public override T Next ()
  23. {
  24. if (!_more)
  25. throw new NoSuchElementException ();
  26. _lastVal = _e.Current;
  27. _more = _e.MoveNext ();
  28. return _lastVal;
  29. }
  30. public override void Remove ()
  31. {
  32. ICollection<T> col = _collection as ICollection<T>;
  33. if (col == null) {
  34. throw new NotSupportedException ();
  35. }
  36. if (_more && !_copied) {
  37. // Read the remaining elements, since the current enumerator
  38. // will be invalid after removing the element
  39. List<T> remaining = new List<T> ();
  40. do {
  41. remaining.Add (_e.Current);
  42. } while (_e.MoveNext ());
  43. _e = remaining.GetEnumerator ();
  44. _e.MoveNext ();
  45. _copied = true;
  46. }
  47. col.Remove (_lastVal);
  48. }
  49. }
  50. }