[Dune-devel] In favor for &&/|| instead of and/or (was: Re: [Dune-Commit] [Commit] dune-istl - f5374e8: Code simplification:) use std::lower_bound instead of hand-coding binary search

Markus Blatt markus at dr-blatt.de
Mon Jan 5 09:57:40 CET 2015


Hi and happy new year to all!

On Sun, Jan 04, 2015 at 09:49:00PM +0100, Oliver Sander wrote:
> New commit, appeared at Sun Jan  4 21:49:00 2015 +0100
> as part of the following ref changes:
> 
>     branch refs/heads/master    updated from f6de8ad -> f5374e8
> 
> Browsable version: http://cgit.dune-project.org/repositories/dune-istl/commit/?id=f5374e8c38d9f95f5e6de882dc3ab570a1aa764b
> 
> ======================================================================
> 
> commit f5374e8c38d9f95f5e6de882dc3ab570a1aa764b
> Author: Oliver Sander <sander at igpm.rwth-aachen.de>
> Date:   Sun Jan 4 21:46:49 2015 +0100
> 
>     Code simplification: use std::lower_bound instead of hand-coding binary search
> 
>  dune/istl/basearray.hh | 26 ++++++--------------------
>  1 file changed, 6 insertions(+), 20 deletions(-)
> 
> 
> 
> diff --git a/dune/istl/basearray.hh b/dune/istl/basearray.hh
> index 579f950..88f1c06 100644
> --- a/dune/istl/basearray.hh
> +++ b/dune/istl/basearray.hh
> @@ -560,33 +560,19 @@ namespace Dune {
>      //! random access to blocks, assumes ascending ordering
>      B& operator[] (size_type i)
>      {
> -      size_type l=0, r=n-1;
> -      while (l<r)
> -      {
> -        size_type q = (l+r)/2;
> -        if (i <= j[q]) r=q;
> -        else l = q+1;
> -      }
> -      if (j[l]!=i) {
> +      const size_type* lb = std::lower_bound(j, j+n, i);
> +      if (lb == j+n or *lb != i)
>          DUNE_THROW(ISTLError,"index "<<i<<" not in compressed array");
> -      }
> -      return p[l];
> +      return p[lb-j];
>      }
> [skipped the rest]

I would assume that the additional test in the if statement does not hurt,
but could we  settle for &&, and || instead of and, and or?
There have been problems with this in the past on some compilers.

Markus

>  
>      //! same for read only access, assumes ascending ordering
>      const B& operator[] (size_type i) const
>      {
> -      size_type l=0, r=n-1;
> -      while (l<r)
> -      {
> -        size_type q = (l+r)/2;
> -        if (i <= j[q]) r=q;
> -        else l = q+1;
> -      }
> -      if (j[l]!=i) {
> +      const size_type* lb = std::lower_bound(j, j+n, i);
> +      if (lb == j+n or *lb != i)
>          DUNE_THROW(ISTLError,"index "<<i<<" not in compressed array");
> -      }
> -      return p[l];
> +      return p[lb-j];
>      }
>  
>      //! iterator class for sequential access
> 
> _______________________________________________
> Dune-Commit mailing list
> Dune-Commit at dune-project.org
> http://lists.dune-project.org/mailman/listinfo/dune-commit
> 

-- 
Do you need more support with DUNE or HPC in general? 

Dr. Markus Blatt - HPC-Simulation-Software & Services http://www.dr-blatt.de
Hans-Bunte-Str. 8-10, 69123 Heidelberg, Germany
Tel.: +49 (0) 160 97590858
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <https://lists.dune-project.org/pipermail/dune-devel/attachments/20150105/371679a8/attachment.sig>


More information about the Dune-devel mailing list