Guan, AFAIK there is no advantage. There are only two good reasons to
use an ns_set rather than a Tcl array:
- You're using an AOLserver API which uses ns_set.
- Maybe because you need to allow multiple key/value
pairs all with the same key. ns_set supports this
directly, Tcl arrays do not.
Many ns_set operations scale very poorly (O(n) with the number of
items in the set), and the ns_set API is generally less powerful and
more annoying to work with than the Tcl array API.
On the other hand, the ns_set C code is very simple and thus might be
easier than Tcl arrays to embed in other C code.
For use from C code, using ns_set could also be easier than Tcl arrays
in some cases, as ns_set has an explicit and complete API in both Tcl
and C. Tcl arrays, on the other hand, don't really have any complete
C API. Some Tcl array operations are really easy from C, but for
others you'll need to code up your own (relatively simple) helper
functions. E.g., I needed to write one for "array names", basically
it's just a wrapper around Tcl_ArrayObjCmd. (Yes you can just call
Tcl_Eval from C to execute the "array names" Tcl command directly, but
this is a Bad Idea if you're doing this from within an inner loop; the
performance hit can be very large.)
I'd say always use a Tcl array (or nsv when appropriate) by default,
never use an ns_set unless you have a very specific and
concrete reason why you should do so.
Actually, ns_set looks like a legacy data structure to me, I'd never
choose to use it for anything new. Even if I really needed a set-like
data structure that supports duplicate keys, I probably wouldn't use
ns_set, I'd code my own using Tcl Arrays (and maybe Tcl lists too)
underneath.