Abstract base classes in C++ can only be used as base classes. Thus, they are allowed to have virtual member functions without definitions.A cache is a component that stores data so future requests for that data can be served faster. The data stored in a cache might be the results of an earlier computation, or the duplicates of data stored elsewhere. A cache hit occurs when the requested data can be found in a cache, while a cache miss occurs when it cannot. Cache hits are served by reading data from the cache which is faster than recomputing a result or reading from a slower data store. Thus, the more requests that can be served from the cache, the faster the system performs.One of the popular cache replacement policies is: "least recently used" (LRU). It discards the least recently used items first.For example, if a cache with a capacity to store 5 keys has the following state(arranged from most recently used key to least recently used key) -5 3 2 1 4Now, If the next key comes as 1(which is a cache hit), then the cache state in the same order will be -1 5 3 2 4Now, If the next key comes as 6(which is a cache miss), then the cache state in the same order will be -6 1 5 3 2You can observe that 4 has been discarded because it was the least recently used key and since the capacity of cache is 5, it could not be retained in the cache any longer.Given an abstract base class Cache with member variables and functions:mp - Map the key to the node in the linked listcp - Capacitytail - Double linked list tail pointerhead - Double linked list head pointerset() - Set/insert the value of the key, if present, otherwise add the key as the most recently used key. If the cache has reached its capacity, it should replace the least recently used key with a new key.get() - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.You have to write a class LRUCache which extends the class Cache and uses the member functions and variables to implement an LRU cache.Input FormatFirst line of input will contain the number of lines containing or commands followed by the capacity of the cache.The following lines can either contain or commands.An input line starting with will be followed by a to be found in the cache. An input line starting with will be followed by the and respectively to be inserted/replaced in the cache.
Question
Abstract base classes in C++ can only be used as base classes. Thus, they are allowed to have virtual member functions without definitions.A cache is a component that stores data so future requests for that data can be served faster. The data stored in a cache might be the results of an earlier computation, or the duplicates of data stored elsewhere. A cache hit occurs when the requested data can be found in a cache, while a cache miss occurs when it cannot. Cache hits are served by reading data from the cache which is faster than recomputing a result or reading from a slower data store. Thus, the more requests that can be served from the cache, the faster the system performs.One of the popular cache replacement policies is: "least recently used" (LRU). It discards the least recently used items first.For example, if a cache with a capacity to store 5 keys has the following state(arranged from most recently used key to least recently used key) -5 3 2 1 4Now, If the next key comes as 1(which is a cache hit), then the cache state in the same order will be -1 5 3 2 4Now, If the next key comes as 6(which is a cache miss), then the cache state in the same order will be -6 1 5 3 2You can observe that 4 has been discarded because it was the least recently used key and since the capacity of cache is 5, it could not be retained in the cache any longer.Given an abstract base class Cache with member variables and functions:mp - Map the key to the node in the linked listcp - Capacitytail - Double linked list tail pointerhead - Double linked list head pointerset() - Set/insert the value of the key, if present, otherwise add the key as the most recently used key. If the cache has reached its capacity, it should replace the least recently used key with a new key.get() - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.You have to write a class LRUCache which extends the class Cache and uses the member functions and variables to implement an LRU cache.Input FormatFirst line of input will contain the number of lines containing or commands followed by the capacity of the cache.The following lines can either contain or commands.An input line starting with will be followed by a to be found in the cache. An input line starting with will be followed by the and respectively to be inserted/replaced in the cache.
Solution
The text you provided is a problem statement asking to implement a Least Recently Used (LRU) Cache using C++. The LRU Cache is a type of cache in which the least recently used entries are removed when the cache capacity has been reached.
The problem statement provides an abstract base class Cache with member variables and functions. The member variables include:
mpwhich maps the key to the node in the linked listcpwhich is the capacity of the cachetailwhich is a pointer to the tail of the double linked listheadwhich is a pointer to the head of the double linked list
The member functions include:
set()which sets or inserts the value of the key. If the key is present, it updates the value, otherwise it adds the key as the most recently used key. If the cache has reached its capacity, it should replace the least recently used key with a new key.get()which gets the value of the key if the key exists in the cache, otherwise it returns -1.
The task is to write a class LRUCache which extends the class Cache and uses the member functions and variables to implement an LRU cache.
The input format is as follows:
- The first line of input will contain the number of lines containing
getorsetcommands followed by the capacity of the cache. - The following lines can either contain
getorsetcommands. An input line starting withgetwill be followed by a key to be found in the cache. An input line starting withsetwill be followed by the key and value respectively to be inserted/replaced in the cache.
Similar Questions
The idea of cache memory is primarily based on:
The caching mechanism is used in computer systems to (A) allocate memory to different processes. (B) store frequently accessed data in a temporary storage area for quicker access. (C) ensure communication between memory and I/O devices. (D) manage network protocols and data transmission. (E) handel the system error.
What is the purpose of a virtual base class in C++?
What is the primary purpose of cache memory in the memory hierarchy? To store large amounts of data permanentlyTo act as an intermediary between the CPU and main memory, speeding up access to frequently used dataTo provide backup storageTo handle input and output operations
Most virtual memory schemes make use of a special high-speed cache for page table entries, called a
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.