В чем проблема: я разбираю файл, который может быть файлом строки 1k или файлом строки 1 миллион. Мои выходные данные для файла 1k соответствуют ожидаемому результату 1:1, но мой файл в 1 миллион строк дает мне неправильные результаты.
Что может быть неправильным с моим алгоритмом для создания таких странных результатов?
Объяснение алгоритма
То, что должно возвращаться, - это 10 лучших результатов в порядке возрастания от набора из 1 миллионного значения (значения, которые используются для некоторых вычислений для вычисления результата, который определяет оценку)
Вот мой алгоритм для выполнения расчетов.
std::vector<ResultType> circularSubvectorMatch(const unsigned int vector_size, std::vector<float>* searchVector, VectorsMap* circularVector, const unsigned int n, unsigned int begin_index, unsigned int end_index, unsigned int shm_start, unsigned int shm_end, float* shm, const bool is_test){
unsigned int i, j, row_index;
const unsigned int item_size = (*circularVector).size();
//vector for the returned top N results;
std::vector<ResultType> results;
results.reserve(n);
ResultType one;
std::vector<float> tmp;
float x, y, dist, dist_tmp;
int offset;
//iterate over the whole set of vectors parsed from the file.
for (row_index=begin_index; row_index < end_index; row_index++) {
//get a copy of the the vector at position row_index and remove it
tmp = (*circularVector).at(row_index);
//get the first and second key and erase them
x = tmp.at(0);
tmp.erase(tmp.begin());
y = tmp.at(0);
tmp.erase(tmp.begin());
//run through every vector point at steps of 5
for(i=0; i<VECTOR_COUNT; i+=5){
dist = 0;
offset = i;
dist_tmp = 0;
//loop through the vector size(9,11,17,29)
for(j=0; j<vector_size; j++){
dist_tmp = fabs((*searchVector).at(j) - tmp.at((j+i)%360));
dist += dist_tmp;
}
//put the result into the structure
one.x = x;
one.y = y;
one.offset = i;
one.dist = dist;
//Begins min heap process
if(results.size() < 10){
results.push_back(one);
}
// Compare it to the max element in the heap
else if (one < results.front()) {
// Add the new element to the vector
results.push_back(one);
// Move the existing minimum to the back and "re-heapify" the rest
std::pop_heap(results.begin(), results.end());
// Remove the last element from the vector
results.pop_back();
}
}
}
//sort to fix the min heap operations
std::sort(results.begin(), results.end());
results.resize(n);
int count = 0;
//if we are not running a test, save to memory
if(!is_test){
for(i=shm_start; i<shm_end; i++){
shm[i] = results.at(count).x;
shm[i+1] = results.at(count).y;
shm[i+2] = results.at(count).offset;
shm[i+3] = results.at(count).dist;
count++;
i+=3;
}
}
return results;
}
Возможно, вы можете использовать очередь приоритетов. После каждого вычисления для строки поместите значение в очередь. Если размер очереди равен 11, то удалите наименьшее значение из очереди. Когда все строки обрабатываются, просто удалите 10 значений в очереди.