我有以下代码,该代码应该从modelService返回orgId,但不能与Optional
一起使用#ifdef _OPENMP
#include <omp.h>
#else
#define omp_get_num_threads() 1
#define omp_get_thread_num() 0
#endif
__inline static
void calculateClusterCentroIDs(int numCoords, int numObjs, int numClusters, float * dataSetMatrix, int * clusterAssignmentCurrent, float *clustersCentroID) {
int * clusterMemberCount = (int *) calloc (numClusters,sizeof(float));
// sum all points
// for every point
#pragma omp parallel
{
int nbOfThreads = omp_get_num_threads();
int thisThread = omp_get_thread_num();
// Schedule for the first step : process only cluster with ID in the [from , to[ range
int clustFrom = (thisThread*numClusters)/nbOfThreads;
int clustTo = (thisThread+1 == nbOfThreads) ? numClusters : ((thisThread+1)*numClusters)/nbOfThreads;
// Each thread will loop through all values of numObjs but only process them depending on activeCluster
// The loop is skipped only if the thread was assigned no cluster
if (clustTo>clustFrom){
for (int i = 0; i < numObjs; ++i) {
// which cluster is it in?
int activeCluster = clusterAssignmentCurrent[i];
if (activeCluster>=clustFrom && activeCluster<clustTo){
// update count of members in that cluster
++clusterMemberCount[activeCluster];
// sum point coordinates for finding centroid
for (int j = 0; j < numCoords; ++j)
clustersCentroID[activeCluster*numCoords + j] += dataSetMatrix[i*numCoords + j];
}
}
}
#pragma omp barrier
// now divide each coordinate sum by number of members to find mean/centroid
// for each cluster
#pragma omp for // straightforward
for (int i = 0; i < numClusters; ++i) {
if (clusterMemberCount[i] != 0)
// for each coordinate
for (int j = 0; j < numCoords; ++j)
clustersCentroID[i*numCoords + j] /= clusterMemberCount[i]; /// XXXX will divide by zero here for any empty clusters!
}
}
free(clusterMemberCount);
}
此代码不起作用 public Long getOrgId(String someId) {
return this.getSpecialOrgId(someId).orElse(this.getById(someId).getOrgId());
}
private Optional<Long> getSpecialOrgId(String someId) {
return modelService.getModel(someId).map(Model::getOrgId).filter(this::isConditionTrue);
}
始终返回getSpecialOrgId
但是下面的代码很好用,不确定上面的代码有什么问题
empty
其中 private Long getOrgId(String someId) {
Optional<Long> orgIdFromModel = this.getSpecialOrgId(someId);
if (orgIdFromModel.isPresent()) {
return orgIdFromModel.get();
}
return this.getById(someId).getOrgId();
}
private Optional<Long> getSpecialOrgId(String someId) {
Optional<Model> modelOptional = modelService.getModel(someId);
if (modelOptional.isPresent()) {
Model model = modelOptional.get();
if (isConditionTrue(model.getOrgId())) {
return of(model.getOrgId());
}
}
return empty();
}
始终是isConditionTrue
,这是准确的代码,我仅更改了一些变量名
答案 0 :(得分:1)
您似乎应该将orElse
中的getOrgId
替换为orElseGet(() -> getById(someId).getOrgId())
。