public override int GetHashCode() { var builder = new StringBuilder(); builder.AppendLine(TypeDescriptor.Encode(Type)); foreach (var argument in Arguments) { builder.Append(String.Format("{0}=", argument.Name)); if (ValueFormat.GetFormat(argument.Value) == ValueFormats.Array) { var array = argument.Value as Array; if (array == null) { throw new ArgumentException(); } for (var i = 0; i < array.Length; i++) { if (i > 0) { builder.Append(","); } builder.Append(array.GetValue(i)); } } else { builder.Append(argument.Value); } builder.AppendLine(); } return(builder.ToString().GetHashCode()); }
public bool Equals(AnnotationArgument other) { return(Name.Equals(other.Name) && ValueFormat.GetFormat(Value).Equals(ValueFormat.GetFormat(other.Value)) && (((ValueFormat.GetFormat(Value) == ValueFormats.Array) && ArrayEquals(Value as Array, other.Value as Array)) || object.Equals(Value, other.Value))); }
public bool Equals(AnnotationArgument other) { if (other == null) { return(false); } return(Name.Equals(other.Name) && ValueFormat.GetFormat(Value).Equals(ValueFormat.GetFormat(other.Value)) && (ValueFormat.GetFormat(Value) == ValueFormats.Array && ArrayEquals(Value as Array, other.Value as Array) || Equals(Value, other.Value))); }
private int CompareValue(Object x, Object y) { var xf = ValueFormat.GetFormat(x); var yf = ValueFormat.GetFormat(y); int result = xf.CompareTo(yf); if (result != 0) { return(result); } switch (xf) { case ValueFormats.Byte: case ValueFormats.Short: case ValueFormats.Char: case ValueFormats.Int: case ValueFormats.Long: case ValueFormats.Float: case ValueFormats.Double: case ValueFormats.Boolean: case ValueFormats.String: return(((IComparable)x).CompareTo((IComparable)y)); case ValueFormats.Null: return(0); case ValueFormats.Type: return(new TypeReferenceComparer().Compare((TypeReference)x, (TypeReference)y)); case ValueFormats.Field: case ValueFormats.Enum: return(new FieldReferenceComparer().Compare((FieldReference)x, (FieldReference)y)); case ValueFormats.Method: return(new MethodReferenceComparer().Compare((MethodReference)x, (MethodReference)y)); case ValueFormats.Annotation: return(new AnnotationComparer().Compare((Annotation)x, (Annotation)y)); case ValueFormats.Array: var ax = (Array)x; var ay = (Array)y; for (int i = 0; i < Math.Min(ax.Length, ay.Length); i++) { result = CompareValue(ax.GetValue(i), ay.GetValue(i)); if (result != 0) { return(result); } } if (ax.Length > ay.Length) { return(1); } if (ay.Length > ax.Length) { return(-1); } return(0); default: throw new NotImplementedException(xf.ToString()); } }
private void WriteValue(BinaryWriter writer, object value) { var valueArgument = 0; var format = ValueFormat.GetFormat(value); switch (format) { case ValueFormats.Char: valueArgument = VerifyMaxEncodedValueSize(writer.GetByteCountForUnsignedPackedNumber(Convert.ToInt64(value)) - 1, 1, format); break; case ValueFormats.Byte: valueArgument = VerifyMaxEncodedValueSize(writer.GetByteCountForSignedPackedNumber(Convert.ToInt64(value)) - 1, 0, format); break; case ValueFormats.Short: valueArgument = VerifyMaxEncodedValueSize(writer.GetByteCountForSignedPackedNumber(Convert.ToInt64(value)) - 1, 1, format); break; case ValueFormats.Int: valueArgument = VerifyMaxEncodedValueSize(writer.GetByteCountForSignedPackedNumber(Convert.ToInt64(value)) - 1, 3, format); break; case ValueFormats.Long: valueArgument = VerifyMaxEncodedValueSize(writer.GetByteCountForSignedPackedNumber(Convert.ToInt64(value)) - 1, 7, format); break; case ValueFormats.Float: valueArgument = writer.GetByteCountForSignedPackedNumber( BitConverter.ToInt32(BitConverter.GetBytes(Convert.ToSingle(value)), 0)) - 1; break; case ValueFormats.Double: valueArgument = writer.GetByteCountForSignedPackedNumber(BitConverter.DoubleToInt64Bits(Convert.ToDouble(value))) - 1; break; case ValueFormats.String: valueArgument = writer.GetByteCountForUnsignedPackedNumber(stringLookup[(String)value]) - 1; break; case ValueFormats.Type: valueArgument = writer.GetByteCountForUnsignedPackedNumber(typeLookup[(TypeReference)value]) - 1; break; case ValueFormats.Field: case ValueFormats.Enum: valueArgument = writer.GetByteCountForUnsignedPackedNumber(fieldLookup[(FieldReference)value]) - 1; break; case ValueFormats.Method: valueArgument = writer.GetByteCountForUnsignedPackedNumber(methodLookup[(MethodReference)value]) - 1; break; case ValueFormats.Boolean: valueArgument = Convert.ToInt32(Convert.ToBoolean(value)); break; } var data = (byte)(valueArgument << 5); data |= (byte)format; writer.Write(data); switch (format) { case ValueFormats.Char: writer.WriteUnsignedPackedNumber(Convert.ToInt64(value)); break; case ValueFormats.Short: case ValueFormats.Byte: case ValueFormats.Int: case ValueFormats.Long: writer.WritePackedSignedNumber(Convert.ToInt64(value)); break; case ValueFormats.Float: writer.WritePackedSignedNumber(BitConverter.ToInt32(BitConverter.GetBytes(Convert.ToSingle(value)), 0)); break; case ValueFormats.Double: writer.WritePackedSignedNumber(BitConverter.DoubleToInt64Bits(Convert.ToDouble(value))); break; case ValueFormats.String: writer.WriteUnsignedPackedNumber(stringLookup[(String)value]); break; case ValueFormats.Type: writer.WriteUnsignedPackedNumber(typeLookup[(TypeReference)value]); break; case ValueFormats.Field: case ValueFormats.Enum: writer.WriteUnsignedPackedNumber(fieldLookup[(FieldReference)value]); break; case ValueFormats.Method: writer.WriteUnsignedPackedNumber(methodLookup[(MethodReference)value]); break; case ValueFormats.Array: WriteValues(writer, (object[])value); break; case ValueFormats.Annotation: WriteEncodedAnnotation(writer, value as Annotation); break; case ValueFormats.Null: case ValueFormats.Boolean: break; default: throw new ArgumentException(); } }
private void WriteEncodedArray(BinaryWriter writer) { var buffers = new Dictionary <string, uint>(); var offset = (uint)writer.BaseStream.Position; uint count = 0; var memoryStream = new MemoryStream(); using (var memoryWriter = new BinaryWriter(memoryStream)) { for (var c = 0; c < flatClasses.Count; c++) { var @class = flatClasses[c]; var values = new List <object>(); var lastNonNullIndex = -1; for (var i = 0; i < @class.Fields.Count; i++) { var field = @class.Fields[i]; switch (ValueFormat.GetFormat(field.Value)) { case ValueFormats.Annotation: case ValueFormats.Array: case ValueFormats.Method: case ValueFormats.Type: case ValueFormats.String: case ValueFormats.Enum: case ValueFormats.Field: // always set lastNonNullIndex = i; break; case ValueFormats.Null: // never set break; case ValueFormats.Double: case ValueFormats.Float: if (Convert.ToDouble(field.Value) != 0) { lastNonNullIndex = i; } break; case ValueFormats.Boolean: case ValueFormats.Byte: case ValueFormats.Char: case ValueFormats.Int: case ValueFormats.Long: case ValueFormats.Short: if (Convert.ToInt64(field.Value) != 0) { lastNonNullIndex = i; } break; default: throw new ArgumentException(); } values.Add(field.Value); } if (lastNonNullIndex != -1) { memoryStream.Position = 0; memoryStream.SetLength(0); WriteValues(memoryWriter, values.Take(lastNonNullIndex + 1).ToArray()); var buffer = memoryStream.ToArray(); var key = GetByteArrayKey(buffer); if (!buffers.ContainsKey(key)) { count++; buffers.Add(key, (uint)writer.BaseStream.Position); writer.Write(buffer); } classDefinitionsMarkers[c].StaticValuesMarker.Value = buffers[key]; } } } if (count > 0) { map.Add(TypeCodes.EncodedArray, count, offset); } }