This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Is there simple way to convert long in to a BitSet?

9 replies
Vitalije Milosevic
Joined: 2012-01-21,
User offline. Last seen 42 years 45 weeks ago.
Hello,
Is there a simple way to create a BitSet object which would contain all integers <i> such that bit<i> is set in given long value?
I can write a function that would iterate through all bits and add than construct BitSet from such iterator, but I wander if there is shorter way, something like:

Long.toBitSet
or BitSet.fromLong(value)

Vitalije
ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Is there simple way to convert long in to a BitSet?
BitSet.fromArray(Array(x))

where x is your Long.  This works only for immutable bitsets, by the way (though you can always add this immutable bitset to an empty mutable one).

  --Rex

On Sat, Jan 21, 2012 at 4:04 AM, Vitalije Milosevic <vitalijem@gmail.com> wrote:
Hello,
Is there a simple way to create a BitSet object which would contain all integers <i> such that bit<i> is set in given long value?
I can write a function that would iterate through all bits and add than construct BitSet from such iterator, but I wander if there is shorter way, something like:

Long.toBitSet
or BitSet.fromLong(value)

Vitalije

Vitalije Milosevic
Joined: 2012-01-21,
User offline. Last seen 42 years 45 weeks ago.
Re: Is there simple way to convert long in to a BitSet?

Thank you. I would never guess it on my own.

Stefan Zeiger
Joined: 2008-12-21,
User offline. Last seen 27 weeks 3 days ago.
Re: Is there simple way to convert long in to a BitSet?
On 2012-01-21 11:39, Vitalije Milosevic wrote:
Thank you. I would never guess it on my own.
It's been renamed to BitSet.fromBitMask and BitSet.fromBitMaskNoCopy in master to make it more guessable. Available on both, mutable.BitSet and immutable.BitSet. Plus a matching .toBitMask method as well.

-sz
ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Is there simple way to convert long in to a BitSet?


On Mon, Jan 23, 2012 at 7:41 AM, Stefan Zeiger <szeiger@novocode.com> wrote:
On 2012-01-21 11:39, Vitalije Milosevic wrote:
Thank you. I would never guess it on my own.
It's been renamed to BitSet.fromBitMask

How about just BitSet.from (or fromLongs, if you also want to provide other types).
 
BitSet.fromBitMaskNoCopy

Isn't this name awfully clunky? How about BitSet.wrap?
 
in master to make it more guessable. Available on both, mutable.BitSet and immutable.BitSet. Plus a matching .toBitMask method as well.

I'm not sure this is better than toLongs, but it's nice to be able to go both ways.

  --Rex

Stefan Zeiger
Joined: 2008-12-21,
User offline. Last seen 27 weeks 3 days ago.
Re: Is there simple way to convert long in to a BitSet?
On 2012-01-23 17:02, Rex Kerr wrote:
pqkUfbu8pUaPKQNnZZd8gkhM0yn6bbWmWEdeVYqCmw [at] mail [dot] gmail [dot] com" type="cite">
It's been renamed to BitSet.fromBitMask

How about just BitSet.from (or fromLongs, if you also want to provide other types).

Simpler names like from, fromLongs or the old fromArray have the problem that they do not make the intent clear. After all, a BitSet is a set of numbers, so what would you expect a method called "from", which takes a sequence of numbers, to do? Maybe I'm the outlier here but I'd expect to get a set containing those numbers.

pqkUfbu8pUaPKQNnZZd8gkhM0yn6bbWmWEdeVYqCmw [at] mail [dot] gmail [dot] com" type="cite">
BitSet.fromBitMaskNoCopy

Isn't this name awfully clunky? How about BitSet.wrap?

Yes, deliberately clunky. Witness:

scala> val a = Array(1L, 0, 0)
a: Array[Long] = Array(1, 0, 0)

scala> val s = collection.immutable.BitSet.fromBitMaskNoCopy(a)
s: scala.collection.immutable.BitSet = BitSet(0)

scala> a(0) = 2

scala> s
res19: scala.collection.immutable.BitSet = BitSet(1)

An immutable collection being changed under the covers. I'd rather call the operation that leads to this effect fromBitMaskNoCopyDoNotUseUnlessYouReallyKnowWhatYouAreDoing than something as innocent as wrap.

