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