实现了拯救内存的二分查找(待强力测试);

master
白封羽 2022-07-11 22:35:58 +08:00
parent 22d2763360
commit 72e23ad0f0
1 changed files with 50 additions and 10 deletions

View File

@ -186,16 +186,56 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
return getHolderLevel(staffId, staffGlobalLevel, taskId); return getHolderLevel(staffId, staffGlobalLevel, taskId);
} }
private int lowerBound(List<Task> data, long x) {
int l = 0, r = data.size() - 1;
while (l < r) {
int mid = (l + r) / 2;
if (data.get(mid).getTaskFatherId() >= x) r = mid;
else l = mid + 1;
}
return l;
}
private int upperBound(List<Task> data, long x) {
int l = 0, r = data.size() - 1;
while (l < r) {
int mid = (l + r) / 2;
if (data.get(mid).getTaskFatherId() <= x) l = mid + 1;
else r = mid;
}
return l - 1;
}
@Override @Override
public List<Task> listMyTasks(String token, Integer projectId) throws BadRequestException { public List<Task> listMyTasks(String token, Integer projectId) throws BadRequestException {
if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) {
throw new BadRequestException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
}
Integer staffId = TokenUtils.getStaffId(token); Integer staffId = TokenUtils.getStaffId(token);
return baseMapper.selectList(Wrappers.<Task>lambdaQuery() List<Task> resList = baseMapper.selectList(Wrappers.<Task>lambdaQuery().select(Task::getTaskId, Task::getTaskFatherId, Task::getTaskHolderId)
.eq(Task::getTaskHolderId, staffId) .eq(Task::getTaskProjectId, projectId).orderByAsc(Task::getTaskFatherId));
.eq(Task::getTaskProjectId, projectId) List<Long> results = new ArrayList<>();
); ArrayList<Long> vec = new ArrayList<>();
ArrayList<Boolean> isTarget = new ArrayList<>();
vec.add(0L);
isTarget.add(false);
int id = 0;
while (id != vec.size()) {
Long pos = vec.get(id);
Boolean flag = isTarget.get(id);
id++;
if (flag) {
results.add(pos);
continue;
}
int l = lowerBound(resList, pos);
int r = upperBound(resList, pos);
if (l > r)
continue;
for (int i = l; i <= r; i++) {
vec.add(resList.get(i).getTaskId());
isTarget.add(resList.get(i).getTaskHolderId().equals(staffId));
}
}
return baseMapper.selectList(Wrappers.<Task>lambdaQuery().in(Task::getTaskId, results));
} }
@Override @Override