/src/libreoffice/i18nutil/source/utility/oneToOneMapping.cxx
Line | Count | Source |
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 i18nutil { |
23 | | |
24 | | oneToOneMapping::oneToOneMapping( OneToOneMappingTable_t const *rpTable, const size_t rnBytes, const size_t rnUnitSize ) |
25 | 0 | : mpTable( rpTable ), |
26 | 0 | mnSize( rnBytes / rnUnitSize ) |
27 | 0 | { |
28 | 0 | } |
29 | | |
30 | | oneToOneMapping::~oneToOneMapping() |
31 | 0 | { |
32 | 0 | } |
33 | | |
34 | | sal_Unicode oneToOneMapping::find(const sal_Unicode nKey) const |
35 | 0 | { |
36 | 0 | if( mpTable ) |
37 | 0 | { |
38 | | // binary search |
39 | 0 | int bottom = 0; |
40 | 0 | int top = mnSize - 1; |
41 | |
|
42 | 0 | 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 | 0 | else |
49 | 0 | return mpTable[current].second; |
50 | | |
51 | 0 | if( bottom > top ) |
52 | 0 | return nKey; |
53 | 0 | } |
54 | 0 | } |
55 | 0 | else |
56 | 0 | return nKey; |
57 | 0 | } |
58 | | |
59 | | oneToOneMappingWithFlag::oneToOneMappingWithFlag( UnicodePairWithFlag const *rpTableWF, const size_t rnSize, const UnicodePairFlag rnFlag ) |
60 | 0 | : oneToOneMapping( nullptr, rnSize, sizeof(UnicodePairWithFlag) ), |
61 | 0 | mpTableWF ( rpTableWF ), |
62 | 0 | mnFlag ( rnFlag ), |
63 | 0 | mbHasIndex( false ) |
64 | 0 | { |
65 | 0 | } |
66 | | |
67 | | oneToOneMappingWithFlag::~oneToOneMappingWithFlag() |
68 | 0 | { |
69 | 0 | } |
70 | | |
71 | | void oneToOneMappingWithFlag::makeIndex() |
72 | 0 | { |
73 | 0 | if( mbHasIndex || !mpTableWF ) |
74 | 0 | return; |
75 | | |
76 | 0 | int current = -1; |
77 | |
|
78 | 0 | for( size_t k = 0; k < mnSize; k++ ) |
79 | 0 | { |
80 | 0 | const int high = (mpTableWF[k].first >> 8) & 0xFF; |
81 | 0 | const int low = (mpTableWF[k].first) & 0xFF; |
82 | 0 | if( high != current ) |
83 | 0 | { |
84 | 0 | current = high; |
85 | 0 | mpIndex[high].reset(new UnicodePairWithFlag const *[256]); |
86 | |
|
87 | 0 | for (int j = 0; j < 256; ++j) |
88 | 0 | mpIndex[high][j] = nullptr; |
89 | 0 | } |
90 | 0 | mpIndex[high][low] = &mpTableWF[k]; |
91 | 0 | } |
92 | |
|
93 | 0 | mbHasIndex = true; |
94 | 0 | } |
95 | | |
96 | | sal_Unicode oneToOneMappingWithFlag::find( const sal_Unicode nKey ) const |
97 | 0 | { |
98 | 0 | if( mpTableWF ) |
99 | 0 | { |
100 | 0 | if( mbHasIndex ) |
101 | 0 | { |
102 | | // index search |
103 | 0 | int high, low; |
104 | 0 | high = (nKey >> 8) & 0xFF; |
105 | 0 | low = nKey & 0xFF; |
106 | 0 | if( mpIndex[high] != nullptr && |
107 | 0 | mpIndex[high][low] != nullptr && |
108 | 0 | mpIndex[high][low]->flag & mnFlag ) |
109 | 0 | return mpIndex[high][low]->second; |
110 | 0 | else |
111 | 0 | return nKey; |
112 | 0 | } |
113 | 0 | else |
114 | 0 | { |
115 | | // binary search |
116 | 0 | int bottom = 0; |
117 | 0 | int top = mnSize - 1; |
118 | |
|
119 | 0 | for (;;) { |
120 | 0 | const int current = (top + bottom) / 2; |
121 | 0 | if( nKey < mpTableWF[current].first ) |
122 | 0 | top = current - 1; |
123 | 0 | else if( nKey > mpTableWF[current].first ) |
124 | 0 | bottom = current + 1; |
125 | 0 | else |
126 | 0 | { |
127 | 0 | if( mpTableWF[current].flag & mnFlag ) |
128 | 0 | return mpTableWF[current].second; |
129 | 0 | else |
130 | 0 | return nKey; |
131 | 0 | } |
132 | | |
133 | 0 | if( bottom > top ) |
134 | 0 | return nKey; |
135 | 0 | } |
136 | 0 | } |
137 | 0 | } |
138 | 0 | else |
139 | 0 | return nKey; |
140 | 0 | } |
141 | | |
142 | | |
143 | | } |
144 | | |
145 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |