Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36826

Re: no easy way to do a reverse pointer?

From Martin Gregorie <martin@address-in-sig.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: no easy way to do a reverse pointer?
Date 2017-07-07 19:48 +0000
Organization A noiseless patient Spider
Message-ID <ojooib$v3s$1@dont-email.me> (permalink)
References <6c91c2b4-7d8f-42ba-8a51-eaef43635f49@googlegroups.com>

Show all headers | View raw


On Fri, 07 Jul 2017 11:56:34 -0700, Eric Douglas wrote:

> public class A {
>   private B fld;
>   public void setFld(B value) {
>     fld = value;
>   }
> }
> 
> So field fld is an object, which means it's a pointer to an instance of
> type B, and I can have multiple instances of A referencing a single
> instance of B.
> So the easiest way for an instance of B to tell you which instances of A
> hold a reference?
> 
> public class A {
>   private B fld;
>   public void setFld(B value) {
>     if (fld != null) {
>       fld.dropRef(this);
>     } value.refer(this); fld = value;
>   }
> }
> public class B {
>   private HashSet<A> refs = new HashSet();
>   public void refer(A value) {
>     refs.add(value);
>   }
>   public HashSet getRefs() {
>     return refs;
>   }
>   public void dropRef(A value) {
>     refs.remove(value);
>   }
> }

Thats actually backwards: the A instance needs to hold a list of all the 
B instances it owns while each B instance holds a reference to its owning 
A instance. It has to be that way round or you can't easily find all the 
Bs that belong to a selected A. The fun and games comes when you want to 
find all the As that reference a given B. At first glance you just keep a 
list of As in each B but that would be hell on wheels to maintain. 

Its OK to use the array of references once, I've done exactly that for a 
set of rosters I maintain: each Roster header has a list of references to 
the roster Members and they in turn have a list of potential Duty slots. 
This allows a Member to be on more than one Roster. 

Working in reverse is different. Theres another structure with an entry 
per rostered date and each of these has a reference to all the Duty slots 
on that day. 

Another way to tackle this is the use the RDBMS model of insisting that 
each class instance has a unique key. Each list of classes is linked to a 
TreeMap instance which allows all access to by managed by using the keys 
in the TreeMaps. For youe example the ERD looks like this:

      (  A* )
         |
         |
        /|\
      (  B  )

and the list of TreeMaps is:

TreeMap Akey  - holds A's unique prime key and a reference to each A
                instance
TreeMap Bkey  - holds B's unique prime key and a reference to each B
                instance
TreeMap Balt1 - holds alternate key 1 for B and a reference to the B
                instance. The Alternate key is  Bkey.Akey
TreeMap Balt2 - holds alternate key 2 for B and a reference to the B
                instance. The Alternate key is  Bkey.Akey

Using this arrangement you can:
 
- find all B instances belonging to an A instance by using Balt1: 
  they are all linked to the keys whose first part is the A instance
  prime key.

- find all the A instances that are linked to a B instance by using Balt2:
  they are all the keys whose first part is the B prime key  

In practise I used both approaches in the rostering application: 
references are used to build the list of members in each roster and the 
list of possible duty slots for each member, but almost all the cross 
references etc are managed by key access via appropriately ordered 
TreeMaps.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

no easy way to do a reverse pointer? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 11:56 -0700
  Re: no easy way to do a reverse pointer? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-07 19:48 +0000
    Re: no easy way to do a reverse pointer? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 13:28 -0700
      Re: no easy way to do a reverse pointer? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-07 21:47 +0000
      Re: no easy way to do a reverse pointer? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-07 18:06 -0400
        Re: no easy way to do a reverse pointer? lewbloch@gmail.com - 2017-07-14 14:32 -0700

csiph-web