DocSet.andNot(),andNotSize()

git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@413399 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2006-06-11 02:18:38 +00:00
parent 0c69b09c6e
commit 3a810792bf
6 changed files with 43 additions and 6 deletions

View File

@ -17,6 +17,7 @@ New Features
9. Added KeywordTokenizerFactory (hossman)
10. copyField accepts dynamicfield-like names as the source.
(Darren Erik Vengroff via yonik, SOLR-21)
11. new DocSet.andNot(), DocSet.andNotSize() (yonik)
Changes in runtime behavior
1. classes reorganized into different packages, package names changed to Apache

View File

@ -143,6 +143,7 @@ public class BitDocSet extends DocSetBase {
return bits.get(doc);
}
@Override
public int intersectionSize(DocSet other) {
if (other instanceof BitDocSet) {
return (int)OpenBitSet.intersectionCount(this.bits, ((BitDocSet)other).bits);
@ -152,6 +153,7 @@ public class BitDocSet extends DocSetBase {
}
}
@Override
public int unionSize(DocSet other) {
if (other instanceof BitDocSet) {
return (int)OpenBitSet.unionCount(this.bits, ((BitDocSet)other).bits);
@ -161,6 +163,18 @@ public class BitDocSet extends DocSetBase {
}
}
@Override
public int andNotSize(DocSet other) {
if (other instanceof BitDocSet) {
// if we don't know our current size, this is faster than
// size - intersection_size
return (int)OpenBitSet.andNotCount(this.bits, ((BitDocSet)other).bits);
} else {
// use BaseDocSet's size-intersection_size
return super.andNotSize(other);
}
}
public long memSize() {
return (bits.getBits().length << 3) + 16;
}

View File

@ -124,6 +124,17 @@ public interface DocSet /* extends Collection<Integer> */ {
*/
public int unionSize(DocSet other);
/**
* Returns the documents in this set that are not in the other set. Neither set is modified - a new DocSet is
* created and returned.
* @return a DocSet representing this AND NOT other
*/
public DocSet andNot(DocSet other);
/**
* Returns the number of documents in this set that are not in the other set.
*/
public int andNotSize(DocSet other);
}
/** A base class that may be usefull for implementing DocSets */
@ -208,11 +219,20 @@ abstract class DocSetBase implements DocSet {
return intersection(other).size();
}
// TODO: do an efficient implementation
// subclasses have more efficient implementations
public int unionSize(DocSet other) {
return union(other).size();
}
public DocSet andNot(DocSet other) {
OpenBitSet newbits = (OpenBitSet)(this.getBits().clone());
newbits.andNot(other.getBits());
return new BitDocSet(newbits);
}
public int andNotSize(DocSet other) {
return this.size() - this.intersectionSize(other);
}
}

View File

@ -278,7 +278,7 @@ public final class HashDocSet extends DocSetBase {
public int unionSize(DocSet other) {
if (other instanceof HashDocSet) {
// set "a" to the smallest doc set for the most efficient
// intersection.
// union count.
final HashDocSet a = size()<=other.size() ? this : (HashDocSet)other;
final HashDocSet b = size()<=other.size() ? (HashDocSet)other : this;
@ -302,5 +302,6 @@ public final class HashDocSet extends DocSetBase {
}
}
// don't implement andNotSize() on purpose... if one of the sets is a HashDocSet,
// its easier to get the intersection size and subtract (implementation in BaseDocSet)
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.apache.solr.search.test;
package org.apache.solr.search;
import org.apache.solr.search.BitDocSet;
import org.apache.solr.search.HashDocSet;

View File

@ -64,14 +64,15 @@ public class TestDocSet extends TestCase {
OpenBitSet a_and = (OpenBitSet)a1.clone(); a_and.and(a2);
OpenBitSet a_or = (OpenBitSet)a1.clone(); a_or.or(a2);
// OpenBitSet a_xor = (OpenBitSet)a1.clone(); a_xor.xor(a2);
// OpenBitSet a_andn = (OpenBitSet)a1.clone(); a_andn.andNot(a2);
OpenBitSet a_andn = (OpenBitSet)a1.clone(); a_andn.andNot(a2);
checkEqual(a_and, b1.intersection(b2));
checkEqual(a_or, b1.union(b2));
checkEqual(a_andn, b1.andNot(b2));
assertEquals(a_and.cardinality(), b1.intersectionSize(b2));
assertEquals(a_or.cardinality(), b1.unionSize(b2));
assertEquals(a_andn.cardinality(), b1.andNotSize(b2));
}