On Mon, 20 Jan 2014 11:31:17 +0400, Patrick Schaaf <[email protected]> wrote:
Regarding the N*M checking complexity, couldn't a little bit of memory
alleviate the problem?
For each array, have a "member type" pointer, initially NULL.
When the member type needs to be checked for the first time, and that
"member type" pointer is not already set, do an O(N) scan of the arrray,
testing compatibility of all members against the hinted type. Error on
mismatch, but when all members match, remember the type behind the "member
type" pointer.
When the member type needs to be checked and the "member type" pointer _is_
set, just compare it to the hinted type O(1).
There must be some overhead when elements are added to an array. Either
"when member type is set, forget it", or even "when member type is already
set, check compatiility of the newly added element with the member type,
and then remove member type on mismatch or keep it on match".
Finally when arrays are copied, and "member type" is already set, copy it
along with the array, Optionally some of the array functions like
array_merge might be made aware of the scheme, too.
BTW, I'm undecided regarding the need for the overall feature, from my
rather limited personal coding practise it seems to me that whenever I am
interested in member type checking I'm already looping over the array and
can easily add up-front instanceof checks to the loops with suitable
explicit error-out. Which I generally prefer over automatic errors,
especially fatal ones, because _I_ control the error case behaviour.
But I think _if_ such a scheme were added, the implementation described
above would solve the O(N*M) problem with acceptable overall cost.
best regards
Patrick
Hi Patrick,
I had this cache-idea too, but it will fail to get you any speedup if you check for array of interfaces (if you want to store objects of different classes but all this classes implement one interface). And this is the same reason why this proposal is very different from having typed array structure (cause, as long as I know, type of the typed array is always the end-class, objects of which will be stored in this array). So if we are proposing to add some new Spl structure - keep that in mind.
And again, reference to an element in the array can be used to change element of the array without affecting (and thus calling any of the functions related to) HT.