コード例 #1
0
        protected override void Run()
        {
            LFUCache <int, string> cache = new LFUCache <int, string>(3);

            Console.WriteLine("Adding p1");
            cache.Set(1, "p1");
            PrintTables(cache);
            Console.WriteLine("Adding p2");
            cache.Set(2, "p2");
            PrintTables(cache);
            Console.WriteLine("Adding p3");
            cache.Set(3, "p3");
            PrintTables(cache);
            Console.WriteLine("Getting p1");
            cache.Get(1);
            PrintTables(cache);
            Console.WriteLine("Getting p3");
            cache.Get(3);
            PrintTables(cache);
            Console.WriteLine("Getting p3");
            cache.Get(3);
            PrintTables(cache);
            Console.WriteLine("Adding p4");
            cache.Set(4, "p4");
            PrintTables(cache);

            Console.WriteLine("Getting p4");
            cache.Get(4);
            PrintTables(cache);

            Console.WriteLine("Adding p5");
            cache.Set(5, "p5");
            PrintTables(cache);
        }
コード例 #2
0
        public void RunProblem()
        {
            var lfu = new LFUCache(2);

            lfu.Put(1, 1);
            lfu.Put(2, 2);
            var v = lfu.Get(1);

            lfu.Put(3, 3);
            v = lfu.Get(2);
        }
コード例 #3
0
        public void ShouldNotEvict()
        {
            var cache = new LFUCache(2);

            Assert.Equal(-1, cache.Get(2));
            cache.Put(2, 6);
            Assert.Equal(-1, cache.Get(1));
            cache.Put(1, 5);
            cache.Put(1, 2);
            Assert.Equal(2, cache.Get(1));
            Assert.Equal(6, cache.Get(2));
        }
コード例 #4
0
        public void Acceptance()
        {
            var lfu = new LFUCache <int, int>(2);

            lfu.Put(3, 1);
            lfu.Put(2, 1);
            lfu.Put(2, 2);    // replace key 2
            lfu.Put(4, 4);    // evicts key 3

            Assert.False(lfu.TryGet(3, out var _));
            Assert.Equal(4, lfu.Get(4));
            Assert.Equal(2, lfu.Get(2));
        }
コード例 #5
0
        public void GotTimeout()
        {
            //["LFUCache","put","put","put","get","put","put","get","put","put","get","put","get","get","get","put","put","get","put","get"]
            //[[10],[7,28],[7,1],[8,15],[6],[10,27],[8,10],[8],[6,29],[1,9],[6],[10,7],[1],[2],[13],[8,30],[1,5],[1],[13,2],[12]]

            var cache    = new LFUCache(10);
            var commands = new []
            {
                "put", "put", "put", "get", "put", "put", "get", "put", "put", "get", "put", "get", "get", "get", "put",
                "put", "get", "put", "get"
            };
            var parameters = new int[, ]
            {
                { 7, 28 }, { 7, 1 }, { 8, 15 }, { 6, -1 }, { 10, 27 }, { 8, 10 }, { 8, -1 }, { 6, 29 }, { 1, 9 }, { 6, -1 }, { 10, 7 }, { 1, -1 }, { 2, -1 }, { 13, -1 },
                { 8, 30 }, { 1, 5 }, { 1, -1 }, { 13, 2 }, { 12, -1 }
            };

            for (int i = 0; i < commands.Length; i++)
            {
                var command = commands[i];
                switch (command)
                {
                case "put":
                    cache.Put(parameters[i, 0], parameters[i, 1]);
                    break;

                case "get":
                    cache.Get(parameters[i, 0]);
                    break;
                }
            }
        }
コード例 #6
0
        public void Example3()
        {
            var lfu = new LFUCache <int, int>(0, -1);

            lfu.Put(0, 0);

            Assert.AreEqual(-1, lfu.Get(0));
        }
コード例 #7
0
        public void UpdateElement()
        {
            var cache = new LFUCache(2);

            cache.Put(3, 1);
            cache.Put(2, 1);
            cache.Put(2, 2);
            cache.Put(4, 4);
            Assert.Equal(2, cache.Get(2));
        }
