/// <summary>Intersect this tree reference with another, returning a new tree reference /// which contains all of the common elements, starting with the root element. /// /// Note that relative references by their nature can't share steps, so intersecting /// any (or by any) relative ref will result in the root ref. Additionally, if the /// two references don't share any steps, the intersection will consist of the root /// reference. /// /// </summary> /// <param name="b">The tree reference to intersect /// </param> /// <returns> The tree reference containing the common basis of this ref and b /// </returns> public virtual TreeReference intersect(TreeReference b) { if (!this.Absolute || !b.Absolute) { return(TreeReference.rootRef()); } if (this.Equals(b)) { return(this); } TreeReference a; //A should always be bigger if one ref is larger than the other if (this.size() < b.size()) { a = b.Clone(); b = this.Clone(); } else { a = this.Clone(); b = b.Clone(); } //Now, trim the refs to the same length. int diff = a.size() - b.size(); for (int i = 0; i < diff; ++i) { a.removeLastLevel(); } int aSize = a.size(); //easy, but requires a lot of re-evaluation. for (int i = 0; i <= aSize; ++i) { if (a.Equals(b)) { return(a); } else if (a.size() == 0) { return(TreeReference.rootRef()); } else { if (!a.removeLastLevel() || !b.removeLastLevel()) { //I don't think it should be possible for us to get here, so flip if we do throw new System.SystemException("Dug too deply into TreeReference during intersection"); } } } //The only way to get here is if a's size is -1 throw new System.SystemException("Impossible state"); }
//return the tree reference that corresponds to this tree element public TreeReference getRef() { TreeElement elem = this; TreeReference ref_ = TreeReference.selfRef(); while (elem != null) { TreeReference step; if (elem.name != null) { step = TreeReference.selfRef(); step.add(elem.name, elem.multiplicity); } else { step = TreeReference.rootRef(); } ref_ = ref_.parent(step); elem = elem.parent; } return(ref_); }