membership test problem
Stone, Joshua I
joshua.i.stone@intel.com
Thu Oct 26 21:24:00 GMT 2006
On Thursday, October 26, 2006 1:54 PM, Mike Mason wrote:
> What should the following code print? I expected TRUE, but got
> FALSE. Looks like if the array element value is 0, the membership
> test fails. Is this correct?
>
> p = pid()
> t = tid()
>
> prot_tx[p,t] = 0
>
> if ([p,t] in prot_tx) {
> print("TRUE\n")
> } else {
> print("FALSE\n")
> }
>
> - Mike
Setting a value to zero effectively deletes it from the map. I believe
this was done to save space in maps, since they have a limited number of
entries. Thus, 'map[key] = 0' is equivalent to 'delete map[key]'. The
same goes for setting an empty string.
This mostly does what one would expect, since reading an uninitialized
value also yields 0 or "". It's only the membership test where the
result may be surprising, since it's exposing an implementation detail.
At one time, the 'delete' keyword didn't work in all situations, so
setting 0 was the only way to clear items from the map. Now that
'delete' works universally, we could think about persisting 0 values so
the membership test succeeds.
Josh
More information about the Systemtap
mailing list