MatrixUtils.java 6.07 KB
package com.huaheng.api.wcs.domain;

import com.huaheng.common.constant.GenConstants;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;

import javax.validation.constraints.NotNull;
import java.util.*;

/***
 * @author tongzonghao
 *
 */
public class MatrixUtils {

    /**
     * 添加矩阵数据
     *
     * @param matr
     * @param inventoryDetailList
     */
    public static void add(@NotNull int[][] matr, List<InventoryDetail> inventoryDetailList) {
        Map<Integer, InventoryDetail> map = new HashMap<>();
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            map.put(inventoryDetail.getContainerDetailNumber(), inventoryDetail);
        }

        int num = 0;
        for (int i = 0; i < matr.length; i++) {
            for (int j = 0; j < matr[0].length; j++) {
                num++;
                if (map.containsKey(num)) {
                    matr[i][j] = num;
                } else {
                    matr[i][j] = 0;
                }
            }
        }
    }

    public static void addPoint(@NotNull int[][] matrix,List<Integer> positions){
        addPoint(matrix,positions,false);
    }

    /**
     * 填充二维数组
     * @param matrix 空二维数组
     * @param positions 添加点位
     * @param mirrorFill 是否镜像填充 默认false true静态填充
     */
    public static void addPoint(@NotNull int[][] matrix,List<Integer> positions,boolean mirrorFill){
        Map<Integer, Integer> map = new HashMap<>();
        for (Integer integer : positions) {
            map.put(integer, integer);
        }
        int num = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                num++;
                boolean containsKey = map.containsKey(num);
                boolean isMirrorFill = mirrorFill ? !containsKey : containsKey;

                if (isMirrorFill) {
                    matrix[i][j] = num;
                } else {
                    matrix[i][j] = 0;
                }
            }
        }
    }

    /**
     * 填充二维数组
     * @param stackMatrix 矩阵对象
     * @param positions 添加点位
     * @param mirrorFill 是否镜像填充 默认false true静态填充
     */
    public static void addPoint(StackMatrix stackMatrix,List<Integer> positions,boolean mirrorFill){
        int[][] matrix = stackMatrix.getMatr();
        Map<Integer, Integer> map = new HashMap<>();
        for (Integer integer : positions) {
            map.put(integer, integer);
        }
        int num = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                num++;
                boolean containsKey = map.containsKey(num);
                boolean isMirrorFill = mirrorFill ? !containsKey : containsKey;

                if (isMirrorFill) {
                    matrix[i][j] = num;
                } else {
                    matrix[i][j] = 0;
                }
            }
        }
        stackMatrix.setMatr(matrix);
    }

    public static void addSeqNatureNumber(int[][] matrix){
        addPoint(matrix,Collections.EMPTY_LIST,true);
    }
    public static void addSeqNatureNumber(StackMatrix matrix){
        int[][] matr = matrix.getMatr();
        addPoint(matr,Collections.EMPTY_LIST,true);
        matrix.setMatr(matr);
    }

    /**
     * 根据矩阵获取位置
     * @param matrix
     * @param rowIdx
     * @param sequence
     * @return
     */
    public static int getPositionValue(StackMatrix matrix, int rowIdx,Integer sequence){
        //获取根据排序类型匹配位置
        int position = 0;
        int row = matrix.getRow();
        int[][] matr = matrix.getMatr();
        if (rowIdx < 0 || rowIdx > row) {
            return 0;
        }
        int[] currentRow = matr[rowIdx];
        int currentRowLength = currentRow.length;

        if(currentRowLength <= 0){
            return 0;
        }
        switch (sequence){
            case GenConstants.NATURE_SEQUENCE:
                for (int i : currentRow) {
                    if(currentRow[i] > 0){
                        position = currentRow[i];
                        break;
                    }
                }
                break;
            case GenConstants.MEDIAN_SEQUENCE:
                double two = 2.0;
                //取中位值
                double mediaValue = (currentRowLength-1) / two;
                int mediaUp = Double.valueOf(Math.ceil(mediaValue)).intValue();
                int mediaDown = Double.valueOf(Math.floor(mediaValue)).intValue();
                for (int i = mediaDown,j = mediaUp; i >= 0 || j <= currentRowLength; i--,j++) {
                    if(currentRow[i] > currentRow[j]){
                        position = currentRow[i];
                        break;
                    }
                    if(currentRow[j] > currentRow[i]){
                        position = currentRow[j];
                        break;
                    }
                    if(i == j){
                        position = currentRow[i];
                        break;
                    }
                }
                break;
            case GenConstants.SKEW_SEQUENCE:
                for (int x = 0,j=currentRow.length-1;x <= j; x++,j--) {
                    if(currentRow[x] > currentRow[j]){
                        position = currentRow[x];
                        break;
                    }
                    if(currentRow[j] > currentRow[x]){
                        position = currentRow[j];
                        break;
                    }
                    if(x == j){
                        position = currentRow[x];
                    }
                }
                break;
            default:
        }
        return position;
    }

    public int getPosition(int[] arys,boolean reverse){

        return 0;
    }

    public int putPositionValue(List<Integer> positionList,Integer sequence){
        boolean reverse = true;
        return 0;
    }


    public List<Integer> sortList(List<Integer> list, Integer sequence, boolean reverse){

        return list;
    }

}