-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbm_ops.py
More file actions
58 lines (46 loc) · 1.02 KB
/
bm_ops.py
File metadata and controls
58 lines (46 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def list_to_bm(list):
bm = 0
for elm in list:
bm = bm | (1 << elm)
return bm
def bm_to_list(bm):
list = []
tmp_bm = bm
count = 0
while tmp_bm != 0:
if tmp_bm & 1:
list.append(count)
count = count + 1
tmp_bm = tmp_bm >> 1
return list
def list_of_list_to_bm_list(list):
bm_list = []
for elm in list:
bm_list.append(list_to_bm(elm))
return bm_list
def bm_list_to_list_of_list(bm_list):
list_of_list = []
for bm in bm_list:
list_of_list.append(bm_to_list(bm))
return list_of_list
def bm_in(elm, bm):
if bm & (1 << elm):
return True
else:
return False
def bm(elm):
return (1 << elm)
def bm_insert(bm, elm):
bm = bm | (1 << elm)
return bm
def bm_rm(bm, elm):
if bm_in(elm, bm):
bm = bm ^ (1 << elm)
return bm
def bm_is_subset(bm1, bm2):
if (bm1 & bm2) ^ bm1 == 0:
return True
else:
return False
def bm_intersection(bm1, bm2):
return bm1 & bm2