5 // Ben Maurer (bmaurer@ximian.com)
6 // Jensen Somers <jensen.somers@gmail.com>
8 // Copyright (C) 2004 Novell
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 public struct ArraySegment
<T
> {
36 public ArraySegment (T
[] array
, int offset
, int count
)
39 throw new ArgumentNullException ("array");
42 throw new ArgumentOutOfRangeException ("offset", "Non-negative number required.");
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");
60 public ArraySegment (T
[] array
)
63 throw new ArgumentNullException ("array");
67 this.count
= array
.Length
;
75 get { return offset; }
82 public override bool Equals (Object obj
)
84 if (obj
is ArraySegment
<T
>) {
85 return this.Equals((ArraySegment
<T
>) obj
);
90 public bool Equals (ArraySegment
<T
> obj
)
92 if ((this.array
== obj
.Array
) && (this.offset
== obj
.Offset
) && (this.count
== obj
.Count
))
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
)
107 public static bool operator !=(ArraySegment
<T
> a
, ArraySegment
<T
> b
)
109 return !(a
.Equals(b
));