public MemoryChainStateStorage(ChainedHeader chainTip = null, int?unspentTxCount = null, int?totalTxCount = null, int?totalInputCount = null, int?totalOutputCount = null, int?unspentOutputCount = null, ImmutableSortedDictionary <UInt256, ChainedHeader> headers = null, ImmutableSortedDictionary <UInt256, UnspentTx> unspentTransactions = null, ImmutableDictionary <int, BlockSpentTxes> blockSpentTxes = null, ImmutableDictionary <UInt256, IImmutableList <UnmintedTx> > blockUnmintedTxes = null) { this.chainTip = CommittedRecord <ChainedHeader> .Initial(chainTip); this.unspentTxCount = CommittedRecord <int> .Initial(unspentTxCount ?? 0); this.unspentOutputCount = CommittedRecord <int> .Initial(unspentOutputCount ?? 0); this.totalTxCount = CommittedRecord <int> .Initial(totalTxCount ?? 0); this.totalInputCount = CommittedRecord <int> .Initial(totalInputCount ?? 0); this.totalOutputCount = CommittedRecord <int> .Initial(totalOutputCount ?? 0); this.headers = CommittedRecord <ImmutableSortedDictionary <UInt256, ChainedHeader> .Builder> .Initial( headers?.ToBuilder() ?? ImmutableSortedDictionary.CreateBuilder <UInt256, ChainedHeader>()); this.unspentTransactions = CommittedRecord <ImmutableSortedDictionary <UInt256, UnspentTx> .Builder> .Initial( unspentTransactions?.ToBuilder() ?? ImmutableSortedDictionary.CreateBuilder <UInt256, UnspentTx>()); this.unspentTxOutputs = CommittedRecord <ImmutableSortedDictionary <TxOutputKey, TxOutput> .Builder> .Initial( ImmutableSortedDictionary.CreateBuilder <TxOutputKey, TxOutput>()); this.blockSpentTxes = CommittedRecord <ImmutableDictionary <int, BlockSpentTxes> .Builder> .Initial( blockSpentTxes?.ToBuilder() ?? ImmutableDictionary.CreateBuilder <int, BlockSpentTxes>()); this.blockUnmintedTxes = CommittedRecord <ImmutableDictionary <UInt256, IImmutableList <UnmintedTx> > .Builder> .Initial( blockUnmintedTxes?.ToBuilder() ?? ImmutableDictionary.CreateBuilder <UInt256, IImmutableList <UnmintedTx> >()); }
public MemoryChainStateStorage(Chain chain = null, ImmutableSortedDictionary <UInt256, UnspentTx> unspentTransactions = null, ImmutableDictionary <int, IImmutableList <SpentTx> > blockSpentTxes = null) { this.chain = chain != null?chain.ToBuilder() : new ChainBuilder(); this.unspentTransactions = unspentTransactions != null?unspentTransactions.ToBuilder() : ImmutableSortedDictionary.CreateBuilder <UInt256, UnspentTx>(); this.blockSpentTxes = blockSpentTxes != null?blockSpentTxes.ToBuilder() : ImmutableDictionary.CreateBuilder <int, IImmutableList <SpentTx> >(); }
public static ImmutableSortedDictionary <K, V> Update <K, V>(this ImmutableSortedDictionary <K, V> dict, K key, V value) { var builder = dict.ToBuilder(); builder.Remove(key); builder.Add(key, value); return(builder.ToImmutable()); }
internal override void SnapshotLoaded(Snapshot snapshot) { base.SnapshotLoaded(snapshot); var builder = unconfirmed.ToBuilder(); foreach (var delivery in snapshot.DeliveryAttempts) { builder[delivery.DeliveryId] = delivery; } unconfirmed = builder.ToImmutable(); }
internal static IDictionary <TKey, TValue> ToBuilder <TKey, TValue>(this IImmutableDictionary <TKey, TValue> dictionary) where TKey : notnull { return(dictionary switch { ImmutableDictionary <TKey, TValue> d => d.ToBuilder(), ImmutableSortedDictionary <TKey, TValue> d => d.ToBuilder(), ImmutableSegmentedDictionary <TKey, TValue> d => d.ToBuilder(), null => throw new ArgumentNullException(nameof(dictionary)), _ => throw ExceptionUtilities.UnexpectedValue(dictionary), });
/// <summary> /// Appends the tags. /// </summary> /// <param name="writer">The writer.</param> /// <param name="pointSettings">The point settings.</param> private void AppendTags(StringBuilder writer, PointSettings pointSettings) { IReadOnlyDictionary <string, string> entries; if (pointSettings == null) { entries = _tags; } else { IReadOnlyDictionary <string, string> defaultTags = pointSettings.GetDefaultTags(); try { entries = _tags.AddRange(defaultTags); } catch (ArgumentException) { // Most cases don't expect to override existing content // override don't consider as best practice // therefore it a trade-off between being less efficient // on the default behavior or on the override scenario var builder = _tags.ToBuilder(); foreach (var item in defaultTags) { var name = item.Key; if (!builder.ContainsKey(name)) // existing tags overrides { builder.Add(name, item.Value); } } entries = builder; } } foreach (var keyValue in entries) { var key = keyValue.Key; var value = keyValue.Value; if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) { continue; } writer.Append(','); EscapeKey(writer, key); writer.Append('='); EscapeKey(writer, value); } writer.Append(' '); }
static void Main(string[] args) { ImmutableSortedDictionary <int, int> isd = ImmutableSortedDictionary.Create <int, int>(); isd = isd.Add(1, 1); isd = isd.Add(2, 2); isd = isd.Remove(2); ImmutableSortedDictionary <int, int> .Builder isdBuilder = isd.ToBuilder(); isdBuilder.Add(10, 10); //adds to original SortedDictionary. returns void. ImmutableSortedDictionary <int, int> .Builder builder = ImmutableSortedDictionary.CreateBuilder <int, int>(); builder.Add(3, 3); isd = builder.ToImmutable(); }
public static ImmutableSortedDictionary <string, HoistAction> RemoveCycles(ImmutableSortedDictionary <string, HoistAction> edges) { ImmutableSortedDictionary <string, HoistAction> .Builder builder = edges.ToBuilder(); foreach (KeyValuePair <string, HoistAction> currentEdge in edges) { string targetKey = FindCycle(builder, currentEdge); if (targetKey != null) { HoistAction nextKey; while (builder.TryGetValue(targetKey, out nextKey)) { builder.Remove(targetKey); targetKey = nextKey.Becomes; } } } return(builder.ToImmutable()); }
public static ImmutableSortedDictionary <string, HoistAction> ApplyTransitiveProperty(ImmutableSortedDictionary <string, HoistAction> edges) { ImmutableSortedDictionary <string, HoistAction> .Builder builder = edges.ToBuilder(); var keys = new List <KeyValuePair <string, HoistAction> >(); foreach (string startingKey in edges.Keys) { keys.Clear(); string currentKey = startingKey; HoistAction currentValue; while (builder.TryGetValue(currentKey, out currentValue)) { keys.Add(Pair.Make(currentKey, currentValue)); currentKey = currentValue.Becomes; } Debug.Assert(keys.Count > 0); if (keys.Count <= 1) { // No transitive behavior to apply continue; } string becomes = keys[keys.Count - 1].Value.Becomes; int addedRanks = keys[keys.Count - 1].Value.AddedRanks; for (int idx = keys.Count - 2; idx >= 0; --idx) { KeyValuePair <string, HoistAction> key = keys[idx]; addedRanks += key.Value.AddedRanks; builder[key.Key] = new HoistAction(becomes, addedRanks); } } return(builder.ToImmutable()); }