1 | using System;
|
---|
2 | using HeuristicLab.Persistence.Core;
|
---|
3 | using HeuristicLab.Persistence.Interfaces;
|
---|
4 | using System.Collections.Generic;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Persistence.Default.Decomposers {
|
---|
7 |
|
---|
8 | public class ArrayDecomposer : IDecomposer {
|
---|
9 |
|
---|
10 | public int Priority {
|
---|
11 | get { return 100; }
|
---|
12 | }
|
---|
13 |
|
---|
14 | public bool CanDecompose(Type type) {
|
---|
15 | return type.IsArray || type == typeof(Array);
|
---|
16 | }
|
---|
17 |
|
---|
18 | public IEnumerable<Tag> CreateMetaInfo(object obj) {
|
---|
19 | Array a = (Array)obj;
|
---|
20 | yield return new Tag("rank", a.Rank);
|
---|
21 | for (int i = 0; i < a.Rank; i++) {
|
---|
22 | yield return new Tag("length_" + i, a.GetLength(i));
|
---|
23 | }
|
---|
24 | for (int i = 0; i < a.Rank; i++) {
|
---|
25 | yield return new Tag("lowerBound_" + i, a.GetLowerBound(i));
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | public IEnumerable<Tag> Decompose(object array) {
|
---|
30 | Array a = (Array)array;
|
---|
31 | int[] lengths = new int[a.Rank];
|
---|
32 | int[] lowerBounds = new int[a.Rank];
|
---|
33 | for (int i = 0; i < a.Rank; i++) {
|
---|
34 | lengths[i] = a.GetLength(i);
|
---|
35 | }
|
---|
36 | for (int i = 0; i < a.Rank; i++) {
|
---|
37 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
38 | }
|
---|
39 | int[] positions = (int[])lowerBounds.Clone();
|
---|
40 | while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
|
---|
41 | yield return new Tag(a.GetValue(positions));
|
---|
42 | positions[0] += 1;
|
---|
43 | for (int i = 0; i < a.Rank - 1; i++) {
|
---|
44 | if (positions[i] >= lowerBounds[i] + lengths[i]) {
|
---|
45 | positions[i] = lowerBounds[i];
|
---|
46 | positions[i + 1] += 1;
|
---|
47 | } else {
|
---|
48 | break;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
|
---|
55 | IEnumerator<Tag> e = metaInfo.GetEnumerator();
|
---|
56 | e.MoveNext();
|
---|
57 | int rank = (int)e.Current.Value;
|
---|
58 | int[] lengths = new int[rank];
|
---|
59 | for (int i = 0; i < rank; i++) {
|
---|
60 | e.MoveNext();
|
---|
61 | lengths[i] = (int)e.Current.Value;
|
---|
62 | }
|
---|
63 | int[] lowerBounds = new int[rank];
|
---|
64 | for (int i = 0; i < rank; i++) {
|
---|
65 | e.MoveNext();
|
---|
66 | lowerBounds[i] = (int)e.Current.Value;
|
---|
67 | }
|
---|
68 | return Array.CreateInstance(t.GetElementType(), lengths, lowerBounds);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
|
---|
72 | Array a = (Array)instance;
|
---|
73 | int[] lengths = new int[a.Rank];
|
---|
74 | int[] lowerBounds = new int[a.Rank];
|
---|
75 | for (int i = 0; i < a.Rank; i++) {
|
---|
76 | lengths[i] = a.GetLength(i);
|
---|
77 | }
|
---|
78 | for (int i = 0; i < a.Rank; i++) {
|
---|
79 | lowerBounds[i] = a.GetLowerBound(i);
|
---|
80 | }
|
---|
81 | int[] positions = (int[])lowerBounds.Clone();
|
---|
82 | IEnumerator<Tag> e = elements.GetEnumerator();
|
---|
83 | while (e.MoveNext()) {
|
---|
84 | int[] currentPositions = positions;
|
---|
85 | a.SetValue(e.Current.Value, currentPositions);
|
---|
86 | positions[0] += 1;
|
---|
87 | for (int i = 0; i < a.Rank-1; i++) {
|
---|
88 | if (positions[i] >= lengths[i]+lowerBounds[i]) {
|
---|
89 | positions[i] = lowerBounds[i];
|
---|
90 | positions[i + 1] += 1;
|
---|
91 | } else {
|
---|
92 | break;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | }
|
---|