Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/ArraySerializer.cs @ 4949

Last change on this file since 4949 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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