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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Persistence.Core;
|
---|
24 | using HeuristicLab.Persistence.Interfaces;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using System.Text;
|
---|
28 |
|
---|
29 | namespace 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 | }
|
---|