Beispiel #1
0
        public void FindElementIn100()
        {
            tml.setTestCaseName("Find elements in 100");

            ImmutableSortedDictionary <Payload, Payload> tree = this.creatASListWithNelement(100);
            Payload toBeFound_25 = new Payload("Load 25");
            Payload toBeFound_50 = new Payload("Load 50");
            Payload toBeFound_75 = new Payload("Load 75");

            Payload toBeFound_100 = new Payload("Load 99");

            Assert.AreEqual(100, tree.Count);
            tml.setInitialTimeAndMemory();
            ImmutableSortedDictionary <Payload, Payload> removedList = tree.Remove(toBeFound_25);//removes element at 25

            Assert.AreEqual(99, removedList.Count);
            tml.logTimeAndMemoryUsage(25);
            removedList = tree.Remove(toBeFound_50);
            Assert.AreEqual(99, removedList.Count);
            tml.logTimeAndMemoryUsage(50);
            removedList = tree.Remove(toBeFound_75);
            Assert.AreEqual(99, removedList.Count);
            tml.logTimeAndMemoryUsage(75);
            removedList = tree.Remove(toBeFound_100);
            Assert.AreEqual(99, removedList.Count);
            tml.logTimeAndMemoryUsage(100);
        }
Beispiel #2
0
        /// <summary>
        /// Call this method when a message has been confirmed by the destination,
        /// or to abort re-sending.
        /// </summary>
        /// <param name="deliveryId">TBD</param>
        /// <returns>True the first time the <paramref name="deliveryId"/> is confirmed, false for duplicate confirmations.</returns>
        public bool ConfirmDelivery(long deliveryId)
        {
            var before = _unconfirmed;

            _unconfirmed = _unconfirmed.Remove(deliveryId);
            return(_unconfirmed.Count < before.Count);
        }
Beispiel #3
0
 public static ImmutableSortedDictionary <int, int> .Builder Decrease(this ImmutableSortedDictionary <int, int> .Builder b, int f)
 {
     if (--b[f] == 0)
     {
         b.Remove(f);
     }
     return(b);
 }
Beispiel #4
0
        /// <summary>
        /// Call this method when a message has been confirmed by the destination,
        /// or to abort re-sending.
        /// </summary>
        /// <param name="deliveryId">TBD</param>
        /// <returns>True the first time the <paramref name="deliveryId"/> is confirmed, false for duplicate confirmations.</returns>
        public bool ConfirmDelivery(long deliveryId)
        {
            var before = _unconfirmed;

            _unconfirmed = _unconfirmed.Remove(deliveryId);
            if (_unconfirmed.IsEmpty)
            {
                Cancel();
            }
            return(_unconfirmed.Count < before.Count);
        }
Beispiel #5
0
        public void Remove(string projectPath)
        {
            if (string.IsNullOrWhiteSpace(projectPath))
            {
                return;
            }

            string key = projectPath.GetFullConsolidatedPath();

            projects = projects.Remove(key);
        }
Beispiel #6
0
        internal DocumentSet WithDocumentRemoved(DocumentReference docRef)
        {
            _keyIndex.TryGetValue(docRef, out var document);
            if (document == null)
            {
                return(this);
            }

            var newKeyIndex  = _keyIndex.Remove(docRef);
            var newSortedSet = _sortedSet.Remove(document);

            return(new DocumentSet(newKeyIndex, newSortedSet));
        }
        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();
        }
Beispiel #8
0
        /// <summary>
        /// Filters the provided <paramref name="values"/> according to this <see cref="BinaryComparison"/>
        /// and the specified <paramref name="reference"/> value. The <paramref name="reference"/> value is
        /// used as the RIGHT side of the binary comparison. Consider the binary comparison is LessThan and
        /// we call Filter(values, 42). We're looking for keys that are less than 42.
        /// </summary>
        public static Tuple <ImmutableSortedDictionary <TKey, TValue>, ImmutableSortedDictionary <TKey, TValue> > SplitBy <TKey, TValue>(
            this BinaryComparison comparison,
            ImmutableSortedDictionary <TKey, TValue> values,
            TKey reference
            )
        {
            var matches = ImmutableSortedDictionary <TKey, TValue> .Empty;
            var removed = ImmutableSortedDictionary <TKey, TValue> .Empty;

            if (comparison.Type == ExpressionType.NotEqual)
            {
                var match = values.Remove(reference);
                removed = BinaryComparison.Equal.Filter(values, reference);
                return(Tuple.Create(match, removed));
            }

            if (comparison.Type == ExpressionType.Equal)
            {
                TValue value;
                if (values.TryGetValue(reference, out value))
                {
                    matches = matches.Add(reference, value);
                    removed = BinaryComparison.NotEqual.Filter(values, reference);
                    return(Tuple.Create(matches, removed));
                }

                // no matches
                return(Tuple.Create(ImmutableSortedDictionary <TKey, TValue> .Empty, values));
            }

            var evaluator = comparison.GetEvaluator <TKey>();

            foreach (var kvp in values)
            {
                if (evaluator(kvp.Key, reference))
                {
                    matches = matches.Add(kvp.Key, kvp.Value);
                }
                else
                {
                    removed = removed.Add(kvp.Key, kvp.Value);
                }
            }

            return(Tuple.Create(matches, removed));
        }
