Пример #1
0
 public RouteMap Deserialize(Stream stream)
 {
     using (var reader = new BinaryReader(stream))
     {
         var guid     = new Guid(reader.ReadBytes(16));
         int strings  = reader.ReadInt32();
         int vertices = reader.ReadInt32();
         int edges    = reader.ReadInt32();
         var result   = new RouteMap()
         {
             Guid = guid.ToString()
         };
         string[] stringTable = new string[strings];
         for (int i = 0; i < strings; i++)
         {
             stringTable[i] = ReadString(reader);
         }
         for (int v = 0; v < vertices; v++)
         {
             result.Vertices.Add(DeserializeVertex(reader, stringTable, v));
         }
         for (int e = 0; e < edges; e++)
         {
             result.Edges.Add(DeserializeEdge(reader, stringTable, e));
         }
         result.UpdateVertices();
         return(result);
     }
 }
Пример #2
0
        public void Serialize(RouteMap routeMap, Stream stream)
        {
            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                writer.Write(Guid.NewGuid().ToByteArray());
                var strings = new SortedSet <string>();
                strings.Add("");
                FillStringTable(routeMap.Vertices.Select(v => v.Name), strings);
                FillStringTable(routeMap.Edges.Select(v => v.Name), strings);

                writer.Write(strings.Count);
                writer.Write(routeMap.Vertices.Count);
                writer.Write(routeMap.Edges.Count);

                var stringTable = new Dictionary <string, int>();
                int index       = 0;

                foreach (var str in strings)
                {
                    stringTable.Add(str, index++);
                    if (string.IsNullOrEmpty(str))
                    {
                        writer.Write((byte)0);
                    }
                    else
                    {
                        var bytes = Encoding.UTF8.GetBytes(str);
                        writer.Write((byte)bytes.Length);
                        writer.Write(bytes);
                    }
                }
                foreach (var v in routeMap.Vertices)
                {
                    Serialize(v, writer, stringTable);
                }
                foreach (var e in routeMap.Edges)
                {
                    Serialize(e, writer, stringTable);
                }
            }
        }