[1542] | 1 | using System.Collections;
|
---|
| 2 | using System.Text;
|
---|
| 3 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 4 | using System;
|
---|
[1625] | 5 | using HeuristicLab.Persistence.Core;
|
---|
[1853] | 6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1542] | 7 |
|
---|
| 8 | namespace HeuristicLab.Persistence.Default.Xml.Compact {
|
---|
[1566] | 9 |
|
---|
[1853] | 10 | [EmptyStorableClass]
|
---|
[1823] | 11 | public abstract class NumberArray2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : class {
|
---|
[1542] | 12 |
|
---|
| 13 | protected virtual string Separator { get { return ";"; } }
|
---|
| 14 | protected abstract string FormatValue(object o);
|
---|
| 15 | protected abstract object ParseValue(string o);
|
---|
| 16 |
|
---|
[1853] | 17 | public override XmlString Format(T t) {
|
---|
[1612] | 18 | Array a = (Array)(object)t;
|
---|
[1542] | 19 | int[] lengths = new int[a.Rank];
|
---|
| 20 | int[] lowerBounds = new int[a.Rank];
|
---|
| 21 | StringBuilder sb = new StringBuilder();
|
---|
| 22 | sb.Append(a.Rank);
|
---|
| 23 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 24 | sb.Append(Separator);
|
---|
| 25 | sb.Append(a.GetLength(i));
|
---|
| 26 | lengths[i] = a.GetLength(i);
|
---|
| 27 | }
|
---|
| 28 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 29 | sb.Append(Separator);
|
---|
| 30 | sb.Append(a.GetLowerBound(i));
|
---|
| 31 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
| 32 | }
|
---|
| 33 | int[] positions = (int[])lowerBounds.Clone();
|
---|
[1566] | 34 | while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
|
---|
[1542] | 35 | sb.Append(Separator);
|
---|
| 36 | sb.Append(FormatValue(a.GetValue(positions)));
|
---|
| 37 | positions[0] += 1;
|
---|
| 38 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
| 39 | if (positions[i] >= lengths[i] + lowerBounds[i]) {
|
---|
| 40 | positions[i] = lowerBounds[i];
|
---|
| 41 | positions[i + 1] += 1;
|
---|
| 42 | } else {
|
---|
| 43 | break;
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
[1566] | 46 | }
|
---|
[1564] | 47 | return new XmlString(sb.ToString());
|
---|
[1542] | 48 | }
|
---|
| 49 |
|
---|
[1564] | 50 | public override T Parse(XmlString x) {
|
---|
[1625] | 51 | try {
|
---|
| 52 | IEnumerator values =
|
---|
| 53 | x.Data.Split(new[] { Separator },
|
---|
| 54 | StringSplitOptions.RemoveEmptyEntries).GetEnumerator();
|
---|
[1542] | 55 | values.MoveNext();
|
---|
[1625] | 56 | int rank = int.Parse((string)values.Current);
|
---|
| 57 | int[] lengths = new int[rank];
|
---|
| 58 | for (int i = 0; i < rank; i++) {
|
---|
| 59 | values.MoveNext();
|
---|
| 60 | lengths[i] = int.Parse((string)values.Current);
|
---|
| 61 | }
|
---|
| 62 | int[] lowerBounds = new int[rank];
|
---|
| 63 | for (int i = 0; i < rank; i++) {
|
---|
| 64 | values.MoveNext();
|
---|
| 65 | lowerBounds[i] = int.Parse((string)values.Current);
|
---|
| 66 | }
|
---|
| 67 | Array a = Array.CreateInstance(this.SourceType.GetElementType(), lengths, lowerBounds);
|
---|
| 68 | int[] positions = (int[])lowerBounds.Clone();
|
---|
| 69 | while (values.MoveNext()) {
|
---|
| 70 | a.SetValue(ParseValue((string)values.Current), positions);
|
---|
| 71 | positions[0] += 1;
|
---|
| 72 | for (int i = 0; i < rank - 1; i++) {
|
---|
| 73 | if (positions[i] >= lengths[i]) {
|
---|
| 74 | positions[i] = 0;
|
---|
| 75 | positions[i + 1] += 1;
|
---|
| 76 | } else {
|
---|
| 77 | break;
|
---|
| 78 | }
|
---|
[1542] | 79 | }
|
---|
| 80 | }
|
---|
[1625] | 81 | if (positions[rank - 1] != lowerBounds[rank - 1] + lengths[rank - 1])
|
---|
| 82 | throw new PersistenceException("Insufficient number of elements while trying to fill number array.");
|
---|
| 83 | return (T)(object)a;
|
---|
| 84 | } catch (InvalidOperationException e) {
|
---|
| 85 | throw new PersistenceException("Insufficient information to rebuild number array.", e);
|
---|
| 86 | } catch (InvalidCastException e) {
|
---|
| 87 | throw new PersistenceException("Invalid element data or meta data to reconstruct number array.", e);
|
---|
| 88 | } catch (OverflowException e) {
|
---|
| 89 | throw new PersistenceException("Overflow during element parsing while trying to reconstruct number array.", e);
|
---|
[1853] | 90 | }
|
---|
[1542] | 91 | }
|
---|
| 92 | }
|
---|
[1566] | 93 |
|
---|
[1542] | 94 | } |
---|