2018-08-26

Bitmap算法


我们可能在算法书中都看过,对于海量数据的处理是有一些独特的算法的,通常来说如下六种:


序号 算法

1 分而治之/hash映射 + hash统计 + 堆/快速/归并排序

2 双层桶划分

3 Bloom filter/Bitmap

4 Trie树/数据库/倒排索引

5 外排序

6 分布式处理之Hadoop/Mapreduce

这里我介绍的是Bitmap,BitMap就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素,该方法在快速查找、去重、排序、压缩数据上都有应用

假设我们要对0-7内的5个元素(4,7,2,5,3)排序,因为要表示8个数,我们就只需要8个bit(1Bytes)。


元素 无 无 2 3 4 5 无 7

占位 × × √ √ √ √ × √

地址 0 1 2 3 4 5 6 7

这样就排序完成了,该方法难在数对二进制位的映射,因为类型到底多长是和平台和环境有关的,我们假定int是32bit,之后假设我们现在有320个数据需要排序,则int a[1+10],a[0]可以表示从0-31的共32个数字,a[1]可以表示从32-61的共32个数字,我们可以想象这是一个二位数组,但是其实并不是

我们可以很容易得出,对于一个十进制数n,对应在数组a[n/32][n%32]中


# encoding: utf-8


from collections import namedtuple

from copy import copy


Colour = namedtuple('Colour','r,g,b')

Colour.copy = lambda self: copy(self)


black = Colour(0,0,0)

white = Colour(255,255,255) # Colour ranges are not enforced.


class Bitmap():

    def __init__(self, width = 40, height = 40, background=white):

        assert width > 0 and height > 0 and type(background) == Colour

        self.width = width

        self.height = height

        self.background = background

        self.map = [[background.copy() for w in range(width)] for h in range(height)]


    def fillrect(self, x, y, width, height, colour=black):

        assert x >= 0 and y >= 0 and width > 0 and height > 0 and type(colour) == Colour

        for h in range(height):

            for w in range(width):

                self.map[y+h][x+w] = colour.copy()


    def chardisplay(self):

        txt = [''.join(' ' if bit==self.background else '@'

                      for bit in row)

              for row in self.map]

        # Boxing

        txt = ['|'+row+'|' for row in txt]

        txt.insert(0, '+' + '-' * self.width + '+')

        txt.append('+' + '-' * self.width + '+')

        print('\n'.join(reversed(txt)))


    def set(self, x, y, colour=black):

        assert type(colour) == Colour

        self.map[y][x]=colour


    def get(self, x, y):

        return self.map[y][x]



bitmap = Bitmap(20,10)

bitmap.fillrect(4, 5, 6, 3)

assert bitmap.get(5, 5) == black

assert bitmap.get(0, 1) == white

bitmap.set(0, 1, black)

assert bitmap.get(0, 1) == black

bitmap.chardisplay()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 现在分析到YYImage 首先看文件 YYImage YYFrameImage YYSpriteSheetImag...
    充满活力的早晨阅读 2,619评论 0 3
  • 留山镇中四制四考方案(试行) 为了充分调动和发挥广大教师献身教育事业的积极性和创造性,全面提高教育教学质量,特制定...
    阿伟_斫轮之手阅读 534评论 0 0
  • 8月25日 万妮亚复盘267天 一、健康管理(目标11:00-6:00) 2:30-7:30起床睡,早睡重度患者 ...
    敢比会重要阅读 153评论 0 0
  • It's Sunday. Laura stayed close to the house. She could s...
    Mr_Oldman阅读 86评论 0 0
  • 一直不知道怎么称呼他,不知道该给他起个什么昵称。他总能给我带来惊喜,总是用37°温暖我的心,总能猜透我心中所想。会...
    小小点滴阅读 231评论 0 0