Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/CompactNumberArray2StringSerializer.cs @ 4132

Last change on this file since 4132 was 4132, checked in by epitzer, 14 years ago

make optimized version of CompactNumberArray2StringSerializer compatible with released version (#1138, #1108)

File size: 6.6 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;
23using System.Collections;
24using System.Collections.Generic;
25using System.Text;
26using HeuristicLab.Persistence.Auxiliary;
27using HeuristicLab.Persistence.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Persistence.Interfaces;
30
31namespace HeuristicLab.Persistence.Default.CompositeSerializers {
32
33  [StorableClass]
34  public sealed class CompactNumberArray2StringSerializer : ICompositeSerializer {
35
36    private class ElementEnumerator : IEnumerator<string> {
37
38      private IEnumerator<Tag> tagIt;
39      private IEnumerator<string> valIt;
40
41      public ElementEnumerator(IEnumerable<Tag> tags) {
42        tagIt = tags.GetEnumerator();
43      }
44
45      public string Current {
46        get {
47          if (valIt == null)
48            throw new InvalidOperationException("no current value");
49          return valIt.Current;
50        }
51      }
52
53      public void Dispose() {
54        valIt.Dispose();
55        valIt = null;
56        tagIt.Dispose();
57      }
58
59      object IEnumerator.Current {
60        get { return this.Current; }
61      }
62
63      public bool MoveNext() {
64        if (valIt != null && valIt.MoveNext())
65          return true;
66        if (tagIt.MoveNext()) {
67          if (valIt != null)
68            valIt.Dispose();
69          valIt = ((string)tagIt.Current.Value).GetSplitEnumerator(';');
70          return MoveNext();
71        }
72        valIt.Dispose();
73        valIt = null;
74        return false;
75      }
76
77      public void Reset() {
78        valIt.Dispose();
79        tagIt.Reset();
80      }
81    }
82
83    public const int SPLIT_THRESHOLD = 1024 * 1024;
84
85    public int Priority {
86      get { return 200; }
87    }
88
89    private static readonly Number2StringSerializer numberConverter =
90      new Number2StringSerializer();
91
92    public bool CanSerialize(Type type) {
93      return
94        (type.IsArray || type == typeof(Array)) &&
95        numberConverter.CanSerialize(type.GetElementType());
96    }
97
98    public string JustifyRejection(Type type) {
99      if (!type.IsArray && type != typeof(Array))
100        return "not an array";
101      return string.Format("number converter cannot serialize elements: " +
102        numberConverter.JustifyRejection(type.GetElementType()));
103    }
104
105    public IEnumerable<Tag> CreateMetaInfo(object obj) {
106      Array a = (Array)obj;
107      int[] lengths = new int[a.Rank];
108      int[] lowerBounds = new int[a.Rank];
109      StringBuilder sb = new StringBuilder(a.Rank * 6);
110      sb.Append(a.Rank).Append(';');
111      for (int i = 0; i < a.Rank; i++) {
112        sb.Append(a.GetLength(i)).Append(';');
113        lengths[i] = a.GetLength(i);
114      }
115      for (int i = 0; i < a.Rank; i++) {
116        sb.Append(a.GetLowerBound(i)).Append(';');
117        lowerBounds[i] = a.GetLowerBound(i);
118      }
119      int nElements = 1;
120      for (int i = 0; i < a.Rank; i++) {
121        lowerBounds[i] = a.GetLowerBound(i);
122        lengths[i] = a.GetLength(i);
123        nElements *= lengths[i];
124      }
125      sb.Capacity += Math.Min(nElements * 3, SPLIT_THRESHOLD);
126      int[] positions = (int[])lowerBounds.Clone();
127      while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
128        sb.Append(numberConverter.Format(a.GetValue(positions))).Append(';');
129        if (sb.Length > SPLIT_THRESHOLD && sb.Length > sb.Capacity - 18) {
130          yield return new Tag(sb.ToString());
131          sb = new StringBuilder(Math.Min(nElements * 3, SPLIT_THRESHOLD));
132        }
133        positions[0] += 1;
134        for (int i = 0; i < a.Rank - 1; i++) {
135          if (positions[i] >= lengths[i] + lowerBounds[i]) {
136            positions[i] = lowerBounds[i];
137            positions[i + 1] += 1;
138          } else {
139            break;
140          }
141        }
142      }
143      if (sb.Length > 0)
144        yield return new Tag(sb.ToString());
145    }
146
147    private static Tag[] emptyTag = new Tag[0];
148    public IEnumerable<Tag> Decompose(object obj) {
149      return emptyTag;
150    }
151
152    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
153      try {
154        var valueIter = new ElementEnumerator(metaInfo);
155        valueIter.MoveNext();
156        int rank = int.Parse(valueIter.Current);
157        int[] lengths = new int[rank];
158        int[] lowerBounds = new int[rank];
159        for (int i = 0; i < rank; i++) {
160          valueIter.MoveNext();
161          lengths[i] = int.Parse(valueIter.Current);
162        }
163        for (int i = 0; i < rank; i++) {
164          valueIter.MoveNext();
165          lowerBounds[i] = int.Parse(valueIter.Current);
166        }
167        Type elementType = type.GetElementType();
168        Array a = Array.CreateInstance(elementType, lengths, lowerBounds);
169        if (a == null) throw new PersistenceException("invalid instance data type, expected array");
170        int[] positions = (int[])lowerBounds.Clone();
171
172        while (valueIter.MoveNext()) {
173          a.SetValue(numberConverter.Parse(valueIter.Current, elementType), positions);
174          positions[0] += 1;
175          for (int i = 0; i < a.Rank - 1; i++) {
176            if (positions[i] >= lengths[i] + lowerBounds[i]) {
177              positions[i + 1] += 1;
178              positions[i] = lowerBounds[i];
179            } else {
180              break;
181            }
182          }
183        }
184        return a;
185      } catch (InvalidOperationException e) {
186        throw new PersistenceException("Insuffictient data to deserialize compact array", e);
187      } catch (InvalidCastException e) {
188        throw new PersistenceException("Invalid element data during compact array deserialization", e);
189      }
190    }
191
192    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
193      // Nothing to do. Arrays are populated during instance creation;
194    }
195
196  }
197
198}
Note: See TracBrowser for help on using the repository browser.