add bitmap class for bit operation

This commit is contained in:
zhengshuxin 2017-08-23 15:14:40 +08:00
parent 28f805981e
commit 5b0cdc1fc9
15 changed files with 159 additions and 0 deletions

View File

@ -1,6 +1,9 @@
修改历史列表: 修改历史列表:
----------------------------------------------------------------------- -----------------------------------------------------------------------
490) 2017.8.23
490.1) feature: 在 stdlib/ 目录下新增 bitmap 类,用于位映射操作
489) 2017.8.18 489) 2017.8.18
489.1) feature: 增加原子操作类 atomic 489.1) feature: 增加原子操作类 atomic
489.2) samples/atomic: 增加测试原子操作的例子 489.2) samples/atomic: 增加测试原子操作的例子

View File

@ -34,6 +34,7 @@
#include "stdlib/dbuf_pool.hpp" #include "stdlib/dbuf_pool.hpp"
#include "stdlib/mbox.hpp" #include "stdlib/mbox.hpp"
#include "stdlib/atomic.hpp" #include "stdlib/atomic.hpp"
#include "stdlib/bitmap.hpp"
#include "serialize/gsoner.hpp" #include "serialize/gsoner.hpp"

View File

@ -0,0 +1,97 @@
#pragma once
namespace acl {
/**
* @author wanghaibin@qiyi.com
* @date 2017-08-16
*/
class bitmap
{
public:
/**
*
* @param buf {const void*}
* @param len {size_} buf
*/
bitmap(const void* buf, size_t len);
/**
*
* @param len {size_t}
*/
bitmap(size_t len);
~bitmap();
/**
*
* @param n {size_t}
* @return {bool} true
*/
bool set(size_t n);
/**
*
* @param n {size_t}
* @return {bool}
*/
bool isset(size_t n);
/**
*
* @param n {size_t}
* @return {bool} false
*/
bool unset(size_t n);
/**
* bitmap信息拷贝到buf中
* @param buf {void*}
* @param len {size_t} buf的最大长度
* @return {size_t} 0 buf
*/
size_t tobuf(void* buf, size_t len);
/**
* buf中设置当前bitmap信息
* @param buf {const void*} bitmap信息
* @param len {size_t} buf的长度
* @return true false失败
*/
bool frombuf(const void* buf, size_t len);
/**
* bitmap为 0
*/
void reset(void);
/**
*
* @return {size_t}
*/
size_t size(void);
/**
*
* @return {size_t}
*/
size_t count(void);
/**
* bitmap是否已满
* @return {bool}
*/
bool full(void);
private:
unsigned char *bmp_;
size_t size_;
size_t count_;
//从新统计count数量
void recount(void);
};
} // namespace acl

View File

@ -33,6 +33,7 @@ public:
~thread_cond(void); ~thread_cond(void);
bool notify(void); bool notify(void);
bool notify_all(void);
bool wait(long long microseconds = 0); bool wait(long long microseconds = 0);
thread_mutex& get_mutex(void) const; thread_mutex& get_mutex(void) const;

View File

@ -387,6 +387,9 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<File <File
RelativePath=".\src\stdlib\atomic.cpp"> RelativePath=".\src\stdlib\atomic.cpp">
</File> </File>
<File
RelativePath=".\src\stdlib\bitmap.cpp">
</File>
<File <File
RelativePath=".\src\stdlib\json.cpp"> RelativePath=".\src\stdlib\json.cpp">
</File> </File>
@ -1066,6 +1069,9 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<File <File
RelativePath=".\include\acl_cpp\stdlib\atomic.hpp"> RelativePath=".\include\acl_cpp\stdlib\atomic.hpp">
</File> </File>
<File
RelativePath=".\include\acl_cpp\stdlib\bitmap.hpp">
</File>
<File <File
RelativePath=".\include\acl_cpp\stdlib\charset_conv.hpp"> RelativePath=".\include\acl_cpp\stdlib\charset_conv.hpp">
</File> </File>

View File

