- Timestamp:
- 08/02/10 17:03:53 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/CompactNumberArray2StringSerializer.cs
r4068 r4132 21 21 22 22 using System; 23 using System.Collections; 23 24 using System.Collections.Generic; 24 25 using System.Text; … … 32 33 [StorableClass] 33 34 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 } 34 82 35 83 public const int SPLIT_THRESHOLD = 1024 * 1024; … … 69 117 lowerBounds[i] = a.GetLowerBound(i); 70 118 } 71 yield return new Tag(sb.ToString());72 119 int nElements = 1; 73 120 for (int i = 0; i < a.Rank; i++) { … … 76 123 nElements *= lengths[i]; 77 124 } 78 sb = new StringBuilder(Math.Min(nElements * 3, SPLIT_THRESHOLD));125 sb.Capacity += Math.Min(nElements * 3, SPLIT_THRESHOLD); 79 126 int[] positions = (int[])lowerBounds.Clone(); 80 127 while (positions[a.Rank - 1] < lengths[a.Rank - 1] + lowerBounds[a.Rank - 1]) { … … 105 152 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 106 153 try { 107 var tagIter = metaInfo.GetEnumerator(); 108 tagIter.MoveNext(); 109 var valueIter = ((string)tagIter.Current.Value).GetSplitEnumerator(';'); 154 var valueIter = new ElementEnumerator(metaInfo); 110 155 valueIter.MoveNext(); 111 156 int rank = int.Parse(valueIter.Current); … … 124 169 if (a == null) throw new PersistenceException("invalid instance data type, expected array"); 125 170 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; 138 181 } 139 182 } 140 183 } 141 184 return a; 142 } 143 catch (InvalidOperationException e) { 185 } catch (InvalidOperationException e) { 144 186 throw new PersistenceException("Insuffictient data to deserialize compact array", e); 145 } 146 catch (InvalidCastException e) { 187 } catch (InvalidCastException e) { 147 188 throw new PersistenceException("Invalid element data during compact array deserialization", e); 148 189 }
Note: See TracChangeset
for help on using the changeset viewer.