-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseDictionaryEngine.cpp
120 lines (100 loc) · 4.16 KB
/
BaseDictionaryEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
#include "Private.h"
#include "BaseDictionaryEngine.h"
#include "Globals.h"
//+---------------------------------------------------------------------------
// ctor
//----------------------------------------------------------------------------
CBaseDictionaryEngine::CBaseDictionaryEngine(LCID locale, _In_ CFile *pDictionaryFile)
{
_locale = locale;
_pDictionaryFile = pDictionaryFile;
}
//+---------------------------------------------------------------------------
// dtor
//----------------------------------------------------------------------------
CBaseDictionaryEngine::~CBaseDictionaryEngine()
{
}
//+---------------------------------------------------------------------------
// SortListItemByFindKeyCode
//----------------------------------------------------------------------------
VOID CBaseDictionaryEngine::SortListItemByFindKeyCode(_Inout_ CSampleImeArray<CCandidateListItem> *pItemList)
{
MergeSortByFindKeyCode(pItemList, 0, pItemList->Count() - 1);
}
//+---------------------------------------------------------------------------
// MergeSortByFindKeyCode
//
// Mergesort the array of element in CCandidateListItem::_FindKeyCode
//
//----------------------------------------------------------------------------
VOID CBaseDictionaryEngine::MergeSortByFindKeyCode(_Inout_ CSampleImeArray<CCandidateListItem> *pItemList,
int leftRange, int rightRange)
{
int candidateCount = CalculateCandidateCount(leftRange, rightRange);
if (candidateCount > 2)
{
int mid = leftRange + (candidateCount / 2);
MergeSortByFindKeyCode(pItemList, leftRange, mid);
MergeSortByFindKeyCode(pItemList, mid, rightRange);
CSampleImeArray<CCandidateListItem> ListItemTemp;
int leftRangeTemp = 0;
int midTemp = 0;
for (leftRangeTemp = leftRange, midTemp = mid; leftRangeTemp != mid || midTemp != rightRange;)
{
CStringRange *psrgLeftTemp = nullptr;
CStringRange *psrgMidTemp = nullptr;
psrgLeftTemp = &pItemList->GetAt(leftRangeTemp)->_FindKeyCode;
psrgMidTemp = &pItemList->GetAt(midTemp)->_FindKeyCode;
CCandidateListItem *pLI = nullptr;
pLI = ListItemTemp.Append();
if (pLI)
{
if (leftRangeTemp == mid)
{
*pLI = *pItemList->GetAt(midTemp++);
}
else if (midTemp == rightRange ||
CStringRange::Compare(_locale, psrgLeftTemp, psrgMidTemp) != CSTR_GREATER_THAN)
{
*pLI = *pItemList->GetAt(leftRangeTemp++);
}
else
{
*pLI = *pItemList->GetAt(midTemp++);
}
}
}
leftRangeTemp = leftRange;
for (UINT count = 0; count < ListItemTemp.Count(); count++)
{
*pItemList->GetAt(leftRangeTemp++) = *ListItemTemp.GetAt(count);
}
}
else if (candidateCount == 2)
{
CStringRange *psrgLeft = nullptr;
CStringRange *psrgLeftNext = nullptr;
psrgLeft = &pItemList->GetAt(leftRange)->_FindKeyCode;
psrgLeftNext = &pItemList->GetAt(leftRange + 1)->_FindKeyCode;
if (CStringRange::Compare(_locale, psrgLeft, psrgLeftNext) == CSTR_GREATER_THAN)
{
CCandidateListItem ListItem;
ListItem = *pItemList->GetAt(leftRange);
*pItemList->GetAt(leftRange) = *pItemList->GetAt(leftRange + 1);
*pItemList->GetAt(leftRange + 1) = ListItem;
}
}
}
int CBaseDictionaryEngine::CalculateCandidateCount(int leftRange, int rightRange)
{
assert(leftRange >= 0);
assert(rightRange >= 0);
return (rightRange - leftRange + 1);
}