1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.archive.util.fingerprint;
25
26
27
28 /***
29 * Like a MemLongFPSet, but with fixed capacity and maximum size.
30 * When an add would expand past the maximum size, an old entry
31 * is deleted via a clock/counter algorithm.
32 *
33 * @author gojomo
34 *
35 */
36 public class LongFPSetCache extends MemLongFPSet {
37
38 private static final long serialVersionUID = -5307436423975825566L;
39
40 long sweepHand = 0;
41
42 public LongFPSetCache() {
43 super();
44 }
45
46 public LongFPSetCache(int capacityPowerOfTwo, float loadFactor) {
47 super(capacityPowerOfTwo, loadFactor);
48 }
49
50 protected void noteAccess(long index) {
51 if(slots[(int)index]<Byte.MAX_VALUE) {
52 slots[(int)index]++;
53 }
54 }
55
56 protected void makeSpace() {
57 discard(1);
58 }
59
60 private void discard(int i) {
61 int toDiscard = i;
62 while(toDiscard>0) {
63 if(slots[(int)sweepHand]==0) {
64 removeAt(sweepHand);
65 toDiscard--;
66 } else {
67 if (slots[(int)sweepHand]>0) {
68 slots[(int)sweepHand]--;
69 }
70 }
71 sweepHand++;
72 if (sweepHand==slots.length) {
73 sweepHand = 0;
74 }
75 }
76 }
77 }