File: ewah_bool_wrap.pyx

package info (click to toggle)
python-ewah-bool-utils 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 448 kB
  • sloc: cpp: 1,709; ansic: 926; python: 249; makefile: 16
file content (1762 lines) | stat: -rw-r--r-- 74,091 bytes parent folder | download | duplicates (2)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
"""
Wrapper for EWAH Bool Array: https://github.com/lemire/EWAHBoolArray



"""


import struct

from cython.operator cimport dereference, preincrement
from libc.stdlib cimport free, malloc
from libcpp.algorithm cimport sort
from libcpp.map cimport map as cmap

import numpy as np

cimport cython
cimport numpy as np
from ewah_bool_utils.morton_utils cimport morton_neighbors_coarse, morton_neighbors_refined


cdef extern from "<algorithm>" namespace "std" nogil:
    Iter unique[Iter](Iter first, Iter last)

cdef np.uint64_t FLAG = ~(<np.uint64_t>0)
cdef np.uint64_t MAX_VECTOR_SIZE = <np.uint64_t>1e7

ctypedef cmap[np.uint64_t, ewah_bool_array] ewahmap
ctypedef cmap[np.uint64_t, ewah_bool_array].iterator ewahmap_it
ctypedef pair[np.uint64_t, ewah_bool_array] ewahmap_p

