[3742] | 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;
|
---|
[4132] | 23 | using System.Collections;
|
---|
[1567] | 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Text;
|
---|
[4068] | 26 | using HeuristicLab.Persistence.Auxiliary;
|
---|
| 27 | using HeuristicLab.Persistence.Core;
|
---|
[1823] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[4068] | 29 | using HeuristicLab.Persistence.Interfaces;
|
---|
[1567] | 30 |
|
---|
[1823] | 31 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
[1567] | 32 |
|
---|
[3017] | 33 | [StorableClass]
|
---|
[3946] | 34 | public sealed class CompactNumberArray2StringSerializer : ICompositeSerializer {
|
---|
[1567] | 35 |
|
---|
[4132] | 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 |
|
---|
[3946] | 83 | public const int SPLIT_THRESHOLD = 1024 * 1024;
|
---|
| 84 |
|
---|
[1567] | 85 | public int Priority {
|
---|
| 86 | get { return 200; }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[1823] | 89 | private static readonly Number2StringSerializer numberConverter =
|
---|
| 90 | new Number2StringSerializer();
|
---|
[1567] | 91 |
|
---|
[1823] | 92 | public bool CanSerialize(Type type) {
|
---|
[1567] | 93 | return
|
---|
| 94 | (type.IsArray || type == typeof(Array)) &&
|
---|
[1823] | 95 | numberConverter.CanSerialize(type.GetElementType());
|
---|
[1567] | 96 | }
|
---|
| 97 |
|
---|
[2993] | 98 | public string JustifyRejection(Type type) {
|
---|
| 99 | if (!type.IsArray && type != typeof(Array))
|
---|
| 100 | return "not an array";
|
---|
| 101 | return string.Format("number converter cannot serialize elements: " +
|
---|
| 102 | numberConverter.JustifyRejection(type.GetElementType()));
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[1567] | 105 | public IEnumerable<Tag> CreateMetaInfo(object obj) {
|
---|
| 106 | Array a = (Array)obj;
|
---|
| 107 | int[] lengths = new int[a.Rank];
|
---|
| 108 | int[] lowerBounds = new int[a.Rank];
|
---|
[3946] | 109 | StringBuilder sb = new StringBuilder(a.Rank * 6);
|
---|
[1567] | 110 | sb.Append(a.Rank).Append(';');
|
---|
| 111 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 112 | sb.Append(a.GetLength(i)).Append(';');
|
---|
| 113 | lengths[i] = a.GetLength(i);
|
---|
| 114 | }
|
---|
| 115 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 116 | sb.Append(a.GetLowerBound(i)).Append(';');
|
---|
| 117 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
| 118 | }
|
---|
[3946] | 119 | int nElements = 1;
|
---|
| 120 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 121 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
| 122 | lengths[i] = a.GetLength(i);
|
---|
| 123 | nElements *= lengths[i];
|
---|
| 124 | }
|
---|
[4132] | 125 | sb.Capacity += Math.Min(nElements * 3, SPLIT_THRESHOLD);
|
---|
[1567] | 126 | int[] positions = (int[])lowerBounds.Clone();
|
---|
| 127 | while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
|
---|
| 128 | sb.Append(numberConverter.Format(a.GetValue(positions))).Append(';');
|
---|
[3946] | 129 | if (sb.Length > SPLIT_THRESHOLD && sb.Length > sb.Capacity - 18) {
|
---|
| 130 | yield return new Tag(sb.ToString());
|
---|
| 131 | sb = new StringBuilder(Math.Min(nElements * 3, SPLIT_THRESHOLD));
|
---|
| 132 | }
|
---|
[1567] | 133 | positions[0] += 1;
|
---|
| 134 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
| 135 | if (positions[i] >= lengths[i] + lowerBounds[i]) {
|
---|
| 136 | positions[i] = lowerBounds[i];
|
---|
| 137 | positions[i + 1] += 1;
|
---|
| 138 | } else {
|
---|
| 139 | break;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
[3946] | 143 | if (sb.Length > 0)
|
---|
| 144 | yield return new Tag(sb.ToString());
|
---|
[1567] | 145 | }
|
---|
| 146 |
|
---|
[3946] | 147 | private static Tag[] emptyTag = new Tag[0];
|
---|
[1567] | 148 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
[3946] | 149 | return emptyTag;
|
---|
[1567] | 150 | }
|
---|
| 151 |
|
---|
| 152 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
[1625] | 153 | try {
|
---|
[4132] | 154 | var valueIter = new ElementEnumerator(metaInfo);
|
---|
[1567] | 155 | valueIter.MoveNext();
|
---|
[3946] | 156 | int rank = int.Parse(valueIter.Current);
|
---|
[1625] | 157 | int[] lengths = new int[rank];
|
---|
| 158 | int[] lowerBounds = new int[rank];
|
---|
| 159 | for (int i = 0; i < rank; i++) {
|
---|
| 160 | valueIter.MoveNext();
|
---|
[3946] | 161 | lengths[i] = int.Parse(valueIter.Current);
|
---|
[1625] | 162 | }
|
---|
| 163 | for (int i = 0; i < rank; i++) {
|
---|
| 164 | valueIter.MoveNext();
|
---|
[3946] | 165 | lowerBounds[i] = int.Parse(valueIter.Current);
|
---|
[1625] | 166 | }
|
---|
| 167 | Type elementType = type.GetElementType();
|
---|
| 168 | Array a = Array.CreateInstance(elementType, lengths, lowerBounds);
|
---|
[3946] | 169 | if (a == null) throw new PersistenceException("invalid instance data type, expected array");
|
---|
[1625] | 170 | int[] positions = (int[])lowerBounds.Clone();
|
---|
[4132] | 171 |
|
---|
| 172 | while (valueIter.MoveNext()) {
|
---|
| 173 | a.SetValue(numberConverter.Parse(valueIter.Current, elementType), positions);
|
---|
| 174 | positions[0] += 1;
|
---|
| 175 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
| 176 | if (positions[i] >= lengths[i] + lowerBounds[i]) {
|
---|
| 177 | positions[i + 1] += 1;
|
---|
| 178 | positions[i] = lowerBounds[i];
|
---|
| 179 | } else {
|
---|
| 180 | break;
|
---|
[1625] | 181 | }
|
---|
[1567] | 182 | }
|
---|
| 183 | }
|
---|
[1625] | 184 | return a;
|
---|
[4132] | 185 | } catch (InvalidOperationException e) {
|
---|
[3946] | 186 | throw new PersistenceException("Insuffictient data to deserialize compact array", e);
|
---|
[4132] | 187 | } catch (InvalidCastException e) {
|
---|
[1625] | 188 | throw new PersistenceException("Invalid element data during compact array deserialization", e);
|
---|
[1703] | 189 | }
|
---|
[1567] | 190 | }
|
---|
| 191 |
|
---|
| 192 | public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
[3946] | 193 | // Nothing to do. Arrays are populated during instance creation;
|
---|
[1567] | 194 | }
|
---|
| 195 |
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | } |
---|