@ -973,6 +973,10 @@
RelativePath=".\src\stdlib\atomic.cpp" RelativePath=".\src\stdlib\atomic.cpp"
> >
</File> </File>
<File
RelativePath=".\src\stdlib\bitmap.cpp"
>
</File>
<File <File
RelativePath=".\src\stdlib\charset_conv.cpp" RelativePath=".\src\stdlib\charset_conv.cpp"
> >
@ -1755,6 +1759,10 @@
RelativePath=".\include\acl_cpp\stdlib\atomic.hpp" RelativePath=".\include\acl_cpp\stdlib\atomic.hpp"
> >
</File> </File>
<File
RelativePath=".\include\acl_cpp\stdlib\bitmap.hpp"
>
</File>
<File <File
RelativePath=".\include\acl_cpp\stdlib\charset_conv.hpp" RelativePath=".\include\acl_cpp\stdlib\charset_conv.hpp"
> >

View File

@ -336,6 +336,7 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<ClCompile Include="src\smtp\mail_message.cpp" /> <ClCompile Include="src\smtp\mail_message.cpp" />
<ClCompile Include="src\smtp\smtp_client.cpp" /> <ClCompile Include="src\smtp\smtp_client.cpp" />
<ClCompile Include="src\stdlib\atomic.cpp" /> <ClCompile Include="src\stdlib\atomic.cpp" />
<ClCompile Include="src\stdlib\bitmap.cpp" />
<ClCompile Include="src\stdlib\charset_conv.cpp" /> <ClCompile Include="src\stdlib\charset_conv.cpp" />
<ClCompile Include="src\stdlib\dbuf_pool.cpp" /> <ClCompile Include="src\stdlib\dbuf_pool.cpp" />
<ClCompile Include="src\stdlib\dns_service.cpp" /> <ClCompile Include="src\stdlib\dns_service.cpp" />
@ -513,6 +514,7 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\smtp\mail_message.hpp" /> <ClInclude Include="include\acl_cpp\smtp\mail_message.hpp" />
<ClInclude Include="include\acl_cpp\smtp\smtp_client.hpp" /> <ClInclude Include="include\acl_cpp\smtp\smtp_client.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\atomic.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\atomic.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\bitmap.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\dbuf_pool.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\dbuf_pool.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\dns_service.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\dns_service.hpp" />

View File

@ -127,6 +127,9 @@
<ClCompile Include="src\stdlib\atomic.cpp"> <ClCompile Include="src\stdlib\atomic.cpp">
<Filter>src\stdlib</Filter> <Filter>src\stdlib</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\stdlib\bitmap.cpp">
<Filter>src\stdlib</Filter>
</ClCompile>
<ClCompile Include="src\stdlib\charset_conv.cpp"> <ClCompile Include="src\stdlib\charset_conv.cpp">
<Filter>src\stdlib</Filter> <Filter>src\stdlib</Filter>
</ClCompile> </ClCompile>
@ -783,6 +786,9 @@
<ClInclude Include="include\acl_cpp\stdlib\atomic.hpp"> <ClInclude Include="include\acl_cpp\stdlib\atomic.hpp">
<Filter>include\stdlib</Filter> <Filter>include\stdlib</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="include\acl_cpp\stdlib\bitmap.hpp">
<Filter>include\stdlib</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp"> <ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp">
<Filter>include\stdlib</Filter> <Filter>include\stdlib</Filter>
</ClInclude> </ClInclude>

View File

