package tracking; import java.util.ArrayList; public class TrackRelation { public ArrayList L; public ArrayList R; public ArrayList P; public TrackRelation() { this.L = new ArrayList(); this.R = new ArrayList(); this.P = new ArrayList(); } /* Adds the pair {(d,e)} to the relation, if not there. Also adds {d} to the left set {L}, if not there, and {e} to the right set {R}, if not there. As a special case, if {d} is null, only adds {e} to {R}. If {e} is null, only adds {d} to {L}. If both are null, does nothing. */ public void addPair(TrackObject d, TrackObject e) { if ((d != null) && (! TrackObjectBelong(d, this.L))) { this.L.add(d); } if ((e != null) && (! TrackObjectBelong(e, this.R))) { this.R.add(e); } if ((d == null) || (e == null)) { return; } TrackObjectPair p = new TrackObjectPair(d, e); if (! PairBelong(p, this.P)) { this.P.add(p); } } /* Returns TRUE iff {d} occurs in {T[0..T.size-1]} */ public boolean TrackObjectBelong (TrackObject d, ArrayList T) { for (int i = 0; i < T.size(); i++) { TrackObject tmp = T.get(i); if (tmp == d) { return true; } } return false; } /* Returns TRUE iff there is a pair in {P[0..P.size-1]} with same left and right elements as {p}. */ public boolean PairBelong (TrackObjectPair p, ArrayList P) { for (int i = 0; i < P.size(); i++) { TrackObjectPair tmp = P.get(i); if ((tmp.L == p.L) && (tmp.R == p.R)) { return true; } } return false; } public TrackObject ApplyInverseRelation (TrackObject e, ArrayList P) { if (P == null) { return null; } for (int i = 0; i < P.size(); i++) { TrackObjectPair tmp = P.get(i); if (tmp.R == e) { return tmp.L; } } return null; } }