Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/ArraySerializer.cs @ 10859

Last change on this file since 10859 was 3742, checked in by gkronber, 14 years ago

Fixed GPL license headers and deleted files which are not referenced by projects. #893

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