Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.utanet.at!newscore.univie.ac.at!aconews-feed.univie.ac.at!aconews.univie.ac.at!not-for-mail Newsgroups: comp.lang.java.programmer From: Andreas Leitgeb Subject: Re: Using Enumerated Types as Array Indexes References: <4e4b0d81$0$314$14726298@news.sunsite.dk> <4e4b1fe4$0$314$14726298@news.sunsite.dk> <6d418cda-dab3-43df-a9ec-293b43f2bbd8@glegroupsg2000goo.googlegroups.com> <9b2aglFuj7U1@mid.individual.net> <9b72b5FrgfU1@mid.individual.net> <9b85ssF8vsU1@mid.individual.net> <9b9ckfF7uhU1@mid.individual.net> Reply-To: avl@logic.at User-Agent: slrn/pre0.9.9-111 (Linux) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: Date: 20 Aug 2011 11:45:33 GMT Lines: 33 NNTP-Posting-Host: gamma.logic.tuwien.ac.at X-Trace: 1313840733 tunews.univie.ac.at 71616 128.130.175.3 X-Complaints-To: abuse@tuwien.ac.at Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7263 Robert Klemme 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. >> 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. 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) But at this point it is possible that your compareAndSet approach is actually clearer/easier to understand :-)