diff --git a/clone_format/format_tags.hpp b/clone_format/format_tags.hpp index 05624b1..bf37452 100644 --- a/clone_format/format_tags.hpp +++ b/clone_format/format_tags.hpp @@ -20,6 +20,7 @@ class format_tags static inline const QString FILE_TABLE="file_table"; static inline const QString RESULTS="results"; static inline const QString CLONE_PAIRS="clone_pairs"; + static inline const QString CLONE_PAIR_SIZE="clone_pair_size"; static inline const QString RESULT_ID="result_id"; static inline const QString FILE_ID="file_id"; static inline const QString PATH="path"; diff --git a/clone_format/jcln.cpp b/clone_format/jcln.cpp index 4e0115f..e83cf67 100644 --- a/clone_format/jcln.cpp +++ b/clone_format/jcln.cpp @@ -44,46 +44,45 @@ bool jcln::write(const detection_results &results, const QString &path) noexcept } -QJsonValue jcln::to_qjson(const file &file) noexcept +QJsonValue jcln::to_qjson(const std::shared_ptr &file_ptr, const QHash, int> file_index_map) noexcept { return QJsonObject { - {FILE_ID, int(file.id())}, - {PATH, file.canonical_file_path()} + {FILE_ID, file_index_map[file_ptr]}, + {PATH, file_ptr->canonical_file_path()} }; } -QJsonValue jcln::to_qjson(const fragment &fragment) noexcept +QJsonValue jcln::to_qjson(const fragment &fragment, const QHash, int> file_index_map) noexcept { return QJsonObject { - {FILE_ID, int(fragment.file_id())}, + {FILE_ID, int(file_index_map[fragment.file_ptr()])}, {BEGIN, int(fragment.begin())}, {END, int(fragment.end())} }; } -QJsonValue jcln::to_qjson(const clone_pair &clone_pair) noexcept +QJsonValue jcln::to_qjson(const std::shared_ptr &clone_pair, const QHash, int> file_index_map) noexcept { return QJsonObject { - {CLONE_PAIR_ID, int(clone_pair.id())}, - {SIMILARITY, int(clone_pair.similarity())}, - {FRAGMENT1, to_qjson(clone_pair.fragment1())}, - {FRAGMENT2, to_qjson(clone_pair.fragment2())} + {SIMILARITY, int(clone_pair->similarity())}, + {FRAGMENT1, to_qjson(clone_pair->fragment1(), file_index_map)}, + {FRAGMENT2, to_qjson(clone_pair->fragment2(), file_index_map)} }; } -QJsonValue jcln::to_qjson(const detection_result &detection_result) noexcept +QJsonValue jcln::to_qjson(const std::shared_ptr &detection_result, const QHash, int> file_index_map) noexcept { QJsonArray json_clone_pairs_array; - for(const auto &p:detection_result.clone_pair_table()) + for(const auto &p:detection_result->clone_pairs()) { - json_clone_pairs_array.append(to_qjson(p)); + json_clone_pairs_array.append(to_qjson(p, file_index_map)); } QJsonObject json_parameters; - const auto parameters=detection_result.context().parameters(); + const auto parameters=detection_result->environment().parameters(); for(const auto &k:parameters.keys()) { json_parameters[k]=parameters[k]; @@ -91,37 +90,32 @@ QJsonValue jcln::to_qjson(const detection_result &detection_result) noexcept return QJsonObject { - {CLONE_PAIRS, json_clone_pairs_array}, {ENVIRONMENT, QJsonObject{ - {SOURCE, detection_result.context().source()}, + {SOURCE, detection_result->environment().source()}, {PARAMETERS, json_parameters} }}, - {RESULT_ID, int(detection_result.id())} + {CLONE_PAIR_SIZE, detection_result->clone_pairs().size()}, + {CLONE_PAIRS, json_clone_pairs_array} }; } QJsonValue jcln::to_qjson(const detection_results &detection_results) noexcept { - QJsonArray files, results, clone_pairs; - for(const auto &f:detection_results.file_table()) + QJsonArray files, results; + auto file_index_map=detection_results.file_index_map(); + for(const auto &f:detection_results.files()) { - files.append(to_qjson(f)); + files.append(to_qjson(f, file_index_map)); } - for(const auto &r:detection_results.result_table()) + for(const auto &r:detection_results.results()) { - results.append(to_qjson(r)); - clone_pairs.append(QJsonObject{{RESULT_ID, int(r.id())}, {SIZE, r.clone_pairs().size()}}); + results.append(to_qjson(r, file_index_map)); } return QJsonObject { {GLOBAL, QJsonObject{ - {SIZE, QJsonObject{ - {RESULTS, detection_results.result_table().size()}, - {FILE_TABLE, detection_results.file_table().size()}, - {CLONE_PAIRS, clone_pairs} - }}, {TARGET, detection_results.target_path()}} }, {FILE_TABLE, files}, @@ -210,8 +204,6 @@ std::optional jcln::read_detection_result(const QJsonObject &j return std::nullopt; } - auto context=result_environment(clone_detector[NAME].toString(), environment[SOURCE].toString()); - shared_set clone_pairs; for(auto pj:json[CLONE_PAIRS].toArray()) { @@ -225,7 +217,7 @@ std::optional jcln::read_detection_result(const QJsonObject &j clone_pairs.insert(std::make_shared(std::move(p.value()))); } - return detection_result(std::move(context), std::move(clone_pairs)); + return detection_result(result_environment(clone_detector[NAME].toString(), environment[SOURCE].toString()), std::move(clone_pairs)); } std::optional jcln::read_detection_results(const QJsonObject &json) noexcept @@ -270,7 +262,7 @@ std::optional jcln::read_detection_results(const QJsonObject return std::nullopt; } - id_file_ptr_map[f[FILE_ID].toInt()]=rs.add(to_canonical_file_path(f[PATH].toString())); + id_file_ptr_map[f[FILE_ID].toInt()]=rs.emplace(to_canonical_file_path(f[PATH].toString())); } // results @@ -288,11 +280,8 @@ std::optional jcln::read_detection_results(const QJsonObject qCritical()<(std::move(r))); } - rs.emplace_detection_result(); - return std::make_optional(rs); } diff --git a/clone_format/jcln.hpp b/clone_format/jcln.hpp index 11924cf..f1c0089 100644 --- a/clone_format/jcln.hpp +++ b/clone_format/jcln.hpp @@ -20,10 +20,10 @@ class jcln final static bool write(const detection_results &results, const QString &path) noexcept; private: - static QJsonValue to_qjson(const file &file) noexcept; - static QJsonValue to_qjson(const fragment &fragment) noexcept; - static QJsonValue to_qjson(const clone_pair &clone_pair) noexcept; - static QJsonValue to_qjson(const detection_result &detection_result) noexcept; + static QJsonValue to_qjson(const std::shared_ptr &file_ptr, const QHash, int> file_index_map) noexcept; + static QJsonValue to_qjson(const fragment &fragment, const QHash, int> file_index_map) noexcept; + static QJsonValue to_qjson(const std::shared_ptr &clone_pair, const QHash, int> file_index_map) noexcept; + static QJsonValue to_qjson(const std::shared_ptr &detection_result, const QHash, int> file_index_map) noexcept; static QJsonValue to_qjson(const detection_results &detection_results) noexcept; static std::optional read_fragment(const QJsonObject &json, const shared_map &id_file_ptr_map) noexcept; diff --git a/core/detection_results.cpp b/core/detection_results.cpp index dce8d0a..c14c072 100644 --- a/core/detection_results.cpp +++ b/core/detection_results.cpp @@ -12,7 +12,7 @@ detection_results::detection_results(const QString &target_path) noexcept std::shared_ptr detection_results::emplace(QString &&canonical_file_path) noexcept { - auto itr=std::find_if(this->files_.begin(), this->files_.end(), [&](const auto &f){ return f==canonical_file_path; }); + auto itr=std::find_if(this->files_.begin(), this->files_.end(), [&](const auto &f){ return f->canonical_file_path()==canonical_file_path; }); if(itr==this->files_.end()) { itr=this->files_.insert(std::make_shared(std::move(canonical_file_path))); @@ -20,12 +20,22 @@ std::shared_ptr detection_results::emplace(QString &&canonical_file_path) return *itr; } -std::shared_ptr detection_results::empalce(result_environment &&context, shared_set &&clone_pairs) noexcept +std::shared_ptr detection_results::emplace(detection_result &&result) noexcept { - auto itr=std::find_if(this->results_.begin(), this->results_.end(), [&](const detection_result &r){ return r.context().source()==context.source(); }); + auto itr=std::find_if(this->results_.begin(), this->results_.end(), [&](const auto &r){ return r->environment()==result.environment(); }); if(itr==this->results_.end()) { - itr=this->results_.insert(std::make_shared(std::move(context), std::move(clone_pairs))); + itr=this->results_.insert(std::make_shared(std::move(result))); + } + return *itr; +} + +std::shared_ptr detection_results::empalce(result_environment &&environment, shared_set &&clone_pairs) noexcept +{ + auto itr=std::find_if(this->results_.begin(), this->results_.end(), [&](const auto &r){ return r->environment()==environment; }); + if(itr==this->results_.end()) + { + itr=this->results_.insert(std::make_shared(std::move(environment), std::move(clone_pairs))); } return *itr; } @@ -42,6 +52,31 @@ bool detection_results::remove(std::shared_ptr &&ptr) noexcept return false; } +const shared_set& detection_results::results() const noexcept +{ + return this->results_; +} + +const shared_set& detection_results::files() const noexcept +{ + return this->files_; +} + +QHash, int> detection_results::file_index_map() const noexcept +{ + auto list=this->files_.toList(); + std::sort(list.begin(), list.end(), [](const auto &f1, const auto &f2){ return *f1<*f2; }); + + QHash, int> r; + int index=0; + for(const auto &key:list) + { + r.insert(key, index); + ++index; + } + return r; +} + QString detection_results::target_path() const noexcept { return this->target_path_; diff --git a/core/detection_results.hpp b/core/detection_results.hpp index ca4a97f..3d7edb5 100644 --- a/core/detection_results.hpp +++ b/core/detection_results.hpp @@ -17,11 +17,14 @@ class detection_results final detection_results(const QString &target_path) noexcept; std::shared_ptr emplace(QString &&canonical_file_path) noexcept; - std::shared_ptr empalce(result_environment &&context, shared_set &&clone_pairs) noexcept; + std::shared_ptr emplace(detection_result &&detection_result) noexcept; + std::shared_ptr empalce(result_environment &&environment, shared_set &&clone_pairs) noexcept; bool remove(std::shared_ptr &&ptr) noexcept; - shared_list results() const noexcept; + const shared_set& results() const noexcept; + const shared_set& files() const noexcept; + QHash, int> file_index_map() const noexcept; void set_target_path(const QString &target_path) noexcept; QString target_path() const noexcept; diff --git a/core/matching_table.cpp b/core/matching_table.cpp index aac870b..5191f67 100644 --- a/core/matching_table.cpp +++ b/core/matching_table.cpp @@ -2,7 +2,7 @@ namespace asterism { - +/* matching_table_unit::matching_table_unit() noexcept {} @@ -184,5 +184,5 @@ uint qHash(const matching_table::key &key, uint seed) noexcept { return uint(key.key1_)^seed+uint(key.key2_); } - +*/ } diff --git a/core/matching_table.hpp b/core/matching_table.hpp index 787d34f..9b93f84 100644 --- a/core/matching_table.hpp +++ b/core/matching_table.hpp @@ -13,32 +13,28 @@ namespace asterism { - +/* class matching_table_unit final { public: matching_table_unit() noexcept; matching_table_unit(const detection_result &left, const detection_result &right, const float t) noexcept; - std::optional has_left_clone_pair_of(const clone_pair::id_t &right_clone_pair_id) const noexcept; - std::optional has_right_clone_pair_of(const clone_pair::id_t &left_clone_pair_id) const noexcept; - - int count_left_clone_pair_of(const clone_pair::id_t &right_clone_pair_id) const noexcept; - int count_right_clone_pair_of(const clone_pair::id_t &left_clone_pair_id) const noexcept; + std::optional> has_left_clone_pair_of(const std::shared_ptr &ptr) const noexcept; + std::optional> has_right_clone_pair_of(const std::shared_ptr &ptr) const noexcept; - detection_result::id_t left_result_id() const noexcept; - detection_result::id_t right_result_id() const noexcept; + int count_left_clone_pair_of(const std::shared_ptr &ptr) const noexcept; + int count_right_clone_pair_of(const std::shared_ptr &ptr) const noexcept; - QVector left_clone_pair_of(const clone_pair::id_t &right_clone_pair_id) const noexcept; - QVector right_clone_pair_of(const clone_pair::id_t &left_clone_pair_id) const noexcept; + shared_vector left_clone_pair_of(const std::shared_ptr &ptr) const noexcept; + shared_vector right_clone_pair_of(const std::shared_ptr &ptr) const noexcept; private: - detection_result::id_t left_result_id_, right_result_id_; - QVector> matching_list_; + QVector, std::shared_ptr>> matching_list_; bool better(const float ok_v, const float good_v, QPair &&ok_good_max, const float t) const noexcept; - QVector> unidirectional_matching(const detection_result &first, const detection_result &second, const float t) const noexcept; - QVector> bidirectional_matching(const detection_result &left, const detection_result &right, const float t) const noexcept; + QVector, std::shared_ptr>> unidirectional_matching(const detection_result &first, const detection_result &second, const float t) const noexcept; + QVector, std::shared_ptr>> bidirectional_matching(const detection_result &left, const detection_result &right, const float t) const noexcept; }; @@ -73,7 +69,7 @@ class matching_table final }; uint qHash(const matching_table::key &key, uint seed) noexcept; - +*/ } #endif // MATCHING_TABLE_HPP diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 8fbe358..9ad3fd9 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -39,6 +39,7 @@ MainWindow::~MainWindow() void MainWindow::open() { + /* auto results=clone_io::read(QFileDialog::getOpenFileName(this, tr("open file"), QDir::currentPath(), tr("Asterism Project (*.jcln)"))); if(results) { @@ -49,6 +50,7 @@ void MainWindow::open() this->scatter_plot_model_->set_heatmap_layer(std::move(h.value())); } } + */ } diff --git a/layer/clone_pair_grid_layer.cpp b/layer/clone_pair_grid_layer.cpp index dcb64ad..daef892 100644 --- a/layer/clone_pair_grid_layer.cpp +++ b/layer/clone_pair_grid_layer.cpp @@ -6,22 +6,26 @@ namespace asterism clone_pair_grid_layer::clone_pair_grid_layer() noexcept {} -clone_pair_grid_layer::clone_pair_grid_layer(const QList &clone_pairs, const uint32_t width) noexcept - : file_separated_grid_layer(width) +clone_pair_grid_layer::clone_pair_grid_layer(const shared_set &clone_pairs, const QMap, int> file_index_map) noexcept + : file_separated_grid_layer(file_index_map.size()) { for(const auto &p:clone_pairs) { - this->values_[grid_2d_coordinate(p.fragment1().file_id(), p.fragment2().file_id()).to_linear()].append(p); + auto x=file_index_map[p->fragment1().file_ptr()]; + auto y=file_index_map[p->fragment2().file_ptr()]; + this->values_[grid_2d_coordinate(x, y).to_linear()].append(p); } } -void clone_pair_grid_layer::make_layer(const QList &clone_pairs, const uint32_t width) noexcept +void clone_pair_grid_layer::make_layer(const shared_set &clone_pairs, const QMap, int> file_index_map) noexcept { - this->width_=width; + this->width_=file_index_map.size(); for(const auto &p:clone_pairs) { - this->values_[grid_2d_coordinate(p.fragment1().file_id(), p.fragment2().file_id()).to_linear()].append(p); + auto x=file_index_map[p->fragment1().file_ptr()]; + auto y=file_index_map[p->fragment2().file_ptr()]; + this->values_[grid_2d_coordinate(x, y).to_linear()].append(p); } } diff --git a/layer/clone_pair_grid_layer.hpp b/layer/clone_pair_grid_layer.hpp index 53a9ba0..de761c0 100644 --- a/layer/clone_pair_grid_layer.hpp +++ b/layer/clone_pair_grid_layer.hpp @@ -3,6 +3,7 @@ #include +#include #include #include @@ -12,26 +13,14 @@ namespace asterism { -template -using shared_set=QSet>; - -template -using shared_list=QList>; - -template -using shared_vector=QVector>; - -template -using shared_map=QMap>; - class clone_pair_grid_layer final : public file_separated_grid_layer> { public: clone_pair_grid_layer() noexcept; - clone_pair_grid_layer(const shared_set &clone_pairs, const uint32_t width) noexcept; + clone_pair_grid_layer(const shared_set &clone_pairs, const QMap, int> file_index_map) noexcept; - void make_layer(const QList &clone_pairs, const uint32_t width) noexcept; + void make_layer(const shared_set &clone_pairs, const QMap, int> file_index_map) noexcept; }; } diff --git a/model/clone_pair.cpp b/model/clone_pair.cpp index ad51d81..cce55df 100644 --- a/model/clone_pair.cpp +++ b/model/clone_pair.cpp @@ -49,9 +49,9 @@ QPair clone_pair::canonical(fragment &&fragment1, fragment & qMakePair(fragment2, fragment1); } -uint qHash(const clone_pair &key, [[maybe_unused]]uint seed) noexcept +uint qHash(const clone_pair &key, uint seed) noexcept { - return key.fragments_.first.begin()+key.fragments_.second.begin(); + return key.fragments_.first.begin()^seed+key.fragments_.second.begin(); } float good(const clone_pair &p1, const clone_pair &p2) noexcept diff --git a/model/detection_result.cpp b/model/detection_result.cpp index dabd240..d468152 100644 --- a/model/detection_result.cpp +++ b/model/detection_result.cpp @@ -6,19 +6,19 @@ namespace asterism detection_result::detection_result() noexcept {} -detection_result::detection_result(result_environment &&context, shared_set &&clone_pairs) noexcept - : context_(std::move(context)), clone_pairs_(std::move(clone_pairs)) +detection_result::detection_result(result_environment &&environment, shared_set &&clone_pairs) noexcept + : environment_(std::move(environment)), clone_pairs_(std::move(clone_pairs)) {} -clone_pair_grid_layer detection_result::to_layer() const noexcept +clone_pair_grid_layer detection_result::to_layer(const QMap, int> file_index_map) const noexcept { - return this->clone_pair_grid_layer_; + return clone_pair_grid_layer(this->clone_pairs_, file_index_map); } -const result_environment& detection_result::context() const noexcept +const result_environment& detection_result::environment() const noexcept { - return this->context_; + return this->environment_; } shared_set& detection_result::clone_pairs() noexcept @@ -31,4 +31,18 @@ const shared_set& detection_result::clone_pairs() const noexcept return this->clone_pairs_; } +bool detection_result::operator ==(const detection_result &detection_result) const noexcept +{ + return this->environment_.source()==detection_result.environment_.source(); +} + +uint qHash(const detection_result &key, uint seed) noexcept +{ + return qHash(key.environment().source(), seed); +} + +uint qHash(const std::shared_ptr &key, uint seed) noexcept +{ + return qHash(key->environment().source(), seed); +} } diff --git a/model/detection_result.hpp b/model/detection_result.hpp index e0a8281..d591ba0 100644 --- a/model/detection_result.hpp +++ b/model/detection_result.hpp @@ -15,18 +15,22 @@ class detection_result final { public: detection_result() noexcept; - detection_result(result_environment &&context, shared_set &&clone_pairs) noexcept; + detection_result(result_environment &&environment, shared_set &&clone_pairs) noexcept; - clone_pair_grid_layer to_layer(const shared_list &header) const noexcept; + clone_pair_grid_layer to_layer(const QMap, int> file_index_map) const noexcept; - const result_environment& context() const noexcept; + const result_environment& environment() const noexcept; shared_set& clone_pairs() noexcept; const shared_set& clone_pairs() const noexcept; + bool operator ==(const detection_result &detection_result) const noexcept; + private: - result_environment context_; + result_environment environment_; shared_set clone_pairs_; }; +uint qHash(const detection_result &key, uint seed) noexcept; +uint qHash(const std::shared_ptr &key, uint seed) noexcept; } #endif // DETECTION_RESULT_HPP diff --git a/model/file.cpp b/model/file.cpp index b39961e..b3583e0 100644 --- a/model/file.cpp +++ b/model/file.cpp @@ -16,14 +16,6 @@ file::file(QString &&canonical_file_path) noexcept : canonical_file_path_(std::move(canonical_file_path)) {} -file::file(const id_t id, const QString &canonical_file_path) noexcept - : canonical_file_path_(canonical_file_path) -{} - -file::file(const id_t id, QString &&canonical_file_path) noexcept - : canonical_file_path_(std::move(canonical_file_path)) -{} - QString file::canonical_file_path() const noexcept { return this->canonical_file_path_; @@ -34,6 +26,11 @@ file::id_t file::id() const noexcept return this->id_; } +bool file::operator <(const file &other) const noexcept +{ + return this->canonical_file_path_canonical_file_path_==to_canonical_file_path(path); @@ -56,5 +53,10 @@ uint qHash(const file &key, uint seed) noexcept return qHash(key.canonical_file_path_, seed); } +uint qHash(const std::shared_ptr &key, uint seed) noexcept +{ + return qHash(key->canonical_file_path(), seed); +} + } diff --git a/model/file.hpp b/model/file.hpp index a696310..f91b837 100644 --- a/model/file.hpp +++ b/model/file.hpp @@ -12,6 +12,24 @@ namespace asterism { +template +uint qHash(const std::shared_ptr &t, uint seed) noexcept +{ + return qHash(*t, seed); +} + +template +using shared_set=QSet>; + +template +using shared_list=QList>; + +template +using shared_vector=QVector>; + +template +using shared_map=QMap>; + class file final { public: @@ -21,12 +39,10 @@ class file final file(const QString &canonical_file_path) noexcept; file(QString &&canonical_file_path) noexcept; - file(const id_t id, const QString &canonical_file_path) noexcept; - file(const id_t id, QString &&canonical_file_path) noexcept; - id_t id() const noexcept; QString canonical_file_path() const noexcept; + bool operator <(const file &other) const noexcept; bool operator ==(const QString &path) const noexcept; bool operator ==(const file &other) const noexcept; @@ -41,6 +57,7 @@ class file final }; uint qHash(const file &key, uint seed) noexcept; +uint qHash(const std::shared_ptr &key, uint seed) noexcept; } #endif // FILE_HPP diff --git a/model/fragment.cpp b/model/fragment.cpp index 53eee8e..7b96885 100644 --- a/model/fragment.cpp +++ b/model/fragment.cpp @@ -15,7 +15,7 @@ fragment::fragment(const std::shared_ptr &file, const uint32_t begin, cons // operator bool fragment::operator <(const fragment &other) const noexcept { - if(this->file_.owner_before(other.file_)) + if(this->file_ result_environment::parameters() const noexcept return this->parameters_; } +bool result_environment::operator ==(const result_environment &other) const noexcept +{ + return this->source_==other.source_; +} + } diff --git a/model/result_environment.hpp b/model/result_environment.hpp index b90abf4..1d3e145 100644 --- a/model/result_environment.hpp +++ b/model/result_environment.hpp @@ -47,6 +47,8 @@ class result_environment final QString clone_detector_string() const noexcept; QHash parameters() const noexcept; + bool operator ==(const result_environment &other) const noexcept; + private: clone_detector clone_detector_; QString source_; diff --git a/ref.jcln b/ref.jcln index 8de5335..05d4802 100644 --- a/ref.jcln +++ b/ref.jcln @@ -1,16 +1,6 @@ { "global":{ - "target":"target_path", - "size":{ - "file_table":2, - "results":1, - "clone_pairs":[ - { - "result_id":0, - "size":1 - } - ] - } + "target":"target_path" }, "file_table":[ { @@ -24,7 +14,6 @@ ], "results":[ { - "result_id":0, "environment":{ "source":"r1_src", "clone_detector":{ @@ -35,6 +24,7 @@ } } }, + "clone_pair_size":0, "clone_pairs":[ { "clone_pair_id":0,