Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
wjr-z committed Dec 19, 2023
1 parent d456b5d commit cfeae56
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 4 deletions.
6 changes: 3 additions & 3 deletions examples/demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ if (NOT CMAKE_BUILD_TYPE)
endif()

if (MSVC)
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} /O2 /std:c++17 /arch:mavx2")
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} /O2 /std:c++17 /Zc:preprocessor")
else()
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -O2 -std=c++17 -mavx2 -lpthread")
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -O2 -std=c++17")
endif()

set(WJR_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(WJR_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/../../include)
set(WJR_LIB_DIR ${WJR_INCLUDE_DIR}/wjr)

file(GLOB_RECURSE WJR_SRCS ${WJR_LIB_DIR}/*.cpp)
Expand Down
84 changes: 83 additions & 1 deletion examples/demo/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,83 @@
int main() { return 0; }
#include <iostream>
#include <wjr/preprocessor.hpp>
#include <wjr/atomic.hpp>

int main() {
// printf("%s\n", WJR_PP_STR((WJR_PP_QUEUE_CALL_GEN(((1, 2, 3), (4, 5, 6)),
// (WJR_PP_QUEUE_ZIP_2_FN)))));

#define SELF(x) x
#define SELF_FN() SELF

#define TRANS(queue, op) WJR_PP_QUEUE_TRANSFORM(queue, op)
#define TRANS_FN() TRANS

#define TESTX(FUNC) WJR_PP_DEFER(WJR_PP_QUEUE_EXPAND(TRANS((FUNC), SELF_FN)))

// #define TT(x) WJR_PP_EXPAND(x)
// #define TT_FN() TT

// #define WJR_PP_INVOKE_FN() WJR_PP_INVOKE
// #define WJR_PP_DEFER_FN() WJR_PP_DEFER

// #define GGX(x) \
// WJR_PP_DEFER_FN WJR_PP_PAREN(WJR_PP_QUEUE_EXPAND_FN)(TESTX(TRANS_FN)((x), SELF_FN))

// printf("%s\n", WJR_PP_STR(WJR_PP_INVOKE(GGX(wjr))));

printf("%s\n", WJR_PP_STR(WJR_PP_QUEUE_TRANSFORM((1, 2, 3), SELF)));

#define ADD(x, y) x + y
#define ADD_FN() ADD

// printf("!!! : %s\n", WJR_PP_STR(WJR_PP_INVOKE(
// TESTX(WJR_PP_QUEUE_ACCUMULATE_FN)()(0, (1, 2, 3), ADD_FN))));

// printf("%s\n", WJR_PP_STR(WJR_PP_INVOKE(TESTX(WJR_PP_QUEUE_BACK_FN)((1, 2,
// 3))))); printf("%s\n",
// WJR_PP_STR(WJR_PP_INVOKE(TESTX(WJR_PP_QUEUE_UNWRAP_FN)(((1), (2),
// (3))))));

// printf("%s\n", WJR_PP_STR(WJR_PP_INVOKE(WJR_PP_QUEUE_POP_BACK((1, 2, 3)))));
// printf("%s\n", WJR_PP_STR(WJR_PP_INVOKE(TESTX(WJR_PP_QUEUE_POP_BACK_FN)((1, 2,
// 3))))); printf("%s\n", WJR_PP_STR(WJR_PP_INVOKE(WJR_PP_QUEUE_POP_BACK((3, 2,
// 1)))));

// // printf("reverse : %s\n", WJR_PP_STR(WJR_PP_INVOKE(WJR_PP_QUEUE_REVERSE((1,
// 2,
// // 3))))); printf("reverse : %s\n",
// // WJR_PP_STR(WJR_PP_INVOKE(TESTX(WJR_PP_QUEUE_REVERSE_FN)((1, 2,
// 3)))));

// printf("reverse : %s\n",
// WJR_PP_STR(WJR_PP_INVOKE(TESTX(WJR_PP_QUEUE_REVERSE_FN)((1, 2, 3)))));

// #define GG WJR_PP_INVOKE(TESTX(WJR_PP_QUEUE_REVERSE_FN)((1, 2, 3)))

// #define WJR_PP_EXPAND2(x) WJR_PP_EXPAND2_I(WJR_PP_EXPAND2_I(WJR_PP_EXPAND2_I(x)))
// #define WJR_PP_EXPAND2_I(x) x

// printf("reverse : %s\n",
// WJR_PP_STR(WJR_PP_INVOKE(WJR_PP_DEFER(WJR_PP_QUEUE_POP_BACK_FN)(
// TESTX(WJR_PP_QUEUE_REVERSE_FN)((1, 2, 3))))));

// printf("reverse : %s\n",
// WJR_PP_STR(WJR_PP_INVOKE(WJR_PP_DEFER(WJR_PP_QUEUE_POP_BACK_FN)(
// TESTX(WJR_PP_QUEUE_REVERSE_FN)((1, 2, 3))))));

// #define args ((1, 2), (4, 5))
// #define ops (WJR_PP_QUEUE_ZIP_2_FN)

// #define WJR_PP_QUEUE_CALL_GEN_FN() WJR_PP_QUEUE_CALL_GEN

// #define TEST(queue1, queue2) \
// WJR_PP_DEFER(WJR_PP_QUEUE_CALL_GEN_FN)(((queue1), queue2), (TEST_CALLER_FN))
// #define TEST_FN() TEST
// #define TEST_CALLER(x, y) WJR_PP_QUEUE_PUSH_FRONT(x, y)
// #define TEST_CALLER_FN() TEST_CALLER

// printf("%s\n", WJR_PP_STR(WJR_PP_INVOKE(WJR_PP_QUEUE_ZIP((1), (2), (3)))));

// printf("yes\n");
return 0;
}
216 changes: 216 additions & 0 deletions include/wjr/preprocessor/queue/algorithm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
#ifndef WJR_PREPROCESSOR_QUEUE_ALGORITHM_HPP__
#define WJR_PREPROCESSOR_QUEUE_ALGORITHM_HPP__

#include <wjr/preprocessor/arithmatic.hpp>
#include <wjr/preprocessor/details.hpp>
#include <wjr/preprocessor/queue/call.hpp>

#define WJR_PP_QUEUE_INIT_N(x, N) WJR_PP_QUEUE_INIT_N_I(x, N)
#define WJR_PP_QUEUE_INIT_N_I(x, N) (WJR_PP_REPEAT(x, N))

#define WJR_PP_QUEUE_CALL_N_SAME(args, op, N) \
WJR_PP_QUEUE_CALL_N(args, WJR_PP_QUEUE_INIT_N(op, N), N)

#define WJR_PP_QUEUE_CALL_SAME(args, op) \
WJR_PP_QUEUE_CALL_N_SAME(args, op, WJR_PP_QUEUE_SIZE(args))

// (1,2,3), (f, g, h) -> (f()(1), g()(2), h()(3))
#define WJR_PP_QUEUE_TRANSFORMS(queue, ops) \
WJR_PP_DEFER(WJR_PP_QUEUE_POP_FRONT_FN) \
()(WJR_PP_DEFER(WJR_PP_QUEUE_FRONT_FN)()(WJR_PP_QUEUE_CALL_N_SAME( \
WJR_PP_QUEUE_PUSH_FRONT(queue, WJR_PP_QUEUE_PUSH_BACK(ops, 0)), \
WJR_PP_QUEUE_TRANSFORMS_CALLER_FN, WJR_PP_QUEUE_SIZE(queue))))

#define WJR_PP_QUEUE_TRANSFORMS_FN() WJR_PP_QUEUE_TRANSFORMS

#define WJR_PP_QUEUE_TRANSFORMS_CALLER(x, y) \
WJR_PP_QUEUE_PUSH_BACK(WJR_PP_QUEUE_POP_FRONT(x), WJR_PP_QUEUE_FRONT(x)()(y))

#define WJR_PP_QUEUE_TRANSFORMS_CALLER_FN() WJR_PP_QUEUE_TRANSFORMS_CALLER

// (1,2,3), f -> (f()(1), f()(2), f()(3))
#define WJR_PP_QUEUE_TRANSFORM(queue, op) \
WJR_PP_QUEUE_TRANSFORMS(queue, WJR_PP_QUEUE_INIT_N(op, WJR_PP_QUEUE_SIZE(queue)))

#define WJR_PP_QUEUE_TRANSFORM_FN() WJR_PP_QUEUE_TRANSFORM

// 0, (1, 2, 3), (f, g, h) -> h()(g()(f()(0, 1), 2), 3)
#define WJR_PP_QUEUE_ACCUMULATES(init, queue, ops) \
WJR_PP_DEFER(WJR_PP_QUEUE_FRONT_FN) \
()(WJR_PP_DEFER(WJR_PP_QUEUE_FRONT_FN)()(WJR_PP_QUEUE_CALL_N_SAME( \
WJR_PP_QUEUE_PUSH_FRONT( \
queue, WJR_PP_QUEUE_PUSH_BACK(WJR_PP_QUEUE_PUSH_FRONT(ops, init), 0)), \
WJR_PP_QUEUE_ACCUMULATES_CALLER_FN, WJR_PP_QUEUE_SIZE(queue))))

#define WJR_PP_QUEUE_ACCUMULATES_FN() WJR_PP_QUEUE_ACCUMULATES

#define WJR_PP_QUEUE_ACCUMULATES_CALLER(x, y) \
WJR_PP_QUEUE_PUSH_FRONT( \
WJR_PP_QUEUE_POP_FRONT(WJR_PP_QUEUE_POP_FRONT(x)), \
WJR_PP_QUEUE_FRONT(WJR_PP_QUEUE_POP_FRONT(x))()(WJR_PP_QUEUE_FRONT(x), y))

#define WJR_PP_QUEUE_ACCUMULATES_CALLER_FN() WJR_PP_QUEUE_ACCUMULATES_CALLER

// 0, (1, 2, 3), f
#define WJR_PP_QUEUE_ACCUMULATE(init, queue, op) \
WJR_PP_QUEUE_ACCUMULATES(init, queue, \
WJR_PP_QUEUE_INIT_N(op, WJR_PP_QUEUE_SIZE(queue)))

#define WJR_PP_QUEUE_ACCUMULATE_FN() WJR_PP_QUEUE_ACCUMULATE

// (1, 2, 3) -> 3
#define WJR_PP_QUEUE_BACK(queue) \
WJR_PP_QUEUE_ACCUMULATE(0, queue, WJR_PP_QUEUE_BACK_CALLER_FN)

#define WJR_PP_QUEUE_BACK_FN() WJR_PP_QUEUE_BACK

#define WJR_PP_QUEUE_BACK_CALLER(x, y) y
#define WJR_PP_QUEUE_BACK_CALLER_FN() WJR_PP_QUEUE_BACK_CALLER

// (1, 2, 3, 4, 5), 2 -> (3, 4, 5)
#define WJR_PP_QUEUE_POP_FRONT_N(queue, N) \
WJR_PP_QUEUE_CALL_N_SAME(queue, WJR_PP_QUEUE_POP_FRONT_N_CALLER_FN, N)

#define WJR_PP_QUEUE_POP_FRONT_N_FN() WJR_PP_QUEUE_POP_FRONT_N

#define WJR_PP_QUEUE_POP_FRONT_N_CALLER(x, y) y
#define WJR_PP_QUEUE_POP_FRONT_N_CALLER_FN() WJR_PP_QUEUE_POP_FRONT_N_CALLER

// (1, 2, 3, 4, 5), 2 -> (1, 2, 3)
#define WJR_PP_QUEUE_POP_BACK_N(queue, N) \
WJR_PP_DEFER(WJR_PP_QUEUE_POP_FRONT_FN) \
(WJR_PP_DEFER(WJR_PP_QUEUE_FRONT_FN)(WJR_PP_QUEUE_CALL( \
WJR_PP_QUEUE_PUSH_FRONT(queue, (0)), \
WJR_PP_QUEUE_POP_FRONT_N( \
(WJR_PP_REPEAT(WJR_PP_QUEUE_POP_BACK_N_HEADER_CALLER_FN, \
WJR_PP_QUEUE_SIZE(queue)), \
WJR_PP_REPEAT(WJR_PP_QUEUE_POP_BACK_N_TAILER_CALLER_FN, N)), \
N))))

#define WJR_PP_QUEUE_POP_BACK_N_FN() WJR_PP_QUEUE_POP_BACK_N

#define WJR_PP_QUEUE_POP_BACK_N_HEADER_CALLER(x, y) WJR_PP_QUEUE_PUSH_BACK(x, y)
#define WJR_PP_QUEUE_POP_BACK_N_HEADER_CALLER_FN() WJR_PP_QUEUE_POP_BACK_N_HEADER_CALLER
#define WJR_PP_QUEUE_POP_BACK_N_TAILER_CALLER(x, y) x
#define WJR_PP_QUEUE_POP_BACK_N_TAILER_CALLER_FN() WJR_PP_QUEUE_POP_BACK_N_TAILER_CALLER

#define WJR_PP_QUEUE_POP_BACK(queue) WJR_PP_QUEUE_POP_BACK_N(queue, 1)

#define WJR_PP_QUEUE_POP_BACK_FN() WJR_PP_QUEUE_POP_BACK

#define WJR_PP_QUEUE_AT(queue, N) \
WJR_PP_DEFER(WJR_PP_QUEUE_FRONT_FN)(WJR_PP_DEFER(WJR_PP_QUEUE_POP_FRONT_FN)(queue, N))

#define WJR_PP_QUEUE_AT_FN() WJR_PP_QUEUE_AT

#define WJR_PP_QUEUE_REVERSE(queue) \
WJR_PP_QUEUE_POP_BACK(WJR_PP_DEFER(WJR_PP_QUEUE_FRONT_FN)(WJR_PP_QUEUE_CALL_N_SAME( \
WJR_PP_QUEUE_PUSH_FRONT(queue, (0)), WJR_PP_QUEUE_REVERSE_CALLER_FN, \
WJR_PP_QUEUE_SIZE(queue))))

#define WJR_PP_QUEUE_REVERSE_FN() WJR_PP_QUEUE_REVERSE

#define WJR_PP_QUEUE_REVERSE_CALLER(x, y) WJR_PP_QUEUE_PUSH_FRONT(x, y)

#define WJR_PP_QUEUE_REVERSE_CALLER_FN() WJR_PP_QUEUE_REVERSE_CALLER

// (a, b, c) -> a b c
#define WJR_PP_QUEUE_PUT(queue) \
WJR_PP_DEFER(WJR_PP_QUEUE_EXPAND_FN) \
(WJR_PP_QUEUE_CALL_SAME(queue, WJR_PP_QUEUE_PUT_CALLER_FN))

#define WJR_PP_QUEUE_PUT_FN() WJR_PP_QUEUE_PUT

#define WJR_PP_QUEUE_PUT_CALLER(x, y) x y
#define WJR_PP_QUEUE_PUT_CALLER_FN() WJR_PP_QUEUE_PUT_CALLER

// ((A), (B), (C)) -> (A, B, C)
#define WJR_PP_QUEUE_UNWRAP(queue) \
WJR_PP_QUEUE_TRANSFORM(queue, WJR_PP_QUEUE_UNWRAP_CALLER_FN)

#define WJR_PP_QUEUE_UNWRAP_FN() WJR_PP_QUEUE_UNWRAP

#define WJR_PP_QUEUE_UNWRAP_CALLER(x) WJR_PP_QUEUE_UNWRAP_CALLER_I x
#define WJR_PP_QUEUE_UNWRAP_CALLER_I(x) x
#define WJR_PP_QUEUE_UNWRAP_CALLER_FN() WJR_PP_QUEUE_UNWRAP_CALLER

// (A, B, C) (x, y, z) -> ((A, x), (B, y), (C, z))
#define WJR_PP_QUEUE_ZIP_2(queue1, queue2) \
WJR_PP_DEFER(WJR_PP_QUEUE_POP_FRONT_FN) \
(WJR_PP_DEFER(WJR_PP_QUEUE_FRONT_FN)(WJR_PP_QUEUE_CALL_N_SAME( \
((queue1), queue2), WJR_PP_QUEUE_ZIP_2_CALLER_FN, WJR_PP_QUEUE_SIZE(queue1))))

#define WJR_PP_QUEUE_ZIP_2_FN() WJR_PP_QUEUE_ZIP_2

#define WJR_PP_QUEUE_ZIP_2_CALLER(x, y) \
WJR_PP_QUEUE_PUSH_FRONT( \
WJR_PP_QUEUE_POP_FRONT(WJR_PP_QUEUE_PUSH_BACK( \
x, (WJR_PP_QUEUE_FRONT(WJR_PP_QUEUE_FRONT(x)), WJR_PP_QUEUE_FRONT(y)))), \
WJR_PP_QUEUE_POP_FRONT(WJR_PP_QUEUE_FRONT(x))), \
WJR_PP_QUEUE_POP_FRONT(y)

#define WJR_PP_QUEUE_ZIP_2_CALLER_FN() WJR_PP_QUEUE_ZIP_2_CALLER

// ((A), (B), (C)) (x, y, z) -> ((A, x), (B, y), (C, z))
#define WJR_PP_QUEUE_ZIP_MORE(queue1, queue2) \
WJR_PP_DEFER(WJR_PP_QUEUE_POP_FRONT_FN) \
(WJR_PP_DEFER(WJR_PP_QUEUE_FRONT_FN)( \
WJR_PP_QUEUE_CALL_N_SAME(((queue1), queue2), WJR_PP_QUEUE_ZIP_MORE_CALLER_FN, \
WJR_PP_QUEUE_SIZE(queue1))))

#define WJR_PP_QUEUE_ZIP_MORE_CALLER(x, y) \
WJR_PP_QUEUE_PUSH_FRONT( \
WJR_PP_QUEUE_POP_FRONT(WJR_PP_QUEUE_PUSH_BACK( \
x, (WJR_PP_QUEUE_EXPAND(WJR_PP_QUEUE_FRONT(WJR_PP_QUEUE_FRONT(x))), \
WJR_PP_QUEUE_FRONT(y)))), \
WJR_PP_QUEUE_POP_FRONT(WJR_PP_QUEUE_FRONT(x))), \
WJR_PP_QUEUE_POP_FRONT(y)

#define WJR_PP_QUEUE_ZIP_MORE_CALLER_FN() WJR_PP_QUEUE_ZIP_MORE_CALLER

#define WJR_PP_QUEUE_ZIP_MORE_FN() WJR_PP_QUEUE_ZIP_MORE

#define WJR_PP_QUEUE_ZIP(...) \
WJR_PP_QUEUE_ZIP_I(WJR_PP_ARGS_LEN(__VA_ARGS__), __VA_ARGS__)
#define WJR_PP_QUEUE_ZIP_I(N, ...) \
WJR_PP_QUEUE_CALL( \
(__VA_ARGS__), \
WJR_PP_QUEUE_PUSH_FRONT(WJR_PP_QUEUE_INIT_N(WJR_PP_QUEUE_ZIP_MORE_FN, N), \
WJR_PP_QUEUE_ZIP_2_FN))

/*
// (a, b, c) -> (f(a), f(b), f(c))
#define WJR_PP_QUEUE_TRANSFORM(queue, F) \
WJR_PP_QUEUE_POP_FRONT(WJR_PP_QUEUE_FRONT( \
WJR_PP_QUEUE_CALL_SAME(WJR_PP_QUEUE_PUSH_FRONT(queue, WJR_PP_QUEUE_INIT(F)), \
WJR_PP_QUEUE_TRANSFORM_CALLER)))
#define WJR_PP_QUEUE_TRANSFORM_CALLER(x, y) \
WJR_PP_QUEUE_PUSH_BACK(x, WJR_PP_QUEUE_FRONT(x)(y))
#define WJR_PP_QUEUE_TRANSFORM_ARG(queue, F, arg) \
WJR_PP_QUEUE_POP_FRONT( \
WJR_PP_QUEUE_POP_FRONT(WJR_PP_QUEUE_FRONT(WJR_PP_QUEUE_CALL_SAME( \
WJR_PP_QUEUE_PUSH_FRONT(queue, WJR_PP_QUEUE_INIT(F, arg)), \
WJR_PP_QUEUE_TRANSFORM_ARG_CALLER))))
#define WJR_PP_QUEUE_TRANSFORM_ARG_CALLER(x, y) \
WJR_PP_QUEUE_PUSH_BACK( \
x, WJR_PP_QUEUE_FRONT(x)(WJR_PP_QUEUE_FRONT(WJR_PP_QUEUE_POP_FRONT(x)), y))
// a, b, c -> ATTRIBUTE(a) ATTRIBUTE(b) ATTRIBUTE(c)
#define WJR_ATTRIBUTES(...) \
WJR_PP_QUEUE_PUT( \
WJR_PP_QUEUE_TRANSFORM(WJR_PP_QUEUE_INIT(__VA_ARGS__), WJR_ATTRIBUTE))
// typename header0, typename header1, typename header2, ...
#define WJR_TYPENAMES(header, tailer) \
WJR_PP_QUEUE_EXPAND(WJR_PP_QUEUE_TRANSFORM_ARG(tailer, WJR_TYPENAMES_CALLER, header))
#define WJR_TYPENAMES_CALLER(header, x) typename WJR_PP_CONCAT(header, x)
#define WJR_DEFINE_FUNC_IMPL(is_template, template_header, template_types, \
template_fowards, template_backwards, func_ret_type, \
func_name, func_types, func_vars)
#define WJR_DEFINE_FUNC()
*/

#endif // WJR_PREPROCESSOR_QUEUE_ALGORITHM_HPP__
31 changes: 31 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.10)
project(wjr LANGUAGES CXX)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

if (MSVC)
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} /O2 /std:c++17 /arch:mavx2")
else()
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -O2 -std=c++17 -mavx2")
endif()

set(WJR_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/../include)
set(WJR_LIB_DIR ${WJR_INCLUDE_DIR}/wjr)

file(GLOB_RECURSE WJR_SRCS ${WJR_LIB_DIR}/*.cpp)

add_executable(
wjr
src/main.cpp
${WJR_SRCS}
)

target_include_directories(wjr PUBLIC
${WJR_INCLUDE_DIR}
)

target_link_libraries(
wjr
)
2 changes: 2 additions & 0 deletions tests/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include <wjr/preprocessor.hpp>

0 comments on commit cfeae56

Please sign in to comment.