@Override public DocSet union(DocSet other) { if (other instanceof HashDocSet) { // set "a" to the smallest doc set final HashDocSet a = size()<=other.size() ? this : (HashDocSet)other; final HashDocSet b = size()<=other.size() ? (HashDocSet)other : this; int[] result = new int[a.size()+b.size()]; int resultCount=0; // iterate over the largest table first, adding w/o checking. for (int i=0; i<b.table.length; i++) { int id=b.table[i]; if (id>=0) result[resultCount++]=id; } // now iterate over smaller set, adding all not already in larger set. for (int i=0; i<a.table.length; i++) { int id=a.table[i]; if (id>=0 && !b.exists(id)) result[resultCount++]=id; } return new HashDocSet(result,0,resultCount); } else { return other.union(this); } }
@Override public DocSet andNot(DocSet other) { int[] result = new int[size()]; int resultCount=0; for (int i=0; i<table.length; i++) { int id=table[i]; if (id >= 0 && !other.exists(id)) { result[resultCount++]=id; } } return new HashDocSet(result,0,resultCount); }
@Override public DocSet intersection(DocSet other) { if (other instanceof HashDocSet) { // set "a" to the smallest doc set for the most efficient // intersection. final HashDocSet a = size()<=other.size() ? this : (HashDocSet)other; final HashDocSet b = size()<=other.size() ? (HashDocSet)other : this; int[] result = new int[a.size()]; int resultCount=0; for (int i=0; i<a.table.length; i++) { int id=a.table[i]; if (id >= 0 && b.exists(id)) { result[resultCount++]=id; } } return new HashDocSet(result,0,resultCount); } else { int[] result = new int[size()]; int resultCount=0; for (int i=0; i<table.length; i++) { int id=table[i]; if (id >= 0 && other.exists(id)) { result[resultCount++]=id; } } return new HashDocSet(result,0,resultCount); } }
@Override public int intersectionSize(DocSet other) { if (other instanceof HashDocSet) { // set "a" to the smallest doc set for the most efficient // intersection. final HashDocSet a = size()<=other.size() ? this : (HashDocSet)other; final HashDocSet b = size()<=other.size() ? (HashDocSet)other : this; int resultCount=0; for (int i=0; i<a.table.length; i++) { int id=a.table[i]; if (id >= 0 && b.exists(id)) { resultCount++; } } return resultCount; } else { int resultCount=0; for (int i=0; i<table.length; i++) { int id=table[i]; if (id >= 0 && other.exists(id)) { resultCount++; } } return resultCount; } }