I tried the following. Unfortunately there is no current way in Jade to compare keys for a dictionary, hence the GetAtKeyLeq on line 15. I'm assuming that comparing keys would be quick in comparison to a getAtKey type call.
Not sure if a startAtObject would be faster than at startKeyGeq method. Assume that startAtObject retrieves the key values and then does a getAtKey internally (at least I hope it's not a linear search for Dictionaries!)
As to whether it's more efficient. I guess it depends on the relative efficiency of the getAtKey calls vs the IndexOf. I'm assuming that the IndexOf will be quite slow for large collections (as it's probably a linear search.) The efficency would also be affected by what percentage of the collection you needed to transverse to count the entries.
You may need to do some performance testing on your collections to see which is best.
Code: Select all
countKeys_TM(key : KeyType) : Integer;
vars
iCount : Integer;
oMember,
oEnd,
oStart : MemberType;
oIter : Iterator;
oSelf : Dictionary;
begin
oStart := getAtKey( key );
if oStart = null then
iCount := 0;
else
oIter := createIterator;
oEnd := getAtKeyGtr(key);
startKeyGeq( key, oIter );
while oIter.next( oMember ) and oMember <> oEnd do
iCount := iCount + 1;
endwhile;
endif;
return iCount;
epilog
delete oIter;
end;