当前位置:网站首页>tars源码分析之2
tars源码分析之2
2022-07-04 06:33:00 【涛歌依旧】
bitmap就是位图,在处理大数据时必不可少,来看下实现:
#include "util/tc_bitmap.h"
#include "util/tc_common.h"
#include <cassert>
#include <string.h>
#include <iostream>
namespace tars
{
const int TC_BitMap::BitMap::_magic_bits[8]={0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};
size_t TC_BitMap::BitMap::calcMemSize(size_t iElementCount)
{
assert(iElementCount > 0);
iElementCount--;
size_t iMemSize = iElementCount/8+1;
iMemSize += sizeof(tagBitMapHead);
return iMemSize;
}
void TC_BitMap::BitMap::create(void *pAddr, size_t iSize)
{
memset((char*)pAddr, 0, iSize);
_pHead = static_cast<tagBitMapHead*>(pAddr);
_pHead->_cVersion = BM_VERSION;
_pHead->_iMemSize = iSize;
_pData = (unsigned char*)pAddr + sizeof(tagBitMapHead);
}
int TC_BitMap::BitMap::connect(void *pAddr, size_t iSize)
{
_pHead = static_cast<tagBitMapHead*>(pAddr);
if(_pHead->_cVersion != BM_VERSION)
{
return -1;
}
if(iSize != _pHead->_iMemSize)
{
return -2;
}
_pData = (unsigned char*)pAddr + sizeof(tagBitMapHead);
return 0;
}
int TC_BitMap::BitMap::get(size_t i)
{
if(i/8 >= (_pHead->_iMemSize-sizeof(tagBitMapHead)))
{
return -1;
}
unsigned char* p =_pData + i/8;
return _get_bit(*p, i%8)>0?1:0;
}
int TC_BitMap::BitMap::set(size_t i)
{
if(i/8 >= (_pHead->_iMemSize-sizeof(tagBitMapHead)))
{
return -1;
}
unsigned char* p=(unsigned char*)_pData + i/8;
*p = _set_bit(*p, i%8);
return (int)(*p)>0?1:0;
}
int TC_BitMap::BitMap::clear(size_t i)
{
if(i/8 >= (_pHead->_iMemSize-sizeof(tagBitMapHead)))
{
return -1;
}
unsigned char* p = (unsigned char*)_pData + i/8;
*p = _clear_bit(*p, i%8);
return (int)(*p)>0?1:0;
}
int TC_BitMap::BitMap::clear4all()
{
memset(_pData, 0, _pHead->_iMemSize-sizeof(tagBitMapHead));
return 0;
}
int TC_BitMap::BitMap::dump2file(const string &sFile)
{
FILE *fp = fopen(sFile.c_str(), "wb");
if(fp == NULL)
{
return -1;
}
size_t ret = fwrite((void*)_pHead, 1, _pHead->_iMemSize, fp);
fclose(fp);
if(ret == _pHead->_iMemSize)
{
return 0;
}
return -1;
}
int TC_BitMap::BitMap::load5file(const string &sFile)
{
FILE *fp = fopen(sFile.c_str(), "rb");
if(fp == NULL)
{
return -1;
}
fseek(fp, 0L, SEEK_END);
size_t fs = ftell(fp);
if(fs != _pHead->_iMemSize)
{
fclose(fp);
return -2;
}
fseek(fp, 0L, SEEK_SET);
size_t iSize = 1024*1024*10;
size_t iLen = 0;
char *pBuffer = new char[iSize];
while(true)
{
int ret = fread(pBuffer, 1, iSize, fp);
if(ret == 0)
{
break;
}
//检查版本
if(iLen == 0)
{
tagBitMapHead *tmp = (tagBitMapHead*)pBuffer;
if(tmp->_cVersion != BM_VERSION)
{
fclose(fp);
delete[] pBuffer;
return -3;
}
if(tmp->_iMemSize != _pHead->_iMemSize)
{
fclose(fp);
delete[] pBuffer;
return -2;
}
}
memcpy((char*)_pHead + iLen, pBuffer, ret);
iLen += ret;
}
fclose(fp);
delete[] pBuffer;
if(iLen != _pHead->_iMemSize)
{
return -2;
}
return 0;
}
边栏推荐
- Which water in the environment needs water quality monitoring
- How to help others effectively
- leetcode 310. Minimum Height Trees
- What is a spotlight effect?
- Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
- [backpack DP] backpack problem
- Lightroom import picture gray / Black rectangular multi display
- FRP intranet penetration, reverse proxy
- Mysql 45讲学习笔记(十)force index
- 【MySQL】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)
猜你喜欢

Functions in C language (detailed explanation)

InputStream/OutputStream(文件的输入输出)

C language - Blue Bridge Cup - Snake filling

Learning multi-level structural information for small organ segmentation
![[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx](/img/a9/72791cbcc6c9da45e89450ab2820c1.jpg)
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx

C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
![[untitled]](/img/32/cfd45bb5e8555ea2ad344161370dbe.png)
[untitled]

Bicolor case

MySQL learning notes 3 - JDBC

C language exercises (recursion)
随机推荐
Internet of things protocol ZigBee ZigBee module uses the concept of protocol stack
Json Web token - jwt vs. Traditional session login Authentication
Uniapp custom environment variables
Operator < <> > fool test case
[number theory] fast power (Euler power)
Mysql 45讲学习笔记(十四)count(*)
MySQL的information_schema数据库
Arcpy uses the updatelayer function to change the symbol system of the layer
Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?
JSON Web Token----JWT和傳統session登錄認證對比
[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)
Dimension and format of data
Understanding of cross domain and how to solve cross domain problems
CORS is not intended to protect API endpoints - nikofischer
Explain in one sentence what social proof is
Nexus 6p downgraded from 8.0 to 6.0+root
Mysql 45讲学习笔记(十一)字符串字段怎么加索引
buuctf-pwn write-ups (8)
剑指 Offer II 038. 每日温度
【问题记录】03 连接MySQL数据库提示:1040 Too many connections