Re: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime()
Hi, On Thu, Jun 17, 2021 at 05:42:12PM +, Peter Stuge wrote: > Rolf Eike Beer wrote: > > The previous algorithm would scan for all primes for a given number, > > which takes needlessly long. > > Please also mention how this caused a problem for you in the commit > message, to help us understand why you're proposing to change this. > > > > +++ b/eclass/qmail.eclass > > -# @FUNCTION: is_prima > > +# @FUNCTION: is_prime > > # @USAGE: > > # @DESCRIPTION: > > # Checks wether a number is a prime number > > Maybe name the algorithm? Looks like an optimization of the sieve of > Eratosthenes? > > > > is_prime() { > > local number=${1} i > > - for i in $(primes ${number} ${number}) > > + > > + if [[ $[number % 2] == 0 ]]; then > > + return 1 > > + fi > > While 2 itself is prime the above returns 1 for any even number. There's actually a much simpler solution to this: $ is_prime() { test $(factor $1 | cut -d: -f2 | wc -w) == 1; } $ for n in $(seq 0 10); do is_prime $n && echo $n is prime; done 2 is prime 3 is prime 5 is prime 7 is prime $ time factor 20187319083467888113 20187319083467888113: 20187319083467888113 0.00 Cheers, -Guilherme
Re: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime()
Rolf Eike Beer wrote: > The previous algorithm would scan for all primes for a given number, > which takes needlessly long. Please also mention how this caused a problem for you in the commit message, to help us understand why you're proposing to change this. > +++ b/eclass/qmail.eclass > -# @FUNCTION: is_prima > +# @FUNCTION: is_prime > # @USAGE: > # @DESCRIPTION: > # Checks wether a number is a prime number Maybe name the algorithm? Looks like an optimization of the sieve of Eratosthenes? > is_prime() { > local number=${1} i > - for i in $(primes ${number} ${number}) > + > + if [[ $[number % 2] == 0 ]]; then > + return 1 > + fi While 2 itself is prime the above returns 1 for any even number. > + for ((i = 3; i * i <= number; i += 2)) > do > - [[ ${i} == ${number} ]] && return 0 > + if [[ $[number % i ] == 0 ]]; then An inconsistent space after "% i" here. I don't know what style is correct. //Peter
[gentoo-dev] [PATCH] qmail.eclass: simplify is_prime()
The previous algorithm would scan for all primes for a given number, which takes needlessly long. Signed-off-by: Rolf Eike Beer --- eclass/qmail.eclass | 45 - 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/eclass/qmail.eclass b/eclass/qmail.eclass index f42f0491515..aaec205a6bd 100644 --- a/eclass/qmail.eclass +++ b/eclass/qmail.eclass @@ -20,46 +20,25 @@ GENQMAIL_S="${WORKDIR}"/genqmail-${GENQMAIL_PV} QMAIL_SPP_F=qmail-spp-${QMAIL_SPP_PV}.tar.gz QMAIL_SPP_S="${WORKDIR}"/qmail-spp-${QMAIL_SPP_PV} -# @FUNCTION: primes -# @USAGE: -# @DESCRIPTION: -# Prints a list of primes between min and max inclusive -# Note: this functions gets very slow when used with large numbers. -primes() { - local min=${1} max=${2} - local result= primelist=2 i p - - [[ ${min} -le 2 ]] && result="${result} 2" - - for ((i = 3; i <= max; i += 2)) - do - for p in ${primelist} - do - [[ $[i % p] == 0 || $[p * p] -gt ${i} ]] && \ - break - done - if [[ $[i % p] != 0 ]] - then - primelist="${primelist} ${i}" - [[ ${i} -ge ${min} ]] && \ - result="${result} ${i}" - fi - done - - echo ${result} -} - -# @FUNCTION: is_prima +# @FUNCTION: is_prime # @USAGE: # @DESCRIPTION: # Checks wether a number is a prime number is_prime() { local number=${1} i - for i in $(primes ${number} ${number}) + + if [[ $[number % 2] == 0 ]]; then + return 1 + fi + + for ((i = 3; i * i <= number; i += 2)) do - [[ ${i} == ${number} ]] && return 0 + if [[ $[number % i ] == 0 ]]; then + return 1 + fi done - return 1 + + return 0 } dospp() { -- 2.26.2 signature.asc Description: This is a digitally signed message part.
Re: [gentoo-dev] [PATCH] fcaps.eclass: support EAPI 8
On Thu, Jun 17, 2021 at 7:02 AM Ulrich Mueller wrote: > > On Thu, 17 Jun 2021, Michał Górny wrote: > > On Thu, 2021-06-17 at 12:10 +0200, Ulrich Mueller wrote: > >> > > > > > On Thu, 17 Jun 2021, David Michael wrote: > >> > @@ -33,15 +34,12 @@ _FCAPS_ECLASS=1 > >> > > >> > IUSE="+filecaps" > >> > > >> > -# Since it is needed in pkg_postinst() it must be in RDEPEND > >> > +# Since it is needed in pkg_postinst() it must be in IDEPEND > >> > case "${EAPI:-0}" in > >> > - [0-6]) > >> > - RDEPEND="filecaps? ( sys-libs/libcap )" > >> > - ;; > >> > - *) > >> > - BDEPEND="filecaps? ( sys-libs/libcap )" > >> > - RDEPEND="${BDEPEND}" > >> > - ;; > >> > + 7) BDEPEND="filecaps? ( sys-libs/libcap )" ;& > >> > >> This is ill-defined in old EAPIs (5 and before), so the case statement > >> may fail. You cannot use ;& in global scope of an eclass. > >> > >> Why not just change * to 7, and add a new case for 8? It's one > >> additional line, and IMHO would be better readable than having a > >> fallthrough. > > > I've already left a comment on the PR suggesting to split it into two > > cases: one to check for valid EAPI, the other for deps. It would be > > cleaner IMO, as the second case would cover all future EAPIs via *, > > and the first one would prevent the syntax error from applying to old > > EAPIs (I've tested that). > > That would work too. But please add a comment making it clear that there > is a fallthrough. I've updated https://github.com/gentoo/gentoo/pull/21239 with this. Thanks. David
Re: [gentoo-dev] [PATCH] fcaps.eclass: support EAPI 8
> On Thu, 17 Jun 2021, Michał Górny wrote: > On Thu, 2021-06-17 at 12:10 +0200, Ulrich Mueller wrote: >> > > > > > On Thu, 17 Jun 2021, David Michael wrote: >> >> > @@ -33,15 +34,12 @@ _FCAPS_ECLASS=1 >> > >> > IUSE="+filecaps" >> > >> > -# Since it is needed in pkg_postinst() it must be in RDEPEND >> > +# Since it is needed in pkg_postinst() it must be in IDEPEND >> > case "${EAPI:-0}" in >> > - [0-6]) >> > - RDEPEND="filecaps? ( sys-libs/libcap )" >> > - ;; >> > - *) >> > - BDEPEND="filecaps? ( sys-libs/libcap )" >> > - RDEPEND="${BDEPEND}" >> > - ;; >> > + 7) BDEPEND="filecaps? ( sys-libs/libcap )" ;& >> >> This is ill-defined in old EAPIs (5 and before), so the case statement >> may fail. You cannot use ;& in global scope of an eclass. >> >> Why not just change * to 7, and add a new case for 8? It's one >> additional line, and IMHO would be better readable than having a >> fallthrough. > I've already left a comment on the PR suggesting to split it into two > cases: one to check for valid EAPI, the other for deps. It would be > cleaner IMO, as the second case would cover all future EAPIs via *, > and the first one would prevent the syntax error from applying to old > EAPIs (I've tested that). That would work too. But please add a comment making it clear that there is a fallthrough. Ulrich signature.asc Description: PGP signature
Re: [gentoo-dev] [PATCH] fcaps.eclass: support EAPI 8
On Thu, 2021-06-17 at 12:10 +0200, Ulrich Mueller wrote: > > > > > > On Thu, 17 Jun 2021, David Michael wrote: > > > @@ -33,15 +34,12 @@ _FCAPS_ECLASS=1 > > > > IUSE="+filecaps" > > > > -# Since it is needed in pkg_postinst() it must be in RDEPEND > > +# Since it is needed in pkg_postinst() it must be in IDEPEND > > case "${EAPI:-0}" in > > - [0-6]) > > - RDEPEND="filecaps? ( sys-libs/libcap )" > > - ;; > > - *) > > - BDEPEND="filecaps? ( sys-libs/libcap )" > > - RDEPEND="${BDEPEND}" > > - ;; > > + 7) BDEPEND="filecaps? ( sys-libs/libcap )" ;& > > This is ill-defined in old EAPIs (5 and before), so the case statement > may fail. You cannot use ;& in global scope of an eclass. > > Why not just change * to 7, and add a new case for 8? It's one > additional line, and IMHO would be better readable than having a > fallthrough. I've already left a comment on the PR suggesting to split it into two cases: one to check for valid EAPI, the other for deps. It would be cleaner IMO, as the second case would cover all future EAPIs via *, and the first one would prevent the syntax error from applying to old EAPIs (I've tested that). -- Best regards, Michał Górny
Re: [gentoo-dev] EAPI 8 is here!
> On Thu, 17 Jun 2021, Sam James wrote: > EAPI 8 is here! In fact, it arrived a few days ago on Sunday: > [...] > Things you need to know: > * You can read the full specification in PMS as above. > * It's fully implemented in Portage 3.0.20, pkgcore 0.12.0, and > pkgcheck 0.10.0. IIUC, implementation of EAPI 8 in pkgcore is still incomplete: https://github.com/pkgcore/pkgcore/issues/313 In general, the PMS team tracks EAPI support in package managers on this wiki page: https://wiki.gentoo.org/wiki/Project:PMS#Implementation_in_package_managers Ulrich signature.asc Description: PGP signature
Re: [gentoo-dev] [PATCH] fcaps.eclass: support EAPI 8
> On Thu, 17 Jun 2021, David Michael wrote: > @@ -33,15 +34,12 @@ _FCAPS_ECLASS=1 > > IUSE="+filecaps" > > -# Since it is needed in pkg_postinst() it must be in RDEPEND > +# Since it is needed in pkg_postinst() it must be in IDEPEND > case "${EAPI:-0}" in > - [0-6]) > - RDEPEND="filecaps? ( sys-libs/libcap )" > - ;; > - *) > - BDEPEND="filecaps? ( sys-libs/libcap )" > - RDEPEND="${BDEPEND}" > - ;; > + 7) BDEPEND="filecaps? ( sys-libs/libcap )" ;& This is ill-defined in old EAPIs (5 and before), so the case statement may fail. You cannot use ;& in global scope of an eclass. Why not just change * to 7, and add a new case for 8? It's one additional line, and IMHO would be better readable than having a fallthrough. > + 6) RDEPEND="filecaps? ( sys-libs/libcap )" ;; > + 8) IDEPEND="filecaps? ( sys-libs/libcap )" ;; > + *) die "EAPI ${EAPI:-0} is unsupported" ;; > esac > > # @ECLASS-VARIABLE: FILECAPS Ulrich signature.asc Description: PGP signature
Re: [gentoo-dev] Up for grabs: various base-system@ packages
On 2021-06-16 19:18, Sam James wrote: - app-editors/hexcurse I'll take this one. -- Marecki OpenPGP_signature Description: OpenPGP digital signature