/// <summary> /// An exception thrown when at least one dimension /// of the bintree is zero. /// </summary> /// <param name="Bintree">The bintree causing this exception.</param> /// <param name="Message">The message that describes the error.</param> /// <param name="InnerException">The exception that is the cause of the current exception.</param> public BT_ZeroDimensionException(Bintree <T> Bintree, String Message = null, Exception InnerException = null) : base(Bintree, Message: "The given bintree has at least one zero dimension!" + Message, InnerException: InnerException) { }
/// <summary> /// An exception thrown when the given element is /// located outside of the bintree bounds! /// </summary> /// <param name="Bintree">The bintree causing this exception.</param> /// <param name="Element">The element causing this exception.</param> /// <param name="Message">The message that describes the error.</param> /// <param name="InnerException">The exception that is the cause of the current exception.</param> public BT_OutOfBoundsException(Bintree <T> Bintree, T Element, String Message = null, Exception InnerException = null) : base(Bintree, Element, "The given element is located outside of the bintree bounds!" + Message, InnerException) { }
/// <summary> /// A general bintree exception occurred! /// </summary> /// <param name="Bintree">The bintree causing this exception.</param> /// <param name="Element">An optional element causing this exception.</param> /// <param name="Message">The message that describes the error.</param> /// <param name="InnerException">The exception that is the cause of the current exception.</param> public BintreeException(Bintree <T> Bintree, T Element = default(T), String Message = null, Exception InnerException = null) : base(Message, InnerException) { this.Bintree = Bintree; this.Element = Element; }
/// <summary> /// Add an element to the bintree. /// </summary> /// <param name="Element">A element of type T.</param> public void Add(T Element) { if (this.Contains(Element)) { #region Check subtrees... if (Subtree1 != null && Subtree1.Contains(Element)) { Subtree1.Add(Element); } else if (Subtree2 != null && Subtree2.Contains(Element)) { Subtree2.Add(Element); } #endregion #region ...or the embedded data. else if (EmbeddedData.Count < MaxNumberOfEmbeddedElements) { EmbeddedData.Add(Element); } #endregion #region If necessary create subtrees... else { #region Create Subtrees if (Subtree1 == null) { Subtree1 = new Bintree <T>(Left, Math.Add(Left, Math.Div2(Length)), MaxNumberOfEmbeddedElements); Subtree1.OnTreeSplit += OnTreeSplit; } if (Subtree2 == null) { Subtree2 = new Bintree <T>(Math.Add(Left, Math.Div2(Length)), Right, MaxNumberOfEmbeddedElements); Subtree2.OnTreeSplit += OnTreeSplit; } #endregion #region Fire BintreeSplit event if (OnTreeSplit != null) { OnTreeSplit(this, Element); } #endregion #region Move all embedded data into the subtrees EmbeddedData.Add(Element); foreach (var _Element in EmbeddedData) { if (Subtree1.Contains(_Element)) { Subtree1.Add(_Element); } else if (Subtree2.Contains(_Element)) { Subtree2.Add(_Element); } else { throw new Exception("Mist!"); } } EmbeddedData.Clear(); #endregion } #endregion } else { throw new BT_OutOfBoundsException <T>(this, Element); } }