Blame lib/m4sugar/foreach.m4

Packit 47b4ca
#                                                  -*- Autoconf -*-
Packit 47b4ca
# This file is part of Autoconf.
Packit 47b4ca
# foreach-based replacements for recursive functions.
Packit 47b4ca
# Speeds up GNU M4 1.4.x by avoiding quadratic $@ recursion, but penalizes
Packit 47b4ca
# GNU M4 1.6 by requiring more memory and macro expansions.
Packit 47b4ca
#
Packit 47b4ca
# Copyright (C) 2008-2012 Free Software Foundation, Inc.
Packit 47b4ca
Packit 47b4ca
# This file is part of Autoconf.  This program is free
Packit 47b4ca
# software; you can redistribute it and/or modify it under the
Packit 47b4ca
# terms of the GNU General Public License as published by the
Packit 47b4ca
# Free Software Foundation, either version 3 of the License, or
Packit 47b4ca
# (at your option) any later version.
Packit 47b4ca
#
Packit 47b4ca
# This program is distributed in the hope that it will be useful,
Packit 47b4ca
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 47b4ca
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 47b4ca
# GNU General Public License for more details.
Packit 47b4ca
#
Packit 47b4ca
# Under Section 7 of GPL version 3, you are granted additional
Packit 47b4ca
# permissions described in the Autoconf Configure Script Exception,
Packit 47b4ca
# version 3.0, as published by the Free Software Foundation.
Packit 47b4ca
#
Packit 47b4ca
# You should have received a copy of the GNU General Public License
Packit 47b4ca
# and a copy of the Autoconf Configure Script Exception along with
Packit 47b4ca
# this program; see the files COPYINGv3 and COPYING.EXCEPTION
Packit 47b4ca
# respectively.  If not, see <http://www.gnu.org/licenses/>.
Packit 47b4ca
Packit 47b4ca
# Written by Eric Blake.
Packit 47b4ca
Packit 47b4ca
# In M4 1.4.x, every byte of $@ is rescanned.  This means that an
Packit 47b4ca
# algorithm on n arguments that recurses with one less argument each
Packit 47b4ca
# iteration will scan n * (n + 1) / 2 arguments, for O(n^2) time.  In
Packit 47b4ca
# M4 1.6, this was fixed so that $@ is only scanned once, then
Packit 47b4ca
# back-references are made to information stored about the scan.
Packit 47b4ca
# Thus, n iterations need only scan n arguments, for O(n) time.
Packit 47b4ca
# Additionally, in M4 1.4.x, recursive algorithms did not clean up
Packit 47b4ca
# memory very well, requiring O(n^2) memory rather than O(n) for n
Packit 47b4ca
# iterations.
Packit 47b4ca
#
Packit 47b4ca
# This file is designed to overcome the quadratic nature of $@
Packit 47b4ca
# recursion by writing a variant of m4_foreach that uses m4_for rather
Packit 47b4ca
# than $@ recursion to operate on the list.  This involves more macro
Packit 47b4ca
# expansions, but avoids the need to rescan a quadratic number of
Packit 47b4ca
# arguments, making these replacements very attractive for M4 1.4.x.
Packit 47b4ca
# On the other hand, in any version of M4, expanding additional macros
Packit 47b4ca
# costs additional time; therefore, in M4 1.6, where $@ recursion uses
Packit 47b4ca
# fewer macros, these replacements actually pessimize performance.
Packit 47b4ca
# Additionally, the use of $10 to mean the tenth argument violates
Packit 47b4ca
# POSIX; although all versions of m4 1.4.x support this meaning, a
Packit 47b4ca
# future m4 version may switch to take it as the first argument
Packit 47b4ca
# concatenated with a literal 0, so the implementations in this file
Packit 47b4ca
# are not future-proof.  Thus, this file is conditionally included as
Packit 47b4ca
# part of m4_init(), only when it is detected that M4 probably has
Packit 47b4ca
# quadratic behavior (ie. it lacks the macro __m4_version__).
Packit 47b4ca
#
Packit 47b4ca
# Please keep this file in sync with m4sugar.m4.
Packit 47b4ca
Packit 47b4ca
# _m4_foreach(PRE, POST, IGNORED, ARG...)
Packit 47b4ca
# ---------------------------------------
Packit 47b4ca
# Form the common basis of the m4_foreach and m4_map macros.  For each
Packit 47b4ca
# ARG, expand PRE[ARG]POST[].  The IGNORED argument makes recursion
Packit 47b4ca
# easier, and must be supplied rather than implicit.
Packit 47b4ca
#
Packit 47b4ca
# This version minimizes the number of times that $@ is evaluated by
Packit 47b4ca
# using m4_for to generate a boilerplate into _m4_f then passing $@ to
Packit 47b4ca
# that temporary macro.  Thus, the recursion is done in m4_for without
Packit 47b4ca
# reparsing any user input, and is not quadratic.  For an idea of how
Packit 47b4ca
# this works, note that m4_foreach(i,[1,2],[i]) calls
Packit 47b4ca
#   _m4_foreach([m4_define([i],],[)i],[],[1],[2])
Packit 47b4ca
# which defines _m4_f:
Packit 47b4ca
#   $1[$4]$2[]$1[$5]$2[]_m4_popdef([_m4_f])
Packit 47b4ca
# then calls _m4_f([m4_define([i],],[)i],[],[1],[2]) for a net result:
Packit 47b4ca
#   m4_define([i],[1])i[]m4_define([i],[2])i[]_m4_popdef([_m4_f]).
Packit 47b4ca
m4_define([_m4_foreach],
Packit 47b4ca
[m4_if([$#], [3], [],
Packit 47b4ca
       [m4_pushdef([_m4_f], _m4_for([4], [$#], [1],
Packit 47b4ca
   [$0_([1], [2],], [)])[_m4_popdef([_m4_f])])_m4_f($@)])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_foreach_],
Packit 47b4ca
[[$$1[$$3]$$2[]]])
Packit 47b4ca
Packit 47b4ca
# m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
Packit 47b4ca
# -----------------------------------------------------------
Packit 47b4ca
# Find the first VAL that SWITCH matches, and expand the corresponding
Packit 47b4ca
# IF-VAL.  If there are no matches, expand DEFAULT.
Packit 47b4ca
#
Packit 47b4ca
# Use m4_for to create a temporary macro in terms of a boilerplate
Packit 47b4ca
# m4_if with final cleanup.  If $# is even, we have DEFAULT; if it is
Packit 47b4ca
# odd, then rounding the last $# up in the temporary macro is
Packit 47b4ca
# harmless.  For example, both m4_case(1,2,3,4,5) and
Packit 47b4ca
# m4_case(1,2,3,4,5,6) result in the intermediate _m4_case being
Packit 47b4ca
#   m4_if([$1],[$2],[$3],[$1],[$4],[$5],_m4_popdef([_m4_case])[$6])
Packit 47b4ca
m4_define([m4_case],
Packit 47b4ca
[m4_if(m4_eval([$# <= 2]), [1], [$2],
Packit 47b4ca
[m4_pushdef([_$0], [m4_if(]_m4_for([2], m4_eval([($# - 1) / 2 * 2]), [2],
Packit 47b4ca
     [_$0_(], [)])[_m4_popdef(
Packit 47b4ca
	 [_$0])]m4_dquote($m4_eval([($# + 1) & ~1]))[)])_$0($@)])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_case_],
Packit 47b4ca
[$0_([1], [$1], m4_incr([$1]))])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_case__],
Packit 47b4ca
[[[$$1],[$$2],[$$3],]])
Packit 47b4ca
Packit 47b4ca
# m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
Packit 47b4ca
# -----------------------------------------------------
Packit 47b4ca
# m4 equivalent of
Packit 47b4ca
#
Packit 47b4ca
# if (SWITCH =~ RE1)
Packit 47b4ca
#   VAL1;
Packit 47b4ca
# elif (SWITCH =~ RE2)
Packit 47b4ca
#   VAL2;
Packit 47b4ca
# elif ...
Packit 47b4ca
#   ...
Packit 47b4ca
# else
Packit 47b4ca
#   DEFAULT
Packit 47b4ca
#
Packit 47b4ca
# We build the temporary macro _m4_b:
Packit 47b4ca
#   m4_define([_m4_b], _m4_defn([_m4_bmatch]))_m4_b([$1], [$2], [$3])...
Packit 47b4ca
#   _m4_b([$1], [$m-1], [$m])_m4_b([], [], [$m+1]_m4_popdef([_m4_b]))
Packit 47b4ca
# then invoke m4_unquote(_m4_b($@)), for concatenation with later text.
Packit 47b4ca
m4_define([m4_bmatch],
Packit 47b4ca
[m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
Packit 47b4ca
       [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
Packit 47b4ca
       [$#], 2, [$2],
Packit 47b4ca
       [m4_pushdef([_m4_b], [m4_define([_m4_b],
Packit 47b4ca
  _m4_defn([_$0]))]_m4_for([3], m4_eval([($# + 1) / 2 * 2 - 1]),
Packit 47b4ca
  [2], [_$0_(], [)])[_m4_b([], [],]m4_dquote([$]m4_eval(
Packit 47b4ca
  [($# + 1) / 2 * 2]))[_m4_popdef([_m4_b]))])m4_unquote(_m4_b($@))])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_bmatch],
Packit 47b4ca
[m4_if(m4_bregexp([$1], [$2]), [-1], [], [[$3]m4_define([$0])])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_bmatch_],
Packit 47b4ca
[$0_([1], m4_decr([$1]), [$1])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_bmatch__],
Packit 47b4ca
[[_m4_b([$$1], [$$2], [$$3])]])
Packit 47b4ca
Packit 47b4ca
Packit 47b4ca
# m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT])
Packit 47b4ca
# -------------------------------------------------------------------
Packit 47b4ca
# Similar to m4_if, except that each TEST is expanded when encountered.
Packit 47b4ca
# If the expansion of TESTn matches the string VALn, the result is IF-VALn.
Packit 47b4ca
# The result is DEFAULT if no tests passed.  This macro allows
Packit 47b4ca
# short-circuiting of expensive tests, where it pays to arrange quick
Packit 47b4ca
# filter tests to run first.
Packit 47b4ca
#
Packit 47b4ca
# m4_cond already guarantees either 3*n or 3*n + 1 arguments, 1 <= n.
Packit 47b4ca
# We only have to speed up _m4_cond, by building the temporary _m4_c:
Packit 47b4ca
#   m4_define([_m4_c], _m4_defn([m4_unquote]))_m4_c([m4_if(($1), [($2)],
Packit 47b4ca
#   [[$3]m4_define([_m4_c])])])_m4_c([m4_if(($4), [($5)],
Packit 47b4ca
#   [[$6]m4_define([_m4_c])])])..._m4_c([m4_if(($m-2), [($m-1)],
Packit 47b4ca
#   [[$m]m4_define([_m4_c])])])_m4_c([[$m+1]]_m4_popdef([_m4_c]))
Packit 47b4ca
# We invoke m4_unquote(_m4_c($@)), for concatenation with later text.
Packit 47b4ca
m4_define([_m4_cond],
Packit 47b4ca
[m4_pushdef([_m4_c], [m4_define([_m4_c],
Packit 47b4ca
  _m4_defn([m4_unquote]))]_m4_for([2], m4_eval([$# / 3 * 3 - 1]), [3],
Packit 47b4ca
  [$0_(], [)])[_m4_c(]m4_dquote(m4_dquote(
Packit 47b4ca
  [$]m4_eval([$# / 3 * 3 + 1])))[_m4_popdef([_m4_c]))])m4_unquote(_m4_c($@))])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_cond_],
Packit 47b4ca
[$0_(m4_decr([$1]), [$1], m4_incr([$1]))])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_cond__],
Packit 47b4ca
[[_m4_c([m4_if(($$1), [($$2)], [[$$3]m4_define([_m4_c])])])]])
Packit 47b4ca
Packit 47b4ca
# m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
Packit 47b4ca
# ----------------------------------------------------
Packit 47b4ca
# m4 equivalent of
Packit 47b4ca
#
Packit 47b4ca
#   $_ = STRING;
Packit 47b4ca
#   s/RE1/SUBST1/g;
Packit 47b4ca
#   s/RE2/SUBST2/g;
Packit 47b4ca
#   ...
Packit 47b4ca
#
Packit 47b4ca
# m4_bpatsubsts already validated an odd number of arguments; we only
Packit 47b4ca
# need to speed up _m4_bpatsubsts.  To avoid nesting, we build the
Packit 47b4ca
# temporary _m4_p:
Packit 47b4ca
#   m4_define([_m4_p], [$1])m4_define([_m4_p],
Packit 47b4ca
#   m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$2], [$3]))m4_define([_m4_p],
Packit 47b4ca
#   m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$4], [$5]))m4_define([_m4_p],...
Packit 47b4ca
#   m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$m-1], [$m]))m4_unquote(
Packit 47b4ca
#   _m4_defn([_m4_p])_m4_popdef([_m4_p]))
Packit 47b4ca
m4_define([_m4_bpatsubsts],
Packit 47b4ca
[m4_pushdef([_m4_p], [m4_define([_m4_p],
Packit 47b4ca
  ]m4_dquote([$]1)[)]_m4_for([3], [$#], [2], [$0_(],
Packit 47b4ca
  [)])[m4_unquote(_m4_defn([_m4_p])_m4_popdef([_m4_p]))])_m4_p($@)])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_bpatsubsts_],
Packit 47b4ca
[$0_(m4_decr([$1]), [$1])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_bpatsubsts__],
Packit 47b4ca
[[m4_define([_m4_p],
Packit 47b4ca
m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$$1], [$$2]))]])
Packit 47b4ca
Packit 47b4ca
# m4_shiftn(N, ...)
Packit 47b4ca
# -----------------
Packit 47b4ca
# Returns ... shifted N times.  Useful for recursive "varargs" constructs.
Packit 47b4ca
#
Packit 47b4ca
# m4_shiftn already validated arguments; we only need to speed up
Packit 47b4ca
# _m4_shiftn.  If N is 3, then we build the temporary _m4_s, defined as
Packit 47b4ca
#   ,[$5],[$6],...,[$m]_m4_popdef([_m4_s])
Packit 47b4ca
# before calling m4_shift(_m4_s($@)).
Packit 47b4ca
m4_define([_m4_shiftn],
Packit 47b4ca
[m4_if(m4_incr([$1]), [$#], [], [m4_pushdef([_m4_s],
Packit 47b4ca
  _m4_for(m4_eval([$1 + 2]), [$#], [1],
Packit 47b4ca
  [[,]m4_dquote($], [)])[_m4_popdef([_m4_s])])m4_shift(_m4_s($@))])])
Packit 47b4ca
Packit 47b4ca
# m4_do(STRING, ...)
Packit 47b4ca
# ------------------
Packit 47b4ca
# This macro invokes all its arguments (in sequence, of course).  It is
Packit 47b4ca
# useful for making your macros more structured and readable by dropping
Packit 47b4ca
# unnecessary dnl's and have the macros indented properly.
Packit 47b4ca
#
Packit 47b4ca
# Here, we use the temporary macro _m4_do, defined as
Packit 47b4ca
#   $1[]$2[]...[]$n[]_m4_popdef([_m4_do])
Packit 47b4ca
m4_define([m4_do],
Packit 47b4ca
[m4_if([$#], [0], [],
Packit 47b4ca
       [m4_pushdef([_$0], _m4_for([1], [$#], [1],
Packit 47b4ca
		   [$], [[[]]])[_m4_popdef([_$0])])_$0($@)])])
Packit 47b4ca
Packit 47b4ca
# m4_dquote_elt(ARGS)
Packit 47b4ca
# -------------------
Packit 47b4ca
# Return ARGS as an unquoted list of double-quoted arguments.
Packit 47b4ca
#
Packit 47b4ca
# _m4_foreach to the rescue.
Packit 47b4ca
m4_define([m4_dquote_elt],
Packit 47b4ca
[m4_if([$#], [0], [], [[[$1]]_m4_foreach([,m4_dquote(], [)], $@)])])
Packit 47b4ca
Packit 47b4ca
# m4_reverse(ARGS)
Packit 47b4ca
# ----------------
Packit 47b4ca
# Output ARGS in reverse order.
Packit 47b4ca
#
Packit 47b4ca
# Invoke _m4_r($@) with the temporary _m4_r built as
Packit 47b4ca
#   [$m], [$m-1], ..., [$2], [$1]_m4_popdef([_m4_r])
Packit 47b4ca
m4_define([m4_reverse],
Packit 47b4ca
[m4_if([$#], [0], [], [$#], [1], [[$1]],
Packit 47b4ca
[m4_pushdef([_m4_r], [[$$#]]_m4_for(m4_decr([$#]), [1], [-1],
Packit 47b4ca
    [[, ]m4_dquote($], [)])[_m4_popdef([_m4_r])])_m4_r($@)])])
Packit 47b4ca
Packit 47b4ca
Packit 47b4ca
# m4_map_args_pair(EXPRESSION, [END-EXPR = EXPRESSION], ARG...)
Packit 47b4ca
# -------------------------------------------------------------
Packit 47b4ca
# Perform a pairwise grouping of consecutive ARGs, by expanding
Packit 47b4ca
# EXPRESSION([ARG1], [ARG2]).  If there are an odd number of ARGs, the
Packit 47b4ca
# final argument is expanded with END-EXPR([ARGn]).
Packit 47b4ca
#
Packit 47b4ca
# Build the temporary macro _m4_map_args_pair, with the $2([$m+1])
Packit 47b4ca
# only output if $# is odd:
Packit 47b4ca
#   $1([$3], [$4])[]$1([$5], [$6])[]...$1([$m-1],
Packit 47b4ca
#   [$m])[]m4_default([$2], [$1])([$m+1])[]_m4_popdef([_m4_map_args_pair])
Packit 47b4ca
m4_define([m4_map_args_pair],
Packit 47b4ca
[m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
Packit 47b4ca
       [$#], [1], [m4_fatal([$0: too few arguments: $#: $1])],
Packit 47b4ca
       [$#], [2], [],
Packit 47b4ca
       [$#], [3], [m4_default([$2], [$1])([$3])[]],
Packit 47b4ca
       [m4_pushdef([_$0], _m4_for([3],
Packit 47b4ca
   m4_eval([$# / 2 * 2 - 1]), [2], [_$0_(], [)])_$0_end(
Packit 47b4ca
   [1], [2], [$#])[_m4_popdef([_$0])])_$0($@)])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_map_args_pair_],
Packit 47b4ca
[$0_([1], [$1], m4_incr([$1]))])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_map_args_pair__],
Packit 47b4ca
[[$$1([$$2], [$$3])[]]])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_map_args_pair_end],
Packit 47b4ca
[m4_if(m4_eval([$3 & 1]), [1], [[m4_default([$$2], [$$1])([$$3])[]]])])
Packit 47b4ca
Packit 47b4ca
# m4_join(SEP, ARG1, ARG2...)
Packit 47b4ca
# ---------------------------
Packit 47b4ca
# Produce ARG1SEPARG2...SEPARGn.  Avoid back-to-back SEP when a given ARG
Packit 47b4ca
# is the empty string.  No expansion is performed on SEP or ARGs.
Packit 47b4ca
#
Packit 47b4ca
# Use a self-modifying separator, since we don't know how many
Packit 47b4ca
# arguments might be skipped before a separator is first printed, but
Packit 47b4ca
# be careful if the separator contains $.  _m4_foreach to the rescue.
Packit 47b4ca
m4_define([m4_join],
Packit 47b4ca
[m4_pushdef([_m4_sep], [m4_define([_m4_sep], _m4_defn([m4_echo]))])]dnl
Packit 47b4ca
[_m4_foreach([_$0([$1],], [)], $@)_m4_popdef([_m4_sep])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_join],
Packit 47b4ca
[m4_if([$2], [], [], [_m4_sep([$1])[$2]])])
Packit 47b4ca
Packit 47b4ca
# m4_joinall(SEP, ARG1, ARG2...)
Packit 47b4ca
# ------------------------------
Packit 47b4ca
# Produce ARG1SEPARG2...SEPARGn.  An empty ARG results in back-to-back SEP.
Packit 47b4ca
# No expansion is performed on SEP or ARGs.
Packit 47b4ca
#
Packit 47b4ca
# A bit easier than m4_join.  _m4_foreach to the rescue.
Packit 47b4ca
m4_define([m4_joinall],
Packit 47b4ca
[[$2]m4_if(m4_eval([$# <= 2]), [1], [],
Packit 47b4ca
	   [_m4_foreach([$1], [], m4_shift($@))])])
Packit 47b4ca
Packit 47b4ca
# m4_list_cmp(A, B)
Packit 47b4ca
# -----------------
Packit 47b4ca
# Compare the two lists of integer expressions A and B.
Packit 47b4ca
#
Packit 47b4ca
# m4_list_cmp takes care of any side effects; we only override
Packit 47b4ca
# _m4_list_cmp_raw, where we can safely expand lists multiple times.
Packit 47b4ca
# First, insert padding so that both lists are the same length; the
Packit 47b4ca
# trailing +0 is necessary to handle a missing list.  Next, create a
Packit 47b4ca
# temporary macro to perform pairwise comparisons until an inequality
Packit 47b4ca
# is found.  For example, m4_list_cmp([1], [1,2]) creates _m4_cmp as
Packit 47b4ca
#   m4_if(m4_eval([($1) != ($3)]), [1], [m4_cmp([$1], [$3])],
Packit 47b4ca
#         m4_eval([($2) != ($4)]), [1], [m4_cmp([$2], [$4])],
Packit 47b4ca
#         [0]_m4_popdef([_m4_cmp]))
Packit 47b4ca
# then calls _m4_cmp([1+0], [0*2], [1], [2+0])
Packit 47b4ca
m4_define([_m4_list_cmp_raw],
Packit 47b4ca
[m4_if([$1], [$2], 0,
Packit 47b4ca
       [_m4_list_cmp($1+0_m4_list_pad(m4_count($1), m4_count($2)),
Packit 47b4ca
		     $2+0_m4_list_pad(m4_count($2), m4_count($1)))])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_list_pad],
Packit 47b4ca
[m4_if(m4_eval($1 < $2), [1],
Packit 47b4ca
       [_m4_for(m4_incr([$1]), [$2], [1], [,0*])])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_list_cmp],
Packit 47b4ca
[m4_pushdef([_m4_cmp], [m4_if(]_m4_for(
Packit 47b4ca
   [1], m4_eval([$# >> 1]), [1], [$0_(], [,]m4_eval([$# >> 1])[)])[
Packit 47b4ca
      [0]_m4_popdef([_m4_cmp]))])_m4_cmp($@)])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_list_cmp_],
Packit 47b4ca
[$0_([$1], m4_eval([$1 + $2]))])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_list_cmp__],
Packit 47b4ca
[[m4_eval([($$1) != ($$2)]), [1], [m4_cmp([$$1], [$$2])],
Packit 47b4ca
]])
Packit 47b4ca
Packit 47b4ca
# m4_max(EXPR, ...)
Packit 47b4ca
# m4_min(EXPR, ...)
Packit 47b4ca
# -----------------
Packit 47b4ca
# Return the decimal value of the maximum (or minimum) in a series of
Packit 47b4ca
# integer expressions.
Packit 47b4ca
#
Packit 47b4ca
# _m4_foreach to the rescue; we only need to replace _m4_minmax.  Here,
Packit 47b4ca
# we need a temporary macro to track the best answer so far, so that
Packit 47b4ca
# the foreach expression is tractable.
Packit 47b4ca
m4_define([_m4_minmax],
Packit 47b4ca
[m4_pushdef([_m4_best], m4_eval([$2]))_m4_foreach(
Packit 47b4ca
  [m4_define([_m4_best], $1(_m4_best,], [))], m4_shift($@))]dnl
Packit 47b4ca
[_m4_best[]_m4_popdef([_m4_best])])
Packit 47b4ca
Packit 47b4ca
# m4_set_add_all(SET, VALUE...)
Packit 47b4ca
# -----------------------------
Packit 47b4ca
# Add each VALUE into SET.  This is O(n) in the number of VALUEs, and
Packit 47b4ca
# can be faster than calling m4_set_add for each VALUE.
Packit 47b4ca
#
Packit 47b4ca
# _m4_foreach to the rescue.  If no deletions have occurred, then
Packit 47b4ca
# avoid the speed penalty of m4_set_add.
Packit 47b4ca
m4_define([m4_set_add_all],
Packit 47b4ca
[m4_if([$#], [0], [], [$#], [1], [],
Packit 47b4ca
       [m4_define([_m4_set_size($1)], m4_eval(m4_set_size([$1])
Packit 47b4ca
	  + m4_len(_m4_foreach(m4_ifdef([_m4_set_cleanup($1)],
Packit 47b4ca
  [[m4_set_add]], [[_$0]])[([$1],], [)], $@))))])])
Packit 47b4ca
Packit 47b4ca
m4_define([_m4_set_add_all],
Packit 47b4ca
[m4_ifdef([_m4_set([$1],$2)], [],
Packit 47b4ca
	  [m4_define([_m4_set([$1],$2)],
Packit 47b4ca
		     [1])m4_pushdef([_m4_set([$1])], [$2])-])])