Skip to content

[pull] master from parallel101:master #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stlseries/stl_map/experiment/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "preprocess.h"

#define FUN(x) hello-x

int arr[] = {PP_FOREACH(FUN, PP_COMMA(), 1, 2, 3)};
25 changes: 16 additions & 9 deletions stlseries/stl_map/experiment/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
using namespace std;

int main() {
map<string, int> m = {
{"answer", 42},
{"timeout", 4096},
};
m.insert({
{"timeout", 985},
{"delay", 211},
});
print(m);
map<string, string> msg = {
{"hello", "world"},
{"fuck", "rust"},
};
print(msg);
if (msg.count("fuck")) {
print("存在fuck,其值为", msg.at("fuck"));
} else {
print("找不到fuck");
}
if (msg.count("suck")) {
print("存在suck,其值为", msg.at("suck"));
} else {
print("找不到suck");
}
return 0;
}
62 changes: 62 additions & 0 deletions stlseries/stl_map/experiment/ppforeach.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

// Based on https://www.codenong.com/6707148/
// With some tiny usage tweaks
/*
* The PP_NARG macro evaluates to the number of arguments that have been
* passed to it.
*
* Laurent Deniau,"__VA_NARG__," 17 January 2006, <comp.std.c> (29 November 2007).
*/
#define PP_NARG(...) PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N
#define PP_RSEQ_N() \
63,62,61,60, \
59,58,57,56,55,54,53,52,51,50, \
49,48,47,46,45,44,43,42,41,40, \
39,38,37,36,35,34,33,32,31,30, \
29,28,27,26,25,24,23,22,21,20, \
19,18,17,16,15,14,13,12,11,10, \
9,8,7,6,5,4,3,2,1,0
/* need extra level to force extra eval */
#define PP_PASTEX(a,b) a ## b
#define PP_XPASTE(a,b) PP_PASTEX(a,b)
/* PP_APPLYXn variadic X-Macro by M Joshua Ryan */
/* Free for all uses. Don't be a jerk. */
/* I got bored after typing 15 of these. */
/* You could keep going upto 64 (PPNARG's limit). */
#define PP_APPLYX1(X,_,a) X(a)
#define PP_APPLYX2(X,_,a,b) X(a)_ X(b)
#define PP_APPLYX3(X,_,a,b,c) X(a)_ X(b)_ X(c)
#define PP_APPLYX4(X,_,a,b,c,d) X(a)_ X(b)_ X(c)_ X(d)
#define PP_APPLYX5(X,_,a,b,c,d,e) X(a)_ X(b)_ X(c)_ X(d)_ X(e)
#define PP_APPLYX6(X,_,a,b,c,d,e,f)_ X(a)_ X(b)_ X(c)_ X(d)_ X(e)_ X(f)
#define PP_APPLYX7(X,_,a,b,c,d,e,f,g) \
X(a)_ X(b)_ X(c)_ X(d)_ X(e)_ X(f)_ X(g)
#define PP_APPLYX8(X,_,a,b,c,d,e,f,g,h) \
X(a)_ X(b)_ X(c)_ X(d)_ X(e)_ X(f)_ X(g)_ X(h)
#define PP_APPLYX9(X,_,a,b,c,d,e,f,g,h,i) \
X(a)_ X(b)_ X(c)_ X(d)_ X(e)_ X(f)_ X(g)_ X(h)_ X(i)
#define PP_APPLYX10(X,_,a,b,c,d,e,f,g,h,i,j) \
X(a)_ X(b)_ X(c)_ X(d)_ X(e)_ X(f)_ X(g)_ X(h)_ X(i)_ X(j)
#define PP_APPLYX11(X,_,a,b,c,d,e,f,g,h,i,j,k) \
X(a)_ X(b)_ X(c)_ X(d)_ X(e)_ X(f)_ X(g)_ X(h)_ X(i)_ X(j)_ X(k)
#define PP_APPLYX12(X,_,a,b,c,d,e,f,g,h,i,j,k,l) \
X(a)_ X(b)_ X(c)_ X(d)_ X(e)_ X(f)_ X(g)_ X(h)_ X(i)_ X(j)_ X(k)_ X(l)
#define PP_APPLYX13(X,_,a,b,c,d,e,f,g,h,i,j,k,l,m) \
X(a)_ X(b)_ X(c)_ X(d)_ X(e)_ X(f)_ X(g)_ X(h)_ X(i)_ X(j)_ X(k)_ X(l)_ X(m)
#define PP_APPLYX14(X,_,a,b,c,d,e,f,g,h,i,j,k,l,m,n) \
X(a)_ X(b)_ X(c)_ X(d)_ X(e)_ X(f)_ X(g)_ X(h)_ X(i)_ X(j)_ X(k)_ X(l)_ X(m)_ X(n)
#define PP_APPLYX15(X,_,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) \
X(a)_ X(b)_ X(c)_ X(d)_ X(e)_ X(f)_ X(g)_ X(h)_ X(i)_ X(j)_ X(k)_ X(l)_ X(m)_ X(n)_ X(o)
#define PP_APPLYX_(X,_,M, ...) M(X,_,__VA_ARGS__)
#define PP_APPLYXn(X,_,...) PP_APPLYX_(X,_,PP_XPASTE(PP_APPLYX, PP_NARG(__VA_ARGS__)), __VA_ARGS__)
#define PP_FOREACH(func, join, ...) PP_APPLYXn(func, join, __VA_ARGS__)
39 changes: 36 additions & 3 deletions stlseries/stl_map/experiment/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,18 @@ namespace _print_details {
using type = U;
};

