move FrameworkName from corlib to System
[mcs.git] / class / corlib / System / ArraySegment.cs
blob44a71391821f6f2e24821b49d25fcbadf2e34855
1 //
2 // ArraySegment.cs
3 //
4 // Authors:
5 // Ben Maurer (bmaurer@ximian.com)
6 // Jensen Somers <jensen.somers@gmail.com>
7 //
8 // Copyright (C) 2004 Novell
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 namespace System {
30 [Serializable]
31 public struct ArraySegment <T> {
32 T [] array;
33 int offset, count;
35 public ArraySegment (T [] array, int offset, int count)
37 if (array == null)
38 throw new ArgumentNullException ("array");
40 if (offset < 0)
41 throw new ArgumentOutOfRangeException ("offset", "Non-negative number required.");
43 if (count < 0)
44 throw new ArgumentOutOfRangeException ("count", "Non-negative number required.");
46 if (offset > array.Length)
47 throw new ArgumentException ("out of bounds");
49 // now offset is valid, or just beyond the end.
50 // Check count -- do it this way to avoid overflow on 'offset + count'
51 if (array.Length - offset < count)
52 throw new ArgumentException ("out of bounds", "offset");
54 this.array = array;
55 this.offset = offset;
56 this.count = count;
59 public ArraySegment (T [] array)
61 if (array == null)
62 throw new ArgumentNullException ("array");
64 this.array = array;
65 this.offset = 0;
66 this.count = array.Length;
69 public T [] Array {
70 get { return array; }
73 public int Offset {
74 get { return offset; }
77 public int Count {
78 get { return count; }
81 public override bool Equals (Object obj)
83 if (obj is ArraySegment<T>) {
84 return this.Equals((ArraySegment<T>) obj);
86 return false;
89 public bool Equals (ArraySegment<T> obj)
91 if ((this.array == obj.Array) && (this.offset == obj.Offset) && (this.count == obj.Count))
92 return true;
93 return false;
96 public override int GetHashCode ()
98 return ((this.array.GetHashCode() ^ this.offset) ^ this.count);
101 public static bool operator ==(ArraySegment<T> a, ArraySegment<T> b)
103 return a.Equals(b);
106 public static bool operator !=(ArraySegment<T> a, ArraySegment<T> b)
108 return !(a.Equals(b));