#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Linq; using HeuristicLab.Persistence.Data; namespace HeuristicLab.Persistence { internal abstract class GenericArrayTransformer : TransformerBase { public override bool CanTransformType(Type type) { return type.IsArray && (type.GetElementType() == typeof(TSource)); } public override PersistenceData ToData(object o, PersistenceMapper mapper) { var arr = (Array)o; var data = new ArrayData(Id); mapper.Cache(o, data); var elementType = arr.GetType().GetElementType(); data.ElementTypeId = mapper.GetTypeId(elementType); if (arr.Rank == 1) { data.Values = GetTargets((TSource[])o, mapper); //data.Values = GetTargets(Array.ConvertAll(arr, x => (object)x), mapper); //data.Values = GetTargets(arr.Cast().ToArray(), mapper); return data; } else { data.Lengths = new int[arr.Rank]; int[] positions = new int[arr.Rank]; for (int i = 0; i < arr.Rank; i++) { data.Lengths[i] = arr.GetLength(i); positions[i] = 0; } data.Values = new TTarget[arr.Length]; for (int i = 0; i < data.Values.Length; i++) { data.Values[i] = GetTarget((TSource)arr.GetValue(positions), mapper); positions[0] += 1; for (int j = 0; j < arr.Rank - 1; j++) { if (positions[j] >= data.Lengths[j]) { positions[j] = 0; positions[j + 1] += 1; } else { break; } } } return data; } } public override object ToObject(PersistenceData data, PersistenceMapper mapper) { var arrayData = (ArrayData)data; var elementType = mapper.GetType(arrayData.ElementTypeId); if (arrayData.Lengths.Length <= 1) { // single dimensional var o = GetSources(arrayData.Values, mapper); mapper.Cache(data, o); return o; } else { var o = Array.CreateInstance(elementType, arrayData.Lengths); mapper.Cache(data, o); int[] positions = new int[arrayData.Lengths.Length]; positions.Initialize(); for (int i = 0; i < arrayData.Values.Length; i++) { o.SetValue(GetSource(arrayData.Values[i], mapper), positions); positions[0] += 1; for (int j = 0; j < o.Rank - 1; j++) { if (positions[j] >= arrayData.Lengths[j]) { positions[j] = 0; positions[j + 1] += 1; } else { break; } } } return o; } } protected abstract TTarget[] GetTargets(TSource[] sources, PersistenceMapper mapper); protected abstract TSource[] GetSources(TTarget[] targets, PersistenceMapper mapper); protected abstract TTarget GetTarget(TSource source, PersistenceMapper mapper); protected abstract TSource GetSource(TTarget target, PersistenceMapper mapper); } internal abstract class PrimitiveArrayTransformer : GenericArrayTransformer { protected override T[] GetTargets(T[] sources, PersistenceMapper mapper) { return sources; } protected override T[] GetSources(T[] targets, PersistenceMapper mapper) { return targets; } protected override T GetTarget(T source, PersistenceMapper mapper) { return source; } protected override T GetSource(T target, PersistenceMapper mapper) { return target; } } [Transformer("F25A73B2-6B67-4493-BD59-B836AF4455D1", 300)] internal sealed class BoolArrayTransformer : PrimitiveArrayTransformer { } [Transformer("FF89F6D1-CDE3-498E-9166-F70AC6EB01F1", 301)] internal sealed class ByteArrayTransformer : PrimitiveArrayTransformer { } [Transformer("B49B3F2D-2E97-4BAB-8705-8D29DA707C6A", 302)] internal sealed class SByteArrayTransformer : PrimitiveArrayTransformer { } [Transformer("2811FDD4-6800-4CBA-86D7-9071ED5775ED", 303)] internal sealed class ShortArrayTransformer : PrimitiveArrayTransformer { } [Transformer("1AAC2625-356C-40BC-8CB4-15CB3D047EB8", 304)] internal sealed class UShortArrayTransformer : PrimitiveArrayTransformer { } [Transformer("12F19098-5D49-4C23-8897-69087F1C146D", 305)] internal sealed class CharArrayTransformer : PrimitiveArrayTransformer { } [Transformer("5F6DC3BC-4433-4AE9-A636-4BD126F7DACD", 306)] internal sealed class IntArrayTransformer : PrimitiveArrayTransformer { } [Transformer("3F10274F-D350-4C82-89EA-A5EB36D4EFCC", 307)] internal sealed class UIntArrayTransformer : PrimitiveArrayTransformer { } [Transformer("E9D550E2-57F7-47F3-803D-37A619DA1A5C", 308)] internal sealed class LongArrayTransformer : PrimitiveArrayTransformer { } [Transformer("C02A205B-2176-4282-AC2B-ADEF96DDBE24", 309)] internal sealed class ULongArrayTransformer : PrimitiveArrayTransformer { } [Transformer("3C4590D9-C76E-4AFB-98FD-E50D3D051648", 310)] internal sealed class FloatArrayTransformer : PrimitiveArrayTransformer { } [Transformer("FB98C399-9323-4470-9A85-9186C2B2D5D4", 311)] internal sealed class DoubleArrayTransformer : PrimitiveArrayTransformer { } [Transformer("9E68A52B-2EC8-447E-B45B-D382A16283D4", 312)] internal sealed class DecimalArrayTransformer : PrimitiveArrayTransformer { } [Transformer("68332513-9CF1-47FA-A093-6DDB663186EC", 313)] internal sealed class StringArrayTransformer : PrimitiveArrayTransformer { } [Transformer("C83F0B5A-68D8-4271-81F9-FF259FC6F126", 400)] internal sealed class ArrayTransformer : GenericArrayTransformer { public override bool CanTransformType(Type type) { return type.IsArray; } protected override uint[] GetTargets(object[] sources, PersistenceMapper mapper) { return sources.Select(x => mapper.GetDataId(x)).ToArray(); } protected override object[] GetSources(uint[] targets, PersistenceMapper mapper) { return targets.Select(x => mapper.GetObject(x)).ToArray(); } protected override uint GetTarget(object source, PersistenceMapper mapper) { return mapper.GetDataId(source); } protected override object GetSource(uint target, PersistenceMapper mapper) { return mapper.GetObject(target); } } }