public TemplateBasedCompressor(short tid, byte[] tmplt, bool huff) { log = LogManager.GetLogger(GetType()); templateId = tid; template = tmplt; tc = new TrieCompressor(templateId, template); tc.DictionaryShortcutChanged += HandleNewAnnouncement; hc = new HuffmanCompressor(templateId); hc.HuffmanFrequenciesChanged += HandleUpdatedFrequencies; hc.HuffmanEncoding = huff; }
internal void CheckSameState(TrieCompressor other) { InvalidStateException.Assert(decodingTable.Count == other.decodingTable.Count, "decoding tables have different lengths", this); for (int i = 0; i < decodingTable.Count; i++) { InvalidStateException.Assert(ByteUtils.Compare(decodingTable[i], other.decodingTable[i]), "different decoding value for " + i, this); } InvalidStateException.Assert(shortcuts.Count == other.shortcuts.Count, "shortcut dictionaries have different counts", this); InvalidStateException.Assert(inverseShortcuts.Length == other.inverseShortcuts.Length, "inverse shortcut lengths have different counts", this); InvalidStateException.Assert(inverseShortcuts[0] == 0 && inverseShortcuts[1] == 0, "bytes 0 and 1 are reserved!", this); for (int i = MinimumShortcut; i <= MaximumShortcut; i++) { InvalidStateException.Assert(shortcuts.ContainsValue((byte)i) == other.shortcuts.ContainsValue((byte)i), "difference in byte mapping", this); InvalidStateException.Assert(inverseShortcuts[i] == other.inverseShortcuts[i], "difference in byte mapping", this); if (shortcuts.ContainsValue((byte)i)) { InvalidStateException.Assert(shortcuts[inverseShortcuts[i]] == i, "Mismatch between instance's shortcuts and inverseShortcuts!", this); InvalidStateException.Assert(inverseShortcuts[i] == other.inverseShortcuts[i], "Mismatch between instances' inverseShortcuts!", this); InvalidStateException.Assert(shortcuts[inverseShortcuts[i]] == other.shortcuts[other.inverseShortcuts[i]], "Mismatch between instances' inverseShortcuts!", this); } } }
/// <summary> /// Check if the provided candidate would make a good template /// FIXME: Why are we throwing the template rep away? /// </summary> /// <param name="templateCandidate">the template</param> /// <returns></returns> public bool ShouldGenerateNewTemplate(byte[] templateCandidate) { TrieCompressor tc = new TrieCompressor(-1, templateCandidate); int candidateSize = 0; int currentSize = 0; foreach(byte[] message in savedMessages) { candidateSize += tc.EncodedSize(message); // This assumes the trie encoding approximates the overall encoding... currentSize += FindBestTrieEncoding(message); } return candidateSize < currentSize; }