Source code: Freenet/support/LimitedBinaryTree.java
1 package Freenet.support;
2 /*
3 This code is part of the Java Adaptive Network Client by Ian Clarke.
4 It is distributed under the GNU Public Licence (GPL) version 2. See
5 http://www.gnu.org/ for further details of the GPL.
6
7 Explanation of Code Versions:
8 0.0.0 = Initial Description
9 0.0.1 = API Specified
10 0.x (x>0) = Partial Implementation
11 x.0 (x>0) = Operational
12
13 Requires Classes: Class (version)
14 Class (version)
15 ...
16 */
17
18 /**
19 * This class represents a binary tree of limited size which uses integers
20 * as keys. If the tree is full and a new value is added, then the oldest
21 * value is removed.
22 *
23 * @author <A HREF="mailto:I.Clarke@strs.co.uk">Ian Clarke</A>
24 **/
25
26 public class LimitedBinaryTree extends BinaryTree
27 {
28 protected CyclicArray ca;
29
30 public LimitedBinaryTree(int size)
31 {
32 super();
33 ca = new CyclicArray(size);
34 }
35
36 public void put(long key, Object data)
37 {
38 Long prev = (Long) ca.put(new Long(key));
39 if (prev != null)
40 {
41 super.delete(prev.longValue());
42 }
43 super.put(key, data);
44 }
45
46 public void delete(long key)
47 {
48 for(int x=0; x<ca.length(); x++)
49 {
50 Long l=(Long)ca.get(x);
51 if(l!=null)
52 if (l.intValue() == key)
53 {
54 ca.remove(x);
55 }
56 }
57 super.delete(key);
58 }
59 }
60