update dev300-m58
[ooovba.git] / i18nutil / source / utility / oneToOneMapping.cxx
blob417b27548df81ee59aa8c705651cd2447ab18c8a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: oneToOneMapping.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <i18nutil/oneToOneMapping.hxx>
33 namespace com { namespace sun { namespace star { namespace i18n {
35 oneToOneMapping::oneToOneMapping( OneToOneMappingTable_t *rpTable, const size_t rnBytes, const size_t rnUnitSize )
36 : mpTable( rpTable ),
37 mnSize( rnBytes / rnUnitSize )
41 oneToOneMapping::~oneToOneMapping()
45 sal_Unicode oneToOneMapping::find(const sal_Unicode nKey) const
47 if( mpTable )
49 // binary search
50 int bottom = 0;
51 int top = mnSize - 1;
52 int current;
54 for (;;) {
55 current = (top + bottom) / 2;
56 if( nKey < mpTable[current].first )
57 top = current - 1;
58 else if( nKey > mpTable[current].first )
59 bottom = current + 1;
60 else
61 return mpTable[current].second;
63 if( bottom > top )
64 return sal_Unicode( nKey );
67 else
68 return sal_Unicode( nKey );
71 oneToOneMappingWithFlag::oneToOneMappingWithFlag( UnicodePairWithFlag *rpTableWF, const size_t rnSize, const UnicodePairFlag rnFlag )
72 : oneToOneMapping( NULL, rnSize, sizeof(UnicodePairWithFlag) ),
73 mpTableWF ( rpTableWF ),
74 mnFlag ( rnFlag ),
75 mbHasIndex( sal_False )
79 oneToOneMappingWithFlag::~oneToOneMappingWithFlag()
81 if( mbHasIndex )
82 for( int i = 0; i < 256; i++ )
83 if( mpIndex[i] )
84 delete [] mpIndex[i];
88 void oneToOneMappingWithFlag::makeIndex()
90 if( !mbHasIndex && mpTableWF )
92 int i, j, high, low, current = -1;
94 for( i = 0; i < 256; i++ )
95 mpIndex[i] = NULL;
97 for( size_t k = 0; k < mnSize; k++ )
99 high = (mpTableWF[k].first >> 8) & 0xFF;
100 low = (mpTableWF[k].first) & 0xFF;
101 if( high != current )
103 current = high;
104 mpIndex[high] = new UnicodePairWithFlag*[256];
106 for( j = 0; j < 256; j++ )
107 mpIndex[high][j] = NULL;
109 mpIndex[high][low] = &mpTableWF[k];
112 mbHasIndex = sal_True;
116 sal_Unicode oneToOneMappingWithFlag::find( const sal_Unicode nKey ) const
118 if( mpTableWF )
120 if( mbHasIndex )
122 // index search
123 int high, low;
124 high = (nKey >> 8) & 0xFF;
125 low = nKey & 0xFF;
126 if( mpIndex[high] != NULL &&
127 mpIndex[high][low] != NULL &&
128 mpIndex[high][low]->flag & mnFlag )
129 return mpIndex[high][low]->second;
130 else
131 return sal_Unicode( nKey );
133 else
135 // binary search
136 int bottom = 0;
137 int top = mnSize - 1;
138 int current;
140 for (;;) {
141 current = (top + bottom) / 2;
142 if( nKey < mpTableWF[current].first )
143 top = current - 1;
144 else if( nKey > mpTableWF[current].first )
145 bottom = current + 1;
146 else
148 if( mpTableWF[current].flag & mnFlag )
149 return mpTableWF[current].second;
150 else
151 return sal_Unicode( nKey );
154 if( bottom > top )
155 return sal_Unicode( nKey );
159 else
160 return sal_Unicode( nKey );
164 } } } }