Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlSerializerBase.cs @ 17181

Last change on this file since 17181 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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