[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[1454] | 24 | using HeuristicLab.Persistence.Core;
|
---|
[4068] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1454] | 26 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 27 |
|
---|
[1823] | 28 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
[1566] | 29 |
|
---|
[3017] | 30 | [StorableClass]
|
---|
[3036] | 31 | internal sealed class ArraySerializer : ICompositeSerializer {
|
---|
[1454] | 32 |
|
---|
[4806] | 33 | [StorableConstructor]
|
---|
| 34 | private ArraySerializer(bool deserializing) { }
|
---|
| 35 | public ArraySerializer() { }
|
---|
| 36 |
|
---|
[1539] | 37 | public int Priority {
|
---|
| 38 | get { return 100; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[1823] | 41 | public bool CanSerialize(Type type) {
|
---|
[1454] | 42 | return type.IsArray || type == typeof(Array);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[2993] | 45 | public string JustifyRejection(Type type) {
|
---|
| 46 | return "not an array and not of type System.Array";
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[1553] | 49 | public IEnumerable<Tag> CreateMetaInfo(object obj) {
|
---|
| 50 | Array a = (Array)obj;
|
---|
| 51 | yield return new Tag("rank", a.Rank);
|
---|
[1566] | 52 | for (int i = 0; i < a.Rank; i++) {
|
---|
[1553] | 53 | yield return new Tag("length_" + i, a.GetLength(i));
|
---|
| 54 | }
|
---|
[1566] | 55 | for (int i = 0; i < a.Rank; i++) {
|
---|
[1553] | 56 | yield return new Tag("lowerBound_" + i, a.GetLowerBound(i));
|
---|
[1566] | 57 | }
|
---|
[1553] | 58 | }
|
---|
| 59 |
|
---|
[1542] | 60 | public IEnumerable<Tag> Decompose(object array) {
|
---|
[1566] | 61 | Array a = (Array)array;
|
---|
[1518] | 62 | int[] lengths = new int[a.Rank];
|
---|
| 63 | int[] lowerBounds = new int[a.Rank];
|
---|
[1454] | 64 | for (int i = 0; i < a.Rank; i++) {
|
---|
[1518] | 65 | lengths[i] = a.GetLength(i);
|
---|
[1454] | 66 | }
|
---|
| 67 | for (int i = 0; i < a.Rank; i++) {
|
---|
[1518] | 68 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
[1454] | 69 | }
|
---|
[1518] | 70 | int[] positions = (int[])lowerBounds.Clone();
|
---|
| 71 | while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
|
---|
| 72 | yield return new Tag(a.GetValue(positions));
|
---|
| 73 | positions[0] += 1;
|
---|
| 74 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
| 75 | if (positions[i] >= lowerBounds[i] + lengths[i]) {
|
---|
| 76 | positions[i] = lowerBounds[i];
|
---|
| 77 | positions[i + 1] += 1;
|
---|
| 78 | } else {
|
---|
| 79 | break;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
[1454] | 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[1703] | 85 | public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
|
---|
[1625] | 86 | try {
|
---|
| 87 | IEnumerator<Tag> e = metaInfo.GetEnumerator();
|
---|
[1454] | 88 | e.MoveNext();
|
---|
[1625] | 89 | int rank = (int)e.Current.Value;
|
---|
| 90 | int[] lengths = new int[rank];
|
---|
| 91 | for (int i = 0; i < rank; i++) {
|
---|
| 92 | e.MoveNext();
|
---|
| 93 | lengths[i] = (int)e.Current.Value;
|
---|
| 94 | }
|
---|
| 95 | int[] lowerBounds = new int[rank];
|
---|
| 96 | for (int i = 0; i < rank; i++) {
|
---|
| 97 | e.MoveNext();
|
---|
| 98 | lowerBounds[i] = (int)e.Current.Value;
|
---|
| 99 | }
|
---|
| 100 | return Array.CreateInstance(t.GetElementType(), lengths, lowerBounds);
|
---|
[4068] | 101 | }
|
---|
| 102 | catch (InvalidOperationException x) {
|
---|
[1625] | 103 | throw new PersistenceException("Insufficient meta information to construct array instance.", x);
|
---|
[4068] | 104 | }
|
---|
| 105 | catch (InvalidCastException x) {
|
---|
[1625] | 106 | throw new PersistenceException("Invalid format of array metainfo.", x);
|
---|
[1703] | 107 | }
|
---|
[1553] | 108 | }
|
---|
| 109 |
|
---|
[1566] | 110 | public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
|
---|
[1553] | 111 | Array a = (Array)instance;
|
---|
| 112 | int[] lengths = new int[a.Rank];
|
---|
| 113 | int[] lowerBounds = new int[a.Rank];
|
---|
| 114 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 115 | lengths[i] = a.GetLength(i);
|
---|
| 116 | }
|
---|
| 117 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 118 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
[1566] | 119 | }
|
---|
[1463] | 120 | int[] positions = (int[])lowerBounds.Clone();
|
---|
[1553] | 121 | IEnumerator<Tag> e = elements.GetEnumerator();
|
---|
[1625] | 122 | try {
|
---|
| 123 | while (e.MoveNext()) {
|
---|
| 124 | int[] currentPositions = positions;
|
---|
| 125 | a.SetValue(e.Current.Value, currentPositions);
|
---|
| 126 | positions[0] += 1;
|
---|
| 127 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
| 128 | if (positions[i] >= lengths[i] + lowerBounds[i]) {
|
---|
| 129 | positions[i] = lowerBounds[i];
|
---|
| 130 | positions[i + 1] += 1;
|
---|
| 131 | } else {
|
---|
| 132 | break;
|
---|
| 133 | }
|
---|
[1454] | 134 | }
|
---|
| 135 | }
|
---|
[4068] | 136 | }
|
---|
| 137 | catch (InvalidOperationException x) {
|
---|
[1625] | 138 | throw new PersistenceException("Insufficient data to fill array instance", x);
|
---|
[4068] | 139 | }
|
---|
| 140 | catch (InvalidCastException x) {
|
---|
[1625] | 141 | throw new PersistenceException("Invalid element data. Cannot fill array", x);
|
---|
[4068] | 142 | }
|
---|
| 143 | catch (IndexOutOfRangeException x) {
|
---|
[1625] | 144 | throw new PersistenceException("Too many elements during array deserialization", x);
|
---|
[1454] | 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
[1566] | 148 |
|
---|
[1454] | 149 | }
|
---|