cdef class FileBitmasks:

    def __cinit__(self, np.uint32_t nfiles):
        cdef int i
        self.nfiles = nfiles
        self.ewah_keys = <ewah_bool_array **>malloc(nfiles*sizeof(ewah_bool_array*))
        self.ewah_refn = <ewah_bool_array **>malloc(nfiles*sizeof(ewah_bool_array*))
        self.ewah_coll = <ewah_map **>malloc(nfiles*sizeof(ewah_map*))
        for i in range(nfiles):
            self.ewah_keys[i] = new ewah_bool_array()
            self.ewah_refn[i] = new ewah_bool_array()
            self.ewah_coll[i] = new ewah_map()

    cdef void _reset(self):
        cdef np.int32_t ifile
        for ifile in range(self.nfiles):
            self.ewah_keys[ifile].reset()
            self.ewah_refn[ifile].reset()
            self.ewah_coll[ifile].clear()

    cdef bint _iseq(self, FileBitmasks solf):
        cdef np.int32_t ifile
        cdef ewah_bool_array* arr1
        cdef ewah_bool_array* arr2
        cdef ewahmap *map1
        cdef ewahmap *map2
        cdef ewahmap_p pair1, pair2
        cdef ewahmap_it it_map1, it_map2
        if self.nfiles != solf.nfiles:
            return 0
        for ifile in range(self.nfiles):
            # Keys
            arr1 = (<ewah_bool_array **> self.ewah_keys)[ifile]
            arr2 = (<ewah_bool_array **> solf.ewah_keys)[ifile]
            if arr1[0] != arr2[0]:
                return 0
            # Refn
            arr1 = (<ewah_bool_array **> self.ewah_refn)[ifile]
            arr2 = (<ewah_bool_array **> solf.ewah_refn)[ifile]
            if arr1[0] != arr2[0]:
                return 0
            # Map
            map1 = (<ewahmap **> self.ewah_coll)[ifile]
            map2 = (<ewahmap **> solf.ewah_coll)[ifile]
            for pair1 in map1[0]:
                it_map2 = map2[0].find(pair1.first)
                if it_map2 == map2[0].end():
                    return 0
                if pair1.second != dereference(it_map2).second:
                    return 0
            for pair2 in map2[0]:
                it_map1 = map1[0].find(pair2.first)
                if it_map1 == map1[0].end():
                    return 0
                if pair2.second != dereference(it_map1).second:
                    return 0
            # Match
            return 1

    def iseq(self, solf):
        return self._iseq(solf)

    cdef BoolArrayCollection _get_bitmask(self, np.uint32_t ifile):
        cdef BoolArrayCollection out = BoolArrayCollection()
        cdef ewah_bool_array **ewah_keys = <ewah_bool_array **>self.ewah_keys
        cdef ewah_bool_array **ewah_refn = <ewah_bool_array **>self.ewah_refn
        cdef ewah_map **ewah_coll = <ewah_map **>self.ewah_coll
        # This version actually copies arrays, which can be costly
        cdef ewah_bool_array *ewah_keys_out = <ewah_bool_array *>out.ewah_keys
        cdef ewah_bool_array *ewah_refn_out = <ewah_bool_array *>out.ewah_refn
        cdef ewah_map *ewah_coll_out = <ewah_map *>out.ewah_coll
        ewah_keys_out[0] = ewah_keys[ifile][0]
        ewah_refn_out[0] = ewah_refn[ifile][0]
        ewah_coll_out[0] = ewah_coll[ifile][0]
        # This version only copies pointers which can lead to deallocation of
        # the source when the copy is deleted.
        # out.ewah_keys = <void *>ewah_keys[ifile]
        # out.ewah_refn = <void *>ewah_refn[ifile]
        # out.ewah_coll = <void *>ewah_coll[ifile]
        return out

    cdef tuple _find_collisions(self, BoolArrayCollection coll, bint verbose = 0):
        cdef tuple cc, cr
        cc = self._find_collisions_coarse(coll, verbose)
        cr = self._find_collisions_refined(coll, verbose)
        return cc, cr

    cdef tuple _find_collisions_coarse(self, BoolArrayCollection coll, bint
                        verbose = 0, file_list = None):
        cdef np.int32_t ifile
        cdef ewah_bool_array arr_two, arr_swap, arr_keys, arr_refn
        cdef ewah_bool_array* iarr
        cdef ewah_bool_array* coll_keys
        cdef ewah_bool_array* coll_refn
        coll_keys = (<ewah_bool_array*> coll.ewah_keys)
        coll_refn = (<ewah_bool_array*> coll.ewah_refn)
        if file_list is None:
            file_list = range(self.nfiles)
        for ifile in file_list:
            iarr = (<ewah_bool_array **>self.ewah_keys)[ifile]
            arr_keys.logicaland(iarr[0], arr_two)
            arr_keys.logicalor(iarr[0], arr_swap)
            arr_keys.swap(arr_swap)
            arr_refn.logicalor(arr_two, arr_swap)
            arr_refn.swap(arr_swap)
        coll_keys[0].swap(arr_keys)
        coll_refn[0].swap(arr_refn)
        # Print
        cdef int nc, nm
        nc = coll_refn[0].numberOfOnes()
        nm = coll_keys[0].numberOfOnes()
        cdef tuple nout = (nc, nm)
        if verbose == 1:
            print("{: 10d}/{: 10d} collisions at coarse refinement.  ({: 10.5f}%)".format(nc,nm,100.0*float(nc)/nm))
        return nout

    cdef tuple _find_collisions_refined(self, BoolArrayCollection coll, bint verbose = 0):
        cdef np.int32_t ifile
        cdef ewah_bool_array iarr, arr_two, arr_swap
        cdef ewah_bool_array* coll_refn
        cdef cmap[np.uint64_t, ewah_bool_array] map_keys, map_refn
        cdef cmap[np.uint64_t, ewah_bool_array]* coll_coll
        cdef cmap[np.uint64_t, ewah_bool_array]* map_bitmask
        coll_refn = <ewah_bool_array*> coll.ewah_refn
        if coll_refn[0].numberOfOnes() == 0:
            if verbose == 1:
                print("{: 10d}/{: 10d} collisions at refined refinement. ({: 10.5f}%)".format(0,0,0))
            return (0,0)
        coll_coll = <cmap[np.uint64_t, ewah_bool_array]*> coll.ewah_coll
        for ifile in range(self.nfiles):
            map_bitmask = (<cmap[np.uint64_t, ewah_bool_array]**> self.ewah_coll)[ifile]
            for it_mi1 in map_bitmask[0]:
                mi1 = it_mi1.first
                iarr = it_mi1.second
                map_keys[mi1].logicaland(iarr, arr_two)
                map_keys[mi1].logicalor(iarr, arr_swap)
                map_keys[mi1].swap(arr_swap)
                map_refn[mi1].logicalor(arr_two, arr_swap)
                map_refn[mi1].swap(arr_swap)
        coll_coll[0] = map_refn
        # Count
        cdef int nc, nm
        nc = 0
        nm = 0
        for it_mi1 in map_refn:
            mi1 = it_mi1.first
            iarr = it_mi1.second
            nc += iarr.numberOfOnes()
            iarr = map_keys[mi1]
            nm += iarr.numberOfOnes()
        cdef tuple nout = (nc, nm)
        # Print
        if verbose == 1:
            if nm == 0:
                print("{: 10d}/{: 10d} collisions at refined refinement. ({: 10.5f}%)".format(nc,nm,0.0))
            else:
                print("{: 10d}/{: 10d} collisions at refined refinement. ({: 10.5f}%)".format(nc,nm,100.0*float(nc)/nm))
        return nout

    cdef void _set(self, np.uint32_t ifile, np.uint64_t i1, np.uint64_t i2 = FLAG):
        cdef ewah_bool_array *ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_map *ewah_coll = (<ewah_map **> self.ewah_coll)[ifile]
        ewah_keys[0].set(i1)
        if i2 != FLAG:
            ewah_refn[0].set(i1)
            ewah_coll[0][i1].set(i2)

    cdef void _set_coarse(self, np.uint32_t ifile, np.uint64_t i1):
        cdef ewah_bool_array *ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
        ewah_keys[0].set(i1)

    cdef void _set_refined(self, np.uint32_t ifile, np.uint64_t i1, np.uint64_t i2):
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_map *ewah_coll = (<ewah_map **> self.ewah_coll)[ifile]
        ewah_refn[0].set(i1)
        ewah_coll[0][i1].set(i2)

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _set_coarse_array(self, np.uint32_t ifile, np.uint8_t[:] arr):
        cdef ewah_bool_array *ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef np.uint64_t i1
        for i1 in range(arr.shape[0]):
            if arr[i1] == 1:
                ewah_keys[0].set(i1)

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _set_refined_array(self, np.uint32_t ifile, np.uint64_t i1, np.uint8_t[:] arr):
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_map *ewah_coll = (<ewah_map **> self.ewah_coll)[ifile]
        cdef np.uint64_t i2
        for i2 in range(arr.shape[0]):
            if arr[i2] == 1:
                ewah_refn[0].set(i1)
                ewah_coll[0][i1].set(i2)

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _set_refined_index_array(self, np.uint32_t ifile, np.int64_t nsub_mi,
                                       np.ndarray[np.uint64_t, ndim=1] sub_mi1,
                                       np.ndarray[np.uint64_t, ndim=1] sub_mi2):
        cdef np.ndarray[np.int64_t, ndim=1] ind = np.lexsort((sub_mi2[:nsub_mi],
                                                              sub_mi1[:nsub_mi]))
        cdef np.int64_t i, p
        cdef BoolArrayCollection temp
        if self._count_refined(ifile) == 0:
            # Add to file bitmask in order
            for i in range(nsub_mi):
                p = ind[i]
                self._set_refined(ifile, sub_mi1[p], sub_mi2[p])
        else:
            # Add to dummy bitmask in order, then combine
            temp = BoolArrayCollection()
            for i in range(nsub_mi):
                p = ind[i]
                temp._set_coarse(sub_mi1[p])
                temp._set_refined(sub_mi1[p], sub_mi2[p])
                self._append(ifile, temp)

    cdef void _set_map(self, np.uint32_t ifile, np.uint64_t i1, np.uint64_t i2):
        cdef ewah_map *ewah_coll = (<ewah_map **> self.ewah_coll)[ifile]
        ewah_coll[0][i1].set(i2)

    cdef void _set_refn(self, np.uint32_t ifile, np.uint64_t i1):
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        ewah_refn[0].set(i1)

    cdef bint _get(self, np.uint32_t ifile, np.uint64_t i1, np.uint64_t i2 = FLAG):
        cdef ewah_bool_array *ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_map *ewah_coll = (<ewah_map **> self.ewah_coll)[ifile]
        if (ewah_keys[0].get(i1) == 0): return 0
        if (i2 == FLAG) or (ewah_refn[0].get(i1) == 0):
            return 1
        return ewah_coll[0][i1].get(i2)

    cdef bint _get_coarse(self, np.uint32_t ifile, np.uint64_t i1):
        cdef ewah_bool_array *ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
        return ewah_keys[0].get(i1)

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _get_coarse_array(self, np.uint32_t ifile, np.uint64_t imax,
                                np.uint8_t[:] arr) except *:
        cdef ewah_bool_array *ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef ewah_bool_iterator *iter_set = new ewah_bool_iterator(ewah_keys[0].begin())
        cdef ewah_bool_iterator *iter_end = new ewah_bool_iterator(ewah_keys[0].end())
        cdef np.uint64_t iset
        while iter_set[0] != iter_end[0]:
            iset = dereference(iter_set[0])
            if iset >= imax:
                raise IndexError("Index {} exceedes max {}.".format(iset, imax))
            arr[iset] = 1
            preincrement(iter_set[0])

    cdef bint _isref(self, np.uint32_t ifile, np.uint64_t i):
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        return ewah_refn[0].get(i)

    def count_coarse(self, ifile):
        return self._count_coarse(ifile)

    def count_total(self, ifile):
        return self._count_total(ifile)

    def count_refined(self, ifile):
        return self._count_refined(ifile)

    cdef np.uint64_t _count_coarse(self, np.uint32_t ifile):
        return self._count_total(ifile) - self._count_refined(ifile)

    cdef np.uint64_t _count_total(self, np.uint32_t ifile):
        cdef ewah_bool_array *ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef np.uint64_t out = ewah_keys[0].numberOfOnes()
        return out

    cdef np.uint64_t _count_refined(self, np.uint32_t ifile):
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef np.uint64_t out = ewah_refn[0].numberOfOnes()
        return out

    def append(self, np.uint32_t ifile, BoolArrayCollection solf):
        if solf is None: return
        self._append(ifile, solf)

    cdef void _append(self, np.uint32_t ifile, BoolArrayCollection solf):
        cdef ewah_bool_array *ewah_keys1 = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef ewah_bool_array *ewah_refn1 = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_map *ewah_coll1 = (<ewah_map **> self.ewah_coll)[ifile]
        cdef ewah_bool_array *ewah_keys2 = <ewah_bool_array *> solf.ewah_keys
        cdef ewah_bool_array *ewah_refn2 = <ewah_bool_array *> solf.ewah_refn
        cdef ewahmap *ewah_coll2 = <ewahmap *> solf.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array swap, mi1_ewah1, mi1_ewah2
        cdef np.uint64_t mi1
        # Keys
        ewah_keys1[0].logicalor(ewah_keys2[0], swap)
        ewah_keys1[0].swap(swap)
        # Refined
        ewah_refn1[0].logicalor(ewah_refn2[0], swap)
        ewah_refn1[0].swap(swap)
        # Map
        it_map2 = ewah_coll2[0].begin()
        while it_map2 != ewah_coll2[0].end():
            mi1 = dereference(it_map2).first
            mi1_ewah2 = dereference(it_map2).second
            it_map1 = ewah_coll1[0].find(mi1)
            if it_map1 == ewah_coll1[0].end():
                ewah_coll1[0][mi1] = mi1_ewah2
            else:
                mi1_ewah1 = dereference(it_map1).second
                mi1_ewah1.logicalor(mi1_ewah2, swap)
                mi1_ewah1.swap(swap)
            preincrement(it_map2)

    cdef bint _intersects(self, np.uint32_t ifile, BoolArrayCollection solf):
        cdef ewah_bool_array *ewah_keys1 = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef ewah_bool_array *ewah_refn1 = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_map *ewah_coll1 = (<ewah_map **> self.ewah_coll)[ifile]
        cdef ewah_bool_array *ewah_keys2 = <ewah_bool_array *> solf.ewah_keys
        cdef ewah_bool_array *ewah_refn2 = <ewah_bool_array *> solf.ewah_refn
        cdef ewahmap *ewah_coll2 = <ewahmap *> solf.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array mi1_ewah1, mi1_ewah2
        cdef np.uint64_t mi1
        cdef ewah_bool_array ewah_coar1, ewah_coar2
        # No intersection
        if ewah_keys1[0].intersects(ewah_keys2[0]) == 0:
            return 0
        # Intersection at coarse level
        ewah_keys1[0].logicalxor(ewah_refn1[0],ewah_coar1)
        ewah_keys2[0].logicalxor(ewah_refn2[0],ewah_coar2)
        if ewah_coar1.intersects(ewah_keys2[0]) == 1:
            return 1
        if ewah_coar2.intersects(ewah_keys1[0]) == 1:
            return 1
        # Intersection at refined level
        if ewah_refn1[0].intersects(ewah_refn2[0]) == 1:
            it_map1 = ewah_coll1[0].begin()
            while (it_map1 != ewah_coll1[0].end()):
                mi1 = dereference(it_map1).first
                it_map2 = ewah_coll2[0].find(mi1)
                if it_map2 != ewah_coll2[0].end():
                    mi1_ewah1 = dereference(it_map1).second
                    mi1_ewah2 = dereference(it_map2).second
                    if mi1_ewah1.intersects(mi1_ewah2):
                        return 1
                preincrement(it_map1)
        return 0

    cdef void _logicalxor(self, np.uint32_t ifile, BoolArrayCollection solf, BoolArrayCollection out):
        cdef ewah_bool_array *ewah_keys1 = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef ewah_bool_array *ewah_refn1 = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_map *ewah_coll1 = (<ewah_map **> self.ewah_coll)[ifile]
        cdef ewah_bool_array *ewah_keys2 = <ewah_bool_array *> solf.ewah_keys
        cdef ewah_bool_array *ewah_refn2 = <ewah_bool_array *> solf.ewah_refn
        cdef ewahmap *ewah_coll2 = <ewahmap *> solf.ewah_coll
        cdef ewah_bool_array *ewah_keys_out = <ewah_bool_array *> out.ewah_keys
        cdef ewah_bool_array *ewah_refn_out = <ewah_bool_array *> out.ewah_refn
        cdef ewah_map *ewah_coll_out = <ewah_map *> out.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array mi1_ewah1, mi1_ewah2, swap
        cdef np.uint64_t mi1
        # Keys
        ewah_keys1[0].logicalxor(ewah_keys2[0],ewah_keys_out[0])
        # Refn
        ewah_refn1[0].logicalxor(ewah_refn2[0],ewah_refn_out[0])
        # Coll
        it_map1 = ewah_coll1[0].begin()
        while (it_map1 != ewah_coll1[0].end()):
            mi1 = dereference(it_map1).first
            mi1_ewah1 = dereference(it_map1).second
            it_map2 = ewah_coll2[0].find(mi1)
            if it_map2 == ewah_coll2[0].end():
                ewah_coll_out[0][mi1] = mi1_ewah1
            else:
                mi1_ewah2 = dereference(it_map2).second
                mi1_ewah1.logicalxor(mi1_ewah2, swap)
                ewah_coll_out[0][mi1] = swap
            preincrement(it_map1)
        it_map2 = ewah_coll2[0].begin()
        while (it_map2 != ewah_coll2[0].end()):
            mi1 = dereference(it_map2).first
            mi1_ewah2 = dereference(it_map2).second
            it_map1 = ewah_coll1[0].find(mi1)
            if it_map1 == ewah_coll1[0].end():
                ewah_coll_out[0][mi1] = mi1_ewah2
            preincrement(it_map2)

    def logicalxor(self, ifile, solf, out):
        return self._logicalxor(ifile, solf, out)

    cdef void _logicaland(self, np.uint32_t ifile, BoolArrayCollection solf, BoolArrayCollection out):
        cdef ewah_bool_array *ewah_keys1 = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef ewah_bool_array *ewah_refn1 = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_map *ewah_coll1 = (<ewah_map **> self.ewah_coll)[ifile]
        cdef ewah_bool_array *ewah_keys2 = <ewah_bool_array *> solf.ewah_keys
        cdef ewah_bool_array *ewah_refn2 = <ewah_bool_array *> solf.ewah_refn
        cdef ewahmap *ewah_coll2 = <ewahmap *> solf.ewah_coll
        cdef ewah_bool_array *ewah_keys_out = <ewah_bool_array *> out.ewah_keys
        cdef ewah_bool_array *ewah_refn_out = <ewah_bool_array *> out.ewah_refn
        cdef ewah_map *ewah_coll_out = <ewah_map *> out.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array mi1_ewah1, mi1_ewah2, swap
        cdef np.uint64_t mi1
        # Keys
        ewah_keys1[0].logicaland(ewah_keys2[0],ewah_keys_out[0])
        # Refn
        ewah_refn1[0].logicaland(ewah_refn2[0],ewah_refn_out[0])
        # Coll
        if ewah_refn_out[0].numberOfOnes() > 0:
            it_map1 = ewah_coll1[0].begin()
            while (it_map1 != ewah_coll1[0].end()):
                mi1 = dereference(it_map1).first
                it_map2 = ewah_coll2[0].find(mi1)
                if it_map2 != ewah_coll2[0].end():
                    mi1_ewah1 = dereference(it_map1).second
                    mi1_ewah2 = dereference(it_map2).second
                    mi1_ewah1.logicaland(mi1_ewah2, swap)
                    ewah_coll_out[0][mi1] = swap
                preincrement(it_map1)

    def logicaland(self, ifile, solf, out):
        return self._logicaland(ifile, solf, out)

    cdef void _select_contaminated(self, np.uint32_t ifile,
                                   BoolArrayCollection mask, np.uint8_t[:] out,
                                   np.uint8_t[:] secondary_files,
                                   BoolArrayCollection mask2 = None):
        # Fill mask at indices owned by this file that are also contaminated by
        # other files.
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_bool_array ewah_mask
        cdef ewah_bool_array *ewah_mask1
        cdef ewah_bool_array *ewah_mask2
        cdef ewah_bool_array ewah_slct
        cdef ewah_bool_array *ewah_file
        cdef np.uint64_t iset
        # Merge masks as necessary
        if mask2 is None:
            ewah_mask = (<ewah_bool_array *> mask.ewah_keys)[0]
        else:
            ewah_mask1 = <ewah_bool_array *> mask.ewah_keys
            ewah_mask2 = <ewah_bool_array *> mask2.ewah_keys
            ewah_mask1[0].logicalor(ewah_mask2[0],ewah_mask)
        # Get just refined cells owned by this file
        ewah_mask.logicaland(ewah_refn[0], ewah_slct)
        # Set array values
        cdef ewah_bool_iterator *iter_set = new ewah_bool_iterator(ewah_slct.begin())
        cdef ewah_bool_iterator *iter_end = new ewah_bool_iterator(ewah_slct.end())
        while iter_set[0] != iter_end[0]:
            iset = dereference(iter_set[0])
            out[iset] = 1
            preincrement(iter_set[0])
        # Find files that intersect this one
        cdef np.uint32_t isfile
        for isfile in range(self.nfiles):
            if isfile == ifile: continue
            ewah_file = (<ewah_bool_array **> self.ewah_keys)[isfile]
            if ewah_slct.intersects(ewah_file[0]) == 1:
                secondary_files[isfile] = 1

    cdef void _select_uncontaminated(self, np.uint32_t ifile,
                                     BoolArrayCollection mask, np.uint8_t[:] out,
                                     BoolArrayCollection mask2 = None):
        # Fill mask at indices that are owned by this file and no other.
        cdef ewah_bool_array *ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_bool_array ewah_mask
        cdef ewah_bool_array *ewah_mask1
        cdef ewah_bool_array *ewah_mask2
        cdef ewah_bool_array ewah_slct
        cdef ewah_bool_array ewah_coar
        cdef np.uint64_t iset
        # Merge masks if necessary
        if mask2 is None:
            ewah_mask = (<ewah_bool_array *> mask.ewah_keys)[0]
        else:
            ewah_mask1 = <ewah_bool_array *> mask.ewah_keys
            ewah_mask2 = <ewah_bool_array *> mask2.ewah_keys
            ewah_mask1[0].logicalor(ewah_mask2[0],ewah_mask)
        # Get coarse cells owned by this file
        ewah_keys[0].logicalxor(ewah_refn[0],ewah_coar)
        ewah_coar.logicaland(ewah_mask,ewah_slct)
        # Set array elements
        cdef ewah_bool_iterator *iter_set = new ewah_bool_iterator(ewah_slct.begin())
        cdef ewah_bool_iterator *iter_end = new ewah_bool_iterator(ewah_slct.end())
        while iter_set[0] != iter_end[0]:
            iset = dereference(iter_set[0])
            out[iset] = 1
            preincrement(iter_set[0])

    cdef bytes _dumps(self, np.uint32_t ifile):
        # TODO: write word size
        cdef sstream ss
        cdef ewah_bool_array *ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_map *ewah_coll = (<ewah_map **> self.ewah_coll)[ifile]
        cdef ewahmap_it it_map
        cdef np.uint64_t nrefn, mi1
        cdef ewah_bool_array mi1_ewah
        # Write mi1 ewah & refinement ewah
        ewah_keys[0].write(ss,1)
        ewah_refn[0].write(ss,1)
        # Number of refined bool arrays
        nrefn = <np.uint64_t>(ewah_refn[0].numberOfOnes())
        ss.write(<const char *> &nrefn, sizeof(nrefn))
        # Loop over refined bool arrays
        it_map = ewah_coll[0].begin()
        while it_map != ewah_coll[0].end():
            mi1 = dereference(it_map).first
            mi1_ewah = dereference(it_map).second
            ss.write(<const char *> &mi1, sizeof(mi1))
            mi1_ewah.write(ss,1)
            preincrement(it_map)
        # Return type cast python bytes string
        return <bytes>ss.str()

    cdef bint _loads(self, np.uint32_t ifile, bytes s):
        # TODO: write word size
        cdef sstream ss
        cdef ewah_bool_array *ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
        cdef ewah_bool_array *ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
        cdef ewah_map *ewah_coll = (<ewah_map **> self.ewah_coll)[ifile]
        cdef np.uint64_t nrefn, mi1
        nrefn = mi1 = 0
        # Write string to string stream
        if len(s) == 0: return 1
        ss.write(s, len(s))
        # Read keys and refinement arrays
        ewah_keys[0].read(ss,1)
        if ss.eof(): return 1
        ewah_refn[0].read(ss,1)
        # Read and check number of refined cells
        ss.read(<char *> (&nrefn), sizeof(nrefn))
        if nrefn != ewah_refn[0].numberOfOnes():
            raise Exception("Error in read. File indicates {} refinements, but bool array has {}.".format(nrefn,ewah_refn[0].numberOfOnes()))
        # Loop over refined cells
        for _ in range(nrefn):
            ss.read(<char *> (&mi1), sizeof(mi1))
            if ss.eof(): return 1
            ewah_coll[0][mi1].read(ss,1)
            # or...
            #mi1_ewah.read(ss,1)
            #ewah_coll[0][mi1].swap(mi1_ewah)
        return 1

    cdef bint _check(self):
        cdef np.uint32_t ifile
        cdef ewah_bool_array *ewah_keys
        cdef ewah_bool_array *ewah_refn
        cdef ewah_bool_array tmp1, tmp2
        cdef np.uint64_t nchk
        cdef str msg
        # Check individual files
        for ifile in range(self.nfiles):
            ewah_keys = (<ewah_bool_array **> self.ewah_keys)[ifile]
            ewah_refn = (<ewah_bool_array **> self.ewah_refn)[ifile]
            # Check that there are not any refn that are not keys
            ewah_keys[0].logicalxor(ewah_refn[0], tmp1)
            ewah_refn[0].logicaland(tmp1, tmp2)
            nchk = tmp2.numberOfOnes()
            if nchk > 0:
                msg = "File {}: There are {} refined cells that are not set on coarse level.".format(ifile,nchk)
                print(msg)
                return 0
                # raise Exception(msg)
        return 1

    def check(self):
        return self._check()

    def __dealloc__(self):
        for ifile in range(self.nfiles):
            del self.ewah_keys[ifile]
            del self.ewah_refn[ifile]
            del self.ewah_coll[ifile]

    def print_info(self, ifile, prefix=''):
        print("{}{: 8d} coarse, {: 8d} refined, {: 8d} total".format(
            prefix,
            self._count_coarse(ifile),
            self._count_refined(ifile),
            self._count_total(ifile)))

