top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can we implement an LRU cache using just a single container i.e. map or unordered_map?

+3 votes
435 views

How can we implement an LRU cache using just a single container i.e. map or unordered_map?

Expected operations:
1. find(key_t) - find a certain value in cache
2. insert(key_t, value_t) - insert a new value to the cache

posted Oct 8, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

impletement LRU class which extends LinkedHashMap and override removeEldestEntry like this:

class LRULinkedHashMap<K,V> extends LinkedHashMap<K,V>{  
    private int capacity;  

    LRULinkedHashMap(int capacity){  
        super(16,0.75f,true);  
        this.capacity=capacity;  
    }  

    @Override  
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest){   
        return size() > capacity;  
    }    
}

For further info please check this : Link Here!

answer Oct 15, 2015 by Mohammed Hussain
Similar Questions
+9 votes

how we can build LRU cache in java using concurrent package??
Can anyone give some code.

0 votes

I am developing a program in C and I want to lock some memory pages in cache, particularly in the core specific level (i.e. level 1). As far as I know the C libraries do not provide an interface to do that so I guess
that there must be some particular directives in gcc to do so. I am interested in locking both a struct and some arbitrary variables.

Could any one provide any information for that? If so it would be very helpful.

+1 vote

Can someone help me with algorithm and code?

+1 vote

I had a couple *.iso disks in my CD/DVD drive recently. I had looked at the images, but did not install. Now, i have a software update prompt and when I go to install, I am asked to put the two iso's in the drive. If I cancel the request and try to continue with the updates, it fails to complete them.

My google efforts have failed to produce a solution to the problem. How do I clean this up so that I can get back to regular updates?

...