예제 #1
0
        private static void Run()
        {
            var cache = new LRUCache(2);

            cache.Put(1, 1);
            cache.Put(2, 2);
            cache.Get(1);
            cache.Put(3, 3);
            cache.Get(2);
            cache.Put(4, 4);
            cache.Get(1);
            cache.Get(3);
            cache.Get(4);
        }
예제 #2
0
        static void Main(string[] args)
        {
            LRUCache lruCache = new LRUCache(2);

            lruCache.Put(1, 1);
            lruCache.Put(2, 2);
            Console.Write(lruCache.Get(1));
            lruCache.Put(3, 3);
            Console.Write(lruCache.Get(2));
            lruCache.Put(4, 4);
            Console.Write(lruCache.Get(1));
            Console.Write(lruCache.Get(3));
            Console.Write(lruCache.Get(4));
            Console.ReadKey();
        }
예제 #3
0
        static void Main(string[] args)
        {
            LRUCache cache = new LRUCache(2 /* capacity */);

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

            Console.WriteLine(cache.Get(1)); // returns 1
            cache.Put(3, 3);                 // evicts key 2
            Console.WriteLine(cache.Get(2)); // returns -1 (not found)
            cache.Put(4, 4);                 // evicts key 1
            Console.WriteLine(cache.Get(1)); // returns -1 (not found)
            Console.WriteLine(cache.Get(3)); // returns 3
            Console.WriteLine(cache.Get(4)); // returns 4
        }
예제 #4
0
        static void Main(string[] args)
        {
            LRUCache cache = new LRUCache(2);

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

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

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

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

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