I am trying to write a DagNode class in Java, where two nodes are logically equal if they are equal to links.
The idea in C ++ - (I'm from C ++) - will use smart pointers and reference counting:
When a node is created, I will search in some table if that node already exists. If so, I will return the pointer to the old one. Otherwise, redo the node.
Overloaded C ++ methods, such as copy constructors and destructors, will recount, and when the number of node backlinks drops to 0, node is excluded from the above table. (C ++ will also free up memory.)
However, there seems to be no way to automatically do the recount in Java. I will need to do reference counting to find out when to deflate the node from the table (so that I can collect garbage), and I really want to avoid calling node->incRef() and node->decRef() at the beginning and end of each function.
How will we do this C ++ idiom in Java?
Joseph Victor
source share