Enhanced C#
Language of your choice: library documentation
|
A reference to a FVList, a so-called persistent list data structure. More...
A reference to a FVList, a so-called persistent list data structure.
An article is available online about the VList data types.
See the remarks of VListBlock{T} for more information about VLists. Items are normally added to, and removed from, the front of a FVList or to the back of a VList; adding, removing or changing items at any other position is inefficient. You can call ToVList() to convert a FVList to its equivalent VList, which is a reverse-order view of the same list that shares the same memory.
Nested classes | |
struct | Enumerator |
Enumerator for FVList; also used by FWList. More... | |
Public static fields | |
static readonly FVList< T > | Empty = new FVList<T>() |
Properties | |
FVList< T > | Tail [get] |
Returns a list without the first item. If the list is empty, an empty list is retured. More... | |
T | First [get] |
Returns the front item of the list (at index 0), which is the head of the list. More... | |
bool | IsEmpty [get] |
int | BlockChainLength [get] |
Gets the number of blocks used by this list. More... | |
T | this[int index] [get, set] |
T | this[int index, T defaultValue] [get] |
Gets an item from the list at the specified index; returns defaultValue if the index is not valid. More... | |
int | Count [get] |
bool | IsReadOnly [get] |
Public Member Functions | |
FVList (T firstItem) | |
FVList (T itemZero, T itemOne) | |
FVList (T[] array) | |
FVList (IList< T > list) | |
FVList< T > | WithoutFirst (int offset) |
FVList< T > | PreviousIn (FVList< T > largerList) |
FVList< T > | Last (int count) |
override bool | Equals (object rhs_) |
Returns whether the two list references are the same. Does not compare the contents of the lists. More... | |
override int | GetHashCode () |
FVList< T > | AddRange (FVList< T > list) |
FVList< T > | AddRange (FVList< T > list, FVList< T > excludeSubList) |
FVList< T > | AddRange (IList< T > list) |
FVList< T > | InsertRange (int index, IList< T > list) |
FVList< T > | RemoveRange (int index, int count) |
T | Pop () |
Removes the front item (at index 0) from the list and returns it. More... | |
FVList< T > | Push (T item) |
Synonym for Add(); adds an item to the front of the list. More... | |
VList< T > | ToVList () |
Returns this list as a VList, which effectively reverses the order of the elements. More... | |
FWList< T > | ToFWList () |
Returns this list as a FWList. More... | |
WList< T > | ToWList () |
Returns this list as a WList, which effectively reverses the order of the elements. More... | |
T[] | ToArray () |
Returns the FVList converted to an array. More... | |
FVList< T > | SmartAdd (T item, FVList< T > original) |
Adds the specified item to the list, or original.WithoutFirst(original.Count - Count - 1) if doing so is equivalent. More... | |
FVList< T > | SmartAdd (T item, ref FVList< T > original) |
int | IndexOf (T item) |
Searches for the specified object and returns the zero-based index of the first occurrence (lowest index) within the entire FVList. More... | |
void IList< T >. | Insert (int index, T item) |
FVList< T > | Insert (int index, T item) |
void IList< T >. | RemoveAt (int index) |
FVList< T > | RemoveAt (int index) |
void ICollection< T >. | Add (T item) |
Inserts an item at the front (index 0) of the FVList. More... | |
FVList< T > | Add (T item) |
Inserts an item at the front (index 0) of the FVList. More... | |
void ICollection< T >. | Clear () |
FVList< T > | Clear () |
bool | Contains (T item) |
void | CopyTo (T[] array, int arrayIndex) |
bool | Remove (T item) |
Enumerator | GetEnumerator () |
IEnumerator< T > IEnumerable< T >. | GetEnumerator () |
System.Collections.IEnumerator System.Collections.IEnumerable. | GetEnumerator () |
T | 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... | |
FVList< T > | Clone () |
object ICloneable. | Clone () |
FVList< T > | Where (Predicate< T > filter) |
Applies a filter to a list, to exclude zero or more items. More... | |
FVList< T > | WhereSelect (Func< T, Maybe< T >> filter) |
Filters and maps a list with a user-defined function. More... | |
FVList< T > | SmartSelect (Func< T, T > map) |
Maps a list to another list of the same length. More... | |
FVList< Out > | Select< Out > (Func< T, Out > map) |
Maps a list to another list of the same length. More... | |
FVList< T > | Transform (VListTransformer< T > x) |
Transforms a list (combines filtering with selection and more). More... | |
Static Public Member Functions | |
static bool | operator== (FVList< T > lhs, FVList< T > rhs) |
Returns whether the two list references are the same. Does not compare the contents of the lists. More... | |
static bool | operator!= (FVList< T > lhs, FVList< T > rhs) |
Returns whether the two list references are different. Does not compare the contents of the lists. More... | |
static | operator VList< T > (FVList< T > list) |
Returns this list as a VList, which effectively reverses the order of the elements. More... | |
static | operator FWList< T > (FVList< T > list) |
Returns this list as a FWList. More... | |
static | operator WList< T > (FVList< T > list) |
Returns this list as a WList, which effectively reverses the order of the elements. More... | |
|
inline |
Inserts an item at the front (index 0) of the FVList.
References Loyc.Collections.FVList< T >.Add().
Referenced by Loyc.Collections.FVList< T >.Add().
|
inline |
Inserts an item at the front (index 0) of the FVList.
|
inline |
Returns whether the two list references are the same. Does not compare the contents of the lists.
|
inline |
Searches for the specified object and returns the zero-based index of the first occurrence (lowest index) within the entire FVList.
item | Item to locate (can be null if T can be null) |
This method determines equality using the default equality comparer EqualityComparer.Default for T, the type of values in the list.
This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.
|
inlineexplicitstatic |
Returns this list as a FWList.
The list contents are not copied until you modify the FWList.
|
inlineexplicitstatic |
Returns this list as a VList, which effectively reverses the order of the elements.
This is a trivial operation; the VList shares the same memory.
|
inlineexplicitstatic |
Returns this list as a WList, which effectively reverses the order of the elements.
The list contents are not copied until you modify the WList.
|
inlinestatic |
Returns whether the two list references are different. Does not compare the contents of the lists.
|
inlinestatic |
Returns whether the two list references are the same. Does not compare the contents of the lists.
|
inline |
Removes the front item (at index 0) from the list and returns it.
|
inline |
Synonym for Add(); adds an item to the front of the list.
References Loyc.Collections.Add.
|
inline |
Maps a list to another list of the same length.
map | A function that transforms each item in the list. |
|
inline |
Returns a sub-range of this list.
start | The new range will start at this index in the current list (this location will be index [0] in the new range). |
count | The desired number of elements in the new range, or int.MaxValue to get all elements until the end of the list. |
ArgumentException | The start index was below zero. |
The (start, count) range is allowed to be invalid, as long as start is zero or above.
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.FVList< T >.Slice().
Referenced by Loyc.Collections.FVList< T >.Slice().
|
inline |
Returns a sub-range of this list.
start | The new range will start at this index in the current list (this location will be index [0] in the new range). |
count | The desired number of elements in the new range, or int.MaxValue to get all elements until the end of the list. |
ArgumentException | The start index was below zero. |
The (start, count) range is allowed to be invalid, as long as start is zero or above.
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 >.
|
inline |
Adds the specified item to the list, or original.WithoutFirst(original.Count - Count - 1) if doing so is equivalent.
item | Item to add |
original | An old version of the list |
This method helps write functional code in which you process an input list and produce an output list that may or may not be the same as the input list. In case the output list is identical, you would prefer to return the original input list rather than wasting memory on a new list. SmartAdd() helps you do this. The following method demonstrates SmartAdd() by removing all negative numbers from a list: FVList<int> RemoveNegative(FVList<int> input) { var output = FVList<int>.Empty; // Enumerate tail-to-head foreach (int n in (VList<int>)input) if (n >= 0) output.SmartAdd(n, input); return output; }
You could also do the same thing with input.Filter(delegate(int i) { return i; } >= 0)
|
inline |
Maps a list to another list of the same length.
map | A function that transforms each item in the list. |
This method is called "Smart" because of what happens if the map doesn't do anything. If the map function returns the first N items unmodified, those N items are typically not copied, but shared between the existing list and the new one. This is useful for functional code that sometimes processes a list without modifying it at all.
|
inline |
Returns the FVList converted to an array.
|
inline |
Returns this list as a FWList.
The list contents are not copied until you modify the FWList.
|
inline |
Returns this list as a VList, which effectively reverses the order of the elements.
|
inline |
Returns this list as a WList, which effectively reverses the order of the elements.
The list contents are not copied until you modify the WList.
|
inline |
Transforms a list (combines filtering with selection and more).
x | Method to apply to each item in the list |
This is my attempt to make an optimized multi-purpose routine for transforming a FVList or VList. It is slightly cumbersome to use, but allows you to do several common operations in one transformer method.
The VListTransformer method takes two arguments: an item and its index in the FVList or VList. It can modify the item if desired, and then it returns a XfAction value, which indicates the action to take. Most often you will return XfAction.Drop, XfAction.Keep, XfAction.Change, which, repectively, drop the item from the output list, copy the item to the output list unchanged (even if you modified the item), and copy the item to the output list (assuming you changed it).
Transform() needs to know if the item changed, at least at first, because if the first items are kept without changes, then the output list can share a common tail with the input list. If the transformer method returns XfAction.Keep for every element, then the output list is exactly the same (operator== returns true).
Of course, it would have been simpler just to return a boolean indicating whether to keep the item, and the Transform method itself could check whether the item changed. But checking for equality is a tad slow in the .NET framework, because there is no bitwise equality operator in .NET, so a virtual function would have to be called instead to test equality, which is especially slow if T is a value type that does not implement IEquatable(of T).
The final possible action, XfAction.Repeat, is like XfAction.Change except that Transform() calls the VListTransformer again. The second call has the form x(~i, ref item), where ~i is the bitwise NOT of the index i, and item is the same item that x returned the first time it was called. On the second call, x() can return XfAction.Change again to get a third call, if it wants.
XfAction.Repeat is best explained by example. In the following examples, assume "list" is a VList holding the numbers (1, 2, 3): output = list.Transform((i, ref n) => { // This example produces (1, 1, 2, 2, 3, 3) return i >= 0 ? XfAction.Repeat : XfAction.Keep; });
output = list.Transform((i, ref n) => { // This example produces (1, 10, 2, 20, 3, 30) if (i >= 0) return XfAction.Repeat; n *= 10; return XfAction.Change; });
output = list.Transform((i, ref n) => { // This example produces (10, 1, 20, 2, 30, 3) if (i >= 0) { n *= 10; return XfAction.Repeat; } return XfAction.Keep; });
output = list.Transform((i, ref n) => { // This example produces (10, 100, 1000, 20, 200, 30, 300) n *= 10; if (n > 1000) return XfAction.Drop; return XfAction.Repeat; });
And now for some examples using XfAction.Keep, XfAction.Drop and XfAction.Change. Assume list is a VList holding the following integers: (-1, 2, -2, 13, 5, 8, 9) output = list.Transform((i, ref n) => { // Keep every second item: (2, 13, 8) return (i % 2) == 1 ? XfAction.Keep : XfAction.Drop; });
output = list.Transform((i, ref n) => { // Keep odd numbers: (-1, 13, 5, 9) return (n % 2) != 0 ? XfAction.Keep : XfAction.Drop; });
output = list.Transform((i, ref n) => { // Keep and square all odd numbers: (1, 169, 25, 81) if ((n % 2) != 0) { n *= n; return XfAction.Change; } else return XfAction.Drop; });
output = list.Transform((i, ref n) => { // Increase each item by its index: (-1, 3, 0, 16, 9, 13, 15) n += i; return i == 0 ? XfAction.Keep : XfAction.Change; });
|
inline |
Gets the item at the specified index, and does not throw an exception on failure.
index | An index in the range 0 to Count-1. |
fail | A flag that is set on failure. |
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:
Implements Loyc.Collections.IListSource< out T >.
|
inline |
Applies a filter to a list, to exclude zero or more items.
filter | A function that chooses which items to include (exclude items by returning false). |
If the predicate keeps the first N items it is passed (which are the last items in a FVList), those N items are typically not copied, but shared between the existing list and the new one.
|
inline |
Filters and maps a list with a user-defined function.
filter | A function that chooses which items to include in a new list, and what to change them to. |
This is a smart function. If the filter does not modify the first N items it is passed (which are the last items in a FVList), those N items are typically not copied, but shared between the existing list and the new one.
|
get |
Gets the number of blocks used by this list.
You might look at this property when optimizing your program, because the runtime of some operations increases as the chain length increases. This property runs in O(BlockChainLength) time. Ideally, BlockChainLength is proportional to log_2(Count), but certain FVList usage patterns can produce long chains.
|
get |
Returns the front item of the list (at index 0), which is the head of the list.
Referenced by Loyc.Collections.VListBlock< T >.AddRange().
|
get |
Returns a list without the first item. If the list is empty, an empty list is retured.
|
get |
Gets an item from the list at the specified index; returns defaultValue if the index is not valid.