Exemple #1
0
        public void BasicTests()
        {
            LRUCache cache = new LRUCache(2);         //capacity

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

            cache.Put("3", "3");                      // evicts key 2
            Assert.IsNull(cache.Get("2"));            // returns null (not found)

            cache.Put("4", "4");                      // evicts key 1
            Assert.IsNull(cache.Get("1"));            // returns null (not found)

            Assert.AreEqual("3", cache.Get("3"));     // returns 3
            Assert.AreEqual("4", cache.Get("4"));     // returns 4

            var seq = cache.GetKeySequence();

            Assert.IsTrue(seq.SequenceEqual(new string[] { "4", "3" }));
        }
Exemple #2
0
        static void Main(string[] args)
        {
            int[][] pages = new int[3][] {
                new int[] { 1, 1 },
                new int[] { 2, 4 },
                new int[] { 3, 5 }
            };

            Dictionary <int, Node>   dict          = new Dictionary <int, Node>();
            Dictionary <string, int> hitsOrFailure = new Dictionary <string, int>();
            LRUCache cache = new LRUCache(3);

            for (int i = 0; i < pages.Length; i++)
            {
                cache.set(pages[i][0], pages[i][1]);
            }

            int value = cache.get(1);

            cache.set(4, 9);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            LRUCache <String> cache = new LRUCache <string>(3);
            int choice = 1;

            //choice = int.Parse(Console.ReadLine());

            while (choice != 0)
            {
                Console.WriteLine("1: Put");
                Console.WriteLine("2: Get");
                Console.WriteLine("0: Exit");
                choice = int.Parse(Console.ReadLine());
                string key;
                string value;
                switch (choice)
                {
                case 1:
                    Console.WriteLine("Enter key");
                    key = Console.ReadLine();
                    Console.WriteLine("Enter Value");
                    value = Console.ReadLine();
                    cache.put(key, value);

                    Console.WriteLine("Inserted");
                    break;

                case 2:
                    Console.WriteLine("Enter key");
                    key = Console.ReadLine();
                    Console.WriteLine("Value is: " + cache.get(key));
                    break;

                default:
                    Console.WriteLine("Stay safe! Wear mask! GOODBYE!!");
                    break;
                }
            }
        }