关键词搜索

源码搜索 ×
×

tars源码分析之2

发布2022-07-03浏览379次

详情内容

bitmap就是位图,在处理大数据时必不可少,来看下实现:

  1. #include "util/tc_bitmap.h"
  2. #include "util/tc_common.h"
  3. #include <cassert>
  4. #include <string.h>
  5. #include <iostream>
  6. namespace tars
  7. {
  8. const int TC_BitMap::BitMap::_magic_bits[8]={0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};
  9. size_t TC_BitMap::BitMap::calcMemSize(size_t iElementCount)
  10. {
  11. assert(iElementCount > 0);
  12. iElementCount--;
  13. size_t iMemSize = iElementCount/8+1;
  14. iMemSize += sizeof(tagBitMapHead);
  15. return iMemSize;
  16. }
  17. void TC_BitMap::BitMap::create(void *pAddr, size_t iSize)
  18. {
  19. memset((char*)pAddr, 0, iSize);
  20. _pHead = static_cast<tagBitMapHead*>(pAddr);
  21. _pHead->_cVersion = BM_VERSION;
  22. _pHead->_iMemSize = iSize;
  23. _pData = (unsigned char*)pAddr + sizeof(tagBitMapHead);
  24. }
  25. int TC_BitMap::BitMap::connect(void *pAddr, size_t iSize)
  26. {
  27. _pHead = static_cast<tagBitMapHead*>(pAddr);
  28. if(_pHead->_cVersion != BM_VERSION)
  29. {
  30. return -1;
  31. }
  32. if(iSize != _pHead->_iMemSize)
  33. {
  34. return -2;
  35. }
  36. _pData = (unsigned char*)pAddr + sizeof(tagBitMapHead);
  37. return 0;
  38. }
  39. int TC_BitMap::BitMap::get(size_t i)
  40. {
  41. if(i/8 >= (_pHead->_iMemSize-sizeof(tagBitMapHead)))
  42. {
  43. return -1;
  44. }
  45. unsigned char* p =_pData + i/8;
  46. return _get_bit(*p, i%8)>0?1:0;
  47. }
  48. int TC_BitMap::BitMap::set(size_t i)
  49. {
  50. if(i/8 >= (_pHead->_iMemSize-sizeof(tagBitMapHead)))
  51. {
  52. return -1;
  53. }
  54. unsigned char* p=(unsigned char*)_pData + i/8;
  55. *p = _set_bit(*p, i%8);
  56. return (int)(*p)>0?1:0;
  57. }
  58. int TC_BitMap::BitMap::clear(size_t i)
  59. {
  60. if(i/8 >= (_pHead->_iMemSize-sizeof(tagBitMapHead)))
  61. {
  62. return -1;
  63. }
  64. unsigned char* p = (unsigned char*)_pData + i/8;
  65. *p = _clear_bit(*p, i%8);
  66. return (int)(*p)>0?1:0;
  67. }
  68. int TC_BitMap::BitMap::clear4all()
  69. {
  70. memset(_pData, 0, _pHead->_iMemSize-sizeof(tagBitMapHead));
  71. return 0;
  72. }
  73. int TC_BitMap::BitMap::dump2file(const string &sFile)
  74. {
  75. FILE *fp = fopen(sFile.c_str(), "wb");
  76. if(fp == NULL)
  77. {
  78. return -1;
  79. }
  80. size_t ret = fwrite((void*)_pHead, 1, _pHead->_iMemSize, fp);
  81. fclose(fp);
  82. if(ret == _pHead->_iMemSize)
  83. {
  84. return 0;
  85. }
  86. return -1;
  87. }
  88. int TC_BitMap::BitMap::load5file(const string &sFile)
  89. {
  90. FILE *fp = fopen(sFile.c_str(), "rb");
  91. if(fp == NULL)
  92. {
  93. return -1;
  94. }
  95. fseek(fp, 0L, SEEK_END);
  96. size_t fs = ftell(fp);
  97. if(fs != _pHead->_iMemSize)
  98. {
  99. fclose(fp);
  100. return -2;
  101. }
  102. fseek(fp, 0L, SEEK_SET);
  103. size_t iSize = 1024*1024*10;
  104. size_t iLen = 0;
  105. char *pBuffer = new char[iSize];
  106. while(true)
  107. {
  108. int ret = fread(pBuffer, 1, iSize, fp);
  109. if(ret == 0)
  110. {
  111. break;
  112. }
  113. //检查版本
  114. if(iLen == 0)
  115. {
  116. tagBitMapHead *tmp = (tagBitMapHead*)pBuffer;
  117. if(tmp->_cVersion != BM_VERSION)
  118. {
  119. fclose(fp);
  120. delete[] pBuffer;
  121. return -3;
  122. }
  123. if(tmp->_iMemSize != _pHead->_iMemSize)
  124. {
  125. fclose(fp);
  126. delete[] pBuffer;
  127. return -2;
  128. }
  129. }
  130. memcpy((char*)_pHead + iLen, pBuffer, ret);
  131. iLen += ret;
  132. }
  133. fclose(fp);
  134. delete[] pBuffer;
  135. if(iLen != _pHead->_iMemSize)
  136. {
  137. return -2;
  138. }
  139. return 0;
  140. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载