dweiss commented on a change in pull request #163: URL: https://github.com/apache/lucene/pull/163#discussion_r650372453
########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/StateSet.java ########## @@ -0,0 +1,106 @@ +/* + * 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.lucene.util.automaton; + +import java.util.Arrays; +import org.apache.lucene.util.BitMixer; +import org.apache.lucene.util.IntIntHashMap; + +/** A thin wrapper of {@link IntIntHashMap} */ +final class StateSet extends IntSet { + + private final IntIntHashMap inner; + private long hashCode; + private boolean hashUpdated = true; + private boolean arrayUpdated = true; + private int[] arrayCache = new int[0]; + + StateSet(int capacity) { + inner = new IntIntHashMap(capacity); + } + + // Adds this state to the set + void incr(int num) { + if (inner.addTo(num, 1) == 1) { + keyChanged(); + } + } + + // Removes this state from the set, if count decrs to 0 + void decr(int num) { + assert inner.containsKey(num); + int keyIndex = inner.indexOf(num); + int count = inner.indexGet(keyIndex) - 1; + if (count == 0) { + inner.remove(num); + keyChanged(); + } else { + inner.indexReplace(keyIndex, count); + } + } + + /** + * Create a snapshot of this int set associated with a given state. The snapshot will not retain + * any frequency information about the elements of this set, only existence. + * + * @param state the state to associate with the frozen set. + * @return A new FrozenIntSet with the same values as this set. + */ + FrozenIntSet freeze(int state) { + return new FrozenIntSet(getArray(), longHashCode(), state); + } + + private void keyChanged() { + hashUpdated = false; + arrayUpdated = false; + } + + @Override + int[] getArray() { + if (arrayUpdated) { + return arrayCache; + } + arrayCache = new int[inner.size()]; + int i = 0; + for (IntIntHashMap.IntCursor cursor : inner.keys()) { + arrayCache[i++] = cursor.value; + } + // we need to sort this array since "equals" method depend on this + Arrays.sort(arrayCache); + arrayUpdated = true; + return arrayCache; + } + + @Override + int size() { + return inner.size(); + } + + @Override + long longHashCode() { + if (hashUpdated) { + return hashCode; + } + hashCode = inner.size(); + for (IntIntHashMap.IntCursor cursor : inner.keys()) { + hashCode += BitMixer.mix(cursor.value); Review comment: I'd still think the simple aggregation 37 * hashCode + cursor.value with a final mix step would be a good enough (or maybe even better) choice here. ########## File path: lucene/core/src/java/org/apache/lucene/util/BitMixer.java ########## @@ -0,0 +1,130 @@ +/* + * 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.lucene.util; + +/** + * Bit mixing utilities. The purpose of these methods is to evenly distribute key space over int32 + * range. + * + * <p>Forked from com.carrotsearch.hppc.BitMixer Review comment: I'd make it a link back to the original project and include a comment which hash ref of the repository this was copied from, for reference and in case somebody wishes to sync/update in the future. ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/StateSet.java ########## @@ -0,0 +1,106 @@ +/* + * 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.lucene.util.automaton; + +import java.util.Arrays; +import org.apache.lucene.util.BitMixer; +import org.apache.lucene.util.IntIntHashMap; + +/** A thin wrapper of {@link IntIntHashMap} */ +final class StateSet extends IntSet { + + private final IntIntHashMap inner; + private long hashCode; + private boolean hashUpdated = true; + private boolean arrayUpdated = true; + private int[] arrayCache = new int[0]; + + StateSet(int capacity) { + inner = new IntIntHashMap(capacity); + } + + // Adds this state to the set + void incr(int num) { + if (inner.addTo(num, 1) == 1) { + keyChanged(); + } + } + + // Removes this state from the set, if count decrs to 0 + void decr(int num) { + assert inner.containsKey(num); + int keyIndex = inner.indexOf(num); + int count = inner.indexGet(keyIndex) - 1; + if (count == 0) { + inner.remove(num); + keyChanged(); + } else { + inner.indexReplace(keyIndex, count); + } + } + + /** + * Create a snapshot of this int set associated with a given state. The snapshot will not retain + * any frequency information about the elements of this set, only existence. + * + * @param state the state to associate with the frozen set. + * @return A new FrozenIntSet with the same values as this set. + */ + FrozenIntSet freeze(int state) { + return new FrozenIntSet(getArray(), longHashCode(), state); + } + + private void keyChanged() { + hashUpdated = false; + arrayUpdated = false; + } + + @Override + int[] getArray() { + if (arrayUpdated) { + return arrayCache; + } + arrayCache = new int[inner.size()]; + int i = 0; + for (IntIntHashMap.IntCursor cursor : inner.keys()) { + arrayCache[i++] = cursor.value; + } + // we need to sort this array since "equals" method depend on this + Arrays.sort(arrayCache); + arrayUpdated = true; + return arrayCache; + } + + @Override + int size() { + return inner.size(); + } + + @Override + long longHashCode() { + if (hashUpdated) { + return hashCode; + } + hashCode = inner.size(); + for (IntIntHashMap.IntCursor cursor : inner.keys()) { + hashCode += BitMixer.mix(cursor.value); Review comment: Duh. Of course! Sorry for not noticing this. -- 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: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org