From 431f858816e94b8584885a0259c3c2a2a3323c00 Mon Sep 17 00:00:00 2001 From: takaoka_k Date: Sat, 23 Mar 2024 18:27:57 +0900 Subject: [PATCH 1/2] Add build action --- .github/{workflow => workflows}/build.yml | 4 +- build.gradle.kts | 4 +- .../worksap/nlp/dartsclone/DoubleArray.java | 177 ++++++++++-------- .../nlp/dartsclone/details/BitVector.java | 24 +-- .../nlp/dartsclone/details/DAWGBuilder.java | 92 ++++++--- .../details/DoubleArrayBuilder.java | 37 ++-- .../nlp/dartsclone/details/KeySet.java | 16 +- .../com/worksap/nlp/dartsclone/DartsTest.java | 46 +++-- 8 files changed, 228 insertions(+), 172 deletions(-) rename .github/{workflow => workflows}/build.yml (90%) diff --git a/.github/workflow/build.yml b/.github/workflows/build.yml similarity index 90% rename from .github/workflow/build.yml rename to .github/workflows/build.yml index 23a14af..2e589db 100644 --- a/.github/workflow/build.yml +++ b/.github/workflows/build.yml @@ -42,11 +42,11 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} if: matrix.java-version >= 17 && env.SONAR_TOKEN != '' - run: ./gradlew --no-daemon --info --stacktrace sonarqube -Dsonar.verbose=true -Dsonar.login=$SONAR_TOKEN + run: ./gradlew --no-daemon --info --stacktrace sonar - uses: actions/upload-artifact@v3 if: failure() with: name: reports (${{ matrix.java-version }}) path: build/reports - name: Build javadoc - run: ./gradlew --no-daemon --info javadoc \ No newline at end of file + run: ./gradlew --no-daemon --info javadoc diff --git a/build.gradle.kts b/build.gradle.kts index 46848a4..feec15c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { id("io.github.gradle-nexus.publish-plugin") version "1.3.0" id("signing") id("jacoco") - id("org.sonarqube") version "3.4.0.2513" + id("org.sonarqube") version "4.4.1.3373" id("com.diffplug.spotless") version "6.25.0" } @@ -54,7 +54,7 @@ sonarqube { property("sonar.sourceEncoding", "UTF-8") property("sonar.host.url", "https://sonarcloud.io") property("sonar.organization", "worksapplications") - property("sonar.projectKey", "com.worksap.nlp.jdartsclone") + property("sonar.projectKey", "WorksApplications_jdartsclone") property("sonar.language", "java") property("sonar.links.homepage", "https://github.com/WorksApplications/jdartsclone") property("sonar.links.ci", "https://github.com/WorksApplications/jdartsclone/actions") diff --git a/src/main/java/com/worksap/nlp/dartsclone/DoubleArray.java b/src/main/java/com/worksap/nlp/dartsclone/DoubleArray.java index d605a72..502ca76 100644 --- a/src/main/java/com/worksap/nlp/dartsclone/DoubleArray.java +++ b/src/main/java/com/worksap/nlp/dartsclone/DoubleArray.java @@ -33,17 +33,16 @@ /** * A trie structure using Double-Array. * - * This class provides storage of key-value pairs. - * The key is an array of bytes, unique, and does not include {@code \0}. - * The value is a non-negative integer. + * This class provides storage of key-value pairs. The key is an array of bytes, + * unique, and does not include {@code \0}. The value is a non-negative integer. */ public class DoubleArray { /** * The result of a traverse operation. * - * This class contains the result of a traverse, the position of the key, - * and the index of the node after the traverse. + * This class contains the result of a traverse, the position of the key, and + * the index of the node after the traverse. */ public static class TraverseResult { /** The result of a traverse */ @@ -62,13 +61,15 @@ public static class TraverseResult { private IntBuffer array; private ByteBuffer buffer; - private int size; // number of elements + private int size; // number of elements /** * Set an integer array as structures of Darts-clone. * - * @param array the structures of Darts-clone - * @param size the number of elements + * @param array + * the structures of Darts-clone + * @param size + * the number of elements */ public void setArray(IntBuffer array, int size) { this.array = array; @@ -102,34 +103,39 @@ public void clear() { } /** - * Returns the number of the internal elements in the structures of - * Darts-clone. + * Returns the number of the internal elements in the structures of Darts-clone. * * It is not the number of keys in the trie. * * @return the number of elements */ - public int size() { return size; } + public int size() { + return size; + } /** * Returns the size of the storage used in the structures of Darts-clone. * * @return the number of elements */ - public int totalSize() { return 4 * size; } + public int totalSize() { + return 4 * size; + } /** * Builds the trie from the pairs of key and value. * - * When a pair of key and value is added to trie, {@code progressFunction} - * is called with the number of added pairs and the total of pairs. + * When a pair of key and value is added to trie, {@code progressFunction} is + * called with the number of added pairs and the total of pairs. * - * @param keys key with which the specified value is to be associated - * @param values value to be associated with the specified key - * @param progressFunction the call for showing the progress of building + * @param keys + * key with which the specified value is to be associated + * @param values + * value to be associated with the specified key + * @param progressFunction + * the call for showing the progress of building */ - public void build(byte[][] keys, int[] values, - BiConsumer progressFunction) { + public void build(byte[][] keys, int[] values, BiConsumer progressFunction) { KeySet keySet = new KeySet(keys, values); DoubleArrayBuilder builder = new DoubleArrayBuilder(progressFunction); builder.build(keySet); @@ -141,13 +147,16 @@ public void build(byte[][] keys, int[] values, /** * Reads the trie from the file. * - * @param inputFile the file to read - * @param position the offset to read - * @param totalSize the size to read - * @throws IOException if reading a file is failed + * @param inputFile + * the file to read + * @param position + * the offset to read + * @param totalSize + * the size to read + * @throws IOException + * if reading a file is failed */ - public void open(FileChannel inputFile, long position, long totalSize) - throws IOException { + public void open(FileChannel inputFile, long position, long totalSize) throws IOException { if (position < 0) { position = 0; @@ -155,8 +164,7 @@ public void open(FileChannel inputFile, long position, long totalSize) if (totalSize <= 0) { totalSize = inputFile.size(); } - buffer = inputFile.map(FileChannel.MapMode.READ_ONLY, - position, totalSize); + buffer = inputFile.map(FileChannel.MapMode.READ_ONLY, position, totalSize); buffer.order(ByteOrder.LITTLE_ENDIAN); array = buffer.asIntBuffer(); size = array.capacity(); @@ -165,20 +173,23 @@ public void open(FileChannel inputFile, long position, long totalSize) /** * Writes this trie to the file. * - * @param outputFile the file to write - * @throws IOException if writing a file is failed + * @param outputFile + * the file to write + * @throws IOException + * if writing a file is failed */ public void save(FileChannel outputFile) throws IOException { outputFile.write(buffer); } /** - * Returns the value to which the specified key is mapped, or - * a negative integer if this trie contains no mapping for the key. + * Returns the value to which the specified key is mapped, or a negative integer + * if this trie contains no mapping for the key. * - * @param key the key whose associated value is to be returned - * @return the array of integer includes the value to which the specified - * key is mapped and the length of the key + * @param key + * the key whose associated value is to be returned + * @return the array of integer includes the value to which the specified key is + * mapped and the length of the key */ public int[] exactMatchSearch(byte[] key) { int[] result = new int[] { -1, 0 }; @@ -204,18 +215,19 @@ public int[] exactMatchSearch(byte[] key) { /** * Returns the values to which the prefixes of the specified key is mapped. * - * If {@code offset} is not 0, the key is evaluated as the sub array - * removed the first {@code offset} bytes. + * If {@code offset} is not 0, the key is evaluated as the sub array removed the + * first {@code offset} bytes. * - * @param key the key whose associated value is to be returned - * @param offset the offset of the key - * @param maxNumResult the maximum size of the list - * @return the list of the array of integer includes the value to which - * the prefix of the specified key is mapped and the length - * of the prefix + * @param key + * the key whose associated value is to be returned + * @param offset + * the offset of the key + * @param maxNumResult + * the maximum size of the list + * @return the list of the array of integer includes the value to which the + * prefix of the specified key is mapped and the length of the prefix */ - public List commonPrefixSearch(byte[] key, int offset, - int maxNumResult) { + public List commonPrefixSearch(byte[] key, int offset, int maxNumResult) { List result = new ArrayList<>(); int nodePos = 0; @@ -228,7 +240,7 @@ public List commonPrefixSearch(byte[] key, int offset, if (label(unit) != Byte.toUnsignedInt(k)) { return result; } - + nodePos ^= offset(unit); if (hasLeaf(unit) && result.size() < maxNumResult) { int[] r = new int[] { value(array.get(nodePos)), i + 1 }; @@ -243,40 +255,44 @@ public List commonPrefixSearch(byte[] key, int offset, * * This method is equivalent to {@code commonPrefixSearch().iterator()}. * - * If {@code offset} is not 0, the key is evaluated as the sub array - * removed the first {@code offset} bytes. + * If {@code offset} is not 0, the key is evaluated as the sub array removed the + * first {@code offset} bytes. * - * @param key the key whose associated value is to be returned - * @param offset the offset of the key - * @return the list of the array of integer includes the value to which - * the prefix of the specified key is mapped and the length - * of the prefix + * @param key + * the key whose associated value is to be returned + * @param offset + * the offset of the key + * @return the list of the array of integer includes the value to which the + * prefix of the specified key is mapped and the length of the prefix */ public Iterator commonPrefixSearch(byte[] key, int offset) { return new Itr(key, offset); } /** - * Returns the value to which the specified key is mapped by - * traversing the trie from the specified node. + * Returns the value to which the specified key is mapped by traversing the trie + * from the specified node. * * If {@code node} is 0, starts traversing from the root node. * - * If {@code offset} is not 0, the key is evaluated as the sub array - * removed the first {@code offset} bytes. + * If {@code offset} is not 0, the key is evaluated as the sub array removed the + * first {@code offset} bytes. * - * If {@code end} is smaller than {@code key.length}, the remains are - * ignored. + * If {@code end} is smaller than {@code key.length}, the remains are ignored. * - * Returns -1 as the value if a traverse is failed at the end of the key, - * or -2 at a middle of the key. + * Returns -1 as the value if a traverse is failed at the end of the key, or -2 + * at a middle of the key. * - * @param key the key whose associated value is to be returned - * @param offset the offset of the key - * @param length the end of the key - * @param nodePosition the node to start a traverse - * @return the value to which the specified key is mapped, - * the offset of the key, and the node after the traverse + * @param key + * the key whose associated value is to be returned + * @param offset + * the offset of the key + * @param length + * the end of the key + * @param nodePosition + * the node to start a traverse + * @return the value to which the specified key is mapped, the offset of the + * key, and the node after the traverse */ public TraverseResult traverse(byte[] key, int offset, int length, int nodePosition) { int nodePos = nodePosition; @@ -300,22 +316,25 @@ public TraverseResult traverse(byte[] key, int offset, int length, int nodePosit } /** - * Returns the value to which the specified key is mapped by - * traversing the trie from the specified node. + * Returns the value to which the specified key is mapped by traversing the trie + * from the specified node. * * If {@code node} is 0, starts traversing from the root node. * - * If {@code offset} is not 0, the key is evaluated as the sub array - * removed the first {@code offset} bytes. + * If {@code offset} is not 0, the key is evaluated as the sub array removed the + * first {@code offset} bytes. * - * Returns -1 as the value if a traverse is failed at the end of the key, - * or -2 at a middle of the key. + * Returns -1 as the value if a traverse is failed at the end of the key, or -2 + * at a middle of the key. * - * @param key the key whose associated value is to be returned - * @param offset the offset of the key - * @param nodePosition the node to start a traverse - * @return the value to which the specified key is mapped, - * the offset of the key, and the node after the traverse + * @param key + * the key whose associated value is to be returned + * @param offset + * the offset of the key + * @param nodePosition + * the node to start a traverse + * @return the value to which the specified key is mapped, the offset of the + * key, and the node after the traverse */ public TraverseResult traverse(byte[] key, int offset, int nodePosition) { return traverse(key, offset, key.length, nodePosition); @@ -355,7 +374,7 @@ public int[] next() { } int[] getNext() { - for ( ; offset < key.length; offset++) { + for (; offset < key.length; offset++) { byte k = key[offset]; nodePos ^= Byte.toUnsignedInt(k); int unit = array.get(nodePos); @@ -377,7 +396,7 @@ int[] getNext() { private boolean hasLeaf(int unit) { return ((unit >>> 8) & 1) == 1; } - + private int value(int unit) { return unit & ((1 << 31) - 1); } diff --git a/src/main/java/com/worksap/nlp/dartsclone/details/BitVector.java b/src/main/java/com/worksap/nlp/dartsclone/details/BitVector.java index 3224607..712c1a1 100644 --- a/src/main/java/com/worksap/nlp/dartsclone/details/BitVector.java +++ b/src/main/java/com/worksap/nlp/dartsclone/details/BitVector.java @@ -21,7 +21,7 @@ class BitVector { private static final int UNIT_SIZE = 32; - + private ArrayList units = new ArrayList<>(); private int[] ranks; private int numOnes; @@ -33,26 +33,28 @@ boolean get(int id) { int rank(int id) { int unitId = id / UNIT_SIZE; - return ranks[unitId] - + popCount(units.get(unitId) & - (~0 >>> (UNIT_SIZE - (id % UNIT_SIZE) - 1))); + return ranks[unitId] + popCount(units.get(unitId) & (~0 >>> (UNIT_SIZE - (id % UNIT_SIZE) - 1))); } void set(int id, boolean bit) { if (bit) { - units.set(id / UNIT_SIZE, - units.get(id / UNIT_SIZE) | 1 << (id % UNIT_SIZE)); + units.set(id / UNIT_SIZE, units.get(id / UNIT_SIZE) | 1 << (id % UNIT_SIZE)); } else { - units.set(id / UNIT_SIZE, - units.get(id / UNIT_SIZE) & ~(1 << (id % UNIT_SIZE))); + units.set(id / UNIT_SIZE, units.get(id / UNIT_SIZE) & ~(1 << (id % UNIT_SIZE))); } } - boolean isEmpty() { return units.isEmpty(); } + boolean isEmpty() { + return units.isEmpty(); + } - int numOnes() { return numOnes; } + int numOnes() { + return numOnes; + } - int size() { return size; } + int size() { + return size; + } void append() { if ((size % UNIT_SIZE) == 0) { diff --git a/src/main/java/com/worksap/nlp/dartsclone/details/DAWGBuilder.java b/src/main/java/com/worksap/nlp/dartsclone/details/DAWGBuilder.java index a8b2032..b39b7e3 100644 --- a/src/main/java/com/worksap/nlp/dartsclone/details/DAWGBuilder.java +++ b/src/main/java/com/worksap/nlp/dartsclone/details/DAWGBuilder.java @@ -31,13 +31,19 @@ static class Node { void reset() { child = 0; sibling = 0; - label = (byte)0; + label = (byte) 0; isState = false; hasSibling = false; } - int getValue() { return child; } - void setValue(int value) { child = value; } + int getValue() { + return child; + } + + void setValue(int value) { + child = value; + } + int unit() { if (label == 0) { return (child << 1) | (hasSibling ? 1 : 0); @@ -48,11 +54,22 @@ int unit() { static class Unit { int unit; - - int child() { return unit >>> 2; } - boolean hasSibling() { return (unit & 1) == 1; } - int value() { return unit >>> 1; } - boolean isState() { return (unit & 2) == 2; } + + int child() { + return unit >>> 2; + } + + boolean hasSibling() { + return (unit & 1) == 1; + } + + int value() { + return unit >>> 1; + } + + boolean isState() { + return (unit & 2) == 2; + } } private static final int INITIAL_TABLE_SIZE = 1 << 10; @@ -66,27 +83,45 @@ static class Unit { private ArrayList recycleBin = new ArrayList<>(); private int numStates; - int root() { return 0; } + int root() { + return 0; + } - int child(int id) { return units.get(id).child(); } + int child(int id) { + return units.get(id).child(); + } int sibling(int id) { return (units.get(id).hasSibling()) ? (id + 1) : 0; } - int value(int id) { return units.get(id).value(); } + int value(int id) { + return units.get(id).value(); + } - boolean isLeaf(int id) { return label(id) == 0; } + boolean isLeaf(int id) { + return label(id) == 0; + } - byte label(int id) { return labels.get(id); } + byte label(int id) { + return labels.get(id); + } - boolean isIntersection(int id) { return isIntersections.get(id); } + boolean isIntersection(int id) { + return isIntersections.get(id); + } - int intersectionId(int id) { return isIntersections.rank(id) - 1; } + int intersectionId(int id) { + return isIntersections.rank(id) - 1; + } - int numIntersections() { return isIntersections.numOnes();} + int numIntersections() { + return isIntersections.numOnes(); + } - int size() { return units.size(); } + int size() { + return units.size(); + } void init() { table.ensureCapacity(INITIAL_TABLE_SIZE); @@ -99,7 +134,7 @@ void init() { numStates = 1; - nodes.get(0).label = (byte)0xFF; + nodes.get(0).label = (byte) 0xFF; nodeStack.add(0); } @@ -128,14 +163,14 @@ void insert(byte[] key, int value) { int id = 0; int keyPos = 0; - for ( ; keyPos <= key.length; keyPos++) { + for (; keyPos <= key.length; keyPos++) { int childId = nodes.get(id).child; if (childId == 0) { break; } byte keyLabel = (keyPos < key.length) ? key[keyPos] : 0; - if (keyPos < key.length && keyLabel == 0) { + if (keyPos < key.length && keyLabel == 0) { throw new IllegalArgumentException("invalid null character"); } @@ -154,7 +189,7 @@ void insert(byte[] key, int value) { return; } - for ( ; keyPos <= key.length; keyPos++) { + for (; keyPos <= key.length; keyPos++) { byte keyLabel = (keyPos < key.length) ? key[keyPos] : 0; int childId = appendNode(); @@ -246,20 +281,20 @@ void expandTable() { private int[] findUnit(int id) { int[] result = new int[2]; int hashId = hashUnit(id) % table.size(); - for ( ; ; hashId = (hashId + 1) % table.size()) { + for (;; hashId = (hashId + 1) % table.size()) { int unitId = table.get(hashId); if (unitId == 0) { break; } } result[1] = hashId; - return result; + return result; } private int[] findNode(int nodeId) { int[] result = new int[2]; int hashId = hashNode(nodeId) % table.size(); - for ( ; ; hashId = (hashId + 1) % table.size()) { + for (;; hashId = (hashId + 1) % table.size()) { int unitId = table.get(hashId); if (unitId == 0) { break; @@ -287,8 +322,7 @@ private boolean areEqual(int nodeId, int unitId) { } for (int i = nodeId; i != 0; i = nodes.get(i).sibling, unitId--) { - if (nodes.get(i).unit() != units.get(unitId).unit || - nodes.get(i).label != labels.get(unitId)) { + if (nodes.get(i).unit() != units.get(unitId).unit || nodes.get(i).label != labels.get(unitId)) { return false; } } @@ -322,7 +356,7 @@ private int hashNode(int id) { private int appendUnit() { isIntersections.append(); units.add(new Unit()); - labels.add((byte)0); + labels.add((byte) 0); return isIntersections.size() - 1; } @@ -345,11 +379,11 @@ private void freeNode(int id) { } private static int hash(int key) { - key = ~key + (key << 15); // key = (key << 15) - key - 1; + key = ~key + (key << 15); // key = (key << 15) - key - 1; key = key ^ (key >> 12); key = key + (key << 2); key = key ^ (key >> 4); - key = key * 2057; // key = (key + (key << 3)) + (key << 11); + key = key * 2057; // key = (key + (key << 3)) + (key << 11); key = key ^ (key >> 16); return key; } diff --git a/src/main/java/com/worksap/nlp/dartsclone/details/DoubleArrayBuilder.java b/src/main/java/com/worksap/nlp/dartsclone/details/DoubleArrayBuilder.java index 37e033c..0821c78 100644 --- a/src/main/java/com/worksap/nlp/dartsclone/details/DoubleArrayBuilder.java +++ b/src/main/java/com/worksap/nlp/dartsclone/details/DoubleArrayBuilder.java @@ -37,7 +37,7 @@ static class DoubleArrayBuilderExtraUnit { boolean isFixed; boolean isUsed; } - + private BiConsumer progressFunction; private ArrayList units = new ArrayList<>(); private DoubleArrayBuilderExtraUnit[] extras; @@ -66,7 +66,7 @@ public ByteBuffer copy() { for (DoubleArrayBuilderUnit u : units) { buffer.putInt(u.unit); } - ((Buffer)buffer).rewind(); + ((Buffer) buffer).rewind(); return buffer; } @@ -77,8 +77,10 @@ public void clear() { table = null; } - int numBlocks() { return units.size() / BLOCK_SIZE; } - + int numBlocks() { + return units.size() / BLOCK_SIZE; + } + DoubleArrayBuilderExtraUnit extras(int id) { return extras[id % NUM_EXTRAS]; } @@ -111,7 +113,7 @@ void buildFromDAWG(DAWGBuilder dawg) { reserveId(0); extras(0).isUsed = true; units.get(0).setOffset(1); - units.get(0).setLabel((byte)0); + units.get(0).setLabel((byte) 0); if (dawg.child(dawg.root()) != 0) { buildFromDAWG(dawg, dawg.root(), 0); @@ -158,7 +160,7 @@ void buildFromDAWG(DAWGBuilder dawg, int dawgId, int dicId) { int arrangeFromDAWG(DAWGBuilder dawg, int dawgId, int dicId) { labels.clear(); - + int dawgChildId = dawg.child(dawgId); while (dawgChildId != 0) { labels.add(dawg.label(dawgChildId)); @@ -202,7 +204,7 @@ void buildFromKeySet(KeySet keySet) { reserveId(0); extras(0).isUsed = true; units.get(0).setOffset(1); - units.get(0).setLabel((byte)0); + units.get(0).setLabel((byte) 0); if (keySet.size() > 0) { buildFromKeySet(keySet, 0, keySet.size(), 0, 0); @@ -214,8 +216,7 @@ void buildFromKeySet(KeySet keySet) { labels = null; } - void buildFromKeySet(KeySet keySet, int begin, int end, int depth, - int dicId) { + void buildFromKeySet(KeySet keySet, int begin, int end, int depth, int dicId) { int offset = arrangeFromKeySet(keySet, begin, end, depth, dicId); while (begin < end) { @@ -233,18 +234,15 @@ void buildFromKeySet(KeySet keySet, int begin, int end, int depth, while (++begin < end) { byte label = keySet.getKeyByte(begin, depth); if (label != lastLabel) { - buildFromKeySet(keySet, lastBegin, begin, depth + 1, - offset ^ Byte.toUnsignedInt(lastLabel)); + buildFromKeySet(keySet, lastBegin, begin, depth + 1, offset ^ Byte.toUnsignedInt(lastLabel)); lastBegin = begin; lastLabel = keySet.getKeyByte(begin, depth); } } - buildFromKeySet(keySet, lastBegin, end, depth + 1, - offset ^ Byte.toUnsignedInt(lastLabel)); + buildFromKeySet(keySet, lastBegin, end, depth + 1, offset ^ Byte.toUnsignedInt(lastLabel)); } - int arrangeFromKeySet(KeySet keySet, int begin, int end, int depth, - int dicId) { + int arrangeFromKeySet(KeySet keySet, int begin, int end, int depth, int dicId) { labels.clear(); int value = -1; @@ -268,8 +266,7 @@ int arrangeFromKeySet(KeySet keySet, int begin, int end, int depth, if (labels.isEmpty()) { labels.add(label); } else if (label != labels.get(labels.size() - 1)) { - if (Byte.toUnsignedInt(label) - < Byte.toUnsignedInt(labels.get(labels.size() - 1))) { + if (Byte.toUnsignedInt(label) < Byte.toUnsignedInt(labels.get(labels.size() - 1))) { throw new IllegalArgumentException("wrong key order"); } labels.add(label); @@ -307,7 +304,7 @@ int findValidOffset(int id) { } unfixedId = extras(unfixedId).next; } while (unfixedId != extrasHead); - + return units.size() | (id & LOWER_MASK); } @@ -333,7 +330,7 @@ boolean isValidOffset(int id, int offset) { void reserveId(int id) { if (id >= units.size()) { expandUnits(); - } + } if (id == extrasHead) { extrasHead = extras(id).next; @@ -410,7 +407,7 @@ void fixBlock(int blockId) { for (int id = begin; id != end; id++) { if (!extras(id).isFixed) { reserveId(id); - units.get(id).setLabel((byte)(id ^ unusedOffset)); + units.get(id).setLabel((byte) (id ^ unusedOffset)); } } } diff --git a/src/main/java/com/worksap/nlp/dartsclone/details/KeySet.java b/src/main/java/com/worksap/nlp/dartsclone/details/KeySet.java index 063dd55..331a2ca 100644 --- a/src/main/java/com/worksap/nlp/dartsclone/details/KeySet.java +++ b/src/main/java/com/worksap/nlp/dartsclone/details/KeySet.java @@ -26,10 +26,14 @@ public KeySet(byte[][] keys, int[] values) { this.values = values; } - int size() { return keys.length; } - - byte[] getKey(int id) { return keys[id]; } - + int size() { + return keys.length; + } + + byte[] getKey(int id) { + return keys[id]; + } + byte getKeyByte(int keyId, int byteId) { if (byteId >= keys[keyId].length) { return 0; @@ -37,7 +41,9 @@ byte getKeyByte(int keyId, int byteId) { return keys[keyId][byteId]; } - boolean hasValues() { return values != null; } + boolean hasValues() { + return values != null; + } int getValue(int id) { return (hasValues()) ? values[id] : id; diff --git a/src/test/java/com/worksap/nlp/dartsclone/DartsTest.java b/src/test/java/com/worksap/nlp/dartsclone/DartsTest.java index 85b9f68..54fafe9 100644 --- a/src/test/java/com/worksap/nlp/dartsclone/DartsTest.java +++ b/src/test/java/com/worksap/nlp/dartsclone/DartsTest.java @@ -59,8 +59,7 @@ public class DartsTest { @Before public void setUp() { SortedSet validKeys = generateValidKeys(NUM_VALID_KEYS); - Set invalidKeys = generateInvalidKeys(NUM_INVALID_KEYS, - validKeys); + Set invalidKeys = generateInvalidKeys(NUM_INVALID_KEYS, validKeys); this.keys = validKeys.toArray(new byte[0][0]); this.invalidKeys = invalidKeys.toArray(new byte[0][0]); @@ -106,7 +105,7 @@ public void buildWithRandomValue() { @Test public void saveAndOpen() throws IOException { DoubleArray dic = new DoubleArray(); - dic.build(keys, values, null); + dic.build(keys, values, null); File dicFile = temporaryFolder.newFile(); FileOutputStream ostream = new FileOutputStream(dicFile); @@ -123,12 +122,12 @@ public void saveAndOpen() throws IOException { } @Test - public void array() { + public void array() { DoubleArray dic = new DoubleArray(); - dic.build(keys, values, null); + dic.build(keys, values, null); IntBuffer array = dic.array(); int size = dic.size(); - + DoubleArray dicCopy = new DoubleArray(); dicCopy.setArray(array, size); testDic(dicCopy); @@ -140,8 +139,7 @@ public void commonPrefixSearch() { dic.build(keys, values, null); for (int i = 0; i < keys.length; i++) { - List results - = dic.commonPrefixSearch(keys[i], 0, MAX_NUM_RESULT); + List results = dic.commonPrefixSearch(keys[i], 0, MAX_NUM_RESULT); assertTrue(results.size() >= 1); assertTrue(results.size() < MAX_NUM_RESULT); @@ -200,7 +198,7 @@ void testDic(DoubleArray dic) { assertEquals(keys[i].length, result[1]); assertEquals(values[i], result[0]); } - + for (byte[] key : invalidKeys) { int[] result = dic.exactMatchSearch(key); assertEquals(-1, result[0]); @@ -209,28 +207,28 @@ void testDic(DoubleArray dic) { SortedSet generateValidKeys(int numKeys) { SortedSet keys = new TreeSet<>(new Comparator() { - @Override - public int compare(byte[] b1, byte[] b2) { - int n1 = b1.length; - int n2 = b2.length; - int min = Math.min(n1, n2); - for (int i = 0; i < min; i++) { - int c1 = b1[i] & 0xFF; - int c2 = b2[i] & 0xFF; - if (c1 != c2) { - return c1 - c2; - } + @Override + public int compare(byte[] b1, byte[] b2) { + int n1 = b1.length; + int n2 = b2.length; + int min = Math.min(n1, n2); + for (int i = 0; i < min; i++) { + int c1 = b1[i] & 0xFF; + int c2 = b2[i] & 0xFF; + if (c1 != c2) { + return c1 - c2; } - return n1 - n2; } - }); + return n1 - n2; + } + }); StringBuilder key = new StringBuilder(); while (keys.size() < numKeys) { key.setLength(0); int length = random.nextInt(8) + 1; for (int i = 0; i < length; i++) { - key.append((char)(1 + random.nextInt(65535))); + key.append((char) (1 + random.nextInt(65535))); } keys.add(key.toString().getBytes(StandardCharsets.UTF_8)); } @@ -244,7 +242,7 @@ Set generateInvalidKeys(int numKeys, Set validKeys) { key.setLength(0); int length = random.nextInt(8) + 1; for (int i = 0; i < length; i++) { - key.append((char)(1 + random.nextInt(65535))); + key.append((char) (1 + random.nextInt(65535))); } byte[] k = key.toString().getBytes(StandardCharsets.UTF_8); if (!validKeys.contains(k)) { From c35428a8461dbd554a3f14e2477e1121a816bc2b Mon Sep 17 00:00:00 2001 From: Kazuma TAKAOKA Date: Sun, 24 Mar 2024 09:52:24 +0900 Subject: [PATCH 2/2] Update build badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c037c1..df128fa 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # jdartsclone -[![Build Status](https://travis-ci.org/WorksApplications/jdartsclone.svg?branch=develop)](https://travis-ci.org/WorksApplications/jdartsclone) +[![Build](https://github.com/WorksApplications/jdartsclone/actions/workflows/build.yml/badge.svg)](https://github.com/WorksApplications/jdartsclone/actions/workflows/build.yml) jdartsclone is a port of the original Darts-clone to Java. Darts-clone is a library of TRIE structure using Double-Array.