[chuck-users] object / class hacking
Hans Aberg
haberg at math.su.se
Wed Sep 30 14:02:09 EDT 2009
On 30 Sep 2009, at 16:16, Andrew C. Smith wrote:
> But with this, you can't easily return the contents (which in this
> case is the list of names, not the "contents" in the normal sense) of
> the associative arrays.
Robert did not tell if he wanted lookup table or something like C++
std::set, which just stores the elements.
> If you can iterate, deleting is pretty easy.
> The problem there is just that everything has to be re-sorted. Without
> easy pointers, it's much more difficult to insert something into a
> sorted list, but luckily an array is basically just a series of
> pointers (right?).
An array in C is a contiguous memory chunk, at least address wise. In C
++, std::vector is such a chunk which can dynamically reallocated.
A hash map typically has such an array, where each entry is a singly
linked list. It average speed is proportional to the average depth of
the lists.
> If you had a sortable list (like note numbers of a chord or something)
> then you could drop the search function into the middle and get to
> your destination quite a bit faster. If you wanted to hack the array
> class to create .insert() and .remove() methods (where .remove() keeps
> the order without any null pointers) then please, do share. It could
> probably be done in ChucK sans-hacking, but where's the fun in that?
A check check in the source codes suggested chuck is using std::map
for the associative array part. Then it is just to export to the chuck
language iterator type and functions begin(), end(). The syntax might
be something like:
int m[0];
1 => m["a"];
2 => m["b"];
...
for (m.begin => iterator i; i < m.end(); i++)
<<< m[i] >>>;
One problem here is the type qualification of i: in C++, one would
have to give it as a type belonging to the class, like
std::map::iterator.
> What is the difference exactly between .cap() and .size() ?
Don't know. I just needed array size, and started using size() :-).
Hans
More information about the chuck-users
mailing list