1. A computer implemented method for encoding data, the method comprising: in response to a symbol retrieved from an input stream having a sequence of symbols, determining whether a current context can encode the retrieved symbol; emitting a code representing an escape token to a code stream if the current context cannot encode the retrieved symbol; escaping from the current context to a preceding context of the current context if the current context is not a root context; emitting the retrieved symbol to a literal stream if the current context is a root context, wherein the code stream and the literal stream are to be compressed and encoded by a compressor; emitting a code representing the retrieved symbol to the code stream if the current context can encode the retrieved symbol, wherein the code representing the retrieved symbol is generated based on an index associated with the retrieved symbol stored within the current context; and sorting at least a portion of the current context based on the emitted code and/or the emitted symbol.