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

Last change on this file since 3945 was 3945, checked in by epitzer, 9 years ago

Remove casts in number array serializer and add handling of lower bounds (#1138)

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