public static TryValue <T> ElementAtOrNone <T>(this IEnumerable <T> values, int index) { Contracts.Requires.That(values != null); Contracts.Requires.That(index >= 0); var readOnlyList = values as IReadOnlyList <T>; if (readOnlyList != null) { return(index.IsIn(readOnlyList.GetIndexRange()) ? TryValue.New(readOnlyList[index]) : TryValue.None <T>()); } var list = values as IList <T>; if (list != null) { return(index.IsIn(list.GetListIndexRange()) ? TryValue.New(list[index]) : TryValue.None <T>()); } int count = 0; foreach (var value in values) { if (count == index) { return(TryValue.New(value)); } count++; } return(TryValue.None <T>()); }
public static TryValue <T> SingleOrNone <T>(this IEnumerable <T> values, Func <T, bool> predicate) { Contracts.Requires.That(values != null); Contracts.Requires.That(predicate != null); return(values.Select(value => TryValue.New(value)).SingleOrDefault(value => predicate(value.Value))); }
public static async Task <TryValue <T> > TryGetValueLazyAsync <T>(this ConcurrentTypeSet <object> set) { Contracts.Requires.That(set != null); AsyncLazy <T> lazy; return(set.TryGetValue(out lazy) ? TryValue.New(await lazy) : TryValue.None <T>()); }
/// <inheritdoc /> public void Add(TIndex key, TValue value) { IDictionaryContracts.Add(this, key); // this method's contracts checks that the dictionary doesn't already contain this key // thus if it passes the contract a new value will always be added this.indexable[key] = TryValue.New(value); this.count++; }
public IndexableSerializerOptions(TIndex lowerBounds, TIndex dimensions) { Contracts.Requires.That(dimensions.IsAllPositiveOrZero()); this.LowerBoundsSerializer = new FixedConstantSerializer <TIndex>(lowerBounds, Endian.Little); this.DimensionsSerializer = new FixedConstantSerializer <TIndex>(dimensions, Endian.Little); this.ConstantLowerBounds = TryValue.New(lowerBounds); this.ConstantDimensions = TryValue.New(dimensions); }
public IndexableSerializerOptions( TIndex lowerBounds, IConstantSerializerDeserializer <TIndex> dimensionsSerializer) { Contracts.Requires.That(dimensionsSerializer != null); this.LowerBoundsSerializer = new FixedConstantSerializer <TIndex>( lowerBounds, dimensionsSerializer.Endianness); this.DimensionsSerializer = dimensionsSerializer; this.ConstantLowerBounds = TryValue.New(lowerBounds); this.ConstantDimensions = TryValue.None <TIndex>(); }
public IndexableSerializerOptions( IConstantSerializerDeserializer <TIndex> lowerBoundsSerializer, TIndex dimensions) { Contracts.Requires.That(lowerBoundsSerializer != null); Contracts.Requires.That(dimensions.IsAllPositiveOrZero()); this.LowerBoundsSerializer = lowerBoundsSerializer; this.DimensionsSerializer = new FixedConstantSerializer <TIndex>( dimensions, lowerBoundsSerializer.Endianness); this.ConstantLowerBounds = TryValue.None <TIndex>(); this.ConstantDimensions = TryValue.New(dimensions); }
/// <summary> /// Tries to asynchronously receives a value from a specified source. /// </summary> /// <typeparam name="TOutput">The type of data contained in the source to receive.</typeparam> /// <param name="source">The source from which to receive the value.</param> /// <returns> /// A task that represents the asynchronous receive operation. When an item value is successfully received from the source, /// the returned task is completed and its <see cref="Task{T}.Result"/> returns a successful <see cref="TryValue{TOutput}"/> /// containing the received value. If an item value cannot be retrieved because the source is empty and completed, the returned /// task is completed and its <see cref="Task{T}.Result"/> returns <see cref="TryValue.None()"/>. /// </returns> public static async Task <TryValue <TOutput> > TryReceiveAsync <TOutput>(this ISourceBlock <TOutput> source) { Contracts.Requires.That(source != null); try { return(TryValue.New(await source.ReceiveAsync().DontMarshallContext())); } catch (InvalidOperationException) { return(TryValue.None <TOutput>()); } }
/// <summary> /// Tries to asynchronously receives a value from a specified source. /// </summary> /// <typeparam name="TOutput">The type of data contained in the source to receive.</typeparam> /// <param name="source">The source from which to receive the value.</param> /// <param name="timeout"> /// The maximum time interval, in milliseconds, to wait for the synchronous operation to complete, /// or an interval that represents -1 milliseconds to wait indefinitely. /// </param> /// <param name="cancellation">The token to use to cancel the receive operation.</param> /// <returns> /// A task that represents the asynchronous receive operation. When an item value is successfully received from the source, /// the returned task is completed and its <see cref="Task{T}.Result"/> returns a successful <see cref="TryValue{TOutput}"/> /// containing the received value. If an item value cannot be retrieved because the source is empty and completed, the returned /// task is completed and its <see cref="Task{T}.Result"/> returns <see cref="TryValue.None()"/>. /// </returns> public static async Task <TryValue <TOutput> > TryReceiveAsync <TOutput>( this ISourceBlock <TOutput> source, TimeSpan timeout, CancellationToken cancellation) { Contracts.Requires.That(source != null); Contracts.Requires.That(timeout.IsDuration()); try { return(TryValue.New(await source.ReceiveAsync(timeout, cancellation).DontMarshallContext())); } catch (InvalidOperationException) { return(TryValue.None <TOutput>()); } }
public static TryValue <T> TryDeserialize <T>(this IValueKey <T> key, string value) { Contracts.Requires.That(key != null); T result; if (key.TryDeserialize(value, out result)) { return(TryValue.New(result)); } else { return(TryValue.None <T>()); } }
public static async Task <TryValue <ReleaseStruct> > TryWaitForUsingBlockAsync( this SemaphoreSlim semaphore, TimeSpan timeout, CancellationToken cancellation = default(CancellationToken)) { Contracts.Requires.That(semaphore != null); Contracts.Requires.That(Time.IsDuration(timeout)); if (await semaphore.WaitAsync(timeout, cancellation).DontMarshallContext()) { return(TryValue.New(new ReleaseStruct(semaphore))); } else { return(TryValue.None <ReleaseStruct>()); } }
/// <inheritdoc /> public TValue this[TIndex index] { // this method's contracts will check that the dictionary contains the key first get { IDictionaryContracts.IndexerGet(this, index); IReadOnlyIndexableContracts.IndexerGet(this, index); return(this.indexable[index].Value); } set { IDictionaryContracts.IndexerSet(this, index); IIndexableContracts.IndexerSet(this, index); if (!this.indexable[index].HasValue) { this.count++; } this.indexable[index] = TryValue.New(value); } }
public static TryValue <T> SingleOrNone <T>(this IEnumerable <T> values) { Contracts.Requires.That(values != null); return(values.IsEmpty() ? TryValue.None <T>() : TryValue.New(values.Single())); }