[1567] | 1 | using System;
|
---|
| 2 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 3 | using HeuristicLab.Persistence.Core;
|
---|
| 4 | using System.Collections.Generic;
|
---|
| 5 | using System.Reflection;
|
---|
| 6 | using System.Globalization;
|
---|
| 7 | using System.Text;
|
---|
[1823] | 8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1567] | 9 |
|
---|
[1823] | 10 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
[1567] | 11 |
|
---|
| 12 | [EmptyStorableClass]
|
---|
[1823] | 13 | public class CompactNumberArray2StringSerializer : ICompositeSerializer {
|
---|
[1567] | 14 |
|
---|
| 15 | public int Priority {
|
---|
| 16 | get { return 200; }
|
---|
| 17 | }
|
---|
| 18 |
|
---|
[1823] | 19 | private static readonly Number2StringSerializer numberConverter =
|
---|
| 20 | new Number2StringSerializer();
|
---|
[1567] | 21 |
|
---|
[1823] | 22 | public bool CanSerialize(Type type) {
|
---|
[1567] | 23 | return
|
---|
| 24 | (type.IsArray || type == typeof(Array)) &&
|
---|
[1823] | 25 | numberConverter.CanSerialize(type.GetElementType());
|
---|
[1567] | 26 | }
|
---|
| 27 |
|
---|
| 28 | public IEnumerable<Tag> CreateMetaInfo(object obj) {
|
---|
| 29 | Array a = (Array)obj;
|
---|
| 30 | int[] lengths = new int[a.Rank];
|
---|
| 31 | int[] lowerBounds = new int[a.Rank];
|
---|
| 32 | StringBuilder sb = new StringBuilder();
|
---|
| 33 | sb.Append(a.Rank).Append(';');
|
---|
| 34 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 35 | sb.Append(a.GetLength(i)).Append(';');
|
---|
| 36 | lengths[i] = a.GetLength(i);
|
---|
| 37 | }
|
---|
| 38 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 39 | sb.Append(a.GetLowerBound(i)).Append(';');
|
---|
| 40 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
| 41 | }
|
---|
| 42 | int[] positions = (int[])lowerBounds.Clone();
|
---|
| 43 | while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
|
---|
| 44 | sb.Append(numberConverter.Format(a.GetValue(positions))).Append(';');
|
---|
| 45 | positions[0] += 1;
|
---|
| 46 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
| 47 | if (positions[i] >= lengths[i] + lowerBounds[i]) {
|
---|
| 48 | positions[i] = lowerBounds[i];
|
---|
| 49 | positions[i + 1] += 1;
|
---|
| 50 | } else {
|
---|
| 51 | break;
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | yield return new Tag("compact array", sb.ToString());
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
| 59 | return new Tag[] { };
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
[1625] | 63 | try {
|
---|
| 64 | var tagIter = metaInfo.GetEnumerator();
|
---|
[1703] | 65 | tagIter.MoveNext();
|
---|
[1625] | 66 | var valueIter = ((string)tagIter.Current.Value)
|
---|
| 67 | .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
|
---|
| 68 | .GetEnumerator();
|
---|
[1567] | 69 | valueIter.MoveNext();
|
---|
[1625] | 70 | int rank = int.Parse((string)valueIter.Current);
|
---|
| 71 | int[] lengths = new int[rank];
|
---|
| 72 | int[] lowerBounds = new int[rank];
|
---|
| 73 | for (int i = 0; i < rank; i++) {
|
---|
| 74 | valueIter.MoveNext();
|
---|
| 75 | lengths[i] = int.Parse((string)valueIter.Current);
|
---|
| 76 | }
|
---|
| 77 | for (int i = 0; i < rank; i++) {
|
---|
| 78 | valueIter.MoveNext();
|
---|
| 79 | lowerBounds[i] = int.Parse((string)valueIter.Current);
|
---|
| 80 | }
|
---|
| 81 | Type elementType = type.GetElementType();
|
---|
| 82 | Array a = Array.CreateInstance(elementType, lengths, lowerBounds);
|
---|
| 83 | int[] positions = (int[])lowerBounds.Clone();
|
---|
| 84 | while (valueIter.MoveNext()) {
|
---|
| 85 | a.SetValue(
|
---|
| 86 | numberConverter.Parse((string)valueIter.Current, elementType),
|
---|
| 87 | positions);
|
---|
| 88 | positions[0] += 1;
|
---|
| 89 | for (int i = 0; i < rank - 1; i++) {
|
---|
| 90 | if (positions[i] >= lengths[i] + lowerBounds[i]) {
|
---|
| 91 | positions[i + 1] += 1;
|
---|
| 92 | positions[i] = lowerBounds[i];
|
---|
| 93 | } else {
|
---|
| 94 | break;
|
---|
| 95 | }
|
---|
[1567] | 96 | }
|
---|
| 97 | }
|
---|
[1625] | 98 | return a;
|
---|
| 99 | } catch (InvalidOperationException e) {
|
---|
| 100 | throw new PersistenceException("Insufficient data to deserialize compact array", e);
|
---|
| 101 | } catch (InvalidCastException e) {
|
---|
| 102 | throw new PersistenceException("Invalid element data during compact array deserialization", e);
|
---|
[1703] | 103 | }
|
---|
[1567] | 104 | }
|
---|
| 105 |
|
---|
| 106 | public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
[1625] | 107 | // Nothing to do: Compact arrays are already populated during instance creation.
|
---|
[1567] | 108 | }
|
---|
| 109 |
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | } |
---|