添加ringbuffer

master
caiyuzheng 2020-05-13 18:36:22 +08:00
parent 47296e6b86
commit fae73f8668
5 changed files with 146 additions and 59 deletions

View File

@ -1,5 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="BranchesTreeState">
<expand>
<path>
<item name="ROOT" type="e8cecc67:BranchNodeDescriptor" />
<item name="LOCAL_ROOT" type="e8cecc67:BranchNodeDescriptor" />
</path>
<path>
<item name="ROOT" type="e8cecc67:BranchNodeDescriptor" />
<item name="REMOTE_ROOT" type="e8cecc67:BranchNodeDescriptor" />
</path>
<path>
<item name="ROOT" type="e8cecc67:BranchNodeDescriptor" />
<item name="REMOTE_ROOT" type="e8cecc67:BranchNodeDescriptor" />
<item name="GROUP_NODE:origin" type="e8cecc67:BranchNodeDescriptor" />
</path>
</expand>
<select />
</component>
<component name="CMakeRunConfigurationManager" shouldGenerate="true" shouldDeleteObsolete="true">
<generated>
<config projectName="General" targetName="General" />
@ -12,19 +30,12 @@
</component>
<component name="ChangeListManager">
<list default="true" id="0facce0d-c642-4d80-b2fb-daf5f3e68dff" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/general/src/function/btree.cpp" afterDir="false" />
<change afterPath="$PROJECT_DIR$/general/src/function/btree.h" afterDir="false" />
<change afterPath="$PROJECT_DIR$/general/src/algorithm/ringbuffer.hpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/general/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/general/CMakeLists.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/general/src/debug.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/general/src/debug.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/general/src/encrypt/aes.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/general/src/encrypt/aes.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/general/src/encrypt/rsa.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/general/src/encrypt/rsa.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/general/src/loger.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/general/src/loger.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/general/src/net/PackageReceiver.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/general/src/net/PackageReceiver.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/general/src/net/TcpClient.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/general/src/net/TcpClient.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/general/src/net/TcpClient.h" beforeDir="false" afterPath="$PROJECT_DIR$/general/src/net/TcpClient.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/test/src/tcptest/CMakeLists.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test/src/heapsort/main.c" beforeDir="false" afterPath="$PROJECT_DIR$/test/src/heapsort/main.c" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test/src/tcpclient_test.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/test/src/tcptest/tcpclient_test.cpp" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -70,24 +81,15 @@
<property name="settings.editor.selected.configurable" value="CPPToolchains" />
</component>
<component name="RecentsManager">
<key name="MoveFile.RECENT_KEYS">
<recent name="D:\project\c++\generallib\test\src\tcptest" />
</key>
<key name="CopyFile.RECENT_KEYS">
<recent name="D:\project\c++\generallib\src\encrypt" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="General" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="General" TARGET_NAME="General" CONFIG_NAME="Debug">
<configuration name="General" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="General" TARGET_NAME="General" CONFIG_NAME="Debug">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
@ -146,24 +148,25 @@
<workItem from="1588685364145" duration="2436000" />
<workItem from="1588731711427" duration="7486000" />
<workItem from="1588756178008" duration="3364000" />
<workItem from="1588988345219" duration="6326000" />
<workItem from="1589364212338" duration="1825000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
<option name="version" value="2" />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State>
<option name="COLUMN_ORDER" />
</State>
<State />
</value>
</entry>
</map>
</option>
<option name="oldMeFiltersMigrated" value="true" />
</component>
<component name="WindowStateProjectService">
<state x="651" y="314" width="409" height="388" key="#Notifications" timestamp="1588741195489">
@ -178,9 +181,33 @@
<screen x="0" y="40" width="1536" height="824" />
</state>
<state x="549" y="206" key="FileChooserDialogImpl/0.40.1536.824@0.40.1536.824" timestamp="1588739069638" />
<state width="1493" height="210" key="GridCell.Tab.0.bottom" timestamp="1589037862487">
<screen x="0" y="40" width="1536" height="824" />
</state>
<state width="1493" height="210" key="GridCell.Tab.0.bottom/0.40.1536.824/-1694.-1040.2560.1040@0.40.1536.824" timestamp="1589037862487" />
<state width="1493" height="210" key="GridCell.Tab.0.bottom/0.40.1536.824@0.40.1536.824" timestamp="1588993827572" />
<state width="1493" height="210" key="GridCell.Tab.0.center" timestamp="1589037862487">
<screen x="0" y="40" width="1536" height="824" />
</state>
<state width="1493" height="210" key="GridCell.Tab.0.center/0.40.1536.824/-1694.-1040.2560.1040@0.40.1536.824" timestamp="1589037862487" />
<state width="1493" height="210" key="GridCell.Tab.0.center/0.40.1536.824@0.40.1536.824" timestamp="1588993827572" />
<state width="1493" height="210" key="GridCell.Tab.0.left" timestamp="1589037862487">
<screen x="0" y="40" width="1536" height="824" />
</state>
<state width="1493" height="210" key="GridCell.Tab.0.left/0.40.1536.824/-1694.-1040.2560.1040@0.40.1536.824" timestamp="1589037862487" />
<state width="1493" height="210" key="GridCell.Tab.0.left/0.40.1536.824@0.40.1536.824" timestamp="1588993827572" />
<state width="1493" height="210" key="GridCell.Tab.0.right" timestamp="1589037862487">
<screen x="0" y="40" width="1536" height="824" />
</state>
<state width="1493" height="210" key="GridCell.Tab.0.right/0.40.1536.824/-1694.-1040.2560.1040@0.40.1536.824" timestamp="1589037862487" />
<state width="1493" height="210" key="GridCell.Tab.0.right/0.40.1536.824@0.40.1536.824" timestamp="1588993827572" />
<state x="270" y="96" key="SettingsEditor" timestamp="1588739395688">
<screen x="0" y="40" width="1536" height="824" />
</state>
<state x="270" y="96" key="SettingsEditor/0.40.1536.824@0.40.1536.824" timestamp="1588739395688" />
<state x="431" y="185" width="672" height="678" key="search.everywhere.popup" timestamp="1589365807287">
<screen x="0" y="40" width="1536" height="824" />
</state>
<state x="431" y="185" width="672" height="678" key="search.everywhere.popup/0.40.1536.824@0.40.1536.824" timestamp="1589365807287" />
</component>
</project>

