I'm writing a program and i have an array of structs, I need to overwrite elements in the array such that I am overwriting the least recently used.
So i have in my struct an variable to take the time it was inserted.
What would be the best way to go about this and what is the smallest increment of time that can be recorded?
No, I cant do that.
I guess I should say what I'm trying to do here.
I'm simulating a cache, so I need to write blocks of information to specific sets in the array.
The structs (my array is an array of structs) have an age variable which I want to set to the time at which they were entered.
Then when i was to write over the least recently used, i just check for the entry with the smallest age variable. I need the time increment to be as small as possible. to ensure no entries get the same time.
You could still use my method but with pointers. I don't really see any advantage to actually storing real times unless you have concurrent access (and you shouldn't)
I'm not really sure what your method is, as it is i am using a vector.
The vector is essentially broken up into sets of several blocks each.
For instance I may have a set being the vector indices 0 to 15, this is made up of 4 blocks, eg 0-3, 4-7, 8-11 and 12-15. During the running of the program, the blocks will initially be filled with data, they will then be accessed a block at a time in an unknown order. At times I will need to copy new data in to this set, and i have to put it in the block that was least recently accessed. For this I need to time stamp the blocks so I know which block to overwrite.
Do you actually require a time to know which block was committed earliest. Wouldn't a sequence index be enough? You said you need to know which block was last access, not how long ago it was accessed.
Andy
PS If I understand L B correctly, his method involves reordering (every time you access a block you move it to the back of the vector, so the blocks are in access order?). I'm not sure that's what he means. But if it is, then a vector is a bad choice. It should be a deque or list.