AlexanderSaydakov commented on a change in pull request #324: URL: https://github.com/apache/incubator-datasketches-java/pull/324#discussion_r455465567
########## File path: src/main/java/org/apache/datasketches/theta/AnotBimpl.java ########## @@ -0,0 +1,266 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.datasketches.theta; + +import static org.apache.datasketches.HashOperations.convertToHashTable; +import static org.apache.datasketches.HashOperations.hashSearch; +import static org.apache.datasketches.Util.REBUILD_THRESHOLD; +import static org.apache.datasketches.Util.checkSeedHashes; +import static org.apache.datasketches.Util.simpleIntLog2; + +import java.util.Arrays; + +import org.apache.datasketches.SketchesArgumentException; +import org.apache.datasketches.memory.Memory; +import org.apache.datasketches.memory.WritableMemory; + +/** + * Implements the A-and-not-B operations. + * @author Lee Rhodes + * @author Kevin Lang + */ +final class AnotBimpl extends AnotB { + private final short seedHash_; + private boolean empty_; + private long thetaLong_; + private long[] hashArr_ = new long[0]; //compact array w curCount_ entries + private int curCount_; + + /** + * Construct a new AnotB SetOperation on the java heap. Called by SetOperation.Builder. + * + * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See seed</a> + */ + AnotBimpl(final long seed) { + this(computeSeedHash(seed)); + } + + /** + * Construct a new AnotB SetOperation on the java heap. Called by PairwiseSetOperation. + * + * @param seedHash 16 bit hash of the chosen update seed. + */ + AnotBimpl(final short seedHash) { + seedHash_ = seedHash; + reset(); + } + + @Override + public void setA(final Sketch skA) { + if (skA == null) { + reset(); + throw new SketchesArgumentException("The input argument must not be null"); + } + if (skA.isEmpty()) { + reset(); + return; + } + //skA is not empty + checkSeedHashes(seedHash_, skA.getSeedHash()); + empty_ = false; + thetaLong_ = skA.getThetaLong(); + final CompactSketch cskA = (skA instanceof CompactSketch) + ? (CompactSketch) skA + : ((UpdateSketch) skA).compact(); + hashArr_ = skA.isDirect() ? cskA.getCache() : cskA.getCache().clone(); + curCount_ = cskA.getRetainedEntries(true); + } + + @Override + public void notB(final Sketch skB) { + if (empty_ || (skB == null) || skB.isEmpty()) { return; } + //skB is not empty + checkSeedHashes(seedHash_, skB.getSeedHash()); + final long thetaLongB = skB.getThetaLong(); + thetaLong_ = Math.min(thetaLong_, thetaLongB); + + //Build hashtable and removes hashes of skB >= theta + final int countB = skB.getRetainedEntries(true); + CompactSketch cskB = null; + UpdateSketch uskB = null; + final long[] hashTableB; + if (skB instanceof CompactSketch) { + cskB = (CompactSketch) skB; + hashTableB = convertToHashTable(cskB.getCache(), countB, thetaLong_, REBUILD_THRESHOLD); + } else { + uskB = (UpdateSketch) skB; + hashTableB = (thetaLong_ < thetaLongB) + ? convertToHashTable(uskB.getCache(), countB, thetaLong_, REBUILD_THRESHOLD) Review comment: why convert hash table to another hash table? theta check can be done before looking up in existing one ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