cdef class BoolArrayCollection:

    def __cinit__(self):
        self.ewah_keys = new ewah_bool_array()
        self.ewah_refn = new ewah_bool_array()
        self.ewah_coar = new ewah_bool_array()
        self.ewah_coll = new ewah_map()

    cdef void _reset(self):
        self.ewah_keys[0].reset()
        self.ewah_refn[0].reset()
        self.ewah_coar[0].reset()
        self.ewah_coll[0].clear()

    cdef int _richcmp(self, BoolArrayCollection solf, int op) except -1:

        cdef ewah_bool_array *arr1
        cdef ewah_bool_array *arr2
        cdef ewahmap *map1
        cdef ewahmap *map2
        cdef ewahmap_it it_map1, it_map2
        # ==
        if op == 2:
            # Keys
            arr1 = <ewah_bool_array *> self.ewah_keys
            arr2 = <ewah_bool_array *> solf.ewah_keys
            if arr1[0] != arr2[0]:
                return 0
            # Refn
            arr1 = <ewah_bool_array *> self.ewah_refn
            arr2 = <ewah_bool_array *> solf.ewah_refn
            if arr1[0] != arr2[0]:
                return 0
            # Map
            map1 = <ewahmap *> self.ewah_coll
            map2 = <ewahmap *> solf.ewah_coll
            it_map1 = map1[0].begin()
            while (it_map1 != map1[0].end()):
                it_map2 = map2[0].find(dereference(it_map1).first)
                if it_map2 == map2[0].end():
                    return 0
                if dereference(it_map1).second != dereference(it_map2).second:
                    return 0
                preincrement(it_map1)
            it_map2 =map2[0].begin()
            while (it_map2 != map2[0].end()):
                it_map1 = map1[0].find(dereference(it_map2).first)
                if it_map1 == map1[0].end():
                    return 0
                if dereference(it_map2).second != dereference(it_map1).second:
                    return 0
                preincrement(it_map2)
            # Match
            return 1
        # !=
        elif op == 3:
            if self._richcmp(solf, 2) == 1:
                return 0
            return 1
        else:
            return -1
            # options = ['<','<=','==','!=','>','>=']
            # raise NotImplementedError("Operator {} is not yet implemented.".format(options[op]))

    def __richcmp__(BoolArrayCollection self, BoolArrayCollection solf, int op):
        if self._richcmp(solf, op) == 1:
            return True
        else:
            return False

    cdef void _set(self, np.uint64_t i1, np.uint64_t i2 = FLAG):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        ewah_keys[0].set(i1)
        # Note the 0 here, for dereferencing
        if i2 != FLAG:
            ewah_refn[0].set(i1)
            ewah_coll[0][i1].set(i2)

    def set(self, i1, i2 = FLAG):
        self._set(i1, i2)

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    def set_from(self, np.uint64_t[:] ids):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef np.uint64_t i
        cdef np.uint64_t last = 0
        for i in range(ids.shape[0]):
            if ids[i] < last:
                raise RuntimeError
            self._set(ids[i])
            last = ids[i]
        print("Set from %s array and ended up with %s bytes" % (
            ids.size, ewah_keys[0].sizeInBytes()))

    cdef void _set_coarse(self, np.uint64_t i1):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        ewah_keys[0].set(i1)

    def set_coarse(self, i1):
        return self._set_coarse(i1)

    cdef void _set_refined(self, np.uint64_t i1, np.uint64_t i2):
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        # Note the 0 here, for dereferencing
        ewah_refn[0].set(i1)
        ewah_coll[0][i1].set(i2)

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _set_coarse_array(self, np.uint8_t[:] arr):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef np.uint64_t i1
        for i1 in range(arr.shape[0]):
            if arr[i1] == 1:
                ewah_keys[0].set(i1)
                # self._set_coarse(i1)

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _set_refined_array(self, np.uint64_t i1, np.uint8_t[:] arr):
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        cdef np.uint64_t i2
        for i2 in range(arr.shape[0]):
            if arr[i2] == 1:
                ewah_refn[0].set(i1)
                ewah_coll[0][i1].set(i2)
                # self._set_refined(i1, i2)

    def set_refined(self, i1, i2):
        return self._set_refined(i1, i2)

    cdef void _set_map(self, np.uint64_t i1, np.uint64_t i2):
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        ewah_coll[0][i1].set(i2)

    def set_map(self, i1, i2):
        self._set_map(i1, i2)

    cdef void _set_refn(self, np.uint64_t i1):
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        ewah_refn[0].set(i1)

    def set_refn(self, i1):
        self._set_refn(i1)

    cdef bint _get(self, np.uint64_t i1, np.uint64_t i2 = FLAG):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        # Note the 0 here, for dereferencing
        if (ewah_keys[0].get(i1) == 0): return 0
        if (ewah_refn[0].get(i1) == 0) or (i2 == FLAG):
            return 1
        return ewah_coll[0][i1].get(i2)

    def get(self, i1, i2 = FLAG):
        return self._get(i1, i2)

    cdef bint _get_coarse(self, np.uint64_t i1):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        return ewah_keys[0].get(i1)

    def get_coarse(self, i1):
        return self._get_coarse(i1)

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _get_coarse_array(self, np.uint64_t imax, np.uint8_t[:] arr) except *:
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_iterator *iter_set = new ewah_bool_iterator(ewah_keys[0].begin())
        cdef ewah_bool_iterator *iter_end = new ewah_bool_iterator(ewah_keys[0].end())
        cdef np.uint64_t iset
        while iter_set[0] != iter_end[0]:
            iset = dereference(iter_set[0])
            if iset >= imax:
                raise IndexError("Index {} exceedes max {}.".format(iset, imax))
            arr[iset] = 1
            preincrement(iter_set[0])

    def get_coarse_array(self, imax, arr):
        return self._get_coarse_array(imax, arr)

    cdef bint _contains(self, np.uint64_t i):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        return ewah_keys[0].get(i)

    def contains(self, np.uint64_t i):
        return self._contains(i)

    cdef bint _isref(self, np.uint64_t i):
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        return ewah_refn[0].get(i)

    def isref(self, np.uint64_t i):
        return self._isref(i)

    cdef void _ewah_coarse(self):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewah_bool_array *ewah_coar = <ewah_bool_array *> self.ewah_coar
        ewah_coar[0].reset()
        ewah_keys[0].logicalxor(ewah_refn[0],ewah_coar[0])
        return

    def ewah_coarse(self):
        return self._ewah_coarse()

    cdef np.uint64_t _count_total(self):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef np.uint64_t out = ewah_keys.numberOfOnes()
        return out

    def count_total(self):
        return self._count_total()

    cdef np.uint64_t _count_refined(self):
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef np.uint64_t out = ewah_refn.numberOfOnes()
        return out

    def count_refined(self):
        return self._count_refined()

    cdef np.uint64_t _count_coarse(self):
        self._ewah_coarse()
        cdef ewah_bool_array *ewah_coar = <ewah_bool_array *> self.ewah_coar
        cdef np.uint64_t out = ewah_coar.numberOfOnes()
        return out

    def count_coarse(self):
        return self._count_coarse()

    cdef void _logicalor(self, BoolArrayCollection solf, BoolArrayCollection out):
        cdef ewah_bool_array *ewah_keys1 = self.ewah_keys
        cdef ewah_bool_array *ewah_refn1 = self.ewah_refn
        cdef ewahmap *ewah_coll1 = self.ewah_coll
        cdef ewah_bool_array *ewah_keys2 = solf.ewah_keys
        cdef ewah_bool_array *ewah_refn2 = solf.ewah_refn
        cdef ewahmap *ewah_coll2 = solf.ewah_coll
        cdef ewah_bool_array *ewah_keys3 = out.ewah_keys
        cdef ewah_bool_array *ewah_refn3 = out.ewah_refn
        cdef ewahmap *ewah_coll3 = out.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array mi1_ewah1, mi1_ewah2
        cdef np.uint64_t mi1
        # Keys
        ewah_keys1[0].logicalor(ewah_keys2[0], ewah_keys3[0])
        # Refined
        ewah_refn1[0].logicalor(ewah_refn2[0], ewah_refn3[0])
        # Map
        it_map1 = ewah_coll1[0].begin()
        while it_map1 != ewah_coll1[0].end():
            mi1 = dereference(it_map1).first
            mi1_ewah1 = dereference(it_map1).second
            ewah_coll3[0][mi1] = mi1_ewah1
            preincrement(it_map1)
        it_map2 = ewah_coll2[0].begin()
        while it_map2 != ewah_coll2[0].end():
            mi1 = dereference(it_map2).first
            mi1_ewah2 = dereference(it_map2).second
            it_map1 = ewah_coll1[0].find(mi1)
            if it_map1 != ewah_coll1[0].end():
                mi1_ewah1 = dereference(it_map1).second
                mi1_ewah1.logicalor(mi1_ewah2, ewah_coll3[0][mi1])
            else:
                ewah_coll3[0][mi1] = mi1_ewah2
            preincrement(it_map2)

    cdef void _append(self, BoolArrayCollection solf):
        cdef ewah_bool_array *ewah_keys1 = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn1 = <ewah_bool_array *> self.ewah_refn
        cdef ewahmap *ewah_coll1 = <ewahmap *> self.ewah_coll
        cdef ewah_bool_array *ewah_keys2 = <ewah_bool_array *> solf.ewah_keys
        cdef ewah_bool_array *ewah_refn2 = <ewah_bool_array *> solf.ewah_refn
        cdef ewahmap *ewah_coll2 = <ewahmap *> solf.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array swap, mi1_ewah1, mi1_ewah2
        cdef np.uint64_t mi1
        # Keys
        ewah_keys1[0].logicalor(ewah_keys2[0], swap)
        ewah_keys1[0].swap(swap)
        # Refined
        ewah_refn1[0].logicalor(ewah_refn2[0], swap)
        ewah_refn1[0].swap(swap)
        # Map
        it_map2 = ewah_coll2[0].begin()
        while it_map2 != ewah_coll2[0].end():
            mi1 = dereference(it_map2).first
            mi1_ewah2 = dereference(it_map2).second
            it_map1 = ewah_coll1[0].find(mi1)
            if it_map1 == ewah_coll1[0].end():
                ewah_coll1[0][mi1] = mi1_ewah2
            else:
                mi1_ewah1 = dereference(it_map1).second
                mi1_ewah1.logicalor(mi1_ewah2, swap)
                mi1_ewah1.swap(swap)
            preincrement(it_map2)

    def append(self, solf):
        return self._append(solf)

    cdef bint _intersects(self, BoolArrayCollection solf):
        cdef ewah_bool_array *ewah_keys1 = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn1 = <ewah_bool_array *> self.ewah_refn
        cdef ewahmap *ewah_coll1 = <ewahmap *> self.ewah_coll
        cdef ewah_bool_array *ewah_keys2 = <ewah_bool_array *> solf.ewah_keys
        cdef ewah_bool_array *ewah_refn2 = <ewah_bool_array *> solf.ewah_refn
        cdef ewahmap *ewah_coll2 = <ewahmap *> solf.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array mi1_ewah1, mi1_ewah2
        cdef np.uint64_t mi1
        cdef ewah_bool_array ewah_coar1, ewah_coar2
        # No intersection
        if ewah_keys1[0].intersects(ewah_keys2[0]) == 0:
            return 0
        # Intersection at coarse level
        ewah_keys1[0].logicalxor(ewah_refn1[0],ewah_coar1)
        ewah_keys2[0].logicalxor(ewah_refn2[0],ewah_coar2)
        if ewah_coar1.intersects(ewah_keys2[0]) == 1:
            return 1
        if ewah_coar2.intersects(ewah_keys1[0]) == 1:
            return 1
        # Intersection at refined level
        if ewah_refn1[0].intersects(ewah_refn2[0]) == 1:
            it_map1 = ewah_coll1[0].begin()
            while (it_map1 != ewah_coll1[0].end()):
                mi1 = dereference(it_map1).first
                it_map2 = ewah_coll2[0].find(mi1)
                if it_map2 != ewah_coll2[0].end():
                    mi1_ewah1 = dereference(it_map1).second
                    mi1_ewah2 = dereference(it_map2).second
                    if mi1_ewah1.intersects(mi1_ewah2):
                        return 1
                preincrement(it_map1)
        return 0

    cdef void _logicalxor(self, BoolArrayCollection solf, BoolArrayCollection out):
        cdef ewah_bool_array *ewah_keys1 = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn1 = <ewah_bool_array *> self.ewah_refn
        cdef ewah_map *ewah_coll1 = <ewah_map *> self.ewah_coll
        cdef ewah_bool_array *ewah_keys2 = <ewah_bool_array *> solf.ewah_keys
        cdef ewah_bool_array *ewah_refn2 = <ewah_bool_array *> solf.ewah_refn
        cdef ewahmap *ewah_coll2 = <ewahmap *> solf.ewah_coll
        cdef ewah_bool_array *ewah_keys_out = <ewah_bool_array *> out.ewah_keys
        cdef ewah_bool_array *ewah_refn_out = <ewah_bool_array *> out.ewah_refn
        cdef ewah_map *ewah_coll_out = <ewah_map *> out.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array mi1_ewah1, mi1_ewah2, swap
        cdef np.uint64_t mi1
        # Keys
        ewah_keys1[0].logicalxor(ewah_keys2[0],ewah_keys_out[0])
        # Refn
        ewah_refn1[0].logicalxor(ewah_refn2[0],ewah_refn_out[0])
        # Coll
        it_map1 = ewah_coll1[0].begin()
        while (it_map1 != ewah_coll1[0].end()):
            mi1 = dereference(it_map1).first
            mi1_ewah1 = dereference(it_map1).second
            it_map2 = ewah_coll2[0].find(mi1)
            if it_map2 == ewah_coll2[0].end():
                ewah_coll_out[0][mi1] = mi1_ewah1
            else:
                mi1_ewah2 = dereference(it_map2).second
                mi1_ewah1.logicalxor(mi1_ewah2, swap)
                ewah_coll_out[0][mi1] = swap
            preincrement(it_map1)
        it_map2 = ewah_coll2[0].begin()
        while (it_map2 != ewah_coll2[0].end()):
            mi1 = dereference(it_map2).first
            mi1_ewah2 = dereference(it_map2).second
            it_map1 = ewah_coll1[0].find(mi1)
            if it_map1 == ewah_coll1[0].end():
                ewah_coll_out[0][mi1] = mi1_ewah2
            preincrement(it_map2)

    def logicalxor(self, solf, out):
        return self._logicalxor(solf, out)

    cdef void _logicaland(self, BoolArrayCollection solf, BoolArrayCollection out):
        cdef ewah_bool_array *ewah_keys1 = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn1 = <ewah_bool_array *> self.ewah_refn
        cdef ewah_map *ewah_coll1 = <ewah_map *> self.ewah_coll
        cdef ewah_bool_array *ewah_keys2 = <ewah_bool_array *> solf.ewah_keys
        cdef ewah_bool_array *ewah_refn2 = <ewah_bool_array *> solf.ewah_refn
        cdef ewahmap *ewah_coll2 = <ewahmap *> solf.ewah_coll
        cdef ewah_bool_array *ewah_keys_out = <ewah_bool_array *> out.ewah_keys
        cdef ewah_bool_array *ewah_refn_out = <ewah_bool_array *> out.ewah_refn
        cdef ewah_map *ewah_coll_out = <ewah_map *> out.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array mi1_ewah1, mi1_ewah2, swap
        cdef np.uint64_t mi1
        # Keys
        ewah_keys1[0].logicaland(ewah_keys2[0],ewah_keys_out[0])
        # Refn
        ewah_refn1[0].logicaland(ewah_refn2[0],ewah_refn_out[0])
        # Coll
        if ewah_refn_out[0].numberOfOnes() > 0:
            it_map1 = ewah_coll1[0].begin()
            while (it_map1 != ewah_coll1[0].end()):
                mi1 = dereference(it_map1).first
                mi1_ewah1 = dereference(it_map1).second
                it_map2 = ewah_coll2[0].find(mi1)
                if it_map2 != ewah_coll2[0].end():
                    mi1_ewah2 = dereference(it_map2).second
                    mi1_ewah1.logicaland(mi1_ewah2, swap)
                    ewah_coll_out[0][mi1] = swap
                preincrement(it_map1)

    def logicaland(self, solf, out):
        return self._logicaland(solf, out)

    cdef void _select_contaminated(self, BoolArrayCollection mask, np.uint8_t[:] out,
                                   BoolArrayCollection mask2 = None):
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewah_bool_array ewah_mask
        cdef ewah_bool_array *ewah_mask1
        cdef ewah_bool_array *ewah_mask2
        if mask2 is None:
            ewah_mask = (<ewah_bool_array *> mask.ewah_keys)[0]
        else:
            ewah_mask1 = <ewah_bool_array *> mask.ewah_keys
            ewah_mask2 = <ewah_bool_array *> mask2.ewah_keys
            ewah_mask1[0].logicalor(ewah_mask2[0],ewah_mask)
        cdef ewah_bool_array ewah_slct
        ewah_refn[0].logicaland(ewah_mask,ewah_slct)
        cdef np.uint64_t iset
        cdef ewah_bool_iterator *iter_set = new ewah_bool_iterator(ewah_slct.begin())
        cdef ewah_bool_iterator *iter_end = new ewah_bool_iterator(ewah_slct.end())
        while iter_set[0] != iter_end[0]:
            iset = dereference(iter_set[0])
            out[iset] = 1
            preincrement(iter_set[0])

    cdef void _select_uncontaminated(self, BoolArrayCollection mask, np.uint8_t[:] out,
                                     BoolArrayCollection mask2 = None):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewah_bool_array ewah_mask
        cdef ewah_bool_array *ewah_mask1
        cdef ewah_bool_array *ewah_mask2
        if mask2 is None:
            ewah_mask = (<ewah_bool_array *> mask.ewah_keys)[0]
        else:
            ewah_mask1 = <ewah_bool_array *> mask.ewah_keys
            ewah_mask2 = <ewah_bool_array *> mask2.ewah_keys
            ewah_mask1[0].logicalor(ewah_mask2[0],ewah_mask)
        cdef ewah_bool_array ewah_slct
        cdef ewah_bool_array ewah_coar
        ewah_keys[0].logicalxor(ewah_refn[0],ewah_coar)
        ewah_coar.logicaland(ewah_mask,ewah_slct)
        cdef np.uint64_t iset
        cdef ewah_bool_iterator *iter_set = new ewah_bool_iterator(ewah_slct.begin())
        cdef ewah_bool_iterator *iter_end = new ewah_bool_iterator(ewah_slct.end())
        while iter_set[0] != iter_end[0]:
            iset = dereference(iter_set[0])
            out[iset] = 1
            preincrement(iter_set[0])

    cdef void _get_ghost_zones(self, int ngz, int order1, int order2,
                               bint periodicity[3], BoolArrayCollection out_ewah,
                               bint coarse_ghosts = 0):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewahmap *ewah_coll = <ewahmap *> self.ewah_coll
        cdef ewah_bool_iterator *iter_set1 = new ewah_bool_iterator(ewah_keys.begin())
        cdef ewah_bool_iterator *iter_end1 = new ewah_bool_iterator(ewah_keys.end())
        cdef ewah_bool_iterator *iter_set2
        cdef ewah_bool_iterator *iter_end2
        cdef np.uint64_t max_index1 = <np.uint64_t>(1 << order1)
        cdef np.uint64_t max_index2 = <np.uint64_t>(1 << order2)
        cdef np.uint64_t nele1 = <np.uint64_t>(max_index1**3)
        cdef np.uint64_t nele2 = <np.uint64_t>(max_index2**3)
        cdef BoolArrayCollectionUncompressed temp_bool = BoolArrayCollectionUncompressed(nele1, nele2)
        cdef BoolArrayCollectionUncompressed out_bool = BoolArrayCollectionUncompressed(nele1, nele2)
        cdef np.uint64_t mi1, mi2, mi1_n, mi2_n
        cdef np.uint32_t ntot, i
        cdef void* pointers[7]
        pointers[0] = malloc( sizeof(np.int32_t) * (2*ngz+1)*3)
        pointers[1] = malloc( sizeof(np.uint64_t) * (2*ngz+1)*3)
        pointers[2] = malloc( sizeof(np.uint64_t) * (2*ngz+1)*3)
        pointers[3] = malloc( sizeof(np.uint64_t) * (2*ngz+1)**3)
        pointers[4] = malloc( sizeof(np.uint64_t) * (2*ngz+1)**3)
        pointers[5] = malloc( sizeof(np.uint8_t) * nele1)
        pointers[6] = malloc( sizeof(np.uint8_t) * nele2)
        cdef np.uint32_t[:,:] index = <np.uint32_t[:2*ngz+1,:3]> pointers[0]
        cdef np.uint64_t[:,:] ind1_n = <np.uint64_t[:2*ngz+1,:3]> pointers[1]
        cdef np.uint64_t[:,:] ind2_n = <np.uint64_t[:2*ngz+1,:3]> pointers[2]
        cdef np.uint64_t[:] neighbor_list1 = <np.uint64_t[:((2*ngz+1)**3)]> pointers[3]
        cdef np.uint64_t[:] neighbor_list2 = <np.uint64_t[:((2*ngz+1)**3)]> pointers[4]
        cdef np.uint8_t *bool_keys = <np.uint8_t *> pointers[5]
        cdef np.uint8_t *bool_coll = <np.uint8_t *> pointers[6]
        cdef SparseUnorderedRefinedBitmaskSet list_coll = SparseUnorderedRefinedBitmaskSet()
        for i in range(nele1):
            bool_keys[i] = 0
        while iter_set1[0] != iter_end1[0]:
            mi1 = dereference(iter_set1[0])
            if (coarse_ghosts == 1) or (ewah_refn[0].get(mi1) == 0):
                # Coarse neighbors
                ntot = morton_neighbors_coarse(mi1, max_index1, periodicity, ngz,
                                               index, ind1_n, neighbor_list1)
                for i in range(ntot):
                    mi1_n = neighbor_list1[i]
                    if ewah_keys[0].get(mi1_n) == 0:
                        bool_keys[mi1_n] = 1
            else:
                for i in range(nele2):
                    bool_coll[i] = 0
                # Refined neighbors
                iter_set2 = new ewah_bool_iterator(ewah_coll[0][mi1].begin())
                iter_end2 = new ewah_bool_iterator(ewah_coll[0][mi1].end())
                while iter_set2[0] != iter_end2[0]:
                    mi2 = dereference(iter_set2[0])
                    ntot = morton_neighbors_refined(mi1, mi2,
                                                    max_index1, max_index2,
                                                    periodicity, ngz, index,
                                                    ind1_n, ind2_n,
                                                    neighbor_list1,
                                                    neighbor_list2)
                    for i in range(ntot):
                        mi1_n = neighbor_list1[i]
                        mi2_n = neighbor_list2[i]
                        if mi1_n == mi1:
                            if ewah_coll[0][mi1].get(mi2_n) == 0:
                                bool_keys[mi1_n] = 1
                                bool_coll[mi2_n] = 1
                        else:
                            if ewah_refn[0].get(mi1_n) == 1:
                                if ewah_coll[0][mi1_n].get(mi2_n) == 0:
                                    bool_keys[mi1_n] = 1
                                    list_coll._set(mi1_n, mi2_n)
                            else:
                                if ewah_keys[0].get(mi1_n) == 0:
                                    bool_keys[mi1_n] = 1
                    preincrement(iter_set2[0])
                # Add to running list
                temp_bool._set_refined_array_ptr(mi1, bool_coll)
            preincrement(iter_set1[0])
        # Set keys
        out_bool._set_coarse_array_ptr(bool_keys)
        list_coll._fill_bool(out_bool)
        out_bool._append(temp_bool)
        out_bool._compress(out_ewah)
        # Free things
        for i in range(7):
            free(pointers[i])

    cdef bytes _dumps(self):
        # TODO: write word size
        cdef sstream ss
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewahmap *ewah_coll = <ewahmap *> self.ewah_coll
        cdef ewahmap_it it_map
        cdef np.uint64_t nrefn, mi1
        cdef ewah_bool_array mi1_ewah
        # Write mi1 ewah & refinement ewah
        ewah_keys[0].write(ss,1)
        ewah_refn[0].write(ss,1)
        # Number of refined bool arrays
        nrefn = <np.uint64_t>(ewah_refn[0].numberOfOnes())
        ss.write(<const char *> &nrefn, sizeof(nrefn))
        # Loop over refined bool arrays
        it_map = ewah_coll[0].begin()
        while it_map != ewah_coll[0].end():
            mi1 = dereference(it_map).first
            mi1_ewah = dereference(it_map).second
            ss.write(<const char *> &mi1, sizeof(mi1))
            mi1_ewah.write(ss,1)
            preincrement(it_map)
        # Return type cast python bytes string
        return <bytes>ss.str()

    def dumps(self):
        return self._dumps()

    cdef bint _loads(self, bytes s):
        # TODO: write word size
        cdef sstream ss
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewahmap *ewah_coll = <ewahmap *> self.ewah_coll
        cdef np.uint64_t nrefn, mi1
        nrefn = mi1 = 0
        # Write string to string stream
        if len(s) == 0: return 1
        ss.write(s, len(s))
        # Read keys and refinement arrays
        if ss.eof(): return 1
        ewah_keys[0].read(ss,1)
        if ss.eof(): return 1
        ewah_refn[0].read(ss,1)
        # Read and check number of refined cells
        if ss.eof(): return 1
        ss.read(<char *> (&nrefn), sizeof(nrefn))
        if nrefn != ewah_refn[0].numberOfOnes():
            raise Exception("Error in read. File indicates {} refinements, but bool array has {}.".format(nrefn,ewah_refn[0].numberOfOnes()))
        # Loop over refined cells
        for _ in range(nrefn):
            ss.read(<char *> (&mi1), sizeof(mi1))
            if ss.eof():
                # A brief note about why we do this!
                # In previous versions of the EWAH code, which were more
                # susceptible to issues with differences in sizes of size_t
                # etc, the ewah_coll.read would use instance variables as
                # destinations; these were initialized to zero.  In recent
                # versions, it uses (uninitialized) temporary variables.  We
                # were passing in streams that were already at EOF - so the
                # uninitialized memory would not be written to, and it would
                # retain the previous values, which would invariably be really
                # really big!  So we do a check for EOF here to make sure we're
                # not up to no good.
                break
            ewah_coll[0][mi1].read(ss,1)
            # or...
            #mi1_ewah.read(ss,1)
            #ewah_coll[0][mi1].swap(mi1_ewah)
        return 1

    def loads(self, s):
        return self._loads(s)

    def save(self, fname):
        cdef bytes serial_BAC
        f = open(fname,'wb')
        serial_BAC = self._dumps()
        f.write(struct.pack('Q',len(serial_BAC)))
        f.write(serial_BAC)
        f.close()

    def load(self, fname):
        cdef np.uint64_t size_serial
        cdef bint flag_read
        f = open(fname,'rb')
        size_serial, = struct.unpack('Q',f.read(struct.calcsize('Q')))
        flag_read = self._loads(f.read(size_serial))
        f.close()
        return flag_read

    cdef bint _check(self):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewah_bool_array tmp1, tmp2
        cdef np.uint64_t nchk
        cdef str msg
        # Check that there are not any refn that are not keys
        ewah_keys[0].logicalxor(ewah_refn[0], tmp1)
        ewah_refn[0].logicaland(tmp1, tmp2)
        nchk = tmp2.numberOfOnes()
        if nchk > 0:
            msg = "There are {} refined cells that are not set on coarse level.".format(nchk)
            print(msg)
            return 0
            # raise Exception(msg)
        return 1

    def __dealloc__(self):
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> self.ewah_keys
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> self.ewah_refn
        cdef ewah_bool_array *ewah_coar = <ewah_bool_array *> self.ewah_coar
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        del ewah_keys
        del ewah_refn
        del ewah_coar
        del ewah_coll

    def print_info(self, prefix=''):
        print("{}{: 8d} coarse, {: 8d} refined, {: 8d} total".format(prefix,
                                                                     self._count_coarse(),
                                                                     self._count_refined(),
                                                                     self._count_total()))

