Free cookie consent management tool by TermsFeed Policy Generator

Changeset 4132


Ignore:
Timestamp:
08/02/10 17:03:53 (14 years ago)
Author:
epitzer
Message:

make optimized version of CompactNumberArray2StringSerializer compatible with released version (#1138, #1108)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/CompactNumberArray2StringSerializer.cs

    r4068 r4132  
    2121
    2222using System;
     23using System.Collections;
    2324using System.Collections.Generic;
    2425using System.Text;
     
    3233  [StorableClass]
    3334  public sealed class CompactNumberArray2StringSerializer : ICompositeSerializer {
     35
     36    private class ElementEnumerator : IEnumerator<string> {
     37
     38      private IEnumerator<Tag> tagIt;
     39      private IEnumerator<string> valIt;
     40
     41      public ElementEnumerator(IEnumerable<Tag> tags) {
     42        tagIt = tags.GetEnumerator();
     43      }
     44
     45      public string Current {
     46        get {
     47          if (valIt == null)
     48            throw new InvalidOperationException("no current value");
     49          return valIt.Current;
     50        }
     51      }
     52
     53      public void Dispose() {
     54        valIt.Dispose();
     55        valIt = null;
     56        tagIt.Dispose();
     57      }
     58
     59      object IEnumerator.Current {
     60        get { return this.Current; }
     61      }
     62
     63      public bool MoveNext() {
     64        if (valIt != null && valIt.MoveNext())
     65          return true;
     66        if (tagIt.MoveNext()) {
     67          if (valIt != null)
     68            valIt.Dispose();
     69          valIt = ((string)tagIt.Current.Value).GetSplitEnumerator(';');
     70          return MoveNext();
     71        }
     72        valIt.Dispose();
     73        valIt = null;
     74        return false;
     75      }
     76
     77      public void Reset() {
     78        valIt.Dispose();
     79        tagIt.Reset();
     80      }
     81    }
    3482
    3583    public const int SPLIT_THRESHOLD = 1024 * 1024;
     
    69117        lowerBounds[i] = a.GetLowerBound(i);
    70118      }
    71       yield return new Tag(sb.ToString());
    72119      int nElements = 1;
    73120      for (int i = 0; i < a.Rank; i++) {
     
    76123        nElements *= lengths[i];
    77124      }
    78       sb = new StringBuilder(Math.Min(nElements * 3, SPLIT_THRESHOLD));
     125      sb.Capacity += Math.Min(nElements * 3, SPLIT_THRESHOLD);
    79126      int[] positions = (int[])lowerBounds.Clone();
    80127      while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) {
     
    105152    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    106153      try {
    107         var tagIter = metaInfo.GetEnumerator();
    108         tagIter.MoveNext();
    109         var valueIter = ((string)tagIter.Current.Value).GetSplitEnumerator(';');
     154        var valueIter = new ElementEnumerator(metaInfo);
    110155        valueIter.MoveNext();
    111156        int rank = int.Parse(valueIter.Current);
     
    124169        if (a == null) throw new PersistenceException("invalid instance data type, expected array");
    125170        int[] positions = (int[])lowerBounds.Clone();
    126         while (tagIter.MoveNext()) {
    127           valueIter = ((string)tagIter.Current.Value).GetSplitEnumerator(';');
    128           while (valueIter.MoveNext()) {
    129             a.SetValue(numberConverter.Parse(valueIter.Current, elementType), positions);
    130             positions[0] += 1;
    131             for (int i = 0; i < a.Rank - 1; i++) {
    132               if (positions[i] >= lengths[i] + lowerBounds[i]) {
    133                 positions[i + 1] += 1;
    134                 positions[i] = lowerBounds[i];
    135               } else {
    136                 break;
    137               }
     171
     172        while (valueIter.MoveNext()) {
     173          a.SetValue(numberConverter.Parse(valueIter.Current, elementType), positions);
     174          positions[0] += 1;
     175          for (int i = 0; i < a.Rank - 1; i++) {
     176            if (positions[i] >= lengths[i] + lowerBounds[i]) {
     177              positions[i + 1] += 1;
     178              positions[i] = lowerBounds[i];
     179            } else {
     180              break;
    138181            }
    139182          }
    140183        }
    141184        return a;
    142       }
    143       catch (InvalidOperationException e) {
     185      } catch (InvalidOperationException e) {
    144186        throw new PersistenceException("Insuffictient data to deserialize compact array", e);
    145       }
    146       catch (InvalidCastException e) {
     187      } catch (InvalidCastException e) {
    147188        throw new PersistenceException("Invalid element data during compact array deserialization", e);
    148189      }
Note: See TracChangeset for help on using the changeset viewer.