#include "common.h"
#include <sstream>

namespace common{


	string float_to_str(double v) {
		return std::to_string(static_cast<long long>(v * STR_DOUBLE_PRECISION));

	}
	double str_to_float(const string& s) {
		return std::stoll(s) / static_cast<double>(STR_DOUBLE_PRECISION);
	}

	string vec_to_str(const vector<string>& vec, char sep){
		if (vec.size() == 0)
			return std::string();
		std::string vec_str;
		for (string v : vec)
			vec_str.append(v + sep);
		vec_str.pop_back();
		return vec_str;
	}

	string vec_to_str(const vector<double>& vec, char sep){
		if (vec.size() == 0)
			return std::string();
		std::string vec_str;
		for (double v : vec)
			vec_str.append(float_to_str(v) + sep);
		vec_str.pop_back();
		return vec_str;
	}

	string vec_to_str(const vector<size_t>& vec, char sep) {
		if (vec.size() == 0)
			return std::string();
		std::string vec_str;
		for (size_t v : vec)
			vec_str.append(std::to_string(v) + sep);
		vec_str.pop_back();
		return vec_str;
	}

	string vec_to_str(const vector<bool>& vec, char sep) {
		if (vec.size() == 0)
			return std::string();
		std::string vec_str;
		for (bool v : vec)
			vec_str.append(bool_to_str(v) + sep);
		vec_str.pop_back();
		return vec_str;
	}


	vector<double> str_to_vec_double(const string& str, char sep) {
		vector<string> vec_str = common::split(str, sep);
		vector<double> res(vec_str.size());
		for (size_t i = 0; i < vec_str.size(); i++)
			res[i] = str_to_float(vec_str[i]);
		return res;
	}

	vector<bool> str_to_vec_bool(const string& str, char sep) {
		vector<string> vec_str = common::split(str, sep);
		vector<bool> res(vec_str.size());
		for (size_t i = 0; i < vec_str.size(); i++)
			res[i] = str_to_bool(vec_str[i]);
		return res;
	}

	vector<unsigned int> str_to_vec_uint(const string& str, char sep) {
		vector<string> vec_str = common::split(str, sep);
		vector<unsigned int> res(vec_str.size());
		for (size_t i = 0; i < vec_str.size(); i++)
			res[i] = std::stoul(vec_str[i]);
		return res;
	}

	string bool_to_str(bool val){
		return val ? "true" : "false";
	}

	bool str_to_bool(const string& str){
		return str.compare("true") == 0;
	}

	std::vector<std::string> split(const std::string &s, char delim) {
		std::stringstream ss(s);
		std::string item;
		std::vector<std::string> elems;
		while (std::getline(ss, item, delim)) {
			elems.push_back(std::move(item));
		}
		return elems;
	}



}