cdef class BoolArrayCollectionUncompressed:

    def __cinit__(self, np.uint64_t nele1, np.uint64_t nele2):
        self.nele1 = <int>nele1
        self.nele2 = <int>nele2
        self.ewah_coll = new ewah_map()
        cdef np.uint64_t i
        self.ewah_keys = <bitarrtype *>malloc(sizeof(bitarrtype)*nele1)
        self.ewah_refn = <bitarrtype *>malloc(sizeof(bitarrtype)*nele1)
        for i in range(nele1):
            self.ewah_keys[i] = 0
            self.ewah_refn[i] = 0

    def reset(self):
        self.__dealloc__()
        self.__init__(self.nele1,self.nele2)

    cdef void _compress(self, BoolArrayCollection solf):
        cdef np.uint64_t i
        cdef ewah_bool_array *ewah_keys = <ewah_bool_array *> solf.ewah_keys
        cdef ewah_bool_array *ewah_refn = <ewah_bool_array *> solf.ewah_refn
        cdef bitarrtype *bool_keys = <bitarrtype *> self.ewah_keys
        cdef bitarrtype *bool_refn = <bitarrtype *> self.ewah_refn
        for i in range(self.nele1):
            if bool_keys[i] == 1:
                ewah_keys[0].set(i)
            if bool_refn[i] == 1:
                ewah_refn[0].set(i)
        cdef ewah_map *ewah_coll1 = <ewah_map *> self.ewah_coll
        cdef ewah_map *ewah_coll2 = <ewah_map *> solf.ewah_coll
        ewah_coll2[0] = ewah_coll1[0]

    cdef void _set(self, np.uint64_t i1, np.uint64_t i2 = FLAG):
        cdef bitarrtype *ewah_keys = <bitarrtype *> self.ewah_keys
        cdef bitarrtype *ewah_refn = <bitarrtype *> self.ewah_refn
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        ewah_keys[i1] = 1
        # Note the 0 here, for dereferencing
        if i2 != FLAG:
            ewah_refn[i1] = 1
            ewah_coll[0][i1].set(i2)

    cdef void _set_coarse(self, np.uint64_t i1):
        cdef bitarrtype *ewah_keys = <bitarrtype *> self.ewah_keys
        ewah_keys[i1] = 1

    cdef void _set_refined(self, np.uint64_t i1, np.uint64_t i2):
        cdef bitarrtype *ewah_refn = <bitarrtype *> self.ewah_refn
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        # Note the 0 here, for dereferencing
        ewah_refn[i1] = 1
        ewah_coll[0][i1].set(i2)

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _set_coarse_array(self, np.uint8_t[:] arr):
        cdef bitarrtype *ewah_keys = <bitarrtype *> self.ewah_keys
        cdef np.uint64_t i1
        for i1 in range(arr.shape[0]):
            if arr[i1] == 1:
                ewah_keys[i1] = 1

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _set_coarse_array_ptr(self, np.uint8_t *arr):
        # TODO: memcpy?
        cdef bitarrtype *ewah_keys = <bitarrtype *> self.ewah_keys
        cdef np.uint64_t i1
        for i1 in range(self.nele1):
            if arr[i1] == 1:
                ewah_keys[i1] = 1

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _set_refined_array(self, np.uint64_t i1, np.uint8_t[:] arr):
        cdef bitarrtype *ewah_refn = <bitarrtype *> self.ewah_refn
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        cdef np.uint64_t i2
        for i2 in range(arr.shape[0]):
            if arr[i2] == 1:
                ewah_refn[i1] = 1
                ewah_coll[0][i1].set(i2)

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.cdivision(True)
    @cython.initializedcheck(False)
    cdef void _set_refined_array_ptr(self, np.uint64_t i1, np.uint8_t *arr):
        cdef bitarrtype *ewah_refn = <bitarrtype *> self.ewah_refn
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        cdef np.uint64_t i2
        cdef ewah_bool_array *barr = &ewah_coll[0][i1]
        for i2 in range(self.nele2):
            if arr[i2] == 1:
                ewah_refn[i1] = 1
                barr.set(i2)

    cdef void _set_map(self, np.uint64_t i1, np.uint64_t i2):
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        ewah_coll[0][i1].set(i2)

    cdef void _set_refn(self, np.uint64_t i1):
        cdef bitarrtype *ewah_refn = <bitarrtype *> self.ewah_refn
        ewah_refn[i1] = 1

    cdef bint _get(self, np.uint64_t i1, np.uint64_t i2 = FLAG):
        cdef bitarrtype *ewah_keys = <bitarrtype *> self.ewah_keys
        cdef bitarrtype *ewah_refn = <bitarrtype *> self.ewah_refn
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        # Note the 0 here, for dereferencing
        if ewah_keys[i1] == 0: return 0
        if (ewah_refn[i1] == 0) or (i2 == FLAG):
            return 1
        return ewah_coll[0][i1].get(i2)

    cdef bint _get_coarse(self, np.uint64_t i1):
        cdef bitarrtype *ewah_keys = <bitarrtype *> self.ewah_keys
        return <bint>ewah_keys[i1]
        # if (ewah_keys[i1] == 0): return 0
        # return 1

    cdef bint _isref(self, np.uint64_t i):
        cdef bitarrtype *ewah_refn = <bitarrtype *> self.ewah_refn
        return <bint>ewah_refn[i]

    cdef np.uint64_t _count_total(self):
        cdef bitarrtype *ewah_keys = <bitarrtype *> self.ewah_keys
        cdef np.uint64_t i
        cdef np.uint64_t out = 0
        for i in range(self.nele1):
            out += ewah_keys[i]
        return out

    cdef np.uint64_t _count_refined(self):
        cdef bitarrtype *ewah_refn = <bitarrtype *> self.ewah_refn
        cdef np.uint64_t i
        cdef np.uint64_t out = 0
        for i in range(self.nele1):
            out += ewah_refn[i]
        return out

    cdef void _append(self, BoolArrayCollectionUncompressed solf):
        cdef bitarrtype *ewah_keys1 = <bitarrtype *> self.ewah_keys
        cdef bitarrtype *ewah_refn1 = <bitarrtype *> self.ewah_refn
        cdef bitarrtype *ewah_keys2 = <bitarrtype *> solf.ewah_keys
        cdef bitarrtype *ewah_refn2 = <bitarrtype *> solf.ewah_refn
        cdef ewahmap *ewah_coll1 = <ewahmap *> self.ewah_coll
        cdef ewahmap *ewah_coll2 = <ewahmap *> solf.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array swap, mi1_ewah1, mi1_ewah2
        cdef np.uint64_t mi1
        # TODO: Check if nele1 is equal?
        # Keys
        for mi1 in range(solf.nele1):
            if ewah_keys2[mi1] == 1:
                ewah_keys1[mi1] = 1
        # Refined
        for mi1 in range(solf.nele1):
            if ewah_refn2[mi1] == 1:
                ewah_refn1[mi1] = 1
        # Map
        it_map2 = ewah_coll2[0].begin()
        while it_map2 != ewah_coll2[0].end():
            mi1 = dereference(it_map2).first
            mi1_ewah2 = dereference(it_map2).second
            it_map1 = ewah_coll1[0].find(mi1)
            if it_map1 == ewah_coll1[0].end():
                ewah_coll1[0][mi1] = mi1_ewah2
            else:
                mi1_ewah1 = dereference(it_map1).second
                mi1_ewah1.logicalor(mi1_ewah2, swap)
                mi1_ewah1.swap(swap)
            preincrement(it_map2)

    cdef bint _intersects(self, BoolArrayCollectionUncompressed solf):
        cdef bitarrtype *ewah_keys1 = <bitarrtype *> self.ewah_keys
        cdef bitarrtype *ewah_refn1 = <bitarrtype *> self.ewah_refn
        cdef bitarrtype *ewah_keys2 = <bitarrtype *> solf.ewah_keys
        cdef bitarrtype *ewah_refn2 = <bitarrtype *> solf.ewah_refn
        cdef ewahmap *ewah_coll1 = <ewahmap *> self.ewah_coll
        cdef ewahmap *ewah_coll2 = <ewahmap *> solf.ewah_coll
        cdef ewahmap_it it_map1, it_map2
        cdef ewah_bool_array mi1_ewah1, mi1_ewah2
        cdef np.uint64_t mi1
        # No intersection
        for mi1 in range(self.nele1):
            if (ewah_keys1[mi1] == 1) and (ewah_keys2[mi1] == 1):
                break
        if (mi1 < self.nele1):
            return 0
        mi1 = self.nele1 # This is to get rid of a warning
        # Intersection at refined level
        for mi1 in range(self.nele1):
            if (ewah_refn1[mi1] == 1) and (ewah_refn2[mi1] == 1):
                it_map1 = ewah_coll1[0].begin()
                while (it_map1 != ewah_coll1[0].end()):
                    mi1 = dereference(it_map1).first
                    it_map2 = ewah_coll2[0].find(mi1)
                    if it_map2 != ewah_coll2[0].end():
                        mi1_ewah1 = dereference(it_map1).second
                        mi1_ewah2 = dereference(it_map2).second
                        if mi1_ewah1.intersects(mi1_ewah2):
                            return 1
                    preincrement(it_map1)
                break
        # Intersection at coarse level or refined inside coarse
        if mi1 == self.nele1:
            return 1
        return 0

    def __dealloc__(self):
        cdef bitarrtype *ewah_keys = <bitarrtype *> self.ewah_keys
        cdef bitarrtype *ewah_refn = <bitarrtype *> self.ewah_refn
        free(ewah_keys)
        free(ewah_refn)
        cdef ewah_map *ewah_coll = <ewah_map *> self.ewah_coll
        del ewah_coll

    def print_info(self, prefix=''):
        cdef np.uint64_t nrefn = self._count_refined()
        cdef np.uint64_t nkeys = self._count_total()
        print("{}{: 8d} coarse, {: 8d} refined, {: 8d} total".format(prefix,
                                                                     nkeys - nrefn,
                                                                     nrefn,
                                                                     nkeys))



