From 69308d7faa1a8c09711799f1279ebac91079e675 Mon Sep 17 00:00:00 2001 From: Pietro Incardona <incardon@mpi-cbg.de> Date: Fri, 26 Jun 2015 18:23:14 +0200 Subject: [PATCH] Added missing files --- Makefile.am | 3 + src/csv_multiarray.hpp | 204 +++++++++++++++++++++++++++++++++++++++++ src/util.hpp | 172 ++++++++++++++++++++++++++++++++++ 3 files changed, 379 insertions(+) create mode 100755 Makefile.am create mode 100644 src/csv_multiarray.hpp create mode 100644 src/util.hpp diff --git a/Makefile.am b/Makefile.am new file mode 100755 index 0000000..7c618d3 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,3 @@ +SUBDIRS = src + +bin_PROGRAMS = \ No newline at end of file diff --git a/src/csv_multiarray.hpp b/src/csv_multiarray.hpp new file mode 100644 index 0000000..27b2f62 --- /dev/null +++ b/src/csv_multiarray.hpp @@ -0,0 +1,204 @@ +/* + * csv_multiarray_copy.hpp + * + * Created on: Jun 20, 2015 + * Author: i-bird + */ + +#ifndef CSV_MULTIARRAY_COPY_HPP_ +#define CSV_MULTIARRAY_COPY_HPP_ + + + +/*! \brief This class is an helper to produce csv headers from multi-array + * + * Usage: + * + * \code{.cpp} + * + * float src[3]; + * + * std::stringstream str; + * csv_col_str<float[3]> cp(std::string("test"),str); + * + * std::cout << str.str() << "\n"; + * + * \endcode + * + * Will produce ",test[0],test[1],test[2]" + * + */ +template<typename T> +struct csv_col_str +{ + inline csv_col_str(std::string prp, std::stringstream & str) + { + str << "," << prp; + } +}; + +//! Partial specialization for N=1 1D-Array +template<typename T,size_t N1> +struct csv_col_str<T[N1]> +{ + inline csv_col_str(std::string prp, std::stringstream & str) + { + for (size_t i = 0 ; i < N1 ; i++) + str << "," << prp << "_" << "[" << i << "]"; + } +}; + +//! Partial specialization for N=2 2D-Array +template<typename T,size_t N1,size_t N2> +struct csv_col_str<T[N1][N2]> +{ + inline csv_col_str(std::string prp, std::stringstream & str) + { + for (size_t i1 = 0 ; i1 < N1 ; i1++) + { + for (size_t i2 = 0 ; i2 < N2 ; i2++) + { + str << "," << prp << "_" << "[" << i1 << "]" << "[" << i2 << "]"; + } + } + } +}; + +//! Partial specialization for N=3 +template<typename T,size_t N1,size_t N2,size_t N3> +struct csv_col_str<T[N1][N2][N3]> +{ + inline csv_col_str(std::string prp, std::stringstream & str) + { + for (size_t i1 = 0 ; i1 < N1 ; i1++) + { + for (size_t i2 = 0 ; i2 < N2 ; i2++) + { + for (size_t i3 = 0 ; i3 < N3 ; i3++) + { + str << "," << prp << "_" << "[" << i1 << "]" << "[" << i2 << "]" << "[" << i3 << "]"; + } + } + } + } +}; + +//! Partial specialization for N=4 +template<typename T,size_t N1,size_t N2,size_t N3,size_t N4> +struct csv_col_str<T[N1][N2][N3][N4]> +{ + inline csv_col_str(std::string prp, std::stringstream & str) + { + for (size_t i1 = 0 ; i1 < N1 ; i1++) + { + for (size_t i2 = 0 ; i2 < N2 ; i2++) + { + for (size_t i3 = 0 ; i3 < N3 ; i3++) + { + for (size_t i4 = 0 ; i4 < N4 ; i4++) + { + str << "," << prp << "_" << "[" << i1 << "]" << "[" << i2 << "]" << "[" << i3 << "]" << "[" << i4 << "]"; + } + } + } + } + } +}; + + +/*! \brief This class is an helper to produce csv data from multi-array + * + * Usage: + * + * \code{.cpp} + * + * float src[3] = {1.0,2.0,3.0}; + * + * std::stringstream str; + * csv_value_str<float[3]> cp(src,str); + * + * std::cout << str.str() << "\n"; + * + * \endcode + * + * Will produce ",1.0,2.0,3.0" + * + */ +template<typename T> +struct csv_value_str +{ + inline csv_value_str(T & v, std::stringstream & str) + { + str << "," << v; + } +}; + +//! Partial specialization for N=1 1D-Array +template<typename T,size_t N1> +struct csv_value_str<T[N1]> +{ + inline csv_value_str(const T v[N1], std::stringstream & str) + { + for (size_t i = 0 ; i < N1 ; i++) + str << "," << v[i]; + } +}; + +//! Partial specialization for N=2 2D-Array +template<typename T,size_t N1,size_t N2> +struct csv_value_str<T[N1][N2]> +{ + inline csv_value_str(const T v[N1][N2], std::stringstream & str) + { + for (size_t i1 = 0 ; i1 < N1 ; i1++) + { + for (size_t i2 = 0 ; i2 < N2 ; i2++) + { + str << "," << v[i1][i2]; + } + } + } +}; + +//! Partial specialization for N=3 +template<typename T,size_t N1,size_t N2,size_t N3> +struct csv_value_str<T[N1][N2][N3]> +{ + inline csv_value_str(const T v[N1][N2][N3], std::stringstream & str) + { + for (size_t i1 = 0 ; i1 < N1 ; i1++) + { + for (size_t i2 = 0 ; i2 < N2 ; i2++) + { + for (size_t i3 = 0 ; i3 < N3 ; i3++) + { + str << "," << v[i1][i2][i3]; + } + } + } + } +}; + +//! Partial specialization for N=4 +template<typename T,size_t N1,size_t N2,size_t N3,size_t N4> +struct csv_value_str<T[N1][N2][N3][N4]> +{ + inline csv_value_str(const T v[N1][N2][N3][N4], std::stringstream & str) + { + for (size_t i1 = 0 ; i1 < N1 ; i1++) + { + for (size_t i2 = 0 ; i2 < N2 ; i2++) + { + for (size_t i3 = 0 ; i3 < N3 ; i3++) + { + for (size_t i4 = 0 ; i4 < N4 ; i4++) + { + str << "," << v[i1][i2][i3][i4]; + } + } + } + } + } +}; + +#endif /* CSV_MULTIARRAY_COPY_HPP_ */ diff --git a/src/util.hpp b/src/util.hpp new file mode 100644 index 0000000..16d4252 --- /dev/null +++ b/src/util.hpp @@ -0,0 +1,172 @@ +/* + * util.hpp + * + * Created on: May 7, 2015 + * Author: Pietro Incardona + */ + +#ifndef UTIL_HPP_ +#define UTIL_HPP_ + +#include <boost/iostreams/device/mapped_file.hpp> + + +/*! \brief Compare two files, return true if they match + * + * \param file1 path1 + * \param file2 path2 + * + * \return true if they match + * + */ +bool compare(std::string file1, std::string file2) +{ + boost::iostreams::mapped_file_source f1(file1); + boost::iostreams::mapped_file_source f2(file2); + + if( f1.size() == f2.size() && std::equal(f1.data(), f1.data() + f1.size(), f2.data()) ) + return true; + else + return false; +} + +struct RGB +{ + float R; + float G; + float B; + + std::string toString() + { + return std::to_string(R) + " " + std::to_string(G) + " " + std::to_string(B); + } +}; + +/*! \brief Return the color sampled from one group + * + * groups: + * + * 0: Red + * 1: Green + * 2: Blue + * 3: Yellow + * 4: Cyan + * 5: Magenta + * 6: Orange + * 7: Chartreuse-Green + * 8: Spring Green + * 9: Azure + * 10: Violet + * 11: Rose + * + * \param group + * + */ + +struct RGB getColor(int group, std::uniform_real_distribution<float> & d, std::default_random_engine & g) +{ + struct RGB col; + + if (group == 0) + { + float s = d(g); + col.R = s/2 + 0.5; + col.G = 0.0; + col.B = 0.0; + } + else if (group == 1) + { + float s = d(g); + col.R = 0.0; + col.G = s/2 + 0.5; + col.B = 0.0; + } + else if (group == 2) + { + float s = d(g); + col.R = 0.0; + col.G = 0.0; + col.B = s; + } + else if (group == 3) + { + float s = d(g); + col.R = s/2 + 0.5; + col.G = s/2 + 0.5; + col.B = 0.0; + } + else if (group == 4) + { + float s = d(g); + col.R = s/2 + 0.5; + col.G = 0.0; + col.B = s/2 + 0.5; + } + else if (group == 5) + { + float s = d(g); + col.R = 0.0; + col.G = s/2 + 0.5; + col.B = s/2 + 0.5; + } + else if (group == 6) + { + float s = d(g); + col.R = s/2 + 0.5; + col.G = s/4 + 0.5; + col.B = 0.0; + } + else if (group == 7) + { + float s = d(g); + col.R = s/4 + 0.5; + col.G = s/2 + 0.5; + col.B = 0.0; + } + else if (group == 8) + { + float s = d(g); + col.R = 0.0; + col.G = s/2 + 0.5; + col.B = s/4 + 0.5; + } + else if (group == 9) + { + float s = d(g); + col.R = 0.0; + col.G = s/4 + 0.5; + col.B = s/2 + 0.5; + } + else if (group == 10) + { + float s = d(g); + col.R = s/4 + 0.5; + col.G = 0.0; + col.B = s/2 + 0.5; + } + else if (group == 11) + { + float s = d(g); + col.R = s/2 + 0.5; + col.G = 0.0; + col.B = s/4 + 0.5; + } + + return col; +} + +/*! \brief Check if one string end with a particular string + * + * \param fullString string to check + * \param ending ending string to check + * + */ +bool hasEnding (std::string const &fullString, std::string const &ending) +{ + if (fullString.length() >= ending.length()) + {return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));} + else + {return false;} +} + +#endif /* UTIL_HPP_ */ -- GitLab