From fae73f8668f55c95eb767b08dae1642f78a3c0a2 Mon Sep 17 00:00:00 2001
From: caiyuzheng <290198252@qq.com>
Date: Wed, 13 May 2020 18:36:22 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ringbuffer?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/workspace.xml | 83 +++++++++++++++--------
general/src/algorithm/ringbuffer.hpp | 63 +++++++++++++++++
test/src/heapsort/main.c | 46 ++++---------
test/src/tcptest/CMakeLists.txt | 13 ++++
test/src/{ => tcptest}/tcpclient_test.cpp | 0
5 files changed, 146 insertions(+), 59 deletions(-)
create mode 100644 general/src/algorithm/ringbuffer.hpp
create mode 100644 test/src/tcptest/CMakeLists.txt
rename test/src/{ => tcptest}/tcpclient_test.cpp (100%)
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 66584da..f941839 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,5 +1,23 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -12,19 +30,12 @@
-
-
+
-
-
-
-
-
-
-
-
+
+
+
-
@@ -70,24 +81,15 @@
+
+
+
-
-
-
-
+
@@ -146,24 +148,25 @@
+
+
-
+
+
@@ -178,9 +181,33 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/general/src/algorithm/ringbuffer.hpp b/general/src/algorithm/ringbuffer.hpp
new file mode 100644
index 0000000..86e0897
--- /dev/null
+++ b/general/src/algorithm/ringbuffer.hpp
@@ -0,0 +1,63 @@
+#include
+#include
+using namespace std;
+
+template
+class RingBuffer{
+public:
+ typedef enum Error{
+ Error = -10,
+ ERROR_FULL = -11,
+ ERROR_NO_ENOUGH_WRITE = -12,
+ };
+ RingBuffer(uint16_t len){
+ mBuffer = new[len];
+ mHead = mTail = mBuffer;
+ if (mBuffer == nullptr){
+ this->mLen = 0;
+ }
+ mLen = len;
+ };
+ uint16_t ReadableCnt(){
+ if (mHead == mTail) return 0;
+ if (mHead < mTail)
+ return mTail - mHead;
+ return mLen - (mHead - mTail);
+ }
+ uint16_t WriteableCnt(){
+ return mLen - this->ReadableCnt();
+ }
+ Error Write(const T *in,uint16_t len){
+ if (len >= WriteableCnt(rb))
+ return ERROR_NO_ENOUGH_WRITE;
+ if (mHead <= mTail) // 头部小于尾部
+ {
+ int tail_avail_sz = mLen - (mTail - this->mBuffer);
+ if (len <= tail_avail_sz) // 是否需要循环到0开始写
+ {
+ memcpy(rb->rb_tail, in, len);
+ mTail += len;
+ if (mTail == mBuffer + mLen) // 刚好相等的情况
+ mTail = mBuffer;
+ return len;
+ }
+ else
+ {
+ memcpy(mTail, in, tail_avail_sz);
+ mTail = rb->rb_buff;
+ return tail_avail_sz + Write(rb, (char*)in + tail_avail_sz, len - tail_avail_sz);
+ }
+ }
+ else
+ {
+ memcpy(mTail, in, len); // 头部大于尾部
+ mTail += len;
+ return count;
+ }
+ }
+private:
+ T *mBuffer;
+ T* mHead ;
+ T* mTail;
+ uint16_t mLen;
+};
\ No newline at end of file
diff --git a/test/src/heapsort/main.c b/test/src/heapsort/main.c
index 2046df6..7ca2687 100644
--- a/test/src/heapsort/main.c
+++ b/test/src/heapsort/main.c
@@ -1,19 +1,10 @@
-/**
- * 二叉堆(最大堆)
- *
- * @author skywang
- * @date 2014/03/07
- */
-
#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)
-
/*
* 返回data在二叉堆中的索引
*
@@ -28,10 +19,8 @@ int get_index(int data)
for(i=0; i= tmp)
break;
else
+<<<<<<< Updated upstream
{ // 父节点小于当前节点,就调换位置,当前节点作为父节点,来源于一个抢位置的概念
+=======
+ { // 父节点小于当前节点,就调换位置,当前节点的值变成父节点的值,父节点的值变成当前节点的值,
+ // 并且父节点接着和父节点的父节点进行比较,如果还是比父节点的值大,再次把发当前节点的值
+ //
+>>>>>>> Stashed changes
m_heap[c] = m_heap[p];
c = p;
- p = (p-1)/2; // 再把当前节点和上一个父节点在比较大小
+ p = (p - 1)/2; // 再把当前节点和上一个父节点在比较大小
}
}
// 找到合适的索引
m_heap[c] = tmp; //最后把设置值
}
-
/*
* 将data插入到二叉堆中
*
@@ -139,41 +130,34 @@ int maxheap_insert(int data)
return 0;
}
-/*
- * 打印二叉堆
- *
- * 返回值:
- * 0,表示成功
- * -1,表示失败
- */
void maxheap_print()
{
int i;
for (i=0; i