public Cache(ImmutableList <Record> records) { if (records.IsEmpty) { _observerRowsMap = ImmutableDictionary.Create <UniqueAddress, ImmutableDictionary <UniqueAddress, Record> >(); _allTerminated = ImmutableHashSet.Create <UniqueAddress>(); _allUnreachable = ImmutableHashSet.Create <UniqueAddress>(); } else { var mapBuilder = new Dictionary <UniqueAddress, ImmutableDictionary <UniqueAddress, Record> >(); var terminatedBuilder = ImmutableHashSet.CreateBuilder <UniqueAddress>(); var unreachableBuilder = ImmutableHashSet.CreateBuilder <UniqueAddress>(); foreach (var r in records) { ImmutableDictionary <UniqueAddress, Record> m; if (mapBuilder.TryGetValue(r.Observer, out m)) { m = m.SetItem(r.Subject, r); } else { //TODO: Other collections take items for Create. Create unnecessary array here m = ImmutableDictionary.CreateRange(new[] { new KeyValuePair <UniqueAddress, Record>(r.Subject, r) }); } mapBuilder.AddOrSet(r.Observer, m); if (r.Status == ReachabilityStatus.Unreachable) { unreachableBuilder.Add(r.Subject); } else if (r.Status == ReachabilityStatus.Terminated) { terminatedBuilder.Add(r.Subject); } } _observerRowsMap = ImmutableDictionary.CreateRange(mapBuilder); _allTerminated = terminatedBuilder.ToImmutable(); _allUnreachable = unreachableBuilder.ToImmutable().Except(AllTerminated); } _allUnreachableOrTerminated = _allTerminated.IsEmpty ? AllUnreachable : AllUnreachable.Union(AllTerminated); }
/// <summary> /// TBD /// </summary> /// <param name="records">TBD</param> public Cache(ImmutableList <Record> records) { if (records.IsEmpty) { ObserverRowMap = ImmutableDictionary <UniqueAddress, ImmutableDictionary <UniqueAddress, Record> > .Empty; AllTerminated = ImmutableHashSet <UniqueAddress> .Empty; AllUnreachable = ImmutableHashSet <UniqueAddress> .Empty; } else { var mapBuilder = new Dictionary <UniqueAddress, ImmutableDictionary <UniqueAddress, Record> >(); var terminatedBuilder = ImmutableHashSet.CreateBuilder <UniqueAddress>(); var unreachableBuilder = ImmutableHashSet.CreateBuilder <UniqueAddress>(); foreach (var r in records) { ImmutableDictionary <UniqueAddress, Record> m = mapBuilder.TryGetValue(r.Observer, out var mR) ? mR.SetItem(r.Subject, r) : ImmutableDictionary <UniqueAddress, Record> .Empty.Add(r.Subject, r); mapBuilder[r.Observer] = m; if (r.Status == ReachabilityStatus.Unreachable) { unreachableBuilder.Add(r.Subject); } else if (r.Status == ReachabilityStatus.Terminated) { terminatedBuilder.Add(r.Subject); } } ObserverRowMap = ImmutableDictionary.CreateRange(mapBuilder); AllTerminated = terminatedBuilder.ToImmutable(); AllUnreachable = unreachableBuilder.ToImmutable().Except(AllTerminated); } AllUnreachableOrTerminated = AllTerminated.IsEmpty ? AllUnreachable : AllUnreachable.Union(AllTerminated); }