Beispiel #9
0
        /// <summary>
        /// Filters the provided <paramref name="values"/> according to this <see cref="BinaryComparison"/>
        /// and the specified <paramref name="reference"/> value. The <paramref name="reference"/> value is
        /// used as the RIGHT side of the binary comparison. Consider the binary comparison is LessThan and
        /// we call Filter(values, 42). We're looking for keys that are less than 42.
        /// </summary>
        public static ImmutableSortedDictionary <TKey, TValue> Filter <TKey, TValue>(
            this BinaryComparison comparison,
            ImmutableSortedDictionary <TKey, TValue> values,
            TKey reference
            )
        {
            if (comparison.Type == ExpressionType.NotEqual)
            {
                return(values.Remove(reference));
            }

            var result = ImmutableSortedDictionary <TKey, TValue> .Empty;

            if (comparison.Type == ExpressionType.Equal)
            {
                TValue value;
                if (values.TryGetValue(reference, out value))
                {
                    result = result.Add(reference, value);
                }

                return(result);
            }

            // since we're enumerating a sorted collection, once we receive
            // a mismatch it means we'll never again receive a match
            var breakAfterFailure =
                comparison == BinaryComparison.LessThanOrEqual ||
                comparison == BinaryComparison.LessThanOrEqual;

            var evaluator = comparison.GetEvaluator <TKey>();

            foreach (var kvp in values)
            {
                if (evaluator(kvp.Key, reference))
                {
                    result = result.Add(kvp.Key, kvp.Value);
                }
                else if (breakAfterFailure)
                {
                    break;
                }
            }

            return(result);
        }
Beispiel #10
0
        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());
        }
Beispiel #11
0
    void Test()
    {
        ImmutableSortedDictionary <int, string> sortedDictionary = ImmutableSortedDictionary <int, string> .Empty;

        sortedDictionary = sortedDictionary.Add(10, "Ten");
        sortedDictionary = sortedDictionary.Add(21, "Twenty-One");
        sortedDictionary = sortedDictionary.SetItem(10, "Diez");

        // Displays "10Diez" followed by "21Twenty-One".
        foreach (KeyValuePair <int, string> item in sortedDictionary)
        {
            Trace.WriteLine(item.Key + item.Value);
        }

        string ten = sortedDictionary[10];

        // ten == "Diez"

        sortedDictionary = sortedDictionary.Remove(21);
    }
