public bool Item(int Index, out T result) { var rv = this; while ((rv._TP.Begin < Index) && (Index <= rv._TP.End)) { TextPortionTree <T> newrv = rv._Children.FirstOrDefault(tpt => (tpt._TP.Begin <= Index) && (Index <= tpt._TP.End)); if (newrv == null) { break; } rv = newrv; } if ((rv._TP.Begin <= Index) && (Index <= rv._TP.End)) { result = rv._Tag; } else { result = default(T); } return((rv._TP.Begin <= Index) && (Index <= rv._TP.End)); }
private bool Add(TextPortionTree <T> Item) { TextPortion tp = Item._TP; if (_TP.IsInit && !_TP.PortionInPortion(tp)) { return(false); } for (int i = 0; i < _Children.Count; i++) { var ch = _Children[i]; var tpt = ch._TP; if (tp.End <= tpt.Begin) //New is before ch { _Children.Insert(i, Item); return(true); } else if (tp.Begin >= tpt.End) //New is after ch { //Nothing to do; just for clarity (so to not include in the next else-ifs) } else if (tpt.PortionInPortion(tp)) { //New is in ch return(ch.Add(Item)); } else if (tp.PortionInPortion(tpt)) //New contains ch (and maybe others { int j; for (j = i + 1; j < _Children.Count; j++) { if (_Children[j]._TP.Begin >= tp.End) { break; } } Item._Children.AddRange(_Children.GetRange(i, j - i)); _Children.RemoveRange(i, j - i); _Children.Insert(i, Item); return(true); } else if ((tp.Begin < tpt.Begin) == (tp.End < tpt.End)) { //They interleave return(false); } } //New is after the end of the list _Children.Add(Item); return(true); }
public bool Add(TextPortion newItem, T newTag) { TextPortionTree <T> ch = new TextPortionTree <T>(newItem, newTag); ch._Tag = newTag; _TP = new TextPortion(); if (!Add(ch)) { return(false); } UpdateTP(); return(true); }