~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Sharpen/Sharpen/EnumeratorWrapper.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

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