void TopK(std::vector<int> &input, int begin, int end, int k) { int pivot = Partition(input, begin, end); int len = pivot - begin + 1; if (len == k) return; else if (len > k) TopK(input, begin, pivot - 1, k); else TopK(input, pivot + 1, end, k - len); }
void Test() { srand(time(NULL)); int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::vector<int> input(a, a + 10); std::random_shuffle(input.begin(), input.end()); std::cout << "input:"; for (int i = 0; i < input.size(); i++) std::cout << input[i] << " "; std::cout << std::endl; std::vector<int> result = TopK(input, 5); std::cout << "Output:"; for (int i = 0; i < result.size(); i++) std::cout << result[i] << " "; std::cout << std::endl; std::random_shuffle(input.begin(), input.end()); std::cout << "input:"; for (int i = 0; i < input.size(); i++) std::cout << input[i] << " "; std::cout << std::endl; result = TopK2(input, 5); std::cout << "Output:"; for (int i = 0; i < result.size(); i++) std::cout << result[i] << " "; std::cout << std::endl; }
void FabMapCalculator::WriteLineOfResults_SparseTopK(unsigned int nImageID) { //The K largest entries of the current location_probability //get written out to file in sparse format m_results_file << "PDF due to image " << nImageID << " :" << endl; m_results_file << "Milliseconds to calculate: " << 1000.0*m_dfLastPDFCalculationTime << endl; const unsigned int K = 100; //Return the probabilities of the K most likely places //in the format PlaceID:prob //with PlaceID 0-based //If there are fewer than K places, return them all double dfReportingThreshold; if(location_probability.size() < K) { dfReportingThreshold = -1.0; //Less than K places, so we want to return the probability of all places. } else { //Find the probability of the K-th most likely place. vector<double> TopK(K); partial_sort_copy(location_probability.begin(),location_probability.end(),TopK.begin(),TopK.end(),greater<double>()); dfReportingThreshold = TopK[K-1]; } //Now, add to the output all places where prob is >= dfReportingThreshold, up to a maximum of K places. vector<double> NonZeroValues; unsigned int nReported = 0; unsigned int max = location_probability.size(); for(unsigned int i=0;i<max;i++) { if(location_probability[i]>dfReportingThreshold) { //Add it to the output m_results_file << i << " "; NonZeroValues.push_back(location_probability[i]); } if(nReported>K) break; } m_results_file << endl; //Then write out the corresponding values. max = NonZeroValues.size(); for(unsigned int i=0; i<max; ++i) { m_results_file << NonZeroValues[i] << " "; } m_results_file << endl; //If data association is turned on, also record imageToPlace #ifdef ALLOW_DATA_ASSOCIATION m_SceneToPlace_file << m_SceneToPlace.back() << " "; #endif }
void TestFruitCount() { vector<string> fruits; fruits.push_back("苹果"); fruits.push_back("梨"); fruits.push_back("苹果"); fruits.push_back("香蕉"); fruits.push_back("葡萄"); fruits.push_back("苹果"); fruits.push_back("苹果"); fruits.push_back("梨"); fruits.push_back("葡萄"); fruits.push_back("西瓜"); fruits.push_back("猕猴桃"); fruits.push_back("葡萄"); fruits.push_back("猕猴桃"); fruits.push_back("猕猴桃"); fruits.push_back("柠檬"); fruits.push_back("香蕉"); fruits.push_back("柚子"); fruits.push_back("香蕉"); TopK(fruits); }
std::vector<int> TopK2(std::vector<int> &input, int k) { TopK(input, 0, (int)input.size() - 1, k); return std::vector<int>(input.begin(), input.begin() + k); }