Merge branch 'master' of https://gitee.com/290198252/generallib into master

# Conflicts:
#	.gitignore
#	CMakeLists.txt
#	general/src/utils.cpp
#	test/src/heapsort/main.c
master
18650180552 2020-09-30 11:47:33 +08:00
commit 9e28e68b66
4 changed files with 67 additions and 49 deletions

1
.gitignore vendored
View File

@ -7,4 +7,3 @@ build/
libd/
general/third/
general/CMakeFiles/
/

View File

@ -7,7 +7,6 @@ enable_language(CXX)
project(generallib)
add_subdirectory(general)
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})
message("CMAKE_BINARY_DIR is " ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/libd)

View File

@ -3,12 +3,14 @@
//
#include "utils.h"
string itos(int x){
string itos(int x)
{
char buf[100] = {0};
itoa(x,buf,10);
itoa(x, buf, 10);
return string(buf);
}
<<<<<<< HEAD
inline ENV_SYS CurrentEnvSys(){
#ifdef linux
return ENV_LINUX
@ -23,19 +25,36 @@ inline ENV_SYS CurrentEnvSys(){
return ENV_WINDOWS;
#endif
#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;
#endif
}
inline ENV_COMPILER CurrentEnvCompiler(){
#ifdef __GNUC__
inline ENV_COMPILER CurrentEnvCompiler()
{
#ifdef __GNUC__
return GCC;
#endif
#ifdef _MSC_VER
#endif
#ifdef _MSC_VER
return CL;
#endif
#ifdef __clang__
#endif
#ifdef __clang__
return CLANG;
#endif
#endif
return UNKNOWN;
}

View File

@ -1,10 +1,10 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) )
static int m_heap[30]; // 数据
static int m_capacity=30; // 总的容量
static int m_size=0; // 实际容量(初始化为0)
#define LENGTH(a) ((sizeof(a)) / (sizeof(a[0])))
static int m_heap[30]; // 数据
static int m_capacity = 30; // 总的容量
static int m_size = 0; // 实际容量(初始化为0)
/*
* data
*
@ -14,10 +14,10 @@ static int m_size=0; // 实际容量(初始化为0)
*/
int get_index(int data)
{
int i=0;
int i = 0;
for(i=0; i<m_size; i++)
if (data==m_heap[i])
for (i = 0; i < m_size; i++)
if (data == m_heap[i])
return i;
return -1;
}
@ -32,22 +32,22 @@ int get_index(int data)
*/
static void maxheap_filterdown(int start, int end)
{
int c = start; // 当前(current)节点的位置
int l = 2*c + 1; // 左(left)孩子的位置
int tmp = m_heap[c]; // 当前(current)节点的大小
int c = start; // 当前(current)节点的位置
int l = 2 * c + 1; // 左(left)孩子的位置
int tmp = m_heap[c]; // 当前(current)节点的大小
while(l <= end)
while (l <= end)
{
// "l"是左孩子,"l+1"是右孩子
if(l < end && m_heap[l] < m_heap[l+1])
l++; // 左右两孩子中选择较大者即m_heap[l+1]
if(tmp >= m_heap[l])
break; //调整结束
if (l < end && m_heap[l] < m_heap[l + 1])
l++; // 左右两孩子中选择较大者即m_heap[l+1]
if (tmp >= m_heap[l])
break; //调整结束
else
{
m_heap[c] = m_heap[l];
c = l;
l = 2*l + 1;
l = 2 * l + 1;
}
}
m_heap[c] = tmp;
@ -64,15 +64,15 @@ int maxheap_remove(int data)
{
int index;
// 如果"堆"已空,则返回-1
if(m_size == 0)
if (m_size == 0)
return -1;
// 获取data在数组中的索引
index = get_index(data);
if (index==-1)
if (index == -1)
return -1;
m_heap[index] = m_heap[--m_size]; // 用最后元素填补
maxheap_filterdown(index, m_size-1); // 从index位置开始自上向下调整为最大堆
m_heap[index] = m_heap[--m_size]; // 用最后元素填补
maxheap_filterdown(index, m_size - 1); // 从index位置开始自上向下调整为最大堆
return 0;
}
/*
@ -85,24 +85,24 @@ int maxheap_remove(int data)
*/
static void maxheap_filterup(int start)
{
int c = start; // 当前节点(current)的位置
int p = ( c- 1)/2; // 父(parent)结点的位置
int tmp = m_heap[c]; // 当前节点(current)的大小
int c = start; // 当前节点(current)的位置
int p = (c - 1) / 2; // 父(parent)结点的位置
int tmp = m_heap[c]; // 当前节点(current)的大小
while(c > 0)
while (c > 0)
{
// 父节点大于当前节点
if(m_heap[p] >= tmp)
if (m_heap[p] >= tmp)
break;
else
{ // 父节点小于当前节点,就调换位置,当前节点作为父节点,来源于一个抢位置的概念
m_heap[c] = m_heap[p];
c = p;
p = (p - 1)/2; // 再把当前节点和上一个父节点在比较大小
p = (p - 1) / 2; // 再把当前节点和上一个父节点在比较大小
}
}
// 找到合适的索引
m_heap[c] = tmp; //最后把设置值
m_heap[c] = tmp; //最后把设置值
}
/*
* data
@ -114,12 +114,12 @@ static void maxheap_filterup(int start)
int maxheap_insert(int data)
{
// 如果"堆"已满,则返回
if(m_size == m_capacity)
if (m_size == m_capacity)
return -1;
m_heap[m_size] = data; // 将"数组"插在表尾
maxheap_filterup(m_size); // 向上调整堆
m_size++; // 堆的实际容量+1
m_heap[m_size] = data; // 将"数组"插在表尾
maxheap_filterup(m_size); // 向上调整堆
m_size++; // 堆的实际容量+1
return 0;
}
@ -127,32 +127,33 @@ int maxheap_insert(int data)
void maxheap_print()
{
int i;
for (i=0; i<m_size; i++)
for (i = 0; i < m_size; i++)
printf("%d ", m_heap[i]);
}
void main()
{
int a[] = {10, 40, 30, 60, 90, 70, 20, 50, 80};
int i, len=LENGTH(a);
int i, len = LENGTH(a);
printf("== 堆排序: ");
for(i=0; i<len; i++)
for (i = 0; i < len; i++)
{
printf("%d \r\n", a[i]);
maxheap_insert(a[i]);
printf("round %d result:\r\n");
for(uint8_t z = 0;z < m_size;z++) {
printf("%d\r\n",m_heap[z]);
for (uint8_t z = 0; z < m_size; z++)
{
printf("%d\r\n", m_heap[z]);
}
printf("end\r\n");
}
printf("\n== 最 大 堆: ");
maxheap_print();
i=85;
i = 85;
maxheap_insert(i);
printf("\n== 添加元素: %d", i);
printf("\n== 最 大 堆: ");
maxheap_print();
i=90;
i = 90;
maxheap_remove(i);
printf("\n== 删除元素: %d", i);
printf("\n== 最 大 堆: ");