LCOV - code coverage report
Current view: top level - libreoffice/basegfx/inc/basegfx/range - basicrange.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 55 68 80.9 %
Date: 2012-12-17 Functions: 20 28 71.4 %
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 _BGFX_RANGE_BASICRANGE_HXX
      21             : #define _BGFX_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      780048 :         BasicRange() :
      41             :             mnMinimum(Traits::maxVal()),
      42      780048 :             mnMaximum(Traits::minVal())
      43             :         {
      44      780048 :         }
      45             : 
      46       65048 :         explicit BasicRange( T nValue ) :
      47             :             mnMinimum(nValue),
      48       65048 :             mnMaximum(nValue)
      49             :         {
      50       65048 :         }
      51             : 
      52       13688 :         void reset()
      53             :         {
      54       13688 :             mnMinimum = Traits::maxVal();
      55       13688 :             mnMaximum = Traits::minVal();
      56       13688 :         }
      57             : 
      58     2222154 :         bool isEmpty() const
      59             :         {
      60     2222154 :             return Traits::maxVal() == mnMinimum;
      61             :         }
      62             : 
      63      343644 :         T getMinimum() const { return mnMinimum; }
      64      342998 :         T getMaximum() const { return mnMaximum; }
      65             : 
      66         556 :         double getCenter() const
      67             :         {
      68         556 :             if(isEmpty())
      69             :             {
      70           0 :                 return 0.0;
      71             :             }
      72             :             else
      73             :             {
      74         556 :                 return ((mnMaximum + mnMinimum) / 2.0);
      75             :             }
      76             :         }
      77             : 
      78       40652 :         bool isInside(T nValue) const
      79             :         {
      80       40652 :             if(isEmpty())
      81             :             {
      82           0 :                 return false;
      83             :             }
      84             :             else
      85             :             {
      86       40652 :                 return (nValue >= mnMinimum) && (nValue <= mnMaximum);
      87             :             }
      88             :         }
      89             : 
      90        2260 :         bool isInside(const BasicRange& rRange) const
      91             :         {
      92        2260 :             if(isEmpty())
      93             :             {
      94           0 :                 return false;
      95             :             }
      96             :             else
      97             :             {
      98        2260 :                 if(rRange.isEmpty())
      99             :                 {
     100           0 :                     return false;
     101             :                 }
     102             :                 else
     103             :                 {
     104        2260 :                     return (rRange.mnMinimum >= mnMinimum) && (rRange.mnMaximum <= mnMaximum);
     105             :                 }
     106             :             }
     107             :         }
     108             : 
     109       20354 :         bool overlaps(const BasicRange& rRange) const
     110             :         {
     111       20354 :             if(isEmpty())
     112             :             {
     113           0 :                 return false;
     114             :             }
     115             :             else
     116             :             {
     117       20354 :                 if(rRange.isEmpty())
     118             :                 {
     119           0 :                     return false;
     120             :                 }
     121             :                 else
     122             :                 {
     123       20354 :                     return !((rRange.mnMaximum < mnMinimum) || (rRange.mnMinimum > mnMaximum));
     124             :                 }
     125             :             }
     126             :         }
     127             : 
     128        9336 :         bool overlapsMore(const BasicRange& rRange) const
     129             :         {
     130        9336 :             if(isEmpty() || rRange.isEmpty())
     131           0 :                 return false;
     132             :             // returns true if the overlap is more than just a touching at the limits
     133        9336 :             return ((rRange.mnMaximum > mnMinimum) && (rRange.mnMinimum < mnMaximum));
     134             :         }
     135             : 
     136           0 :         bool operator==( const BasicRange& rRange ) const
     137             :         {
     138           0 :             return (mnMinimum == rRange.mnMinimum && mnMaximum == rRange.mnMaximum);
     139             :         }
     140             : 
     141             :         bool operator!=( const BasicRange& rRange ) const
     142             :         {
     143             :             return (mnMinimum != rRange.mnMinimum || mnMaximum != rRange.mnMaximum);
     144             :         }
     145             : 
     146             :         bool equal(const BasicRange& rRange) const
     147             :         {
     148             :             return (
     149             :                 fTools::equal(mnMinimum, rRange.mnMinimum) &&
     150             :                 fTools::equal(mnMaximum, rRange.mnMaximum));
     151             :         }
     152             : 
     153     1706084 :         void expand(T nValue)
     154             :         {
     155     1706084 :             if(isEmpty())
     156             :             {
     157      405398 :                 mnMinimum = mnMaximum = nValue;
     158             :             }
     159             :             else
     160             :             {
     161     1300686 :                 if(nValue < mnMinimum)
     162             :                 {
     163      120320 :                     mnMinimum = nValue;
     164             :                 }
     165             : 
     166     1300686 :                 if(nValue > mnMaximum)
     167             :                 {
     168      337903 :                     mnMaximum = nValue;
     169             :                 }
     170             :             }
     171     1706084 :         }
     172             : 
     173      390866 :         void expand(const BasicRange& rRange)
     174             :         {
     175      390866 :             if(isEmpty())
     176             :             {
     177      385146 :                 mnMinimum = rRange.mnMinimum;
     178      385146 :                 mnMaximum = rRange.mnMaximum;
     179             :             }
     180             :             else
     181             :             {
     182        5720 :                 if(!rRange.isEmpty())
     183             :                 {
     184        5720 :                     if(rRange.mnMinimum < mnMinimum)
     185             :                     {
     186        1394 :                         mnMinimum = rRange.mnMinimum;
     187             :                     }
     188             : 
     189        5720 :                     if(rRange.mnMaximum > mnMaximum)
     190             :                     {
     191         536 :                         mnMaximum = rRange.mnMaximum;
     192             :                     }
     193             :                 }
     194             :             }
     195      390866 :         }
     196             : 
     197             :         void intersect(const BasicRange& rRange)
     198             :         {
     199             :             // here, overlaps also tests all isEmpty() conditions already.
     200             :             if( !overlaps( rRange ) )
     201             :             {
     202             :                 reset();
     203             :             }
     204             :             else
     205             :             {
     206             :                 if(rRange.mnMinimum > mnMinimum)
     207             :                 {
     208             :                     mnMinimum = rRange.mnMinimum;
     209             :                 }
     210             : 
     211             :                 if(rRange.mnMaximum < mnMaximum)
     212             :                 {
     213             :                     mnMaximum = rRange.mnMaximum;
     214             :                 }
     215             :             }
     216             :         }
     217             : 
     218             :         void grow(T nValue)
     219             :         {
     220             :             if(!isEmpty())
     221             :             {
     222             :                 bool bLessThanZero(nValue < 0);
     223             : 
     224             :                 if(nValue > 0 || bLessThanZero)
     225             :                 {
     226             :                     mnMinimum -= nValue;
     227             :                     mnMaximum += nValue;
     228             : 
     229             :                     if(bLessThanZero)
     230             :                     {
     231             :                         // test if range did collapse
     232             :                         if(mnMinimum > mnMaximum)
     233             :                         {
     234             :                             // if yes, collapse to center
     235             :                             mnMinimum = mnMaximum = (mnMinimum + mnMaximum) / 2;
     236             :                         }
     237             :                     }
     238             :                 }
     239             :             }
     240             :         }
     241             : 
     242         532 :         typename Traits::DifferenceType getRange() const
     243             :         {
     244         532 :             if(isEmpty())
     245             :             {
     246           0 :                 return Traits::neutral();
     247             :             }
     248             :             else
     249             :             {
     250         532 :                 return (mnMaximum - mnMinimum);
     251             :             }
     252             :         }
     253             :     };
     254             : 
     255             :     // some pre-fabricated traits
     256             :     struct DoubleTraits
     257             :     {
     258      793736 :         static double minVal() { return DBL_MIN; };
     259     3015886 :         static double maxVal() { return DBL_MAX; };
     260           0 :         static double neutral() { return 0.0; };
     261             : 
     262             :         typedef double DifferenceType;
     263             :     };
     264             : 
     265             :     struct Int32Traits
     266             :     {
     267           0 :         static sal_Int32 minVal() { return SAL_MIN_INT32; };
     268           4 :         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 /* _BGFX_RANGE_BASICRANGE_HXX */
     277             : 
     278             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10