Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlSerializerBase.cs @ 7284

Last change on this file since 7284 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

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