Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
6494ed76
TDengine
项目概览
taosdata
/
TDengine
大约 2 年 前同步成功
通知
1192
Star
22018
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
6494ed76
编写于
2月 28, 2022
作者:
S
Shengliang Guan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
talgo
上级
c7e5ee34
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
82 addition
and
74 deletion
+82
-74
include/util/talgo.h
include/util/talgo.h
+14
-10
source/util/src/talgo.c
source/util/src/talgo.c
+68
-64
未找到文件。
include/util/talgo.h
浏览文件 @
6494ed76
...
@@ -13,16 +13,18 @@
...
@@ -13,16 +13,18 @@
* 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_TALGO_H
#ifndef _TD_UTIL_TALGO_H_
#define _TD_UTIL_TALGO_H
#define _TD_UTIL_TALGO_H_
#include "os.h"
#ifdef __cplusplus
#ifdef __cplusplus
extern
"C"
{
extern
"C"
{
#endif
#endif
#ifndef __COMPAR_FN_T
#ifndef __COMPAR_FN_T
#
define __COMPAR_FN_T
#define __COMPAR_FN_T
typedef
int
(
*
__compar_fn_t
)
(
const
void
*
,
const
void
*
);
typedef
int
32_t
(
*
__compar_fn_t
)
(
const
void
*
,
const
void
*
);
#endif
#endif
#define TD_EQ 0x1
#define TD_EQ 0x1
...
@@ -45,7 +47,7 @@ typedef void (*__ext_swap_fn_t)(void *p1, void *p2, const void *param);
...
@@ -45,7 +47,7 @@ typedef void (*__ext_swap_fn_t)(void *p1, void *p2, const void *param);
* @param param
* @param param
* @param comparFn
* @param comparFn
*/
*/
void
taosqsort
(
void
*
src
,
size_t
numOfElem
,
size_t
size
,
const
void
*
param
,
__ext_compar_fn_t
comparFn
);
void
taosqsort
(
void
*
src
,
int64_t
numOfElem
,
int64_t
size
,
const
void
*
param
,
__ext_compar_fn_t
comparFn
);
/**
/**
* binary search, with range support
* binary search, with range support
...
@@ -58,7 +60,7 @@ void taosqsort(void *src, size_t numOfElem, size_t size, const void* param, __ex
...
@@ -58,7 +60,7 @@ void taosqsort(void *src, size_t numOfElem, size_t size, const void* param, __ex
* @param flags
* @param flags
* @return
* @return
*/
*/
void
*
taosbsearch
(
const
void
*
key
,
const
void
*
base
,
size_t
nmemb
,
size_t
size
,
__compar_fn_t
fn
,
in
t
flags
);
void
*
taosbsearch
(
const
void
*
key
,
const
void
*
base
,
int64_t
nmemb
,
int64_t
size
,
__compar_fn_t
fn
,
int32_
t
flags
);
/**
/**
* adjust heap
* adjust heap
...
@@ -74,7 +76,8 @@ void *taosbsearch(const void *key, const void *base, size_t nmemb, size_t size,
...
@@ -74,7 +76,8 @@ void *taosbsearch(const void *key, const void *base, size_t nmemb, size_t size,
* @param maxroot: if heap is max root heap
* @param maxroot: if heap is max root heap
* @return
* @return
*/
*/
void
taosheapadjust
(
void
*
base
,
int32_t
size
,
int32_t
start
,
int32_t
end
,
const
void
*
parcompar
,
__ext_compar_fn_t
compar
,
const
void
*
parswap
,
__ext_swap_fn_t
swap
,
bool
maxroot
);
void
taosheapadjust
(
void
*
base
,
int32_t
size
,
int32_t
start
,
int32_t
end
,
const
void
*
parcompar
,
__ext_compar_fn_t
compar
,
const
void
*
parswap
,
__ext_swap_fn_t
swap
,
bool
maxroot
);
/**
/**
* sort heap to make sure it is a max/min root heap
* sort heap to make sure it is a max/min root heap
...
@@ -89,10 +92,11 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const
...
@@ -89,10 +92,11 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const
* @param maxroot: if heap is max root heap
* @param maxroot: if heap is max root heap
* @return
* @return
*/
*/
void
taosheapsort
(
void
*
base
,
int32_t
size
,
int32_t
len
,
const
void
*
parcompar
,
__ext_compar_fn_t
compar
,
const
void
*
parswap
,
__ext_swap_fn_t
swap
,
bool
maxroot
);
void
taosheapsort
(
void
*
base
,
int32_t
size
,
int32_t
len
,
const
void
*
parcompar
,
__ext_compar_fn_t
compar
,
const
void
*
parswap
,
__ext_swap_fn_t
swap
,
bool
maxroot
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
#endif
#endif
#endif
/*_TD_UTIL_TALGO_H*/
#endif
/*_TD_UTIL_TALGO_H_*/
source/util/src/talgo.c
浏览文件 @
6494ed76
...
@@ -13,16 +13,18 @@
...
@@ -13,16 +13,18 @@
* 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 "talgo.h"
#include "talgo.h"
#define doswap(__left, __right, __size, __buf) do {\
#define doswap(__left, __right, __size, __buf) \
memcpy((__buf), (__left), (__size));\
do { \
memcpy((__left), (__right),(__size));\
memcpy((__buf), (__left), (__size)); \
memcpy((__right), (__buf), (__size));\
memcpy((__left), (__right), (__size)); \
} while (0);
memcpy((__right), (__buf), (__size)); \
} while (0);
static
void
median
(
void
*
src
,
size_t
size
,
size_t
s
,
size_t
e
,
const
void
*
param
,
__ext_compar_fn_t
comparFn
,
void
*
buf
)
{
static
void
median
(
void
*
src
,
int64_t
size
,
int64_t
s
,
int64_t
e
,
const
void
*
param
,
__ext_compar_fn_t
comparFn
,
void
*
buf
)
{
int32_t
mid
=
((
int32_t
)(
e
-
s
)
>>
1u
)
+
(
int32_t
)
s
;
int32_t
mid
=
((
int32_t
)(
e
-
s
)
>>
1u
)
+
(
int32_t
)
s
;
if
(
comparFn
(
elePtrAt
(
src
,
size
,
mid
),
elePtrAt
(
src
,
size
,
s
),
param
)
==
1
)
{
if
(
comparFn
(
elePtrAt
(
src
,
size
,
mid
),
elePtrAt
(
src
,
size
,
s
),
param
)
==
1
)
{
...
@@ -36,7 +38,8 @@ static void median(void *src, size_t size, size_t s, size_t e, const void *param
...
@@ -36,7 +38,8 @@ static void median(void *src, size_t size, size_t s, size_t e, const void *param
doswap
(
elePtrAt
(
src
,
size
,
s
),
elePtrAt
(
src
,
size
,
e
),
size
,
buf
);
doswap
(
elePtrAt
(
src
,
size
,
s
),
elePtrAt
(
src
,
size
,
e
),
size
,
buf
);
}
}
assert
(
comparFn
(
elePtrAt
(
src
,
size
,
mid
),
elePtrAt
(
src
,
size
,
s
),
param
)
<=
0
&&
comparFn
(
elePtrAt
(
src
,
size
,
s
),
elePtrAt
(
src
,
size
,
e
),
param
)
<=
0
);
assert
(
comparFn
(
elePtrAt
(
src
,
size
,
mid
),
elePtrAt
(
src
,
size
,
s
),
param
)
<=
0
&&
comparFn
(
elePtrAt
(
src
,
size
,
s
),
elePtrAt
(
src
,
size
,
e
),
param
)
<=
0
);
#ifdef _DEBUG_VIEW
#ifdef _DEBUG_VIEW
// tTagsPrints(src[s], pOrderDesc->pColumnModel, &pOrderDesc->orderIdx);
// tTagsPrints(src[s], pOrderDesc->pColumnModel, &pOrderDesc->orderIdx);
...
@@ -45,8 +48,8 @@ static void median(void *src, size_t size, size_t s, size_t e, const void *param
...
@@ -45,8 +48,8 @@ static void median(void *src, size_t size, size_t s, size_t e, const void *param
#endif
#endif
}
}
static
void
tInsertSort
(
void
*
src
,
size
_t
size
,
int32_t
s
,
int32_t
e
,
const
void
*
param
,
__ext_compar_fn_t
comparFn
,
static
void
tInsertSort
(
void
*
src
,
int64
_t
size
,
int32_t
s
,
int32_t
e
,
const
void
*
param
,
__ext_compar_fn_t
comparFn
,
void
*
buf
)
{
void
*
buf
)
{
for
(
int32_t
i
=
s
+
1
;
i
<=
e
;
++
i
)
{
for
(
int32_t
i
=
s
+
1
;
i
<=
e
;
++
i
)
{
for
(
int32_t
j
=
i
;
j
>
s
;
--
j
)
{
for
(
int32_t
j
=
i
;
j
>
s
;
--
j
)
{
if
(
comparFn
(
elePtrAt
(
src
,
size
,
j
),
elePtrAt
(
src
,
size
,
j
-
1
),
param
)
==
-
1
)
{
if
(
comparFn
(
elePtrAt
(
src
,
size
,
j
),
elePtrAt
(
src
,
size
,
j
-
1
),
param
)
==
-
1
)
{
...
@@ -58,8 +61,8 @@ static void tInsertSort(void *src, size_t size, int32_t s, int32_t e, const void
...
@@ -58,8 +61,8 @@ static void tInsertSort(void *src, size_t size, int32_t s, int32_t e, const void
}
}
}
}
static
void
tqsortImpl
(
void
*
src
,
int32_t
start
,
int32_t
end
,
size_t
size
,
const
void
*
param
,
__ext_compar_fn_t
comparFn
,
static
void
tqsortImpl
(
void
*
src
,
int32_t
start
,
int32_t
end
,
int64_t
size
,
const
void
*
param
,
void
*
buf
)
{
__ext_compar_fn_t
comparFn
,
void
*
buf
)
{
// short array sort, incur another sort procedure instead of quick sort process
// short array sort, incur another sort procedure instead of quick sort process
const
int32_t
THRESHOLD_SIZE
=
6
;
const
int32_t
THRESHOLD_SIZE
=
6
;
if
(
end
-
start
+
1
<=
THRESHOLD_SIZE
)
{
if
(
end
-
start
+
1
<=
THRESHOLD_SIZE
)
{
...
@@ -79,7 +82,7 @@ static void tqsortImpl(void *src, int32_t start, int32_t end, size_t size, const
...
@@ -79,7 +82,7 @@ static void tqsortImpl(void *src, int32_t start, int32_t end, size_t size, const
break
;
break
;
}
}
//move the data that equals to pivotal value to the right end of the list
//
move the data that equals to pivotal value to the right end of the list
if
(
ret
==
0
&&
e
!=
endRightS
)
{
if
(
ret
==
0
&&
e
!=
endRightS
)
{
doswap
(
elePtrAt
(
src
,
size
,
e
),
elePtrAt
(
src
,
size
,
endRightS
),
size
,
buf
);
doswap
(
elePtrAt
(
src
,
size
,
e
),
elePtrAt
(
src
,
size
,
endRightS
),
size
,
buf
);
endRightS
--
;
endRightS
--
;
...
@@ -149,13 +152,13 @@ static void tqsortImpl(void *src, int32_t start, int32_t end, size_t size, const
...
@@ -149,13 +152,13 @@ static void tqsortImpl(void *src, int32_t start, int32_t end, size_t size, const
}
}
}
}
void
taosqsort
(
void
*
src
,
size_t
numOfElem
,
size_t
size
,
const
void
*
param
,
__ext_compar_fn_t
comparFn
)
{
void
taosqsort
(
void
*
src
,
int64_t
numOfElem
,
int64_t
size
,
const
void
*
param
,
__ext_compar_fn_t
comparFn
)
{
char
*
buf
=
calloc
(
1
,
size
);
// prepare the swap buffer
char
*
buf
=
calloc
(
1
,
size
);
// prepare the swap buffer
tqsortImpl
(
src
,
0
,
(
int32_t
)
numOfElem
-
1
,
(
int32_t
)
size
,
param
,
comparFn
,
buf
);
tqsortImpl
(
src
,
0
,
(
int32_t
)
numOfElem
-
1
,
(
int32_t
)
size
,
param
,
comparFn
,
buf
);
tfree
(
buf
);
tfree
(
buf
);
}
}
void
*
taosbsearch
(
const
void
*
key
,
const
void
*
base
,
size_t
nmemb
,
size
_t
size
,
__compar_fn_t
compar
,
int
flags
)
{
void
*
taosbsearch
(
const
void
*
key
,
const
void
*
base
,
int64_t
nmemb
,
int64
_t
size
,
__compar_fn_t
compar
,
int
flags
)
{
// TODO: need to check the correctness of this function
// TODO: need to check the correctness of this function
int
l
=
0
;
int
l
=
0
;
int
r
=
(
int
)
nmemb
;
int
r
=
(
int
)
nmemb
;
...
@@ -225,8 +228,8 @@ void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size,
...
@@ -225,8 +228,8 @@ void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size,
return
NULL
;
return
NULL
;
}
}
void
taosheapadjust
(
void
*
base
,
int32_t
size
,
int32_t
start
,
int32_t
end
,
const
void
*
parcompar
,
__ext_compar_fn_t
compar
,
const
void
*
parswap
,
__ext_swap_fn_t
swap
,
bool
maxroot
)
void
taosheapadjust
(
void
*
base
,
int32_t
size
,
int32_t
start
,
int32_t
end
,
const
void
*
parcompar
,
{
__ext_compar_fn_t
compar
,
const
void
*
parswap
,
__ext_swap_fn_t
swap
,
bool
maxroot
)
{
int32_t
parent
;
int32_t
parent
;
int32_t
child
;
int32_t
child
;
char
*
buf
;
char
*
buf
;
...
@@ -244,7 +247,8 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const
...
@@ -244,7 +247,8 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const
if
(
maxroot
)
{
if
(
maxroot
)
{
while
(
child
<=
end
)
{
while
(
child
<=
end
)
{
if
(
child
+
1
<=
end
&&
(
*
compar
)(
elePtrAt
(
base
,
size
,
child
),
elePtrAt
(
base
,
size
,
child
+
1
),
parcompar
)
<
0
)
{
if
(
child
+
1
<=
end
&&
(
*
compar
)(
elePtrAt
(
base
,
size
,
child
),
elePtrAt
(
base
,
size
,
child
+
1
),
parcompar
)
<
0
)
{
child
++
;
child
++
;
}
}
...
@@ -263,7 +267,8 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const
...
@@ -263,7 +267,8 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const
}
}
}
else
{
}
else
{
while
(
child
<=
end
)
{
while
(
child
<=
end
)
{
if
(
child
+
1
<=
end
&&
(
*
compar
)(
elePtrAt
(
base
,
size
,
child
),
elePtrAt
(
base
,
size
,
child
+
1
),
parcompar
)
>
0
)
{
if
(
child
+
1
<=
end
&&
(
*
compar
)(
elePtrAt
(
base
,
size
,
child
),
elePtrAt
(
base
,
size
,
child
+
1
),
parcompar
)
>
0
)
{
child
++
;
child
++
;
}
}
...
@@ -288,8 +293,8 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const
...
@@ -288,8 +293,8 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const
}
}
}
}
void
taosheapsort
(
void
*
base
,
int32_t
size
,
int32_t
len
,
const
void
*
parcompar
,
__ext_compar_fn_t
compar
,
const
void
*
parswap
,
__ext_swap_fn_t
swap
,
bool
maxroot
)
void
taosheapsort
(
void
*
base
,
int32_t
size
,
int32_t
len
,
const
void
*
parcompar
,
__ext_compar_fn_t
compar
,
{
const
void
*
parswap
,
__ext_swap_fn_t
swap
,
bool
maxroot
)
{
int32_t
i
;
int32_t
i
;
if
(
base
&&
size
>
0
)
{
if
(
base
&&
size
>
0
)
{
...
@@ -298,7 +303,7 @@ void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar,
...
@@ -298,7 +303,7 @@ void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar,
}
}
}
}
/*
/*
char *buf = calloc(1, size);
char *buf = calloc(1, size);
for (i = len - 1; i > 0; i--) {
for (i = len - 1; i > 0; i--) {
...
@@ -307,6 +312,5 @@ void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar,
...
@@ -307,6 +312,5 @@ void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar,
}
}
tfree(buf);
tfree(buf);
*/
*/
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录