private ulong GetFrequency(ulong linkIndex, ulong source, ulong target) { var frequency = (_linkFrequenciesCache.GetFrequency(source, target) ?? new LinkFrequency <ulong>(0, 0)).Frequency; if (frequency == default && linkIndex > 0) { frequency = _totalSequenceSymbolFrequencyCounter.Count(linkIndex); } return(frequency); }
private IList <TLink> Compress(IList <TLink> sequence) { if (sequence.IsNullOrEmpty()) { return(null); } if (sequence.Count == 1) { return(sequence); } if (sequence.Count == 2) { return(new[] { _links.GetOrCreate(sequence[0], sequence[1]) }); } // TODO: arraypool with min size (to improve cache locality) or stackallow with Sigil var copy = new HalfDoublet[sequence.Count]; Doublet <TLink> doublet = default; for (var i = 1; i < sequence.Count; i++) { doublet = new Doublet <TLink>(sequence[i - 1], sequence[i]); LinkFrequency <TLink> data; if (_doInitialFrequenciesIncrement) { data = _doubletFrequenciesCache.IncrementFrequency(ref doublet); } else { data = _doubletFrequenciesCache.GetFrequency(ref doublet); if (data == null) { throw new NotSupportedException("If you ask not to increment frequencies, it is expected that all frequencies for the sequence are prepared."); } } copy[i - 1].Element = sequence[i - 1]; copy[i - 1].DoubletData = data; UpdateMaxDoublet(ref doublet, data); } copy[sequence.Count - 1].Element = sequence[sequence.Count - 1]; copy[sequence.Count - 1].DoubletData = new LinkFrequency <TLink>(); if (_comparer.Compare(_maxDoubletData.Frequency, default) > 0) { var newLength = ReplaceDoublets(copy); sequence = new TLink[newLength]; for (int i = 0; i < newLength; i++) { sequence[i] = copy[i].Element; } } return(sequence); }