1 // Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
3 // You may modify, copy, and redistribute this code under the terms of
4 // the GNU Library Public License version 2.1, with the exception of
5 // the portion of clause 6a after the semicolon (aka the "obnoxious
10 // FIXME needs to be a weak hash
13 * A Hash table with a fixed size; drops extraneous elements. Uses
16 public class Cache extends Hash {
18 /** head of list is the mru; tail is the lru */
24 public Cache(int maxSize) {
25 super(maxSize * 2, 3);
26 this.maxSize = maxSize;
29 /** A doubly-linked list */
34 public Node(Object k1, Object k2, Object val) { this.k1 = k1; this.k2 = k2; this.val = val; }
38 if (this == lru) lru = prev;
39 if (this == mru) mru = next;
40 if (next != null) next.prev = prev;
41 if (prev != null) prev.next = next;
44 void placeAfter(Node n) {
46 if (n == null) return;
48 if (n.next != null) n.next.prev = this;
52 void placeBefore(Node n) {
54 if (n == null) return;
58 if (prev != null) prev.next = this;
67 public void remove(Object k1, Object k2) {
68 Object o = super.get(k1, k2);
69 if (o != null) ((Node)o).remove();
73 public Object get(Object k1, Object k2) {
74 Node n = (Node)super.get(k1, k2);
75 if (n == null) return null;
82 public void put(Object k1, Object k2, Object v) {
83 Node n = new Node(k1, k2, v);
90 if (super.get(k1, k2) != null) remove(k1, k2);
92 if (size > maxSize) remove(lru.k1, lru.k2);