曦光代码托管

曦光1.0
XDU_mzb
#include <bits/stdc++.h>

namespace xi_guang
  {
    using ll = long long int;
  	using std::map;
  	using std::string;
  	using std::function;
  	using std::cin;
  	using std::cout;
  	using std::endl;
  	using std::make_shared;
  	using pair_t = std::pair<string,string>;
#define add_func(name) func[#name] = std::bind(name,this,std::placeholders::_1,std::placeholders::_2)
    template<typename T,typename U>
    std::pair<string,string> pair(T t,U u)
      {
      	return std::make_pair<string,string>(t,u);
	  }
	
	string next_string(string val)
	  {
	  	return std::to_string(std::stoll(val) + 1);
	  }
	string prev_string(string val)
	  {
	  	return std::to_string(std::stoll(val) - 1);
	  }
	
	struct base;
	using parameter = std::shared_ptr<base>;
  	struct base
  	  {
  	  	map<string,string> value;
  	  	map<string,function<base*(base*,parameter)>> func;
  	  	base* run(string name,parameter par = nullptr)
  	  	  {
  	  	  	if (func.count(name) == 0)
  	  	  	  {
  	  	  	  	cout << "base -> run -> unknow function :: " << name << endl;
  	  	  	  	exit(2);
					}
  	  	  	return func[name](this,par);
			  }
		base(std::vector<std::pair<string,string>> v = {})
  	  	  {
  	  	  	for (auto it : v)
  	  	  	  value[it.first] = it.second;
			  }
  	  	virtual base* clone()
		  {
		    return new base(*this);	
		  }
  	  	virtual ~base() {};
		};
	void help_make_ptr(std::vector<std::pair<string,string>> &ret)
	  {}
	template<typename ...Args>
	void help_make_ptr(std::vector<std::pair<string,string>> &ret,std::pair<string,string> val,Args ... args)
	  {
	  	ret.emplace_back(val);
	  	help_make_ptr(ret,args...);
	  }
	template<typename ...Args>
	parameter make_par(Args ... args)
	  {
	  	std::vector<std::pair<string,string>> ret;
	  	help_make_ptr(ret,args...);
	  	return std::make_shared<base>(ret);
	  }

	struct vector : base
	  {
		string type;
	  	vector(string type,string number = "0",string val = "")
	  	  : type(type)
	  	    {
	  	    	add_func(show);
	  	    	add_func(push_back);
	  	    	add_func(add); 
	  	    	for (ll i = 0;i < std::stoll(number);i++)
	  	    	  {
	  	    	  	value[std::to_string(i)] = val;
					}
			  }	  
		base* push_back(base *raw,parameter par)
		  {
		  	for (auto it : par -> value)
		  	  {
		  	  	auto &value = raw -> value;
		  	  	value[it.first] = it.second;
				}
		  	return raw;
		  }
		base* add(base *raw,parameter par)
		  {
		  	std::vector<pair_t> v;
	  	  	for (auto it : raw -> value)
	  	  	  {
	  	  	  	v.emplace_back(it);
				}
			std::sort(v.begin(),v.end(),[](pair_t a,pair_t b){return stoll(a.first) < stoll(b.first);});
 	  	  	
	  	  	for (auto &it : v)
		  	  {
		  	  	it.second = it.second + par -> value["a"];
		  	  	if (par -> func.size())
		  	  	  par -> run("next");
				}
			raw -> value.clear();
			for (auto it : v)
			  raw -> value.insert(it); 
		  	return raw;
		  }
	  	base* show(base *raw,parameter par)
	  	  {
	  	  	cout << "[";
	  	  	std::vector<pair_t> v;
	  	  	for (auto it : raw -> value)
	  	  	  {
	  	  	  	v.emplace_back(it);
				}
			std::sort(v.begin(),v.end(),[](pair_t a,pair_t b){return stoll(a.first) < stoll(b.first);});
 	  	  	bool is_first = 0;
	  	  	for (auto it : v)
	  	  	  {
	  	  	  	if (is_first == 0)
	  	  	  	  cout << it.second,
	  	  	  	  is_first = 1;
	  	  	  	else
	  	  	  	  cout << "," << it.second;
				  }
	  	  	cout << "]\n";
	  	  	return raw;
			}
	  	virtual base* clone()
	  	  {
	  	  	return new vector(*this);
			}
	  };
	  
	struct inc : base
	  {
	  	inc(string init)
	  	  {
	  	  	add_func(next);
	  	  	value["a"] = init;
			}
		base* next(base *raw,parameter par)
		  {
		  	raw -> value["a"] = next_string(raw -> value["a"]);
		  	return raw;
		  }
	  };
	
	int main()
	  {
	  	auto vec = std::make_shared<vector>("string","10","test");
	  	vec -> run("show") -> run("push_back",make_par(pair("11","a"),pair("12","c"))) -> run("show");
	  	vec -> run("add",std::make_shared<inc>("1")) -> run("show");
		vec -> run("add",make_par(pair("a",".in"))) -> run("show");
		return 0;
	  }
   } 
int main()
  {
	xi_guang::main();
	return 0;
  }
上一篇:oss 上传


下一篇:IDEA第三章----idea常用配置