Enhanced C#
Language of your choice: library documentation

Documentation moved to ecsharp.net

GitHub doesn't support HTTP redirects, so you'll be redirected in 3 seconds.

 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Public fields | Public static fields | Properties | Public Member Functions | List of all members
Loyc.Collections.Slice_< T > Struct Template Reference

Adapter: a random-access range for a slice of an IListSource{T}. More...


Source file:
Inheritance diagram for Loyc.Collections.Slice_< T >:
Loyc.Collections.IRange< out T > Loyc.ICloneable< out T > Loyc.Collections.IIsEmpty Loyc.Collections.IBRange< out T > Loyc.Collections.IListSource< out T > Loyc.ICloneable< out T > Loyc.Collections.IFRange< out T > Loyc.ICloneable< out T > Loyc.ICloneable< out T > Loyc.Collections.IIsEmpty

Remarks

Adapter: a random-access range for a slice of an IListSource{T}.

Template Parameters
TItem type in the list

This type was supposed to be called simply Slice, but this was not allowed because in plain C#, "CS0542: member names cannot be the same as their enclosing type" and of course, this type contains the Slice() method from IListSource.

Public fields

IListSource< T > _list
 
int _start
 
int _count
 

Public static fields

static readonly Slice_< T > Empty = new Slice_<T>()
 

Properties

int Count [get]
 
bool IsEmpty [get]
 
Front [get]
 
Back [get]
 
this[int index] [get]
 
this[int index, T defaultValue] [get]
 
- Properties inherited from Loyc.Collections.IBRange< out T >
Back [get]
 Returns the value of the last item in the range. More...
 
- Properties inherited from Loyc.Collections.IFRange< out T >
Front [get]
 Returns the first value in the range, without popping it. More...
 
- Properties inherited from Loyc.Collections.IIsEmpty
bool IsEmpty [get]
 

Public Member Functions

 Slice_ (IListSource< T > list, int start, int count=int.MaxValue)
 Initializes a slice. More...
 
 Slice_ (IListSource< T > list)
 
PopFront (out bool empty)
 Removes the first item from the range and returns it. More...
 
PopBack (out bool empty)
 Removes the last item from the range and returns it. More...
 
IFRange< T > ICloneable
< IFRange< T > >. 
Clone ()
 
IBRange< T > ICloneable
< IBRange< T > >. 
Clone ()
 
IRange< T > ICloneable< IRange
< T > >. 
Clone ()
 
Slice_< T > Clone ()
 
IEnumerator< T > IEnumerable< T >. GetEnumerator ()
 
System.Collections.IEnumerator
System.Collections.IEnumerable. 
GetEnumerator ()
 
RangeEnumerator< Slice_< T >, T > GetEnumerator ()
 
TryGet (int index, out bool fail)
 Gets the item at the specified index, and does not throw an exception on failure. More...
 
IRange< T > IListSource< T >. Slice (int start, int count)
 Returns a sub-range of this list. More...
 
Slice_< T > Slice (int start, int count=int.MaxValue)
 Returns a sub-range of this list. More...
 

Constructor & Destructor Documentation

Loyc.Collections.Slice_< T >.Slice_ ( IListSource< T >  list,
int  start,
int  count = int.MaxValue 
)
inline

Initializes a slice.

Exceptions
ArgumentExceptionThe start index was below zero.

The (start, count) range is allowed to be invalid, as long as 'start' and 'count' are zero or above.

  • If 'start' is above the original Count, the Count of the new slice is set to zero.
  • if (start + count) is above the original Count, the Count of the new slice is reduced to list.Count - start.

Member Function Documentation

T Loyc.Collections.Slice_< T >.PopBack ( out bool  fail)
inline

Removes the last item from the range and returns it.

Parameters
failReceives the current value of IsEmpty.
Returns
The first item of the range, or default(T) if IsEmpty.

The remarks of IFRange{T}.PopFront apply to this method.

Implements Loyc.Collections.IBRange< out T >.

T Loyc.Collections.Slice_< T >.PopFront ( out bool  fail)
inline

Removes the first item from the range and returns it.

Parameters
failReceives the current value of IIsEmpty.IsEmpty.
Returns
The first item of the range, or default(T) if IsEmpty.

This method is a little unweildy in plain C#, but in EC# it will be a bit more convenient to use via extension methods like PopFirst(ref this Range range, T defaultValue) and PopFirst(ref this Range range), which are illegal in plain C#.

I wanted to give this method the signature "bool PopFirst(out T first)" but the generic parameter "T" is covariant, i.e. it is marked "out T" which, ironically, is not compatible with "out T" parameters, only with return values.

Implements Loyc.Collections.IFRange< out T >.

IRange<T> IListSource<T>. Loyc.Collections.Slice_< T >.Slice ( int  start,
int  count 
)
inline

Returns a sub-range of this list.

Parameters
startThe new range will start at this index in the current list (this location will be index [0] in the new range).
countThe desired number of elements in the new range, or int.MaxValue to get all elements until the end of the list.
Returns
Returns a sub-range of this range.
Exceptions
ArgumentExceptionThe start index was below zero.

The (start, count) range is allowed to be invalid, as long as start is zero or above.

  • If count is below zero, or if start is above the original Count, the Count of the new slice is set to zero.
  • if (start + count) is above the original Count, the Count of the new slice is reduced to this.Count - start. Implementation note: do not compute (start + count) because it may overflow. Instead, test whether (count > this.Count - start).

Most collections should use the following implementation:

IRange<T> IListSource<T>.Slice(int start, int count) { return Slice(start, count); }
public Slice_<T> Slice(int start, int count) { return new Slice_<T>(this, start, count); }

Implements Loyc.Collections.IListSource< out T >.

References Loyc.Collections.Slice_< T >.Slice().

Referenced by Loyc.Collections.Slice_< T >.Slice().

Slice_<T> Loyc.Collections.Slice_< T >.Slice ( int  start,
int  count = int.MaxValue 
)
inline

Returns a sub-range of this list.

Parameters
startThe new range will start at this index in the current list (this location will be index [0] in the new range).
countThe desired number of elements in the new range, or int.MaxValue to get all elements until the end of the list.
Returns
Returns a sub-range of this range.
Exceptions
ArgumentExceptionThe start index was below zero.

The (start, count) range is allowed to be invalid, as long as start is zero or above.

  • If count is below zero, or if start is above the original Count, the Count of the new slice is set to zero.
  • if (start + count) is above the original Count, the Count of the new slice is reduced to this.Count - start. Implementation note: do not compute (start + count) because it may overflow. Instead, test whether (count > this.Count - start).

Most collections should use the following implementation:

IRange<T> IListSource<T>.Slice(int start, int count) { return Slice(start, count); }
public Slice_<T> Slice(int start, int count) { return new Slice_<T>(this, start, count); }

Implements Loyc.Collections.IListSource< out T >.

T Loyc.Collections.Slice_< T >.TryGet ( int  index,
out bool  fail 
)
inline

Gets the item at the specified index, and does not throw an exception on failure.

Parameters
indexAn index in the range 0 to Count-1.
failA flag that is set on failure.
Returns
The element at the specified index, or default(T) if the index is not valid.

In my original design, the caller could provide a value to return on failure, but this would not allow T to be marked as "out" in C# 4. For the same reason, we cannot have a ref/out T parameter. Instead, the following extension methods are provided:

bool TryGet(int index, ref T value);
T TryGet(int, T defaultValue);

Implements Loyc.Collections.IListSource< out T >.