mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

add indexOf to all immutable Set/collection #1786

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
As immutable Set and SortedSet cannot change, it is safe to add an indexOf 
method.

I find this usefull while tryng to implement a Map with immutable keyset

Original issue reported on code.google.com by mombelli...@gmail.com on 20 Jun 2014 at 10:13

GoogleCodeExporter commented 9 years ago
If you don't need special performance characteristics for this, then you can 
just do set.asList().indexOf(object).

If you do need special performance characteristics, that's a bit more of an 
issue: that's difficult to add without incurring performance overhead -- either 
CPU or memory -- for every other user of ImmutableSet, of whom the vast 
majority have no need for this feature.

Original comment by wasserman.louis on 20 Jun 2014 at 4:31

GoogleCodeExporter commented 9 years ago
I need performance, or i would just use a ImmutableList. I need to do a lot
of lookup, so I'm thinking about using a sorted array and the java's
binarySearch instead of a collection.

I would use a
Il 20/giu/2014 18:31 <guava-libraries@googlecode.com> ha scritto:

Original comment by mombelli...@gmail.com on 24 Jun 2014 at 9:07

GoogleCodeExporter commented 9 years ago
ImmutableSortedSet.asList().indexOf(...) already performs binary search, so if 
we're talking about sorted data, there's no need to roll it yourself.

Original comment by cpov...@google.com on 24 Jun 2014 at 11:48

GoogleCodeExporter commented 9 years ago
The simplest way is `ImmutableSortedMap<YourObject, Integer>` from which you 
can use the `keySet` as your `ImmutableSortedSet` and `get` for `indexOf`. If 
you don't care about the order, an `ImmutableHashMap` would be faster. If you 
need also indexed access, there's the `ImmutableBiMap`.

Original comment by Maaarti...@gmail.com on 24 Jun 2014 at 11:53

GoogleCodeExporter commented 9 years ago
@cpov asList() i THINK will slow everything down, and keeping one referece
of tge asList result may non be a good idea, as this isn't immutable a lot
of things may go wrong (basically why i use immutable break).
@maaarti the idea of the Map is good, but i fear it is hiding more
complexity (one array extraction more -at least-, also i would pay
attention on the creation of Integer..very dangerous, as there is no check
on what duplicate!)

i think the best thing to do in this case is to use an ImmutableList, as it
is compliant with the method signature from official API (only list has
indexOf) but that way it is not using binarySearch;
maybe a ImmutableSortedList witch want it's element to be Comparable,or an
ImmutableHashList wich use hashCode()
BTW i wanted this for
https://code.google.com/p/guava-libraries/issues/detail?id=1787, so if you
don't think fast immutable list value/index lookup it can be closed as nofix

Sorry to use your precius time to solve this nobbish problem :D

2014-06-24 13:53 GMT+02:00 <guava-libraries@googlecode.com>:

Original comment by mombelli...@gmail.com on 24 Jun 2014 at 12:20

GoogleCodeExporter commented 9 years ago
I guess, Chris' solution is better. Be assured that ImmutableWhatever.asList() 
gets cached and is immutable, too. However, I'd suggest to continue on SO with 
tag Guava, as this doesn't look as feature request anymore. Moreover, you'll 
get your answer even faster there.

http://stackoverflow.com/questions/tagged/guava

Original comment by Maaarti...@gmail.com on 24 Jun 2014 at 12:40

GoogleCodeExporter commented 9 years ago
 asList() i THINK will slow everything down, and keeping one referece
of tge asList result may non be a good idea, as this isn't immutable a lot
of things may go wrong (basically why i use immutable break).
@maaarti the idea of the Map is good, but i fear it is hiding more
complexity (one array extraction more -at least-, also i would pay
attention on the creation of Integer..very dangerous, as there is no check
on what duplicate!)

ImmutableSortedMap.keySet().asList() performs no "array extraction," creates no 
boxed Integers that were not already there, and returns in constant time.  
Please look up the details of the implementation: it is as cheap as you could 
want, and has binary-search indexOf() for free.  It's *designed* to be used in 
this way.

Original comment by wasserman.louis on 24 Jun 2014 at 3:10

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07