コード例 #8
0
        public static void DownloadImage(String url, ResultHandler resultHandler, [CallerMemberName] string caller = null)
        {
            String urll = url.ToLower().Trim();

            try
            {
                if (cachedImage.Contains(urll))
                {
                    resultHandler(cachedImage.Get(urll));
                    return;
                }
                new Task(() =>
                {
                    using (WebClient client = new WebClient())
                    {
                        Console.WriteLine(caller + " downloading sticker...");
                        byte[] data = client.DownloadData(url);

                        BitmapImage bitmap = new BitmapImage();
                        bitmap.BeginInit();
                        bitmap.StreamSource = new MemoryStream(data);
                        bitmap.EndInit();

                        if (resultHandler != null)
                        {
                            bitmap.Freeze();
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                cachedImage.AddReplace(urll, bitmap);
                                resultHandler(bitmap);
                            });
                        }
                    }
                }).Start();
            } catch (Exception e)
            {
            }
        }
コード例 #9
0
        private static void Run()
        {
            var cache = new LFUCache(2); // capacity

            cache.Put(1, 1);
            cache.Put(2, 2);
            cache.Get(1);       // returns 1
            cache.Put(3, 3);    // evicts key 2
            cache.Get(2);       // returns -1 (not found)
            cache.Get(3);       // returns 3.
            cache.Put(4, 4);    // evicts key 1.
            cache.Get(1);       // returns -1 (not found)
            cache.Get(3);       // returns 3
            cache.Get(4);       // returns 4
        }
コード例 #10
0
        public void Run()
        {
            LFUCache cache = new LFUCache(2 /* capacity (缓存容量) */);

            cache.Put(1, 1);
            cache.Put(2, 2);
            cache.Get(1);       // 返回 1
            cache.Put(3, 3);    // 去除 key 2
            cache.Get(2);       // 返回 -1 (未找到key 2)
            cache.Get(3);       // 返回 3
            cache.Put(4, 4);    // 去除 key 1
            cache.Get(1);       // 返回 -1 (未找到 key 1)
            cache.Get(3);       // 返回 3
            cache.Get(4);       // 返回 4
        }
コード例 #11
0
        public void GivenLFUCache_Put_ShouldPutKeyValuePairIntoTheCache()
        {
            var cache = new LFUCache(2);

            cache.Put(1, 1);
            cache.Put(2, 2);
            Assert.IsTrue(cache.Get(1) == 1);  // returns 1
            cache.Put(3, 3);                   // evicts key 2
            Assert.IsTrue(cache.Get(2) == -1); // returns -1 (not found)
            Assert.IsTrue(cache.Get(3) == 3);  // returns 3.
            cache.Put(4, 4);                   // evicts key 1.
            Assert.IsTrue(cache.Get(1) == -1); // returns -1 (not found)
            Assert.IsTrue(cache.Get(3) == 3);  // returns 3
            Assert.IsTrue(cache.Get(4) == 4);  // returns 4
        }
コード例 #12
0
        public void Example2()
        {
            var lfu = new LFUCache <int, int>(3, -1);

            lfu.Put(2, 2);
            lfu.Put(1, 1);

            Assert.AreEqual(2, lfu.Get(2));
            Assert.AreEqual(1, lfu.Get(1));
            Assert.AreEqual(2, lfu.Get(2));

            lfu.Put(3, 3);
            lfu.Put(4, 4);

            Assert.AreEqual(-1, lfu.Get(3));
            Assert.AreEqual(2, lfu.Get(2));
            Assert.AreEqual(1, lfu.Get(1));
            Assert.AreEqual(4, lfu.Get(4));
        }
コード例 #13
0
ファイル: LFUCacheTests.cs プロジェクト: tsalaka/Problems
        public void TestCase()
        {
            var cache = new LFUCache(2);

            cache.Put(1, 1);
            cache.Put(2, 2);
            Assert.AreEqual(1, cache.Get(1));

            cache.Put(3, 3);
            Assert.AreEqual(-1, cache.Get(2));
            Assert.AreEqual(3, cache.Get(3));
            cache.Put(4, 4);
            Assert.AreEqual(-1, cache.Get(1));
            Assert.AreEqual(3, cache.Get(3));
            Assert.AreEqual(4, cache.Get(4));
        }
コード例 #14
0
    public static void Main()
    {
        var cache = new LFUCache(2);

        cache.Put(1, 1);
        cache.Put(2, 2);
        var value = cache.Get(1);

        cache.Put(3, 3);
        value = cache.Get(2);
        value = cache.Get(3);
        cache.Put(4, 4);
        value = cache.Get(1);
        value = cache.Get(3);
        value = cache.Get(4);
    }
