-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alphabet.java
49 lines (41 loc) · 1.24 KB
/
Alphabet.java
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
package com.sleepycoders.jlib.datastructure.trie;
/**
* @author Joshua Moody ([email protected])
*/
public enum Alphabet {
LETTERS("abcdefghijklmnopqrstuvwxyz"),
NUMBERS("0123456789"),
BINARY("01"),
NONCONSECUTIVE("a1b2c3") {
@Override
public byte getIndex(char c) {
return (byte) alphabet.indexOf(c);
}
};
public final String alphabet;
Alphabet(String validChars) {
this.alphabet = validChars;
consecutiveBase = Character.getNumericValue(validChars.charAt(0));
}
private final int consecutiveBase;
private byte getConsecutiveIndex(char c) {
return (byte) (Character.getNumericValue(c) - consecutiveBase);
}
/**
* the default index function is Case Insensitive for A-Z,a-z
* So for letters that are a different case it will still generate a valid index
*/
public byte getIndex(char c) {
return getConsecutiveIndex(c);
}
public int size() {
return alphabet.length();
}
public byte[] toInt(String word) {
byte[] indexes = new byte[word.length()];
for (int i = 0; i < indexes.length; i++) {
indexes[i] = getIndex(word.charAt(i));
}
return indexes;
}
}