MatrixUtils.java
6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.huaheng.api.wcs.domain;
import com.huaheng.common.constant.GenConstants;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.function.Function;
/***
* @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 matrix 二维数组
* @param target 目标值
* @return 返回 数组 [0]X坐标 [1]Y坐标
*/
public static int[] getPosition(int[][] matrix,int target){
Map<Integer, List<int[]>> valueToCoords = new HashMap<>();
// 遍历二维数组,记录每个值在数组中的位置
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
int value = matrix[i][j];
if (valueToCoords.containsKey(value)) {
valueToCoords.get(value).add(new int[]{i, j});
} else {
List<int[]> list = new ArrayList<>();
list.add(new int[]{i, j});
valueToCoords.put(value, list);
}
}
}
if (valueToCoords.containsKey(target)) {
List<int[]> coords = valueToCoords.get(target);
if(coords.size()>1){
throw new SecurityException("检索指定值【"+target+"】的位置数量大于1");
}
return coords.get(0);
}else{
throw new SecurityException("未检索指定值【"+target+"】的位置数量");
}
}
public static int getMaxValue(int[][] data){
int maxValue = data[0][0];
for (int j = 0; j < data.length; j++) {
for (int i = 0; i < data[j].length; i++) {
if (data[j][i] > maxValue) {
maxValue = data[j][i];
}
}
}
return maxValue;
}
public static int offsetPosition(int[][] data,int maxValue,int offset){
int[] position = MatrixUtils.getPosition(data, maxValue);
if(data.length * data[0].length <= offset){
throw new ServiceException("偏移量offset过多超出索引");
}
int maxRow = position[0];
int maxColumn = position[1];
maxRow += (maxColumn + offset) / data[0].length;
int newColumn = (maxColumn + offset) % data[0].length;
if((maxColumn + offset) > data[0].length - 1){
if(maxRow > data[0].length){
throw new ServiceException("偏移量过多超出索引");
}
if( maxRow > data.length -1){
throw new ServiceException("数据异常点位已满");
}
}
if((maxColumn + offset) < data[0].length - 1){
if(maxRow > data[0].length){
throw new ServiceException("偏移量过多超出索引");
}
if( maxRow > data.length -1){
throw new ServiceException("数据异常点位已满");
}
}
setForEach(data,x -> x);
return data[maxRow][newColumn];
}
public static void setForEach(int[][] data,Function<Integer, Integer> funtion) {
int dataLength = data[0].length;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = funtion.apply( (i * dataLength) + j + 1);
}
}
}
/**
* 垛型
* @param type 垛型:由箱子类型 和 箱子数量组合而成 boxType * 100 + totalQty
* @return
*/
public static int[][] getMatrix(int type){
int[][] matrix = null;
type = type / 100;
switch (type){
case 2:
case 3:
matrix = new int[3][3];
break;
case 1:
matrix = new int[3][4];
break;
default:
}
return matrix;
}
public static int rowValueCount(int[][] data,int row){
int maxValeXCount = 0;
for (int i = 0; i < data[0].length; i++) {
maxValeXCount += data[row][i];
}
return maxValeXCount;
}
public static int columnValueCount(int[][] data,int column){
return 0;
}
}