#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Globalization; using System.Text; namespace HeuristicLab.Services.ProblemInstances { public static class PrimitiveSerializer { public static string SerializeDoubleMatrix(double[][] matrix) { StringBuilder builder = new StringBuilder(); int dim1 = matrix.Length; int dim2 = matrix[0].Length; builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat)); builder.Append(";"); builder.Append(dim2.ToString(CultureInfo.InvariantCulture.NumberFormat)); for (int i = 0; i < dim1; i++) { for (int j = 0; j < dim2; j++) { if (matrix[i].Length != dim2) throw new ArgumentException("ERROR jagged array is not a matrix!"); builder.Append(";" + matrix[i][j].ToString(CultureInfo.InvariantCulture.NumberFormat)); } } return builder.ToString(); } public static double[][] DeserializeDoubleMatrix(string data) { string[] tokens = data.Split(';'); int dim1 = int.Parse(tokens[0], CultureInfo.InvariantCulture.NumberFormat); int dim2 = int.Parse(tokens[1], CultureInfo.InvariantCulture.NumberFormat); if (tokens.Length != (dim1 * dim2) + 2) throw new ArgumentException("ERROR serialized data does not represent a matrix!"); double[][] result = new double[dim1][]; for (int i = 0; i < dim1; i++) { result[i] = new double[dim2]; for (int j = 0; j < dim2; j++) result[i][j] = double.Parse(tokens[i * dim1 + j + 2], CultureInfo.InvariantCulture.NumberFormat); } return result; } public static string SerializeMatrixWithHeaders(double[][] matrix, string[] headers) { StringBuilder builder = new StringBuilder(); int dim1 = matrix.Length; if (headers.Length != dim1) throw new ArgumentException("ERROR: headers and matrix size do not correspond"); int dim2 = matrix[0].Length; if (dim2 != dim1) throw new ArgumentException("ERROR: square matrix expected"); builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat)); builder.Append(";"); builder.Append(dim2.ToString(CultureInfo.InvariantCulture.NumberFormat)); for (int i = 0; i < headers.Length; i++) { builder.Append(";" + headers[i]); } for (int i = 0; i < dim1; i++) { for (int j = 0; j < dim2; j++) { if (matrix[i].Length != dim2) throw new ArgumentException("ERROR jagged array is not a matrix!"); builder.Append(";" + matrix[i][j].ToString(CultureInfo.InvariantCulture.NumberFormat)); } } return builder.ToString(); } public static double[][] DeserializeMatrixWithHeaders(string data, out string[] headers) { string[] tokens = data.Split(';'); int dim1 = int.Parse(tokens[0], CultureInfo.InvariantCulture.NumberFormat); int dim2 = int.Parse(tokens[1], CultureInfo.InvariantCulture.NumberFormat); if (dim1 != dim2) throw new ArgumentException("ERROR: square matrix expected"); headers = new string[dim1]; if (tokens.Length != (dim1 * dim2) + 2 + dim1) throw new ArgumentException("ERROR serialized data does not represent a matrix!"); for (int i = 0; i < dim1; i++) headers[i] = tokens[i + 2]; double[][] result = new double[dim1][]; for (int i = 0; i < dim1; i++) { result[i] = new double[dim2]; for (int j = 0; j < dim2; j++) result[i][j] = double.Parse(tokens[(i + 1) * dim1 + j + 2], CultureInfo.InvariantCulture.NumberFormat); } return result; } public static string SerializeIntArray(int[] array) { StringBuilder builder = new StringBuilder(); int dim1 = array.Length; builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat)); builder.Append(";"); for (int i = 0; i < dim1; i++) { builder.Append(";" + array[i].ToString(CultureInfo.InvariantCulture.NumberFormat)); } return builder.ToString(); } public static int[] DeserializeIntArray(string array) { string[] tokens = array.Split(';'); int dim1 = int.Parse(tokens[0], CultureInfo.InvariantCulture.NumberFormat); int[] result = new int[dim1]; for (int i = 0; i < dim1; i++) { result[i] = int.Parse(tokens[i + 1], CultureInfo.InvariantCulture.NumberFormat); } return result; } } }