result.setCachedKeys(new ArrayList<>(this.getCachedKeys()));
for (Long key : second.getCachedKeys()) {
result.addKey(key);
}
} else {
LongBloomFilter mergedCounts;
if (!this.isSparse() && !second.isSparse()) {
mergedCounts = ApproximateCountsUtils.mergeBloomFilters(this.getFilterCounts(), second.getFilterCounts());
} else {
if (!this.isSparse()) {
mergedCounts = this.getFilterCounts().clone();
} else if (!second.isSparse()) {
mergedCounts = second.getFilterCounts().clone();
} else {
//Both sparse
mergedCounts = new LongBloomFilter(this.getNumberOfFeatures(), CompactIndex.FPP);
}
}
if (this.isSparse()) {
for (Long key : this.getCachedKeys()) {
mergedCounts.put(key);
}
}
if (second.isSparse()) {
for (Long key : second.getCachedKeys()) {
mergedCounts.put(key);
}
}
result = new CompactIndex(this.getNumberOfFeatures(), mergedCounts, this.getNumberOfCounts() + second.getNumberOfCounts());
}
return result;