This is the mail archive of the
systemtap@sourceware.org
mailing list for the systemtap project.
RE: membership test problem
- From: "Stone, Joshua I" <joshua dot i dot stone at intel dot com>
- To: "Mike Mason" <mmlnx at us dot ibm dot com>
- Cc: <systemtap at sourceware dot org>
- Date: Thu, 26 Oct 2006 14:24:20 -0700
- Subject: RE: membership test problem
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