| 1: | // LookupTable.cs | |
| 2: | // Copyright (c) 2000 Mike Krueger | |
| 3: | // | |
| 4: | // This program is free software; you can redistribute it and/or modify | |
| 5: | // it under the terms of the GNU General Public License as published by | |
| 6: | // the Free Software Foundation; either version 2 of the License, or | |
| 7: | // (at your option) any later version. | |
| 8: | // | |
| 9: | // This program is distributed in the hope that it will be useful, | |
| 10: | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11: | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12: | // GNU General Public License for more details. | |
| 13: | // | |
| 14: | // You should have received a copy of the GNU General Public License | |
| 15: | // along with this program; if not, write to the Free Software | |
| 16: | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 17: | ||
| 18: | using System; | |
| 19: | using System.Collections; | |
| 20: | using System.Drawing; | |
| 21: | using SharpDevelop.DefaultEditor.Text; | |
| 22: | ||
| 23: | namespace SharpDevelop.DefaultEditor.Util { | |
| 24: | ||
| 25: | /// <summary> | |
| 26: | /// This class implements a keyword map. It implements a digital search trees (tries) to find | |
| 27: | /// a word. | |
| 28: | /// </summary> | |
| 29: | public class LookupTable | |
| 30: | { | |
| 31: | Node root = new Node(null, null); | |
| 32: | bool casesensitive; | |
| 33: | int length; | |
| 34: | ||
| 35: | public int Length { | |
| 36: | get { | |
| 37: | return length; | |
| 38: | } | |
| 39: | } | |
| 40: | ||
| 41: | /// <summary> | |
| 42: | /// Get the object, which was inserted under the keyword (line, at offset, with length length), | |
| 43: | /// returns null, if no such keyword was inserted. | |
| 44: | /// </summary> | |
| 45: | public object this[IDocument document, LineSegment line, int offset, int length] { | |
| 46: | get { | |
| 47: | if(length == 0) { | |
| 48: | return null; | |
| 49: | } | |
| 50: | Node next = root; | |
| 51: | ||
| 52: | int wordOffset = line.Offset + offset; | |
| 53: | if (casesensitive) { | |
| 54: | for (int i = 0; i < length; ++i) { | |
| 55: | int index = ((int)document.GetCharAt(wordOffset + i)) % 256; | |
| 56: | next = next.leaf[index]; | |
| 57: | ||
| 58: | if (next == null) { | |
| 59: | return null; | |
| 60: | } | |
| 61: | ||
| 62: | if (next.color != null && SharpDevelop.DefaultEditor.Util.TextUtility.RegionMatches(document, wordOffset, length, next.word)) { | |
| 63: | return next.color; | |
| 64: | } | |
| 65: | } | |
| 66: | } else { | |
| 67: | for (int i = 0; i < length; ++i) { | |
| 68: | int index = ((int)Char.ToUpper(document.GetCharAt(wordOffset + i))) % 256; | |
| 69: | ||
| 70: | next = next.leaf[index]; | |
| 71: | ||
| 72: | if (next == null) { | |
| 73: | return null; | |
| 74: | } | |
| 75: | ||
| 76: | if (next.color != null && SharpDevelop.DefaultEditor.Util.TextUtility.RegionMatches(document, casesensitive, wordOffset, length, next.word)) { | |
| 77: | return next.color; | |
| 78: | } | |
| 79: | } | |
| 80: | } | |
| 81: | return null; | |
| 82: | } | |
| 83: | } | |
| 84: | ||
| 85: | /// <summary> | |
| 86: | /// Inserts an object in the tree, under keyword | |
| 87: | /// </summary> | |
| 88: | public object this[string keyword] { | |
| 89: | set { | |
| 90: | Node node = root; | |
| 91: | Node next = root; | |
| 92: | if (!casesensitive) { | |
| 93: | keyword = keyword.ToUpper(); | |
| 94: | } | |
| 95: | ++length; | |
| 96: | ||
| 97: | // insert word into the tree | |
| 98: | for (int i = 0; i < keyword.Length; ++i) { | |
| 99: | int index = ((int)keyword[i]) % 256; // index of curchar | |
| 100: | bool d = keyword[i] == '\\'; | |
| 101: | ||
| 102: | next = next.leaf[index]; // get node to this index | |
| 103: | ||
| 104: | if (next == null) { // no node created -> insert word here | |
| 105: | node.leaf[index] = new Node(value, keyword); | |
| 106: | break; | |
| 107: | } | |
| 108: | ||
| 109: | if (next.word != null && next.word.Length != i) { // node there, take node content and insert them again | |
| 110: | string tmpword = next.word; // this word will be inserted 1 level deeper (better, don't need too much | |
| 111: | object tmpcolor = next.color; // string comparisons for finding.) | |
| 112: | next.color = next.word = null; | |
| 113: | this[tmpword] = tmpcolor; | |
| 114: | } | |
| 115: | ||
| 116: | if (i == keyword.Length - 1) { // end of keyword reached, insert node there, if a node was here it was | |
| 117: | next.word = keyword; // reinserted, if it has the same length (keyword EQUALS this word) it will be overwritten | |
| 118: | next.color = value; | |
| 119: | break; | |
| 120: | } | |
| 121: | ||
| 122: | node = next; | |
| 123: | } | |
| 124: | } | |
| 125: | } | |
| 126: | ||
| 127: | public LookupTable(bool casesensitive) | |
| 128: | { | |
| 129: | this.casesensitive = casesensitive; | |
| 130: | } | |
| 131: | ||
| 132: | class Node | |
| 133: | { | |
| 134: | public Node(object color, string word) | |
| 135: | { | |
| 136: | this.word = word; | |
| 137: | this.color = color; | |
| 138: | } | |
| 139: | ||
| 140: | public string word; | |
| 141: | public object color; | |
| 142: | ||
| 143: | public Node[] leaf = new Node[256]; | |
| 144: | } | |
| 145: | } | |
| 146: | } |
This page was automatically generated by SharpDevelop.