fork download
  1. //-----------------------------------------------------------------------------------------------------------------------------------------------
  2. //-----------------------------------------------------------------------------------------------------------------------------------------------
  3. // ./src/typeDef.h
  4.  
  5. #ifdef _WIN32
  6. typedef unsigned char uchar;
  7.  
  8. typedef __int8 int8;
  9. typedef __int16 int16;
  10. typedef __int32 int32;
  11. typedef __int64 int64;
  12.  
  13. typedef unsigned __int8 uint8;
  14. typedef unsigned __int16 uint16;
  15. typedef unsigned __int32 uint32;
  16. typedef unsigned __int64 uint64;
  17. #else
  18. #include <stdint.h>
  19.  
  20. typedef unsigned char uchar;
  21.  
  22. typedef int8_t int8;
  23. typedef int16_t int16;
  24. typedef int32_t int32;
  25. typedef int64_t int64;
  26.  
  27. typedef uint8_t uint8;
  28. typedef uint16_t uint16;
  29. typedef uint32_t uint32;
  30. typedef uint64_t uint64;
  31. #endif
  32.  
  33. #ifndef uint
  34. typedef unsigned int uint;
  35. #endif
  36.  
  37. //-----------------------------------------------------------------------------------------------------------------------------------------------
  38. //-----------------------------------------------------------------------------------------------------------------------------------------------
  39. // ./src/math.hpp
  40.  
  41. namespace sstd{
  42. uint8 pow(const uint8 & base, const uint8 & exp);
  43. uint16 pow(const uint16& base, const uint16& exp);
  44. uint32 pow(const uint32& base, const uint32& exp);
  45. uint64 pow(const uint64& base, const uint64& exp);
  46. float pow(const float& base, const float& exp);
  47. double pow(const double& base, const double& exp);
  48. }
  49.  
  50. //-----------------------------------------------------------------------------------------------------------------------------------------------
  51. //-----------------------------------------------------------------------------------------------------------------------------------------------
  52. // ./src/math.cpp
  53.  
  54. #include <cmath>
  55.  
  56. // base ^ exponent
  57. #define SSTD_DEF_pow_unsigned(T, base, exp) \
  58.   T b=base, e=exp; \
  59.   \
  60.   if(e==(T)0){ return (T)1; } \
  61.   \
  62.   for(; e>(T)0; e>>=1){ \
  63.   if(e & 1){ \
  64.   if(e==(T)1){ return b; } \
  65.   break; \
  66.   } \
  67.   b *= b; \
  68.   } \
  69.   T buf = b; \
  70.   buf *= buf; \
  71.   e>>=1; \
  72.   \
  73.   for(;;e>>=1){ \
  74.   if(e & 1){ \
  75.   b = b * buf; \
  76.   if(e==(T)1){ return b; } \
  77.   } \
  78.   buf *= buf; \
  79.   } \
  80.   return b;
  81. uint8 sstd::pow(const uint8 & base, const uint8 & exp){ SSTD_DEF_pow_unsigned(uint8, base, exp); }
  82. uint16 sstd::pow(const uint16& base, const uint16& exp){ SSTD_DEF_pow_unsigned(uint16, base, exp); }
  83. uint32 sstd::pow(const uint32& base, const uint32& exp){ SSTD_DEF_pow_unsigned(uint32, base, exp); }
  84. uint64 sstd::pow(const uint64& base, const uint64& exp){ SSTD_DEF_pow_unsigned(uint64, base, exp); }
  85. float sstd::pow(const float& base, const float& exp){ return std::pow( (float)base, (float)exp); }
  86. double sstd::pow(const double& base, const double& exp){ return std::pow((double)base, (double)exp); }
  87.  
  88. #undef SSTD_DEF_pow_unsigned
  89.  
  90. //-----------------------------------------------------------------------------------------------------------------------------------------------
  91. //-----------------------------------------------------------------------------------------------------------------------------------------------
  92. // ./src/stdVector_expansion/stdVector_expansion.hpp
  93.  
  94. #include <vector>
  95.  
  96. #define SSTD_DEF_stdVecEx_defInNamespace(Func) \
  97.   template <typename T> std::vector<T> Func(const std::vector<T>& lhs, const std::vector<T>& rhs); \
  98.   template <typename T, typename rhsType> std::vector<T> Func(const std::vector<T>& lhs, const rhsType& rhs); \
  99.   template <typename T, typename lhsType> std::vector<T> Func(const lhsType& lhs, const std::vector<T>& rhs);
  100. #define SSTD_DEF_stdVecEx_defInNamespace_eq(Func) \
  101.   template <typename T> std::vector<T>& Func( std::vector<T>& lhs, const std::vector<T>& rhs); \
  102.   template <typename T, typename rhsType> std::vector<T>& Func( std::vector<T>& lhs, const rhsType& rhs);
  103.  
  104. namespace sstd_stdVecEx{
  105. // operators for mathematics
  106. SSTD_DEF_stdVecEx_defInNamespace (add ); // +
  107. SSTD_DEF_stdVecEx_defInNamespace_eq(add_eq); // +=
  108. SSTD_DEF_stdVecEx_defInNamespace (sub ); // -
  109. SSTD_DEF_stdVecEx_defInNamespace_eq(sub_eq); // -=
  110. SSTD_DEF_stdVecEx_defInNamespace (mul ); // *
  111. SSTD_DEF_stdVecEx_defInNamespace_eq(mul_eq); // *=
  112. SSTD_DEF_stdVecEx_defInNamespace (div ); // /
  113. SSTD_DEF_stdVecEx_defInNamespace_eq(div_eq); // /=
  114. SSTD_DEF_stdVecEx_defInNamespace (mod ); // %
  115. SSTD_DEF_stdVecEx_defInNamespace_eq(mod_eq); // %=
  116. SSTD_DEF_stdVecEx_defInNamespace (pow ); // ^
  117. SSTD_DEF_stdVecEx_defInNamespace_eq(pow_eq); // ^=
  118.  
  119. // operators for std::vector
  120. SSTD_DEF_stdVecEx_defInNamespace (push_back ); // <<
  121. SSTD_DEF_stdVecEx_defInNamespace_eq(push_back_eq); // <<=
  122. }
  123.  
  124. #undef SSTD_DEF_stdVecEx_defInNamespace // Deletion of used definition, in order not to pollute the namespace
  125. #undef SSTD_DEF_stdVecEx_defInNamespace_eq // Deletion of used definition, in order not to pollute the namespace
  126.  
  127. //-----------------------------------------------------------------------------------------------------------------------------------------------
  128.  
  129. // operators for mathematics
  130. #define SSTD_DEF_stdVecEx_o(Func, Ope) \
  131.   template <typename T> \
  132.   inline std::vector<T> Func(const std::vector<T>& lhs, const std::vector<T>& rhs){ \
  133.   std::vector<T> ret(lhs.size()); \
  134.   for(uint p=0; p<ret.size(); p++){ ret[p]=lhs[p] Ope rhs[p]; } \
  135.   return ret; \
  136.   } \
  137.   template <typename T, typename rhsType> \
  138.   inline std::vector<T> Func(const std::vector<T>& lhs, const rhsType& rhs){ \
  139.   std::vector<T> ret(lhs.size()); \
  140.   for(uint p=0; p<ret.size(); p++){ ret[p]=lhs[p] Ope rhs; } \
  141.   return ret; \
  142.   } \
  143.   template <typename T, typename rhsType> \
  144.   inline std::vector<T> Func(const rhsType& lhs, const std::vector<T>& rhs){ \
  145.   std::vector<T> ret(rhs.size()); \
  146.   for(uint p=0; p<ret.size(); p++){ ret[p]=lhs Ope rhs[p]; } \
  147.   return ret; \
  148.   }
  149. #define SSTD_DEF_stdVecEx_o_eq(Func, Ope) \
  150.   template <typename T> \
  151.   inline std::vector<T>& Func(std::vector<T>& lhs, const std::vector<T>& rhs){ \
  152.   for(uint p=0; p<lhs.size(); p++){ lhs[p] Ope rhs[p]; } \
  153.   return lhs; \
  154.   } \
  155.   template <typename T, typename rhsType> \
  156.   inline std::vector<T>& Func(std::vector<T>& lhs, const rhsType& rhs){ \
  157.   for(uint p=0; p<lhs.size(); p++){ lhs[p] Ope rhs; } \
  158.   return lhs; \
  159.   }
  160. #define SSTD_DEF_stdVecEx_f(Func, Func2) \
  161.   template <typename T> \
  162.   inline std::vector<T> Func(const std::vector<T>& lhs, const std::vector<T>& rhs){ \
  163.   std::vector<T> ret(lhs.size()); \
  164.   for(uint p=0; p<ret.size(); p++){ ret[p]=Func2(lhs[p], rhs[p]); } \
  165.   return ret; \
  166.   } \
  167.   template <typename T, typename rhsType> \
  168.   inline std::vector<T> Func(const std::vector<T>& lhs, const rhsType& rhs){ \
  169.   std::vector<T> ret(lhs.size()); \
  170.   for(uint p=0; p<ret.size(); p++){ ret[p]=Func2(lhs[p], rhs); } \
  171.   return ret; \
  172.   } \
  173.   template <typename T, typename rhsType> \
  174.   inline std::vector<T> Func(const rhsType& lhs, const std::vector<T>& rhs){ \
  175.   std::vector<T> ret(rhs.size()); \
  176.   for(uint p=0; p<ret.size(); p++){ ret[p]=Func2(lhs, rhs[p]); } \
  177.   return ret; \
  178.   }
  179. #define SSTD_DEF_stdVecEx_f_eq(Func, Func2) \
  180.   template <typename T> \
  181.   inline std::vector<T>& Func(std::vector<T>& lhs, const std::vector<T>& rhs){ \
  182.   for(uint p=0; p<lhs.size(); p++){ lhs[p]=Func2(lhs[p], rhs[p]); } \
  183.   return lhs; \
  184.   } \
  185.   template <typename T, typename rhsType> \
  186.   inline std::vector<T>& Func(std::vector<T>& lhs, const rhsType& rhs){ \
  187.   for(uint p=0; p<lhs.size(); p++){ lhs[p]=Func2(lhs[p], rhs); } \
  188.   return lhs; \
  189.   }
  190. SSTD_DEF_stdVecEx_o (sstd_stdVecEx::add , + );
  191. SSTD_DEF_stdVecEx_o_eq(sstd_stdVecEx::add_eq, +=);
  192. SSTD_DEF_stdVecEx_o (sstd_stdVecEx::sub , - );
  193. SSTD_DEF_stdVecEx_o_eq(sstd_stdVecEx::sub_eq, -=);
  194. SSTD_DEF_stdVecEx_o (sstd_stdVecEx::mul , * );
  195. SSTD_DEF_stdVecEx_o_eq(sstd_stdVecEx::mul_eq, *=);
  196. SSTD_DEF_stdVecEx_o (sstd_stdVecEx::div , / );
  197. SSTD_DEF_stdVecEx_o_eq(sstd_stdVecEx::div_eq, /=);
  198. SSTD_DEF_stdVecEx_o (sstd_stdVecEx::mod , % );
  199. SSTD_DEF_stdVecEx_o_eq(sstd_stdVecEx::mod_eq, %=);
  200. SSTD_DEF_stdVecEx_f (sstd_stdVecEx::pow , sstd::pow); // ^
  201. SSTD_DEF_stdVecEx_f_eq(sstd_stdVecEx::pow_eq, sstd::pow); // ^=
  202. #undef SSTD_DEF_stdVecEx_o // Deletion of used definition, in order not to pollute the namespace
  203. #undef SSTD_DEF_stdVecEx_o_eq // Deletion of used definition, in order not to pollute the namespace
  204. #undef SSTD_DEF_stdVecEx_f // Deletion of used definition, in order not to pollute the namespace
  205. #undef SSTD_DEF_stdVecEx_f_eq // Deletion of used definition, in order not to pollute the namespace
  206.  
  207. //---
  208.  
  209. // operators for std::vector
  210. template <typename T>
  211. inline std::vector<T> sstd_stdVecEx::push_back(const std::vector<T>& lhs, const std::vector<T>& rhs){
  212. std::vector<T> ret(lhs.size()+rhs.size());
  213. uint i=0;
  214. for(uint p=0; p<lhs.size(); p++){ ret[i]=lhs[p]; i++; }
  215. for(uint p=0; p<rhs.size(); p++){ ret[i]=lhs[p]; i++; }
  216. return ret;
  217. }
  218. template <typename T, typename rhsType>
  219. inline std::vector<T> sstd_stdVecEx::push_back(const std::vector<T>& lhs, const rhsType& rhs){
  220. std::vector<T> ret(lhs.size()+1);
  221. for(uint p=0; p<lhs.size(); p++){ ret[p]=lhs[p]; }
  222. ret[lhs.size()]=rhs;
  223. return ret;
  224. }
  225. template <typename T, typename lhsType>
  226. inline std::vector<T> sstd_stdVecEx::push_back(const lhsType& lhs, const std::vector<T>& rhs){
  227. std::vector<T> ret(rhs.size()+1);
  228. ret[0]=lhs;
  229. for(uint p=0; p<rhs.size(); p++){ ret[p+1]=rhs[p]; }
  230. return ret;
  231. }
  232.  
  233. template <typename T>
  234. inline std::vector<T>& sstd_stdVecEx::push_back_eq(std::vector<T>& lhs, const std::vector<T>& rhs){
  235. lhs.insert(lhs.end(), rhs.begin(), rhs.end());
  236. return lhs;
  237. }
  238. template <typename T, typename rhsType>
  239. inline std::vector<T>& sstd_stdVecEx::push_back_eq(std::vector<T>& lhs, const rhsType& rhs){
  240. lhs.push_back(rhs);
  241. return lhs;
  242. }
  243.  
  244. //-----------------------------------------------------------------------------------------------------------------------------------------------
  245.  
  246. #define SSTD_DEF_stdVecEx_Operator(Func, Ope) \
  247.   template <typename T> inline std::vector<T> operator Ope(const std::vector<T>& lhs, const std::vector<T>& rhs){ return Func<T> (lhs, rhs); } \
  248.   template <typename T, typename rhsType> inline std::vector<T> operator Ope(const std::vector<T>& lhs, const rhsType& rhs){ return Func<T, rhsType>(lhs, rhs); } \
  249.   template <typename T, typename lhsType> inline std::vector<T> operator Ope(const lhsType& lhs, const std::vector<T>& rhs){ return Func<T, lhsType>(lhs, rhs); }
  250. #define SSTD_DEF_stdVecEx_Operator_eq(Func, Ope) \
  251.   template <typename T> inline std::vector<T>& operator Ope(std::vector<T>& lhs, const std::vector<T>& rhs){ return Func<T> (lhs, rhs); } \
  252.   template <typename T, typename rhsType> inline std::vector<T>& operator Ope(std::vector<T>& lhs, const rhsType& rhs){ return Func<T, rhsType>(lhs, rhs); }
  253.  
  254. // operators for mathematics
  255. SSTD_DEF_stdVecEx_Operator (sstd_stdVecEx::add , + );
  256. SSTD_DEF_stdVecEx_Operator_eq(sstd_stdVecEx::add_eq, +=);
  257. SSTD_DEF_stdVecEx_Operator (sstd_stdVecEx::sub , - );
  258. SSTD_DEF_stdVecEx_Operator_eq(sstd_stdVecEx::sub_eq, -=);
  259. SSTD_DEF_stdVecEx_Operator (sstd_stdVecEx::mul , * );
  260. SSTD_DEF_stdVecEx_Operator_eq(sstd_stdVecEx::mul_eq, *=);
  261. SSTD_DEF_stdVecEx_Operator (sstd_stdVecEx::div , / );
  262. SSTD_DEF_stdVecEx_Operator_eq(sstd_stdVecEx::div_eq, /=);
  263. SSTD_DEF_stdVecEx_Operator (sstd_stdVecEx::mod , % );
  264. SSTD_DEF_stdVecEx_Operator_eq(sstd_stdVecEx::mod_eq, %=);
  265. SSTD_DEF_stdVecEx_Operator (sstd_stdVecEx::pow , ^ );
  266. SSTD_DEF_stdVecEx_Operator_eq(sstd_stdVecEx::pow_eq, ^=);
  267.  
  268. // operators for std::vector
  269. SSTD_DEF_stdVecEx_Operator (sstd_stdVecEx::push_back , << );
  270. SSTD_DEF_stdVecEx_Operator_eq(sstd_stdVecEx::push_back_eq, <<=);
  271.  
  272. #undef SSTD_DEF_stdVecEx_Operator // Deletion of used definition, in order not to pollute the namespace
  273. #undef SSTD_DEF_stdVecEx_Operator_eq // Deletion of used definition, in order not to pollute the namespace
  274.  
  275. //---
  276.  
  277. template <typename T> inline std::vector<T>& operator++(std::vector<T>& rhs) { for(uint p=0; p<rhs.size(); p++){ rhs[p]++; } return rhs; } // ++rhs
  278. template <typename T> inline std::vector<T>& operator++(std::vector<T>& rhs, int){ for(uint p=0; p<rhs.size(); p++){ rhs[p]++; } return rhs; } // rhs++
  279. template <typename T> inline std::vector<T>& operator--(std::vector<T>& rhs) { for(uint p=0; p<rhs.size(); p++){ rhs[p]--; } return rhs; } // --rhs
  280. template <typename T> inline std::vector<T>& operator--(std::vector<T>& rhs, int){ for(uint p=0; p<rhs.size(); p++){ rhs[p]--; } return rhs; } // rhs--
  281.  
  282. //-----------------------------------------------------------------------------------------------------------------------------------------------
  283. //-----------------------------------------------------------------------------------------------------------------------------------------------
  284. // print.hpp
  285.  
  286. #include <iostream>
  287.  
  288. namespace sstd{
  289. inline void printn_dummy(){}
  290.  
  291. inline void printn(...){}
  292. inline void printn_all(...){}
  293.  
  294. template <typename T>
  295. inline void for_printn(const std::vector<T>& rhs);
  296. }
  297.  
  298. #define printn(var) printn_dummy();{printf("%s", #var);sstd::for_printn(var);}
  299. #define printn_all(var) printn_dummy();{printf("%s(%d): ", __func__, __LINE__);printf("%s", #var);sstd::for_printn(var);}
  300.  
  301. template <typename T>
  302. inline void sstd::for_printn(const std::vector<T>& rhs){
  303. std::cout<<'['<<rhs.size()<<']';
  304. std::cout<<" = [ ";
  305. for(int i=0; i<rhs.size(); i++){ std::cout<<rhs[i]<<' '; }
  306. std::cout<<"]"<<std::endl;
  307. }
  308.  
  309. //-----------------------------------------------------------------------------------------------------------------------------------------------
  310. //-----------------------------------------------------------------------------------------------------------------------------------------------
  311. // main.cpp
  312.  
  313. int main(){
  314. std::vector<double> a={1, 2, 3}, b={4, 5, 6};
  315. sstd::printn(a);
  316. a*=4; // same as "for(int i=0; i<a.size(); i++){ a[i]*=4; }"
  317. sstd::printn(a);
  318. printf("\n");
  319.  
  320. a<<=b; // same as "a.insert(a.end(), b.begin(), b.end());".
  321. sstd::printn(a);
  322. a<<=7.0; // same as "a.push_back(7.0);".
  323. sstd::printn(a);
  324. sstd::printn(a<<8.0); // same as "{ std::vector<double> tmp(a.size()+1); for(uint p=0;p<a.size();p++){tmp[p]=a[p];} tmp[a.size()]=8.0; sstd::printn(tmp); }"
  325.  
  326. return 0;
  327. }
  328.  
Success #stdin #stdout 0s 4404KB
stdin
Standard input is empty
stdout
a[3] = [ 1 2 3 ]
a[3] = [ 4 8 12 ]

a[6] = [ 4 8 12 4 5 6 ]
a[7] = [ 4 8 12 4 5 6 7 ]
a<<8.0[8] = [ 4 8 12 4 5 6 7 8 ]