位图数据结构
文章目錄
- 1 位圖數據結構
- 1.1 位圖定義
- 1.2 位圖實現
1 位圖數據結構
1.1 位圖定義
位圖概述:
- 位圖是一組連續的標志位,每一位用于標識某種狀態的有無。
操作接口:
- 初始化:將所有的位清零。
- 設置某1位。
- 清除某1位。
- 尋找第一個置位的位置(從第0位開始)。
尋找第一個置位的位置:
- 移位測試(稍慢)。
- 分組查表(較快)。
1.2 位圖實現
位圖結構定義:
位圖初始化:
設置指定位:
清除指定位:
尋找第一個置位的位置(從第0位開始):
tLib.h:
tBitmap.c:
/*************************************** Copyright (c)****************************************************** ** File name : tBitmap.c ** Latest modified Date : 2016-06-01 ** Latest Version : 0.1 ** Descriptions : tinyOS所用的位圖數據結構。 ** **-------------------------------------------------------------------------------------------------------- ** Created by : 01課堂 lishutong ** Created date : 2016-06-01 ** Version : 1.0 ** Descriptions : The original version ** **-------------------------------------------------------------------------------------------------------- ** Copyright : 版權所有,禁止用于商業用途 ** Author Blog : http://ilishutong.com **********************************************************************************************************/ #include "tLib.h"/********************************************************************************************************** ** Function name : tBitmapInit ** Descriptions : 初始化bitmap將所有的位全清0 ** parameters : 無 ** Returned value : 無 ***********************************************************************************************************/ void tBitmapInit (tBitmap * bitmap) {bitmap->bitmap = 0; }/********************************************************************************************************** ** Function name : tBitmapPosCount ** Descriptions : 返回最大支持的位置數量 ** parameters : 無 ** Returned value : 最大支持的位置數量 ***********************************************************************************************************/ uint32_t tBitmapPosCount (void) {return 32; }/********************************************************************************************************** ** Function name : tBitmapSet ** Descriptions : 設置bitmap中的某個位 ** parameters : pos 需要設置的位 ** Returned value : 無 ***********************************************************************************************************/ void tBitmapSet (tBitmap * bitmap, uint32_t pos) {bitmap->bitmap |= 1 << pos; }/********************************************************************************************************** ** Function name : tBitmapClear ** Descriptions : 清除bitmap中的某個位 ** parameters : pos 需要清除的位 ** Returned value : 無 ***********************************************************************************************************/ void tBitmapClear (tBitmap * bitmap, uint32_t pos) {bitmap->bitmap &= ~(1 << pos); }/********************************************************************************************************** ** Function name : tBitmapGetFirstSet ** Descriptions : 從位圖中第0位開始查找,找到第1個被設置的位置序號 ** parameters : 無 ** Returned value : 第1個被設置的位序號 ***********************************************************************************************************/ uint32_t tBitmapGetFirstSet (tBitmap * bitmap) {static const uint8_t quickFindTable[] = {/* 00 */ 0xff, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,/* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0};if (bitmap->bitmap & 0xff){return quickFindTable[bitmap->bitmap & 0xff]; }else if (bitmap->bitmap & 0xff00){return quickFindTable[(bitmap->bitmap >> 8) & 0xff] + 8; }else if (bitmap->bitmap & 0xff0000){return quickFindTable[(bitmap->bitmap >> 16) & 0xff] + 16; }else if (bitmap->bitmap & 0xFF000000){return quickFindTable[(bitmap->bitmap >> 24) & 0xFF] + 24;}else{return tBitmapPosCount();} }參考資料:
總結
- 上一篇: 文本编辑器实现打开文件、保存文件、文件另
- 下一篇: 一加哈苏合作 建立四大影像研发中心