Merge branch 'master' of https://gitee.com/290198252/generallib into master
# Conflicts: # .gitignore # CMakeLists.txt # general/src/utils.cpp # test/src/heapsort/main.cmaster
commit
9e28e68b66
|
@ -7,4 +7,3 @@ build/
|
||||||
libd/
|
libd/
|
||||||
general/third/
|
general/third/
|
||||||
general/CMakeFiles/
|
general/CMakeFiles/
|
||||||
/
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ enable_language(CXX)
|
||||||
project(generallib)
|
project(generallib)
|
||||||
add_subdirectory(general)
|
add_subdirectory(general)
|
||||||
SET(SRC_SDK sdk_main.c test/src/heapsort/main.c)#生成动态库需要至少包含一个源文件
|
SET(SRC_SDK sdk_main.c test/src/heapsort/main.c)#生成动态库需要至少包含一个源文件
|
||||||
message("library object is " $<TARGET_OBJECTS:General>)
|
|
||||||
add_library(generallib STATIC $<TARGET_OBJECTS:General> ${SRC_SDK})
|
add_library(generallib STATIC $<TARGET_OBJECTS:General> ${SRC_SDK})
|
||||||
message("CMAKE_BINARY_DIR is " ${CMAKE_BINARY_DIR})
|
message("CMAKE_BINARY_DIR is " ${CMAKE_BINARY_DIR})
|
||||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/libd)
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/libd)
|
||||||
|
|
|
@ -3,12 +3,14 @@
|
||||||
//
|
//
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
string itos(int x){
|
string itos(int x)
|
||||||
|
{
|
||||||
char buf[100] = {0};
|
char buf[100] = {0};
|
||||||
itoa(x,buf,10);
|
itoa(x, buf, 10);
|
||||||
return string(buf);
|
return string(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
inline ENV_SYS CurrentEnvSys(){
|
inline ENV_SYS CurrentEnvSys(){
|
||||||
#ifdef linux
|
#ifdef linux
|
||||||
return ENV_LINUX
|
return ENV_LINUX
|
||||||
|
@ -23,19 +25,36 @@ inline ENV_SYS CurrentEnvSys(){
|
||||||
return ENV_WINDOWS;
|
return ENV_WINDOWS;
|
||||||
#endif
|
#endif
|
||||||
#if !defined(linux) && defined(_WINDOWS)&&defined(_UNIX)&&defined(_WIN32)
|
#if !defined(linux) && defined(_WINDOWS)&&defined(_UNIX)&&defined(_WIN32)
|
||||||
|
=======
|
||||||
|
inline ENV_SYS CurrentEnvSys()
|
||||||
|
{
|
||||||
|
#ifdef linux
|
||||||
|
return ENV_LINUX;
|
||||||
|
#endif
|
||||||
|
#ifdef _WINDOWS
|
||||||
|
return ENV_WINDOWS;
|
||||||
|
#endif
|
||||||
|
#ifdef _UNIX
|
||||||
|
return ENV_UNIX;
|
||||||
|
#endif
|
||||||
|
#ifdef _WIN32
|
||||||
|
return ENV_WINDOWS;
|
||||||
|
#endif
|
||||||
|
>>>>>>> 762bd76fed88ab27fac300ec4a5efb17ce0af1b8
|
||||||
return ENV_NONE;
|
return ENV_NONE;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
inline ENV_COMPILER CurrentEnvCompiler(){
|
inline ENV_COMPILER CurrentEnvCompiler()
|
||||||
#ifdef __GNUC__
|
{
|
||||||
|
#ifdef __GNUC__
|
||||||
return GCC;
|
return GCC;
|
||||||
#endif
|
#endif
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
return CL;
|
return CL;
|
||||||
#endif
|
#endif
|
||||||
#ifdef __clang__
|
#ifdef __clang__
|
||||||
return CLANG;
|
return CLANG;
|
||||||
#endif
|
#endif
|
||||||
return UNKNOWN;
|
return UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) )
|
#define LENGTH(a) ((sizeof(a)) / (sizeof(a[0])))
|
||||||
static int m_heap[30]; // 数据
|
static int m_heap[30]; // 数据
|
||||||
static int m_capacity=30; // 总的容量
|
static int m_capacity = 30; // 总的容量
|
||||||
static int m_size=0; // 实际容量(初始化为0)
|
static int m_size = 0; // 实际容量(初始化为0)
|
||||||
/*
|
/*
|
||||||
* 返回data在二叉堆中的索引
|
* 返回data在二叉堆中的索引
|
||||||
*
|
*
|
||||||
|
@ -14,10 +14,10 @@ static int m_size=0; // 实际容量(初始化为0)
|
||||||
*/
|
*/
|
||||||
int get_index(int data)
|
int get_index(int data)
|
||||||
{
|
{
|
||||||
int i=0;
|
int i = 0;
|
||||||
|
|
||||||
for(i=0; i<m_size; i++)
|
for (i = 0; i < m_size; i++)
|
||||||
if (data==m_heap[i])
|
if (data == m_heap[i])
|
||||||
return i;
|
return i;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -32,22 +32,22 @@ int get_index(int data)
|
||||||
*/
|
*/
|
||||||
static void maxheap_filterdown(int start, int end)
|
static void maxheap_filterdown(int start, int end)
|
||||||
{
|
{
|
||||||
int c = start; // 当前(current)节点的位置
|
int c = start; // 当前(current)节点的位置
|
||||||
int l = 2*c + 1; // 左(left)孩子的位置
|
int l = 2 * c + 1; // 左(left)孩子的位置
|
||||||
int tmp = m_heap[c]; // 当前(current)节点的大小
|
int tmp = m_heap[c]; // 当前(current)节点的大小
|
||||||
|
|
||||||
while(l <= end)
|
while (l <= end)
|
||||||
{
|
{
|
||||||
// "l"是左孩子,"l+1"是右孩子
|
// "l"是左孩子,"l+1"是右孩子
|
||||||
if(l < end && m_heap[l] < m_heap[l+1])
|
if (l < end && m_heap[l] < m_heap[l + 1])
|
||||||
l++; // 左右两孩子中选择较大者,即m_heap[l+1]
|
l++; // 左右两孩子中选择较大者,即m_heap[l+1]
|
||||||
if(tmp >= m_heap[l])
|
if (tmp >= m_heap[l])
|
||||||
break; //调整结束
|
break; //调整结束
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_heap[c] = m_heap[l];
|
m_heap[c] = m_heap[l];
|
||||||
c = l;
|
c = l;
|
||||||
l = 2*l + 1;
|
l = 2 * l + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_heap[c] = tmp;
|
m_heap[c] = tmp;
|
||||||
|
@ -64,15 +64,15 @@ int maxheap_remove(int data)
|
||||||
{
|
{
|
||||||
int index;
|
int index;
|
||||||
// 如果"堆"已空,则返回-1
|
// 如果"堆"已空,则返回-1
|
||||||
if(m_size == 0)
|
if (m_size == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
// 获取data在数组中的索引
|
// 获取data在数组中的索引
|
||||||
index = get_index(data);
|
index = get_index(data);
|
||||||
if (index==-1)
|
if (index == -1)
|
||||||
return -1;
|
return -1;
|
||||||
m_heap[index] = m_heap[--m_size]; // 用最后元素填补
|
m_heap[index] = m_heap[--m_size]; // 用最后元素填补
|
||||||
maxheap_filterdown(index, m_size-1); // 从index位置开始自上向下调整为最大堆
|
maxheap_filterdown(index, m_size - 1); // 从index位置开始自上向下调整为最大堆
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
@ -85,24 +85,24 @@ int maxheap_remove(int data)
|
||||||
*/
|
*/
|
||||||
static void maxheap_filterup(int start)
|
static void maxheap_filterup(int start)
|
||||||
{
|
{
|
||||||
int c = start; // 当前节点(current)的位置
|
int c = start; // 当前节点(current)的位置
|
||||||
int p = ( c- 1)/2; // 父(parent)结点的位置
|
int p = (c - 1) / 2; // 父(parent)结点的位置
|
||||||
int tmp = m_heap[c]; // 当前节点(current)的大小
|
int tmp = m_heap[c]; // 当前节点(current)的大小
|
||||||
|
|
||||||
while(c > 0)
|
while (c > 0)
|
||||||
{
|
{
|
||||||
// 父节点大于当前节点
|
// 父节点大于当前节点
|
||||||
if(m_heap[p] >= tmp)
|
if (m_heap[p] >= tmp)
|
||||||
break;
|
break;
|
||||||
else
|
else
|
||||||
{ // 父节点小于当前节点,就调换位置,当前节点作为父节点,来源于一个抢位置的概念
|
{ // 父节点小于当前节点,就调换位置,当前节点作为父节点,来源于一个抢位置的概念
|
||||||
m_heap[c] = m_heap[p];
|
m_heap[c] = m_heap[p];
|
||||||
c = p;
|
c = p;
|
||||||
p = (p - 1)/2; // 再把当前节点和上一个父节点在比较大小
|
p = (p - 1) / 2; // 再把当前节点和上一个父节点在比较大小
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 找到合适的索引
|
// 找到合适的索引
|
||||||
m_heap[c] = tmp; //最后把设置值
|
m_heap[c] = tmp; //最后把设置值
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* 将data插入到二叉堆中
|
* 将data插入到二叉堆中
|
||||||
|
@ -114,12 +114,12 @@ static void maxheap_filterup(int start)
|
||||||
int maxheap_insert(int data)
|
int maxheap_insert(int data)
|
||||||
{
|
{
|
||||||
// 如果"堆"已满,则返回
|
// 如果"堆"已满,则返回
|
||||||
if(m_size == m_capacity)
|
if (m_size == m_capacity)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
m_heap[m_size] = data; // 将"数组"插在表尾
|
m_heap[m_size] = data; // 将"数组"插在表尾
|
||||||
maxheap_filterup(m_size); // 向上调整堆
|
maxheap_filterup(m_size); // 向上调整堆
|
||||||
m_size++; // 堆的实际容量+1
|
m_size++; // 堆的实际容量+1
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -127,32 +127,33 @@ int maxheap_insert(int data)
|
||||||
void maxheap_print()
|
void maxheap_print()
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<m_size; i++)
|
for (i = 0; i < m_size; i++)
|
||||||
printf("%d ", m_heap[i]);
|
printf("%d ", m_heap[i]);
|
||||||
}
|
}
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
int a[] = {10, 40, 30, 60, 90, 70, 20, 50, 80};
|
int a[] = {10, 40, 30, 60, 90, 70, 20, 50, 80};
|
||||||
int i, len=LENGTH(a);
|
int i, len = LENGTH(a);
|
||||||
printf("== 堆排序: ");
|
printf("== 堆排序: ");
|
||||||
for(i=0; i<len; i++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
printf("%d \r\n", a[i]);
|
printf("%d \r\n", a[i]);
|
||||||
maxheap_insert(a[i]);
|
maxheap_insert(a[i]);
|
||||||
printf("round %d result:\r\n");
|
printf("round %d result:\r\n");
|
||||||
for(uint8_t z = 0;z < m_size;z++) {
|
for (uint8_t z = 0; z < m_size; z++)
|
||||||
printf("%d\r\n",m_heap[z]);
|
{
|
||||||
|
printf("%d\r\n", m_heap[z]);
|
||||||
}
|
}
|
||||||
printf("end\r\n");
|
printf("end\r\n");
|
||||||
}
|
}
|
||||||
printf("\n== 最 大 堆: ");
|
printf("\n== 最 大 堆: ");
|
||||||
maxheap_print();
|
maxheap_print();
|
||||||
i=85;
|
i = 85;
|
||||||
maxheap_insert(i);
|
maxheap_insert(i);
|
||||||
printf("\n== 添加元素: %d", i);
|
printf("\n== 添加元素: %d", i);
|
||||||
printf("\n== 最 大 堆: ");
|
printf("\n== 最 大 堆: ");
|
||||||
maxheap_print();
|
maxheap_print();
|
||||||
i=90;
|
i = 90;
|
||||||
maxheap_remove(i);
|
maxheap_remove(i);
|
||||||
printf("\n== 删除元素: %d", i);
|
printf("\n== 删除元素: %d", i);
|
||||||
printf("\n== 最 大 堆: ");
|
printf("\n== 最 大 堆: ");
|
||||||
|
|
Loading…
Reference in New Issue