# Vector version
cdef class SparseUnorderedBitmaskVector:
    def __cinit__(self):
        self.total = 0

    cdef void _set(self, np.uint64_t ind):
        self.entries.push_back(ind)
        self.total += 1

    def set(self, ind):
        self._set(ind)

    cdef void _fill(self, np.uint8_t[:] mask):
        cdef np.uint64_t i, ind
        for i in range(self.entries.size()):
            ind = self.entries[i]
            mask[ind] = 1

    cdef void _fill_ewah(self, BoolArrayCollection mm):
        self._remove_duplicates()
        cdef np.uint64_t i, ind
        for i in range(self.entries.size()):
            ind = self.entries[i]
            mm._set_coarse(ind)

    cdef void _fill_bool(self, BoolArrayCollectionUncompressed mm):
        self._remove_duplicates()
        cdef np.uint64_t i, ind
        for i in range(self.entries.size()):
            ind = self.entries[i]
            mm._set_coarse(ind)

    cdef void _reset(self):
        self.entries.erase(self.entries.begin(), self.entries.end())
        self.total = 0

    cdef to_array(self):
        self._remove_duplicates()
        cdef np.ndarray[np.uint64_t, ndim=1] rv
        rv = np.empty(self.entries.size(), dtype='uint64')
        for i in range(self.entries.size()):
            rv[i] = self.entries[i]
        return rv

    cdef void _remove_duplicates(self):
        cdef vector[np.uint64_t].iterator last
        sort(self.entries.begin(), self.entries.end())
        last = unique(self.entries.begin(), self.entries.end())
        self.entries.erase(last, self.entries.end())

    cdef void _prune(self):
        if self.total > MAX_VECTOR_SIZE:
            self._remove_duplicates()
            self.total = 0

    def __dealloc__(self):
        self.entries.clear()

