1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Persistence.Auxiliary;
|
---|
27 | using HeuristicLab.Persistence.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Persistence.Interfaces;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
32 |
|
---|
33 | [StorableClass]
|
---|
34 | public sealed class CompactNumberArray2StringSerializer : ICompositeSerializer {
|
---|
35 |
|
---|
36 | private class ElementEnumerator : IEnumerator<string> {
|
---|
37 |
|
---|
38 | private IEnumerator<Tag> tagIt;
|
---|
39 | private IEnumerator<string> valIt;
|
---|
40 |
|
---|
41 | public ElementEnumerator(IEnumerable<Tag> tags) {
|
---|
42 | tagIt = tags.GetEnumerator();
|
---|
43 | }
|
---|
44 |
|
---|
45 | public string Current {
|
---|
46 | get {
|
---|
47 | if (valIt == null)
|
---|
48 | throw new InvalidOperationException("no current value");
|
---|
49 | return valIt.Current;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void Dispose() {
|
---|
54 | valIt.Dispose();
|
---|
55 | valIt = null;
|
---|
56 | tagIt.Dispose();
|
---|
57 | }
|
---|
58 |
|
---|
59 | object IEnumerator.Current {
|
---|
60 | get { return this.Current; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public bool MoveNext() {
|
---|
64 | if (valIt != null && valIt.MoveNext())
|
---|
65 | return true;
|
---|
66 | if (tagIt.MoveNext()) {
|
---|
67 | if (valIt != null)
|
---|
68 | valIt.Dispose();
|
---|
69 | valIt = ((string)tagIt.Current.Value).GetSplitEnumerator(';');
|
---|
70 | return MoveNext();
|
---|
71 | }
|
---|
72 | valIt.Dispose();
|
---|
73 | valIt = null;
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void Reset() {
|
---|
78 | valIt.Dispose();
|
---|
79 | tagIt.Reset();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | [StorableConstructor]
|
---|
84 | private CompactNumberArray2StringSerializer(bool deserializing) { }
|
---|
85 | public CompactNumberArray2StringSerializer() { }
|
---|
86 |
|
---|
87 | public const int SPLIT_THRESHOLD = 1024 * 1024;
|
---|
88 |
|
---|
89 | public int Priority {
|
---|
90 | get { return 200; }
|
---|
91 | }
|
---|
92 |
|
---|
93 | private static readonly Number2StringSerializer numberConverter =
|
---|
94 | new Number2StringSerializer();
|
---|
95 |
|
---|
96 | public bool CanSerialize(Type type) {
|
---|
97 | return
|
---|
98 | (type.IsArray || type == typeof(Array)) &&
|
---|
99 | numberConverter.CanSerialize(type.GetElementType());
|
---|
100 | }
|
---|
101 |
|
---|
102 | public string JustifyRejection(Type type) {
|
---|
103 | if (!type.IsArray && type != typeof(Array))
|
---|
104 | return "not an array";
|
---|
105 | return string.Format("number converter cannot serialize elements: " +
|
---|
106 | numberConverter.JustifyRejection(type.GetElementType()));
|
---|
107 | }
|
---|
108 |
|
---|
109 | public IEnumerable<Tag> CreateMetaInfo(object obj) {
|
---|
110 | Array a = (Array)obj;
|
---|
111 | int[] lengths = new int[a.Rank];
|
---|
112 | int[] lowerBounds = new int[a.Rank];
|
---|
113 | StringBuilder sb = new StringBuilder(a.Rank * 6);
|
---|
114 | sb.Append(a.Rank).Append(';');
|
---|
115 | for (int i = 0; i < a.Rank; i++) {
|
---|
116 | sb.Append(a.GetLength(i)).Append(';');
|
---|
117 | lengths[i] = a.GetLength(i);
|
---|
118 | }
|
---|
119 | for (int i = 0; i < a.Rank; i++) {
|
---|
120 | sb.Append(a.GetLowerBound(i)).Append(';');
|
---|
121 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
122 | }
|
---|
123 | int nElements = 1;
|
---|
124 | for (int i = 0; i < a.Rank; i++) {
|
---|
125 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
126 | lengths[i] = a.GetLength(i);
|
---|
127 | nElements *= lengths[i];
|
---|
128 | }
|
---|
129 | sb.Capacity += Math.Min(nElements * 3, SPLIT_THRESHOLD);
|
---|
130 | int[] positions = (int[])lowerBounds.Clone();
|
---|
131 | while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
|
---|
132 | sb.Append(numberConverter.Format(a.GetValue(positions))).Append(';');
|
---|
133 | if (sb.Length > SPLIT_THRESHOLD && sb.Length > sb.Capacity - 18) {
|
---|
134 | yield return new Tag(sb.ToString());
|
---|
135 | sb = new StringBuilder(Math.Min(nElements * 3, SPLIT_THRESHOLD));
|
---|
136 | }
|
---|
137 | positions[0] += 1;
|
---|
138 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
139 | if (positions[i] >= lengths[i] + lowerBounds[i]) {
|
---|
140 | positions[i] = lowerBounds[i];
|
---|
141 | positions[i + 1] += 1;
|
---|
142 | } else {
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | if (sb.Length > 0)
|
---|
148 | yield return new Tag(sb.ToString());
|
---|
149 | }
|
---|
150 |
|
---|
151 | private static Tag[] emptyTag = new Tag[0];
|
---|
152 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
153 | return emptyTag;
|
---|
154 | }
|
---|
155 |
|
---|
156 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
157 | try {
|
---|
158 | var valueIter = new ElementEnumerator(metaInfo);
|
---|
159 | valueIter.MoveNext();
|
---|
160 | int rank = int.Parse(valueIter.Current);
|
---|
161 | int[] lengths = new int[rank];
|
---|
162 | int[] lowerBounds = new int[rank];
|
---|
163 | for (int i = 0; i < rank; i++) {
|
---|
164 | valueIter.MoveNext();
|
---|
165 | lengths[i] = int.Parse(valueIter.Current);
|
---|
166 | }
|
---|
167 | for (int i = 0; i < rank; i++) {
|
---|
168 | valueIter.MoveNext();
|
---|
169 | lowerBounds[i] = int.Parse(valueIter.Current);
|
---|
170 | }
|
---|
171 | Type elementType = type.GetElementType();
|
---|
172 | Array a = Array.CreateInstance(elementType, lengths, lowerBounds);
|
---|
173 | if (a == null) throw new PersistenceException("invalid instance data type, expected array");
|
---|
174 | int[] positions = (int[])lowerBounds.Clone();
|
---|
175 |
|
---|
176 | while (valueIter.MoveNext()) {
|
---|
177 | a.SetValue(numberConverter.Parse(valueIter.Current, elementType), positions);
|
---|
178 | positions[0] += 1;
|
---|
179 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
180 | if (positions[i] >= lengths[i] + lowerBounds[i]) {
|
---|
181 | positions[i + 1] += 1;
|
---|
182 | positions[i] = lowerBounds[i];
|
---|
183 | } else {
|
---|
184 | break;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | return a;
|
---|
189 | }
|
---|
190 | catch (InvalidOperationException e) {
|
---|
191 | throw new PersistenceException("Insuffictient data to deserialize compact array", e);
|
---|
192 | }
|
---|
193 | catch (InvalidCastException e) {
|
---|
194 | throw new PersistenceException("Invalid element data during compact array deserialization", e);
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
199 | // Nothing to do. Arrays are populated during instance creation;
|
---|
200 | }
|
---|
201 |
|
---|
202 | }
|
---|
203 |
|
---|
204 | } |
---|