php实现bitmap

Bitmap

(1)bitmap原理
所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素。由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省。

(2)应用范围
Bitmap算法可用于处理大量数据的排序、查询以及去重。缓存系统中的 布隆过滤器 就是利用了bitmap算法实现。实际应用中一般超过亿级应用使用

(3)php实现bitmap进行数组排序

<?php
/**
 * +----------------------------------------------------------------------
 * |Created by PhpStorm.
 * +----------------------------------------------------------------------
 * |User: gongxulei <email:790707988@qq.com>
 * +----------------------------------------------------------------------
 * |Date: 2020/9/12
 * +----------------------------------------------------------------------
 * |Time: 3:06 下午
 * +----------------------------------------------------------------------
 */

class Bitmap
{
    private $max_number;
    private $bitmapArr;
    private $php_int_bit_size;
    private $outputSortArr = [];

    public function __construct(int $max_number = 10)
    {
        $this->max_number = $max_number;
        //设置php bitmap存储数组
        $this->bitmapArr = array_fill(0, $max_number, 0);
        //php中默认 int类型占用空间为8byte,即:64bit。如果数组个数设置为的10个,则存储最大不能超过 10 * 64 = 640;
        $this->php_int_bit_size = PHP_INT_SIZE * 8;
    }


    public function shiftBit(array $sortArray = [])
    {
        foreach ($sortArray as $value) {
            if ($this->php_int_bit_size * $this->max_number < $value) {
                throw new \Exception('排序数组中' . $value . '超出最大值,请设置合适的数组', 400);
            }
            $quotient = $value / $this->php_int_bit_size;
            $quotient = floor($quotient);
            $remainde = $value % $this->php_int_bit_size;
            $this->bitmapArr[$quotient] = $this->bitmapArr[$quotient] | (1 << $remainde);
        }
        return $this->bitmapArr;
    }


    public function outputSortResult()
    {
        foreach ($this->bitmapArr as $key => $item) {
            for ($i = 0; $i < $this->php_int_bit_size; $i++) {
                if ((1 << $i) & $item) {
                    //存在数字
                    $this->outputSortArr[] = $key * $this->php_int_bit_size + $i;
                }
            }
        }
        return $this->outputSortArr;
    }


}

测试结果:

$BitMap = new Bitmap(20);
$BitMap->shiftBit([10, 6, 3, 4, 5, 8, 2, 3, 600, 100, 640, 400, 200, 350]);
$sortResult = $BitMap->outputSortResult();
var_dump($sortResult);

array(13) {
  [0]=>
  int(2)
  [1]=>
  int(3)
  [2]=>
  int(4)
  [3]=>
  int(5)
  [4]=>
  int(6)
  [5]=>
  int(8)
  [6]=>
  int(10)
  [7]=>
  int(100)
  [8]=>
  int(200)
  [9]=>
  int(350)
  [10]=>
  int(400)
  [11]=>
  int(600)
  [12]=>
  int(640)
}

redis设置布隆过滤器

如项目A的布隆过滤器key为 bitmap_projetcA
通过hash函数将缓存key转换为数字,然后设置bitmap

127.0.0.1:7001 > setbit bitmap_projetcA 10 1
127.0.0.1:7001 > getbit bitmap_projetcA 10
(integer) 1

注意:由于存在hash碰撞,因此在获取bitmap中值如果存在,那实际不一定存在,如果不存在,那就一定不存在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BITMAP调度算法是一种常用的内存管理算法,用于管理分配和释放内存块。下面是一个使用C语言实现BITMAP调度算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_BLOCKS 1024 #define BLOCK_SIZE 4096 typedef struct { unsigned char* bitmap; unsigned char* memory; } BitmapScheduler; BitmapScheduler* createScheduler() { BitmapScheduler* scheduler = (BitmapScheduler*)malloc(sizeof(BitmapScheduler)); scheduler->bitmap = (unsigned char*)calloc(MAX_BLOCKS / 8, sizeof(unsigned char)); scheduler->memory = (unsigned char*)malloc(MAX_BLOCKS * BLOCK_SIZE * sizeof(unsigned char)); return scheduler; } void destroyScheduler(BitmapScheduler* scheduler) { free(scheduler->bitmap); free(scheduler->memory); free(scheduler); } void* allocateBlock(BitmapScheduler* scheduler) { for (int i = 0; i < MAX_BLOCKS; i++) { int byteIndex = i / 8; int bitIndex = i % 8; if ((scheduler->bitmap[byteIndex] & (1 << bitIndex)) == 0) { scheduler->bitmap[byteIndex] |= (1 << bitIndex); return scheduler->memory + (i * BLOCK_SIZE); } } return NULL; } void freeBlock(BitmapScheduler* scheduler, void* block) { int blockIndex = ((unsigned char*)block - scheduler->memory) / BLOCK_SIZE; int byteIndex = blockIndex / 8; int bitIndex = blockIndex % 8; scheduler->bitmap[byteIndex] &= ~(1 << bitIndex); } void printBitmap(BitmapScheduler* scheduler) { printf("Bitmap:\n"); for (int i = 0; i < MAX_BLOCKS; i++) { int byteIndex = i / 8; int bitIndex = i % 8; bool allocated = (scheduler->bitmap[byteIndex] & (1 << bitIndex)) != 0; printf("%d ", allocated); if ((i + 1) % 32 == 0) { printf("\n"); } } } int main() { BitmapScheduler* scheduler = createScheduler(); // 分配内存块 void* block1 = allocateBlock(scheduler); void* block2 = allocateBlock(scheduler); void* block3 = allocateBlock(scheduler); // 释放内存块 freeBlock(scheduler, block2); // 打印位图 printBitmap(scheduler); destroyScheduler(scheduler); return 0; } ``` 这个示例代码实现BITMAP调度算法的内存分配和释放功能。通过`allocateBlock`函数可以分配一个内存块,通过`freeBlock`函数可以释放一个内存块。`printBitmap`函数用于打印当前的位图情况。 希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值