提交 a2829971 编写于 作者: S Shengliang Guan

losertree

上级 9735dcf9
...@@ -13,14 +13,16 @@ ...@@ -13,14 +13,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _TD_UTIL_LOSERTREE_H #ifndef _TD_UTIL_LOSERTREE_H_
#define _TD_UTIL_LOSERTREE_H #define _TD_UTIL_LOSERTREE_H_
#include "os.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef int (*__merge_compare_fn_t)(const void *, const void *, void *param); typedef int32_t (*__merge_compare_fn_t)(const void *, const void *, void *param);
typedef struct STreeNode { typedef struct STreeNode {
int32_t index; int32_t index;
...@@ -31,16 +33,17 @@ typedef struct SMultiwayMergeTreeInfo { ...@@ -31,16 +33,17 @@ typedef struct SMultiwayMergeTreeInfo {
int32_t numOfSources; int32_t numOfSources;
int32_t totalSources; int32_t totalSources;
__merge_compare_fn_t comparFn; __merge_compare_fn_t comparFn;
void * param; void *param;
struct STreeNode *pNode; struct STreeNode *pNode;
} SMultiwayMergeTreeInfo; } SMultiwayMergeTreeInfo;
#define tMergeTreeGetChosenIndex(t_) ((t_)->pNode[0].index) #define tMergeTreeGetChosenIndex(t_) ((t_)->pNode[0].index)
#define tMergeTreeGetAdjustIndex(t_) (tMergeTreeGetChosenIndex(t_) + (t_)->numOfSources) #define tMergeTreeGetAdjustIndex(t_) (tMergeTreeGetChosenIndex(t_) + (t_)->numOfSources)
int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo **pTree, uint32_t numOfEntries, void *param, __merge_compare_fn_t compareFn); int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo **pTree, uint32_t numOfEntries, void *param,
__merge_compare_fn_t compareFn);
void tMergeTreeDestroy(SMultiwayMergeTreeInfo* pTree); void tMergeTreeDestroy(SMultiwayMergeTreeInfo *pTree);
void tMergeTreeAdjust(SMultiwayMergeTreeInfo *pTree, int32_t idx); void tMergeTreeAdjust(SMultiwayMergeTreeInfo *pTree, int32_t idx);
...@@ -52,4 +55,4 @@ void tMergeTreePrint(const SMultiwayMergeTreeInfo *pTree); ...@@ -52,4 +55,4 @@ void tMergeTreePrint(const SMultiwayMergeTreeInfo *pTree);
} }
#endif #endif
#endif /*_TD_UTIL_LOSERTREE_H*/ #endif /*_TD_UTIL_LOSERTREE_H_*/
...@@ -24,7 +24,7 @@ extern "C" { ...@@ -24,7 +24,7 @@ extern "C" {
// Module init/clear MACRO definitions // Module init/clear MACRO definitions
#define TD_MOD_UNINITIALIZED 0 #define TD_MOD_UNINITIALIZED 0
#define TD_MOD_INITIALIZED 1 #define TD_MOD_INITIALIZED 1
typedef int8_t td_mode_flag_t; typedef int8_t td_mode_flag_t;
......
...@@ -13,12 +13,10 @@ ...@@ -13,12 +13,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "os.h" #define _DEFAULT_SOURCE
#include "tlog.h"
#include "tlosertree.h" #include "tlosertree.h"
#include "taoserror.h" #include "taoserror.h"
#include "tlog.h"
// Set the initial value of the multiway merge tree. // Set the initial value of the multiway merge tree.
static void tMergeTreeInit(SMultiwayMergeTreeInfo* pTree) { static void tMergeTreeInit(SMultiwayMergeTreeInfo* pTree) {
...@@ -33,10 +31,12 @@ static void tMergeTreeInit(SMultiwayMergeTreeInfo* pTree) { ...@@ -33,10 +31,12 @@ static void tMergeTreeInit(SMultiwayMergeTreeInfo* pTree) {
} }
} }
int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo** pTree, uint32_t numOfSources, void* param, __merge_compare_fn_t compareFn) { int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo** pTree, uint32_t numOfSources, void* param,
__merge_compare_fn_t compareFn) {
int32_t totalEntries = numOfSources << 1u; int32_t totalEntries = numOfSources << 1u;
SMultiwayMergeTreeInfo* pTreeInfo = (SMultiwayMergeTreeInfo*)calloc(1, sizeof(SMultiwayMergeTreeInfo) + sizeof(STreeNode) * totalEntries); SMultiwayMergeTreeInfo* pTreeInfo =
(SMultiwayMergeTreeInfo*)calloc(1, sizeof(SMultiwayMergeTreeInfo) + sizeof(STreeNode) * totalEntries);
if (pTreeInfo == NULL) { if (pTreeInfo == NULL) {
uError("allocate memory for loser-tree failed. reason:%s", strerror(errno)); uError("allocate memory for loser-tree failed. reason:%s", strerror(errno));
return TAOS_SYSTEM_ERROR(errno); return TAOS_SYSTEM_ERROR(errno);
...@@ -88,7 +88,7 @@ void tMergeTreeAdjust(SMultiwayMergeTreeInfo* pTree, int32_t idx) { ...@@ -88,7 +88,7 @@ void tMergeTreeAdjust(SMultiwayMergeTreeInfo* pTree, int32_t idx) {
return; return;
} }
int32_t parentId = idx >> 1; int32_t parentId = idx >> 1;
STreeNode kLeaf = pTree->pNode[idx]; STreeNode kLeaf = pTree->pNode[idx];
while (parentId > 0) { while (parentId > 0) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册