Woops. And of course there should be the check for bound in the update
functions of the lru and mu strategies...

(defn lru-cache-strategy
  "Implements LRU cache strategy for memoize. At most bound number of
  argument lists are kept in the cache. They are dropped in LRU
order."
  [bound]
  (let [values (ref {})]
    [(fn lru-cache-strategy-lookup
       [cache args]
       (when-let [e (find cache args)]
         (let [now (System/currentTimeMillis)]
           (dosync (alter values assoc args now))
           e)))
     (fn lru-cache-strategy-update
       [cache args]
       (let [now (System/currentTimeMillis)]
         (alter values assoc args now)
         (if (> (co...@values) bound)
           (let [k (min-key @values (keys @values))]
             (alter values dissoc k)
             (dissoc cache k))
           cache))))]))

(defn most-used-cache-strategy
  "Implements MU cache strategy for memoize. At most bound number of
  argument lists are kept in the cache. They are dropped in LU order.
  In case elements have the same usage count, the order of drop is
  unspecified."
  [bound]
  (let [values (ref {})]
    [(fn most-used-cache-strategy-lookup
       [cache args]
       (when-let [e (find cache args)]
         (dosync (alter values update-in [args] inc))
         e))
     (fn most-used-cache-strategy-update
       [cache args]
       (alter values assoc args 1)
       (if (> (count @values) bound)
         (let [k (min-key @values (keys @values))]
           (alter values dissoc k)
           (dissoc cache k))
         cache))]))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to