# # Copyright (C) 2017 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #
# Returns true if $(1) is a non-negative integer <= 100, otherwise returns nothing. define math_is_number_in_100
$(strip \
$(if $(1),,$(call math-error,Argument missing)) \
$(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
$(if $(filter $(1),$(__MATH_NUMBERS)),true)) endef
# Same with math_is_number_in_100, but no limit. define _math_ext_is_number
$(strip \
$(if $(1),,$(call math-error,Argument missing)) \
$(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
$(eval should_empty:=$(1)) \
$(foreach num,$(__MATH_ONE_NUMBERS),\
$(eval should_empty:=$(subst $(num),$(empty),$(should_empty)))) \
$(if $(should_empty),,true)) endef
# Returns true if $(1) is a non-negative integer. define math_is_number
$(strip $(if $(call math_is_number_in_100,$(1)),true,$(call _math_ext_is_number,$(1)))) endef
# Returns true if $(1) is a positive or negative integer. define math_is_int
$(call math_is_number,$(patsubst -%,%,$(1))) endef
define math_is_zero
$(strip \
$(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
$(if $(filter 0,$(1)),true)) endef
$(call math-expect-true,(call math_is_number,0))
$(call math-expect-true,(call math_is_number,2))
$(call math-expect-true,(call math_is_number,202412))
$(call math-expect-false,(call math_is_number,foo))
$(call math-expect-false,(call math_is_number,-1))
$(call math-expect-true,(call math_is_int,50))
$(call math-expect-true,(call math_is_int,-1))
$(call math-expect-true,(call math_is_int,-528))
$(call math-expect-true,(call math_is_int,-0))
$(call math-expect-false,(call math_is_int,--1))
$(call math-expect-false,(call math_is_int,-))
$(call math-expect-error,(call math_is_number,12),Multiple words in a single argument: 12)
$(call math-expect-error,(call math_is_number,no 2),Multiple words in a single argument: no 2)
$(call math-expect-true,(call math_is_zero,0))
$(call math-expect-false,(call math_is_zero,1))
$(call math-expect-false,(call math_is_zero,foo))
$(call math-expect-error,(call math_is_zero,12),Multiple words in a single argument: 12)
$(call math-expect-error,(call math_is_zero,no 2),Multiple words in a single argument: no 2)
# Split an integer into a list of digits define _math_number_to_list
$(strip \
$(if $(call _math_ext_is_number,$(1)),,\
$(call math-error,Only non-negative integers are supported (not $(1)))) \
$(eval num_list:=$(1)) \
$(foreach num,$(__MATH_ONE_NUMBERS),\
$(eval num_list:=$(subst $(num),$(space)$(num),$(num_list)))) \
$(if $(filter $(words $(num_list)),$(__MATH_ONE_NUMBERS)),,\
$(call math-error,Only non-negative integers with less than 9 digits are supported (not $(1)))) \
$(if $(filter 0,$(word 1,$(num_list))),\
$(call math-error,Only non-negative integers without leading zeros are supported (not $(1)))) \
$(num_list)) endef
$(call math-expect,(call _math_number_to_list,123),123)
$(call math-expect-error,(call _math_number_to_list,123456),Multiple words in a single argument: 123456)
$(call math-expect-error,(call _math_number_to_list,-123),Only non-negative integers are supported (not -123))
$(call math-expect-error,(call _math_number_to_list,002),Only non-negative integers without leading zeros are supported (not 002))
$(call math-expect-error,(call _math_number_to_list,1234567890),Only non-negative integers with less than 9 digits are supported (not 1234567890))
# Compare 1-digit integer $(1) and $(2). # Returns 1 if $(1) > $(2), -1 if $(1) < $(2), nothing if equals. define _math_1digit_comp
$(strip \
$(if $(filter $(1),$(2)),,\
$(if $(filter $(1),$(firstword $(filter $(1) $(2),$(__MATH_ONE_NUMBERS)))),-1,1))) endef
# Compare the same $(3)-digit-length integers $(1) and $(2) that are split into a list of digits. # Returns 1 if $(1) > $(2), -1 if $(1) < $(2), nothing if equals. define _math_list_comp
$(strip \
$(eval ans:=) \
$(foreach num,$(call int_range_list,1,$(3)),\
$(if $(ans),,$(eval ans:=$(call _math_1digit_comp,$(word $(num),$(1)),$(word $(num),$(2)))))) \
$(ans)) endef
# Compare any two non-negative integers $(1) and $(2). # Returns 1 if $(1) > $(2), -1 if $(1) < $(2), nothing if equals. define _math_ext_comp
$(strip \
$(eval num_list1:=$(call _math_number_to_list,$(1))) \
$(eval len1:=$(words $(num_list1))) \
$(eval num_list2:=$(call _math_number_to_list,$(2))) \
$(eval len2:=$(words $(num_list2))) \
$(eval comp:=$(call _math_1digit_comp,$(len1),$(len2))) \
$(if $(comp),$(comp),$(call _math_list_comp,$(num_list1),$(num_list2),$(len1)))) endef
# Returns the greater of $1 or $2. # If $1 or $2 is not a positive integer, then an error is generated. define math_max
$(strip \
$(if $(filter truetrue,$(call math_is_number_in_100,$(1))$(call math_is_number_in_100,$(2))),\
$(lastword $(filter $(1) $(2),$(__MATH_NUMBERS))),\
$(if $(filter 1,$(call _math_ext_comp,$(1),$(2))),$(1),$(2)))) endef
# Returns the lesser of $1 or $2. define math_min
$(strip \
$(if $(filter truetrue,$(call math_is_number_in_100,$(1))$(call math_is_number_in_100,$(2))),\
$(firstword $(filter $(1) $(2),$(__MATH_NUMBERS))),\
$(if $(filter -1,$(call _math_ext_comp,$(1),$(2))),$(1),$(2)))) endef
$(call math-expect-error,(call math_max),Argument missing)
$(call math-expect-error,(call math_max,1),Argument missing)
$(call math-expect-error,(call math_max,12,3),Multiple words in a single argument: 12)
$(call math-expect-error,(call math_min,1,23),Multiple words in a single argument: 23)
$(call math-expect,(call math_max,0,1),1)
$(call math-expect,(call math_max,1,0),1)
$(call math-expect,(call math_max,1,1),1)
$(call math-expect,(call math_max,5,42),42)
$(call math-expect,(call math_max,42,5),42)
$(call math-expect,(call math_min,0,1),0)
$(call math-expect,(call math_min,1,0),0)
$(call math-expect,(call math_min,1,1),1)
$(call math-expect,(call math_min,7,32),7)
$(call math-expect,(call math_min,32,7),7)
# Returns the words in $2 that are numbers and are less than $1 define numbers_less_than
$(strip \
$(foreach n,$2, \
$(if $(call math_is_number,$(n)), \
$(if $(call math_lt,$(n),$(1)), \
$(n))))) endef
# Returns the words in $2 that are numbers and are greater or equal to $1 define numbers_greater_or_equal_to
$(strip \
$(foreach n,$2, \
$(if $(call math_is_number,$(n)), \
$(if $(call math_gt_or_eq,$(n),$(1)), \
$(n))))) endef
# 10,001 = 10 ** 4 + 1, contains 10,001 x's, so 1 more than 10,000 (future) API level
_INT_LIMIT_WORDS := x $(foreach a,0123456789,$(foreach b,0123456789,\
$(foreach c,0123456789,x x x x x x x x x x)))
define _int_encode
$(if $(filter $(words x $(_INT_LIMIT_WORDS)),$(words $(wordlist 1,$(1),x $(_INT_LIMIT_WORDS)))),\
$(call math-error,integer greater than $(words $(_INT_LIMIT_WORDS)) is not supported!),\
$(wordlist 1,$(1),$(_INT_LIMIT_WORDS))) endef
# _int_max returns the maximum of the two arguments # input: two (x) lists; output: one (x) list # integer cannot be passed in directly. It has to be converted using _int_encode. define _int_max
$(subst xx,x,$(join $(1),$(2))) endef
# first argument is greater than second argument # output: non-empty if true # integer cannot be passed in directly. It has to be converted using _int_encode. define _int_greater-than
$(filter-out $(words $(2)),$(words $(call _int_max,$(1),$(2)))) endef
# first argument equals to second argument # output: non-empty if true # integer cannot be passed in directly. It has to be converted using _int_encode. define _int_equal
$(filter $(words $(1)),$(words $(2))) endef
# first argument is greater than or equal to second argument # output: non-empty if true # integer cannot be passed in directly. It has to be converted using _int_encode. define _int_greater-or-equal
$(call _int_greater-than,$(1),$(2))$(call _int_equal,$(1),$(2)) endef
define int_divide
$(if $(filter 0,$(2)),$(call math-error,division by zero is not allowed!),$(strip \
$(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))), \
$(call int_plus,$(call int_divide,$(call int_subtract,$(1),$(2)),$(2)),1),0))) endef
$(call math-expect,(call int_divide,1,1),1)
$(call math-expect,(call int_divide,200,1),200)
$(call math-expect,(call int_divide,200,3),66)
$(call math-expect,(call int_divide,1,2),0)
$(call math-expect-error,(call int_divide,0,0),division by zero is not allowed!)
$(call math-expect-error,(call int_divide,1,0),division by zero is not allowed!)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.