1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.archive.util.fingerprint;
26
27 /***
28 * Simple long fingerprint cache using a backing array; any long maps to
29 * one of 'smear' slots. Longs inserted should be randomly distributed,
30 *
31 * @author gojomo
32 */
33 public class ArrayLongFPCache implements LongFPSet {
34 public static final int DEFAULT_CAPACITY = 1 << 20;
35 public static final int DEFAULT_SMEAR = 5;
36
37 long cache[] = new long[DEFAULT_CAPACITY];
38 int smear = DEFAULT_SMEAR;
39 int count = 0;
40
41 public void setCapacity(int newCapacity) {
42 long[] oldCache = cache;
43 cache = new long[newCapacity];
44 for(int i=0;i<oldCache.length;i++) {
45 add(oldCache[i]);
46 }
47 }
48
49
50
51
52 public boolean add(long l) {
53 if(contains(l)) {
54 return false;
55 }
56 int index = (Math.abs((int) (l % cache.length)) + (count % smear)) % cache.length;
57 count++;
58 cache[index]=l;
59 return true;
60 }
61
62
63
64
65 public boolean contains(long l) {
66 int index = Math.abs((int) (l % cache.length));
67 for(int i = index; i < index + smear; i++) {
68 if(cache[i%cache.length]==l) {
69 return true;
70 }
71 }
72 return false;
73 }
74
75
76
77
78 public boolean remove(long l) {
79 int index = Math.abs((int) (l % cache.length));
80 for(int i = index; i < index + smear; i++) {
81 if(cache[i%cache.length]==l) {
82 cache[i%cache.length]=0;
83 count = Math.min(count,cache.length);
84 count--;
85 return true;
86 }
87 }
88 return false;
89 }
90
91
92
93
94 public long count() {
95 return Math.min(count,cache.length);
96 }
97
98
99
100
101 public boolean quickContains(long fp) {
102 return contains(fp);
103 }
104
105 public int cacheLength() {
106 return cache.length;
107 }
108
109 }