/// <summary> Look up a reference by kernel. /// Use a binary search on the ordered list of known references. /// Since the binary search returns the position at which a new item should /// be inserted, we check the references earlier in the list if there is /// a failure. /// </summary> /// <param name="key">A character reference with the kernel set to the string /// to be found. It need not be truncated at the exact end of the reference. /// </param> protected internal static CharacterReference LookUp(CharacterReference key) { System.String string_Renamed; int index; System.String kernel; char character; CharacterReference test; CharacterReference ret; // Care should be taken here because some entity references are // prefixes of others, i.e.: // \u2209[notin] \u00ac[not] // \u00ba[ordm] \u2228[or] // \u03d6[piv] \u03c0[pi] // \u00b3[sup3] \u2283[sup] ret = null; index = SortImpl.Bsearch(mCharacterReferences, key); string_Renamed = key.Kernel; if (index < mCharacterReferences.Length) { ret = mCharacterReferences[index]; kernel = ret.Kernel; //if (!(String.Compare(string_Renamed, 0, kernel, 0, kernel.Length) == 0)) if (!(PlatformTools.CompareStr(string_Renamed, 0, kernel, 0, kernel.Length) == 0)) { // not exact, check references starting with same character // to see if a subset matches ret = null; } } if (null == ret) { character = string_Renamed[0]; while (--index >= 0) { test = mCharacterReferences[index]; kernel = test.Kernel; if (character == kernel[0]) { //if (String.Compare(string_Renamed, 0, kernel, 0, kernel.Length) == 0) if (PlatformTools.CompareStr(string_Renamed, 0, kernel, 0, kernel.Length) == 0) { ret = test; break; } } else { break; } } } return(ret); }
/// <summary> Remove an element from the list</summary> /// <param name="cursor">The element to remove. /// </param> public virtual void Remove(Cursor cursor) { int i; // find it i = SortImpl.Bsearch(this, cursor); // remove if ((i < Size()) && (cursor.Position == mIndices[i])) { RemoveElementAt(i); } }
/// <summary> Get the line number for a cursor.</summary> /// <param name="cursor">The character offset into the page. /// </param> /// <returns> The line number the character is in. /// </returns> public virtual int Row(Cursor cursor) { int ret; ret = SortImpl.Bsearch(this, cursor); // handle line transition, the search returns the index if it matches // exactly one of the line end positions, so we advance one line if // it's equal to the offset at the row index, since that position is // actually the beginning of the next line if ((ret < mCount) && (cursor.Position == mIndices[ret])) { ret++; } return(ret); }
/// <summary> Add an element to the list</summary> /// <param name="cursor">The element to add. /// </param> /// <returns> The position at which the element was inserted or /// the index of the existing element if it is a duplicate. /// </returns> public virtual int Add(Cursor cursor) { int position; int last; int ret; position = cursor.Position; if (0 == mCount) { ret = 0; InsertElementAt(position, ret); } else { last = mIndices[mCount - 1]; if (position == last) { ret = mCount - 1; } else if (position > last) { ret = mCount; InsertElementAt(position, ret); } else { // find where it goes ret = SortImpl.Bsearch(this, cursor); // insert, but not twice if (!((ret < Size()) && (position == mIndices[ret]))) { InsertElementAt(position, ret); } } } return(ret); }
/// <summary> Binary search for the element.</summary> /// <param name="cursor">The element to search for. /// </param> /// <param name="first">The index to start at. /// </param> /// <param name="last">The index to stop at. /// </param> /// <returns> The index at which the element was found or is to be inserted. /// </returns> protected internal virtual int Bsearch(int cursor, int first, int last) { return(SortImpl.Bsearch(this, new Cursor(Page, cursor), first, last)); }
/// <summary> Binary search for the element.</summary> /// <param name="cursor">The element to search for. /// </param> /// <returns> The index at which the element was found or is to be inserted. /// </returns> protected internal virtual int Bsearch(int cursor) { return(SortImpl.Bsearch(this, new Cursor(Page, cursor))); }