LCOV - code coverage report
Current view: top level - include/basegfx/range - basicrange.hxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 83 92 90.2 %
Date: 2014-04-11 Functions: 38 40 95.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #ifndef INCLUDED_BASEGFX_RANGE_BASICRANGE_HXX
      21             : #define INCLUDED_BASEGFX_RANGE_BASICRANGE_HXX
      22             : 
      23             : #include <sal/types.h>
      24             : #include <float.h>
      25             : #include <basegfx/numeric/ftools.hxx>
      26             : 
      27             : 
      28             : namespace basegfx
      29             : {
      30             :     template< typename T, typename Traits > class BasicRange
      31             :     {
      32             :     protected:
      33             :         T       mnMinimum;
      34             :         T       mnMaximum;
      35             : 
      36             :     public:
      37             :         typedef T       ValueType;
      38             :         typedef Traits  TraitsType;
      39             : 
      40    18922078 :         BasicRange() :
      41    18922078 :             mnMinimum(Traits::maxVal()),
      42    18922078 :             mnMaximum(Traits::minVal())
      43             :         {
      44    18922078 :         }
      45             : 
      46    67241590 :         explicit BasicRange( T nValue ) :
      47             :             mnMinimum(nValue),
      48    67241590 :             mnMaximum(nValue)
      49             :         {
      50    67241590 :         }
      51             : 
      52      514634 :         void reset()
      53             :         {
      54      514634 :             mnMinimum = Traits::maxVal();
      55      514634 :             mnMaximum = Traits::minVal();
      56      514634 :         }
      57             : 
      58   219761255 :         bool isEmpty() const
      59             :         {
      60   219761255 :             return Traits::maxVal() == mnMinimum;
      61             :         }
      62             : 
      63    49207000 :         T getMinimum() const { return mnMinimum; }
      64    42229368 :         T getMaximum() const { return mnMaximum; }
      65             : 
      66       39803 :         double getCenter() const
      67             :         {
      68       39803 :             if(isEmpty())
      69             :             {
      70           2 :                 return 0.0;
      71             :             }
      72             :             else
      73             :             {
      74       39801 :                 return ((mnMaximum + mnMinimum) / 2.0);
      75             :             }
      76             :         }
      77             : 
      78    42864548 :         bool isInside(T nValue) const
      79             :         {
      80    42864548 :             if(isEmpty())
      81             :             {
      82           0 :                 return false;
      83             :             }
      84             :             else
      85             :             {
      86    42864548 :                 return (nValue >= mnMinimum) && (nValue <= mnMaximum);
      87             :             }
      88             :         }
      89             : 
      90      248776 :         bool isInside(const BasicRange& rRange) const
      91             :         {
      92      248776 :             if(isEmpty())
      93             :             {
      94           0 :                 return false;
      95             :             }
      96             :             else
      97             :             {
      98      248776 :                 if(rRange.isEmpty())
      99             :                 {
     100           0 :                     return false;
     101             :                 }
     102             :                 else
     103             :                 {
     104      248776 :                     return (rRange.mnMinimum >= mnMinimum) && (rRange.mnMaximum <= mnMaximum);
     105             :                 }
     106             :             }
     107             :         }
     108             : 
     109    28175460 :         bool overlaps(const BasicRange& rRange) const
     110             :         {
     111    28175460 :             if(isEmpty())
     112             :             {
     113           0 :                 return false;
     114             :             }
     115             :             else
     116             :             {
     117    28175460 :                 if(rRange.isEmpty())
     118             :                 {
     119           0 :                     return false;
     120             :                 }
     121             :                 else
     122             :                 {
     123    28175460 :                     return !((rRange.mnMaximum < mnMinimum) || (rRange.mnMinimum > mnMaximum));
     124             :                 }
     125             :             }
     126             :         }
     127             : 
     128      538014 :         bool overlapsMore(const BasicRange& rRange) const
     129             :         {
     130      538014 :             if(isEmpty() || rRange.isEmpty())
     131           0 :                 return false;
     132             :             // returns true if the overlap is more than just a touching at the limits
     133      538014 :             return ((rRange.mnMaximum > mnMinimum) && (rRange.mnMinimum < mnMaximum));
     134             :         }
     135             : 
     136        4767 :         bool operator==( const BasicRange& rRange ) const
     137             :         {
     138        4767 :             return (mnMinimum == rRange.mnMinimum && mnMaximum == rRange.mnMaximum);
     139             :         }
     140             : 
     141        3516 :         bool operator!=( const BasicRange& rRange ) const
     142             :         {
     143        3516 :             return (mnMinimum != rRange.mnMinimum || mnMaximum != rRange.mnMaximum);
     144             :         }
     145             : 
     146         411 :         bool equal(const BasicRange& rRange) const
     147             :         {
     148             :             return (
     149         411 :                 fTools::equal(mnMinimum, rRange.mnMinimum) &&
     150         411 :                 fTools::equal(mnMaximum, rRange.mnMaximum));
     151             :         }
     152             : 
     153   100362889 :         void expand(T nValue)
     154             :         {
     155   100362889 :             if(isEmpty())
     156             :             {
     157     6381878 :                 mnMinimum = mnMaximum = nValue;
     158             :             }
     159             :             else
     160             :             {
     161    93981011 :                 if(nValue < mnMinimum)
     162             :                 {
     163    25593603 :                     mnMinimum = nValue;
     164             :                 }
     165             : 
     166    93981011 :                 if(nValue > mnMaximum)
     167             :                 {
     168    46177803 :                     mnMaximum = nValue;
     169             :                 }
     170             :             }
     171   100362889 :         }
     172             : 
     173    11332674 :         void expand(const BasicRange& rRange)
     174             :         {
     175    11332674 :             if(isEmpty())
     176             :             {
     177    10135903 :                 mnMinimum = rRange.mnMinimum;
     178    10135903 :                 mnMaximum = rRange.mnMaximum;
     179             :             }
     180             :             else
     181             :             {
     182     1196771 :                 if(!rRange.isEmpty())
     183             :                 {
     184     1186974 :                     if(rRange.mnMinimum < mnMinimum)
     185             :                     {
     186      169873 :                         mnMinimum = rRange.mnMinimum;
     187             :                     }
     188             : 
     189     1186974 :                     if(rRange.mnMaximum > mnMaximum)
     190             :                     {
     191      240575 :                         mnMaximum = rRange.mnMaximum;
     192             :                     }
     193             :                 }
     194             :             }
     195    11332674 :         }
     196             : 
     197     7109164 :         void intersect(const BasicRange& rRange)
     198             :         {
     199             :             // here, overlaps also tests all isEmpty() conditions already.
     200     7109164 :             if( !overlaps( rRange ) )
     201             :             {
     202      126733 :                 reset();
     203             :             }
     204             :             else
     205             :             {
     206     6982431 :                 if(rRange.mnMinimum > mnMinimum)
     207             :                 {
     208      717515 :                     mnMinimum = rRange.mnMinimum;
     209             :                 }
     210             : 
     211     6982431 :                 if(rRange.mnMaximum < mnMaximum)
     212             :                 {
     213      872475 :                     mnMaximum = rRange.mnMaximum;
     214             :                 }
     215             :             }
     216     7109164 :         }
     217             : 
     218      404820 :         void grow(T nValue)
     219             :         {
     220      404820 :             if(!isEmpty())
     221             :             {
     222      404820 :                 bool bLessThanZero(nValue < 0);
     223             : 
     224      404820 :                 if(nValue > 0 || bLessThanZero)
     225             :                 {
     226      404820 :                     mnMinimum -= nValue;
     227      404820 :                     mnMaximum += nValue;
     228             : 
     229      404820 :                     if(bLessThanZero)
     230             :                     {
     231             :                         // test if range did collapse
     232           0 :                         if(mnMinimum > mnMaximum)
     233             :                         {
     234             :                             // if yes, collapse to center
     235           0 :                             mnMinimum = mnMaximum = (mnMinimum + mnMaximum) / 2;
     236             :                         }
     237             :                     }
     238             :                 }
     239             :             }
     240      404820 :         }
     241             : 
     242     4537884 :         typename Traits::DifferenceType getRange() const
     243             :         {
     244     4537884 :             if(isEmpty())
     245             :             {
     246          46 :                 return Traits::neutral();
     247             :             }
     248             :             else
     249             :             {
     250     4537838 :                 return (mnMaximum - mnMinimum);
     251             :             }
     252             :         }
     253             :     };
     254             : 
     255             :     // some pre-fabricated traits
     256             :     struct DoubleTraits
     257             :     {
     258    17432941 :         static double minVal() { return DBL_MIN; };
     259   198739559 :         static double maxVal() { return DBL_MAX; };
     260          46 :         static double neutral() { return 0.0; };
     261             : 
     262             :         typedef double DifferenceType;
     263             :     };
     264             : 
     265             :     struct Int32Traits
     266             :     {
     267     2003771 :         static sal_Int32 minVal() { return SAL_MIN_INT32; };
     268    40458408 :         static sal_Int32 maxVal() { return SAL_MAX_INT32; };
     269           0 :         static sal_Int32 neutral() { return 0L; };
     270             : 
     271             :         typedef sal_Int64 DifferenceType;
     272             :     };
     273             : 
     274             : } // end of namespace basegfx
     275             : 
     276             : #endif // INCLUDED_BASEGFX_RANGE_BASICRANGE_HXX
     277             : 
     278             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10