Abstract Classes - Polymorphism

Sort by

recency

|

340 Discussions

|

  • + 0 comments

    Here is my solution using std::list(follows solution posted here by kubalak_2k18):

    #include <iostream>
    #include<map>
    #include <list> 
    #include <algorithm>
    using namespace std;
    struct Node {
        int value, key;
        Node (int k,int v):value(v),key(k){};
    };
    class Cache{
        protected :
        list <Node> l;
        map<int, list<Node>::iterator> mp;
        int cp;
        virtual void set(int,int)=0;
        virtual int get(int)=0;
        
    };
    class LRUCache:public Cache{
    
        void removeBack(){
            auto tail = --l.end();
            mp.erase(tail->key);
            l.pop_back();
        }
         void addNodeFront(int k, int v) {
             Node *node = new Node(k,v);
             
             if (mp.size() == cp)
                removeBack();
    						
             //l.push_front({k,v});
             l.push_front(*node);
             mp[k] = l.begin();//head
         }  
         void moveToHead(list<Node>::iterator  arg, int new_val){
            (*arg).value = new_val;
    
            l.splice(l.begin(),l, arg);
            mp[arg->key]= l.begin();
         }
    public:
        LRUCache(int &cap){
            cp = cap;
        }
         void set(int k,int v){
             if (mp.find(k)!=mp.end()){
                if (mp.size()>1)
                    moveToHead(mp[k],v);
                else {
                    l.begin()->value = v;
                }
             }
             else 
                addNodeFront(k,v);
         }
         
         int get (int k){
             if (mp.find(k)!=mp.end()){
                return mp[k]->value;
             }
            return -1;
         }
    };
    
    int main()
    {
        int n, capacity,i;
        cin>> n>> capacity;
        LRUCache l(capacity);
        for (i =0;i< n;i++){
            string command;
            cin>> command;
            if (command == "get"){
                int key;
                cin >> key;
                cout<< l.get(key)<< endl;
            }
            else if(command == "set"){
                int key,value;
                cin>> key>>value;
                l.set(key,value);
            }
        }
        return 0;
    }
    
  • + 0 comments

    It allows one interface to be used for different underlying data types or implementations. Cricbet99

  • + 0 comments

    This was a great exercise to understand how abstract classes and polymorphism work in C++. Cricbet99 club register

  • + 1 comment

    Thiiiis feels like a poorly-designed problem. In addition to the lack of polymorphism mentioned in the top voted comment, the question presents the cache as a way to avoid doing expensive lookups, which, okay, a linked list is expensive to sort through. But then, it provides you a std::map mp, which lets you look up nodes quickly anyways?

    std::map can't be used for implementing LRU as-is since it doesn't store any information about recency, unless I'm missing something? It's not clear from the description, either:

    "mp - Map the key to the node in the linked list"

    That's a request, not a description.

  • + 0 comments

    This challenge is a great way to understand how abstract classes and polymorphism work in object-oriented programming. 11xplay