From b7f5a0d9f94d64b323c2859583f393e175c6435d Mon Sep 17 00:00:00 2001 From: AndrewB330 Date: Tue, 6 Oct 2020 18:27:04 +0300 Subject: [PATCH] Added CMAKE and some small code polishing --- .gitignore | 336 +--------------------- CMakeLists.txt | 24 ++ main.cpp => example.cpp | 7 +- benchmark.h => include/emst/benchmark.hpp | 28 +- include/emst/dsu.hpp | 23 ++ emst.h => include/emst/emst.hpp | 10 +- model.h => include/emst/model.hpp | 0 tree.h => include/emst/tree.hpp | 2 +- dsu.h => src/dsu.cpp | 29 +- standalone_header/emst.hpp | 10 +- 10 files changed, 78 insertions(+), 391 deletions(-) create mode 100644 CMakeLists.txt rename main.cpp => example.cpp (86%) rename benchmark.h => include/emst/benchmark.hpp (69%) create mode 100644 include/emst/dsu.hpp rename emst.h => include/emst/emst.hpp (97%) rename model.h => include/emst/model.hpp (100%) rename tree.h => include/emst/tree.hpp (99%) rename dsu.h => src/dsu.cpp (64%) diff --git a/.gitignore b/.gitignore index 7ee3a18..49999a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,335 +1,3 @@ -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. -## -## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore - -# User-specific files -*.suo -*.user -*.userosscache -*.sln.docstates - -# User-specific files (MonoDevelop/Xamarin Studio) -*.userprefs - -# Build results -[Dd]ebug/ -[Dd]ebugPublic/ -[Rr]elease/ -[Rr]eleases/ -x64/ -x86/ -bld/ -[Bb]in/ -[Oo]bj/ -[Ll]og/ - -# Visual Studio 2015/2017 cache/options directory -.vs/ -# Uncomment if you have tasks that create the project's static files in wwwroot -#wwwroot/ - -# Visual Studio 2017 auto generated files -Generated\ Files/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -# NUNIT -*.VisualState.xml -TestResult.xml - -# Build Results of an ATL Project -[Dd]ebugPS/ -[Rr]eleasePS/ -dlldata.c - -# Benchmark Results -BenchmarkDotNet.Artifacts/ - -# .NET Core -project.lock.json -project.fragment.lock.json -artifacts/ -**/Properties/launchSettings.json - -# StyleCop -StyleCopReport.xml - -# Files built by Visual Studio -*_i.c -*_p.c -*_i.h -*.ilk -*.meta -*.obj -*.iobj -*.pch -*.pdb -*.ipdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.svclog -*.scc - -# Chutzpah Test files -_Chutzpah* - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opendb -*.opensdf -*.sdf -*.cachefile -*.VC.db -*.VC.VC.opendb - -# Visual Studio profiler -*.psess -*.vsp -*.vspx -*.sap - -# Visual Studio Trace Files -*.e2e - -# TFS 2012 Local Workspace -$tf/ - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper -*.DotSettings.user - -# JustCode is a .NET coding add-in -.JustCode - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# AxoCover is a Code Coverage Tool -.axoCover/* -!.axoCover/settings.json - -# Visual Studio code coverage results -*.coverage -*.coveragexml - -# NCrunch -_NCrunch_* -.*crunch*.local.xml -nCrunchTemp_* - -# MightyMoose -*.mm.* -AutoTest.Net/ - -# Web workbench (sass) -.sass-cache/ - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.[Pp]ublish.xml -*.azurePubxml -# Note: Comment the next line if you want to checkin your web deploy settings, -# but database connection strings (with potential passwords) will be unencrypted -*.pubxml -*.publishproj - -# Microsoft Azure Web App publish settings. Comment the next line if you want to -# checkin your Azure Web App publish settings, but sensitive information contained -# in these scripts will be unencrypted -PublishScripts/ - -# NuGet Packages -*.nupkg -# The packages folder can be ignored because of Package Restore -**/[Pp]ackages/* -# except build/, which is used as an MSBuild target. -!**/[Pp]ackages/build/ -# Uncomment if necessary however generally it will be regenerated when needed -#!**/[Pp]ackages/repositories.config -# NuGet v3's project.json files produces more ignorable files -*.nuget.props -*.nuget.targets - -# Microsoft Azure Build Output -csx/ -*.build.csdef - -# Microsoft Azure Emulator -ecf/ -rcf/ - -# Windows Store app package directories and files -AppPackages/ -BundleArtifacts/ -Package.StoreAssociation.xml -_pkginfo.txt -*.appx - -# Visual Studio cache files -# files ending in .cache can be ignored -*.[Cc]ache -# but keep track of directories ending in .cache -!*.[Cc]ache/ - -# Others -ClientBin/ -~$* -*~ -*.dbmdl -*.dbproj.schemaview -*.jfm -*.pfx -*.publishsettings -orleans.codegen.cs - -# Including strong name files can present a security risk -# (https://github.com/github/gitignore/pull/2483#issue-259490424) -#*.snk - -# Since there are multiple workflows, uncomment next line to ignore bower_components -# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) -#bower_components/ - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file -# to a newer Visual Studio version. Backup files are not needed, -# because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm -ServiceFabricBackup/ -*.rptproj.bak - -# SQL Server files -*.mdf -*.ldf -*.ndf - -# Business Intelligence projects -*.rdl.data -*.bim.layout -*.bim_*.settings -*.rptproj.rsuser - -# Microsoft Fakes -FakesAssemblies/ - -# GhostDoc plugin setting file -*.GhostDoc.xml - -# Node.js Tools for Visual Studio -.ntvs_analysis.dat -node_modules/ - -# Visual Studio 6 build log -*.plg - -# Visual Studio 6 workspace options file -*.opt - -# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) -*.vbw - -# Visual Studio LightSwitch build output -**/*.HTMLClient/GeneratedArtifacts -**/*.DesktopClient/GeneratedArtifacts -**/*.DesktopClient/ModelManifest.xml -**/*.Server/GeneratedArtifacts -**/*.Server/ModelManifest.xml -_Pvt_Extensions - -# Paket dependency manager -.paket/paket.exe -paket-files/ - -# FAKE - F# Make -.fake/ - -# JetBrains Rider +cmake-build-release/ +cmake-build-debug/ .idea/ -*.sln.iml - -# CodeRush -.cr/ - -# Python Tools for Visual Studio (PTVS) -__pycache__/ -*.pyc - -# Cake - Uncomment if you are using it -# tools/** -# !tools/packages.config - -# Tabs Studio -*.tss - -# Telerik's JustMock configuration file -*.jmconfig - -# BizTalk build output -*.btp.cs -*.btm.cs -*.odx.cs -*.xsd.cs - -# OpenCover UI analysis results -OpenCover/ - -# Azure Stream Analytics local run output -ASALocalRun/ - -# MSBuild Binary and Structured Log -*.binlog - -# NVidia Nsight GPU debugger configuration file -*.nvuser - -# MFractors (Xamarin productivity tool) working folder -.mfractor/ - -*.vcxproj -*.vcxproj.filters - -*.exe \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..93abc2d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.13) +project(EuclideanMST) +set(CMAKE_CXX_STANDARD 17) + +set( + HEADER_FILES + include/emst/benchmark.hpp + include/emst/dsu.hpp + include/emst/model.hpp + include/emst/tree.hpp + include/emst/emst.hpp +) + +set( + SOURCE_FILES + src/dsu.cpp +) + +# Add executable target with source files listed in SOURCE_FILES variable +add_library(EuclideanMST ${HEADER_FILES} ${SOURCE_FILES}) +target_include_directories(EuclideanMST PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") + +add_executable(Example example.cpp) +target_link_libraries(Example EuclideanMST) diff --git a/main.cpp b/example.cpp similarity index 86% rename from main.cpp rename to example.cpp index f1c329b..dd8d801 100644 --- a/main.cpp +++ b/example.cpp @@ -1,19 +1,16 @@ #include #include #include -#include #include -#include #include #include -#include "emst.h" -#include "benchmark.h" +#include "emst/emst.hpp" using namespace std; template void example() { - fstream fin("test_data/dim" + to_string(DIM) + ".txt"); + fstream fin("../test_data/dim" + to_string(DIM) + ".txt"); size_t n; fin >> n; vector> points(n); for (size_t i = 0; i < n; i++) { diff --git a/benchmark.h b/include/emst/benchmark.hpp similarity index 69% rename from benchmark.h rename to include/emst/benchmark.hpp index 5448658..00c1b52 100644 --- a/benchmark.h +++ b/include/emst/benchmark.hpp @@ -3,7 +3,7 @@ #include #include #include -#include "emst.h" +#include "emst.hpp" using namespace std::chrono; double uniform() { @@ -43,7 +43,7 @@ struct BenchmarkResult { template BenchmarkResult run_benchmark(const std::vector> & points, size_t samples = 1) { - BenchmarkResult result; + BenchmarkResult result{}; auto start = std::chrono::system_clock::now(); for (size_t i = 0; i < samples; i++) { EmstSolverType solver(points); @@ -57,7 +57,7 @@ BenchmarkResult run_benchmark(const std::vector> & points, size_t sam } template -void run_becnhmarks(const std::vector> & points, size_t samples, std::ostream & cout, std::string distribution = "none") { +void run_benchmark(const std::vector> & points, size_t samples, std::ostream & cout, const std::string& distribution = "none") { auto result_kd = run_benchmark, DIM>(points, samples); auto result_prim = (points.size() < 100000 ? run_benchmark, DIM>(points, samples / 50 + 1) : BenchmarkResult{ result_kd.answer, 0 }); @@ -71,15 +71,15 @@ void run_becnhmarks(const std::vector> & points, size_t samples, std: template void run_becnhmarks(std::ostream & cout) { - run_becnhmarks(generate_points_uniform(10), 100, cout, "uniform"); - run_becnhmarks(generate_points_uniform(50), 100, cout, "uniform"); - run_becnhmarks(generate_points_uniform(100), 1, cout, "uniform"); - run_becnhmarks(generate_points_uniform(500), 1, cout, "uniform"); - run_becnhmarks(generate_points_uniform(1000), 1, cout, "uniform"); - run_becnhmarks(generate_points_uniform(5000), 1, cout, "uniform"); - run_becnhmarks(generate_points_uniform(10000), 1, cout, "uniform"); - run_becnhmarks(generate_points_uniform(50000), 1, cout, "uniform"); - /*run_becnhmarks(generate_points_uniform(100000), 1, cout, "uniform"); - run_becnhmarks(generate_points_uniform(1000000), 1, cout, "uniform"); - run_becnhmarks(generate_points_uniform(10000000), 1, cout, "uniform");*/ + run_benchmark(generate_points_uniform(10), 100, cout, "uniform"); + run_benchmark(generate_points_uniform(50), 100, cout, "uniform"); + run_benchmark(generate_points_uniform(100), 1, cout, "uniform"); + run_benchmark(generate_points_uniform(500), 1, cout, "uniform"); + run_benchmark(generate_points_uniform(1000), 1, cout, "uniform"); + run_benchmark(generate_points_uniform(5000), 1, cout, "uniform"); + run_benchmark(generate_points_uniform(10000), 1, cout, "uniform"); + run_benchmark(generate_points_uniform(50000), 1, cout, "uniform"); + /*run_benchmark(generate_points_uniform(100000), 1, cout, "uniform"); + run_benchmark(generate_points_uniform(1000000), 1, cout, "uniform"); + run_benchmark(generate_points_uniform(10000000), 1, cout, "uniform");*/ } \ No newline at end of file diff --git a/include/emst/dsu.hpp b/include/emst/dsu.hpp new file mode 100644 index 0000000..f997888 --- /dev/null +++ b/include/emst/dsu.hpp @@ -0,0 +1,23 @@ +#pragma once +#include + +/** + * Disjoint-set-union data structure + */ +class DSU { + +public: + + explicit DSU(size_t n = 0); + + size_t get_set(size_t x) const; + bool is_in_same_set(size_t x, size_t y) const; + + void reset(size_t n); + bool unite(size_t x, size_t y); + +private: + + mutable std::vector p; + std::vector rank; +}; diff --git a/emst.h b/include/emst/emst.hpp similarity index 97% rename from emst.h rename to include/emst/emst.hpp index 88fff47..e09d1de 100644 --- a/emst.h +++ b/include/emst/emst.hpp @@ -1,14 +1,14 @@ #pragma once -#include "model.h" -#include "tree.h" -#include "dsu.h" +#include "model.hpp" +#include "tree.hpp" +#include "dsu.hpp" typedef std::pair Edge; template class EmstSolver { public: - EmstSolver() {} + EmstSolver() = default; const std::vector & get_solution() const { return solution; } const double & get_total_length() const { return total_length; } @@ -26,7 +26,7 @@ class EmstSolver { template class KdTreeSolver : public EmstSolver { public: - KdTreeSolver(const std::vector> & points) :num_points(points.size()) { + explicit KdTreeSolver(const std::vector> & points) :num_points(points.size()) { dsu.reset(num_points); tree = KdTree(points, static_cast(floor(log2(num_points)) - 1)); is_fully_connected.assign(tree.get_maximal_id() + 1, false); diff --git a/model.h b/include/emst/model.hpp similarity index 100% rename from model.h rename to include/emst/model.hpp diff --git a/tree.h b/include/emst/tree.hpp similarity index 99% rename from tree.h rename to include/emst/tree.hpp index 1f9b36d..17eaffa 100644 --- a/tree.h +++ b/include/emst/tree.hpp @@ -3,7 +3,7 @@ #include #include #include -#include "model.h" +#include "model.hpp" /** * K-d tree data structure diff --git a/dsu.h b/src/dsu.cpp similarity index 64% rename from dsu.h rename to src/dsu.cpp index 4247020..a8ae046 100644 --- a/dsu.h +++ b/src/dsu.cpp @@ -1,34 +1,9 @@ -#pragma once -#include - -/** - * Disjoint-set-union data structure - */ -class DSU { - -public: - - DSU(size_t n = 0); - - size_t get_set(size_t x) const; - bool is_in_same_set(size_t x, size_t y) const; - - void reset(size_t n); - bool unite(size_t x, size_t y); - -private: - - mutable std::vector p; - std::vector rank; -}; - -// Implementation +#include DSU::DSU(size_t n) { reset(n); } - size_t DSU::get_set(size_t x) const { return p[x] == x ? x : p[x] = get_set(p[x]); } @@ -59,4 +34,4 @@ bool DSU::unite(size_t x, size_t y) { } p[set_a] = set_b; return true; -} \ No newline at end of file +} diff --git a/standalone_header/emst.hpp b/standalone_header/emst.hpp index a387fe1..e7408ea 100644 --- a/standalone_header/emst.hpp +++ b/standalone_header/emst.hpp @@ -283,7 +283,7 @@ class DSU { public: - DSU(size_t n = 0); + explicit DSU(size_t n = 0); size_t get_set(size_t x) const; bool is_in_same_set(size_t x, size_t y) const; @@ -341,7 +341,7 @@ typedef std::pair Edge; template class EmstSolver { public: - EmstSolver() {} + EmstSolver() = default; const std::vector & get_solution() const { return solution; } const double & get_total_length() const { return total_length; } @@ -359,7 +359,7 @@ class EmstSolver { template class KdTreeSolver : public EmstSolver { public: - KdTreeSolver(const std::vector> & points) :num_points(points.size()) { + explicit KdTreeSolver(const std::vector> & points) :num_points(points.size()) { dsu.reset(num_points); tree = KdTree(points, static_cast(floor(log2(num_points)) - 1)); is_fully_connected.assign(tree.get_maximal_id() + 1, false); @@ -474,7 +474,7 @@ class KdTreeSolver : public EmstSolver { template class PrimSolver : public EmstSolver { public: - PrimSolver(const std::vector> & points) { + explicit PrimSolver(const std::vector> & points) { solve(points); } @@ -509,4 +509,4 @@ class PrimSolver : public EmstSolver { } } -}; \ No newline at end of file +};