LCOV - code coverage report
Current view: top level - i18nutil/source/utility - oneToOneMapping.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 40 68 58.8 %
Date: 2015-06-13 12:38:46 Functions: 6 9 66.7 %
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             : #include <i18nutil/oneToOneMapping.hxx>
      21             : 
      22             : namespace com { namespace sun { namespace star { namespace i18n {
      23             : 
      24          97 : oneToOneMapping::oneToOneMapping( OneToOneMappingTable_t *rpTable, const size_t rnBytes, const size_t rnUnitSize )
      25             :     : mpTable( rpTable ),
      26          97 :       mnSize( rnBytes / rnUnitSize )
      27             : {
      28          97 : }
      29             : 
      30          97 : oneToOneMapping::~oneToOneMapping()
      31             : {
      32          97 : }
      33             : 
      34           0 : sal_Unicode oneToOneMapping::find(const sal_Unicode nKey) const
      35             : {
      36           0 :     if( mpTable )
      37             :     {
      38             :         // binary search
      39           0 :         int bottom = 0;
      40           0 :         int top = mnSize - 1;
      41             : 
      42             :         for (;;) {
      43           0 :             const int current = (top + bottom) / 2;
      44           0 :             if( nKey < mpTable[current].first )
      45           0 :                 top = current - 1;
      46           0 :             else if( nKey > mpTable[current].first )
      47           0 :                 bottom = current + 1;
      48             :             else
      49           0 :                 return mpTable[current].second;
      50             : 
      51           0 :             if( bottom > top )
      52           0 :                 return sal_Unicode( nKey );
      53           0 :         }
      54             :     }
      55             :     else
      56           0 :         return sal_Unicode( nKey );
      57             : }
      58             : 
      59          97 : oneToOneMappingWithFlag::oneToOneMappingWithFlag( UnicodePairWithFlag *rpTableWF, const size_t rnSize, const UnicodePairFlag rnFlag )
      60             :     : oneToOneMapping( NULL, rnSize, sizeof(UnicodePairWithFlag) ),
      61             :       mpTableWF ( rpTableWF ),
      62             :       mnFlag    ( rnFlag ),
      63          97 :       mbHasIndex( false )
      64             : {
      65          97 :     memset(mpIndex, 0, sizeof(mpIndex));
      66          97 : }
      67             : 
      68         194 : oneToOneMappingWithFlag::~oneToOneMappingWithFlag()
      69             : {
      70          97 :     if( mbHasIndex )
      71             :     {
      72       24929 :         for (size_t i = 0; i < SAL_N_ELEMENTS(mpIndex); ++i)
      73       24832 :             delete [] mpIndex[i];
      74             :     }
      75          97 : }
      76             : 
      77         186 : void oneToOneMappingWithFlag::makeIndex()
      78             : {
      79         186 :     if( !mbHasIndex && mpTableWF )
      80             :     {
      81          97 :         int current = -1;
      82             : 
      83       24929 :         for (size_t i = 0; i < SAL_N_ELEMENTS(mpIndex); ++i)
      84       24832 :             mpIndex[i] = NULL;
      85             : 
      86       21770 :         for( size_t k = 0; k < mnSize; k++ )
      87             :         {
      88       21673 :             const int high = (mpTableWF[k].first >> 8) & 0xFF;
      89       21673 :             const int low  = (mpTableWF[k].first)      & 0xFF;
      90       21673 :             if( high != current )
      91             :             {
      92         309 :                 current = high;
      93         309 :                 mpIndex[high] = new UnicodePairWithFlag*[256];
      94             : 
      95       79413 :                 for (int j = 0; j < 256; ++j)
      96       79104 :                     mpIndex[high][j] = NULL;
      97             :             }
      98       21673 :             mpIndex[high][low] = &mpTableWF[k];
      99             :         }
     100             : 
     101          97 :         mbHasIndex = true;
     102             :     }
     103         186 : }
     104             : 
     105      118332 : sal_Unicode oneToOneMappingWithFlag::find( const sal_Unicode nKey ) const
     106             : {
     107      118332 :     if( mpTableWF )
     108             :     {
     109      118332 :         if( mbHasIndex )
     110             :         {
     111             :             // index search
     112             :             int high, low;
     113      118332 :             high = (nKey >> 8) & 0xFF;
     114      118332 :             low = nKey & 0xFF;
     115      234778 :             if( mpIndex[high] != NULL &&
     116      231226 :                 mpIndex[high][low] != NULL &&
     117      114780 :                 mpIndex[high][low]->flag & mnFlag )
     118      114780 :                 return mpIndex[high][low]->second;
     119             :             else
     120        3552 :                 return sal_Unicode( nKey );
     121             :         }
     122             :         else
     123             :         {
     124             :             // binary search
     125           0 :             int bottom = 0;
     126           0 :             int top = mnSize - 1;
     127             : 
     128             :             for (;;) {
     129           0 :                 const int current = (top + bottom) / 2;
     130           0 :                 if( nKey < mpTableWF[current].first )
     131           0 :                     top = current - 1;
     132           0 :                 else if( nKey > mpTableWF[current].first )
     133           0 :                     bottom = current + 1;
     134             :                 else
     135             :                 {
     136           0 :                     if( mpTableWF[current].flag & mnFlag )
     137           0 :                         return mpTableWF[current].second;
     138             :                     else
     139           0 :                         return sal_Unicode( nKey );
     140             :                 }
     141             : 
     142           0 :                 if( bottom > top )
     143           0 :                     return sal_Unicode( nKey );
     144           0 :             }
     145             :         }
     146             :     }
     147             :     else
     148           0 :         return sal_Unicode( nKey );
     149             : }
     150             : 
     151             : 
     152             : } } } }
     153             : 
     154             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11