File: ListBase.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (243 lines) | stat: -rw-r--r-- 7,731 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//------------------------------------------------------------------------------
// <copyright file="ListBase.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.Xml;
using System.Xml.Schema;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace System.Xml.Xsl {

    /// <summary>
    /// Implementation of read-only IList and IList<T> interfaces.  Derived classes can inherit from
    /// this class and implement only two methods, Count and Item, rather than the entire IList interface.
    /// </summary>
    internal abstract class ListBase<T> : IList<T>, System.Collections.IList {
        //-----------------------------------------------
        // Abstract IList methods that must be
        // implemented by derived classes
        //-----------------------------------------------
        public abstract int Count { get; }
        public abstract T this[int index] { get; set; }


        //-----------------------------------------------
        // Implemented by base class -- accessible on
        // ListBase
        //-----------------------------------------------
        public virtual bool Contains(T value) {
            return IndexOf(value) != -1;
        }

        public virtual int IndexOf(T value) {
            for (int i = 0; i < Count; i++)
                if (value.Equals(this[i]))
                    return i;

            return -1;
        }

        public virtual void CopyTo(T[] array, int index) {
            for (int i = 0; i < Count; i++)
                array[index + i] = this[i];
        }

        public virtual IListEnumerator<T> GetEnumerator() {
            return new IListEnumerator<T>(this);
        }

        public virtual bool IsFixedSize {
            get { return true; }
        }

        public virtual bool IsReadOnly {
            get { return true; }
        }

        public virtual void Add(T value) {
            Insert(Count, value);
        }

        public virtual void Insert(int index, T value) {
            throw new NotSupportedException();
        }

        public virtual bool Remove(T value) {
            int index = IndexOf(value);
            if (index >= 0) {
                RemoveAt(index);
                return true;
            }
            return false;
        }

        public virtual void RemoveAt(int index) {
            throw new NotSupportedException();
        }

        public virtual void Clear() {
            for (int index = Count - 1; index >= 0; index--)
                RemoveAt(index);
        }


        //-----------------------------------------------
        // Implemented by base class -- only accessible
        // after casting to IList
        //-----------------------------------------------
        IEnumerator<T> IEnumerable<T>.GetEnumerator() {
            return new IListEnumerator<T>(this);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return new IListEnumerator<T>(this);
        }

        bool System.Collections.ICollection.IsSynchronized {
            get { return IsReadOnly; }
        }

        object System.Collections.ICollection.SyncRoot {
            get { return this; }
        }

        void System.Collections.ICollection.CopyTo(Array array, int index) {
            for (int i = 0; i < Count; i++)
                array.SetValue(this[i], index);
        }

        object System.Collections.IList.this[int index] {
            get { return this[index]; }
            set {
                if (!IsCompatibleType(value.GetType()))
                    throw new ArgumentException(Res.GetString(Res.Arg_IncompatibleParamType), "value");

                this[index] = (T) value;
            }
        }

        int System.Collections.IList.Add(object value) {
            if (!IsCompatibleType(value.GetType()))
                throw new ArgumentException(Res.GetString(Res.Arg_IncompatibleParamType), "value");

            Add((T) value);
            return Count - 1;
        }

        void System.Collections.IList.Clear() {
            Clear();
        }
    
        bool System.Collections.IList.Contains(object value) {
            if (!IsCompatibleType(value.GetType()))
                return false;

            return Contains((T) value);
        }

        int System.Collections.IList.IndexOf(object value) {
            if (!IsCompatibleType(value.GetType()))
                return -1;

            return IndexOf((T) value);
        }

        void System.Collections.IList.Insert(int index, object value) {
            if (!IsCompatibleType(value.GetType()))
                throw new ArgumentException(Res.GetString(Res.Arg_IncompatibleParamType), "value");
           
            Insert(index, (T) value);
        }

        void System.Collections.IList.Remove(object value) {
            if (IsCompatibleType(value.GetType())) {
                Remove((T)value);
            }
        }


        //-----------------------------------------------
        // Helper methods and classes
        //-----------------------------------------------

        private static bool IsCompatibleType(object value) {
            if((value == null && !typeof(T).IsValueType) || (value is T))
                return true;

            return false;
        }
    }

    /// <summary>
    /// Implementation of IEnumerator<T> and IEnumerator over an IList<T>.
    /// </summary>
    internal struct IListEnumerator<T> : IEnumerator<T>, System.Collections.IEnumerator {
        private IList<T> sequence;
        private int index;
        private T current;

        /// <summary>
        /// Constructor.
        /// </summary>
        public IListEnumerator(IList<T> sequence) {
            this.sequence = sequence;
            this.index = 0;
            this.current = default(T);
        }

        /// <summary>
        /// No-op.
        /// </summary>
        public void Dispose() {
        }

        /// <summary>
        /// Return current item.  Return default value if before first item or after last item in the list.
        /// </summary>
        public T Current {
            get { return this.current; }
        }

        /// <summary>
        /// Return current item.  Throw exception if before first item or after last item in the list.
        /// </summary>
        object System.Collections.IEnumerator.Current {
            get {
                if (this.index == 0)
                    throw new InvalidOperationException(Res.GetString(Res.Sch_EnumNotStarted, string.Empty));

                if (this.index > this.sequence.Count)
                    throw new InvalidOperationException(Res.GetString(Res.Sch_EnumFinished, string.Empty));

                return this.current;
            }
        }

        /// <summary>
        /// Advance enumerator to next item in list.  Return false if there are no more items.
        /// </summary>
        public bool MoveNext() {
            if (this.index < this.sequence.Count) {
                this.current = this.sequence[this.index];
                this.index++;
                return true;
            }

            this.current = default(T);
            return false;
        }

        /// <summary>
        /// Set the enumerator to its initial position, which is before the first item in the list.
        /// </summary>
        void System.Collections.IEnumerator.Reset() {
            this.index = 0;
            this.current = default(T);
        }
    }
}