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 SimpleSerializer {
|
---|
28 | #region Double Matrix
|
---|
29 | public static string SerializeDoubleMatrix(double[][] matrix) {
|
---|
30 | StringBuilder builder = new StringBuilder();
|
---|
31 | int dim1 = matrix.Length;
|
---|
32 | int dim2 = matrix[0].Length;
|
---|
33 | builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
34 | builder.Append(";");
|
---|
35 | builder.Append(dim2.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
36 | for (int i = 0; i < dim1; i++) {
|
---|
37 | for (int j = 0; j < dim2; j++) {
|
---|
38 | if (matrix[i].Length != dim2) throw new ArgumentException("ERROR jagged array is not a matrix!");
|
---|
39 | builder.Append(";" + matrix[i][j].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
40 | }
|
---|
41 | }
|
---|
42 | return builder.ToString();
|
---|
43 | }
|
---|
44 | public static string SerializeDoubleMatrix(double[,] matrix) {
|
---|
45 | StringBuilder builder = new StringBuilder();
|
---|
46 | int dim1 = matrix.GetLength(0);
|
---|
47 | int dim2 = matrix.GetLength(1);
|
---|
48 | builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
49 | builder.Append(";");
|
---|
50 | builder.Append(dim2.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
51 | for (int i = 0; i < dim1; i++) {
|
---|
52 | for (int j = 0; j < dim2; j++) {
|
---|
53 | builder.Append(";" + matrix[i, j].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
54 | }
|
---|
55 | }
|
---|
56 | return builder.ToString();
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static double[][] DeserializeDoubleMatrix(string data) {
|
---|
60 | string[] tokens = data.Split(';');
|
---|
61 | int dim1 = int.Parse(tokens[0], CultureInfo.InvariantCulture.NumberFormat);
|
---|
62 | int dim2 = int.Parse(tokens[1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
63 | if (tokens.Length != (dim1 * dim2) + 2) throw new SimpleSerializationException("ERROR: serialized data does not represent a matrix!");
|
---|
64 | double[][] result = new double[dim1][];
|
---|
65 | for (int i = 0; i < dim1; i++) {
|
---|
66 | result[i] = new double[dim2];
|
---|
67 | for (int j = 0; j < dim2; j++)
|
---|
68 | result[i][j] = double.Parse(tokens[i * dim1 + j + 2], CultureInfo.InvariantCulture.NumberFormat);
|
---|
69 | }
|
---|
70 | return result;
|
---|
71 | }
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | #region Int Matrix
|
---|
75 | public static string SerializeIntMatrix(int[][] matrix) {
|
---|
76 | StringBuilder builder = new StringBuilder();
|
---|
77 | int dim1 = matrix.Length;
|
---|
78 | int dim2 = matrix[0].Length;
|
---|
79 | builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
80 | builder.Append(";");
|
---|
81 | builder.Append(dim2.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
82 | for (int i = 0; i < dim1; i++) {
|
---|
83 | for (int j = 0; j < dim2; j++) {
|
---|
84 | if (matrix[i].Length != dim2) throw new ArgumentException("ERROR jagged array is not a matrix!");
|
---|
85 | builder.Append(";" + matrix[i][j].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
86 | }
|
---|
87 | }
|
---|
88 | return builder.ToString();
|
---|
89 | }
|
---|
90 | public static string SerializeIntMatrix(int[,] matrix) {
|
---|
91 | StringBuilder builder = new StringBuilder();
|
---|
92 | int dim1 = matrix.GetLength(0);
|
---|
93 | int dim2 = matrix.GetLength(1);
|
---|
94 | builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
95 | builder.Append(";");
|
---|
96 | builder.Append(dim2.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
97 | for (int i = 0; i < dim1; i++) {
|
---|
98 | for (int j = 0; j < dim2; j++) {
|
---|
99 | builder.Append(";" + matrix[i, j].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return builder.ToString();
|
---|
103 | }
|
---|
104 |
|
---|
105 | public static int[][] DeserializeIntMatrix(string data) {
|
---|
106 | string[] tokens = data.Split(';');
|
---|
107 | int dim1 = int.Parse(tokens[0], CultureInfo.InvariantCulture.NumberFormat);
|
---|
108 | int dim2 = int.Parse(tokens[1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
109 | if (tokens.Length != (dim1 * dim2) + 2) throw new SimpleSerializationException("ERROR: serialized data does not represent a matrix!");
|
---|
110 | int[][] result = new int[dim1][];
|
---|
111 | for (int i = 0; i < dim1; i++) {
|
---|
112 | result[i] = new int[dim2];
|
---|
113 | for (int j = 0; j < dim2; j++)
|
---|
114 | result[i][j] = int.Parse(tokens[i * dim1 + j + 2], CultureInfo.InvariantCulture.NumberFormat);
|
---|
115 | }
|
---|
116 | return result;
|
---|
117 | }
|
---|
118 | #endregion
|
---|
119 |
|
---|
120 | #region Double Array
|
---|
121 | public static string SerializeDoubleArray(double[] array) {
|
---|
122 | StringBuilder builder = new StringBuilder();
|
---|
123 | int dim1 = array.Length;
|
---|
124 | builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
125 | builder.Append(";");
|
---|
126 | for (int i = 0; i < dim1; i++) {
|
---|
127 | builder.Append(";" + array[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
128 | }
|
---|
129 | return builder.ToString();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public static double[] DeserializeDoubleArray(string array) {
|
---|
133 | string[] tokens = array.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
---|
134 | int dim1 = int.Parse(tokens[0], CultureInfo.InvariantCulture.NumberFormat);
|
---|
135 | double[] result = new double[dim1];
|
---|
136 | for (int i = 0; i < dim1; i++) {
|
---|
137 | result[i] = double.Parse(tokens[i + 1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
138 | }
|
---|
139 | return result;
|
---|
140 | }
|
---|
141 | #endregion
|
---|
142 |
|
---|
143 | #region Int Array
|
---|
144 | public static string SerializeIntArray(int[] array) {
|
---|
145 | StringBuilder builder = new StringBuilder();
|
---|
146 | int dim1 = array.Length;
|
---|
147 | builder.Append(dim1.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
148 | builder.Append(";");
|
---|
149 | for (int i = 0; i < dim1; i++) {
|
---|
150 | builder.Append(";" + array[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
151 | }
|
---|
152 | return builder.ToString();
|
---|
153 | }
|
---|
154 |
|
---|
155 | public static int[] DeserializeIntArray(string array) {
|
---|
156 | string[] tokens = array.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
---|
157 | int dim1 = int.Parse(tokens[0], CultureInfo.InvariantCulture.NumberFormat);
|
---|
158 | int[] result = new int[dim1];
|
---|
159 | for (int i = 0; i < dim1; i++) {
|
---|
160 | result[i] = int.Parse(tokens[i + 1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
161 | }
|
---|
162 | return result;
|
---|
163 | }
|
---|
164 | #endregion
|
---|
165 | }
|
---|
166 | } |
---|