-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRLangKeywordMap.java
More file actions
95 lines (81 loc) · 2.25 KB
/
Copy pathRLangKeywordMap.java
File metadata and controls
95 lines (81 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package rprocessing.mode;
import javax.swing.text.Segment;
import processing.app.syntax.KeywordMap;
import processing.app.syntax.Token;
public class RLangKeywordMap extends KeywordMap {
private final Keyword[] map;
/**
* Creates a new <code>KeywordMap</code>.
*/
public RLangKeywordMap() {
this(52);
}
/**
* Creates a new <code>KeywordMap</code>.
*
* @param mapLength The number of `buckets' to create. A value of 52 will give good performance
* for most maps.
*/
public RLangKeywordMap(final int mapLength) {
super(true);
this.mapLength = mapLength;
map = new Keyword[mapLength];
}
/**
* Looks up a key.
*
* @param text The text segment
* @param offset The offset of the substring within the text segment
* @param length The length of the substring
*/
public byte lookup(final Segment text, final int offset, final int length) {
if (length == 0) {
return Token.NULL;
}
Keyword k = map[getSegmentMapKey(text, offset, length)];
while (k != null) {
if (length != k.keyword.length) {
k = k.next;
continue;
}
if (SyntaxUtilities.regionMatches(text, offset, k.keyword)) {
return k.id;
}
k = k.next;
}
return Token.NULL;
}
/**
* Adds a key-value mapping.
*
* @param keyword The key
* @Param id The value
*/
public void add(final String keyword, final byte id) {
final int key = getStringMapKey(keyword);
map[key] = new Keyword(keyword.toCharArray(), id, map[key]);
}
// protected members
protected int mapLength;
@Override
protected int getStringMapKey(final String s) {
return (Character.toUpperCase(s.charAt(0)) + Character.toUpperCase(s.charAt(s.length() - 1)))
% mapLength;
}
@Override
protected int getSegmentMapKey(final Segment s, final int off, final int len) {
return (Character.toUpperCase(s.array[off]) + Character.toUpperCase(s.array[off + len - 1]))
% mapLength;
}
// private members
class Keyword {
public char[] keyword;
public byte id;
public Keyword next;
public Keyword(final char[] keyword, final byte id, final Keyword next) {
this.keyword = keyword;
this.id = id;
this.next = next;
}
}
}