Enhanced C#
Language of your choice: library documentation
Static Public Member Functions | List of all members
Loyc.Collections.LCInterfaces Class Reference

Extension methods for Loyc collection interfaces More...


Source files:

Remarks

Extension methods for Loyc collection interfaces

This class contains extension methods that are provided as part of various Loyc.Collections interfaces. For example, it provides methods such as IndexOf(), Contains() and CopyTo(), that the traditional ICollection<T> and IList<T> interfaces require the author to write himself.

For covariant collections such as IReadOnlyCollection<T> and IListSource<T>, the CLR actually prohibits methods such as Contains(T) and IndexOf(T), because T is not allowed in "input" positions. Therefore, these extension methods must be used to fill the gap. Even methods such as bool TryGet(int, out T) are prohibited, so TryGet() has the signature T TryGet(ref bool failed) instead, and extension methods provide the original version of the method in addition.

Static Public Member Functions

static void Resize< T > (this IListRangeMethods< T > list, int newSize)
 
static void Resize< T > (this IListEx< T > list, int newSize)
 
static bool Any (this ICount c)
 Returns true if the collection contains any elements. More...
 
static bool GetAndEdit< K, V > (IDictionary< K, V > dict, K key, ref V value, DictEditMode mode)
 Default implementation of IDictionaryEx<K, V>.GetAndEdit. More...
 
static bool AddIfNotPresent< K, V > (this IDictionaryEx< K, V > dict, K key, V value)
 Adds a key/value pair to the dictionary if the key is not present. If the key is already present, this method has no effect. More...
 
static bool TryAdd< K, V > (this IDictionaryEx< K, V > dict, K key, V value)
 
static V GetOrAdd< K, V > (this IDictionaryEx< K, V > dict, K key, V value)
 Adds a key/value pair to the dictionary if the key is not already present, and returns the existing or new value. More...
 
static Maybe< V > AddOrGetExisting< K, V > (this IDictionaryEx< K, V > dict, K key, V value)
 Adds a new key/value pair if the key was not present, or gets the existing value if the key was present. More...
 
static bool ReplaceIfPresent< K, V > (this IDictionaryEx< K, V > dict, K key, V value)
 Replaces an item in the map if the key was already present. If the key was not already present, this method has no effect. More...
 
static Maybe< V > SwapIfPresent< K, V > (this IDictionaryEx< K, V > dict, K key, V value)
 Replaces an item in the map if the key was already present. If the key was not already present, this method has no effect. More...
 
static Maybe< V > SetAndGet< K, V > (this IDictionaryEx< K, V > dict, K key, V value)
 
static Maybe< V > GetAndSet< K, V > (this IDictionaryEx< K, V > dict, K key, V value)
 Associates a key with a value in the dictionary, and gets the old value if the key was already present. More...
 
static bool TryGet< T > (this IListSource< T > list, int index, ref T value)
 Tries to get a value from the list at the specified index. More...
 
static T TryGet< T > (this IListSource< T > list, int index, T defaultValue)
 Tries to get a value from the list at the specified index. More...
 
static Maybe< T > TryGet< T > (this IListSource< T > list, int index)
 
static bool HasIndex< T > (this IListSource< T > list, int index)
 Uses list.TryGet(index) to find out if the specified index is valid. More...
 
static ? int FirstIndexOf< T > (this IReadOnlyList< T > list, T item)
 Determines the index of a specific value. More...
 
static int IndexOf< T > (this IReadOnlyList< T > list, T item)
 Determines the index of a specific value. More...
 
static void CopyTo< T > (this IReadOnlyList< T > c, T[] array, int arrayIndex)
 
static ? int FirstIndexWhere< T > (this IReadOnlyList< T > source, Func< T, bool > pred)
 Gets the lowest index at which a condition is true, or null if nowhere. More...
 
static int IndexWhere< T > (this IReadOnlyList< T > source, Func< T, bool > pred)
 Gets the lowest index at which a condition is true, or -1 if nowhere. More...
 
static ? int FinalIndexWhere< T > (this IReadOnlyList< T > source, Func< T, bool > pred)
 Gets the highest index at which a condition is true, or null if nowhere. More...
 
static int LastIndexWhere< T > (this IReadOnlyList< T > source, Func< T, bool > pred)
 Gets the highest index at which a condition is true, or -1 if nowhere. More...
 
static bool TryGet< T > (this INegListSource< T > list, int index, ref T value)
 Tries to get a value from the list at the specified index. More...
 
static ? int IndexOf< T > (this INegListSource< T > list, T item)
 Determines the index of a specific value. More...
 
