H
Harald Kirsch
In my previous life, using C, the two arrays
contained pointers to objects and I kept
the arrays sorted according to the pointers'
equivalent int value. Comparing the two arrays
to check whether they contain identical objects
could be done in O(n) where n is the length
of the arrays.
In Java, identity can be established by x==y,
but sadly there is no total order defined
on object references. Consequently I cannot
keep the arrays sorted --- except in special
cases where objects implement Comparable. As
a result, the comparison becomes O(n^2).
A half solution uses System.identityHashCode()
to keep the arrays roughly sorted. Only within
a run of equal hash codes, each element of
one array must be compared with each element
of another array.
The resulting code is unnecessarily complicated
in particular in view of the fact that
on most Java implementations the
System.identityHashCode() is in fact unique.
It seems a total order on object references
would be a great thing for such problems.
Other ideas?
Harald Kirsch
contained pointers to objects and I kept
the arrays sorted according to the pointers'
equivalent int value. Comparing the two arrays
to check whether they contain identical objects
could be done in O(n) where n is the length
of the arrays.
In Java, identity can be established by x==y,
but sadly there is no total order defined
on object references. Consequently I cannot
keep the arrays sorted --- except in special
cases where objects implement Comparable. As
a result, the comparison becomes O(n^2).
A half solution uses System.identityHashCode()
to keep the arrays roughly sorted. Only within
a run of equal hash codes, each element of
one array must be compared with each element
of another array.
The resulting code is unnecessarily complicated
in particular in view of the fact that
on most Java implementations the
System.identityHashCode() is in fact unique.
It seems a total order on object references
would be a great thing for such problems.
Other ideas?
Harald Kirsch