Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7264
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Robert Klemme <shortcutter@googlemail.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Using Enumerated Types as Array Indexes |
| Date | Sat, 20 Aug 2011 15:23:58 +0200 |
| Lines | 54 |
| Message-ID | <9b9qreFj62U1@mid.individual.net> (permalink) |
| References | <b9ad6662-3240-4754-86a0-b715e8cdbc48@z7g2000vbp.googlegroups.com> <4e4b0d81$0$314$14726298@news.sunsite.dk> <j2f3ci$1lh$1@dont-email.me> <4e4b1fe4$0$314$14726298@news.sunsite.dk> <j2fc1g$b9j$1@dont-email.me> <6d418cda-dab3-43df-a9ec-293b43f2bbd8@glegroupsg2000goo.googlegroups.com> <vTL2q.38278$tp.27319@newsfe06.iad> <slrnj4ngfi.6gl.avl@gamma.logic.tuwien.ac.at> <f08d98e8-e04d-4d4e-b518-44bba735dba6@glegroupsg2000goo.googlegroups.com> <9b2aglFuj7U1@mid.individual.net> <j2hhnj$4js$1@dont-email.me> <j2hr70$qiq$1@dont-email.me> <9b72b5FrgfU1@mid.individual.net> <slrnj4tgh1.6gl.avl@gamma.logic.tuwien.ac.at> <9b85ssF8vsU1@mid.individual.net> <slrnj4trd9.6gl.avl@gamma.logic.tuwien.ac.at> <9b9ckfF7uhU1@mid.individual.net> <slrnj4v7it.6gl.avl@gamma.logic.tuwien.ac.at> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net 7DgQWkFetwpR3/If7HL3/gB+evvmG1o+807uSpvWvw/pg6Sfc= |
| Cancel-Lock | sha1:vRAbuWTe9E7wcplcWz7Jf7UpNzg= |
| User-Agent | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.18) Gecko/20110617 Lightning/1.0b2 Thunderbird/3.1.11 |
| In-Reply-To | <slrnj4v7it.6gl.avl@gamma.logic.tuwien.ac.at> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7264 |
Show key headers only | View raw
On 08/20/2011 01:45 PM, Andreas Leitgeb wrote: > Robert Klemme<shortcutter@googlemail.com> 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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Using Enumerated Types as Array Indexes KevinSimonson <kvnsmnsn@hotmail.com> - 2011-08-16 07:53 -0700
Re: Using Enumerated Types as Array Indexes Patricia Shanahan <pats@acm.org> - 2011-08-16 09:30 -0700
Re: Using Enumerated Types as Array Indexes Tom Anderson <twic@urchin.earth.li> - 2011-08-16 21:37 +0100
Re: Using Enumerated Types as Array Indexes Gene Wirchenko <genew@ocis.net> - 2011-08-16 14:54 -0700
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-16 20:47 -0400
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-16 20:38 -0400
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-16 17:52 -0700
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-16 21:56 -0400
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-16 20:20 -0700
Re: Using Enumerated Types as Array Indexes Lew <lewbloch@gmail.com> - 2011-08-16 20:45 -0700
Re: Using Enumerated Types as Array Indexes Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-17 06:31 -0300
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-17 13:28 +0000
Re: Using Enumerated Types as Array Indexes Lew <lewbloch@gmail.com> - 2011-08-17 07:17 -0700
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-17 19:02 +0200
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-17 16:09 -0700
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-17 23:48 +0000
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-17 17:34 -0700
Re: Using Enumerated Types as Array Indexes Patricia Shanahan <pats@acm.org> - 2011-08-17 18:07 -0700
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-18 08:22 -0700
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 18:37 +0000
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-18 11:50 -0700
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 20:49 +0000
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-18 14:48 -0700
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 22:50 +0000
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-17 21:49 -0400
Re: Using Enumerated Types as Array Indexes Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-17 21:50 -0400
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-19 14:13 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-19 20:05 +0000
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-20 00:20 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-19 23:11 +0000
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-20 11:21 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-20 11:45 +0000
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-20 15:23 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-20 16:15 +0000
Re: Using Enumerated Types as Array Indexes Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-17 21:34 -0400
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-18 07:58 +0200
Re: Using Enumerated Types as Array Indexes Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-18 06:29 -0300
Re: Using Enumerated Types as Array Indexes RedGrittyBrick <RedGrittyBrick@spamweary.invalid> - 2011-08-18 10:47 +0100
Re: Using Enumerated Types as Array Indexes Lew <lewbloch@gmail.com> - 2011-08-18 11:35 -0700
Re: Using Enumerated Types as Array Indexes Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-18 18:30 -0300
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-19 14:57 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-19 20:10 +0000
Re: Using Enumerated Types as Array Indexes Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-19 19:12 -0300
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-20 00:26 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-19 22:59 +0000
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-17 21:34 -0400
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-17 21:46 -0400
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-17 19:51 -0700
Re: Using Enumerated Types as Array Indexes David Lamb <dalamb@cs.queensu.ca> - 2011-08-17 08:29 -0400
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-17 16:04 +0200
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-17 21:51 -0400
Re: Using Enumerated Types as Array Indexes Niklas Holsti <niklas.holsti@tidorum.invalid> - 2011-08-17 11:24 +0300
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-17 21:55 -0400
Re: Using Enumerated Types as Array Indexes Niklas Holsti <niklas.holsti@tidorum.invalid> - 2011-08-18 10:59 +0300
Re: Using Enumerated Types as Array Indexes Roedy Green <see_website@mindprod.com.invalid> - 2011-08-17 22:09 -0700
csiph-web