← Back to team overview

kicad-developers team mailing list archive

Re: Cache algorithms

 

Hi Marcantonio,

I was expecting to borrow some library but I end to write my own. I added it to my kicad working branch, you can have a look:
http://bazaar.launchpad.net/~mrluzeiro/kicad/kicad_new3d-viewer/view/head:/include/lru_cache.h

I tested a little and seems to work.
The only thing it is not clear for me is:
"throw std::range_error( "Requested a key that dont exists" );"

if I make
try
{
 get a non existent key
}
catch ()
{
catch the error
}

I cannot catch and it really throw an error and break the code.. (popup a message with that error code) I have no experience on this so I don't know if it is the correct behaviour.
I was expect that I could catch the error and then do something appropriate to fix it.
Otherwise, I dont think it is very good to do always
if( exist(key) )
 get(key)

because it have to search twice for the key (==overhead)

Suggestions? Thanks!

Regards,
Mario Luzeiro
________________________________________
From: Kicad-developers [kicad-developers-bounces+mrluzeiro=ua.pt@xxxxxxxxxxxxxxxxxxx] on behalf of Lorenzo Marcantonio [l.marcantonio@xxxxxxxxxxxx]
Sent: 23 April 2015 08:36
To: Kicad Developers
Subject: Re: [Kicad-developers] Cache algorithms

On Wed, Apr 22, 2015 at 10:06:09PM +0000, Mário Luzeiro wrote:
> Does anyone where have some implementation that maybe added to KiCad project?
> Or any suggestion where / how can I choose one to add?

An LRU cache can be implemented in different ways; boost suggest this:

http://www.boost.org/doc/libs/1_51_0/libs/multi_index/doc/examples.html#example9

Another way (the lispier one that I prefer) is to use an embedded list
structure backed by a set to index it: when you access an element you
cons it to the front, when you want to trim it you drop the tail. If the
list is sufficiently small it's actually faster to traverse the list
than maintain the set (also you don't need a d-list since you can keep
the prev pointer during the search traversal).

--
Lorenzo Marcantonio
Logos Srl

References