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