@ -530,6 +530,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClCompile Include="src\smtp\mail_message.cpp" /> <ClCompile Include="src\smtp\mail_message.cpp" />
<ClCompile Include="src\smtp\smtp_client.cpp" /> <ClCompile Include="src\smtp\smtp_client.cpp" />
<ClCompile Include="src\stdlib\atomic.cpp" /> <ClCompile Include="src\stdlib\atomic.cpp" />
<ClCompile Include="src\stdlib\bitmap.cpp" />
<ClCompile Include="src\stdlib\charset_conv.cpp" /> <ClCompile Include="src\stdlib\charset_conv.cpp" />
<ClCompile Include="src\stdlib\dbuf_pool.cpp" /> <ClCompile Include="src\stdlib\dbuf_pool.cpp" />
<ClCompile Include="src\stdlib\dns_service.cpp" /> <ClCompile Include="src\stdlib\dns_service.cpp" />
@ -707,6 +708,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\smtp\mail_message.hpp" /> <ClInclude Include="include\acl_cpp\smtp\mail_message.hpp" />
<ClInclude Include="include\acl_cpp\smtp\smtp_client.hpp" /> <ClInclude Include="include\acl_cpp\smtp\smtp_client.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\atomic.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\atomic.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\bitmap.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\dbuf_pool.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\dbuf_pool.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\dns_service.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\dns_service.hpp" />

View File

@ -124,6 +124,12 @@
<ClCompile Include="src\stdlib\atomic.cpp"> <ClCompile Include="src\stdlib\atomic.cpp">
<Filter>Source Files\stdlib</Filter> <Filter>Source Files\stdlib</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\stdlib\bitmap.cpp">
<Filter>Source Files\stdlib</Filter>
</ClCompile>
<ClCompile Include="src\stdlib\bitmap.cpp">
<Filter>Source Files\stdlib</Filter>
</ClCompile>
<ClCompile Include="src\stdlib\charset_conv.cpp"> <ClCompile Include="src\stdlib\charset_conv.cpp">
<Filter>Source Files\stdlib</Filter> <Filter>Source Files\stdlib</Filter>
</ClCompile> </ClCompile>
@ -777,6 +783,12 @@
<ClInclude Include="include\acl_cpp\stdlib\atomic.hpp"> <ClInclude Include="include\acl_cpp\stdlib\atomic.hpp">
<Filter>Header Files\stdlib</Filter> <Filter>Header Files\stdlib</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="include\acl_cpp\stdlib\bitmap.hpp">
<Filter>Header Files\stdlib</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\stdlib\bitmap.hpp">
<Filter>Header Files\stdlib</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp"> <ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp">
<Filter>Header Files\stdlib</Filter> <Filter>Header Files\stdlib</Filter>
</ClInclude> </ClInclude>

View File

@ -530,6 +530,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClCompile Include="src\smtp\mail_message.cpp" /> <ClCompile Include="src\smtp\mail_message.cpp" />
<ClCompile Include="src\smtp\smtp_client.cpp" /> <ClCompile Include="src\smtp\smtp_client.cpp" />
<ClCompile Include="src\stdlib\atomic.cpp" /> <ClCompile Include="src\stdlib\atomic.cpp" />
<ClCompile Include="src\stdlib\bitmap.cpp" />
<ClCompile Include="src\stdlib\charset_conv.cpp" /> <ClCompile Include="src\stdlib\charset_conv.cpp" />
<ClCompile Include="src\stdlib\dbuf_pool.cpp" /> <ClCompile Include="src\stdlib\dbuf_pool.cpp" />
<ClCompile Include="src\stdlib\dns_service.cpp" /> <ClCompile Include="src\stdlib\dns_service.cpp" />
@ -707,6 +708,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\smtp\mail_message.hpp" /> <ClInclude Include="include\acl_cpp\smtp\mail_message.hpp" />
<ClInclude Include="include\acl_cpp\smtp\smtp_client.hpp" /> <ClInclude Include="include\acl_cpp\smtp\smtp_client.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\atomic.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\atomic.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\bitmap.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\dbuf_pool.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\dbuf_pool.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\dns_service.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\dns_service.hpp" />

View File

