| 1 25 package org.archive.util.fingerprint; 26 27 33 public class ArrayLongFPCache implements LongFPSet { 34 public static final int DEFAULT_CAPACITY = 1 << 20; 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 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 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 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 94 public long count() { 95 return Math.min(count,cache.length); 96 } 97 98 101 public boolean quickContains(long fp) { 102 return contains(fp); 103 } 104 105 public int cacheLength() { 106 return cache.length; 107 } 108 109 } 110 | Popular Tags |