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 dupd 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)
35 TUPLE: fifo-cache < linked-assoc max-size ;
37 : <fifo-cache> ( max-size exemplar -- assoc )
38 dupd new-assoc <dlist> rot fifo-cache boa ;
40 : <fifo-hash> ( max-size -- assoc )
44 [ call-next-method ] keep dup max-size>> [
45 over assoc>> assoc-size < [
46 [ dlist>> pop-front first-unsafe ]
48 [ dlist>> ] tri (delete-at)
52 TUPLE: lifo-cache < linked-assoc max-size ;
54 : <lifo-cache> ( max-size exemplar -- assoc )
55 dupd new-assoc <dlist> rot lifo-cache boa ;
57 : <lifo-hash> ( max-size -- assoc )
62 over assoc>> assoc-size <= [
64 [ dlist>> pop-back first-unsafe ]
66 [ dlist>> ] tri (delete-at)
68 ] when* call-next-method ;