7

Apologize in advance for poor wording and a complicated question, English is me second language.

I'm looking for a data structure to hold an array/list/collection of objects (in my case - integer IDs - but that doesn't matter), that would track the "usage frequency" of the elements. So I can run a background job and remove lesser-used elements (to free up memory usage).

Something like, when an element is accessed or searched for - the element is moved "to the surface", pushing less used elements "to the bottom". Again, the goal is to determine "lesser accessed" values and remove them from time to time.

I was thinking about writing my own class, inheriting/subclassing some existing list structure (LinkedList<T,T> or Queue<T> maybe) and then overload some "GetElement" method so that it would "return the element AND move it to the beginning of the list". So later, in my background job, I can remove the elements from the "end of the list" (b/c they are accessed less frequent).

  1. Am I in the right direction?
  2. Is there any built-in data structure in .NET for this or some well-known practice maybe? I tried googling, haven't found anything. But I bet this problem is as old as the history of computing :)

Thank you!

UPDATE: thanks for the comments. Turns out "LRU" is what it's called in plain English. I'm closing this question, since googling for "LRU cache c#" offers tons of answers.

jazzcat
  • 4,351
  • 5
  • 36
  • 37
  • 2
    A starting point for this might be looking into a `LRU Cache` of some sort. – Gilgamesh Jan 26 '16 at 12:49
  • @Gilgamesh ah, THAT is what it's called. Thanks! – jazzcat Jan 26 '16 at 12:53
  • 1
    LRU evicts the least-recently used value from the cache, if you want total frequency then look at LFU (least-frequently used) caches. If you want to move any accessed item to the front so it can be accessed more quickly in future then you might want to look at splay trees. – Lee Jan 26 '16 at 12:56
  • [Here](http://stackoverflow.com/questions/953212/lfu-cache-in-c) is an answer for an LFU cache. Not sure if we want to mark this as a duplicate. – krillgar Jan 26 '16 at 13:16
  • There are splay trees, too. – usr Jan 26 '16 at 14:50

0 Answers0