(This is not a CS class homework, even if it looks like one)
I use bit fields to represent ranges from 0 to 22. As an input, I have several different ranges, for example (order doesn't matter). I have used . for 0 and X for 1 for better readability.
.....XXXXX.............. ..XXXX.................. .....XXXXXXXXXXXXXXX.... ........XXXXXXX......... XXXXXXXXXXXXXXXXXXXXXXXX
The number of bitfield ranges is usually less than 10, but can reach 100. From this input I want to calculate mutually exclusive adjacent ranges, for example:
XX...................... ..XXX................... .....X.................. ......XX................ ........XX.............. ..........XXXXX......... ...............XXXXX.... ....................XXXX
(again, the order of the output does not matter, they just have to be mutually exclusive and adjacent, i.e. they cannot have holes in them. .....XXX.......XXXXX.... must be divided into two separate ranges).
I tried several algorithms, but all of them turned out to be quite complex and uncontrollable. What really helped me was a way to find that .....XXX.......XXXXX.... has a hole and a way to determine the index of one of the bits in the hole.
Edit: The range of bit fields is the scale levels on the map. They are intended to be used to output XML stylesheets for Mapnik (a tile rendering system that, among other things, is used by OpenStreetMap).
source share