Warning: session_start(): open(/var/lib/php/sessions/sess_cab5onvg0ok8q0gk4lml78g79f, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
// A = type of value in nodes.
// Can be case-sensitive or case-insensitive
// values can't be null
sclass StringTrie {
A value;
TreeMap> children;
*() {}
*(A *value) {}
void makeCaseInsensitive() {
children = asCIMap(children);
}
bool isCaseInsensitive() {
ret isCIMap(children);
}
void add(S string, A value) {
// case 1: empty string
if (empty(string)) ret with this.value = value;
bool ci = isCaseInsensitive();
// case 2: some prefix of new string exists
S existingPrefix = ci
? longestPrefixInCISet(string, navigableKeys(children))
: longestPrefixInNavigableSet(string, navigableKeys(children));
if (nempty(existingPrefix)) {
StringTrie child = children.get(existingPrefix);
S substring = substring(string, l(existingPrefix));
// case 2a: new string is complete suffix of existing child
if (child != null)
ret with child.add(substring, value);
// case 2b: make new branch
StringTrie branch = newChild(null);
if (branch.children == null) branch.children = new TreeMap;
S fullEntry = higherEntry(children, existingPrefix).getValue();
children.put(existingPrefix, branch);
branch.children.put(substring(fullEntry, l(existingPrefix)), child);
branch.children.put(substring, newChild(value));
ret;
}
// case 3: no existing prefix, just add new child
if (children == null) children = new TreeMap;
children.put(string, newChild(value));
}
// returns TreeMap or ciMap
Map asMap() {
Map map = isCaseInsensitive() ? ciMap() : new TreeMap;
new StringBuilder buf;
collectMap(buf, map);
ret map;
}
private void collectMap(StringBuilder buf, Map map) {
if (value != null) map.put(str(buf), value);
int n = l(buf);
for (S string, StringTrie child : children) {
buf.append(string);
child.collectMap(buf, map);
buf.setLength(n);
}
}
toString {
new StringBuilder buf;
if (value != null) {
buf.append("\"\" => ").append(value).append("\n");
childrenToString(buf, 2);
} else
childrenToString(buf, 0);
ret str(buf);
}
void toString(StringBuilder buf, int indent) {
if (value != null)
buf.append(" => ").append(value);
buf.append("\n");
childrenToString(buf, indent+2);
}
void childrenToString(StringBuilder buf, int indent) {
for (S string, StringTrie child : children) {
buf.append(spaces(indent)).append(quote(string));
child.toString(buf, indent);
}
}
private StringTrie newChild(A value) {
StringTrie child = new(value);
if (isCaseInsensitive()) child.makeCaseInsensitive();
ret child;
}
Set childStrings() { ret keys(children); }
StringTrie getChild(S string) { ret children.get(string); }
}