From d1891ca1a3003dadb579e5d8b5ce8bb23bcb2e43 Mon Sep 17 00:00:00 2001 From: inter Date: Thu, 4 Sep 2025 14:08:50 +0800 Subject: [PATCH] Add File --- .../easyai/matrixTools/MatrixList.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/main/java/org/dromara/easyai/matrixTools/MatrixList.java diff --git a/src/main/java/org/dromara/easyai/matrixTools/MatrixList.java b/src/main/java/org/dromara/easyai/matrixTools/MatrixList.java new file mode 100644 index 0000000..52fd150 --- /dev/null +++ b/src/main/java/org/dromara/easyai/matrixTools/MatrixList.java @@ -0,0 +1,114 @@ +package org.dromara.easyai.matrixTools; + +/** + * @author lidapeng 矩阵预扩容容器 + * @time 2025/2/21 10:18 + */ +public class MatrixList { + private int pointerX; + private int pointerY; + private int capacity = 256; + private boolean addRow; + private Matrix matrix; + + public int getX() { + return pointerX; + } + + public int getY() { + return pointerY; + } + + private void copy(Matrix myMatrix) throws Exception { + for (int i = 0; i < this.pointerX; i++) { + for (int j = 0; j < this.pointerY; j++) { + myMatrix.setNub(i, j, matrix.getNumber(i, j)); + } + } + matrix = myMatrix; + } + + public Matrix getMatrix() throws Exception { + Matrix myMatrix = new Matrix(pointerX, pointerY); + for (int i = 0; i < this.pointerX; i++) { + for (int j = 0; j < this.pointerY; j++) { + myMatrix.setNub(i, j, matrix.getNumber(i, j)); + } + } + return myMatrix; + } + + private void insert(Matrix addMatrix) throws Exception { + int xSize, ySize, startX, startY; + if (addRow) { + xSize = pointerX + addMatrix.getX(); + ySize = pointerY; + startX = pointerX; + startY = 0; + } else { + xSize = pointerX; + ySize = pointerY + addMatrix.getY(); + startX = 0; + startY = pointerY; + } + for (int i = startX; i < xSize; i++) { + for (int j = startY; j < ySize; j++) { + matrix.setNub(i, j, addMatrix.getNumber(i - startX, j - startY)); + } + } + } + + public void add(Matrix addMatrix) throws Exception { + if (addMatrix != null) { + if (addRow) {//向行添加 + if (pointerY == addMatrix.getY()) { + int x = addMatrix.getX(); + if (pointerX + x > matrix.getX()) {//超出容量了 进行扩容后复制数值 + copy(new Matrix(pointerX + x + capacity, pointerY)); + } + insert(addMatrix); + pointerX = pointerX + x; + } else { + throw new Exception("向行添加,列数不一致"); + } + } else {//向列添加 + if (pointerX == addMatrix.getX()) { + int y = addMatrix.getY(); + if (pointerY + y > matrix.getY()) {//超出容量,进行扩容后复制数值 + copy(new Matrix(pointerX, pointerY + y + capacity)); + } + insert(addMatrix); + pointerY = pointerY + y; + } else { + throw new Exception("向列添加,行数不一致"); + } + } + + } + } + + public MatrixList(Matrix firstMatrix, boolean addRow, int capacity) throws Exception { + this.capacity = capacity; + init(firstMatrix, addRow); + } + + public MatrixList(Matrix firstMatrix, boolean addRow) throws Exception { + init(firstMatrix, addRow); + } + + private void init(Matrix firstMatrix, boolean addRow) throws Exception { + this.addRow = addRow; + pointerX = firstMatrix.getX(); + pointerY = firstMatrix.getY(); + if (addRow) { + matrix = new Matrix(pointerX + capacity, pointerY); + } else { + matrix = new Matrix(pointerX, pointerY + capacity); + } + for (int i = 0; i < pointerX; i++) { + for (int j = 0; j < pointerY; j++) { + matrix.setNub(i, j, firstMatrix.getNumber(i, j)); + } + } + } +}