public static int Write(BiDictionary <HmmOnlineNode, int> map, HashSet <HmmOnlineNode> written, HmmOnlineNode node, BinaryWriter output) { if (node == null) { output.Write(false); output.Write(-1); return(-1); } int index = map[node]; if (!written.Contains(node)) { output.Write(true); output.Write(index); output.Write(node.Position); output.Write(node.State); output.Write(node.ChildNumber); written.Add(node); Write(map, written, node.Parent, output); } else { output.Write(false); output.Write(index); } return(index); }
public void ReadFields(BinaryReader input) { var probsLength = input.ReadInt32(); _probs = new double[probsLength]; for (int i = 0; i < probsLength; i++) { _probs[i] = input.ReadDouble(); } _backpointers = new List <int[]>(); int backpointersSize = input.ReadInt32(); int stateNumber = input.ReadInt32(); for (int i = 0; i < backpointersSize; ++i) { int[] optimalStates = new int[stateNumber]; for (int j = 0; j < optimalStates.Length; ++j) { optimalStates[j] = input.ReadInt32(); } _backpointers.Add(optimalStates); } Position = input.ReadInt32(); LastLikelihood = input.ReadDouble(); int mapSize = input.ReadInt32(); var nodeMap = new BiDictionary <HmmOnlineNode, int>(); // HashBiMap.create(mapSize); for (int i = 0; i < mapSize; ++i) { HmmOnlineNode.Read(nodeMap, input); } Tree = new HmmOnlineTree(); int treeSize = input.ReadInt32(); for (int i = 0; i < treeSize; ++i) { Tree.AddLast(HmmOnlineNode.Read(nodeMap, input)); } Root = HmmOnlineNode.Read(nodeMap, input); _leaves = new HmmOnlineNode[stateNumber]; for (int i = 0; i < _leaves.Length; ++i) { _leaves[i] = HmmOnlineNode.Read(nodeMap, input); } }
static int MapNode(BiDictionary <HmmOnlineNode, int> map, HmmOnlineNode node) { if (node == null) { return(-1); } int value; if (!map.TryGetValue(node, out value)) { value = map.Count; map.Add(node, value); return(value); } return(value); }
public static HmmOnlineNode Read(BiDictionary <HmmOnlineNode, int> map, BinaryReader input) { bool first = input.ReadBoolean(); int index = input.ReadInt32(); if (first) { HmmOnlineNode node = new HmmOnlineNode(); node.Position = input.ReadInt32(); node.State = input.ReadInt32(); node.ChildNumber = input.ReadInt32(); map.Add(node, index); node.Parent = Read(map, input); return(node); } if (index == -1) { return(null); } return(map.Reverse[index]); }
public ReverseDictionary(BiDictionary <TFirst, TSecond> owner) { _owner = owner; }
public void Write(BinaryWriter output) { output.Write(_probs.Length); foreach (var p in _probs) { output.Write(p); } output.Write(_backpointers.Count); output.Write(_leaves.Length); foreach (var optimalStates in _backpointers) { foreach (var state in optimalStates) { output.Write(state); } } output.Write(Position); output.Write(LastLikelihood); //serializing compressed backpointers tree //3 * |H| - 2 is the upper bound of node number var nodeMap = new BiDictionary <HmmOnlineNode, int>(); // 3 * leaves.length - 2; var node = Tree.First; while (node != null) { MapNode(nodeMap, node); node = node.Next; } MapNode(nodeMap, Root); foreach (var leave in _leaves) { MapNode(nodeMap, leave); } var alreadyWritten = new HashSet <HmmOnlineNode>(); output.Write(nodeMap.Count); foreach (var entry in nodeMap.Keys) { HmmOnlineNode.Write(nodeMap, alreadyWritten, entry, output); } output.Write(Tree.Size); node = Tree.First; while (node != null) { HmmOnlineNode.Write(nodeMap, alreadyWritten, node, output); node = node.Next; } HmmOnlineNode.Write(nodeMap, alreadyWritten, Root, output); foreach (var leave in _leaves) { HmmOnlineNode.Write(nodeMap, alreadyWritten, leave, output); } }