[DGD]Arrays and call others

John West McKenna john at ucc.gu.uwa.edu.au
Sat Jun 23 11:22:12 CEST 2001


mtaylor writes:

>for (i = 0, sz = sizeof(static_contents[i]->query_reference_names()); i <
>sz; i++)
>{
>    if (static_contents[i]->query_reference_names()[ii] == name_test)
>    {
>        return static_contents[I];
>    }              
>}    

or

>Rnames = current_object->query_reference_names();
>
>for (i = 0, sz = sizeof(rnames); i < sz; i++)
>{
>    if (rnames[i] == name_test)
>    {
>        return current_object;
>    }              
>}    

I don't think memory is a problem in this case.  Anything that gets
temporarily allocated will be released, and there's not going to be much in
the first place.  Garbage collection is a wonderful thing.

The second one saves a lot of call_others.  If query_reference_names() is a
very short quick function, this probably won't make much difference.  It
should at least be making a copy of the object's internal names array,
instead of returning a reference to the original (if the function is simply
"return reference_names;", then a caller is able to modify the array in the
object).

There are other ways to do it.

if (current_object->is_reference_name(name_test))
{
    return current_object;
}

and let a function in the object handle the test.  This makes it possible
to extend the scheme, and do much fancier object-dependent things later (if
that's needed).

If you're expecting lots of these queries, and objects tend to stay where
they are, then you could try something along these lines:

mapping reference_name_to_object;

void add_object_to_scope(object obj)
{
    string *names;
    int i;

    ...whatever else needs to be done...
    names=obj->query_reference_names();
    for (i=0; i<sizeof(names); i++)
    {
         reference_name_to_object[names[i]]=obj;
    }
}

void remove_object_from_scope(object obj)
{
    string *names;
    int i;

    names=obj->query_reference_names();
    for (i=0; i<sizeof(names); i++)
    {
         reference_name_to_object[names[i]]=nil;
    }
    ... whatever else needs to be done ...
}

object find_object_from_reference_name(string name)
{
    return reference_name_to_object[name];
}

(That's just a start - it needs some way of dealing with multiple objects
sharing the same name)

This is more work when an object enters or leaves scope, but greatly
reduces the work of testing.  It does use a lot more memory, but is memory
use really a worry?  I'd have thought the primary concern is overall
response time.  Reducing memory use reduces swapping, which can make things
faster - but there are often methods that use more memory and are faster
anyway.

With a bit more thought, I'm sure it's possible to come up with a hundred
different methods - and each has its place.

I write code like your first option all the time, and I've stopped worrying
about it.  The speed is rarely an issue (when it is, you can come back and
improve the code later), so the best choice is whatever is easiest to write
and understand.

John

List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list