Add a new id class

This commit is contained in:
Marc Hernandez 2019-07-11 20:45:18 -07:00
parent 4d9181e6ad
commit dd21ff0023
7 changed files with 639 additions and 4 deletions

616
Id.cs Normal file
View File

@ -0,0 +1,616 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
//using System.MemoryExtensions;
namespace lib
{
public struct Id<T> : IComparable, IFormattable, IConvertible, IComparable<ulong>, IEquatable<ulong>
{
public const ulong Min = 0uL;
public const ulong Max = 18446744073709551615uL;
static Random s_rand = new Random();
// TODO PERF Make span versions of all these functions
unsafe public static Id<T> Generate()
{
var buf = new byte[8];
s_rand.NextBytes( buf );
var newId = BitConverter.ToUInt64( buf, 0 );
return new Id<T> { m_value = newId };
}
ulong m_value;
public int CompareTo( object value )
{
if( value == null )
{
return 1;
}
if( value is ulong )
{
ulong num = (ulong)value;
if( m_value < num )
{
return -1;
}
if( m_value > num )
{
return 1;
}
return 0;
}
throw new ArgumentException( "" );
}
public int CompareTo( ulong value )
{
if( m_value < value )
{
return -1;
}
if( m_value > value )
{
return 1;
}
return 0;
}
public override bool Equals( object obj )
{
if( !( obj is ulong ) )
{
return false;
}
return m_value == (ulong)obj;
}
public bool Equals( ulong obj )
{
return m_value == obj;
}
public override int GetHashCode()
{
return (int)m_value ^ (int)( m_value >> 32 );
}
#region ToString
//
// Summary:
// Converts the numeric value of m_value instance to its equivalent string representation.
//
// Returns:
// The string representation of the value of m_value instance, consisting of a sequence
// of digits ranging from 0 to 9, without a sign or leading zeroes.
[SecuritySafeCritical]
public override string ToString()
{
return m_value.ToString( null, NumberFormatInfo.CurrentInfo );
}
//
// Summary:
// Converts the numeric value of m_value instance to its equivalent string representation
// using the specified culture-specific format information.
//
// Parameters:
// provider:
// An object that supplies culture-specific formatting information.
//
// Returns:
// The string representation of the value of m_value instance as specified by provider.
[SecuritySafeCritical]
public string ToString( IFormatProvider provider )
{
return m_value.ToString( null, NumberFormatInfo.GetInstance( provider ) );
}
//
// Summary:
// Converts the numeric value of m_value instance to its equivalent string representation
// using the specified format.
//
// Parameters:
// format:
// A numeric format string.
//
// Returns:
// The string representation of the value of m_value instance as specified by format.
//
// Exceptions:
// T:System.FormatException:
// The format parameter is invalid.
[SecuritySafeCritical]
public string ToString( string format )
{
return m_value.ToString( format, NumberFormatInfo.CurrentInfo );
}
//
// Summary:
// Converts the numeric value of m_value instance to its equivalent string representation
// using the specified format and culture-specific format information.
//
// Parameters:
// format:
// A numeric format string.
//
// provider:
// An object that supplies culture-specific formatting information about m_value instance.
//
// Returns:
// The string representation of the value of m_value instance as specified by format
// and provider.
//
// Exceptions:
// T:System.FormatException:
// The format parameter is invalid.
[SecuritySafeCritical]
public string ToString( string format, IFormatProvider provider )
{
return m_value.ToString( format, NumberFormatInfo.GetInstance( provider ) );
}
#endregion
#region Parse
//
// Summary:
// Converts the string representation of a number to its 64-bit unsigned integer
// equivalent.
//
// Parameters:
// s:
// A string that represents the number to convert.
//
// Returns:
// A 64-bit unsigned integer equivalent to the number contained in s.
//
// Exceptions:
// T:System.ArgumentNullException:
// The s parameter is null.
//
// T:System.FormatException:
// The s parameter is not in the correct format.
//
// T:System.OverflowException:
// The s parameter represents a number less than System.UInt64.MinValue or greater
// than System.UInt64.MaxValue.
public static ulong Parse( string s )
{
return ulong.Parse( s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo );
}
//
// Summary:
// Converts the string representation of a number in a specified style to its 64-bit
// unsigned integer equivalent.
//
// Parameters:
// s:
// A string that represents the number to convert. The string is interpreted by
// using the style specified by the style parameter.
//
// style:
// A bitwise combination of the enumeration values that specifies the permitted
// format of s. A typical value to specify is System.Globalization.NumberStyles.Integer.
//
// Returns:
// A 64-bit unsigned integer equivalent to the number specified in s.
//
// Exceptions:
// T:System.ArgumentNullException:
// The s parameter is null.
//
// T:System.ArgumentException:
// /// style is not a System.Globalization.NumberStyles value. -or-style is not
// a combination of System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber
// values.
//
// T:System.FormatException:
// The s parameter is not in a format compliant with style.
//
// T:System.OverflowException:
// The s parameter represents a number less than System.UInt64.MinValue or greater
// than System.UInt64.MaxValue. -or-s includes non-zero, fractional digits.
public static ulong Parse( string s, NumberStyles style )
{
return ulong.Parse( s, style, NumberFormatInfo.CurrentInfo );
}
//
// Summary:
// Converts the string representation of a number in a specified culture-specific
// format to its 64-bit unsigned integer equivalent.
//
// Parameters:
// s:
// A string that represents the number to convert.
//
// provider:
// An object that supplies culture-specific formatting information about s.
//
// Returns:
// A 64-bit unsigned integer equivalent to the number specified in s.
//
// Exceptions:
// T:System.ArgumentNullException:
// The s parameter is null.
//
// T:System.FormatException:
// The s parameter is not in the correct style.
//
// T:System.OverflowException:
// The s parameter represents a number less than System.UInt64.MinValue or greater
// than System.UInt64.MaxValue.
public static ulong Parse( string s, IFormatProvider provider )
{
return ulong.Parse( s, NumberStyles.Integer, NumberFormatInfo.GetInstance( provider ) );
}
//
// Summary:
// Converts the string representation of a number in a specified style and culture-specific
// format to its 64-bit unsigned integer equivalent.
//
// Parameters:
// s:
// A string that represents the number to convert. The string is interpreted by
// using the style specified by the style parameter.
//
// style:
// A bitwise combination of enumeration values that indicates the style elements
// that can be present in s. A typical value to specify is System.Globalization.NumberStyles.Integer.
//
// provider:
// An object that supplies culture-specific formatting information about s.
//
// Returns:
// A 64-bit unsigned integer equivalent to the number specified in s.
//
// Exceptions:
// T:System.ArgumentNullException:
// The s parameter is null.
//
// T:System.ArgumentException:
// /// style is not a System.Globalization.NumberStyles value. -or-style is not
// a combination of System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber
// values.
//
// T:System.FormatException:
// The s parameter is not in a format compliant with style.
//
// T:System.OverflowException:
// The s parameter represents a number less than System.UInt64.MinValue or greater
// than System.UInt64.MaxValue. -or-s includes non-zero, fractional digits.
public static ulong Parse( string s, NumberStyles style, IFormatProvider provider )
{
return ulong.Parse( s, style, NumberFormatInfo.GetInstance( provider ) );
}
//
// Summary:
// Tries to convert the string representation of a number to its 64-bit unsigned
// integer equivalent. A return value indicates whether the conversion succeeded
// or failed.
//
// Parameters:
// s:
// A string that represents the number to convert.
//
// result:
// When m_value method returns, contains the 64-bit unsigned integer value that is
// equivalent to the number contained in s, if the conversion succeeded, or zero
// if the conversion failed. The conversion fails if the s parameter is null, is
// not of the correct format, or represents a number less than System.UInt64.MinValue
// or greater than System.UInt64.MaxValue. m_value parameter is passed uninitialized.
//
// Returns:
// true if s was converted successfully; otherwise, false.
public static bool TryParse( string s, out ulong result )
{
return ulong.TryParse( s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result );
}
//
// Summary:
// Tries to convert the string representation of a number in a specified style and
// culture-specific format to its 64-bit unsigned integer equivalent. A return value
// indicates whether the conversion succeeded or failed.
//
// Parameters:
// s:
// A string that represents the number to convert. The string is interpreted by
// using the style specified by the style parameter.
//
// style:
// A bitwise combination of enumeration values that indicates the permitted format
// of s. A typical value to specify is System.Globalization.NumberStyles.Integer.
//
// provider:
// An object that supplies culture-specific formatting information about s.
//
// result:
// When m_value method returns, contains the 64-bit unsigned integer value equivalent
// to the number contained in s, if the conversion succeeded, or zero if the conversion
// failed. The conversion fails if the s parameter is null, is not in a format compliant
// with style, or represents a number less than System.UInt64.MinValue or greater
// than System.UInt64.MaxValue. m_value parameter is passed uninitialized.
//
// Returns:
// true if s was converted successfully; otherwise, false.
//
// Exceptions:
// T:System.ArgumentException:
// /// style is not a System.Globalization.NumberStyles value. -or-style is not
// a combination of System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber
// values.
public static bool TryParse( string s, NumberStyles style, IFormatProvider provider, out ulong result )
{
return ulong.TryParse( s, style, NumberFormatInfo.GetInstance( provider ), out result );
}
#endregion
//
// Summary:
// Returns the System.TypeCode for value type System.UInt64.
//
// Returns:
// The enumerated constant, System.TypeCode.UInt64.
public TypeCode GetTypeCode()
{
return TypeCode.UInt64;
}
#region Converters
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToBoolean(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// true if the value of the current instance is not zero; otherwise, false.
bool IConvertible.ToBoolean( IFormatProvider provider )
{
return Convert.ToBoolean( m_value );
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToChar(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, converted to a System.Char.
char IConvertible.ToChar( IFormatProvider provider )
{
return Convert.ToChar( m_value );
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToSByte(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, converted to an System.SByte.
sbyte IConvertible.ToSByte( IFormatProvider provider )
{
return Convert.ToSByte( m_value );
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToByte(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, converted to a System.Byte.
byte IConvertible.ToByte( IFormatProvider provider )
{
return Convert.ToByte( m_value );
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToInt16(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, converted to an System.Int16.
short IConvertible.ToInt16( IFormatProvider provider )
{
return Convert.ToInt16( m_value );
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToUInt16(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, converted to a System.UInt16.
ushort IConvertible.ToUInt16( IFormatProvider provider )
{
return Convert.ToUInt16( m_value );
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToInt32(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, converted to an System.Int32.
int IConvertible.ToInt32( IFormatProvider provider )
{
return Convert.ToInt32( m_value );
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToUInt32(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, converted to a System.UInt32.
uint IConvertible.ToUInt32( IFormatProvider provider )
{
return Convert.ToUInt32( m_value );
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToInt64(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, converted to an System.Int64.
long IConvertible.ToInt64( IFormatProvider provider )
{
return Convert.ToInt64( m_value );
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToUInt64(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, unchanged.
ulong IConvertible.ToUInt64( IFormatProvider provider )
{
return m_value;
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToSingle(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, converted to a System.Single.
float IConvertible.ToSingle( IFormatProvider provider )
{
return Convert.ToSingle( m_value );
}
//
// Summary:
// For a description of m_value member, see System.IConvertible.ToDouble(System.IFormatProvider).
//
// Parameters:
// provider:
// m_value parameter is ignored.
//
// Returns:
// The value of the current instance, converted to a System.Double.
double IConvertible.ToDouble( IFormatProvider provider )
{
return Convert.ToDouble( m_value );
}
decimal IConvertible.ToDecimal( IFormatProvider provider )
{
return Convert.ToDecimal( m_value );
}
DateTime IConvertible.ToDateTime( IFormatProvider provider )
{
throw new InvalidCastException( $"InvalidCast_FromTo UInt64 DateTime" );
}
public object ToType( Type conversionType, IFormatProvider provider )
{
return Convert.ChangeType( m_value, conversionType, provider );
}
#endregion
}
}

2
Log.cs
View File

@ -178,8 +178,6 @@ namespace lib
static public void expected<T>( T value, string falseString, string trueString = "", T notExpectedValue = default(T) ) static public void expected<T>( T value, string falseString, string trueString = "", T notExpectedValue = default(T) )
{ {
var name = nameof(value);
if( !value.Equals( notExpectedValue ) ) if( !value.Equals( notExpectedValue ) )
{ {
lib.Log.info( $"Properly got {value}{trueString}" ); lib.Log.info( $"Properly got {value}{trueString}" );

View File

@ -32,6 +32,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateSerializationAssemblies>On</GenerateSerializationAssemblies> <GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
<LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
@ -44,12 +45,25 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Collections.Immutable, Version=1.2.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Collections.Immutable, Version=1.2.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Collections.Immutable.1.6.0-preview3.19128.7\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath> <HintPath>..\..\packages\System.Collections.Immutable.1.6.0-preview3.19128.7\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Core"> <Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework> <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference> </Reference>
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization"> <Reference Include="System.Runtime.Serialization">
<RequiredTargetFramework>3.0</RequiredTargetFramework> <RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference> </Reference>
@ -70,6 +84,7 @@
<Compile Include="Clock.cs" /> <Compile Include="Clock.cs" />
<Compile Include="Config.cs" /> <Compile Include="Config.cs" />
<Compile Include="Helpers.cs" /> <Compile Include="Helpers.cs" />
<Compile Include="Id.cs" />
<Compile Include="Imm.cs" /> <Compile Include="Imm.cs" />
<Compile Include="Log.cs" /> <Compile Include="Log.cs" />
<Compile Include="math\AngleSingle.cs" /> <Compile Include="math\AngleSingle.cs" />

View File

@ -3,6 +3,9 @@ using System.Diagnostics;
namespace lib namespace lib
{ {
//TODO PERF fix this and make it fast.
[Serializable] [Serializable]
public struct Token public struct Token
{ {

View File

@ -461,7 +461,7 @@ public class XmlFormatter2 : IFormatter
obj = Activator.CreateInstance( type ); obj = Activator.CreateInstance( type );
} }
catch( Exception ex ) catch( Exception )
{ {
try try
{ {

View File

@ -31,7 +31,6 @@ using System.Globalization;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Runtime.Serialization;
namespace math namespace math
{ {

View File

@ -1,4 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="System.Buffers" version="4.4.0" targetFramework="net472" />
<package id="System.Collections.Immutable" version="1.6.0-preview3.19128.7" targetFramework="net462" /> <package id="System.Collections.Immutable" version="1.6.0-preview3.19128.7" targetFramework="net462" />
<package id="System.Memory" version="4.5.3" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" />
</packages> </packages>