Bitmap is a very common data structure, such as for Bloom filter, for sorting without repeating integers, and so on. Bitmap are usually based on arrays, and each element in an array can be viewed as a series of binary numbers, all elements constituting a larger binary set. For Python, the integer type is a signed type by default, so the number of available digits for an integer is 31 bits.
Bitmap realization Idea
Bitmap is used to manipulate each one. For example, a Python array contains 4 32-bit signed integers, with a total of 4 * 31 = 124 bits available. If you want to operate on the 90th bits, get the first element of the action array, get the corresponding bit index, and then perform the action.
The figure above shows a 32-bit integer, which is signed by default in Python, with the highest bit being the symbol bit and bitmap cannot use it. On the left is the high, the right is low, the lowest digit is the No. 0 place.
Bitmap is used to manipulate each one. For example, a Python array contains 4 32-bit signed integers, with a total of 4 * 31 = 124 bits available. If you want to operate on the 90th bits, get the first element of the action array, get the corresponding bit index, and then perform the action.
Initialize bitmap
First you need to initialize the bitmap. Take 90, because a single integer can only use 31 bits, so 90 divided by 31 and rounded up to get a few array elements. The code is as follows:
Copy Code code as follows:
#!/usr/bin/env python
#coding: UTF8
Class Bitmap (object):
def __init__ (self, max):
self.size = Int (max + 31-1) #向上取整
if __name__ = = ' __main__ ':
Bitmap = Bitmap (90)
print ' requires%d elements. '% bitmap.size
Copy Code code as follows:
$ python bitmap.py
Requires 3 elements.
To evaluate an index in an array
The index evaluated in the array is the same as the size of the previously computed array. Just before the maximum number is calculated, it is now replaced by any integer that needs to be stored. But one difference is that the index in the array is rounded down, so the implementation of the Calcelemindex method needs to be modified. The code should read as follows:
Copy Code code as follows:
#!/usr/bin/env python
#coding: UTF8
Class Bitmap (object):
def __init__ (self, max):
Self.size = Self.calcelemindex (max, True)
Self.array = [0 for I in range (self.size)]
def calcelemindex (self, num, up=false):
' Up ' is true to take the whole up, otherwise it will be rounded down '
If up:
return int ((num + 31-1)/#向上取整
Return NUM/31
if __name__ = = ' __main__ ':
Bitmap = Bitmap (90)
Print ' array requires%d elements. '% bitmap.size
print ' 47 should be stored on the first%d array element. '% Bitmap.calcelemindex (47)
Copy Code code as follows:
$ python bitmap.py
The array requires 3 elements.
47 should be stored on the 1th array element.
So it is important to get the largest integer, otherwise it is possible to create an array that holds some data.
To evaluate the bit index in an array element
The bit index in an array element can be obtained by modulo operations. The number of integers to be stored and 31 modulo can get the bit index. The code should read as follows:
Copy Code code as follows:
#!/usr/bin/env python
#coding: UTF8
Class Bitmap (object):
def __init__ (self, max):
Self.size = Self.calcelemindex (max, True)
Self.array = [0 for I in range (self.size)]
def calcelemindex (self, num, up=false):
' Up ' is true to take the whole up, otherwise it will be rounded down '
If up:
return int ((num + 31-1)/#向上取整
Return NUM/31
def calcbitindex (self, num):
Return num% 31
if __name__ = = ' __main__ ':
Bitmap = Bitmap (90)
Print ' array requires%d elements. '% bitmap.size
print ' 47 should be stored on the first%d array element. '% Bitmap.calcelemindex (47)
print ' 47 should be stored on the top%d of the first%d array of elements. '% (Bitmap.calcelemindex ($), Bitmap.calcbitindex (47),)
Don't forget to count from the No. 0 place.
Set 1 operation
The bits default is 0, and a location of 1 indicates that the data is stored in this bit. The code should read as follows:
Copy Code code as follows:
#!/usr/bin/env python
#coding: UTF8
Class Bitmap (object):
def __init__ (self, max):
Self.size = Self.calcelemindex (max, True)
Self.array = [0 for I in range (self.size)]
def calcelemindex (self, num, up=false):
' Up ' is true to take the whole up, otherwise it will be rounded down '
If up:
return int ((num + 31-1)/#向上取整
Return NUM/31
def calcbitindex (self, num):
Return num% 31
def set (self, num):
Elemindex = Self.calcelemindex (num)
Byteindex = Self.calcbitindex (num)
Elem = Self.array[elemindex]
Self.array[elemindex] = Elem | (1 << byteindex)
if __name__ = = ' __main__ ':
Bitmap = Bitmap (90)
Bitmap.set (0)
Print Bitmap.array
Since the No. 0 digit, so if you need to store 0, you need to place the No. 0 position 1.
Clear 0 operation
The stored data is discarded at a location of 0. The code is as follows:
Copy Code code as follows:
#!/usr/bin/env python
#coding: UTF8
Class Bitmap (object):
def __init__ (self, max):
Self.size = Self.calcelemindex (max, True)
Self.array = [0 for I in range (self.size)]
def calcelemindex (self, num, up=false):
' Up ' is true to take the whole up, otherwise it will be rounded down '
If up:
return int ((num + 31-1)/#向上取整
Return NUM/31
def calcbitindex (self, num):
Return num% 31
def set (self, num):
Elemindex = Self.calcelemindex (num)
Byteindex = Self.calcbitindex (num)
Elem = Self.array[elemindex]
Self.array[elemindex] = Elem | (1 << byteindex)
def clean (self, i):
Elemindex = Self.calcelemindex (i)
Byteindex = Self.calcbitindex (i)
Elem = Self.array[elemindex]
Self.array[elemindex] = Elem & (~ (1 << byteindex))
if __name__ = = ' __main__ ':
Bitmap = Bitmap (87)
Bitmap.set (0)
Bitmap.set (34)
Print Bitmap.array
Bitmap.clean (0)
Print Bitmap.array
Bitmap.clean (34)
Print Bitmap.array
Clear 0 and 1 are reciprocal operations.
Test whether a bit is 1
Determine if a bit is 1 to take out the data stored previously. The code is as follows:
Copy Code code as follows:
#!/usr/bin/env python
#coding: UTF8
Class Bitmap (object):
def __init__ (self, max):
Self.size = Self.calcelemindex (max, True)
Self.array = [0 for I in range (self.size)]
def calcelemindex (self, num, up=false):
' Up ' is true to take the whole up, otherwise it will be rounded down '
If up:
return int ((num + 31-1)/#向上取整
Return NUM/31
def calcbitindex (self, num):
Return num% 31
def set (self, num):
Elemindex = Self.calcelemindex (num)
Byteindex = Self.calcbitindex (num)
Elem = Self.array[elemindex]
Self.array[elemindex] = Elem | (1 << byteindex)
def clean (self, i):
Elemindex = Self.calcelemindex (i)
Byteindex = Self.calcbitindex (i)
Elem = Self.array[elemindex]
Self.array[elemindex] = Elem & (~ (1 << byteindex))
def test (self, i):
Elemindex = Self.calcelemindex (i)
Byteindex = Self.calcbitindex (i)
If Self.array[elemindex] & (1 << byteindex):
Return True
Return False
if __name__ = = ' __main__ ':
Bitmap = Bitmap (90)
Bitmap.set (0)
Print Bitmap.array
Print Bitmap.test (0)
Bitmap.set (1)
Print Bitmap.test (1)
Print Bitmap.test (2)
Bitmap.clean (1)
Print Bitmap.test (1)
Copy Code code as follows:
$ python bitmap.py
[1, 0, 0]
True
True
False
False
Next, you implement the sort of a repeating array. The maximum element of a unordered nonnegative integer array is known to be 879, which is sorted naturally. The code is as follows:
Copy Code code as follows:
#!/usr/bin/env python
#coding: UTF8
Class Bitmap (object):
def __init__ (self, max):
Self.size = Self.calcelemindex (max, True)
Self.array = [0 for I in range (self.size)]
def calcelemindex (self, num, up=false):
' Up ' is true to take the whole up, otherwise it will be rounded down '
If up:
return int ((num + 31-1)/#向上取整
Return NUM/31
def calcbitindex (self, num):
Return num% 31
def set (self, num):
Elemindex = Self.calcelemindex (num)
Byteindex = Self.calcbitindex (num)
Elem = Self.array[elemindex]
Self.array[elemindex] = Elem | (1 << byteindex)
def clean (self, i):
Elemindex = Self.calcelemindex (i)
Byteindex = Self.calcbitindex (i)
Elem = Self.array[elemindex]
Self.array[elemindex] = Elem & (~ (1 << byteindex))
def test (self, i):
Elemindex = Self.calcelemindex (i)
Byteindex = Self.calcbitindex (i)
If Self.array[elemindex] & (1 << byteindex):
Return True
Return False
if __name__ = = ' __main__ ':
MAX = 879
Suffle_array = [45, 2, 78, 35, 67, 90, 879, 0, 340, 123, 46]
result = []
Bitmap = Bitmap (MAX)
For num in Suffle_array:
Bitmap.set (num)
For I in range (MAX + 1):
If Bitmap.test (i):
Result.append (i)
print ' original array is:%s '% Suffle_array
print ' sorted array:%s '% result
Bitmap is implemented, it is easy to sort by using it. Other languages can also be implemented bitmap, but for static type language, such as C/golang, such as the language, because you can directly declare unsigned integer, so the available bits into 32-bit, just the code above 31 to 32 can be, this point please note.