Cheers,
Stefan
ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Is there simple way to convert long in to a BitSet?
On Mon, Jan 23, 2012 at 3:49 PM, Stefan Zeiger <szeiger@novocode.com> wrote:
On 2012-01-23 17:02, Rex Kerr wrote:
It's been renamed to BitSet.fromBitMask

How about just BitSet.from (or fromLongs, if you also want to provide other types).

Simpler names like from, fromLongs or the old fromArray have the problem that they do not make the intent clear. After all, a BitSet is a set of numbers, so what would you expect a method called "from", which takes a sequence of numbers, to do? Maybe I'm the outlier here but I'd expect to get a set containing those numbers.

How about fromMask and toMask?  Or fromBitMaskLeastSignificantLongFirst, if you like longer names?
 
BitSet.fromBitMaskNoCopy

Isn't this name awfully clunky? How about BitSet.wrap?

Yes, deliberately clunky. Witness:

scala> val a = Array(1L, 0, 0)
a: Array[Long] = Array(1, 0, 0)

scala> val s = collection.immutable.BitSet.fromBitMaskNoCopy(a)

I don't think making your API ugly is a good solution to a dangerously designed API.

"wrap" and "backed" both imply that the original array is there underlying the data.  If no short word like that adequately alerts the user that their "immutable" collection is not, you're just announcing
  iWishIDidntHaveThisInMyAPIButIHaveToForPerformancePleaseDontBeMad
or
  iAmPenalizingYouByMakingYourCodeUglySoYouWontUseItUnlessYouMust

Why even allow this with immutable bitsets, if you're worried about how it's used?  There is no direct access to the arrays underlying Vector, for example.

  --Rex

Ismael Juma 2
Joined: 2011-01-22,
User offline. Last seen 42 years 45 weeks ago.
Re: Is there simple way to convert long in to a BitSet?
On Mon, Jan 23, 2012 at 10:43 PM, Rex Kerr <ichoran@gmail.com> wrote:
Why even allow this with immutable bitsets, if you're worried about how it's used?  There is no direct access to the arrays underlying Vector, for example.

Yes, I am not sure this should be allowed. Seems to go against the general approach taken by immutable collections in Scala.
Best, Ismael
Stefan Zeiger
Joined: 2008-12-21,
User offline. Last seen 27 weeks 3 days ago.
Re: Is there simple way to convert long in to a BitSet?

On 2012-01-23 23:43, Rex Kerr wrote:
> How about fromMask and toMask? Or
> fromBitMaskLeastSignificantLongFirst, if you like longer names?

Yeah, I think fromMask and toMask would work as well.

> "wrap" and "backed" both imply that the original array is there
> underlying the data.

Except that's not always the case. The original data gets copied when
the array length is 1 or 2.

> If no short word like that adequately alerts the user that their
> "immutable" collection is not, you're just announcing
> iWishIDidntHaveThisInMyAPIButIHaveToForPerformancePleaseDontBeMad

That's indeed what this method is, a performance optimization. I didn't
do the original design (which includes this method as BitSet.fromArray)
but I guess the idea was that you'd be using a BitSet instead of some
generic Set when you really care about about the performance and memory
usage, so it makes sense to add unsafe methods for that purpose.

Cheers,
Stefan

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Is there simple way to convert long in to a BitSet?
+1 for fromMask, it's both concise and obvious what it does.
Naming is always difficult.  As the adage goes: There's only really 2 hard problems in programming; naming things, cache invalidation, and off-by-one errors


On 24 January 2012 09:19, Stefan Zeiger <szeiger@novocode.com> wrote:
On 2012-01-23 23:43, Rex Kerr wrote:
How about fromMask and toMask?  Or fromBitMaskLeastSignificantLongFirst, if you like longer names?

Yeah, I think fromMask and toMask would work as well.

 "wrap" and "backed" both imply that the original array is there underlying the data.

Except that's not always the case. The original data gets copied when the array length is 1 or 2.

If no short word like that adequately alerts the user that their "immutable" collection is not, you're just announcing
 iWishIDidntHaveThisInMyAPIButIHaveToForPerformancePleaseDontBeMad

That's indeed what this method is, a performance optimization. I didn't do the original design (which includes this method as BitSet.fromArray) but I guess the idea was that you'd be using a BitSet instead of some generic Set when you really care about about the performance and memory usage, so it makes sense to add unsafe methods for that purpose.

Cheers,
Stefan


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland