1 # /* **************************************************************************
2 #  *                                                                          *
3 #  *     (C) Copyright Paul Mensonides 2002.
4 #  *     Distributed under the Boost Software License, Version 1.0. (See
5 #  *     accompanying file LICENSE_1_0.txt or copy at
6 #  *     http://www.boost.org/LICENSE_1_0.txt)
7 #  *                                                                          *
8 #  ************************************************************************** */
9 #
10 # /* Revised by Edward Diener (2020) */
11 #
12 # /* See http://www.boost.org for most recent version. */
13 #
14 # ifndef BOOST_PREPROCESSOR_ARRAY_INSERT_HPP
15 # define BOOST_PREPROCESSOR_ARRAY_INSERT_HPP
16 #
17 # include <boost/preprocessor/config/config.hpp>
18 #
19 # if ~BOOST_PP_CONFIG_FLAGS() & BOOST_PP_CONFIG_STRICT()
20 #
21 # include <boost/preprocessor/arithmetic/inc.hpp>
22 # include <boost/preprocessor/array/elem.hpp>
23 # include <boost/preprocessor/array/push_back.hpp>
24 # include <boost/preprocessor/array/size.hpp>
25 # include <boost/preprocessor/comparison/not_equal.hpp>
26 # include <boost/preprocessor/control/deduce_d.hpp>
27 # include <boost/preprocessor/control/iif.hpp>
28 # include <boost/preprocessor/control/while.hpp>
29 # include <boost/preprocessor/tuple/elem.hpp>
30 #
31 # /* BOOST_PP_ARRAY_INSERT */
32 #
33 # define BOOST_PP_ARRAY_INSERT(array, i, elem) BOOST_PP_ARRAY_INSERT_I(BOOST_PP_DEDUCE_D(), array, i, elem)
34 # define BOOST_PP_ARRAY_INSERT_I(d, array, i, elem) BOOST_PP_ARRAY_INSERT_D(d, array, i, elem)
35 #
36 # /* BOOST_PP_ARRAY_INSERT_D */
37 #
38 # if ~BOOST_PP_CONFIG_FLAGS() & BOOST_PP_CONFIG_EDG()
39 #    define BOOST_PP_ARRAY_INSERT_D(d, array, i, elem) BOOST_PP_TUPLE_ELEM(5, 3, BOOST_PP_WHILE_ ## d(BOOST_PP_ARRAY_INSERT_P, BOOST_PP_ARRAY_INSERT_O, (0, i, elem, (0, ()), array)))
40 # else
41 #    define BOOST_PP_ARRAY_INSERT_D(d, array, i, elem) BOOST_PP_ARRAY_INSERT_D_I(d, array, i, elem)
42 #    define BOOST_PP_ARRAY_INSERT_D_I(d, array, i, elem) BOOST_PP_TUPLE_ELEM(5, 3, BOOST_PP_WHILE_ ## d(BOOST_PP_ARRAY_INSERT_P, BOOST_PP_ARRAY_INSERT_O, (0, i, elem, (0, ()), array)))
43 # endif
44 #
45 # if BOOST_PP_CONFIG_FLAGS() & BOOST_PP_CONFIG_STRICT()
46 #    define BOOST_PP_ARRAY_INSERT_P(d, state) BOOST_PP_ARRAY_INSERT_P_I state
47 # else
48 #    define BOOST_PP_ARRAY_INSERT_P(d, state) BOOST_PP_ARRAY_INSERT_P_I(nil, nil, nil, BOOST_PP_TUPLE_ELEM(5, 3, state), BOOST_PP_TUPLE_ELEM(5, 4, state))
49 # endif
50 #
51 # define BOOST_PP_ARRAY_INSERT_P_I(_i, _ii, _iii, res, arr) BOOST_PP_NOT_EQUAL(BOOST_PP_ARRAY_SIZE(res), BOOST_PP_INC(BOOST_PP_ARRAY_SIZE(arr)))
52 #
53 # if BOOST_PP_CONFIG_FLAGS() & BOOST_PP_CONFIG_STRICT()
54 #    define BOOST_PP_ARRAY_INSERT_O(d, state) BOOST_PP_ARRAY_INSERT_O_I state
55 # else
56 #    define BOOST_PP_ARRAY_INSERT_O(d, state) BOOST_PP_ARRAY_INSERT_O_I(BOOST_PP_TUPLE_ELEM(5, 0, state), BOOST_PP_TUPLE_ELEM(5, 1, state), BOOST_PP_TUPLE_ELEM(5, 2, state), BOOST_PP_TUPLE_ELEM(5, 3, state), BOOST_PP_TUPLE_ELEM(5, 4, state))
57 # endif
58 #
59 # define BOOST_PP_ARRAY_INSERT_O_I(n, i, elem, res, arr) (BOOST_PP_IIF(BOOST_PP_NOT_EQUAL(BOOST_PP_ARRAY_SIZE(res), i), BOOST_PP_INC(n), n), i, elem, BOOST_PP_ARRAY_PUSH_BACK(res, BOOST_PP_IIF(BOOST_PP_NOT_EQUAL(BOOST_PP_ARRAY_SIZE(res), i), BOOST_PP_ARRAY_ELEM(n, arr), elem)), arr)
60 #
61 # else
62 #
63 # include <boost/preprocessor/arithmetic/inc.hpp>
64 # include <boost/preprocessor/array/elem.hpp>
65 # include <boost/preprocessor/array/push_back.hpp>
66 # include <boost/preprocessor/array/size.hpp>
67 # include <boost/preprocessor/comparison/not_equal.hpp>
68 # include <boost/preprocessor/config/limits.hpp>
69 # include <boost/preprocessor/control/deduce_d.hpp>
70 # include <boost/preprocessor/control/iif.hpp>
71 # include <boost/preprocessor/control/while.hpp>
72 # if BOOST_PP_LIMIT_TUPLE == 256
73 # include <boost/preprocessor/facilities/identity.hpp>
74 # include <boost/preprocessor/logical/not.hpp>
75 # endif
76 # include <boost/preprocessor/tuple/elem.hpp>
77 #
78 # /* BOOST_PP_ARRAY_INSERT */
79 #
80 # define BOOST_PP_ARRAY_INSERT(array, i, elem) BOOST_PP_ARRAY_INSERT_I(BOOST_PP_DEDUCE_D(), array, i, elem)
81 # define BOOST_PP_ARRAY_INSERT_I(d, array, i, elem) BOOST_PP_ARRAY_INSERT_D(d, array, i, elem)
82 #
83 # /* BOOST_PP_ARRAY_INSERT_D */
84 #
85 # if ~BOOST_PP_CONFIG_FLAGS() & BOOST_PP_CONFIG_EDG()
86 # if BOOST_PP_LIMIT_TUPLE == 256
87 #    define BOOST_PP_ARRAY_INSERT_D(d, array, i, elem) \
88             BOOST_PP_IIF(BOOST_PP_NOT(BOOST_PP_ARRAY_SIZE(array)),BOOST_PP_IDENTITY_N((1,(elem)),5),BOOST_PP_ARRAY_INSERT_ZERO_D)(d, array, i, elem, BOOST_PP_NOT(i))
89 # else
90 #    define BOOST_PP_ARRAY_INSERT_D(d, array, i, elem) BOOST_PP_TUPLE_ELEM(5, 3, BOOST_PP_WHILE_ ## d(BOOST_PP_ARRAY_INSERT_P, BOOST_PP_ARRAY_INSERT_O, (0, i, elem, (0, ()), array)))
91 # endif
92 # else
93 #    define BOOST_PP_ARRAY_INSERT_D(d, array, i, elem) BOOST_PP_ARRAY_INSERT_D_I(d, array, i, elem)
94 # if BOOST_PP_LIMIT_TUPLE == 256
95 #    define BOOST_PP_ARRAY_INSERT_D_I(d, array, i, elem) \
96             BOOST_PP_IIF(BOOST_PP_NOT(BOOST_PP_ARRAY_SIZE(array)),BOOST_PP_IDENTITY_N((1,(elem)),5),BOOST_PP_ARRAY_INSERT_ZERO_D)(d, array, i, elem, BOOST_PP_NOT(i))
97 # else
98 #    define BOOST_PP_ARRAY_INSERT_D_I(d, array, i, elem) BOOST_PP_TUPLE_ELEM(5, 3, BOOST_PP_WHILE_ ## d(BOOST_PP_ARRAY_INSERT_P, BOOST_PP_ARRAY_INSERT_O, (0, i, elem, (0, ()), array)))
99 # endif
100 # endif
101 #
102 # if BOOST_PP_LIMIT_TUPLE == 256
103 # define BOOST_PP_ARRAY_INSERT_ZERO_D(d, array, i, elem, zero) \
104          BOOST_PP_TUPLE_ELEM(5, 3, BOOST_PP_WHILE_ ## d(BOOST_PP_ARRAY_INSERT_P, BOOST_PP_ARRAY_INSERT_O, \
105          (1, i, elem, BOOST_PP_IIF( zero, ( 2 , ( elem , BOOST_PP_ARRAY_ELEM(0,array) ) ) , ( 1 , ( BOOST_PP_ARRAY_ELEM(0,array) ) ) ), array)))
106 # endif
107 #
108 # if BOOST_PP_CONFIG_FLAGS() & BOOST_PP_CONFIG_STRICT()
109 #    define BOOST_PP_ARRAY_INSERT_P(d, state) BOOST_PP_ARRAY_INSERT_P_I state
110 # else
111 #    define BOOST_PP_ARRAY_INSERT_P(d, state) BOOST_PP_ARRAY_INSERT_P_I(nil, nil, nil, BOOST_PP_TUPLE_ELEM(5, 3, state), BOOST_PP_TUPLE_ELEM(5, 4, state))
112 # endif
113 #
114 # define BOOST_PP_ARRAY_INSERT_P_I(_i, _ii, _iii, res, arr) BOOST_PP_NOT_EQUAL(BOOST_PP_ARRAY_SIZE(res), BOOST_PP_INC(BOOST_PP_ARRAY_SIZE(arr)))
115 #
116 # if BOOST_PP_CONFIG_FLAGS() & BOOST_PP_CONFIG_STRICT()
117 #    define BOOST_PP_ARRAY_INSERT_O(d, state) BOOST_PP_ARRAY_INSERT_O_I state
118 # else
119 #    define BOOST_PP_ARRAY_INSERT_O(d, state) BOOST_PP_ARRAY_INSERT_O_I(BOOST_PP_TUPLE_ELEM(5, 0, state), BOOST_PP_TUPLE_ELEM(5, 1, state), BOOST_PP_TUPLE_ELEM(5, 2, state), BOOST_PP_TUPLE_ELEM(5, 3, state), BOOST_PP_TUPLE_ELEM(5, 4, state))
120 # endif
121 #
122 # define BOOST_PP_ARRAY_INSERT_O_I(n, i, elem, res, arr) (BOOST_PP_IIF(BOOST_PP_NOT_EQUAL(BOOST_PP_ARRAY_SIZE(res), i), BOOST_PP_INC(n), n), i, elem, BOOST_PP_ARRAY_PUSH_BACK(res, BOOST_PP_IIF(BOOST_PP_NOT_EQUAL(BOOST_PP_ARRAY_SIZE(res), i), BOOST_PP_ARRAY_ELEM(n, arr), elem)), arr)
123 #
124 # endif
125 #
126 # endif
127