static ? int NextHigherIndex< T > (this ISparseListSource< T > list, int? index)
 Gets the next higher index that is not classified as an empty space, or null if there are no non-blank higher indexes. More...
 
static ? int NextLowerIndex< T > (this ISparseListSource< T > list, int? index)
 Gets the next lower index that is not classified as an empty space, or null if there are no non-blank lower indexes. More...
 
static IEnumerable< KeyValuePair< int, T > > Items< T > (this ISparseListSource< T > list)
 Returns the non-cleared items in the sparse list, along with their indexes, sorted by index. More...
 
static bool TryPop< T > (this IPop< T > c, out T value)
 
static bool TryPeek< T > (this IPop< T > c, out T value)
 
static T PopFirst< T > (this IDeque< T > c)
 
static T PopLast< T > (this IDeque< T > c)
 
static T PeekFirst< T > (this IDeque< T > c)
 
static T PeekLast< T > (this IDeque< T > c)
 

Member Function Documentation

◆ AddIfNotPresent< K, V >()

static bool Loyc.Collections.LCInterfaces.AddIfNotPresent< K, V > ( this IDictionaryEx< K, V >  dict,
key,
value 
)
inlinestatic

Adds a key/value pair to the dictionary if the key is not present. If the key is already present, this method has no effect.

Returns
True if the pair was added, false if not.

References Loyc.Collections.IDictionaryEx< K, V >.GetAndEdit().

◆ AddOrGetExisting< K, V >()

static Maybe<V> Loyc.Collections.LCInterfaces.AddOrGetExisting< K, V > ( this IDictionaryEx< K, V >  dict,
key,
value 
)
inlinestatic

Adds a new key/value pair if the key was not present, or gets the existing value if the key was present.

Returns
The existing value. If a new pair was added, the result has no value.
See also
GetOrAdd<K, V>(IDictionaryEx<K, V>, K, V)

References Loyc.Collections.IDictionaryEx< K, V >.GetAndEdit().

◆ Any()

static bool Loyc.Collections.LCInterfaces.Any ( this ICount  c)
inlinestatic

Returns true if the collection contains any elements.

References Loyc.Collections.ICount.Count.

◆ FinalIndexWhere< T >()

static ? int Loyc.Collections.LCInterfaces.FinalIndexWhere< T > ( this IReadOnlyList< T >  source,
Func< T, bool >  pred 
)
inlinestatic

Gets the highest index at which a condition is true, or null if nowhere.

◆ FirstIndexOf< T >()

static ? int Loyc.Collections.LCInterfaces.FirstIndexOf< T > ( this IReadOnlyList< T >  list,
item 
)
inlinestatic

Determines the index of a specific value.

Returns
The index of the value, if found, or null if it was not found.

◆ FirstIndexWhere< T >()

static ? int Loyc.Collections.LCInterfaces.FirstIndexWhere< T > ( this IReadOnlyList< T >  source,
Func< T, bool >  pred 
)
inlinestatic

Gets the lowest index at which a condition is true, or null if nowhere.

◆ GetAndEdit< K, V >()

static bool Loyc.Collections.LCInterfaces.GetAndEdit< K, V > ( IDictionary< K, V >  dict,
key,
ref V  value,
DictEditMode  mode 
)
inlinestatic

Default implementation of IDictionaryEx<K, V>.GetAndEdit.

◆ GetAndSet< K, V >()

static Maybe<V> Loyc.Collections.LCInterfaces.GetAndSet< K, V > ( this IDictionaryEx< K, V >  dict,
key,
value 
)
inlinestatic

Associates a key with a value in the dictionary, and gets the old value if the key was already present.

Returns
The old value associated with the same key. If a new pair was added, the result has no value.

References Loyc.Collections.IDictionaryEx< K, V >.GetAndEdit().

◆ GetOrAdd< K, V >()

static V Loyc.Collections.LCInterfaces.GetOrAdd< K, V > ( this IDictionaryEx< K, V >  dict,
key,
value 
)
inlinestatic

Adds a key/value pair to the dictionary if the key is not already present, and returns the existing or new value.

Returns
The existing value (if the key already existed) or the new value.

References Loyc.Collections.IDictionaryEx< K, V >.GetAndEdit().

◆ HasIndex< T >()

static bool Loyc.Collections.LCInterfaces.HasIndex< T > ( this IListSource< T >  list,
int  index 
)
inlinestatic

Uses list.TryGet(index) to find out if the specified index is valid.

Returns
true if the specified index is valid, false if not.

◆ IndexOf< T >() [1/2]

static ? int Loyc.Collections.LCInterfaces.IndexOf< T > ( this INegListSource< T >  list,
item 
)
inlinestatic

Determines the index of a specific value.

