mattkrick / fast-bitset

A fast bitset with some nice methods

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Left shift/right shift

jamesplease opened this issue · comments

Any plans to add these?

could you give me a quick user story? I can't think of how this would be useful

Sure thing! I'm writing a library that works with schedules of people. I'm mapping a person's schedule to a binary representation to see if I can write more efficient algorithms around questions I'm trying to answer about schedules.

You can think of the schema of a schedule looking like:

{
  firstDay: '2015-01-02',
  binaryRepresentation: 0b00010,
  length: 5
}

This means that the first day of this "schedule" is January 2nd, 2015. It lasts for five days, and on the fourth day, there's something scheduled, which is why the value is 0b00010. If, for instance, something was scheduled on all 5 days, it would be 0b11111, and if nothing was scheduled at all, it would be 0b00000.

An operation I need to be able to do is to slide a given binaryRepresentation to a different firstDay. So, taking the example above, mapping that "schedule" to 2015-01-01 would have a binary representation of 0b00001, as it's been shifted back a day. Using good ol' JavaScript shifts works for schedules shorter than 32 days, but I need to work with longer schedules than that, which is why I'm using a BitSet.

Does that make sense?

fwiw I implemented a faster approach than what I planned to do here, so for my particular use case this isn't necessary. Feel free to close if ya don't think it's a worthwhile addition to the lib.

in this case I'd probably store a bit for each day of the year (or the maximum schedule length, 2 years, 4 years for leap year, etc), and then replace firstDay with an index to the starting day. In doing so, your bitset wouldn't be dependent on your firstDay var & you could eliminate the bit twiddling all together. Maybe that's what you did? In such a case I could see adding a start & end range to getCardinality() could be useful, but we'll cross that if it comes.

I'll close for now, since I think the problem can be solved in a more efficient manner than shifts on a bitset, feel free to reopen if there's a case where a shift would be a best practice.

Interesting approach! I'm going to consider adding the limitation of a max schedule length if it speeds up the algorithms substantially. At the moment, they support an arbitrarily long schedule, but realistically that won't be necessary...

I'll close for now

👍