+ public class BindingGroup {
+ private HashSet<E> left = new HashSet<E>();
+ private HashSet<E> right = new HashSet<E>();
+ public BindingGroup() { }
+ public BindingGroup(E e) {
+ left.add(e);
+ }
+ public void add(E e) {
+ if (e.bg != null) {
+ if (e.bg == this) return;
+ for(E ex : e.bg.left) {
+ ex.bg = this;
+ left.add(ex);
+ }
+ for(E ex : e.bg.right) {
+ ex.bg = this;
+ right.add(ex);
+ }
+ } else {
+ left.add(e);
+ e.bg = this;
+ }
+ }
+ public void dobind(E e) {
+ // assumes e is part of the "left" set
+ for(E ex : left) {
+ if (ex==e) continue;
+ e.p1.bind(ex.p1);
+ e.p2.bind(ex.p2);
+ }
+ for(E ex : right) {
+ if (ex==e) continue;
+ e.p1.bind(ex.pair.p1);
+ e.p2.bind(ex.pair.p2);
+ }
+ }
+ public void shatter(BindingGroup bg1, BindingGroup bg2) {
+ for(E e : left) {
+ e.shatter(e.midpoint(), bg1, bg2);
+ }
+ for(E e : right) {
+ e.shatter(e.midpoint(), bg2, bg1); /* swap correct? */
+ }
+ }
+ }
+