www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Lists Allowing for Switching Values

reply "Jeroen Bollen" <jbinero gmail.com> writes:
Is there a way in D to have a list that allows you to shift 
around values? I'm creating a cache and I would like to keep a 
log of when an item was last accessed so when the cache shrinks 
or is about to overflow I can delete the items that haven't been 
accessed in a while.

I would like to be able to push to the back and pop from the 
front. Do note that I would like to pop from the front, and not 
from the back, as I want the oldest items to be popped out.

I would like the be able to search if the item is actually in the 
list. If no such functionality exists yet I could alternatively 
implement it myself; it's not crucial.

I would like to be able to switch items around. This allows me 
when an item already in the list is accessed, I can put it back 
at the back, where the most recent items reside.

Does such a feature exist already in D, is there perhaps a better 
approach about doing this?
Feb 10 2014
next sibling parent "Jeroen Bollen" <jbinero gmail.com> writes:
Also posted on SO: 
http://stackoverflow.com/questions/21686962/lists-allowing-for-pushing-popping-switching-values
Feb 10 2014
prev sibling parent "Francesco Cattoglio" <francesco.cattoglio gmail.com> writes:
On Monday, 10 February 2014 at 19:35:55 UTC, Jeroen Bollen wrote:
 Is there a way in D to have a list that allows you to shift 
 around values? I'm creating a cache and I would like to keep a 
 log of when an item was last accessed so when the cache shrinks 
 or is about to overflow I can delete the items that haven't 
 been accessed in a while.

 I would like to be able to push to the back and pop from the 
 front. Do note that I would like to pop from the front, and not 
 from the back, as I want the oldest items to be popped out.

 I would like the be able to search if the item is actually in 
 the list. If no such functionality exists yet I could 
 alternatively implement it myself; it's not crucial.

 I would like to be able to switch items around. This allows me 
 when an item already in the list is accessed, I can put it back 
 at the back, where the most recent items reside.

 Does such a feature exist already in D, is there perhaps a 
 better approach about doing this?
It all depends on the access times you need. If you are fine with O(n) or O(log n), then something from std.container with a few extra helper functions will do just fine. O(1) on the other hand, might require 2 associative arrays you will have to keep it sync. Nothing impossible anyway.
Feb 10 2014