日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

位图数据结构

發布時間:2025/4/5 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 位图数据结构 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 位圖數據結構
      • 1.1 位圖定義
      • 1.2 位圖實現

1 位圖數據結構

1.1 位圖定義

位圖概述:

  • 位圖是一組連續的標志位,每一位用于標識某種狀態的有無。

操作接口:

  • 初始化:將所有的位清零。
  • 設置某1位。
  • 清除某1位。
  • 尋找第一個置位的位置(從第0位開始)。

尋找第一個置位的位置:

  • 移位測試(稍慢)。
  • 分組查表(較快)。

1.2 位圖實現

位圖結構定義:

位圖初始化:

設置指定位:

清除指定位:

尋找第一個置位的位置(從第0位開始):


tLib.h:

/*************************************** Copyright (c)****************************************************** ** File name : tLib.h ** 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 **********************************************************************************************************/ #ifndef TLIB_H #define TLIB_H// 標準頭文件,里面包含了常用的類型定義,如uint32_t #include <stdint.h>// 位圖類型 typedef struct {uint32_t bitmap; }tBitmap;/********************************************************************************************************** ** Function name : tBitmapInit ** Descriptions : 初始化bitmap將所有的位全清0 ** parameters : 無 ** Returned value : 無 ***********************************************************************************************************/ void tBitmapInit (tBitmap * bitmap);/********************************************************************************************************** ** Function name : tBitmapPosCount ** Descriptions : 返回最大支持的位置數量 ** parameters : 無 ** Returned value : 最大支持的位置數量 ***********************************************************************************************************/ uint32_t tBitmapPosCount (void);/********************************************************************************************************** ** Function name : tBitmapSet ** Descriptions : 設置bitmap中的某個位 ** parameters : pos 需要設置的位 ** Returned value : 無 ***********************************************************************************************************/ void tBitmapSet (tBitmap * bitmap, uint32_t pos);/********************************************************************************************************** ** Function name : tBitmapClear ** Descriptions : 清除bitmap中的某個位 ** parameters : pos 需要清除的位 ** Returned value : 無 ***********************************************************************************************************/ void tBitmapClear (tBitmap * bitmap, uint32_t pos);/********************************************************************************************************** ** Function name : tBitmapGetFirstSet ** Descriptions : 從位圖中第0位開始查找,找到第1個被設置的位置序號 ** parameters : 無 ** Returned value : 第1個被設置的位序號 ***********************************************************************************************************/ uint32_t tBitmapGetFirstSet (tBitmap * bitmap);#endif /* 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();} }

參考資料:

  • 【李述銅】從0到1自己動手寫嵌入式操作系統
  • 總結

    以上是生活随笔為你收集整理的位图数据结构的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。