// Path: src/main/java/com/worksap/nlp/sudachi/dictionary/Connection.java
// public final class Connection {
//     private final ShortBuffer matrix;
//     private final int leftSize;
//     private final int rightSize;
// 
//     public Connection(ShortBuffer matrix, int leftSize, int rightSize) {
//         this.matrix = matrix;
//         this.leftSize = leftSize;
//         this.rightSize = rightSize;
//     }
// 
//     private int ix(int left, int right) {
//         assert left < leftSize;
//         assert right < rightSize;
//         return right * leftSize + left;
//     }
// 
//     /**
//      *
//      * @param left
//      *            left connection index
//      * @param right
//      *            right connection index
//      * @return connection weight in the matrix
//      */
//     public short cost(int left, int right) {
//         return matrix.get(ix(left, right));
//     }
// 
//     public int getLeftSize() {
//         return leftSize;
//     }
// 
//     public int getRightSize() {
//         return rightSize;
//     }
// 
//     public void setCost(int left, int right, short cost) {
//         matrix.put(ix(left, right), cost);
//     }
// 
//     /**
//      * @return a copy of itself with the buffer owned, instead of slice
//      */
//     public Connection ownedCopy() {
//         ShortBuffer copy = ShortBuffer.allocate(matrix.limit());
//         copy.put(matrix);
// 
//         return new Connection(copy, leftSize, rightSize);
//     }
// 
//     public void validate(int leftId) {
//         if (matrix == null) {
//             // should never happen, but elides compiler checks
//             throw new NullPointerException("matrix");
//         }
// 
//         if (leftId >= leftSize) {
//             // should never happen, but adds a compiler precondition to the inlined method
//             throw new IllegalArgumentException(String.format("leftId < leftSize: (%d, %d)", leftId, leftSize));
//         }
//     }
// }


import com.worksap.nlp.sudachi.dictionary.Connection;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;
     *             when IO fails
     */
    public long readEntries(InputStream data) throws IOException {
        LineNumberReader reader = new LineNumberReader(new InputStreamReader(data, StandardCharsets.UTF_8));

        String header = reader.readLine();
        if (header == null) {
            throw new IllegalArgumentException("invalid format at line " + reader.getLineNumber());
        }

        String[] lr = WHITESPACE.split(header, 2);
        if (lr.length != 2) {
            throw new IllegalArgumentException("invalid header " + header + ", expected two 16-bit integers");
        }

        try {
            numLeft = Short.parseShort(lr[0]);
            numRight = Short.parseShort(lr[1]);
        } catch (NumberFormatException ignored) {
            throw new IllegalArgumentException("invalid header " + header + ", expected two 16-bit integers");
        }

        ByteBuffer buffer = ByteBuffer.allocate(2 * numLeft * numRight + 4);
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        ShortBuffer matrix = buffer.asShortBuffer();
        matrix.put(numLeft);
        matrix.put(numRight);

        matrix = matrix.slice();
// Path: src/main/java/com/worksap/nlp/sudachi/dictionary/Connection.java
// public final class Connection {
//     private final ShortBuffer matrix;
//     private final int leftSize;
//     private final int rightSize;
// 
//     public Connection(ShortBuffer matrix, int leftSize, int rightSize) {
//         this.matrix = matrix;
//         this.leftSize = leftSize;
//         this.rightSize = rightSize;
//     }
// 
//     private int ix(int left, int right) {
//         assert left < leftSize;
//         assert right < rightSize;
//         return right * leftSize + left;
//     }
// 
//     /**
//      *
//      * @param left
//      *            left connection index
//      * @param right
//      *            right connection index
//      * @return connection weight in the matrix
//      */
//     public short cost(int left, int right) {
//         return matrix.get(ix(left, right));
//     }
// 
//     public int getLeftSize() {
//         return leftSize;
//     }
// 
//     public int getRightSize() {
//         return rightSize;
//     }
// 
//     public void setCost(int left, int right, short cost) {
//         matrix.put(ix(left, right), cost);
//     }
// 
//     /**
//      * @return a copy of itself with the buffer owned, instead of slice
//      */
//     public Connection ownedCopy() {
//         ShortBuffer copy = ShortBuffer.allocate(matrix.limit());
//         copy.put(matrix);
// 
//         return new Connection(copy, leftSize, rightSize);
//     }
// 
//     public void validate(int leftId) {
//         if (matrix == null) {
//             // should never happen, but elides compiler checks
//             throw new NullPointerException("matrix");
//         }
// 
//         if (leftId >= leftSize) {
//             // should never happen, but adds a compiler precondition to the inlined method
//             throw new IllegalArgumentException(String.format("leftId < leftSize: (%d, %d)", leftId, leftSize));
//         }
//     }
// }


// Path: src/main/java/com/worksap/nlp/sudachi/dictionary/build/ConnectionMatrix.java
import com.worksap.nlp.sudachi.dictionary.Connection;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

     *             when IO fails
     */
    public long readEntries(InputStream data) throws IOException {
        LineNumberReader reader = new LineNumberReader(new InputStreamReader(data, StandardCharsets.UTF_8));

        String header = reader.readLine();
        if (header == null) {
            throw new IllegalArgumentException("invalid format at line " + reader.getLineNumber());
        }

        String[] lr = WHITESPACE.split(header, 2);
        if (lr.length != 2) {
            throw new IllegalArgumentException("invalid header " + header + ", expected two 16-bit integers");
        }

        try {
            numLeft = Short.parseShort(lr[0]);
            numRight = Short.parseShort(lr[1]);
        } catch (NumberFormatException ignored) {
            throw new IllegalArgumentException("invalid header " + header + ", expected two 16-bit integers");
        }

        ByteBuffer buffer = ByteBuffer.allocate(2 * numLeft * numRight + 4);
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        ShortBuffer matrix = buffer.asShortBuffer();
        matrix.put(numLeft);
        matrix.put(numRight);

        matrix = matrix.slice();
        Connection conn = new Connection(matrix, numLeft, numRight);