Created
January 14, 2017 14:30
-
-
Save NalaGinrut/ca8e6c094bb4b82c94cb46f37ea891a0 to your computer and use it in GitHub Desktop.
Simple LRU implementation in Scheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (make-lru size) | |
(define cnt 0) | |
(define fl '()) | |
(define ht (make-hash-table)) | |
(define (refresh-fl i) | |
(set! fl (cons i (filter (lambda (x) (not (= x i))) fl)))) | |
(lambda (cmd . i/v) | |
(case cmd | |
((get) | |
(refresh-fl (car i/v)) | |
(hash-ref ht (car i/v) "no")) | |
((set) | |
(display fl) (newline) | |
(if (= cnt size) | |
(let ((i (list-ref fl (1- cnt)))) | |
(hash-set! ht i (car i/v)) | |
(refresh-fl i)) | |
(begin | |
(set! fl (cons cnt fl)) | |
(hash-set! ht cnt (car i/v)) | |
(set! cnt (1+ cnt)))) | |
(hash-map->list cons ht)) | |
((cnt) cnt) | |
((fl) fl)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment