1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Collections; 6 7namespace Antlr.Runtime.JavaExtensions 8{ 9 public class SubList 10 : IList 11 { 12 IList _source; 13 int _startIndex; 14 int _endIndex; 15 16 public SubList( IList source, int startIndex, int endIndex ) 17 { 18 if ( source == null ) 19 throw new ArgumentNullException( "source" ); 20 if ( startIndex < 0 || endIndex < 0 ) 21 throw new ArgumentOutOfRangeException(); 22 if ( startIndex > endIndex || endIndex >= source.Count ) 23 throw new ArgumentException(); 24 25 _source = source; 26 _startIndex = startIndex; 27 _endIndex = endIndex; 28 } 29 30 #region IList Members 31 32 public int Add( object value ) 33 { 34 throw new NotSupportedException(); 35 } 36 37 public void Clear() 38 { 39 throw new NotSupportedException(); 40 } 41 42 public bool Contains( object value ) 43 { 44 return _source 45 .Cast<object>() 46 .Skip( _startIndex ) 47 .Take( _endIndex - _startIndex + 1 ) 48 .Contains( value ); 49 } 50 51 public int IndexOf( object value ) 52 { 53 for ( int i = 0; i < Count; i++ ) 54 { 55 if ( object.Equals( this[i], value ) ) 56 return i; 57 } 58 59 return -1; 60 } 61 62 public void Insert( int index, object value ) 63 { 64 throw new NotSupportedException(); 65 } 66 67 public bool IsFixedSize 68 { 69 get 70 { 71 return true; 72 } 73 } 74 75 public bool IsReadOnly 76 { 77 get 78 { 79 return true; 80 } 81 } 82 83 public void Remove( object value ) 84 { 85 throw new NotSupportedException(); 86 } 87 88 public void RemoveAt( int index ) 89 { 90 throw new NotSupportedException(); 91 } 92 93 public object this[int index] 94 { 95 get 96 { 97 if ( index < 0 || index >= Count ) 98 throw new ArgumentOutOfRangeException(); 99 100 return _source[index + _startIndex]; 101 } 102 set 103 { 104 if ( index < 0 || index >= Count ) 105 throw new ArgumentOutOfRangeException(); 106 107 _source[index + _startIndex] = value; 108 } 109 } 110 111 #endregion 112 113 #region ICollection Members 114 115 public void CopyTo( Array array, int index ) 116 { 117 if ( array == null ) 118 throw new ArgumentNullException( "array" ); 119 120 if ( index < 0 ) 121 throw new ArgumentOutOfRangeException(); 122 123 if ( index + Count > array.Length ) 124 throw new ArgumentException(); 125 126 for ( int i = 0; i < Count; i++ ) 127 { 128 array.SetValue( this[i], index + i ); 129 } 130 } 131 132 public int Count 133 { 134 get 135 { 136 return _endIndex - _startIndex + 1; 137 } 138 } 139 140 public bool IsSynchronized 141 { 142 get 143 { 144 return false; 145 } 146 } 147 148 public object SyncRoot 149 { 150 get 151 { 152 return _source.SyncRoot; 153 } 154 } 155 156 #endregion 157 158 #region IEnumerable Members 159 160 public System.Collections.IEnumerator GetEnumerator() 161 { 162 return _source.Cast<object>() 163 .Skip( _startIndex ) 164 .Take( _endIndex - _startIndex + 1 ) 165 .GetEnumerator(); 166 } 167 168 #endregion 169 } 170 171 public class SubList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable 172 { 173 IList<T> _source; 174 int _startIndex; 175 int _endIndex; 176 177 public SubList( IList<T> source, int startIndex, int endIndex ) 178 { 179 if ( source == null ) 180 throw new ArgumentNullException( "source" ); 181 if ( startIndex < 0 || endIndex < 0 ) 182 throw new ArgumentOutOfRangeException(); 183 if ( startIndex > endIndex || endIndex >= source.Count ) 184 throw new ArgumentException(); 185 186 _source = source; 187 _startIndex = startIndex; 188 _endIndex = endIndex; 189 } 190 191 #region IEnumerable Members 192 193 IEnumerator IEnumerable.GetEnumerator() 194 { 195 return GetEnumerator(); 196 } 197 198 #endregion 199 200 #region ICollection Members 201 202 void ICollection.CopyTo( Array array, int index ) 203 { 204 if ( array == null ) 205 throw new ArgumentNullException( "array" ); 206 207 if ( index < 0 ) 208 throw new ArgumentOutOfRangeException(); 209 210 if ( index + Count > array.Length ) 211 throw new ArgumentException(); 212 213 for ( int i = 0; i < Count; i++ ) 214 { 215 array.SetValue( this[i], index + i ); 216 } 217 } 218 219 public int Count 220 { 221 get 222 { 223 return _endIndex - _startIndex + 1; 224 } 225 } 226 227 public bool IsSynchronized 228 { 229 get 230 { 231 ICollection sourceCollection = _source as ICollection; 232 if ( sourceCollection != null ) 233 return sourceCollection.IsSynchronized; 234 235 return false; 236 } 237 } 238 239 public object SyncRoot 240 { 241 get 242 { 243 ICollection sourceCollection = _source as ICollection; 244 if ( sourceCollection != null ) 245 return sourceCollection.SyncRoot; 246 247 return _source; 248 } 249 } 250 251 #endregion 252 253 #region IList Members 254 255 int IList.Add( object value ) 256 { 257 throw new NotSupportedException(); 258 } 259 260 void IList.Clear() 261 { 262 throw new NotSupportedException(); 263 } 264 265 public bool Contains( object value ) 266 { 267 return _source.Cast<object>().Skip( _startIndex ).Take( Count ).Contains( value ); 268 } 269 270 public int IndexOf( object value ) 271 { 272 for ( int i = _startIndex; i <= _endIndex; i++ ) 273 { 274 if ( object.Equals( _source[i], value ) ) 275 return i - _startIndex; 276 } 277 278 return -1; 279 } 280 281 void IList.Insert( int index, object value ) 282 { 283 throw new NotSupportedException(); 284 } 285 286 public bool IsFixedSize 287 { 288 get 289 { 290 var sourceCollection = _source as IList; 291 if ( sourceCollection != null ) 292 return sourceCollection.IsFixedSize; 293 294 return false; 295 } 296 } 297 298 public bool IsReadOnly 299 { 300 get 301 { 302 return true; 303 } 304 } 305 306 void IList.Remove( object value ) 307 { 308 throw new NotSupportedException(); 309 } 310 311 void IList.RemoveAt( int index ) 312 { 313 throw new NotSupportedException(); 314 } 315 316 object IList.this[int index] 317 { 318 get 319 { 320 return this[index]; 321 } 322 set 323 { 324 this[index] = (T)value; 325 } 326 } 327 328 #endregion 329 330 #region IEnumerable<T> Members 331 332 public IEnumerator<T> GetEnumerator() 333 { 334 return _source.Skip( _startIndex ).Take( Count ).GetEnumerator(); 335 } 336 337 #endregion 338 339 #region ICollection<T> Members 340 341 void ICollection<T>.Add( T item ) 342 { 343 throw new NotSupportedException(); 344 } 345 346 void ICollection<T>.Clear() 347 { 348 throw new NotSupportedException(); 349 } 350 351 public bool Contains( T item ) 352 { 353 return _source.Skip( _startIndex ).Take( Count ).Contains( item ); 354 } 355 356 public void CopyTo( T[] array, int arrayIndex ) 357 { 358 if ( array == null ) 359 throw new ArgumentNullException( "array" ); 360 361 if ( arrayIndex < 0 ) 362 throw new ArgumentOutOfRangeException(); 363 364 if ( arrayIndex + Count > array.Length ) 365 throw new ArgumentException(); 366 367 for ( int i = 0; i < Count; i++ ) 368 { 369 array[arrayIndex + i] = this[i]; 370 } 371 } 372 373 bool ICollection<T>.Remove( T item ) 374 { 375 throw new NotSupportedException(); 376 } 377 378 #endregion 379 380 #region IList<T> Members 381 382 public int IndexOf( T item ) 383 { 384 for ( int i = 0; i < Count; i++ ) 385 { 386 if ( object.Equals( this[i], item ) ) 387 return i; 388 } 389 390 return -1; 391 } 392 393 void IList<T>.Insert( int index, T item ) 394 { 395 throw new NotSupportedException(); 396 } 397 398 void IList<T>.RemoveAt( int index ) 399 { 400 throw new NotSupportedException(); 401 } 402 403 public T this[int index] 404 { 405 get 406 { 407 if ( index < 0 || index >= Count ) 408 throw new ArgumentOutOfRangeException(); 409 410 return _source[index + _startIndex]; 411 } 412 set 413 { 414 if ( index < 0 || index >= Count ) 415 throw new ArgumentOutOfRangeException(); 416 417 _source[index + _startIndex] = value; 418 } 419 } 420 421 #endregion 422 } 423} 424