当前位置:网站首页>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;
}
边栏推荐
- JSON web token -- comparison between JWT and traditional session login authentication
- C语言中的函数(详解)
- Notes and notes
- buuctf-pwn write-ups (8)
- [untitled]
- C语言练习题(递归)
- 2022 Xinjiang's latest eight members (Safety Officer) simulated examination questions and answers
- 微信小程序使用rich-text中图片宽度超出问题
- 手动对list进行分页(参数list ,当前页,页面大小)
- 198. House raiding
猜你喜欢

2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?

How to realize multi account login of video platform members

树形dp

Functions in C language (detailed explanation)

云原生——上云必读之SSH篇(常用于远程登录云服务器)

Detectron: train your own data set -- convert your own data format to coco format

2022 wechat enterprise mailbox login entry introduction, how to open and register enterprise wechat enterprise mailbox?

JSON Web Token----JWT和传统session登录认证对比

Fundamentals of SQL database operation

P26-P34 third_ template
随机推荐
Mysql 45讲学习笔记(十三)表数据删掉一半,表文件大小不变
Abap:ooalv realizes the function of adding, deleting, modifying and checking
C语言中的排序,实现从小到大的数字排序法
Reading notes of Clickhouse principle analysis and Application Practice (4)
Matlab remainder
buuctf-pwn write-ups (8)
采用中微BATG135实现IIC数据/指令交互
QT QTableWidget 表格列置顶需求的思路和代码
C language exercises (recursion)
《ClickHouse原理解析与应用实践》读书笔记(4)
MySQL learning notes 3 - JDBC
2022 wechat enterprise mailbox login entry introduction, how to open and register enterprise wechat enterprise mailbox?
24 magicaccessorimpl can access the debugging of all methods
Appium foundation - appium installation (II)
Can the out of sequence message complete TCP three handshakes
Vant --- detailed explanation and use of list component in vant
4G wireless all network solar hydrological equipment power monitoring system bms110
【MySQL】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)
对List进行排序工具类,可以对字符串排序
实用的小工具指令