Discussions related to Visual Prolog
Martin Meyer
VIP Member
Posts: 296
Joined: 14 Nov 2002 0:01

Remove predicate

Unread post by Martin Meyer »

Hello Thomas and fellow users,

the implementation of the remove predicate in mapMSupport does not use the custom key comparison and its runtime is in O(n) (where -as usual- n denotes the number of elements in the collection):

Code: Select all

remove(T) :-     if T = site:getAll_nd() then         tuple(Key, _Value) = T,         site:removeKey(Key)     end if.
Therefor it could be replaced by

Code: Select all

remove(tuple(Key, _Value)) :-     site:removeKey(Key).
However it then still ignores the data part of the key/data-tuple, albeit the implementation of the contains predicate compares that part too:

Code: Select all

contains(tuple(Key, Value)) :-     Value = site:tryGet(Key).
Maybe the optimal solution would be, to introduce an additional custom data comparator, which defaults analogously to the custom key comparator to ::compare and use it in the remove and contains predicates.

User avatar
Thomas Linder Puls
VIP Member
Posts: 1232
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

Thank you.

The problem has already been solved for the next release.

Moving the solution back it looks like this:

Code: Select all

clauses     remove(tuple(Key, Value)) :-         if Value = site:tryGet(Key) then             site:removeKey(Key)         end if.
Adding a custom comparator for the @Data type seems to be overkill: because "goal" of maps lies in the keys. It is removeKey rather than remove that is the essential remove operation on a map.
Regards Thomas Linder Puls
Post Reply