diff --git a/.gitignore b/.gitignore index fad9e31..d1a5751 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,3 @@ build/ libd/ general/third/ general/CMakeFiles/ -/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 97f1257..6e0810c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 " $) add_library(generallib STATIC $ ${SRC_SDK}) message("CMAKE_BINARY_DIR is " ${CMAKE_BINARY_DIR}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/libd) diff --git a/general/src/utils.cpp b/general/src/utils.cpp index d8b7307..7087da9 100644 --- a/general/src/utils.cpp +++ b/general/src/utils.cpp @@ -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; } diff --git a/test/src/heapsort/main.c b/test/src/heapsort/main.c index da08441..38cb8c2 100644 --- a/test/src/heapsort/main.c +++ b/test/src/heapsort/main.c @@ -1,10 +1,10 @@ #include #include #include -#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_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