EnumeratorWrapper.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. {
  35. throw new NotSupportedException();
  36. }
  37. if (_more && !_copied)
  38. {
  39. // Read the remaining elements, since the current enumerator
  40. // will be invalid after removing the element
  41. List<T> remaining = new List<T>();
  42. do
  43. {
  44. remaining.Add(_e.Current);
  45. } while (_e.MoveNext());
  46. _e = remaining.GetEnumerator();
  47. _e.MoveNext();
  48. _copied = true;
  49. }
  50. col.Remove(_lastVal);
  51. }
  52. }
  53. }