# Set version
cdef class SparseUnorderedBitmaskSet:
    cdef void _set(self, np.uint64_t ind):
        self.entries.insert(ind)

    def set(self, ind):
        self._set(ind)

    cdef void _fill(self, np.uint8_t[:] mask):
        for it in self.entries:
            mask[it] = 1

    cdef void _fill_ewah(self, BoolArrayCollection mm):
        for it in self.entries:
            mm._set_coarse(it)

    cdef void _fill_bool(self, BoolArrayCollectionUncompressed mm):
        for it in self.entries:
            mm._set_coarse(it)

    cdef void _reset(self):
        self.entries.clear()

    cdef to_array(self):
        cdef np.uint64_t ind
        cdef np.ndarray[np.uint64_t, ndim=1] rv
        cdef cset[np.uint64_t].iterator it
        rv = np.empty(self.entries.size(), dtype='uint64')
        it = self.entries.begin()
        i = 0
        while it != self.entries.end():
            ind = dereference(it)
            rv[i] = ind
            preincrement(it)
            i += 1
        return rv

    def __dealloc__(self):
        self.entries.clear()

# vector version
cdef class SparseUnorderedRefinedBitmaskVector:
    def __cinit__(self):
        self.total = 0

    cdef void _set(self, np.uint64_t ind1, np.uint64_t ind2):
        cdef ind_pair ind
        ind.first = ind1
        ind.second = ind2
        self.entries.push_back(ind)
        self.total += 1

    def set(self, ind1, ind2):
        self._set(ind1, ind2)

    cdef void _fill(self, np.uint8_t[:] mask1, np.uint8_t[:] mask2):
        for it in self.entries:
            mask1[it.first] = mask2[it.second] = 1

    cdef void _fill_ewah(self, BoolArrayCollection mm):
        self._remove_duplicates()
        for it in self.entries:
            mm._set_refined(it.first, it.second)

    cdef void _fill_bool(self, BoolArrayCollectionUncompressed mm):
        self._remove_duplicates()
        for it in self.entries:
            mm._set_refined(it.first, it.second)

    cdef void _reset(self):
        self.entries.erase(self.entries.begin(), self.entries.end())
        self.total = 0

    cdef to_array(self):
        cdef np.uint64_t i
        cdef np.ndarray[np.uint64_t, ndim=2] rv
        self._remove_duplicates()
        rv = np.empty((self.entries.size(),2),dtype='uint64')
        i = 0
        for it in self.entries:
            rv[i,0] = it.first
            rv[i,1] = it.second
            i += 1
        return rv

    cdef void _remove_duplicates(self):
        cdef vector[ind_pair].iterator last
        sort(self.entries.begin(), self.entries.end())
        last = unique(self.entries.begin(), self.entries.end())
        self.entries.erase(last, self.entries.end())
        # http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array
        # cdef np.ndarray[np.uint64_t, ndim=2] rv
        # cdef np.ndarray[np.uint64_t, ndim=2] rv_uni
        # cdef np.uint64_t m
        # cdef vector[np.uint64_t].iterator last1
        # cdef vector[np.uint64_t].iterator last2
        # # cdef np.ndarray[np.uint64_t, ndim=1] _
        # cdef vector[np.uint64_t] *entries1 = <vector[np.uint64_t]*> self.entries1
        # cdef vector[np.uint64_t] *entries2 = <vector[np.uint64_t]*> self.entries2
        # rv = np.empty((entries1[0].size(),2),dtype='uint64')
        # for i in range(entries1[0].size()):
        #     rv[i,0] = entries1[0][i]
        #     rv[i,1] = entries2[0][i]
        # rv_uni = np.unique(np.ascontiguousarray(rv).view(np.dtype((np.void, rv.dtype.itemsize * rv.shape[1])))).view(rv.dtype).reshape(-1,rv.shape[1])
        # last1 = entries1[0].begin() + rv_uni.shape[0]
        # last2 = entries2[0].begin() + rv_uni.shape[0]
        # for m in range(rv_uni.shape[0]):
        #     entries1[0][m] = rv_uni[m,0]
        #     entries2[0][m] = rv_uni[m,1]
        # entries1[0].erase(last1, entries1[0].end())
        # entries2[0].erase(last2, entries2[0].end())

    cdef void _prune(self):
        if self.total > MAX_VECTOR_SIZE:
            self._remove_duplicates()
            self.total = 0

    def __dealloc__(self):
        self.entries.clear()

# Set version
cdef class SparseUnorderedRefinedBitmaskSet:
    cdef void _set(self, np.uint64_t ind1, np.uint64_t ind2):
        cdef ind_pair ind
        ind.first = ind1
        ind.second = ind2
        self.entries.insert(ind)

    def set(self, ind1, ind2):
        self._set(ind1, ind2)

    cdef void _fill(self, np.uint8_t[:] mask1, np.uint8_t[:] mask2):
        for p in self.entries:
            mask1[p.first] = mask2[p.second] = 1

    cdef void _fill_ewah(self, BoolArrayCollection mm):
        for it in self.entries:
            mm._set_refined(it.first, it.second)

    cdef void _fill_bool(self, BoolArrayCollectionUncompressed mm):
        for it in self.entries:
            mm._set_refined(it.first, it.second)

    cdef void _reset(self):
        self.entries.clear()

    cdef to_array(self):
        cdef np.uint64_t i
        cdef np.ndarray[np.uint64_t, ndim=2] rv
        rv = np.empty((self.entries.size(),2),dtype='uint64')
        i = 0
        for it in self.entries:
            rv[i,0] = it.first
            rv[i,1] = it.second
            i += 1
        return rv

    def __dealloc__(self):
        self.entries.clear()