[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3742] | 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 |
|
---|
[4806] | 83 | [StorableConstructor]
|
---|
| 84 | private CompactNumberArray2StringSerializer(bool deserializing) { }
|
---|
| 85 | public CompactNumberArray2StringSerializer() { }
|
---|
| 86 |
|
---|
[3946] | 87 | public const int SPLIT_THRESHOLD = 1024 * 1024;
|
---|
| 88 |
|
---|
[1567] | 89 | public int Priority {
|
---|
| 90 | get { return 200; }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[1823] | 93 | private static readonly Number2StringSerializer numberConverter =
|
---|
| 94 | new Number2StringSerializer();
|
---|
[1567] | 95 |
|
---|
[1823] | 96 | public bool CanSerialize(Type type) {
|
---|
[1567] | 97 | return
|
---|
| 98 | (type.IsArray || type == typeof(Array)) &&
|
---|
[1823] | 99 | numberConverter.CanSerialize(type.GetElementType());
|
---|
[1567] | 100 | }
|
---|
| 101 |
|
---|
[2993] | 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 |
|
---|
[1567] | 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];
|
---|
[3946] | 113 | StringBuilder sb = new StringBuilder(a.Rank * 6);
|
---|
[1567] | 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 | }
|
---|
[3946] | 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 | }
|
---|
[4132] | 129 | sb.Capacity += Math.Min(nElements * 3, SPLIT_THRESHOLD);
|
---|
[1567] | 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(';');
|
---|
[3946] | 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 | }
|
---|
[1567] | 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 | }
|
---|
[3946] | 147 | if (sb.Length > 0)
|
---|
| 148 | yield return new Tag(sb.ToString());
|
---|
[1567] | 149 | }
|
---|
| 150 |
|
---|
[3946] | 151 | private static Tag[] emptyTag = new Tag[0];
|
---|
[1567] | 152 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
[3946] | 153 | return emptyTag;
|
---|
[1567] | 154 | }
|
---|
| 155 |
|
---|
| 156 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
[1625] | 157 | try {
|
---|
[4132] | 158 | var valueIter = new ElementEnumerator(metaInfo);
|
---|
[1567] | 159 | valueIter.MoveNext();
|
---|
[3946] | 160 | int rank = int.Parse(valueIter.Current);
|
---|
[1625] | 161 | int[] lengths = new int[rank];
|
---|
| 162 | int[] lowerBounds = new int[rank];
|
---|
| 163 | for (int i = 0; i < rank; i++) {
|
---|
| 164 | valueIter.MoveNext();
|
---|
[3946] | 165 | lengths[i] = int.Parse(valueIter.Current);
|
---|
[1625] | 166 | }
|
---|
| 167 | for (int i = 0; i < rank; i++) {
|
---|
| 168 | valueIter.MoveNext();
|
---|
[3946] | 169 | lowerBounds[i] = int.Parse(valueIter.Current);
|
---|
[1625] | 170 | }
|
---|
| 171 | Type elementType = type.GetElementType();
|
---|
| 172 | Array a = Array.CreateInstance(elementType, lengths, lowerBounds);
|
---|
[3946] | 173 | if (a == null) throw new PersistenceException("invalid instance data type, expected array");
|
---|
[1625] | 174 | int[] positions = (int[])lowerBounds.Clone();
|
---|
[4132] | 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;
|
---|
[1625] | 185 | }
|
---|
[1567] | 186 | }
|
---|
| 187 | }
|
---|
[1625] | 188 | return a;
|
---|
[4806] | 189 | }
|
---|
| 190 | catch (InvalidOperationException e) {
|
---|
[3946] | 191 | throw new PersistenceException("Insuffictient data to deserialize compact array", e);
|
---|
[4806] | 192 | }
|
---|
| 193 | catch (InvalidCastException e) {
|
---|
[1625] | 194 | throw new PersistenceException("Invalid element data during compact array deserialization", e);
|
---|
[1703] | 195 | }
|
---|
[1567] | 196 | }
|
---|
| 197 |
|
---|
| 198 | public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
[3946] | 199 | // Nothing to do. Arrays are populated during instance creation;
|
---|
[1567] | 200 | }
|
---|
| 201 |
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | } |
---|