Better ImmutableList #3816
Conversation
|
There is one stupid error in |
55024a9 to
c08f219
Compare
Drodt
left a comment
There was a problem hiding this comment.
Nice work! Do you have any idea why tests are failing?
Drodt
left a comment
There was a problem hiding this comment.
Nice work! Do you have any idea why tests are failing?
|
Thanks for the help. The tests is green now. The null pointer warning will be fixed. |
|
Can we stay with Java's name scheme and call it |
It depends on how you read this. My reading is: The main idea of this PR is to get rid of To tackle this, the PR also contains operational wrappers for |
|
Nice idea! Appears to make a lot of sense indeed! So: Could you or Claude (& you checking) please provide a lot of documentation on the public interface, the static methods and the new classes? Perhaps you can also mention there: Why does ImultabeListList exist? Can the array class not do the same job? |
# Conflicts: # key.core/src/main/java/de/uka/ilkd/key/proof/init/InitConfig.java # key.core/src/main/java/de/uka/ilkd/key/taclettranslation/lemma/TacletLoader.java # Conflicts: # key.core/src/main/java/de/uka/ilkd/key/nparser/builder/TacletPBuilder.java # key.core/src/main/java/de/uka/ilkd/key/rule/tacletbuilder/TacletBuilder.java # key.core/src/main/java/de/uka/ilkd/key/rule/tacletbuilder/TacletGenerator.java
You should never need to use the names in your snippets. You should always use the interface
The impl classes are package-private!
There is one use case: The cases in which you have a list, and you are knowing that the list is fresh and has not leaked, i.e., in In all other cases, you should copy the list to an "unmodifiable" list, and in this cases you have not won anything in contrast to an array (except for one layer unmodifiability). |
|
Thanks for the response.
OK, I can see this. I like that they are package private; this is good.
Currently, the only constructor does not spare copying.
... and one or two layers of additional call complexity. |
| @@ -15,9 +15,6 @@ | |||
|
|
|||
| public class ImmutableArray<S extends @Nullable Object> | |||
There was a problem hiding this comment.
You mentioned this class is covered by ImmutableListArray
Consider adding @Deprecated
| * @author Alexander Weigl | ||
| * @version 1 (25.04.26) | ||
| */ | ||
| final class ImmutableListConcat<T extends @Nullable Object> |
| * @version 1 (25.04.26) | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| final class ImmutableListList<T extends @Nullable Object> implements ImmutableList<T> { |
| final class ImmutableListList<T extends @Nullable Object> implements ImmutableList<T> { | ||
| private final List<T> data; | ||
|
|
||
| public ImmutableListList(Collection<T> data) { |
There was a problem hiding this comment.
If this is the only constructor, this class does not really make sense.
| * @author Alexander Weigl | ||
| * @version 1 (25.04.26) | ||
| */ | ||
| final class ImmutableListReverse<T extends @Nullable Object> implements ImmutableList<T> { |
There was a problem hiding this comment.
Documentation for this class, please.
| * @author Alexander Weigl | ||
| * @version 1 (25.04.26) | ||
| */ | ||
| final class ImmutableListSubList<T extends @Nullable Object> |
| /** | ||
| * List interface to be implemented by non-destructive lists | ||
| */ | ||
| @SuppressWarnings("unchecked") |
There was a problem hiding this comment.
Please document the class and all methods because they are API toplevel.
There was a problem hiding this comment.
The methods like of(T e1, T e2) with a fixed number of arguments are usually a lot faster than creating an intermediate array. Perhaps keep them?
| * @return IList<T> the new list | ||
| */ | ||
| ImmutableList<T> prepend(@SuppressWarnings("unchecked") T... array); | ||
| default ImmutableList<T> prepend(ImmutableList<T> list) { |
There was a problem hiding this comment.
This applies also to the next method signatures ...
| */ | ||
| final class ImmutableListConcat<T extends @Nullable Object> | ||
| implements ImmutableList<T> { | ||
| private final ImmutableList<T> left; |
There was a problem hiding this comment.
Do they really need to have the same type parameter?
I think ? extends T should actually do for both of them?
Issue
We currently have two implementations of immutable sequences with
ImmutableListandImmutableArray. This requires conversions sometimes, but mainly it leads to confusion about which implementation should be taken.Intended Change
This PR unifies everything under
ImmutableList.There are two fresh children classes:
ImmutableListArrayandImmutableListList. Both behave likeImmutableList, but use different data storage (T[]orList<T>) in the background.Some operations from
ImmutableListare quite expensive on this data storages, e.g.,tail()orreverse(). Therefore, I introduced views that emulate these operations, without copying the data, i.e.,ImmutableListConcatasImmutableList#prepend(other)ImmutableListReverseforImmutableList#reverse()ImmutableListSubListforImmutableList#take(n),ImmutableList#skip(n),ImmutableList#tail()These operations are now in$O(1)$ .
The class
ImmutableArrayshould be considered obsolete.Developers should use only the methods and functions of
ImmutableList. Implementation may override them if there is a faster implementation, e.g,tail()onImmutableSList.Plan
ImmutableSListtoImmutableList.Type of pull request
Refactoring (behaviour should not change or only minimally change)
New feature (non-breaking change which adds functionality)
There are changes to the (Java) code
Ensuring quality