Warning: session_start(): open(/var/lib/php/sessions/sess_bhc46i664ergrdaif9ckfk3v4q, 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
sclass AdaptiveIdentifierCompression {
new SS shortenings;
new MultiSet tokenCount;
new SS expansions;
S escapeWord = "xx";
S codeAlphabet = lowerCaseAlphabet();
ItIt newCodeMaker;
S nextCode;
int conflicts;
settable int minCountToCompress = 1;
void init {
if (newCodeMaker == null) {
newCodeMaker = allWordsOfAlphabet(codeAlphabet);
newCode();
}
}
S encode(S token) {
if (!isIdentifier(token)) ret token;
init();
ret compressIdentifier(token);
}
S compressIdentifier(S token) {
tokenCount.add(token);
S code = shortenings.get(token);
if (code != null)
ret code;
int count = tokenCount.get(token);
if (count < minCountToCompress)
ret token;
if (eq(token, nextCode)) {
newCode();
ret escapeWord + " " + token;
}
// check if token clashes with a code we creatd
S existingMeaning = expansions.get(token);
if (existingMeaning != null) {
printVars(+token, +existingMeaning);
// It's not a problem - we just send the escape word
// and the token will get a new code.
ret escapeWord + " " + createCodeFor(token);
fail();
}
ret createCodeFor(token);
}
S createCodeFor(S token) {
// create code for token
code = nextCode;
if (code != null) {
newCode();
S conflict = expansions.get(code);
if (conflict != null) {
++conflicts;
fail("conflict: " + token + " / " + code);
// TODO
}
shortenings.put(token, code);
expansions.put(code, token);
// first time, so return original token
ret token;
}
// out of codes (unlikely), return as is
ret token;
}
S newCode() {
do {
if (!newCodeMaker.hasNext())
null;
nextCode = newCodeMaker.next();
} while (shortenings.containsKey(nextCode) && !eq(nextCode, escape));
ret nextCode;
}
/// decoder
bool escape;
S decode(S token) {
if (!isIdentifier(token)) ret escape ? "" : token;
init();
if (escape) {
escape = false;
ret decoded(token);
}
if (eq(token, escapeWord)) {
set escape;
ret "";
}
S expanded = expansions.get(token);
if (expanded != null)
ret decoded(expanded);
ret decoded(token);
}
S decoded(S token) {
compressIdentifier(token);
ret token;
}
}