Returns
The index of the value, if found, or null if it was not found.

At first, this method was a member of IListSource itself, just in case the source might have some kind of fast lookup logic (e.g. binary search) or custom comparer. However, since the item to find is an "in" argument, it would prevent IListSource from being marked covariant when I upgrade to C# 4.

References Loyc.Collections.INegListSource< out out T >.Max, and Loyc.Collections.INegListSource< out out T >.Min.

◆ IndexOf< T >() [2/2]

static int Loyc.Collections.LCInterfaces.IndexOf< T > ( this IReadOnlyList< T >  list,
item 
)
static

Determines the index of a specific value.

Returns
The index of the value, if found, or -1 if it was not found.

◆ IndexWhere< T >()

static int Loyc.Collections.LCInterfaces.IndexWhere< T > ( this IReadOnlyList< T >  source,
Func< T, bool >  pred 
)
static

Gets the lowest index at which a condition is true, or -1 if nowhere.

◆ Items< T >()

static IEnumerable<KeyValuePair<int, T> > Loyc.Collections.LCInterfaces.Items< T > ( this ISparseListSource< T >  list)
inlinestatic

Returns the non-cleared items in the sparse list, along with their indexes, sorted by index.

The returned sequence should exactly match the set of indexes for which list.IsSet(Key) returns true.

References Loyc.Collections.ISparseListSource< T >.NextHigherItem().

◆ LastIndexWhere< T >()

static int Loyc.Collections.LCInterfaces.LastIndexWhere< T > ( this IReadOnlyList< T >  source,
Func< T, bool >  pred 
)
static

Gets the highest index at which a condition is true, or -1 if nowhere.

◆ NextHigherIndex< T >()

static ? int Loyc.Collections.LCInterfaces.NextHigherIndex< T > ( this ISparseListSource< T >  list,
int?  index 
)
inlinestatic

Gets the next higher index that is not classified as an empty space, or null if there are no non-blank higher indexes.

This extension method works by calling NextHigherItem().

References Loyc.Collections.ISparseListSource< T >.NextHigherItem().

◆ NextLowerIndex< T >()

static ? int Loyc.Collections.LCInterfaces.NextLowerIndex< T > ( this ISparseListSource< T >  list,
int?  index 
)
inlinestatic

Gets the next lower index that is not classified as an empty space, or null if there are no non-blank lower indexes.

This extension method works by calling NextHigherItem().

References Loyc.Collections.ISparseListSource< T >.NextLowerItem().

◆ ReplaceIfPresent< K, V >()

static bool Loyc.Collections.LCInterfaces.ReplaceIfPresent< K, V > ( this IDictionaryEx< K, V >  dict,
key,
value 
)
inlinestatic

Replaces an item in the map if the key was already present. If the key was not already present, this method has no effect.

Returns
True if a value existed and was replaced, false if not.

References Loyc.Collections.IDictionaryEx< K, V >.GetAndEdit().

◆ SwapIfPresent< K, V >()

static Maybe<V> Loyc.Collections.LCInterfaces.SwapIfPresent< K, V > ( this IDictionaryEx< K, V >  dict,
key,
value 
)
inlinestatic

Replaces an item in the map if the key was already present. If the key was not already present, this method has no effect.

Returns
The old value if a value was replaced, or an empty value of Maybe<V> otherwise.

References Loyc.Collections.IDictionaryEx< K, V >.GetAndEdit().

◆ TryGet< T >() [1/3]

static bool Loyc.Collections.LCInterfaces.TryGet< T > ( this IListSource< T >  list,
int  index,
ref T  value 
)
inlinestatic

Tries to get a value from the list at the specified index.

Parameters
indexThe index to access. Valid indexes are between 0 and Count-1.
valueA variable that will be changed to the retrieved value. If the index is not valid, this variable is left unmodified.
Returns
True on success, or false if the index was not valid.

◆ TryGet< T >() [2/3]

static T Loyc.Collections.LCInterfaces.TryGet< T > ( this IListSource< T >  list,
int  index,
defaultValue 
)
static

Tries to get a value from the list at the specified index.

Parameters
indexThe index to access. Valid indexes are between 0 and Count-1.
defaultValueA value to return if the index is not valid.

◆ TryGet< T >() [3/3]

static bool Loyc.Collections.LCInterfaces.TryGet< T > ( this INegListSource< T >  list,
int  index,
ref T  value 
)
inlinestatic

Tries to get a value from the list at the specified index.

Parameters
indexThe index to access. Valid indexes are between Min and Max.
valueA variable that will be changed to the retrieved value. If the index is not valid, this variable is left unmodified.
Returns
True on success, or false if the index was not valid.