zhaih commented on a change in pull request #225: URL: https://github.com/apache/lucene/pull/225#discussion_r677669213
########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/NFARunAutomaton.java ########## @@ -0,0 +1,225 @@ +/* + * 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 java.util.HashMap; +import java.util.Map; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.hppc.BitMixer; + +/** + * A RunAutomaton that does not require DFA, it will determinize and memorize the generated DFA + * state along with the run + * + * <p>implemented based on: https://swtch.com/~rsc/regexp/regexp1.html + */ +public class NFARunAutomaton { + + /** state ordinal of "no such state" */ + public static final int MISSING = -1; + + private static final int NOT_COMPUTED = -2; + + private final Automaton automaton; + private final int[] points; + private final Map<DState, Integer> dStateToOrd = new HashMap<>(); // could init lazily? + private DState[] dStates; + private final int alphabetSize; + + /** + * Constructor, assuming alphabet size is the whole codepoint space Review comment: ++ ########## File path: lucene/core/src/test/org/apache/lucene/util/automaton/TestNFARunAutomaton.java ########## @@ -0,0 +1,83 @@ +/* + * 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.IntsRef; +import org.apache.lucene.util.LuceneTestCase; + +public class TestNFARunAutomaton extends LuceneTestCase { + + public void testRandom() { + for (int i = 0; i < 100; i++) { + RegExp regExp = null; + while (regExp == null) { + try { + regExp = new RegExp(AutomatonTestUtil.randomRegexp(random())); + } catch (IllegalArgumentException e) { + ignoreException(e); + } + } + Automaton dfa = regExp.toAutomaton(); Review comment: Yeah I think that's better, will change it. ########## File path: lucene/core/src/test/org/apache/lucene/util/automaton/TestNFARunAutomaton.java ########## @@ -0,0 +1,83 @@ +/* + * 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.IntsRef; +import org.apache.lucene.util.LuceneTestCase; + +public class TestNFARunAutomaton extends LuceneTestCase { + + public void testRandom() { + for (int i = 0; i < 100; i++) { + RegExp regExp = null; + while (regExp == null) { + try { + regExp = new RegExp(AutomatonTestUtil.randomRegexp(random())); + } catch (IllegalArgumentException e) { + ignoreException(e); Review comment: Yeah ecj is complaining that :( I haven't dived too deep into why the `randomRegexp` generates invalid `RegExp`, maybe I'll just open another issue to fix that behavior? ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/NFARunAutomaton.java ########## @@ -0,0 +1,225 @@ +/* + * 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 java.util.HashMap; +import java.util.Map; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.hppc.BitMixer; + +/** + * A RunAutomaton that does not require DFA, it will determinize and memorize the generated DFA + * state along with the run + * + * <p>implemented based on: https://swtch.com/~rsc/regexp/regexp1.html + */ +public class NFARunAutomaton { + + /** state ordinal of "no such state" */ + public static final int MISSING = -1; + + private static final int NOT_COMPUTED = -2; + + private final Automaton automaton; + private final int[] points; + private final Map<DState, Integer> dStateToOrd = new HashMap<>(); // could init lazily? + private DState[] dStates; + private final int alphabetSize; + + /** + * Constructor, assuming alphabet size is the whole codepoint space + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} for + * better efficiency + */ + public NFARunAutomaton(Automaton automaton) { + this(automaton, Character.MAX_CODE_POINT); + } + + /** + * Constructor + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} * + * for better efficiency + * @param alphabetSize alphabet size + */ + public NFARunAutomaton(Automaton automaton, int alphabetSize) { + this.automaton = automaton; + points = automaton.getStartPoints(); + this.alphabetSize = alphabetSize; + dStates = new DState[10]; + findDState(new DState(new int[] {0})); + } + + /** + * For a given state and an incoming character (codepoint), return the next state + * + * @param state incoming state, should either be 0 or some state that is returned previously by + * this function + * @param c codepoint + * @return the next state or {@link #MISSING} if the transition doesn't exist + */ + public int step(int state, int c) { + assert dStates[state] != null; + return step(dStates[state], c); + } + + /** + * Run through a given codepoint array, return accepted or not, should only be used in test + * + * @param s String represented by an int array + * @return accept or not + */ + boolean run(int[] s) { + int p = 0; + for (int c : s) { + p = step(p, c); + if (p == MISSING) return false; + } + return dStates[p].isAccept; + } + + /** + * From an existing DFA state, step to next DFA state given character c if the transition is + * previously tried then this operation will just use the cached result, otherwise it will call + * {@link #step(int[], int)} to get the next state and cache the result + */ + private int step(DState dState, int c) { + int charClass = getCharClass(c); + if (dState.nextState(charClass) == NOT_COMPUTED) { + // the next dfa state has not been computed yet + dState.setNextState(charClass, findDState(step(dState.nfaStates, c))); + } + return dState.nextState(charClass); + } + + /** + * given a list of NFA states and a character c, compute the output list of NFA state which is + * wrapped as a DFA state + */ + private DState step(int[] nfaStates, int c) { + Transition transition = new Transition(); + StateSet stateSet = new StateSet(5); // fork IntHashSet from hppc instead? + int numTransitions; + for (int nfaState : nfaStates) { + numTransitions = automaton.initTransition(nfaState, transition); + for (int i = 0; i < numTransitions; i++) { + automaton.getNextTransition(transition); + if (transition.min <= c && transition.max >= c) { + stateSet.incr(transition.dest); + } + } + } + if (stateSet.size() == 0) { + return null; + } + return new DState(stateSet.getArray()); + } + + /** + * return the ordinal of given DFA state, generate a new ordinal if the given DFA state is a new + * one + */ + private int findDState(DState dState) { + if (dState == null) { + return MISSING; + } + int ord = dStateToOrd.getOrDefault(dState, -1); + if (ord >= 0) { + return ord; + } + ord = dStateToOrd.size(); + dStateToOrd.put(dState, ord); + assert ord >= dStates.length || dStates[ord] == null; + if (ord >= dStates.length) { + dStates = ArrayUtil.grow(dStates, ord + 1); + } + dStates[ord] = dState; + return ord; + } + + /** Gets character class of given codepoint */ + final int getCharClass(int c) { + assert c < alphabetSize; + // binary search + int a = 0; + int b = points.length; + while (b - a > 1) { + int d = (a + b) >>> 1; + if (points[d] > c) b = d; + else if (points[d] < c) a = d; + else return d; + } + return a; + } + + private class DState { + private final int[] nfaStates; + private int[] transitions; Review comment: ++ ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/NFARunAutomaton.java ########## @@ -0,0 +1,225 @@ +/* + * 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 java.util.HashMap; +import java.util.Map; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.hppc.BitMixer; + +/** + * A RunAutomaton that does not require DFA, it will determinize and memorize the generated DFA + * state along with the run + * + * <p>implemented based on: https://swtch.com/~rsc/regexp/regexp1.html + */ +public class NFARunAutomaton { + + /** state ordinal of "no such state" */ + public static final int MISSING = -1; + + private static final int NOT_COMPUTED = -2; + + private final Automaton automaton; + private final int[] points; + private final Map<DState, Integer> dStateToOrd = new HashMap<>(); // could init lazily? + private DState[] dStates; + private final int alphabetSize; + + /** + * Constructor, assuming alphabet size is the whole codepoint space + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} for + * better efficiency + */ + public NFARunAutomaton(Automaton automaton) { + this(automaton, Character.MAX_CODE_POINT); + } + + /** + * Constructor + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} * + * for better efficiency + * @param alphabetSize alphabet size + */ + public NFARunAutomaton(Automaton automaton, int alphabetSize) { + this.automaton = automaton; + points = automaton.getStartPoints(); + this.alphabetSize = alphabetSize; + dStates = new DState[10]; + findDState(new DState(new int[] {0})); + } + + /** + * For a given state and an incoming character (codepoint), return the next state + * + * @param state incoming state, should either be 0 or some state that is returned previously by + * this function + * @param c codepoint + * @return the next state or {@link #MISSING} if the transition doesn't exist + */ + public int step(int state, int c) { + assert dStates[state] != null; + return step(dStates[state], c); + } + + /** + * Run through a given codepoint array, return accepted or not, should only be used in test + * + * @param s String represented by an int array + * @return accept or not + */ + boolean run(int[] s) { + int p = 0; + for (int c : s) { + p = step(p, c); + if (p == MISSING) return false; + } + return dStates[p].isAccept; + } + + /** + * From an existing DFA state, step to next DFA state given character c if the transition is + * previously tried then this operation will just use the cached result, otherwise it will call + * {@link #step(int[], int)} to get the next state and cache the result + */ + private int step(DState dState, int c) { + int charClass = getCharClass(c); + if (dState.nextState(charClass) == NOT_COMPUTED) { + // the next dfa state has not been computed yet + dState.setNextState(charClass, findDState(step(dState.nfaStates, c))); + } + return dState.nextState(charClass); + } + + /** + * given a list of NFA states and a character c, compute the output list of NFA state which is + * wrapped as a DFA state + */ + private DState step(int[] nfaStates, int c) { + Transition transition = new Transition(); + StateSet stateSet = new StateSet(5); // fork IntHashSet from hppc instead? + int numTransitions; + for (int nfaState : nfaStates) { + numTransitions = automaton.initTransition(nfaState, transition); + for (int i = 0; i < numTransitions; i++) { + automaton.getNextTransition(transition); + if (transition.min <= c && transition.max >= c) { + stateSet.incr(transition.dest); + } + } + } + if (stateSet.size() == 0) { + return null; + } + return new DState(stateSet.getArray()); + } + + /** + * return the ordinal of given DFA state, generate a new ordinal if the given DFA state is a new + * one + */ + private int findDState(DState dState) { + if (dState == null) { + return MISSING; + } + int ord = dStateToOrd.getOrDefault(dState, -1); + if (ord >= 0) { + return ord; + } + ord = dStateToOrd.size(); + dStateToOrd.put(dState, ord); + assert ord >= dStates.length || dStates[ord] == null; + if (ord >= dStates.length) { + dStates = ArrayUtil.grow(dStates, ord + 1); + } + dStates[ord] = dState; + return ord; + } + + /** Gets character class of given codepoint */ + final int getCharClass(int c) { + assert c < alphabetSize; + // binary search + int a = 0; + int b = points.length; + while (b - a > 1) { + int d = (a + b) >>> 1; + if (points[d] > c) b = d; + else if (points[d] < c) a = d; + else return d; + } + return a; + } + + private class DState { + private final int[] nfaStates; + private int[] transitions; + private final int hashCode; + private final boolean isAccept; + + private DState(int[] nfaStates) { + assert nfaStates != null && nfaStates.length > 0; + this.nfaStates = nfaStates; + int hashCode = nfaStates.length; + boolean isAccept = false; + for (int s : nfaStates) { + hashCode += BitMixer.mix(s); + if (automaton.isAccept(s)) { + isAccept = true; + } + } + this.isAccept = isAccept; + this.hashCode = hashCode; + } + + private int nextState(int charClass) { + initTransitions(); + assert charClass < transitions.length; + return transitions[charClass]; + } + + private void setNextState(int charClass, int nextState) { + initTransitions(); + assert charClass < transitions.length; + transitions[charClass] = nextState; Review comment: ++ ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/NFARunAutomaton.java ########## @@ -0,0 +1,225 @@ +/* + * 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 java.util.HashMap; +import java.util.Map; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.hppc.BitMixer; + +/** + * A RunAutomaton that does not require DFA, it will determinize and memorize the generated DFA + * state along with the run + * + * <p>implemented based on: https://swtch.com/~rsc/regexp/regexp1.html + */ +public class NFARunAutomaton { + + /** state ordinal of "no such state" */ + public static final int MISSING = -1; + + private static final int NOT_COMPUTED = -2; + + private final Automaton automaton; + private final int[] points; + private final Map<DState, Integer> dStateToOrd = new HashMap<>(); // could init lazily? + private DState[] dStates; + private final int alphabetSize; + + /** + * Constructor, assuming alphabet size is the whole codepoint space + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} for + * better efficiency + */ + public NFARunAutomaton(Automaton automaton) { + this(automaton, Character.MAX_CODE_POINT); + } + + /** + * Constructor + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} * + * for better efficiency + * @param alphabetSize alphabet size + */ + public NFARunAutomaton(Automaton automaton, int alphabetSize) { + this.automaton = automaton; + points = automaton.getStartPoints(); + this.alphabetSize = alphabetSize; + dStates = new DState[10]; + findDState(new DState(new int[] {0})); + } + + /** + * For a given state and an incoming character (codepoint), return the next state + * + * @param state incoming state, should either be 0 or some state that is returned previously by + * this function + * @param c codepoint + * @return the next state or {@link #MISSING} if the transition doesn't exist + */ + public int step(int state, int c) { + assert dStates[state] != null; + return step(dStates[state], c); + } + + /** + * Run through a given codepoint array, return accepted or not, should only be used in test + * + * @param s String represented by an int array + * @return accept or not + */ + boolean run(int[] s) { + int p = 0; + for (int c : s) { + p = step(p, c); + if (p == MISSING) return false; + } + return dStates[p].isAccept; + } + + /** + * From an existing DFA state, step to next DFA state given character c if the transition is + * previously tried then this operation will just use the cached result, otherwise it will call + * {@link #step(int[], int)} to get the next state and cache the result + */ + private int step(DState dState, int c) { + int charClass = getCharClass(c); + if (dState.nextState(charClass) == NOT_COMPUTED) { + // the next dfa state has not been computed yet + dState.setNextState(charClass, findDState(step(dState.nfaStates, c))); + } + return dState.nextState(charClass); + } + + /** + * given a list of NFA states and a character c, compute the output list of NFA state which is + * wrapped as a DFA state + */ + private DState step(int[] nfaStates, int c) { + Transition transition = new Transition(); + StateSet stateSet = new StateSet(5); // fork IntHashSet from hppc instead? + int numTransitions; + for (int nfaState : nfaStates) { + numTransitions = automaton.initTransition(nfaState, transition); + for (int i = 0; i < numTransitions; i++) { + automaton.getNextTransition(transition); + if (transition.min <= c && transition.max >= c) { + stateSet.incr(transition.dest); + } + } + } + if (stateSet.size() == 0) { + return null; + } + return new DState(stateSet.getArray()); + } + + /** + * return the ordinal of given DFA state, generate a new ordinal if the given DFA state is a new + * one + */ + private int findDState(DState dState) { + if (dState == null) { + return MISSING; + } + int ord = dStateToOrd.getOrDefault(dState, -1); + if (ord >= 0) { + return ord; + } + ord = dStateToOrd.size(); + dStateToOrd.put(dState, ord); + assert ord >= dStates.length || dStates[ord] == null; + if (ord >= dStates.length) { + dStates = ArrayUtil.grow(dStates, ord + 1); + } + dStates[ord] = dState; + return ord; + } + + /** Gets character class of given codepoint */ + final int getCharClass(int c) { + assert c < alphabetSize; + // binary search + int a = 0; + int b = points.length; + while (b - a > 1) { + int d = (a + b) >>> 1; + if (points[d] > c) b = d; + else if (points[d] < c) a = d; + else return d; + } + return a; + } + + private class DState { Review comment: It's slightly different, basically `DState` owns this `transitions` array that manages the outgoing transitions which `FrozenIntSet` doesn't have. I'm thinking of making `FrozenIntSet` a part of this class so that we can reuse what's there. ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/NFARunAutomaton.java ########## @@ -0,0 +1,225 @@ +/* + * 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 java.util.HashMap; +import java.util.Map; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.hppc.BitMixer; + +/** + * A RunAutomaton that does not require DFA, it will determinize and memorize the generated DFA + * state along with the run + * + * <p>implemented based on: https://swtch.com/~rsc/regexp/regexp1.html + */ +public class NFARunAutomaton { + + /** state ordinal of "no such state" */ + public static final int MISSING = -1; + + private static final int NOT_COMPUTED = -2; + + private final Automaton automaton; + private final int[] points; + private final Map<DState, Integer> dStateToOrd = new HashMap<>(); // could init lazily? + private DState[] dStates; + private final int alphabetSize; + + /** + * Constructor, assuming alphabet size is the whole codepoint space + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} for + * better efficiency + */ + public NFARunAutomaton(Automaton automaton) { + this(automaton, Character.MAX_CODE_POINT); + } + + /** + * Constructor + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} * + * for better efficiency + * @param alphabetSize alphabet size + */ + public NFARunAutomaton(Automaton automaton, int alphabetSize) { + this.automaton = automaton; + points = automaton.getStartPoints(); + this.alphabetSize = alphabetSize; + dStates = new DState[10]; + findDState(new DState(new int[] {0})); + } + + /** + * For a given state and an incoming character (codepoint), return the next state + * + * @param state incoming state, should either be 0 or some state that is returned previously by + * this function + * @param c codepoint + * @return the next state or {@link #MISSING} if the transition doesn't exist + */ + public int step(int state, int c) { + assert dStates[state] != null; + return step(dStates[state], c); + } + + /** + * Run through a given codepoint array, return accepted or not, should only be used in test + * + * @param s String represented by an int array + * @return accept or not + */ + boolean run(int[] s) { + int p = 0; + for (int c : s) { + p = step(p, c); + if (p == MISSING) return false; + } + return dStates[p].isAccept; + } + + /** + * From an existing DFA state, step to next DFA state given character c if the transition is + * previously tried then this operation will just use the cached result, otherwise it will call + * {@link #step(int[], int)} to get the next state and cache the result + */ + private int step(DState dState, int c) { + int charClass = getCharClass(c); + if (dState.nextState(charClass) == NOT_COMPUTED) { + // the next dfa state has not been computed yet + dState.setNextState(charClass, findDState(step(dState.nfaStates, c))); + } + return dState.nextState(charClass); + } + + /** + * given a list of NFA states and a character c, compute the output list of NFA state which is + * wrapped as a DFA state + */ + private DState step(int[] nfaStates, int c) { + Transition transition = new Transition(); + StateSet stateSet = new StateSet(5); // fork IntHashSet from hppc instead? + int numTransitions; + for (int nfaState : nfaStates) { + numTransitions = automaton.initTransition(nfaState, transition); + for (int i = 0; i < numTransitions; i++) { + automaton.getNextTransition(transition); + if (transition.min <= c && transition.max >= c) { + stateSet.incr(transition.dest); + } + } + } + if (stateSet.size() == 0) { + return null; + } + return new DState(stateSet.getArray()); + } + + /** + * return the ordinal of given DFA state, generate a new ordinal if the given DFA state is a new + * one + */ + private int findDState(DState dState) { + if (dState == null) { + return MISSING; + } + int ord = dStateToOrd.getOrDefault(dState, -1); + if (ord >= 0) { + return ord; + } + ord = dStateToOrd.size(); + dStateToOrd.put(dState, ord); + assert ord >= dStates.length || dStates[ord] == null; + if (ord >= dStates.length) { + dStates = ArrayUtil.grow(dStates, ord + 1); + } + dStates[ord] = dState; + return ord; + } + + /** Gets character class of given codepoint */ + final int getCharClass(int c) { Review comment: Yeah we need to refactor `RunAutomaton` a little bit, eventually we should use it directly instead of forking the code. ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/MinimizationOperations.java ########## @@ -53,6 +53,11 @@ private MinimizationOperations() {} * what to specify. */ public static Automaton minimize(Automaton a, int determinizeWorkLimit) { + // nocommit: probably shouldn't set the logic here Review comment: Just a fast way to let me generate a NFA instead of DFA from the `RegExp`, I'm not sure whether set `determinizeWorkLimit` to 0 is a good way or not to indicate that we're going to generate the NFA. ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/NFARunAutomaton.java ########## @@ -0,0 +1,225 @@ +/* + * 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 java.util.HashMap; +import java.util.Map; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.hppc.BitMixer; + +/** + * A RunAutomaton that does not require DFA, it will determinize and memorize the generated DFA + * state along with the run + * + * <p>implemented based on: https://swtch.com/~rsc/regexp/regexp1.html + */ +public class NFARunAutomaton { + + /** state ordinal of "no such state" */ + public static final int MISSING = -1; + + private static final int NOT_COMPUTED = -2; + + private final Automaton automaton; + private final int[] points; + private final Map<DState, Integer> dStateToOrd = new HashMap<>(); // could init lazily? + private DState[] dStates; + private final int alphabetSize; + + /** + * Constructor, assuming alphabet size is the whole codepoint space + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} for + * better efficiency + */ + public NFARunAutomaton(Automaton automaton) { + this(automaton, Character.MAX_CODE_POINT); + } + + /** + * Constructor + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} * + * for better efficiency + * @param alphabetSize alphabet size + */ + public NFARunAutomaton(Automaton automaton, int alphabetSize) { + this.automaton = automaton; + points = automaton.getStartPoints(); + this.alphabetSize = alphabetSize; + dStates = new DState[10]; + findDState(new DState(new int[] {0})); + } + + /** + * For a given state and an incoming character (codepoint), return the next state + * + * @param state incoming state, should either be 0 or some state that is returned previously by + * this function + * @param c codepoint + * @return the next state or {@link #MISSING} if the transition doesn't exist + */ + public int step(int state, int c) { + assert dStates[state] != null; + return step(dStates[state], c); + } + + /** + * Run through a given codepoint array, return accepted or not, should only be used in test + * + * @param s String represented by an int array + * @return accept or not + */ + boolean run(int[] s) { Review comment: Probably yes and probably no? Current structure of `RunAutomaton` hierarchy is `RunAutomaton` has only `step` method while the children own their own `run` method, I haven't figured out a good way to incorporate this new NFA to the current structure. One idea is to let the children, such as `ByteRunAutomaton` not to extend `RunAutomaton` but use it instead, so it can control which `RunAutomaton` to use (NFA or DFA version). ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/NFARunAutomaton.java ########## @@ -0,0 +1,225 @@ +/* + * 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 java.util.HashMap; +import java.util.Map; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.hppc.BitMixer; + +/** + * A RunAutomaton that does not require DFA, it will determinize and memorize the generated DFA + * state along with the run + * + * <p>implemented based on: https://swtch.com/~rsc/regexp/regexp1.html + */ +public class NFARunAutomaton { + + /** state ordinal of "no such state" */ + public static final int MISSING = -1; + + private static final int NOT_COMPUTED = -2; + + private final Automaton automaton; + private final int[] points; + private final Map<DState, Integer> dStateToOrd = new HashMap<>(); // could init lazily? + private DState[] dStates; + private final int alphabetSize; + + /** + * Constructor, assuming alphabet size is the whole codepoint space + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} for + * better efficiency + */ + public NFARunAutomaton(Automaton automaton) { + this(automaton, Character.MAX_CODE_POINT); + } + + /** + * Constructor + * + * @param automaton incoming automaton, should be NFA, for DFA please use {@link RunAutomaton} * + * for better efficiency + * @param alphabetSize alphabet size + */ + public NFARunAutomaton(Automaton automaton, int alphabetSize) { + this.automaton = automaton; + points = automaton.getStartPoints(); + this.alphabetSize = alphabetSize; + dStates = new DState[10]; + findDState(new DState(new int[] {0})); + } + + /** + * For a given state and an incoming character (codepoint), return the next state + * + * @param state incoming state, should either be 0 or some state that is returned previously by + * this function + * @param c codepoint + * @return the next state or {@link #MISSING} if the transition doesn't exist + */ + public int step(int state, int c) { + assert dStates[state] != null; + return step(dStates[state], c); + } + + /** + * Run through a given codepoint array, return accepted or not, should only be used in test + * + * @param s String represented by an int array + * @return accept or not + */ + boolean run(int[] s) { + int p = 0; + for (int c : s) { + p = step(p, c); + if (p == MISSING) return false; + } + return dStates[p].isAccept; + } + + /** + * From an existing DFA state, step to next DFA state given character c if the transition is + * previously tried then this operation will just use the cached result, otherwise it will call + * {@link #step(int[], int)} to get the next state and cache the result + */ + private int step(DState dState, int c) { + int charClass = getCharClass(c); + if (dState.nextState(charClass) == NOT_COMPUTED) { + // the next dfa state has not been computed yet + dState.setNextState(charClass, findDState(step(dState.nfaStates, c))); + } + return dState.nextState(charClass); + } + + /** + * given a list of NFA states and a character c, compute the output list of NFA state which is + * wrapped as a DFA state + */ + private DState step(int[] nfaStates, int c) { + Transition transition = new Transition(); + StateSet stateSet = new StateSet(5); // fork IntHashSet from hppc instead? + int numTransitions; + for (int nfaState : nfaStates) { + numTransitions = automaton.initTransition(nfaState, transition); + for (int i = 0; i < numTransitions; i++) { + automaton.getNextTransition(transition); + if (transition.min <= c && transition.max >= c) { + stateSet.incr(transition.dest); + } + } + } + if (stateSet.size() == 0) { + return null; + } + return new DState(stateSet.getArray()); + } + + /** + * return the ordinal of given DFA state, generate a new ordinal if the given DFA state is a new + * one + */ + private int findDState(DState dState) { + if (dState == null) { + return MISSING; + } + int ord = dStateToOrd.getOrDefault(dState, -1); + if (ord >= 0) { + return ord; + } + ord = dStateToOrd.size(); + dStateToOrd.put(dState, ord); + assert ord >= dStates.length || dStates[ord] == null; + if (ord >= dStates.length) { + dStates = ArrayUtil.grow(dStates, ord + 1); + } + dStates[ord] = dState; + return ord; + } + + /** Gets character class of given codepoint */ + final int getCharClass(int c) { + assert c < alphabetSize; + // binary search + int a = 0; + int b = points.length; + while (b - a > 1) { + int d = (a + b) >>> 1; + if (points[d] > c) b = d; + else if (points[d] < c) a = d; + else return d; + } + return a; + } + + private class DState { + private final int[] nfaStates; + private int[] transitions; + private final int hashCode; + private final boolean isAccept; + + private DState(int[] nfaStates) { + assert nfaStates != null && nfaStates.length > 0; + this.nfaStates = nfaStates; + int hashCode = nfaStates.length; + boolean isAccept = false; + for (int s : nfaStates) { + hashCode += BitMixer.mix(s); + if (automaton.isAccept(s)) { + isAccept = true; + } + } + this.isAccept = isAccept; + this.hashCode = hashCode; + } + + private int nextState(int charClass) { + initTransitions(); + assert charClass < transitions.length; + return transitions[charClass]; Review comment: Good idea, will do -- 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. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org 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