Re: patch for wonders NSArray implementation
Re: patch for wonders NSArray implementation
- Subject: Re: patch for wonders NSArray implementation
- From: Johann Werner <email@hidden>
- Date: Thu, 06 Jun 2013 11:38:12 +0200
Hi Oliver,
you are right that the behavior is a little different in Wonder. I looked into that method that had been changed to the current form in April 2009:
NSArray._findObjectInArray(…) is a private method so it cannot be called outside of NSArray which makes it easier to assess the implications of the different behaviors (code snippet at end of mail for illustration purposes). If that method is called with the param identical = true – like from NSArray.indexOfIdenticalObject(…) – the result is not affected by the different coding, thus everything is fine.
If that method is called with identical = false – like from NSArray.indexOfObject(…) – the result can differ. Let's make an example:
Integer one = new Integer(1),
Integer otherOne = new Integer(1);
NSArray<Integer> array = new NSArray<Integer>(new Integer[] { one, otherOne } );
int index = array.indexOfObject(otherOne);
With the original implementation of NSArray we would get an index of 1 as it will first check all objects on reference equality in the first run and then on value equality in a second run. With the Wonder version we will get an index of 0 as that implementation checks every item on reference and on value equality before checking the next one. If you really wanted the exact index you would have to call
int index = array.indexOfIdenticalObject(otherOne);
To sum up, that code change in NSArray is an optimization compared to the original. Especially with big arrays that method will probably take much less time to return. You have to choose between indexOfObject and indexOfIdenticalObject in your code depending on if it is really important to you to get the identical object, though for the cases I can think of you should be fine getting the index of an object that could be either identical or equal. If not please elaborate :)
jw
Current Wonder code:
private final int _findObjectInArray(int index, int length, Object object, boolean identical) {
if (count() > 0) {
Object[] objects = objectsNoCopy();
int maxIndex = (index + length) - 1;
for (int i = index; i <= maxIndex; i++) {
if (objects[i] == object) {
return i;
}
if (!identical && object.equals(objects[i])) {
return i;
}
}
}
return NotFound;
}
Original code:
private final int _findObjectInArray(int index, int length, Object object, boolean identical) {
if (count() > 0) {
Object[] objects = objectsNoCopy();
int maxIndex = (index + length) - 1;
for (int i = index; i <= maxIndex; i++) {
if (objects[i] == object) {
return i;
}
}
if (!identical) {
for (int i = index; i <= maxIndex; i++) {
if (object.equals(objects[i])) {
return i;
}
}
}
}
return NotFound;
}
Am 18.02.2013 um 15:43 schrieb Oliver Egger <email@hidden>:
> Hi all
>
> During upgrading the wonder frameworks to the 6.0 versions we noticed a changed behaviour
> in NSArray.
>
> In the NSArray_findObjectInArray function, if identical is set to false, the orginial version does not
> return an earlier equal object (as the wonder version does) but the first object matching the same object.
>
> Please consider to include the attached patch, if you need further information do not hesitate to contact
> me.
>
> Best regards
> Oliver
>
>
> @@ -510,10 +510,12 @@ public class NSArray<E> implements Cloneable, Serializable, NSCoding, NSKeyValue
>
> if (objects[i] == object) {
>
> return i;
>
> }
>
> - if (!identical && object.equals(objects[i])) {
>
> + }
>
> + if(!identical)
>
> + for (int i = index; i <= maxIndex; i++) {
>
> + if (object.equals(objects[i])) {
>
> return i;
>
> }
>
> -
>
> }
>
>
> }
>
>
> --
> --
> oliver egger
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden