[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3742] | 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 |
|
---|
[4068] | 22 | using System;
|
---|
[3742] | 23 | using System.Collections;
|
---|
[4068] | 24 | using System.Collections.Generic;
|
---|
[9388] | 25 | using System.Linq;
|
---|
[1542] | 26 | using System.Text;
|
---|
[4068] | 27 | using HeuristicLab.Persistence.Auxiliary;
|
---|
[1625] | 28 | using HeuristicLab.Persistence.Core;
|
---|
[1853] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1542] | 30 |
|
---|
| 31 | namespace HeuristicLab.Persistence.Default.Xml.Compact {
|
---|
[1566] | 32 |
|
---|
[3017] | 33 | [StorableClass]
|
---|
[3036] | 34 | internal abstract class NumberArray2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : class {
|
---|
[1542] | 35 |
|
---|
[3937] | 36 | protected virtual char Separator { get { return ';'; } }
|
---|
[1542] | 37 | protected abstract string FormatValue(object o);
|
---|
| 38 | protected abstract object ParseValue(string o);
|
---|
| 39 |
|
---|
[1853] | 40 | public override XmlString Format(T t) {
|
---|
[1612] | 41 | Array a = (Array)(object)t;
|
---|
[1542] | 42 | int[] lengths = new int[a.Rank];
|
---|
| 43 | int[] lowerBounds = new int[a.Rank];
|
---|
[3945] | 44 | StringBuilder sb = new StringBuilder(3 + a.Rank * 3);
|
---|
[1542] | 45 | sb.Append(a.Rank);
|
---|
[3945] | 46 | int nElements = 1;
|
---|
[1542] | 47 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 48 | sb.Append(Separator);
|
---|
| 49 | sb.Append(a.GetLength(i));
|
---|
| 50 | lengths[i] = a.GetLength(i);
|
---|
[3945] | 51 | nElements *= lengths[i];
|
---|
[4068] | 52 | }
|
---|
[3945] | 53 | sb.EnsureCapacity(sb.Length + nElements * 3);
|
---|
[1542] | 54 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 55 | sb.Append(Separator);
|
---|
| 56 | sb.Append(a.GetLowerBound(i));
|
---|
| 57 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
| 58 | }
|
---|
[9388] | 59 | if (lengths.Any(l => l == 0))
|
---|
| 60 | return new XmlString(sb.ToString());
|
---|
[1542] | 61 | int[] positions = (int[])lowerBounds.Clone();
|
---|
[1566] | 62 | while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
|
---|
[1542] | 63 | sb.Append(Separator);
|
---|
| 64 | sb.Append(FormatValue(a.GetValue(positions)));
|
---|
| 65 | positions[0] += 1;
|
---|
| 66 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
| 67 | if (positions[i] >= lengths[i] + lowerBounds[i]) {
|
---|
| 68 | positions[i] = lowerBounds[i];
|
---|
| 69 | positions[i + 1] += 1;
|
---|
| 70 | } else {
|
---|
| 71 | break;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
[4068] | 74 | }
|
---|
[1564] | 75 | return new XmlString(sb.ToString());
|
---|
[1542] | 76 | }
|
---|
| 77 |
|
---|
[1564] | 78 | public override T Parse(XmlString x) {
|
---|
[1625] | 79 | try {
|
---|
[3945] | 80 | IEnumerator<string> values = x.Data.GetSplitEnumerator(Separator);
|
---|
[1542] | 81 | values.MoveNext();
|
---|
[3945] | 82 | int rank = int.Parse(values.Current);
|
---|
[1625] | 83 | int[] lengths = new int[rank];
|
---|
| 84 | for (int i = 0; i < rank; i++) {
|
---|
| 85 | values.MoveNext();
|
---|
[3945] | 86 | lengths[i] = int.Parse(values.Current);
|
---|
[1625] | 87 | }
|
---|
| 88 | int[] lowerBounds = new int[rank];
|
---|
| 89 | for (int i = 0; i < rank; i++) {
|
---|
| 90 | values.MoveNext();
|
---|
[3945] | 91 | lowerBounds[i] = int.Parse(values.Current);
|
---|
[1625] | 92 | }
|
---|
| 93 | Array a = Array.CreateInstance(this.SourceType.GetElementType(), lengths, lowerBounds);
|
---|
| 94 | int[] positions = (int[])lowerBounds.Clone();
|
---|
[4068] | 95 | while (values.MoveNext()) {
|
---|
[3945] | 96 | a.SetValue(ParseValue(values.Current), positions);
|
---|
[1625] | 97 | positions[0] += 1;
|
---|
| 98 | for (int i = 0; i < rank - 1; i++) {
|
---|
[3945] | 99 | if (positions[i] >= lowerBounds[i] + lengths[i]) {
|
---|
| 100 | positions[i] = lowerBounds[i];
|
---|
[1625] | 101 | positions[i + 1] += 1;
|
---|
| 102 | } else {
|
---|
| 103 | break;
|
---|
| 104 | }
|
---|
[1542] | 105 | }
|
---|
| 106 | }
|
---|
[9388] | 107 | if (positions[rank - 1] != lowerBounds[rank - 1] + lengths[rank - 1] && lengths.All(l => l != 0))
|
---|
[1625] | 108 | throw new PersistenceException("Insufficient number of elements while trying to fill number array.");
|
---|
| 109 | return (T)(object)a;
|
---|
[4068] | 110 | }
|
---|
| 111 | catch (InvalidOperationException e) {
|
---|
[1625] | 112 | throw new PersistenceException("Insufficient information to rebuild number array.", e);
|
---|
[4068] | 113 | }
|
---|
| 114 | catch (InvalidCastException e) {
|
---|
[1625] | 115 | throw new PersistenceException("Invalid element data or meta data to reconstruct number array.", e);
|
---|
[4068] | 116 | }
|
---|
| 117 | catch (OverflowException e) {
|
---|
[1625] | 118 | throw new PersistenceException("Overflow during element parsing while trying to reconstruct number array.", e);
|
---|
[1853] | 119 | }
|
---|
[1542] | 120 | }
|
---|
| 121 | }
|
---|
[1566] | 122 |
|
---|
[1542] | 123 | } |
---|