mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-30 10:57:34 +08:00
add lost files
This commit is contained in:
parent
6c7fa88358
commit
3554fd6b25
117
lib_acl_cpp/src/stdlib/bitmap.cpp
Normal file
117
lib_acl_cpp/src/stdlib/bitmap.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
#include "acl_stdafx.hpp"
|
||||
#ifndef ACL_PREPARE_COMPILE
|
||||
#include "acl_cpp/stdlib/bitmap.hpp"
|
||||
#endif
|
||||
#include "acl_cpp/stdlib/bitmap.hpp"
|
||||
|
||||
/**
|
||||
* @author wanghaibin@qiyi.com
|
||||
* @date 2017-08-16
|
||||
*/
|
||||
namespace acl {
|
||||
|
||||
bitmap::bitmap(const void* buf, size_t len)
|
||||
: size_(len)
|
||||
, count_(0)
|
||||
{
|
||||
assert(len > 0);
|
||||
|
||||
bmp_ = new unsigned char[(len + 7) / 8];
|
||||
memcpy(bmp_, buf, (len + 7) / 8);
|
||||
recount();
|
||||
}
|
||||
|
||||
bitmap::bitmap(size_t len)
|
||||
: size_(len)
|
||||
, count_(0)
|
||||
{
|
||||
assert(len > 0);
|
||||
|
||||
bmp_ = new unsigned char[(len + 7) / 8];
|
||||
memset(bmp_, 0, (len + 7) / 8);
|
||||
}
|
||||
|
||||
bitmap::~bitmap(void)
|
||||
{
|
||||
delete[] bmp_;
|
||||
}
|
||||
|
||||
bool bitmap::isset(size_t n)
|
||||
{
|
||||
if (n < size_)
|
||||
return (bmp_[n / 8] >> n % 8) & 1;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool bitmap::set(size_t n)
|
||||
{
|
||||
if (n < size_ && !isset(n)) {
|
||||
unsigned char t = 1 << n % 8;
|
||||
bmp_[n / 8] |= t;
|
||||
count_++;
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool bitmap::unset(size_t n)
|
||||
{
|
||||
if (isset(n)) {
|
||||
unsigned char t = 1 << n % 8;
|
||||
bmp_[n / 8] &= ~t;
|
||||
count_--;
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t bitmap::tobuf(void* buf, size_t len)
|
||||
{
|
||||
if (len >= (size_ + 7) / 8) {
|
||||
memcpy(buf, bmp_, (size_ + 7) / 8);
|
||||
return size_;
|
||||
}
|
||||
return size_;
|
||||
}
|
||||
|
||||
bool bitmap::frombuf(const void* buf, size_t len)
|
||||
{
|
||||
if (len >= (size_ + 7) / 8) {
|
||||
memcpy(bmp_, buf, (size_ + 7) / 8);
|
||||
recount();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void bitmap::reset(void)
|
||||
{
|
||||
memset(bmp_, 0, (size_ + 7) / 8);
|
||||
}
|
||||
|
||||
size_t bitmap::size(void)
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
size_t bitmap::count(void)
|
||||
{
|
||||
return count_;
|
||||
}
|
||||
|
||||
bool bitmap::full(void)
|
||||
{
|
||||
return size_ == count_;
|
||||
}
|
||||
|
||||
void bitmap::recount(void)
|
||||
{
|
||||
count_ = 0;
|
||||
for (size_t i = 0; i < size_; ++i) {
|
||||
if ((bmp_[i / 8] >> i % 8) & 1)
|
||||
count_++;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace acl
|
Loading…
Reference in New Issue
Block a user