コード例 #15
0
        public void Basic()
        {
            var cache = new LFUCache(2);

            cache.Put(1, 1);
            cache.Put(2, 2);
            Assert.Equal(1, cache.Get(1));

            cache.Put(3, 3);                // evicts key 2
            Assert.Equal(-1, cache.Get(2)); // returns -1 (not found)

            Assert.Equal(3, cache.Get(3));  // returns 3.
            cache.Put(4, 4);                // evicts key 1.
            Assert.Equal(-1, cache.Get(1)); // returns -1 (not found)
            Assert.Equal(3, cache.Get(3));  // returns 3
            Assert.Equal(4, cache.Get(4));  // returns 4
        }
コード例 #16
0
        public void LFUCacheTest()
        {
            //    ["LFUCache","put","put","get","put","get","get","put","get","get","get"]
            //      [[2],[1,1],[2,2],[1],[3,3],[2],[3],[4,4],[1],[3],[4]]

            int result;

            LFUCache lfuCache = new LFUCache(2);

            lfuCache.Put(1, 1);
            lfuCache.Put(2, 2);

            result = lfuCache.Get(1);

            lfuCache.Put(3, 3);
            result = lfuCache.Get(2);
            result = lfuCache.Get(3);

            lfuCache.Put(4, 4);

            result = lfuCache.Get(1);
            result = lfuCache.Get(3);
            result = lfuCache.Get(4);
            //result = lfuCache.Get(3);

            //    Test case 2:

            //    ["LFUCache","put","put","put","put","get"]
            //[[2],[3,1],[2,1],[2,2],[4,4],[2]]
            lfuCache = new LFUCache(2);

            lfuCache.Put(3, 1);
            lfuCache.Put(2, 1);
            lfuCache.Put(2, 2);
            lfuCache.Put(2, 4);

            result = lfuCache.Get(2);
        }
コード例 #17
0
ファイル: Program.cs プロジェクト: nvnamsss/leetcode
        static void Main(string[] args)
        {
            LFUCache cache = new LFUCache(3);

            cache.Put(1, 1);
            int rs = cache.Get(1);

            rs = cache.Get(1);
            rs = cache.Get(1);
            Console.WriteLine($"Result: {rs} - Expect: {1}");
            cache.Put(2, 2);
            cache.Put(3, 3);
            cache.Put(4, 4);
            rs = cache.Get(2);
            Console.WriteLine($"Result: {rs} - Expect: {-1}");
            rs = cache.Get(4);
            Console.WriteLine($"Result: {rs} - Expect: {4}");
            rs = cache.Get(4);
            rs = cache.Get(4);
            rs = cache.Get(3);
            Console.WriteLine($"Result: {rs} - Expect: {3}");
            cache.Put(5, 5);
            rs = cache.Get(2);
            Console.WriteLine($"Result: {rs} - Expect: {-1}");
            rs = cache.Get(5);
            Console.WriteLine($"Result: {rs} - Expect: {5}");

            // phase 2

            cache = new LFUCache(1);
            cache.Put(1, 1);
            rs = cache.Get(1);
            Console.WriteLine($"Result: {rs} - Expect: {1}");
            cache.Put(1, 3);
            rs = cache.Get(1);
            Console.WriteLine($"Result: {rs} - Expect: {3}");
            cache.Put(2, 4);
            rs = cache.Get(2);
            Console.WriteLine($"Result: {rs} - Expect: {4}");
            rs = cache.Get(1);
            Console.WriteLine($"Result: {rs} - Expect: {-1}");

            // phase 3

            cache = new LFUCache(2);
            cache.Put(1, 1);
            cache.Put(2, 2);
            rs = cache.Get(1);
            Console.WriteLine($"Result: {rs} - Expect: {1}");
            cache.Put(3, 3);
            rs = cache.Get(2);
            Console.WriteLine($"Result: {rs} - Expect: {-1}");
            rs = cache.Get(3);
            Console.WriteLine($"Result: {rs} - Expect: {3}");
            cache.Put(4, 4);
            rs = cache.Get(1);
            Console.WriteLine($"Result: {rs} - Expect: {-1}");
            rs = cache.Get(-1);
            Console.WriteLine($"Result: {rs} - Expect: {-1}");
            rs = cache.Get(3);
            Console.WriteLine($"Result: {rs} - Expect: {3}");

            cache = new LFUCache(0);
            cache.Put(0, 0);
            cache.Get(0);
        }