~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/NRefactory/Project/Src/Lexer/LookupTable.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// <file>
2
 
//     <copyright see="prj:///doc/copyright.txt"/>
3
 
//     <license see="prj:///doc/license.txt"/>
4
 
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
5
 
//     <version>$Revision: 4482 $</version>
6
 
// </file>
7
 
 
8
 
using System;
9
 
using System.Globalization;
10
 
 
11
 
namespace ICSharpCode.OldNRefactory.Parser
12
 
{
13
 
        /// <summary>
14
 
        /// This class implements a keyword map. It implements a digital search trees (tries) to find
15
 
        /// a word.
16
 
        /// </summary>
17
 
        internal class LookupTable
18
 
        {
19
 
                Node root = new Node(-1, null);
20
 
                bool casesensitive;
21
 
                int  length;
22
 
                
23
 
                /// <value>
24
 
                /// The number of elements in the table
25
 
                /// </value>
26
 
                public int Count {
27
 
                        get {
28
 
                                return length;
29
 
                        }
30
 
                }
31
 
                
32
 
                /// <summary>
33
 
                /// Inserts an int in the tree, under keyword
34
 
                /// </summary>
35
 
                public int this[string keyword] {
36
 
                        get {
37
 
                                Node next = root;
38
 
                                
39
 
                                if (!casesensitive) {
40
 
                                        keyword = keyword.ToUpper(CultureInfo.InvariantCulture);
41
 
                                }
42
 
                                
43
 
                                for (int i = 0; i < keyword.Length; ++i) {
44
 
                                        int index = ((int)keyword[i]) % 256;
45
 
                                        next = next.leaf[index];
46
 
                                        
47
 
                                        if (next == null) {
48
 
                                                return -1;
49
 
                                        }
50
 
                                        
51
 
                                        if (keyword == next.word) {
52
 
                                                return next.val;
53
 
                                        }
54
 
                                }
55
 
                                return -1;
56
 
                        }
57
 
                        set {
58
 
                                Node node = root;
59
 
                                Node next = root;
60
 
                                
61
 
                                if (!casesensitive) {
62
 
                                        keyword = keyword.ToUpper(CultureInfo.InvariantCulture);
63
 
                                }
64
 
                                
65
 
                                ++length;
66
 
                                
67
 
                                // insert word into the tree
68
 
                                for (int i = 0; i < keyword.Length; ++i) {
69
 
                                        int index = ((int)keyword[i]) % 256; // index of curchar
70
 
                                        bool d = keyword[i] == '\\';
71
 
                                        
72
 
                                        next = next.leaf[index];             // get node to this index
73
 
                                        
74
 
                                        if (next == null) { // no node created -> insert word here
75
 
                                                node.leaf[index] = new Node(value, keyword);
76
 
                                                break;
77
 
                                        }
78
 
                                        
79
 
                                        if (next.word != null && next.word.Length != i) { // node there, take node content and insert them again
80
 
                                                string tmpword  = next.word;                  // this word will be inserted 1 level deeper (better, don't need too much 
81
 
                                                int    tmpval = next.val;                 // string comparisons for finding.)
82
 
                                                next.val = -1;
83
 
                                                next.word = null;
84
 
                                                this[tmpword] = tmpval;
85
 
                                        }
86
 
                                        
87
 
                                        if (i == keyword.Length - 1) { // end of keyword reached, insert node there, if a node was here it was
88
 
                                                next.word = keyword;       // reinserted, if it has the same length (keyword EQUALS this word) it will be overwritten
89
 
                                                next.val = value;
90
 
                                                break;
91
 
                                        }
92
 
                                        
93
 
                                        node = next;
94
 
                                }
95
 
                        }
96
 
                }
97
 
                
98
 
                /// <summary>
99
 
                /// Creates a new instance of <see cref="LookupTable"/>
100
 
                /// </summary>
101
 
                public LookupTable(bool casesensitive)
102
 
                {
103
 
                        this.casesensitive = casesensitive;
104
 
                }
105
 
                
106
 
                class Node
107
 
                {
108
 
                        public Node(int val, string word)
109
 
                        {
110
 
                                this.word  = word;
111
 
                                this.val   = val;
112
 
                        }
113
 
                        
114
 
                        public string word;
115
 
                        public int    val;
116
 
                        
117
 
                        public Node[] leaf = new Node[256];
118
 
                }
119
 
        }
120
 
}