1 ! Copyright (C) 2017 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors assocs deques dlists fry kernel linked-assocs
5 linked-assocs.private math sequences.private ;
9 TUPLE: lru-cache < linked-assoc max-size ;
11 : <lru-cache> ( max-size exemplar -- assoc )
12 0 swap new-assoc <dlist> rot lru-cache boa ;
14 : <lru-hash> ( max-size -- assoc )
18 [ assoc>> at* ] [ dlist>> dup ] bi '[
22 [ obj>> second-unsafe ] tri
27 [ call-next-method ] keep dup max-size>> [
28 over assoc>> assoc-size < [
29 [ dlist>> pop-front first-unsafe ]
31 [ dlist>> ] tri (delete-at)
36 [ assoc>> clone ] [ dlist>> clone ] [ max-size>> ] tri