From 3554fd6b2560c05062178ef6828a626149d17950 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Thu, 24 Aug 2017 09:09:05 +0800 Subject: [PATCH] add lost files --- lib_acl_cpp/src/stdlib/bitmap.cpp | 117 ++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 lib_acl_cpp/src/stdlib/bitmap.cpp diff --git a/lib_acl_cpp/src/stdlib/bitmap.cpp b/lib_acl_cpp/src/stdlib/bitmap.cpp new file mode 100644 index 000000000..df978ce7e --- /dev/null +++ b/lib_acl_cpp/src/stdlib/bitmap.cpp @@ -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