- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Is there simple way to convert long in to a BitSet?
Sat, 2012-01-21, 10:05
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
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
Sat, 2012-01-21, 11:41
#2
Re: Is there simple way to convert long in to a BitSet?
Thank you. I would never guess it on my own.
Mon, 2012-01-23, 13:51
#3
Re: Is there simple way to convert long in to a BitSet?
On 2012-01-21 11:39, Vitalije Milosevic wrote:
-sz
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
Mon, 2012-01-23, 17:11
#4
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
Mon, 2012-01-23, 21:51
#5
Re: Is there simple way to convert long in to a BitSet?
On 2012-01-23 17:02, Rex Kerr wrote:
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.
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
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
Mon, 2012-01-23, 23:51
#6
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:
How about fromMask and toMask? Or fromBitMaskLeastSignificantLongFirst, if you like longer names?
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
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
Tue, 2012-01-24, 01:11
#7
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:
Yes, I am not sure this should be allowed. Seems to go against the general approach taken by immutable collections in Scala.
Best, Ismael
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
Tue, 2012-01-24, 10:21
#8
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
Tue, 2012-01-24, 10:31
#9
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:
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
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: