static private IGeometry parseAoUnsafeWkb(byte[] wkb, BitConversion bitConversion, wkbGeometryType geometryType) { IGeometry geom = null; // Keeps track of what byte we're at. // (assume the byte order and geometry // type data has already been read) int i = 5; switch (geometryType) { case wkbGeometryType.wkbMultiLinestring: createWkbMultiLineString(wkb, ref i, bitConversion, out geom); break; } return(geom); }
static void getWkbUInt32(byte[] wkb, ref int startIndex, BitConversion conversionType, out UInt32 theUInt32) { switch (conversionType) { case BitConversion.toLittle: theUInt32 = 0; break; case BitConversion.toBig: theUInt32 = 0; break; case BitConversion.none: default: theUInt32 = BitConverter.ToUInt32(wkb, startIndex); break; } startIndex += 4; }
static void getWkbDouble(byte[] wkb, ref int startIndex, BitConversion conversionType, out double theDouble) { switch (conversionType) { case BitConversion.toLittle: theDouble = 0; break; case BitConversion.toBig: theDouble = 0; break; case BitConversion.none: default: theDouble = BitConverter.ToDouble(wkb, startIndex); break; } startIndex += 8; }
public virtual void TestNaNs() { long[] nans = new long[] { 0x7ff0000000000000L, unchecked ((long)0xfff0000000000000L), 0x7ff8000000000000L }; for (int i = 0; i < nans.Length; i++) { long longBitsIn = nans[i]; double dbl = BitConversion.Int64BitsToDouble(longBitsIn); long longBitsOut = BitConversion.DoubleToRawInt64Bits(dbl); // Sanity check assertTrue(longBitsIn == longBitsOut); // Store the double and retrieve it ByteBuffer buffer = ByteBuffer.Allocate(8); buffer.PutDouble(dbl); double bufDoubleOut = buffer.GetDouble(0); // Check the bits sequence was not normalized long bufLongOut = BitConversion.DoubleToRawInt64Bits(bufDoubleOut); assertTrue(longBitsIn == bufLongOut); } }
public static byte[] GenerateTicket(Nintendo3DSTitle title) { var ticket = Convert.FromBase64String(Resources.TikTemplate); var titleId = BitConversion.HexToBytes(title.TitleId); var titleKey = BitConversion.HexToBytes(title.EncKey); const int titleKeyOffset = 0x1BF; for (var offset = titleKeyOffset; offset < titleKeyOffset + 0x10; offset++) { ticket[offset] = titleKey[offset - titleKeyOffset]; } const int titleIdOffset = 0x1DC; for (int offset = titleIdOffset; offset < titleIdOffset + 0x8; offset++) { ticket[offset] = titleId[offset - titleIdOffset]; } return(ticket); }
static void getWkbUInt32(byte[] wkb, ref int startIndex, BitConversion conversionType, out UInt32 theUInt32) { switch (conversionType) { case BitConversion.toLittle: theUInt32 = 0; break; case BitConversion.toBig: theUInt32 = 0; break; case BitConversion.none: default: theUInt32 = BitConverter.ToUInt32(wkb, startIndex); break; } startIndex += 4; }
static void createWkbMultiLineString(byte[] wkb, ref int startIndex, BitConversion bitConversion, out IGeometry geometry) { geometry = new PolylineClass(); IPointCollection mp = (IPointCollection)geometry; // Get the number of line strings. UInt32 lineStringCnt; getWkbUInt32(wkb, ref startIndex, bitConversion, out lineStringCnt); UInt32 pointCnt; double x, y; object missing = Type.Missing; // Loop through each LineString. for (int i = 0; i < lineStringCnt; i++) { startIndex += 5; // Jump past useless header stuff. getWkbUInt32(wkb, ref startIndex, bitConversion, out pointCnt); IPointCollection tempPc = new MultipointClass(); // Loop through each point. for (int j = 0; j < pointCnt; j++) { getWkbDouble(wkb, ref startIndex, bitConversion, out x); getWkbDouble(wkb, ref startIndex, bitConversion, out y); tempPc.AddPoint(createAoPoint(x, y), ref missing, ref missing); } mp.AddPointCollection(tempPc); } }
static void getWkbDouble(byte[] wkb, ref int startIndex, BitConversion conversionType, out double theDouble) { switch (conversionType) { case BitConversion.toLittle: theDouble = 0; break; case BitConversion.toBig: theDouble = 0; break; case BitConversion.none: default: theDouble = BitConverter.ToDouble(wkb, startIndex); break; } startIndex += 8; }
public override void SetSingleValue(float value) { base.SetInt64Value(BitConversion.SingleToRawInt32Bits(value)); }
/// <summary> /// Returns a hash code value for this object. </summary> public override int GetHashCode() { return(BitConversion.SingleToInt32Bits(Boost) ^ clauses.GetHashCode() + MinimumNumberShouldMatch + (disableCoord ? 17 : 0)); }
private static long ToSortable(double value) { return(Flip(BitConversion.DoubleToInt64Bits(value))); }
/// <summary> /// Creates a new DocValues field with the specified 32-bit <see cref="float"/> value </summary> /// <param name="name"> field name </param> /// <param name="value"> 32-bit <see cref="float"/> value </param> /// <exception cref="ArgumentNullException"> if the field name is <c>null</c> </exception> public SingleDocValuesField(string name, float value) : base(name, BitConversion.SingleToRawInt32Bits(value)) { }
/// <summary> /// Returns the <see cref="float"/> at the specified index. /// <para/> /// The 4 bytes starting at the specified index are composed into a <see cref="float"/> /// according to the current byte order and returned. The position is not /// changed. /// <para/> /// NOTE: This was getFloat() in the JDK /// </summary> /// <param name="index">The index, must not be negative and equal or less than <c>limit - 4</c>.</param> /// <returns>The <see cref="float"/> at the specified index.</returns> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="index"/> is invalid.</exception> public override sealed float GetSingle(int index) { return(BitConversion.Int32BitsToSingle(GetInt32(index))); }
public override void WriteField(FieldInfo info, IIndexableField field) { int bits /* = 0*/; // LUCENENET: IDE0059: Remove unnecessary value assignment BytesRef bytes; string @string; // LUCENENET specific - To avoid boxing/unboxing, we don't // call GetNumericValue(). Instead, we check the field.NumericType and then // call the appropriate conversion method. if (field.NumericType != NumericFieldType.NONE) { switch (field.NumericType) { case NumericFieldType.BYTE: case NumericFieldType.INT16: case NumericFieldType.INT32: bits = NUMERIC_INT32; break; case NumericFieldType.INT64: bits = NUMERIC_INT64; break; case NumericFieldType.SINGLE: bits = NUMERIC_SINGLE; break; case NumericFieldType.DOUBLE: bits = NUMERIC_DOUBLE; break; default: throw new ArgumentException("cannot store numeric type " + field.NumericType); } @string = null; bytes = null; } else { bytes = field.GetBinaryValue(); if (bytes != null) { bits = BYTE_ARR; @string = null; } else { bits = STRING; @string = field.GetStringValue(); if (@string == null) { throw new ArgumentException("field " + field.Name + " is stored but does not have BinaryValue, StringValue nor NumericValue"); } } } long infoAndBits = (((long)info.Number) << TYPE_BITS) | (uint)bits; bufferedDocs.WriteVInt64(infoAndBits); if (bytes != null) { bufferedDocs.WriteVInt32(bytes.Length); bufferedDocs.WriteBytes(bytes.Bytes, bytes.Offset, bytes.Length); } else if (@string != null) { bufferedDocs.WriteString(field.GetStringValue()); } else { switch (field.NumericType) { case NumericFieldType.BYTE: case NumericFieldType.INT16: case NumericFieldType.INT32: bufferedDocs.WriteInt32(field.GetInt32Value().Value); break; case NumericFieldType.INT64: bufferedDocs.WriteInt64(field.GetInt64Value().Value); break; case NumericFieldType.SINGLE: bufferedDocs.WriteInt32(BitConversion.SingleToInt32Bits(field.GetSingleValue().Value)); break; case NumericFieldType.DOUBLE: bufferedDocs.WriteInt64(BitConversion.DoubleToInt64Bits(field.GetDoubleValue().Value)); break; default: throw AssertionError.Create("Cannot get here"); } } }
/// <summary> /// Writes a 32-bit <see cref="float"/> to the target stream. The resulting output is the /// four bytes resulting from calling <see cref="BitConversion.SingleToInt32Bits(float)"/>. /// <para/> /// NOTE: This was writeFloat() in Java /// </summary> /// <param name="value">The <see cref="float"/> to write to the target stream.</param> /// <exception cref="IOException">If an error occurs while writing to the target stream.</exception> /// <seealso cref="DataInputStream.ReadSingle()"/> public void WriteSingle(float value) { WriteInt32(BitConversion.SingleToInt32Bits(value)); }
public override ByteBuffer PutSingle(int index, float value) { return(PutInt32(index, BitConversion.SingleToInt32Bits(value))); }
/// <summary> /// Writes a 64-bit <see cref="double"/> to the target stream. The resulting output is the /// eight bytes resulting from calling <see cref="BitConversion.DoubleToInt64Bits(double)"/>. /// </summary> /// <param name="value">The <see cref="double"/> to write to the target stream.</param> /// <exception cref="IOException">If an error occurs while writing to the target stream.</exception> /// <seealso cref="DataInputStream.ReadDouble()"/> public void WriteDouble(double value) { WriteInt64(BitConversion.DoubleToInt64Bits(value)); }
private static int ToSortable(float value) { return(Flip(BitConversion.SingleToInt32Bits(value))); }
private static float FromSortable(int sortable) { return(BitConversion.Int32BitsToSingle(Flip(sortable))); }
private static IGeometry parseAoUnsafeWkb(byte[] wkb, BitConversion bitConversion, wkbGeometryType geometryType) { IGeometry geom = null; // Keeps track of what byte we're at. // (assume the byte order and geometry // type data has already been read) int i = 5; switch (geometryType) { case wkbGeometryType.wkbMultiLinestring: createWkbMultiLineString(wkb, ref i, bitConversion, out geom); break; } return geom; }
public static void test(int level, DoubleBuffer b, bool direct) { Show(level, b); //if (direct != b.IsDirect) // J2N: IsDirect not supported // fail("Wrong direction", b); // Gets and puts relPut(b); relGet(b); absGet(b); bulkGet(b); absPut(b); relGet(b); absGet(b); bulkGet(b); bulkPutArray(b); relGet(b); bulkPutBuffer(b); relGet(b); // Compact relPut(b); b.Position = (13); b.Compact(); b.Flip(); relGet(b, 13); // Exceptions relPut(b); b.Limit = (b.Capacity / 2); b.Position = (b.Limit); tryCatch(b, typeof(BufferUnderflowException), () => { b.Get(); }); tryCatch(b, typeof(BufferOverflowException), () => { b.Put((double)42); }); // The index must be non-negative and lesss than the buffer's limit. tryCatch(b, typeof(ArgumentOutOfRangeException), () => { b.Get(b.Limit); }); tryCatch(b, typeof(ArgumentOutOfRangeException), () => { b.Get(-1); }); tryCatch(b, typeof(ArgumentOutOfRangeException), () => { b.Put(b.Limit, (double)42); }); tryCatch(b, typeof(InvalidMarkException), () => { b.Position = (0); b.Mark(); b.Compact(); b.Reset(); }); // Values b.Clear(); b.Put((double)0); b.Put((double)-1); b.Put((double)1); b.Put(double.MaxValue); b.Put(double.MinValue); b.Put(-double.MaxValue); b.Put(-double.MinValue); b.Put(double.NegativeInfinity); b.Put(double.PositiveInfinity); b.Put(double.NaN); b.Put(0.5121609353879392); // Changes value if incorrectly swapped double v; b.Flip(); ck(b, b.Get(), 0); ck(b, b.Get(), (double)-1); ck(b, b.Get(), 1); ck(b, b.Get(), double.MaxValue); ck(b, b.Get(), double.MinValue); ck(b, b.Get(), -double.MaxValue); ck(b, b.Get(), -double.MinValue); ck(b, b.Get(), double.NegativeInfinity); ck(b, b.Get(), double.PositiveInfinity); if (BitConversion.DoubleToRawInt64Bits(v = b.Get()) != BitConversion.DoubleToRawInt64Bits(double.NaN)) { fail(b, unchecked ((long)double.NaN), (long)v); } ck(b, b.Get(), 0.5121609353879392); // Comparison b.Rewind(); DoubleBuffer b2 = DoubleBuffer.Allocate(b.Capacity); b2.Put(b); b2.Flip(); b.Position = (2); b2.Position = (2); if (!b.Equals(b2)) { for (int i = 2; i < b.Limit; i++) { double x = b.Get(i); double y = b2.Get(i); if (x != y || x.CompareTo(y) != 0) { output.WriteLine("[" + i + "] " + x + " != " + y); } } fail("Identical buffers not equal", b, b2); } if (b.CompareTo(b2) != 0) { fail("Comparison to identical buffer != 0", b, b2); } b.Limit = (b.Limit + 1); b.Position = (b.Limit - 1); b.Put((double)99); b.Rewind(); b2.Rewind(); if (b.Equals(b2)) { fail("Non-identical buffers equal", b, b2); } if (b.CompareTo(b2) <= 0) { fail("Comparison to shorter buffer <= 0", b, b2); } b.Limit = (b.Limit - 1); b.Put(2, (double)42); if (b.Equals(b2)) { fail("Non-identical buffers equal", b, b2); } if (b.CompareTo(b2) <= 0) { fail("Comparison to lesser buffer <= 0", b, b2); } // Check equals and compareTo with interesting values foreach (double x in VALUES) { DoubleBuffer xb = DoubleBuffer.Wrap(new double[] { x }); if (xb.CompareTo(xb) != 0) { fail("compareTo not reflexive", xb, xb, x, x); } if (!xb.Equals(xb)) { fail("equals not reflexive", xb, xb, x, x); } foreach (double y in VALUES) { DoubleBuffer yb = DoubleBuffer.Wrap(new double[] { y }); if (xb.CompareTo(yb) != -yb.CompareTo(xb)) { fail("compareTo not anti-symmetric", xb, yb, x, y); } if ((xb.CompareTo(yb) == 0) != xb.Equals(yb)) { fail("compareTo inconsistent with equals", xb, yb, x, y); } if (xb.CompareTo(yb) != x.CompareTo(y)) { if (x == 0.0 && y == 0.0) { continue; } fail("Incorrect results for DoubleBuffer.compareTo", xb, yb, x, y); } if (xb.Equals(yb) != ((x == y) || (double.IsNaN(x) && double.IsNaN(y)))) { fail("Incorrect results for DoubleBuffer.equals", xb, yb, x, y); } } } // Sub, dup relPut(b); relGet(b.Duplicate()); b.Position = (13); relGet(b.Duplicate(), 13); relGet(b.Duplicate().Slice(), 13); relGet(b.Slice(), 13); relGet(b.Slice().Duplicate(), 13); // Slice b.Position = (5); DoubleBuffer sb = b.Slice(); checkSlice(b, sb); b.Position = (0); DoubleBuffer sb2 = sb.Slice(); checkSlice(sb, sb2); if (!sb.Equals(sb2)) { fail("Sliced slices do not match", sb, sb2); } if ((sb.HasArray) && (sb.ArrayOffset != sb2.ArrayOffset)) { fail("Array offsets do not match: " + sb.ArrayOffset + " != " + sb2.ArrayOffset, sb, sb2); } // Read-only views b.Rewind(); DoubleBuffer rb = b.AsReadOnlyBuffer(); if (!b.Equals(rb)) { fail("Buffer not equal to read-only view", b, rb); } Show(level + 1, rb); tryCatch(b, typeof(ReadOnlyBufferException), () => { relPut(rb); }); tryCatch(b, typeof(ReadOnlyBufferException), () => { absPut(rb); }); tryCatch(b, typeof(ReadOnlyBufferException), () => { bulkPutArray(rb); }); tryCatch(b, typeof(ReadOnlyBufferException), () => { bulkPutBuffer(rb); }); tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.Compact(); }); if (rb.GetType().Name.StartsWith("Heap", StringComparison.Ordinal)) { tryCatch(b, typeof(ReadOnlyBufferException), () => { var _ = rb.Array; }); tryCatch(b, typeof(ReadOnlyBufferException), () => { var _ = rb.ArrayOffset; }); if (rb.HasArray) { fail("Read-only heap buffer's backing array is accessible", rb); } } // Bulk puts from read-only buffers b.Clear(); rb.Rewind(); b.Put(rb); relPut(b); // Required by testViews }
public override ByteBuffer PutDouble(int index, double value) { return(PutInt64(index, BitConversion.DoubleToRawInt64Bits(value))); }
private static double FromSortable(long sortable) { return(BitConversion.Int64BitsToDouble(Flip(sortable))); }
/// <summary> /// Returns the <see cref="double"/> at the specified index. /// <para/> /// The 8 bytes starting at the specified index are composed into a <see cref="double"/> /// according to the current byte order and returned. The position is not /// changed. /// </summary> /// <param name="index">The index, must not be negative and equal or less than <c>limit - 8</c>.</param> /// <returns>The <see cref="double"/> at the specified index.</returns> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="index"/> is invalid.</exception> public override sealed double GetDouble(int index) { return(BitConversion.Int64BitsToDouble(GetInt64(index))); }
public double ReadDouble() { return(BitConversion.Int64BitsToDouble(ReadInt64())); }
/// <summary> /// Gets a value indicating whether the current <see cref="double"/> has the value negative zero (<c>-0.0d</c>). /// While negative zero is supported by the <see cref="double"/> datatype in .NET, comparisons and string formatting ignore /// this feature. This method allows a simple way to check whether the current <see cref="double"/> has the value negative zero. /// </summary> /// <param name="d">This <see cref="double"/>.</param> /// <returns><c>true</c> if the current value represents negative zero; otherwise, <c>false</c>.</returns> public static bool IsNegativeZero(this double d) { return(d == 0 && BitConversion.DoubleToRawInt64Bits(d) == BitConversion.DoubleToRawInt64Bits(NegativeZero)); }
public float ReadSingle() { return(BitConversion.Int32BitsToSingle(ReadInt32())); }
public override void WriteField(FieldInfo info, IIndexableField field) { fieldsStream.WriteVInt32(info.Number); int bits = 0; BytesRef bytes; string @string; // TODO: maybe a field should serialize itself? // this way we don't bake into indexer all these // specific encodings for different fields? and apps // can customize... // LUCENENET specific - To avoid boxing/unboxing, we don't // call GetNumericValue(). Instead, we check the field.NumericType and then // call the appropriate conversion method. if (field.NumericType != NumericFieldType.NONE) { switch (field.NumericType) { case NumericFieldType.BYTE: case NumericFieldType.INT16: case NumericFieldType.INT32: bits |= FIELD_IS_NUMERIC_INT; break; case NumericFieldType.INT64: bits |= FIELD_IS_NUMERIC_LONG; break; case NumericFieldType.SINGLE: bits |= FIELD_IS_NUMERIC_FLOAT; break; case NumericFieldType.DOUBLE: bits |= FIELD_IS_NUMERIC_DOUBLE; break; default: throw new ArgumentException("cannot store numeric type " + field.NumericType); } @string = null; bytes = null; } else { bytes = field.GetBinaryValue(); if (bytes != null) { bits |= FIELD_IS_BINARY; @string = null; } else { @string = field.GetStringValue(); if (@string == null) { throw new ArgumentException("field " + field.Name + " is stored but does not have binaryValue, stringValue nor numericValue"); } } } fieldsStream.WriteByte((byte)(sbyte)bits); if (bytes != null) { fieldsStream.WriteVInt32(bytes.Length); fieldsStream.WriteBytes(bytes.Bytes, bytes.Offset, bytes.Length); } else if (@string != null) { fieldsStream.WriteString(field.GetStringValue()); } else { switch (field.NumericType) { case NumericFieldType.BYTE: case NumericFieldType.INT16: case NumericFieldType.INT32: fieldsStream.WriteInt32(field.GetInt32Value().Value); break; case NumericFieldType.INT64: fieldsStream.WriteInt64(field.GetInt64Value().Value); break; case NumericFieldType.SINGLE: fieldsStream.WriteInt32(BitConversion.SingleToInt32Bits(field.GetSingleValue().Value)); break; case NumericFieldType.DOUBLE: fieldsStream.WriteInt64(BitConversion.DoubleToInt64Bits(field.GetDoubleValue().Value)); break; default: throw new InvalidOperationException("Cannot get here"); } } }
/// <summary> /// Gets a value indicating whether the current <see cref="float"/> has the value negative zero (<c>-0.0f</c>). /// While negative zero is supported by the <see cref="float"/> datatype in .NET, comparisons and string formatting ignore /// this feature. This method allows a simple way to check whether the current <see cref="float"/> has the value negative zero. /// </summary> /// <param name="f">This <see cref="float"/>.</param> /// <returns><c>true</c> if the current value represents negative zero; otherwise, <c>false</c>.</returns> public static bool IsNegativeZero(this float f) { return(f == 0 && BitConversion.SingleToRawInt32Bits(f) == BitConversion.SingleToRawInt32Bits(NegativeZero)); }