template <class T, class U = void, class = void>
struct _enable_if_c_str {
using not_type = U;
};

template <class T, class U>
struct _enable_if_c_str<T, U, std::enable_if_t<std::is_pointer_v<std::decay_t<T>> && std::is_same_v<std::remove_const_t<std::remove_pointer_t<std::decay_t<T>>>, char>>> {
using type = U;
};

template <class T, class U>
struct _enable_if_string<T, U, std::enable_if_t<std::is_pointer_v<std::decay_t<T>> && std::is_same_v<std::remove_const_t<std::remove_pointer_t<std::decay_t<T>>>, char>>> {
struct _enable_if_c_str<T, U, std::enable_if_t<std::is_pointer_v<std::decay_t<T>> && std::is_same_v<std::remove_const_t<std::remove_pointer_t<std::decay_t<T>>>, wchar_t>>> {
using type = U;
};

Expand All @@ -110,7 +120,7 @@ namespace _print_details {
};

template <class T>
struct _printer<T, typename _enable_if_iterable<T, typename _enable_if_string<T, typename _enable_if_tuple<T, typename _enable_if_map<T>::not_type>::not_type>::not_type>::type> {
struct _printer<T, typename _enable_if_iterable<T, typename _enable_if_c_str<T, typename _enable_if_string<T, typename _enable_if_map<T>::not_type>::not_type>::not_type>::type> {
static void print(T const &t) {
std::cout << "{";
bool once = false;
Expand Down Expand Up @@ -167,6 +177,13 @@ namespace _print_details {
}
};

template <class T>
struct _printer<T, typename _enable_if_c_str<T>::type> {
static void print(T const &t) {
std::cout << t;
}
};

template <class T>
struct _printer<T, typename _enable_if_char<T>::type> {
static void print(T const &t) {
Expand Down Expand Up @@ -218,7 +235,7 @@ namespace _print_details {
template <class T0, class ...Ts>
void print(T0 const &t0, Ts const &...ts) {
_printer<_rmcvref_t<T0>>::print(t0);
((std::cout << " " << _printer<_rmcvref_t<Ts>>::print(ts)), ...);
((std::cout << " ", _printer<_rmcvref_t<Ts>>::print(ts)), ...);
std::cout << "\n";
}
}
Expand All @@ -229,3 +246,19 @@ using _print_details::print;
//
// map<string, optional<int>> m = {{"hello", 42}, {"world", nullopt}};
// print(m); // {"hello": 42, "world": nullopt}

#include "ppforeach.h"

#define DEF_PRINT(Class, TmplArgs, ...) \
template <TmplArgs> \
struct _print_details::_printer<Class, void> { \
static void print(Class const &_cls) { \
std::cout << "{"; \
PP_FOREACH(_PRINTER_PER_MEMBER, std::cout << ", ";, __VA_ARGS__); \
std::cout << "}"; \
} \
};

#define _PRINTER_PER_MEMBER(memb) \
std::cout << #memb << ": "; \
_print_details::_printer<_print_details::_rmcvref_t<decltype(_cls.memb)>>::print(_cls.memb);
Binary file added stlseries/stl_map/images/maijiaxiu.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading