Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlSerializerBase.cs @ 16723

Last change on this file since 16723 was 16723, checked in by abeham, 5 years ago

#2521: merged changes from r15684 to trunk HEAD (r16716) and resolved all merge conflicts

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