Changeset 3937 for trunk/sources
- Timestamp:
- 06/21/10 18:46:04 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3
- Files:
-
- 1 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/TypeMapping.cs
r3743 r3937 65 65 /// <returns>A dictionary containing all properties 66 66 /// and values of this instance.</returns> 67 public Dictionary<string, object> GetDict() {68 return new Dictionary<string, object> {69 {"id", Id },67 public Dictionary<string, string> GetDict() { 68 return new Dictionary<string, string> { 69 {"id", Id.ToString()}, 70 70 {"typeName", TypeName}, 71 71 {"serializer", Serializer}}; -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/CompactNumberArray2StringSerializer.cs
r3742 r3937 21 21 22 22 using System; 23 using System.Linq; 23 24 using HeuristicLab.Persistence.Interfaces; 24 25 using HeuristicLab.Persistence.Core; … … 28 29 using System.Text; 29 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Persistence.Auxiliary; 30 32 31 33 namespace HeuristicLab.Persistence.Default.CompositeSerializers { … … 58 60 int[] lengths = new int[a.Rank]; 59 61 int[] lowerBounds = new int[a.Rank]; 60 StringBuilder sb = new StringBuilder( );62 StringBuilder sb = new StringBuilder(a.Rank * 3); 61 63 sb.Append(a.Rank).Append(';'); 64 int capacity = 1; 62 65 for (int i = 0; i < a.Rank; i++) { 63 66 sb.Append(a.GetLength(i)).Append(';'); 64 67 lengths[i] = a.GetLength(i); 68 capacity *= lengths[i]; 65 69 } 70 sb.EnsureCapacity(capacity * 3); 66 71 for (int i = 0; i < a.Rank; i++) { 67 72 sb.Append(a.GetLowerBound(i)).Append(';'); … … 92 97 var tagIter = metaInfo.GetEnumerator(); 93 98 tagIter.MoveNext(); 94 var valueIter = ((string)tagIter.Current.Value) 95 .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) 96 .GetEnumerator(); 99 var valueIter = ((string)tagIter.Current.Value).GetSplitEnumerator(';'); 97 100 valueIter.MoveNext(); 98 101 int rank = int.Parse((string)valueIter.Current); -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/NumberEnumerable2StringSerializer.cs
r3742 r3937 93 93 } 94 94 95 private static object[] emptyArgs = new object[0]; 95 96 public IEnumerable<Tag> Decompose(object obj) { 96 97 Type type = obj.GetType(); … … 102 103 iMap.InterfaceMethods, 103 104 enumerable.GetMethod("GetEnumerator"))]; 104 object[] empty = new object[] { }; 105 object genericEnumerator = getEnumeratorMethod.Invoke(obj, empty); 105 object genericEnumerator = getEnumeratorMethod.Invoke(obj, emptyArgs); 106 106 MethodInfo moveNextMethod = genericEnumerator.GetType().GetMethod("MoveNext"); 107 107 PropertyInfo currentProperty = genericEnumerator.GetType().GetProperty("Current"); 108 108 StringBuilder sb = new StringBuilder(); 109 while ((bool)moveNextMethod.Invoke(genericEnumerator, empty ))109 while ((bool)moveNextMethod.Invoke(genericEnumerator, emptyArgs)) 110 110 sb.Append( 111 111 numberConverter.Format( -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleList2XmlSerializer.cs
r3913 r3937 27 27 using HeuristicLab.Persistence.Default.Xml.Primitive; 28 28 using System.Text; 29 using HeuristicLab.Persistence.Auxiliary; 29 30 30 31 namespace HeuristicLab.Persistence.Default.Xml.Compact { … … 32 33 internal sealed class DoubleList2XmlSerializer : CompactXmlSerializerBase<List<double>> { 33 34 34 private static readonly char[] separators = new char[] { ';' };35 36 35 public override XmlString Format(List<double> list) { 37 StringBuilder sb = new StringBuilder( );36 StringBuilder sb = new StringBuilder(list.Count * 3); 38 37 foreach (var d in list) { 39 38 sb.Append(Double2XmlSerializer.FormatG17(d)).Append(';'); … … 44 43 public override List<double> Parse(XmlString data) { 45 44 try { 46 var values = data.Data.Split(separators, StringSplitOptions.RemoveEmptyEntries); 47 List<double> list = new List<double>(values.Length); 48 foreach (var value in values) { 45 List<double> list = new List<double>(); 46 foreach (var value in data.Data.EnumerateSplit(';')) { 49 47 list.Add(Double2XmlSerializer.ParseG17(value)); 50 48 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlSerializerBase.cs
r3742 r3937 26 26 using HeuristicLab.Persistence.Core; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Persistence.Auxiliary; 28 29 29 30 namespace HeuristicLab.Persistence.Default.Xml.Compact { … … 32 33 internal abstract class NumberArray2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : class { 33 34 34 protected virtual string Separator { get { return ";"; } }35 protected virtual char Separator { get { return ';'; } } 35 36 protected abstract string FormatValue(object o); 36 37 protected abstract object ParseValue(string o); … … 40 41 int[] lengths = new int[a.Rank]; 41 42 int[] lowerBounds = new int[a.Rank]; 42 StringBuilder sb = new StringBuilder( );43 StringBuilder sb = new StringBuilder(a.Rank * 3); 43 44 sb.Append(a.Rank); 45 int capacity = 1; 44 46 for (int i = 0; i < a.Rank; i++) { 45 47 sb.Append(Separator); 46 48 sb.Append(a.GetLength(i)); 47 49 lengths[i] = a.GetLength(i); 50 capacity *= lengths[i]; 48 51 } 52 sb.EnsureCapacity(capacity * 3); 49 53 for (int i = 0; i < a.Rank; i++) { 50 54 sb.Append(Separator); … … 71 75 public override T Parse(XmlString x) { 72 76 try { 73 IEnumerator values = 74 x.Data.Split(new[] { Separator }, 75 StringSplitOptions.RemoveEmptyEntries).GetEnumerator(); 77 IEnumerator values = x.Data.GetSplitEnumerator(Separator); 76 78 values.MoveNext(); 77 79 int rank = int.Parse((string)values.Current); -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberEnumeration2XmlSerializerBase.cs
r3742 r3937 26 26 using HeuristicLab.Persistence.Core; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Persistence.Auxiliary; 28 29 29 30 namespace HeuristicLab.Persistence.Default.Xml.Compact { … … 32 33 internal abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable { 33 34 34 protected virtual string Separator { get { return ";"; } }35 protected virtual char Separator { get { return ';'; } } 35 36 protected abstract void Add(IEnumerable enumeration, object o); 36 37 protected abstract IEnumerable Instantiate(); … … 50 51 try { 51 52 IEnumerable enumeration = Instantiate(); 52 string[] values = x.Data.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries); 53 foreach (var value in values) { 53 foreach (var value in x.Data.EnumerateSplit(Separator)) { 54 54 Add(enumeration, ParseValue(value)); 55 55 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/EasyXmlGenerator.cs
r3935 r3937 49 49 /// <returns>The token in serialized form.</returns> 50 50 protected override string Format(BeginToken beginToken) { 51 var dict = new Dictionary<string, object> {51 var dict = new Dictionary<string, string> { 52 52 {"name", beginToken.Name}, 53 {"id", beginToken.Id }};54 return CreateNodeStart(typeCache[beginToken.TypeId], dict); 53 {"id", beginToken.Id.ToString()}}; 54 return CreateNodeStart(typeCache[beginToken.TypeId], dict); 55 55 } 56 56 … … 70 70 /// <returns>The token in serialized form.</returns> 71 71 protected override string Format(PrimitiveToken dataToken) { 72 var dict = new Dictionary<string, object> {72 var dict = new Dictionary<string, string> { 73 73 {"name", dataToken.Name}, 74 {"id", dataToken.Id }};74 {"id", dataToken.Id.ToString()}}; 75 75 return CreateNode(typeCache[dataToken.TypeId], dict, 76 76 ((XmlString)dataToken.SerialData).Data); … … 84 84 protected override string Format(ReferenceToken refToken) { 85 85 return CreateNode(refToken.Id.ToString(), 86 new Dictionary<string, object> {86 new Dictionary<string, string> { 87 87 {"name", refToken.Name}}); 88 88 } … … 95 95 protected override string Format(NullReferenceToken nullRefToken) { 96 96 return CreateNode(XmlStringConstants.NULL, 97 new Dictionary<string, object>{97 new Dictionary<string, string>{ 98 98 {"name", nullRefToken.Name}}); 99 99 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r3935 r3937 23 23 using System; 24 24 using System.Text; 25 using System.Linq; 25 26 using HeuristicLab.Persistence.Interfaces; 26 27 using HeuristicLab.Persistence.Core; … … 63 64 protected enum NodeType { Start, End, Inline } ; 64 65 65 protected static void AddXmlTagContent(StringBuilder sb, string name, Dictionary<string, object> attributes) {66 protected static void AddXmlTagContent(StringBuilder sb, string name, Dictionary<string, string> attributes) { 66 67 sb.Append(name); 67 68 foreach (var attribute in attributes) { … … 76 77 } 77 78 78 protected static void AddXmlStartTag(StringBuilder sb, string name, Dictionary<string, object> attributes) { 79 protected static int AttributeLength(Dictionary<string, string> attributes) { 80 return attributes 81 .Where(kvp => !string.IsNullOrEmpty(kvp.Key) && !string.IsNullOrEmpty(kvp.Value)) 82 .Select(kvp => kvp.Key.Length + kvp.Value.Length + 4).Sum(); 83 } 84 85 protected static void AddXmlStartTag(StringBuilder sb, string name, Dictionary<string, string> attributes) { 79 86 sb.Append('<'); 80 87 AddXmlTagContent(sb, name, attributes); … … 82 89 } 83 90 84 protected static void AddXmlInlineTag(StringBuilder sb, string name, Dictionary<string, object> attributes) {91 protected static void AddXmlInlineTag(StringBuilder sb, string name, Dictionary<string, string> attributes) { 85 92 sb.Append('<'); 86 93 AddXmlTagContent(sb, name, attributes); … … 94 101 } 95 102 96 protected string CreateNodeStart(string name, Dictionary<string, object> attributes) { 97 StringBuilder sb = new StringBuilder(); 103 protected string CreateNodeStart(string name, Dictionary<string, string> attributes) { 104 StringBuilder sb = new StringBuilder(prefix.Length + name.Length + 4 105 + AttributeLength(attributes)); 98 106 sb.Append(prefix); 99 107 Depth += 1; … … 103 111 } 104 112 113 private static Dictionary<string, string> emptyDict = new Dictionary<string, string>(); 105 114 protected string CreateNodeStart(string name) { 106 return CreateNodeStart(name, new Dictionary<string, object>());115 return CreateNodeStart(name, emptyDict); 107 116 } 108 117 109 118 protected string CreateNodeEnd(string name) { 110 119 Depth -= 1; 111 StringBuilder sb = new StringBuilder( );120 StringBuilder sb = new StringBuilder(prefix.Length + name.Length + 5); 112 121 sb.Append(prefix); 113 122 AddXmlEndTag(sb, name); … … 116 125 } 117 126 118 protected string CreateNode(string name, Dictionary<string, object> attributes) { 119 StringBuilder sb = new StringBuilder(); 127 protected string CreateNode(string name, Dictionary<string, string> attributes) { 128 StringBuilder sb = new StringBuilder(prefix.Length + name.Length + 5 129 + AttributeLength(attributes)); 120 130 sb.Append(prefix); 121 131 AddXmlInlineTag(sb, name, attributes); … … 124 134 } 125 135 126 protected string CreateNode(string name, Dictionary<string, object> attributes, string content) {136 protected string CreateNode(string name, Dictionary<string, string> attributes, string content) { 127 137 StringBuilder sb = new StringBuilder(); 128 138 sb.Append(prefix); … … 139 149 /// <returns>The token in serialized form.</returns> 140 150 protected override string Format(BeginToken beginToken) { 141 var dict = new Dictionary<string, object> {151 var dict = new Dictionary<string, string> { 142 152 {"name", beginToken.Name}, 143 {"typeId", beginToken.TypeId },144 {"id", beginToken.Id }};153 {"typeId", beginToken.TypeId.ToString()}, 154 {"id", beginToken.Id.ToString()}}; 145 155 AddTypeInfo(beginToken.TypeId, dict); 146 156 return CreateNodeStart(XmlStringConstants.COMPOSITE, dict); … … 148 158 } 149 159 150 protected void AddTypeInfo(int typeId, Dictionary<string, object> dict) {160 protected void AddTypeInfo(int typeId, Dictionary<string, string> dict) { 151 161 if (lastTypeToken != null) { 152 162 if (typeId == lastTypeToken.Id) { … … 175 185 /// <returns>The token in serialized form.</returns> 176 186 protected override string Format(PrimitiveToken dataToken) { 177 var dict = new Dictionary<string, object> {178 {"typeId", dataToken.TypeId },187 var dict = new Dictionary<string, string> { 188 {"typeId", dataToken.TypeId.ToString()}, 179 189 {"name", dataToken.Name}, 180 {"id", dataToken.Id }};190 {"id", dataToken.Id.ToString()}}; 181 191 AddTypeInfo(dataToken.TypeId, dict); 182 192 return CreateNode(XmlStringConstants.PRIMITIVE, dict, … … 191 201 protected override string Format(ReferenceToken refToken) { 192 202 return CreateNode(XmlStringConstants.REFERENCE, 193 new Dictionary<string, object> {194 {"ref", refToken.Id },203 new Dictionary<string, string> { 204 {"ref", refToken.Id.ToString()}, 195 205 {"name", refToken.Name}}); 196 206 } … … 203 213 protected override string Format(NullReferenceToken nullRefToken) { 204 214 return CreateNode(XmlStringConstants.NULL, 205 new Dictionary<string, object>{215 new Dictionary<string, string>{ 206 216 {"name", nullRefToken.Name}}); 207 217 } … … 241 251 try { 242 252 return CreateNode(XmlStringConstants.TYPE, 243 new Dictionary<string, object> {244 {"id", lastTypeToken.Id },253 new Dictionary<string, string> { 254 {"id", lastTypeToken.Id.ToString()}, 245 255 {"typeName", lastTypeToken.TypeName }, 246 256 {"serializer", lastTypeToken.Serializer }}); -
trunk/sources/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj
r3935 r3937 98 98 </ItemGroup> 99 99 <ItemGroup> 100 <Compile Include="Auxiliary\StringExtensions.cs" /> 100 101 <Compile Include="Auxiliary\TypeLoader.cs" /> 101 102 <Compile Include="Auxiliary\TypeNameParser.cs" /> -
trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs
r3935 r3937 1065 1065 } 1066 1066 1067 [TestMethod] 1068 public void TestStringSplit() { 1069 string s = "1.2;2.3;3.4;;;4.9"; 1070 var l = s.EnumerateSplit(';').ToList(); 1071 Assert.AreEqual("1.2", l[0]); 1072 Assert.AreEqual("2.3", l[1]); 1073 Assert.AreEqual("3.4", l[2]); 1074 Assert.AreEqual("4.9", l[3]); 1075 } 1076 1067 1077 1068 1078 [ClassInitialize]
Note: See TracChangeset
for help on using the changeset viewer.