Skip to content

Commit

Permalink
Merge pull request #11 from WorksApplications/feature-use-gradle
Browse files Browse the repository at this point in the history
Add build action
  • Loading branch information
kazuma-t authored Mar 24, 2024
2 parents 98b651e + c35428a commit 190e981
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 173 deletions.
4 changes: 2 additions & 2 deletions .github/workflow/build.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
run: ./gradlew --no-daemon --info javadoc
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down Expand Up @@ -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")
Expand Down
177 changes: 98 additions & 79 deletions src/main/java/com/worksap/nlp/dartsclone/DoubleArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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<Integer, Integer> progressFunction) {
public void build(byte[][] keys, int[] values, BiConsumer<Integer, Integer> progressFunction) {
KeySet keySet = new KeySet(keys, values);
DoubleArrayBuilder builder = new DoubleArrayBuilder(progressFunction);
builder.build(keySet);
Expand All @@ -141,22 +147,24 @@ 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;
}
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();
Expand All @@ -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 };
Expand All @@ -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<int[]> commonPrefixSearch(byte[] key, int offset,
int maxNumResult) {
public List<int[]> commonPrefixSearch(byte[] key, int offset, int maxNumResult) {
List<int[]> result = new ArrayList<>();

int nodePos = 0;
Expand All @@ -228,7 +240,7 @@ public List<int[]> 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 };
Expand All @@ -243,40 +255,44 @@ public List<int[]> 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<int[]> 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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit 190e981

Please sign in to comment.