Author: xor
Date: 2008-11-12 00:23:34 +0000 (Wed, 12 Nov 2008)
New Revision: 23501
Modified:
trunk/freenet/src/freenet/support/LRUQueue.java
Log:
Add the possibility to use as a fixed-size queue: When adding an element to the
beginning and the size limit is exceeded, the last element is dropped. When
adding to the end and the size limit is exceeded, the first element is dropped.
Modified: trunk/freenet/src/freenet/support/LRUQueue.java
===================================================================
--- trunk/freenet/src/freenet/support/LRUQueue.java 2008-11-11 23:47:17 UTC
(rev 23500)
+++ trunk/freenet/src/freenet/support/LRUQueue.java 2008-11-12 00:23:34 UTC
(rev 23501)
@@ -14,7 +14,16 @@
*/
private final DoublyLinkedListImpl list = new DoublyLinkedListImpl();
private final Hashtable hash = new Hashtable();
+ private final int sizeLimit;
+ public LRUQueue() {
+ sizeLimit = -1;
+ }
+
+ public LRUQueue(int mySizeLimit) {
+ sizeLimit = mySizeLimit;
+ }
+
/**
* push()ing an object that is already in
* the queue moves that object to the most
@@ -26,6 +35,9 @@
if (insert == null) {
insert = new QItem(obj);
hash.put(obj,insert);
+
+ if(sizeLimit!=-1 && list.size() > sizeLimit)
+ pop();
} else {
list.remove(insert);
}
@@ -41,6 +53,9 @@
if (insert == null) {
insert = new QItem(obj);
hash.put(obj,insert);
+ if(sizeLimit!=-1 && list.size() > sizeLimit) {
+ hash.remove(((QItem)list.shift()).obj);
+ }
} else {
list.remove(insert);
}
_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs