[6659] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Globalization;
|
---|
| 24 | using System.Text;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Services.ProblemInstances {
|
---|
| 27 | public static class PrimitiveSerializer {
|
---|
| 28 | public static string SerializeDoubleMatrix(double[][] matrix) {
|
---|
| 29 | StringBuilder builder = new StringBuilder();
|
---|
| 30 | int dim1 = matrix.Length;
|
---|
| 31 | int dim2 = matrix[0].Length;
|
---|
| 32 | builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
| 33 | builder.Append(";");
|
---|
| 34 | builder.Append(dim2.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
| 35 | for (int i = 0; i < dim1; i++) {
|
---|
| 36 | for (int j = 0; j < dim2; j++) {
|
---|
| 37 | if (matrix[i].Length != dim2) throw new ArgumentException("ERROR jagged array is not a matrix!");
|
---|
| 38 | builder.Append(";" + matrix[i][j].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 | return builder.ToString();
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public static double[][] DeserializeDoubleMatrix(string data) {
|
---|
| 45 | string[] tokens = data.Split(';');
|
---|
| 46 | int dim1 = int.Parse(tokens[0], CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 47 | int dim2 = int.Parse(tokens[1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 48 | if (tokens.Length != (dim1 * dim2) + 2) throw new ArgumentException("ERROR serialized data does not represent a matrix!");
|
---|
| 49 | double[][] result = new double[dim1][];
|
---|
| 50 | for (int i = 0; i < dim1; i++) {
|
---|
| 51 | result[i] = new double[dim2];
|
---|
| 52 | for (int j = 0; j < dim2; j++)
|
---|
| 53 | result[i][j] = double.Parse(tokens[i * dim1 + j + 2], CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 54 | }
|
---|
| 55 | return result;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public static string SerializeMatrixWithHeaders(double[][] matrix, string[] headers) {
|
---|
| 59 | StringBuilder builder = new StringBuilder();
|
---|
| 60 | int dim1 = matrix.Length;
|
---|
| 61 | if (headers.Length != dim1) throw new ArgumentException("ERROR: headers and matrix size do not correspond");
|
---|
| 62 | int dim2 = matrix[0].Length;
|
---|
| 63 | if (dim2 != dim1) throw new ArgumentException("ERROR: square matrix expected");
|
---|
| 64 | builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
| 65 | builder.Append(";");
|
---|
| 66 | builder.Append(dim2.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
| 67 | for (int i = 0; i < headers.Length; i++) {
|
---|
| 68 | builder.Append(";" + headers[i]);
|
---|
| 69 | }
|
---|
| 70 | for (int i = 0; i < dim1; i++) {
|
---|
| 71 | for (int j = 0; j < dim2; j++) {
|
---|
| 72 | if (matrix[i].Length != dim2) throw new ArgumentException("ERROR jagged array is not a matrix!");
|
---|
| 73 | builder.Append(";" + matrix[i][j].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | return builder.ToString();
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public static double[][] DeserializeMatrixWithHeaders(string data, out string[] headers) {
|
---|
| 80 | string[] tokens = data.Split(';');
|
---|
| 81 | int dim1 = int.Parse(tokens[0], CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 82 | int dim2 = int.Parse(tokens[1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 83 | if (dim1 != dim2) throw new ArgumentException("ERROR: square matrix expected");
|
---|
| 84 | headers = new string[dim1];
|
---|
| 85 | if (tokens.Length != (dim1 * dim2) + 2 + dim1) throw new ArgumentException("ERROR serialized data does not represent a matrix!");
|
---|
| 86 | for (int i = 0; i < dim1; i++)
|
---|
| 87 | headers[i] = tokens[i + 2];
|
---|
| 88 | double[][] result = new double[dim1][];
|
---|
| 89 | for (int i = 0; i < dim1; i++) {
|
---|
| 90 | result[i] = new double[dim2];
|
---|
| 91 | for (int j = 0; j < dim2; j++)
|
---|
| 92 | result[i][j] = double.Parse(tokens[(i + 1) * dim1 + j + 2], CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 93 | }
|
---|
| 94 | return result;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | public static string SerializeIntArray(int[] array) {
|
---|
| 98 | StringBuilder builder = new StringBuilder();
|
---|
| 99 | int dim1 = array.Length;
|
---|
| 100 | builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
| 101 | builder.Append(";");
|
---|
| 102 | for (int i = 0; i < dim1; i++) {
|
---|
| 103 | builder.Append(";" + array[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
| 104 | }
|
---|
| 105 | return builder.ToString();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public static int[] DeserializeIntArray(string array) {
|
---|
| 109 | string[] tokens = array.Split(';');
|
---|
| 110 | int dim1 = int.Parse(tokens[0], CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 111 | int[] result = new int[dim1];
|
---|
| 112 | for (int i = 0; i < dim1; i++) {
|
---|
| 113 | result[i] = int.Parse(tokens[i + 1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 114 | }
|
---|
| 115 | return result;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | } |
---|