I have been wondering why the index operations on multisets work like they do:
| > multiset v=(<1,1,1>); | > v[1]; | Result: 1 | > v[1]=0; | Result: 0 | > v; | Result: (<1,1>)
This could of course be used to do a `- that removes one for one:
multiset res=copy_value(v); foreach (v;mixed elem;) res[elem]=0; // remove one
But why doesn't v[1] return 3 above, and remove all the elements with v[1]=0?
It would also be much more useful to do v[1]=17 to get 17 of element 1. You can't, however, even add elements using that method now:
| > v[1]=17; | Result: 17 | > v; | Result: (<1,1>) | > v[1]=1; | Result: 1 | > v; | Result: (<1,1>)
How much would break if we fixed this behaviour, so that
multiset v=(<1,1,1>); v[1] == 3 v[1]=0 => v==(<>) v[1]=17 => v==(<1,1,1,1...1,1>) /* 17 elements */
Sidenote: This code works for the above `- for both the current and my suggestion:
multiset res=copy_value(v); foreach (v;mixed elem;int n) res[elem]-=n; // remove n
/ Mirar
Previous text:
2003-02-05 17:16: Subject: Xor
Depends on what `- is used for. I guess the common use is to clear out all elements.
Having (<1,1,2>) - (<1>) = (<1,2>) would be kind of nice, but then one would need some other operation for deleteing all the ones. m - (<1,1,1,1,1,1,1,1,1>) or m - sizeof(m) * (<1>) isn't particularly pretty.
BTW, why doesn't int * multiset work?
/ Niels Möller ()