Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/ArraySerializer.cs @ 3205

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

make most serializers internal and complete API documentation (#548)

File size: 4.1 KB
Line 
1using System;
2using HeuristicLab.Persistence.Core;
3using HeuristicLab.Persistence.Interfaces;
4using System.Collections.Generic;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6using System.Text;
7
8namespace HeuristicLab.Persistence.Default.CompositeSerializers {
9
10  [StorableClass]
11  internal sealed class ArraySerializer : ICompositeSerializer {
12
13    public int Priority {
14      get { return 100; }
15    }
16
17    public bool CanSerialize(Type type) {
18      return type.IsArray || type == typeof(Array);
19    }
20
21    public string JustifyRejection(Type type) {
22      return "not an array and not of type System.Array";
23    }
24
25    public IEnumerable<Tag> CreateMetaInfo(object obj) {
26      Array a = (Array)obj;
27      yield return new Tag("rank", a.Rank);
28      for (int i = 0; i < a.Rank; i++) {
29        yield return new Tag("length_" + i, a.GetLength(i));
30      }
31      for (int i = 0; i < a.Rank; i++) {
32        yield return new Tag("lowerBound_" + i, a.GetLowerBound(i));
33      }
34    }
35
36    public IEnumerable<Tag> Decompose(object array) {
37      Array a = (Array)array;
38      int[] lengths = new int[a.Rank];
39      int[] lowerBounds = new int[a.Rank];
40      for (int i = 0; i < a.Rank; i++) {
41        lengths[i] = a.GetLength(i);
42      }
43      for (int i = 0; i < a.Rank; i++) {
44        lowerBounds[i] = a.GetLowerBound(i);
45      }
46      int[] positions = (int[])lowerBounds.Clone();
47      while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
48        yield return new Tag(a.GetValue(positions));
49        positions[0] += 1;
50        for (int i = 0; i < a.Rank - 1; i++) {
51          if (positions[i] >= lowerBounds[i] + lengths[i]) {
52            positions[i] = lowerBounds[i];
53            positions[i + 1] += 1;
54          } else {
55            break;
56          }
57        }
58      }
59    }
60
61    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
62      try {
63        IEnumerator<Tag> e = metaInfo.GetEnumerator();
64        e.MoveNext();
65        int rank = (int)e.Current.Value;
66        int[] lengths = new int[rank];
67        for (int i = 0; i < rank; i++) {
68          e.MoveNext();
69          lengths[i] = (int)e.Current.Value;
70        }
71        int[] lowerBounds = new int[rank];
72        for (int i = 0; i < rank; i++) {
73          e.MoveNext();
74          lowerBounds[i] = (int)e.Current.Value;
75        }
76        return Array.CreateInstance(t.GetElementType(), lengths, lowerBounds);
77      } catch (InvalidOperationException x) {
78        throw new PersistenceException("Insufficient meta information to construct array instance.", x);
79      } catch (InvalidCastException x) {
80        throw new PersistenceException("Invalid format of array metainfo.", x);
81      }
82    }
83
84    public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
85      Array a = (Array)instance;
86      int[] lengths = new int[a.Rank];
87      int[] lowerBounds = new int[a.Rank];
88      for (int i = 0; i < a.Rank; i++) {
89        lengths[i] = a.GetLength(i);
90      }
91      for (int i = 0; i < a.Rank; i++) {
92        lowerBounds[i] = a.GetLowerBound(i);
93      }
94      int[] positions = (int[])lowerBounds.Clone();
95      IEnumerator<Tag> e = elements.GetEnumerator();
96      try {
97        while (e.MoveNext()) {
98          int[] currentPositions = positions;
99          a.SetValue(e.Current.Value, currentPositions);
100          positions[0] += 1;
101          for (int i = 0; i < a.Rank - 1; i++) {
102            if (positions[i] >= lengths[i] + lowerBounds[i]) {
103              positions[i] = lowerBounds[i];
104              positions[i + 1] += 1;
105            } else {
106              break;
107            }
108          }
109        }
110      } catch (InvalidOperationException x) {
111        throw new PersistenceException("Insufficient data to fill array instance", x);
112      } catch (InvalidCastException x) {
113        throw new PersistenceException("Invalid element data. Cannot fill array", x);
114      } catch (IndexOutOfRangeException x) {
115        throw new PersistenceException("Too many elements during array deserialization", x);
116      }
117    }
118  }
119
120}
Note: See TracBrowser for help on using the repository browser.