@ -124,6 +124,9 @@
<ClCompile Include="src\stdlib\atomic.cpp"> <ClCompile Include="src\stdlib\atomic.cpp">
<Filter>Source Files\stdlib</Filter> <Filter>Source Files\stdlib</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\stdlib\bitmap.cpp">
<Filter>Source Files\stdlib</Filter>
</ClCompile>
<ClCompile Include="src\stdlib\charset_conv.cpp"> <ClCompile Include="src\stdlib\charset_conv.cpp">
<Filter>Source Files\stdlib</Filter> <Filter>Source Files\stdlib</Filter>
</ClCompile> </ClCompile>
@ -777,6 +780,9 @@
<ClInclude Include="include\acl_cpp\stdlib\atomic.hpp"> <ClInclude Include="include\acl_cpp\stdlib\atomic.hpp">
<Filter>Header Files\stdlib</Filter> <Filter>Header Files\stdlib</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="include\acl_cpp\stdlib\bitmap.hpp">
<Filter>Header Files\stdlib</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp"> <ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp">
<Filter>Header Files\stdlib</Filter> <Filter>Header Files\stdlib</Filter>
</ClInclude> </ClInclude>

View File

@ -530,6 +530,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClCompile Include="src\smtp\mail_message.cpp" /> <ClCompile Include="src\smtp\mail_message.cpp" />
<ClCompile Include="src\smtp\smtp_client.cpp" /> <ClCompile Include="src\smtp\smtp_client.cpp" />
<ClCompile Include="src\stdlib\atomic.cpp" /> <ClCompile Include="src\stdlib\atomic.cpp" />
<ClCompile Include="src\stdlib\bitmap.cpp" />
<ClCompile Include="src\stdlib\charset_conv.cpp" /> <ClCompile Include="src\stdlib\charset_conv.cpp" />
<ClCompile Include="src\stdlib\dbuf_pool.cpp" /> <ClCompile Include="src\stdlib\dbuf_pool.cpp" />
<ClCompile Include="src\stdlib\dns_service.cpp" /> <ClCompile Include="src\stdlib\dns_service.cpp" />
@ -707,6 +708,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\smtp\mail_message.hpp" /> <ClInclude Include="include\acl_cpp\smtp\mail_message.hpp" />
<ClInclude Include="include\acl_cpp\smtp\smtp_client.hpp" /> <ClInclude Include="include\acl_cpp\smtp\smtp_client.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\atomic.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\atomic.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\bitmap.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\dbuf_pool.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\dbuf_pool.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\dns_service.hpp" /> <ClInclude Include="include\acl_cpp\stdlib\dns_service.hpp" />

View File

@ -124,6 +124,9 @@
<ClCompile Include="src\stdlib\atomic.cpp"> <ClCompile Include="src\stdlib\atomic.cpp">
<Filter>Source Files\stdlib</Filter> <Filter>Source Files\stdlib</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\stdlib\bitmap.cpp">
<Filter>Source Files\stdlib</Filter>
</ClCompile>
<ClCompile Include="src\stdlib\charset_conv.cpp"> <ClCompile Include="src\stdlib\charset_conv.cpp">
<Filter>Source Files\stdlib</Filter> <Filter>Source Files\stdlib</Filter>
</ClCompile> </ClCompile>
@ -777,6 +780,9 @@
<ClInclude Include="include\acl_cpp\stdlib\atomic.hpp"> <ClInclude Include="include\acl_cpp\stdlib\atomic.hpp">
<Filter>Header Files\stdlib</Filter> <Filter>Header Files\stdlib</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="include\acl_cpp\stdlib\bitmap.hpp">
<Filter>Header Files\stdlib</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp"> <ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp">
<Filter>Header Files\stdlib</Filter> <Filter>Header Files\stdlib</Filter>
</ClInclude> </ClInclude>

View File

@ -51,6 +51,11 @@ bool thread_cond::notify(void)
return acl_pthread_cond_signal(cond_) == 0; return acl_pthread_cond_signal(cond_) == 0;
} }
bool thread_cond::notify_all(void)
{
return acl_pthread_cond_broadcast(cond_) == 0;
}
bool thread_cond::wait(long long microseconds /* = 0 */) bool thread_cond::wait(long long microseconds /* = 0 */)
{ {
#define SEC_TO_NS 1000000000 // nanoseconds per second #define SEC_TO_NS 1000000000 // nanoseconds per second