-
Notifications
You must be signed in to change notification settings - Fork 162
use a open addressing map to store folding paths #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ | |
| */ | ||
| class Inst { | ||
|
|
||
| private static final int RUNES_LINER_SEARCH_SIZE = 10; | ||
|
|
||
| enum Op { | ||
| ALT, | ||
| ALT_MATCH, | ||
|
|
@@ -57,25 +59,37 @@ boolean matchRune(int r) { | |
| // Special case: single-rune slice is from literal string, not char | ||
| // class. | ||
| if (runes.length == 1) { | ||
| int r0 = runes[0]; | ||
| if (r == r0) { | ||
| return true; | ||
| } | ||
| if ((arg & RE2.FOLD_CASE) != 0) { | ||
| for (int r1 = Unicode.simpleFold(r0); | ||
| r1 != r0; | ||
| r1 = Unicode.simpleFold(r1)) { | ||
| if (r == r1) { | ||
| return true; | ||
| } | ||
| return singleMatchRune(r); | ||
| } | ||
|
|
||
| return multiMatchRune(r); | ||
| } | ||
|
|
||
| private boolean singleMatchRune(int r) { | ||
| int r0 = runes[0]; | ||
| if (r == r0) { | ||
| return true; | ||
| } | ||
|
|
||
| if ((arg & RE2.FOLD_CASE) != 0) { | ||
| int[] folds = Unicode.optimizedFoldOrbit(r0); | ||
|
|
||
| if (folds == null) { | ||
| return (r == Unicode.optimizedFoldNonOrbit(r0)); | ||
|
||
| } else { | ||
| for(int i = 0; i < folds.length; i++) { | ||
| if (folds[i] == r) return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // Peek at the first few pairs. | ||
| private boolean multiMatchRune(int r) { | ||
| // Peek at the first 5 pairs. | ||
| // Should handle ASCII well. | ||
| for (int j = 0; j < runes.length && j <= 8; j += 2) { | ||
| int length = Math.min(runes.length, RUNES_LINER_SEARCH_SIZE); | ||
| for (int j = 0; j < length; j += 2) { | ||
| if (r < runes[j]) { | ||
| return false; | ||
| } | ||
|
|
@@ -84,12 +98,12 @@ boolean matchRune(int r) { | |
| } | ||
| } | ||
|
|
||
| // Otherwise binary search. | ||
| for (int lo = 0, hi = runes.length / 2; lo < hi; ) { | ||
| // Otherwise binary search on rest of the array | ||
|
||
| for (int lo = 0, hi = (runes.length - RUNES_LINER_SEARCH_SIZE) / 2; lo < hi; ) { | ||
|
||
| int m = lo + (hi - lo) / 2; | ||
| int c = runes[2 * m]; | ||
| int c = runes[2 * m + RUNES_LINER_SEARCH_SIZE]; | ||
| if (c <= r) { | ||
| if (r <= runes[2 * m + 1]) { | ||
| if (r <= runes[2 * m + 1 + RUNES_LINER_SEARCH_SIZE]) { | ||
| return true; | ||
| } | ||
| lo = m + 1; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
|
|
||
| package com.google.re2j; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| /** | ||
| * Utilities for dealing with Unicode better than Java does. | ||
| * | ||
|
|
@@ -227,6 +229,99 @@ static int simpleFold(int r) { | |
| return toUpper(r); | ||
| } | ||
|
|
||
| static int[] optimizedFoldOrbit(int r) { | ||
| if (r >= ORBIT_MIN_VALUE) { | ||
| return FOLD_MAP.get(r); | ||
| } else return null; | ||
|
||
| } | ||
|
|
||
| static int optimizedFoldNonOrbit(int r) { | ||
| int l = toLower(r); | ||
| if (l != r) { | ||
| return l; | ||
| } | ||
| return toUpper(r); | ||
| } | ||
|
|
||
| private static final FoldMap FOLD_MAP = new FoldMap(UnicodeTables.CASE_ORBIT.length * 4); | ||
|
||
| private static final int ORBIT_MIN_VALUE = UnicodeTables.CASE_ORBIT[0][0]; | ||
|
|
||
| static { | ||
| for(int i = 0; i < UnicodeTables.CASE_ORBIT.length; i++) { | ||
|
||
| int r0 = UnicodeTables.CASE_ORBIT[i][0]; | ||
| int[] folds = new int[0]; | ||
| int r1 = r0; | ||
| while((r1 = simpleFold(r1)) != r0) { | ||
| folds = Arrays.copyOf(folds, folds.length + 1); | ||
|
||
| folds[folds.length - 1] = r1; | ||
| } | ||
| FOLD_MAP.put(r0, folds); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| associate a rune to it's unfolded case equivalent | ||
|
||
| */ | ||
| private static class FoldMap { | ||
| private static final int MAX_DISTANCE = 4; | ||
|
||
| private final int[] keys; | ||
| private final int[][] values; | ||
| private final int mask; | ||
| private final int maxDistance; | ||
|
|
||
| FoldMap(int size) { | ||
|
||
| int s = findNextPositivePowerOfTwo(size); | ||
| keys = new int[s]; | ||
| Arrays.fill(keys, -1); | ||
| values = new int[s][]; | ||
| mask = s - 1; | ||
| maxDistance = Math.min(keys.length, MAX_DISTANCE); | ||
| } | ||
|
|
||
| public void put(int k, int[] fold) { | ||
| if (k == -1) throw new IllegalArgumentException("-1 is the empty marker"); | ||
|
|
||
| int hash = hashCode(k); | ||
| int i = 0; | ||
| do { | ||
| int index = (hash + i) & mask; | ||
| if (keys[index] == -1) { // empty slot | ||
|
||
| values[index] = fold; | ||
| keys[index] = k; | ||
| return; | ||
| } | ||
| i++; | ||
| } while (i < maxDistance); | ||
|
|
||
| throw new IllegalStateException("Map is full"); | ||
| } | ||
|
|
||
| public int[] get(int k) { | ||
| int hash = hashCode(k); | ||
|
|
||
| int i = 0; | ||
| do { | ||
| int index = (hash + i) & mask; | ||
|
||
| int key = keys[index]; | ||
| if (key == -1) return null; // empty slot | ||
| if (key == k) return values[index]; | ||
| i++; | ||
| } while (i < maxDistance); | ||
|
|
||
| return null; | ||
| } | ||
| private static final int INT_PHI = 0x9E3779B9; | ||
|
||
|
|
||
| private int hashCode(int k) { | ||
| final int h = k * INT_PHI; | ||
|
||
| return h ^ (h >> 16); | ||
| } | ||
|
|
||
| public static int findNextPositivePowerOfTwo(final int value) { | ||
|
||
| return 1 << (32 - Integer.numberOfLeadingZeros(value - 1)); | ||
| } | ||
| } | ||
|
|
||
| private Unicode() {} // uninstantiable | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package com.google.re2j; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class RunesTest { | ||
|
|
||
| @Test | ||
| public void testRunes() { | ||
| RE2 compile = RE2.compile("[0-13-46-78-9b-ce-fh-i]"); | ||
| assertTrue(compile.match("0")); | ||
| assertTrue(compile.match("1")); | ||
| assertTrue(compile.match("3")); | ||
| assertTrue(compile.match("4")); | ||
| assertTrue(compile.match("6")); | ||
| assertTrue(compile.match("7")); | ||
| assertTrue(compile.match("8")); | ||
| assertTrue(compile.match("9")); | ||
| assertTrue(compile.match("b")); | ||
| assertTrue(compile.match("c")); | ||
| assertTrue(compile.match("e")); | ||
| assertTrue(compile.match("f")); | ||
| assertTrue(compile.match("h")); | ||
| assertTrue(compile.match("i")); | ||
|
|
||
| assertFalse(compile.match("2")); | ||
| assertFalse(compile.match("5")); | ||
| assertFalse(compile.match("a")); | ||
| assertFalse(compile.match("d")); | ||
| assertFalse(compile.match("g")); | ||
| assertFalse(compile.match("j")); | ||
| } | ||
| @Test | ||
| public void testRunesWithFold() { | ||
| Pattern pattern = Pattern.compile("ak", Pattern.CASE_INSENSITIVE); | ||
| String upperCase = "AK"; | ||
| String withKelvin = "AK"; | ||
| assertFalse(withKelvin.equals(upperCase));// check we use the kelvin sign | ||
|
||
|
|
||
| assertTrue(pattern.matches("ak")); | ||
| assertTrue(pattern.matches(upperCase)); | ||
| assertTrue(pattern.matches("aK")); | ||
| assertTrue(pattern.matches("Ak")); | ||
| assertTrue(pattern.matches(withKelvin)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| package com.google.re2j; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import org.junit.Test; | ||
|
|
@@ -38,4 +39,24 @@ public void testFoldConstants() { | |
| // int toLower(int r); | ||
| // int simpleFold(int r); | ||
|
|
||
| @Test | ||
| public void testOptimizedFold() { | ||
| // check that the new optimized fold alg will have the same result as using simple fold | ||
|
||
| for (int i = 0; i <= Unicode.MAX_RUNE; i++) { | ||
| int[] orbit = Unicode.optimizedFoldOrbit(i); | ||
| if (orbit != null) { | ||
| int r = Unicode.simpleFold(i); | ||
|
||
| int j = 0; | ||
| while (r != i) { | ||
| assertEquals(r, orbit[j]); | ||
| j++; | ||
| r = Unicode.simpleFold(r); | ||
| } | ||
| } else { | ||
| int r = Unicode.simpleFold(i); | ||
| assertEquals(r, Unicode.optimizedFoldNonOrbit(i)); // first fold map the same | ||
| assertEquals(i, Unicode.simpleFold(r)); // second fold always go back to first | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LINEAR?
Move this constant into the sole function that uses it.