1 // Copyright (C) 2003 Adam Megacz <adam@xwt.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
12 // FIXME needs to be a weak hash
15 * A Hash table with a fixed size; drops extraneous elements. Uses
18 public class Cache extends Hash {
20 /** head of list is the mru; tail is the lru */
26 public Cache(int maxSize) {
27 super(maxSize * 2, 3);
28 this.maxSize = maxSize;
31 /** A doubly-linked list */
36 public Node(Object k1, Object k2, Object val) { this.k1 = k1; this.k2 = k2; this.val = val; }
40 if (this == lru) lru = prev;
41 if (this == mru) mru = next;
42 if (next != null) next.prev = prev;
43 if (prev != null) prev.next = next;
46 void placeAfter(Node n) {
48 if (n == null) return;
50 if (n.next != null) n.next.prev = this;
54 void placeBefore(Node n) {
56 if (n == null) return;
60 if (prev != null) prev.next = this;
69 public void remove(Object k1, Object k2) {
70 Object o = super.get(k1, k2);
71 if (o != null) ((Node)o).remove();
75 public Object get(Object k1, Object k2) {
76 Node n = (Node)super.get(k1, k2);
77 if (n == null) return null;
84 public void put(Object k1, Object k2, Object v) {
85 Node n = new Node(k1, k2, v);
92 if (super.get(k1, k2) != null) remove(k1, k2);
94 if (size > maxSize) remove(lru.k1, lru.k2);