View File

@ -0,0 +1,63 @@
#include <vector>
#include <stdint.h>
using namespace std;
template<typename T>
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;
};

View File

@ -1,19 +1,10 @@
/**
* ()
*
* @author skywang
* @date 2014/03/07
*/
#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)
/*
* data
*
@ -28,10 +19,8 @@ int get_index(int data)
for(i=0; i<m_size; i++)
if (data==m_heap[i])
return i;
return -1;
}
/*
*
*
@ -82,13 +71,10 @@ int maxheap_remove(int data)
index = get_index(data);
if (index==-1)
return -1;
m_heap[index] = m_heap[--m_size]; // 用最后元素填补
maxheap_filterdown(index, m_size-1); // 从index位置开始自上向下调整为最大堆
return 0;
}
/*
* (start0)
*
@ -109,16 +95,21 @@ static void maxheap_filterup(int start)
if(m_heap[p] >= 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<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);
printf("== : ");
printf("== 堆排序: ");
for(i=0; i<len; i++)
{
printf("%d ", a[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]);
}
printf("end\r\n");
}
printf("\n== 最 大 堆: ");
maxheap_print();
i=85;
maxheap_insert(i);
printf("\n== 添加元素: %d", i);
printf("\n== 最 大 堆: ");
maxheap_print();
i=90;
maxheap_remove(i);
printf("\n== 删除元素: %d", i);

View File

@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.15)
project(tcptest)
message("current dir" ../..)
include_directories(../../../../general/obj/inc)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../general/obj/inc/third/include)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake-build-debug/libgenerallib.a)
link_libraries(libGeneral.a)
link_libraries(ws2_32)
link_libraries(${CMAKE_CURRENT_SOURCE_DIR}/../../third/lib/libevent.a)
link_libraries(${CMAKE_CURRENT_SOURCE_DIR}/../../third/lib/libevent_core.a)
add_executable(tcptest tcpclient_test.cpp)