Skip to content

Commit 9aa4802

Browse files
committed
Use Binary Search instead of Linear search to reduce time complexity.
Add Key Property for debugging.
1 parent 1e016d7 commit 9aa4802

File tree

1 file changed

+58
-12
lines changed

1 file changed

+58
-12
lines changed

Flow.Launcher.Infrastructure/PinyinAlphabet.cs

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public class TranslationMapping
1818
private List<int> translatedIndexs = new List<int>();
1919
private int translaedLength = 0;
2020

21+
public string key { get; private set; }
22+
23+
public void setKey(string key)
24+
{
25+
this.key = key;
26+
}
27+
2128
public void AddNewIndex(int originalIndex, int translatedIndex, int length)
2229
{
2330
if (constructed)
@@ -29,28 +36,64 @@ public void AddNewIndex(int originalIndex, int translatedIndex, int length)
2936
translaedLength += length - 1;
3037
}
3138

32-
public int? MapToOriginalIndex(int translatedIndex)
39+
public int MapToOriginalIndex(int translatedIndex)
3340
{
3441
if (translatedIndex > translatedIndexs.Last())
3542
return translatedIndex - translaedLength - 1;
36-
37-
for (var i = 0; i < originalIndexs.Count; i++)
43+
44+
int lowerBound = 0;
45+
int upperBound = originalIndexs.Count - 1;
46+
47+
int count = 0;
48+
49+
50+
// Corner case handle
51+
if (translatedIndex < translatedIndexs[0])
52+
return translatedIndex;
53+
if (translatedIndex > translatedIndexs.Last())
54+
{
55+
int indexDef = 0;
56+
for (int k = 0; k < originalIndexs.Count; k++)
57+
{
58+
indexDef += translatedIndexs[k * 2 + 1] - translatedIndexs[k * 2];
59+
}
60+
61+
return translatedIndex - indexDef - 1;
62+
}
63+
64+
// Binary Search with Range
65+
for (int i = originalIndexs.Count / 2;; count++)
3866
{
39-
if (translatedIndex >= translatedIndexs[i * 2] && translatedIndex < translatedIndexs[i * 2 + 1])
40-
return originalIndexs[i];
4167
if (translatedIndex < translatedIndexs[i * 2])
4268
{
43-
int indexDiff = 0;
44-
for (int j = 0; j < i; j++)
69+
// move to lower middle
70+
upperBound = i;
71+
i = (i + lowerBound) / 2;
72+
}
73+
else if (translatedIndex > translatedIndexs[i * 2 + 1] - 1)
74+
{
75+
lowerBound = i;
76+
// move to upper middle
77+
// due to floor of integer division, move one up on corner case
78+
i = (i + upperBound + 1) / 2;
79+
}
80+
else
81+
return originalIndexs[i];
82+
83+
if (upperBound - lowerBound <= 1 &&
84+
translatedIndex > translatedIndexs[lowerBound * 2 + 1] &&
85+
translatedIndex < translatedIndexs[upperBound * 2])
86+
{
87+
int indexDef = 0;
88+
89+
for (int j = 0; j < upperBound; j++)
4590
{
46-
indexDiff += translatedIndexs[i * 2 + 1] - translatedIndexs[i * 2] - 1;
91+
indexDef += translatedIndexs[j * 2 + 1] - translatedIndexs[j * 2];
4792
}
4893

49-
return translatedIndex - indexDiff;
94+
return translatedIndex - indexDef - 1;
5095
}
5196
}
52-
53-
return translatedIndex;
5497
}
5598

5699
public void endConstruct()
@@ -117,7 +160,10 @@ public void Initialize([NotNull] Settings settings)
117160

118161
map.endConstruct();
119162

120-
return _pinyinCache[content] = (resultBuilder.ToString(), map);
163+
var key = resultBuilder.ToString();
164+
map.setKey(key);
165+
166+
return _pinyinCache[content] = (key, map);
121167
}
122168
else
123169
{

0 commit comments

Comments
 (0)