提交 bc2c2c07 编写于 作者: X xywang

[TD-2574]<enhance>: refactored algorithms for top and bottom functions.

上级 43fd9b58
......@@ -2201,6 +2201,101 @@ static void valuePairAssign(tValuePair *dst, int16_t type, const char *val, int6
memcpy((dst)->pTags, (src)->pTags, (size_t)(__l)); \
} while (0)
static void heapSwap(tValuePair *a, tValuePair *b, const int16_t tagLen) {
char tag[32768];
tValuePair temp;
memset(tag, 0, sizeof(tag));
temp.pTags = tag;
VALUEPAIRASSIGN(&temp, a, tagLen);
VALUEPAIRASSIGN(a, b, tagLen);
VALUEPAIRASSIGN(b, &temp, tagLen);
}
static void heapAdjust(tValuePair **pList, uint16_t type, int16_t tagLen, int32_t start, int32_t end, bool minRoot) {
int32_t parent = start;
int32_t child = 2 * parent + 1;
while (child <= end) {
if (IS_SIGNED_NUMERIC_TYPE(type)) {
if (minRoot) {
if (child + 1 <= end && pList[child]->v.i64 < pList[child + 1]->v.i64) {
child++;
}
if (pList[parent]->v.i64 > pList[child]->v.i64) {
break;
}
} else {
if (child + 1 <= end && pList[child]->v.i64 >= pList[child + 1]->v.i64) {
child++;
}
if (pList[parent]->v.i64 <= pList[child]->v.i64) {
break;
}
}
} else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
if (minRoot) {
if (child + 1 <= end && pList[child]->v.u64 < pList[child + 1]->v.u64) {
child++;
}
if (pList[parent]->v.u64 > pList[child]->v.u64) {
break;
}
} else {
if (child + 1 <= end && pList[child]->v.u64 >= pList[child + 1]->v.u64) {
child++;
}
if (pList[parent]->v.u64 <= pList[child]->v.u64) {
break;
}
}
} else {
if (minRoot) {
if (child + 1 <= end && pList[child]->v.dKey < pList[child + 1]->v.dKey) {
child++;
}
if (pList[parent]->v.dKey > pList[child]->v.dKey) {
break;
}
} else {
if (child + 1 <= end && pList[child]->v.dKey >= pList[child + 1]->v.dKey) {
child++;
}
if (pList[parent]->v.dKey <= pList[child]->v.dKey) {
break;
}
}
}
heapSwap(pList[parent], pList[child], tagLen);
parent = child;
child = parent * 2 + 1;
}
}
void heapSort(tValuePair **pList, uint16_t type, int16_t tagLen, int32_t len, bool minRoot) {
int32_t i;
for (i = len / 2 - 1; i >= 0; i--) {
heapAdjust(pList, type, i, tagLen, len - 1, minRoot);
}
/*
for (i = len - 1; i > 0; i--) {
heapSwap(pList[0], pList[i], tagsLen);
heapAdjust(pList, type, tagsLen, i - 1, minRoot);
}
*/
}
static void do_top_function_add(STopBotInfo *pInfo, int32_t maxLen, void *pData, int64_t ts, uint16_t type,
SExtTagsInfo *pTagInfo, char *pTags, int16_t stage) {
tVariant val = {0};
......@@ -2210,59 +2305,17 @@ static void do_top_function_add(STopBotInfo *pInfo, int32_t maxLen, void *pData,
assert(pList != NULL);
if (pInfo->num < maxLen) {
if (pInfo->num == 0 ||
(IS_SIGNED_NUMERIC_TYPE(type) && val.i64 >= pList[pInfo->num - 1]->v.i64) ||
(IS_UNSIGNED_NUMERIC_TYPE(type) && val.u64 >= pList[pInfo->num - 1]->v.u64) ||
(IS_FLOAT_TYPE(type) && val.dKey >= pList[pInfo->num - 1]->v.dKey)) {
valuePairAssign(pList[pInfo->num], type, (const char*)&val.i64, ts, pTags, pTagInfo, stage);
} else {
int32_t i = pInfo->num - 1;
if (IS_SIGNED_NUMERIC_TYPE(type)) {
while (i >= 0 && pList[i]->v.i64 > val.i64) {
VALUEPAIRASSIGN(pList[i + 1], pList[i], pTagInfo->tagsLen);
i -= 1;
}
} else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
while (i >= 0 && pList[i]->v.u64 > val.u64) {
VALUEPAIRASSIGN(pList[i + 1], pList[i], pTagInfo->tagsLen);
i -= 1;
}
} else {
while (i >= 0 && pList[i]->v.dKey > val.dKey) {
VALUEPAIRASSIGN(pList[i + 1], pList[i], pTagInfo->tagsLen);
i -= 1;
}
}
valuePairAssign(pList[i + 1], type, (const char*) &val.i64, ts, pTags, pTagInfo, stage);
}
valuePairAssign(pList[pInfo->num], type, (const char *)&val.i64, ts, pTags, pTagInfo, stage);
heapSort(pList, type, pTagInfo->tagsLen, pInfo->num + 1, 0);
pInfo->num++;
} else {
int32_t i = 0;
if ((IS_SIGNED_NUMERIC_TYPE(type) && val.i64 > pList[0]->v.i64) ||
(IS_UNSIGNED_NUMERIC_TYPE(type) && val.u64 > pList[0]->v.u64) ||
(IS_FLOAT_TYPE(type) && val.dKey > pList[0]->v.dKey)) {
// find the appropriate the slot position
if (IS_SIGNED_NUMERIC_TYPE(type)) {
while (i + 1 < maxLen && pList[i + 1]->v.i64 < val.i64) {
VALUEPAIRASSIGN(pList[i], pList[i + 1], pTagInfo->tagsLen);
i += 1;
}
} if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
while (i + 1 < maxLen && pList[i + 1]->v.u64 < val.u64) {
VALUEPAIRASSIGN(pList[i], pList[i + 1], pTagInfo->tagsLen);
i += 1;
}
} else {
while (i + 1 < maxLen && pList[i + 1]->v.dKey < val.dKey) {
VALUEPAIRASSIGN(pList[i], pList[i + 1], pTagInfo->tagsLen);
i += 1;
}
}
valuePairAssign(pList[i], type, (const char *)&val.i64, ts, pTags, pTagInfo, stage);
valuePairAssign(pList[0], type, (const char *)&val.i64, ts, pTags, pTagInfo, stage);
heapAdjust(pList, type, pTagInfo->tagsLen, 0, maxLen - 1, 0);
}
}
}
......@@ -2276,57 +2329,17 @@ static void do_bottom_function_add(STopBotInfo *pInfo, int32_t maxLen, void *pDa
assert(pList != NULL);
if (pInfo->num < maxLen) {
if (pInfo->num == 0) {
valuePairAssign(pList[pInfo->num], type, (const char*) &val.i64, ts, pTags, pTagInfo, stage);
} else {
int32_t i = pInfo->num - 1;
if (IS_SIGNED_NUMERIC_TYPE(type)) {
while (i >= 0 && pList[i]->v.i64 < val.i64) {
VALUEPAIRASSIGN(pList[i + 1], pList[i], pTagInfo->tagsLen);
i -= 1;
}
} else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
while (i >= 0 && pList[i]->v.u64 < val.u64) {
VALUEPAIRASSIGN(pList[i + 1], pList[i], pTagInfo->tagsLen);
i -= 1;
}
} else {
while (i >= 0 && pList[i]->v.dKey < val.dKey) {
VALUEPAIRASSIGN(pList[i + 1], pList[i], pTagInfo->tagsLen);
i -= 1;
}
}
valuePairAssign(pList[i + 1], type, (const char*)&val.i64, ts, pTags, pTagInfo, stage);
}
valuePairAssign(pList[pInfo->num], type, (const char *)&val.i64, ts, pTags, pTagInfo, stage);
heapSort(pList, type, pTagInfo->tagsLen, pInfo->num + 1, 1);
pInfo->num++;
} else {
int32_t i = 0;
if ((IS_SIGNED_NUMERIC_TYPE(type) && val.i64 < pList[0]->v.i64) ||
(IS_UNSIGNED_NUMERIC_TYPE(type) && val.u64 < pList[0]->v.u64) ||
(IS_FLOAT_TYPE(type) && val.dKey < pList[0]->v.dKey)) {
// find the appropriate the slot position
if (IS_SIGNED_NUMERIC_TYPE(type)) {
while (i + 1 < maxLen && pList[i + 1]->v.i64 > val.i64) {
VALUEPAIRASSIGN(pList[i], pList[i + 1], pTagInfo->tagsLen);
i += 1;
}
} if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
while (i + 1 < maxLen && pList[i + 1]->v.u64 > val.u64) {
VALUEPAIRASSIGN(pList[i], pList[i + 1], pTagInfo->tagsLen);
i += 1;
}
} else {
while (i + 1 < maxLen && pList[i + 1]->v.dKey > val.dKey) {
VALUEPAIRASSIGN(pList[i], pList[i + 1], pTagInfo->tagsLen);
i += 1;
}
}
valuePairAssign(pList[i], type, (const char*)&val.i64, ts, pTags, pTagInfo, stage);
valuePairAssign(pList[0], type, (const char *)&val.i64, ts, pTags, pTagInfo, stage);
heapAdjust(pList, type, pTagInfo->tagsLen, 0, maxLen - 1, 1);
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册