Beispiel #12
0
        public void Remove_EmptyDictionary_DoesNothing()
        {
            ImmutableSortedDictionary <int, string> dictionary = ImmutableSortedDictionary <int, string> .Empty;

            Assert.Equal(0, dictionary.Remove(2).Count);
        }
 public DictionaryDatum Remove(Datum k)
 {
     return(new DictionaryDatum(dict.Remove(k)));
 }
 private void Confirm(string deliveryId) => unconfirmed = unconfirmed.Remove(deliveryId);
        static void Main(string[] args)
        {
            // 不可变栈和队列
            ImmutableStack <int> stack = ImmutableStack <int> .Empty;

            stack = stack.Push(13);
            stack = stack.Push(7);
            foreach (int item in stack)
            {
                Console.WriteLine(item);
            }
            int lastItem;

            stack = stack.Pop(out lastItem);
            Console.WriteLine(lastItem);

            // 队列和栈类似
            ImmutableQueue <int> queue = ImmutableQueue <int> .Empty;

            queue = queue.Enqueue(13);
            queue = queue.Enqueue(7);
            // 在7之前显示13
            foreach (int item in queue)
            {
                Console.WriteLine(item);
            }
            int nextItem;

            queue = queue.Dequeue(out nextItem);
            // 只显示13
            Console.WriteLine(nextItem);


            //不可变列表
            ImmutableList <int> list = ImmutableList <int> .Empty;

            list = list.Insert(0, 13);
            list = list.Insert(0, 7);
            // 在13之前显示7
            foreach (int item in list)
            {
                Console.WriteLine(item);
            }
            list = list.RemoveAt(1);

            //不可变set
            //hash的
            ImmutableHashSet <int> hashSet = ImmutableHashSet <int> .Empty;

            hashSet = hashSet.Add(13);
            hashSet = hashSet.Add(7);
            // 以不可预知的顺序显示7和13
            foreach (int item in hashSet)
            {
                Console.WriteLine(item);
            }
            hashSet = hashSet.Remove(7);
            //sorted
            ImmutableSortedSet <int> sortedSet = ImmutableSortedSet <int> .Empty;

            sortedSet = sortedSet.Add(13);
            sortedSet = sortedSet.Add(7);
            // 在13之前显示7
            foreach (int item in sortedSet)
            {
                Console.WriteLine(item);
            }
            int smallestItem = sortedSet[0];

            // smallestItem == 7
            sortedSet = sortedSet.Remove(7);

            //不可变Dictionary
            //无排序
            ImmutableDictionary <int, string> dictionary =
                ImmutableDictionary <int, string> .Empty;

            dictionary = dictionary.Add(10, "Ten");
            dictionary = dictionary.Add(21, "Twenty-One");
            dictionary = dictionary.SetItem(10, "Diez");
            // 以不可预知的顺序显示10Diez和21Twenty-One
            foreach (KeyValuePair <int, string> item in dictionary)
            {
                Console.WriteLine(item.Key + item.Value);
            }
            string ten = dictionary[10];

            // ten == "Diez"
            dictionary = dictionary.Remove(21);
            //有排序
            ImmutableSortedDictionary <int, string> sortedDictionary =
                ImmutableSortedDictionary <int, string> .Empty;

            sortedDictionary = sortedDictionary.Add(10, "Ten");
            sortedDictionary = sortedDictionary.Add(21, "Twenty-One");
            sortedDictionary = sortedDictionary.SetItem(10, "Diez");
            // 在21Twenty-One之前显示10Diez
            foreach (KeyValuePair <int, string> item in sortedDictionary)
            {
                Console.WriteLine(item.Key + item.Value);
            }
            ten = sortedDictionary[10];
            // ten == "Diez"
            sortedDictionary = sortedDictionary.Remove(21);

            //线程安全的字典
            var    cd        = new ConcurrentDictionary <int, string>();
            string newvalue  = cd.AddOrUpdate(0, key => "Zero", (key, oldvalue) => "Zero");
            bool   keyExists = cd.TryGetValue(0, out string currentvalue);

            if (keyExists)
            {
                Console.WriteLine(currentvalue);
            }
            bool keyExisted = cd.TryRemove(0, out currentvalue);
            {
                Console.WriteLine(currentvalue);
            }

            //阻塞队列
            BlockingCollection <int> blockQueue = new BlockingCollection <int>();

            blockQueue.Add(7);
            blockQueue.Add(13);
            blockQueue.CompleteAdding(); //标记完成
            //消费者消费
            foreach (int item in blockQueue.GetConsumingEnumerable())
            {
                Console.WriteLine(item);
            }

            // 异步队列
            Channel <int> channelQueue = Channel.CreateUnbounded <int>();
            //生产者代码
            ChannelWriter <int> writer = channelQueue.Writer;
            var t  = writer.WriteAsync(7);
            var t2 = writer.WriteAsync(13);

            writer.Complete();
            Task.WhenAll(t.AsTask(), t2.AsTask()).Wait();
            //消费者代码
            ChannelReader <int> reader = channelQueue.Reader;

            ReaderCode(reader).Wait();
            //使用TPL完成
            var asyncQueue = new BufferBlock <int>();
            //生产者代码
            var t3 = asyncQueue.SendAsync(7);
            var t4 = asyncQueue.SendAsync(13);

            asyncQueue.Complete();
            Task.WhenAll(t3, t4).Wait();
            //消费者代码
            TPLReaderCode(asyncQueue).Wait();


            //节流队列的几种创建方式
            Channel <int>     cq = Channel.CreateBounded <int>(1);
            BufferBlock <int> b  = new BufferBlock <int>(new DataflowBlockOptions {
                BoundedCapacity = 1
            });
            BlockingCollection <int> bb = new BlockingCollection <int>(boundedCapacity: 1);

            //采样队列的几种创建方式
            cq = Channel.CreateBounded <int>(new BoundedChannelOptions(1)
            {
                FullMode = BoundedChannelFullMode.DropOldest
            });

            //使用ActionBlock来定义阻塞异步队列
            BlockAsyncQueue().Wait();
        }