SimilaritySearch.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "SimilaritySearch.hpp"
  2. #include "ImageHasher.hpp"
  3. #include <QThread>
  4. #include <QtConcurrent>
  5. #include <algorithm>
  6. #include <map>
  7. #include <mutex>
  8. #include <numeric>
  9. #include <set>
  10. #include <utility>
  11. // 高速なマージのための Union-Find (Disjoint Set Union)
  12. struct DSU {
  13. std::vector<int> parent;
  14. DSU(int n) {
  15. parent.resize(n);
  16. std::iota(parent.begin(), parent.end(), 0);
  17. }
  18. int find(int i) {
  19. if (parent[i] == i)
  20. return i;
  21. return parent[i] = find(parent[i]);
  22. }
  23. void unite(int i, int j) {
  24. int root_i = find(i);
  25. int root_j = find(j);
  26. if (root_i != root_j)
  27. parent[root_i] = root_j;
  28. }
  29. };
  30. std::vector<DuplicateGroup>
  31. SimilaritySearch::findDuplicates(const std::vector<ImageData> &images,
  32. int threshold, bool strict) {
  33. if (images.empty())
  34. return {};
  35. int n = static_cast<int>(images.size());
  36. DSU dsu(n);
  37. std::mutex dsuMutex;
  38. // スレッドプール数に合わせて処理をチャンク分割して実行
  39. int numThreads = QThread::idealThreadCount();
  40. if (numThreads <= 0)
  41. numThreads = 8;
  42. // タスク数(チャンク数)をスレッド数より多く設定し、スレッドプールによる動的負荷分散を効かせる
  43. int numChunks = numThreads * 16;
  44. struct Chunk {
  45. int start_i;
  46. int end_i;
  47. };
  48. std::vector<Chunk> chunks;
  49. // 全体の比較回数を計算し、各チャンクがおおむね均等な比較回数(処理量)になるように分割
  50. long long totalPairs = static_cast<long long>(n) * (n - 1) / 2;
  51. long long pairsPerChunk = std::max(1LL, totalPairs / numChunks);
  52. int current_start = 0;
  53. long long current_pairs = 0;
  54. for (int i = 0; i < n; ++i) {
  55. current_pairs += (n - 1 - i);
  56. // 各チャンクの処理量が目標値に達するか、最後の行に到達したらチャンクとして登録
  57. if (current_pairs >= pairsPerChunk || i == n - 1) {
  58. chunks.push_back({current_start, i + 1});
  59. current_start = i + 1;
  60. current_pairs = 0;
  61. }
  62. }
  63. auto mapper = [&](Chunk &chunk) {
  64. std::vector<std::pair<int, int>> local_edges;
  65. local_edges.reserve(1024); // 一時バッファ
  66. for (int i = chunk.start_i; i < chunk.end_i; ++i) {
  67. for (int j = i + 1; j < n; ++j) {
  68. int distD =
  69. ImageHasher::hammingDistance(images[i].dhash, images[j].dhash);
  70. int distP =
  71. ImageHasher::hammingDistance(images[i].phash, images[j].phash);
  72. bool similar = strict ? (distD <= threshold && distP <= threshold)
  73. : (distD <= threshold || distP <= threshold);
  74. if (similar) {
  75. local_edges.emplace_back(i, j);
  76. // バッファが一定量溜まったら、排他制御でDSUにマージして解放
  77. // 全結果を最後に結合するのを避け、メモリ使用量とヒープの競合を極小化
  78. if (local_edges.size() >= 1024) {
  79. std::lock_guard<std::mutex> lock(dsuMutex);
  80. for (const auto &edge : local_edges) {
  81. dsu.unite(edge.first, edge.second);
  82. }
  83. local_edges.clear();
  84. }
  85. }
  86. }
  87. }
  88. // 残りのエッジをマージ
  89. if (!local_edges.empty()) {
  90. std::lock_guard<std::mutex> lock(dsuMutex);
  91. for (const auto &edge : local_edges) {
  92. dsu.unite(edge.first, edge.second);
  93. }
  94. }
  95. };
  96. // 結果のリストを構築せず、チャンクごとに並列処理しながらインプレースでマージを実行
  97. QtConcurrent::blockingMap(chunks, mapper);
  98. // 結果の集計 (ルートごとに画像をまとめる)
  99. std::map<int, DuplicateGroup> groupsMap;
  100. for (int i = 0; i < n; ++i) {
  101. int root = dsu.find(i);
  102. groupsMap[root].images.push_back(images[i]);
  103. }
  104. std::vector<DuplicateGroup> results;
  105. for (auto &pair : groupsMap) {
  106. if (pair.second.images.size() > 1) {
  107. results.push_back(pair.second);
  108. }
  109. }
  110. return results;
  111. }