Skip to content

Commit

Permalink
use std::shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
kk-mats committed Apr 10, 2019
1 parent 0e6eb95 commit a72c69d
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 118 deletions.
1 change: 1 addition & 0 deletions clone_format/format_tags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
59 changes: 24 additions & 35 deletions clone_format/jcln.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,84 +44,78 @@ 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> &file_ptr, const QHash<std::shared_ptr<file>, 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<std::shared_ptr<file>, 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> &clone_pair, const QHash<std::shared_ptr<file>, 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> &detection_result, const QHash<std::shared_ptr<file>, 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];
}

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},
Expand Down Expand Up @@ -210,8 +204,6 @@ std::optional<detection_result> jcln::read_detection_result(const QJsonObject &j
return std::nullopt;
}

auto context=result_environment(clone_detector[NAME].toString(), environment[SOURCE].toString());

shared_set<clone_pair> clone_pairs;
for(auto pj:json[CLONE_PAIRS].toArray())
{
Expand All @@ -225,7 +217,7 @@ std::optional<detection_result> jcln::read_detection_result(const QJsonObject &j
clone_pairs.insert(std::make_shared<clone_pair>(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<detection_results> jcln::read_detection_results(const QJsonObject &json) noexcept
Expand Down Expand Up @@ -270,7 +262,7 @@ std::optional<detection_results> 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
Expand All @@ -288,11 +280,8 @@ std::optional<detection_results> jcln::read_detection_results(const QJsonObject
qCritical()<<code_clone_loading_error::invalid_file_format;
return std::nullopt;
}
results.insert(std::make_shared<detection_result>(std::move(r)));
}

rs.emplace_detection_result();

return std::make_optional(rs);
}

Expand Down
8 changes: 4 additions & 4 deletions clone_format/jcln.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> &file_ptr, const QHash<std::shared_ptr<file>, int> file_index_map) noexcept;
static QJsonValue to_qjson(const fragment &fragment, const QHash<std::shared_ptr<file>, int> file_index_map) noexcept;
static QJsonValue to_qjson(const std::shared_ptr<clone_pair> &clone_pair, const QHash<std::shared_ptr<file>, int> file_index_map) noexcept;
static QJsonValue to_qjson(const std::shared_ptr<detection_result> &detection_result, const QHash<std::shared_ptr<file>, int> file_index_map) noexcept;
static QJsonValue to_qjson(const detection_results &detection_results) noexcept;

static std::optional<fragment> read_fragment(const QJsonObject &json, const shared_map<int, file> &id_file_ptr_map) noexcept;
Expand Down
43 changes: 39 additions & 4 deletions core/detection_results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,30 @@ detection_results::detection_results(const QString &target_path) noexcept

std::shared_ptr<file> 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<file>(std::move(canonical_file_path)));
}
return *itr;
}

std::shared_ptr<detection_result> detection_results::empalce(result_environment &&context, shared_set<clone_pair> &&clone_pairs) noexcept
std::shared_ptr<detection_result> 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<detection_result>(std::move(context), std::move(clone_pairs)));
itr=this->results_.insert(std::make_shared<detection_result>(std::move(result)));
}
return *itr;
}

std::shared_ptr<detection_result> detection_results::empalce(result_environment &&environment, shared_set<clone_pair> &&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<detection_result>(std::move(environment), std::move(clone_pairs)));
}
return *itr;
}
Expand All @@ -42,6 +52,31 @@ bool detection_results::remove(std::shared_ptr<detection_result> &&ptr) noexcept
return false;
}

const shared_set<detection_result>& detection_results::results() const noexcept
{
return this->results_;
}

const shared_set<file>& detection_results::files() const noexcept
{
return this->files_;
}

QHash<std::shared_ptr<file>, 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<std::shared_ptr<file>, 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_;
Expand Down
7 changes: 5 additions & 2 deletions core/detection_results.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ class detection_results final
detection_results(const QString &target_path) noexcept;

std::shared_ptr<file> emplace(QString &&canonical_file_path) noexcept;
std::shared_ptr<detection_result> empalce(result_environment &&context, shared_set<clone_pair> &&clone_pairs) noexcept;
std::shared_ptr<detection_result> emplace(detection_result &&detection_result) noexcept;
std::shared_ptr<detection_result> empalce(result_environment &&environment, shared_set<clone_pair> &&clone_pairs) noexcept;

