Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!nuzba.szn.dk!pnx.dk!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Using Enumerated Types as Array Indexes Date: Sat, 20 Aug 2011 00:20:02 +0200 Lines: 27 Message-ID: <9b85ssF8vsU1@mid.individual.net> 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> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net ns+3/96EbETZik3DMHDpkQg64KHHu1vTHlQL01Iniz4xEK8aw= Cancel-Lock: sha1:myzyFEco/rtbJERyD8vpD1G5jwA= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7256 On 19.08.2011 22:05, Andreas Leitgeb wrote: > Robert Klemme wrote: >> All solutions to fix remainder into modulo but keep the wrap around >> which have been proposed so far share a common disadvantage: if the >> number range is not a multiple of the divisor the value distribution >> will not be uniform. >> >> The solution I used to fix this employed a do while loop and >> compareAndSet() to reset the value to the beginning of the range (0 in >> this case) in a thread safe manner. > > There's a much simpler "meta-way" to fix it: just stick to powers of > 2 for the array length - then the overflow *does* always happen at a > multiple of the divisor and the value distribution uniform :-) I reckon you do not mean that suggestion serious. Just in case: limiting to powers of 2 was not an option here. And if it was, I would not have used modulo or AtomicInteger's features but just used binary & to cut off leading bits. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/