[1454] | 1 | using System;
|
---|
| 2 | using HeuristicLab.Persistence.Core;
|
---|
| 3 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 4 | using System.Collections.Generic;
|
---|
[1823] | 5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2993] | 6 | using System.Text;
|
---|
[1454] | 7 |
|
---|
[1823] | 8 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
[1566] | 9 |
|
---|
[3017] | 10 | [StorableClass]
|
---|
[3036] | 11 | internal sealed class ArraySerializer : ICompositeSerializer {
|
---|
[1454] | 12 |
|
---|
[1539] | 13 | public int Priority {
|
---|
| 14 | get { return 100; }
|
---|
| 15 | }
|
---|
| 16 |
|
---|
[1823] | 17 | public bool CanSerialize(Type type) {
|
---|
[1454] | 18 | return type.IsArray || type == typeof(Array);
|
---|
| 19 | }
|
---|
| 20 |
|
---|
[2993] | 21 | public string JustifyRejection(Type type) {
|
---|
| 22 | return "not an array and not of type System.Array";
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[1553] | 25 | public IEnumerable<Tag> CreateMetaInfo(object obj) {
|
---|
| 26 | Array a = (Array)obj;
|
---|
| 27 | yield return new Tag("rank", a.Rank);
|
---|
[1566] | 28 | for (int i = 0; i < a.Rank; i++) {
|
---|
[1553] | 29 | yield return new Tag("length_" + i, a.GetLength(i));
|
---|
| 30 | }
|
---|
[1566] | 31 | for (int i = 0; i < a.Rank; i++) {
|
---|
[1553] | 32 | yield return new Tag("lowerBound_" + i, a.GetLowerBound(i));
|
---|
[1566] | 33 | }
|
---|
[1553] | 34 | }
|
---|
| 35 |
|
---|
[1542] | 36 | public IEnumerable<Tag> Decompose(object array) {
|
---|
[1566] | 37 | Array a = (Array)array;
|
---|
[1518] | 38 | int[] lengths = new int[a.Rank];
|
---|
| 39 | int[] lowerBounds = new int[a.Rank];
|
---|
[1454] | 40 | for (int i = 0; i < a.Rank; i++) {
|
---|
[1518] | 41 | lengths[i] = a.GetLength(i);
|
---|
[1454] | 42 | }
|
---|
| 43 | for (int i = 0; i < a.Rank; i++) {
|
---|
[1518] | 44 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
[1454] | 45 | }
|
---|
[1518] | 46 | int[] positions = (int[])lowerBounds.Clone();
|
---|
| 47 | while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
|
---|
| 48 | yield return new Tag(a.GetValue(positions));
|
---|
| 49 | positions[0] += 1;
|
---|
| 50 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
| 51 | if (positions[i] >= lowerBounds[i] + lengths[i]) {
|
---|
| 52 | positions[i] = lowerBounds[i];
|
---|
| 53 | positions[i + 1] += 1;
|
---|
| 54 | } else {
|
---|
| 55 | break;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
[1454] | 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[1703] | 61 | public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
|
---|
[1625] | 62 | try {
|
---|
| 63 | IEnumerator<Tag> e = metaInfo.GetEnumerator();
|
---|
[1454] | 64 | e.MoveNext();
|
---|
[1625] | 65 | int rank = (int)e.Current.Value;
|
---|
| 66 | int[] lengths = new int[rank];
|
---|
| 67 | for (int i = 0; i < rank; i++) {
|
---|
| 68 | e.MoveNext();
|
---|
| 69 | lengths[i] = (int)e.Current.Value;
|
---|
| 70 | }
|
---|
| 71 | int[] lowerBounds = new int[rank];
|
---|
| 72 | for (int i = 0; i < rank; i++) {
|
---|
| 73 | e.MoveNext();
|
---|
| 74 | lowerBounds[i] = (int)e.Current.Value;
|
---|
| 75 | }
|
---|
| 76 | return Array.CreateInstance(t.GetElementType(), lengths, lowerBounds);
|
---|
| 77 | } catch (InvalidOperationException x) {
|
---|
| 78 | throw new PersistenceException("Insufficient meta information to construct array instance.", x);
|
---|
| 79 | } catch (InvalidCastException x) {
|
---|
| 80 | throw new PersistenceException("Invalid format of array metainfo.", x);
|
---|
[1703] | 81 | }
|
---|
[1553] | 82 | }
|
---|
| 83 |
|
---|
[1566] | 84 | public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
|
---|
[1553] | 85 | Array a = (Array)instance;
|
---|
| 86 | int[] lengths = new int[a.Rank];
|
---|
| 87 | int[] lowerBounds = new int[a.Rank];
|
---|
| 88 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 89 | lengths[i] = a.GetLength(i);
|
---|
| 90 | }
|
---|
| 91 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 92 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
[1566] | 93 | }
|
---|
[1463] | 94 | int[] positions = (int[])lowerBounds.Clone();
|
---|
[1553] | 95 | IEnumerator<Tag> e = elements.GetEnumerator();
|
---|
[1625] | 96 | try {
|
---|
| 97 | while (e.MoveNext()) {
|
---|
| 98 | int[] currentPositions = positions;
|
---|
| 99 | a.SetValue(e.Current.Value, currentPositions);
|
---|
| 100 | positions[0] += 1;
|
---|
| 101 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
| 102 | if (positions[i] >= lengths[i] + lowerBounds[i]) {
|
---|
| 103 | positions[i] = lowerBounds[i];
|
---|
| 104 | positions[i + 1] += 1;
|
---|
| 105 | } else {
|
---|
| 106 | break;
|
---|
| 107 | }
|
---|
[1454] | 108 | }
|
---|
| 109 | }
|
---|
[1625] | 110 | } catch (InvalidOperationException x) {
|
---|
| 111 | throw new PersistenceException("Insufficient data to fill array instance", x);
|
---|
| 112 | } catch (InvalidCastException x) {
|
---|
| 113 | throw new PersistenceException("Invalid element data. Cannot fill array", x);
|
---|
| 114 | } catch (IndexOutOfRangeException x) {
|
---|
| 115 | throw new PersistenceException("Too many elements during array deserialization", x);
|
---|
[1454] | 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
[1566] | 119 |
|
---|
[1454] | 120 | }
|
---|