tags:

views:

64

answers:

3
+1  A: 

I can't answer your all questions, but

I'm under the impression that Java will delete all these pointers implicitly if I delete the underlying object.

In Java you don't delete an object; instead, it gets effectively deleted when the reference count to it goes to zero. However, you may want to utilize weak references here; this way the object disappears when the strong reference count goes to zero.

Joonas Pulakka
A: 

Actually, your impression is basicly backwards: Java will assume an object (the host in this case) is dead when there's no longer any pointer to give access to that object. At that point it'll clean up the object (automatically).

At a guess, however, there's one collection that "owns" the hosts, and would be responsible for deleting a host when it dies. The other pointers to the host don't own it. If that's the case, then in C++ you'd normally handle this by having the "owning" collection contain a shared_ptr to the host, and the other collections contain weak_ptrs to the host. To use the object via a weak_ptr, you have to first convert that to a shared_ptr that you can dereference to get to the host itself. If, however, the object has been deleted, the attempt at converting the weak_ptr to a shared_ptr will fail, and you'll know the host is dead (and you can then delete your reference to it).

Jerry Coffin
+1  A: 
I'm under the impression that Java will delete all these pointers implicitly if I delete the underlying object. Is this true?

Nope. You actually have it backwards; if you delete all the pointers, Java will delete the underlying object. So you'll still need to search through all three of your data structures (hosts, immune, and infectious) to kill that particular host.

However, this "search" will be fast and simple if you use the right data structures; a HashSet will do the job very nicely.


    private HashSet<Host> hosts;
    private HashSet<Host> immune;
    private HashSet<Host> infectious;

    public void killHost(Host deadManWalking) {
        hosts.remove(deadManWalking);
        immune.remove(deadManWalking);
        infectious.remove(deadManWalking);
    }

It's really that simple, and will take place in O(lg n) time. (Though you will have to override the equals and hashCode methods in your implementation of Host; this is not technically challenging.)

My memories of C++ are too hazy for me to give any sort of authoritative comparison between the two languages; I did a ton of C++ work in college, haven't touched it since. Will C++ code run faster? Done right and assuming you don't have any memory leaks, I'd suspect it would, though Java's rep as a slow language is mostly a holdover from its youth; it's pretty decent these days. Easier to write? Well, give that you'd be learning the language, probably not. But the learning curve from C++ to Java is pretty gentle, and I personally don't miss C++ at all. Once you know the languages, Java is, in my opinion, vastly easier to work with. YMMV, natch, but it may well be worth the effort for you.

BlairHippo
Thanks to you and the other commenters for correcting my bad ideas about Java. I had also forgotten about the existence of hash sets (or multi-index containers in Boost). In your example in Java, there is still only one instance of each host in memory, right? I absolutely want to avoid copying instances of an object. What you wrote looks easy in Java, but I'm not sure how to use pointers as keys for hash sets in C++. I will look into it. Thanks again.
Sarah
Yup. The HashSets will be collections of pointers; if the same Host appears in multiple HashSets, then what you'll have are multiple pointers to the same object. Glad I could help. :-)
BlairHippo
... and since nobody mentioned this: there's actually no such thing as an explicit destructor in Java. Deleting all the pointers to an object is actually the ONLY way to delete the object.
BlairHippo