In Test/System.Windows.Forms:
[mono-project.git] / mcs / class / corlib / System / ArraySegment.cs
blob2b8b0eef73f0f2d0d7f6220562f19f6b2aa6fb47
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 #if NET_2_0
30 namespace System {
31 [Serializable]
32 public struct ArraySegment <T> {
33 T [] array;
34 int offset, count;
36 public ArraySegment (T [] array, int offset, int count)
38 if (array == null)
39 throw new ArgumentNullException ("array");
41 if (offset < 0)
42 throw new ArgumentOutOfRangeException ("offset", "Non-negative number required.");
44 if (count < 0)
45 throw new ArgumentOutOfRangeException ("count", "Non-negative number required.");
47 if (offset > array.Length)
48 throw new ArgumentException ("out of bounds");
50 // now offset is valid, or just beyond the end.
51 // Check count -- do it this way to avoid overflow on 'offset + count'
52 if (array.Length - offset < count)
53 throw new ArgumentException ("out of bounds", "offset");
55 this.array = array;
56 this.offset = offset;
57 this.count = count;
60 public ArraySegment (T [] array)
62 if (array == null)
63 throw new ArgumentNullException ("array");
65 this.array = array;
66 this.offset = 0;
67 this.count = array.Length;
70 public T [] Array {
71 get { return array; }
74 public int Offset {
75 get { return offset; }
78 public int Count {
79 get { return count; }
82 public override bool Equals (Object obj)
84 if (obj is ArraySegment<T>) {
85 return this.Equals((ArraySegment<T>) obj);
87 return false;
90 public bool Equals (ArraySegment<T> obj)
92 if ((this.array == obj.Array) && (this.offset == obj.Offset) && (this.count == obj.Count))
93 return true;
94 return false;
97 public override int GetHashCode ()
99 return ((this.array.GetHashCode() ^ this.offset) ^ this.count);
102 public static bool operator ==(ArraySegment<T> a, ArraySegment<T> b)
104 return a.Equals(b);
107 public static bool operator !=(ArraySegment<T> a, ArraySegment<T> b)
109 return !(a.Equals(b));
113 #endif