bool remove(std::shared_ptr<detection_result> &&ptr) noexcept;

shared_list<detection_result> results() const noexcept;
const shared_set<detection_result>& results() const noexcept;
const shared_set<file>& files() const noexcept;
QHash<std::shared_ptr<file>, int> file_index_map() const noexcept;

void set_target_path(const QString &target_path) noexcept;
QString target_path() const noexcept;
Expand Down
4 changes: 2 additions & 2 deletions core/matching_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace asterism
{

/*
matching_table_unit::matching_table_unit() noexcept
{}
Expand Down Expand Up @@ -184,5 +184,5 @@ uint qHash(const matching_table::key &key, uint seed) noexcept
{
return uint(key.key1_)^seed+uint(key.key2_);
}

*/
}
26 changes: 11 additions & 15 deletions core/matching_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<clone_pair::id_t> has_left_clone_pair_of(const clone_pair::id_t &right_clone_pair_id) const noexcept;
std::optional<clone_pair::id_t> 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<std::shared_ptr<clone_pair>> has_left_clone_pair_of(const std::shared_ptr<clone_pair> &ptr) const noexcept;
std::optional<std::shared_ptr<clone_pair>> has_right_clone_pair_of(const std::shared_ptr<clone_pair> &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<clone_pair> &ptr) const noexcept;
int count_right_clone_pair_of(const std::shared_ptr<clone_pair> &ptr) const noexcept;
QVector<clone_pair::id_t> left_clone_pair_of(const clone_pair::id_t &right_clone_pair_id) const noexcept;
QVector<clone_pair::id_t> right_clone_pair_of(const clone_pair::id_t &left_clone_pair_id) const noexcept;
shared_vector<clone_pair> left_clone_pair_of(const std::shared_ptr<clone_pair> &ptr) const noexcept;
shared_vector<clone_pair> right_clone_pair_of(const std::shared_ptr<clone_pair> &ptr) const noexcept;
private:
detection_result::id_t left_result_id_, right_result_id_;
QVector<QPair<clone_pair::id_t, clone_pair::id_t>> matching_list_;
QVector<QPair<std::shared_ptr<clone_pair>, std::shared_ptr<clone_pair>>> matching_list_;
bool better(const float ok_v, const float good_v, QPair<float, float> &&ok_good_max, const float t) const noexcept;
QVector<QPair<clone_pair::id_t, clone_pair::id_t>> unidirectional_matching(const detection_result &first, const detection_result &second, const float t) const noexcept;
QVector<QPair<clone_pair::id_t, clone_pair::id_t>> bidirectional_matching(const detection_result &left, const detection_result &right, const float t) const noexcept;
QVector<QPair<std::shared_ptr<clone_pair>, std::shared_ptr<clone_pair>>> unidirectional_matching(const detection_result &first, const detection_result &second, const float t) const noexcept;
QVector<QPair<std::shared_ptr<clone_pair>, std::shared_ptr<clone_pair>>> bidirectional_matching(const detection_result &left, const detection_result &right, const float t) const noexcept;
};
Expand Down Expand Up @@ -73,7 +69,7 @@ class matching_table final
};
uint qHash(const matching_table::key &key, uint seed) noexcept;

*/
}

#endif // MATCHING_TABLE_HPP
2 changes: 2 additions & 0 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -49,6 +50,7 @@ void MainWindow::open()
this->scatter_plot_model_->set_heatmap_layer(std::move(h.value()));
}
}
*/
}


Expand Down
16 changes: 10 additions & 6 deletions layer/clone_pair_grid_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_pair> &clone_pairs, const uint32_t width) noexcept
: file_separated_grid_layer(width)
clone_pair_grid_layer::clone_pair_grid_layer(const shared_set<clone_pair> &clone_pairs, const QMap<std::shared_ptr<file>, 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_pair> &clone_pairs, const uint32_t width) noexcept
void clone_pair_grid_layer::make_layer(const shared_set<clone_pair> &clone_pairs, const QMap<std::shared_ptr<file>, 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);
}
}

Expand Down
Loading

0 comments on commit a72c69d

Please sign in to comment.