当前位置:网站首页>Tar source code analysis Part 2
Tar source code analysis Part 2
2022-07-04 06:36:00 【Tao song remains the same】
bitmap It's a bitmap , Essential when dealing with big data , Let's look at the implementation :
#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;
}
// Check version
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;
}
边栏推荐
- 金盾视频播放器拦截的软件关键词和进程信息
- Manually page the list (parameter list, current page, page size)
- ABAP:OOALV实现增删改查功能
- The sorting in C language realizes the number sorting method from small to large
- tars源码分析之2
- How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
- C语言中的排序,实现从小到大的数字排序法
- thread priority
- Explain in one sentence what social proof is
- QT releases multilingual International Translation
猜你喜欢

Functions in C language (detailed explanation)

GoogleChromePortable 谷歌chrome浏览器便携版官网下载方式

How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?

uniapp 自定义环境变量

P26-P34 third_ template

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

树形dp

List of top ten professional skills required for data science work

Download kicad on Alibaba cloud image station

Learning multi-level structural information for small organ segmentation
随机推荐
Common JS tool Libraries
What is the "relative dilemma" in cognitive fallacy?
Variables d'environnement personnalisées uniapp
Common usage of time library
How to use multithreading to export excel under massive data? Source code attached!
Summary of leetcode BFS question brushing
Learning multi-level structural information for small organ segmentation
tars源码分析之8
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?
uniapp 自定義環境變量
leetcode 310. Minimum Height Trees
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Download kicad on Alibaba cloud image station
The sorting in C language realizes the number sorting method from small to large
JS execution mechanism
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Json Web token - jwt vs. Traditional session login Authentication
11. Dimitt's law
Dimension and format of data
C language exercises (recursion)