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 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>()); }
public IndexableSerializerOptions(IConstantSerializerDeserializer <TIndex> indexSerializer) { Contracts.Requires.That(indexSerializer != null); this.LowerBoundsSerializer = indexSerializer; this.DimensionsSerializer = indexSerializer; this.ConstantLowerBounds = TryValue.None <TIndex>(); this.ConstantDimensions = TryValue.None <TIndex>(); }
/// <inheritdoc /> public async Task <TryValue <T> > TryGetAsync <T>( IValueKey <T> key, CancellationToken cancellation = default(CancellationToken)) { IKeyValueStoreContracts.TryGetAsync(key); cancellation.ThrowIfCancellationRequested(); var result = await this.store.TryGetAsync(key.Key, cancellation).DontMarshallContext(); return(result.HasValue ? key.TryDeserialize(result.Value.Value) : TryValue.None <T>()); }
/// <inheritdoc /> public void Clear() { ICollectionContracts.Clear(this); foreach (var entry in this.indexable) { this.indexable[entry.Key] = TryValue.None <TValue>(); } this.count = 0; }
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>()); } }
/// <inheritdoc /> public bool Remove(KeyValuePair <TIndex, TValue> item) { ICollectionContracts.Remove(this); if (!this.Contains(item)) { return(false); } this.indexable[item.Key] = TryValue.None <TValue>(); this.count--; return(true); }
/// <inheritdoc /> public bool Remove(TIndex key) { IDictionaryContracts.Remove(this, key); if (!this.ContainsKey(key)) { return(false); } this.indexable[key] = TryValue.None <TValue>(); this.count--; return(true); }
/// <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>()); } }
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())); }