Velocity Reviews > Java > Using Enumerated Types as Array Indexes

# Using Enumerated Types as Array Indexes

Robert Klemme
Guest
Posts: n/a

 08-20-2011
On 08/20/2011 01:45 PM, Andreas Leitgeb wrote:
> Robert Klemme<(E-Mail Removed)> wrote:
>> On 08/20/2011 01:11 AM, Andreas Leitgeb wrote:
>>> Of course I don't know the wider context, but to me it seemed as if
>>> a power-of-2 sized array would be such a natural choice, that you'd

>> That never occurred to me. Limiting to powers of 2 does have some
>> advantages in some situation but it's certainly nothing I would consider
>> "natural" generally.

>
> Not generally, but in a context, where you've got an integer uniformly
> cycling through a power-of-2 sized range, and quickly(develop-time-wise)
> need a uniform cycle on a smaller range.

??? If the power of two is given then of course limiting sizes to power
of two is "natural" - whatever that means in case of a tautology.

>>> May I ask, what other entity imposes the array length on you, and why
>>> choosing the next larger power of 2 will not do?

>> The value is user configurable and limiting to powers of 2 as valid
>> values would reduce the range of possible values unnecessarily
>> especially since the value is resource intensive - so the difference
>> between 6 and 8 could be significant.

>
> Since I can't believe the array's length itself is at stake here, I guess
> it's the objects that get stored in it, and only freed when overwritten.

Roughly. Items are not overwritten, dunno where you got that from.
Andreas, you are on the Holzweg.

> If that's the case, I'd do this:
> M being the configured value for the "size"
> N being the smallest power-of-2 that is>= M
>
> create the array with a size of N, and before each write for a new object
> into position n, you write a null at position (n - M)& (N-1).
> (modulo eventual off-by-one-errors)

Number M is given, it's the number of items to pick (in a thread safe
manner also but that is irrelevant for the math). If M is not a power
of two then N > M and N mod M != 0 and no matter how you store your M
items in an array of length N, iterating through the complete array of N
elements will never produce uniform distribution of picks per item. If
you only use first M slots of the array and iterating them you simply
waste N - M slots of the array and the power of two magic is completely
worthless.

> But at this point it is possible that your compareAndSet approach
> is actually clearer/easier to understand

Certainly.

Cheers

robert

Andreas Leitgeb
Guest
Posts: n/a

 08-20-2011
Robert Klemme <(E-Mail Removed)> wrote:
> On 08/20/2011 01:45 PM, Andreas Leitgeb wrote:
>> Not generally, but in a context, where you've got an integer uniformly
>> cycling through a power-of-2 sized range, and quickly(develop-time-wise)
>> need a uniform cycle on a smaller range.

> ??? If the power of two is given then of course limiting sizes to power
> of two is "natural" - whatever that means in case of a tautology.

It was merely a semi-tautology: big (2^32) range on the one hand means
that a (smaller) 2^# range on the other hand is easier handled.
A triviality, of course, but not exactly a tautology. An irrelevancy,
too, as it turned out, as you have no free choice on the smaller range.

>> Since I can't believe the array's length itself is at stake here, I guess
>> it's the objects that get stored in it, and only freed when overwritten.

> Roughly. Items are not overwritten, dunno where you got that from.

That was a weak (and obviously inaccurate) extrapolation from some
vague patterns that I believed to recognize.

> Andreas, you are on the Holzweg.

I expected the possibility of it happening

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Immortal Nephi C++ 4 08-24-2010 05:20 PM Kevin Neilson VHDL 9 05-09-2008 05:19 PM ypjofficial@indiatimes.com C++ 8 01-23-2006 12:29 PM David R. Throop XML 0 05-05-2005 11:39 PM rfractal30 Java 6 02-25-2005 02:38 AM

Advertisments