Stride

OPEN / CLOSE
  • Features
  • Blog
  • Documentation
  • Community
(icon) Download

  • Discord
  • Facebook
  • Twitter
  • YouTube

LANGUAGE

OPEN / CLOSE
  • English
  • 日本語
    Show / Hide Table of Contents

    Utilities Class

    Namespace: Stride.Core
    Assembly: Stride.Core.dll

    Utility class.

    System.Object → Utilities
    Derived from Utilities:

    public static class Utilities
    Name Description
    Methods
    AllocateClearedMemory(Int32, Byte, Int32)

    Allocate an aligned memory buffer and clear it with a specified value (0 by defaault).

    AllocateMemory(Int32, Int32)

    Allocate an aligned memory buffer.

    ClearMemory(IntPtr, Byte, Int32)

    Clears the memory.

    Compare(IEnumerable, IEnumerable)

    Compares two collection, element by elements.

    Compare(IEnumerator, IEnumerator)

    Compares two collection, element by elements.

    Compare<T>(T[], T[])
    Compare<T>(ICollection<T>, ICollection<T>)

    Compares two collection, element by elements.

    Compare<T>(List<T>, List<T>)

    Compares two list, element by elements.

    Compare<TKey, TValue>(SortedList<TKey, TValue>, SortedList<TKey, TValue>)

    Compares two collection, element by elements.

    Compare<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>)

    Compares two collection, element by elements.

    CompareMemory(IntPtr, IntPtr, Int32)

    Compares two block of memory.

    ConvertRawToTimestamp(Int64)

    Converts a System.Diagnostics.Stopwatch raw time to a System.TimeSpan.

    CopyMemory(IntPtr, IntPtr, Int32)

    Copy memory.

    Dispose<T>(ref T)

    If non-null, disposes the specified object and set it to null, otherwise do nothing.

    FreeMemory(IntPtr)

    Allocate an aligned memory buffer.

    GetHashCode(IDictionary)

    Computes a hashcode for a dictionary.

    GetHashCode(IEnumerable)

    Computes a hashcode for an enumeration

    GetHashCode(IEnumerator)

    Computes a hashcode for an enumeration

    IsMemoryAligned(IntPtr, Int32)

    Determines whether the specified memory pointer is aligned in memory.

    IterateTree<T>(T, Func<T, IEnumerable<T>>)

    Linq assisted full tree iteration and collection in a single line. Warning, could be slow.

    Join(String, IEnumerable)

    String helper join method to display an enumrable of object as a single string.

    Join(String, IEnumerator)

    String helper join method to display an enumrable of object as a single string.

    Join<T>(String, T[])

    String helper join method to display an array of object as a single string.

    Pin<T>(ref T, Action<IntPtr>)

    Pins the specified source and call an action with the pinned pointer.

    Pin<T>(T[], Action<IntPtr>)

    Pins the specified source and call an action with the pinned pointer.

    Read<T>(IntPtr)

    Reads the specified T data from a memory location.

    Read<T>(IntPtr, ref T)

    Reads the specified T data from a memory location.

    Read<T>(IntPtr, T[], Int32, Int32)

    Reads the specified array T[] data from a memory location.

    ReadAndPosition<T>(IntPtr, ref T)

    Reads the specified T data from a memory location.

    ReadOut<T>(IntPtr, out T)

    Reads the specified T data from a memory location.

    ReadStream(Stream)

    Read stream to a byte[] buffer

    ReadStream(Stream, ref Int32)

    Read stream to a byte[] buffer

    SizeOf<T>()

    Return the sizeof a struct from a CLR. Equivalent to sizeof operator but works on generics too.

    SizeOf<T>(T[])

    Return the sizeof an array of struct. Equivalent to sizeof operator but works on generics too.

    Sleep(Int32)

    Suspends current thread for a .

    Sleep(TimeSpan)

    Suspends current thread for a .

    Swap<T>(ref T, ref T)

    Swaps the value between two references.

    ToByteArray<T>(T[])

    Covnerts a structured array to an equivalent byte array.

    Write<T>(Byte[], T[], Int32, Int32)

    Writes the specified array T[] data to a memory location.

    Write<T>(IntPtr, ref T)

    Writes the specified T data to a memory location.

    Write<T>(IntPtr, T[], Int32, Int32)

    Writes the specified array T[] data to a memory location.

    WriteAndPosition<T>(IntPtr, ref T)

    Writes the specified T data to a memory location.

    | Improve this Doc View Source

    Methods


    AllocateClearedMemory(Int32, Byte, Int32)

    Allocate an aligned memory buffer and clear it with a specified value (0 by defaault).

    public static IntPtr AllocateClearedMemory(int sizeInBytes, byte clearValue = 0, int align = 16)
    Parameters
    Type Name Description
    System.Int32 sizeInBytes

    Size of the buffer to allocate.

    System.Byte clearValue

    Default value used to clear the buffer.

    System.Int32 align

    Alignment, 16 bytes by default.

    Returns
    Type Description
    System.IntPtr

    A pointer to a buffer aligned.

    Remarks

    To free this buffer, call FreeMemory(IntPtr)


    AllocateMemory(Int32, Int32)

    Allocate an aligned memory buffer.

    public static IntPtr AllocateMemory(int sizeInBytes, int align = 16)
    Parameters
    Type Name Description
    System.Int32 sizeInBytes

    Size of the buffer to allocate.

    System.Int32 align

    Alignment, a positive value which is a power of 2. 16 bytes by default.

    Returns
    Type Description
    System.IntPtr

    A pointer to a buffer aligned.

    Remarks

    To free this buffer, call FreeMemory(IntPtr)


    ClearMemory(IntPtr, Byte, Int32)

    Clears the memory.

    public static void ClearMemory(IntPtr dest, byte value, int sizeInBytesToClear)
    Parameters
    Type Name Description
    System.IntPtr dest

    The dest.

    System.Byte value

    The value.

    System.Int32 sizeInBytesToClear

    The size in bytes to clear.


    Compare(IEnumerable, IEnumerable)

    Compares two collection, element by elements.

    public static bool Compare(IEnumerable left, IEnumerable right)
    Parameters
    Type Name Description
    System.Collections.IEnumerable left

    A "from" enumerator.

    System.Collections.IEnumerable right

    A "to" enumerator.

    Returns
    Type Description
    System.Boolean

    True if lists are identical. False otherwise.


    Compare(IEnumerator, IEnumerator)

    Compares two collection, element by elements.

    public static bool Compare(IEnumerator leftIt, IEnumerator rightIt)
    Parameters
    Type Name Description
    System.Collections.IEnumerator leftIt

    A "from" enumerator.

    System.Collections.IEnumerator rightIt

    A "to" enumerator.

    Returns
    Type Description
    System.Boolean

    True if lists are identical. False otherwise.


    Compare<T>(T[], T[])

    public static bool Compare<T>(T[] left, T[] right)
    Parameters
    Type Name Description
    T[] left
    T[] right
    Returns
    Type Description
    System.Boolean
    Type Parameters
    Name Description
    T

    Compare<T>(ICollection<T>, ICollection<T>)

    Compares two collection, element by elements.

    public static bool Compare<T>(ICollection<T> left, ICollection<T> right)
    Parameters
    Type Name Description
    System.Collections.Generic.ICollection<T> left

    The collection to compare from.

    System.Collections.Generic.ICollection<T> right

    The colllection to compare to.

    Returns
    Type Description
    System.Boolean

    True if lists are identical (but no necessarely of the same time). False otherwise.

    Type Parameters
    Name Description
    T

    Compare<T>(List<T>, List<T>)

    Compares two list, element by elements.

    public static bool Compare<T>(List<T> left, List<T> right)
    Parameters
    Type Name Description
    System.Collections.Generic.List<T> left

    The list to compare from.

    System.Collections.Generic.List<T> right

    The colllection to compare to.

    Returns
    Type Description
    System.Boolean

    True if lists are sequentially equal. False otherwise.

    Type Parameters
    Name Description
    T
    Remarks

    Concrete List is favored over interface to avoid enumerator object allocation.


    Compare<TKey, TValue>(SortedList<TKey, TValue>, SortedList<TKey, TValue>)

    Compares two collection, element by elements.

    public static bool Compare<TKey, TValue>(SortedList<TKey, TValue> first, SortedList<TKey, TValue> second)
    Parameters
    Type Name Description
    SortedList<TKey, TValue> first

    The collection to compare from.

    SortedList<TKey, TValue> second

    The colllection to compare to.

    Returns
    Type Description
    System.Boolean

    True if lists are identical (but not necessarily in the same order). False otherwise.

    Type Parameters
    Name Description
    TKey
    TValue
    Remarks

    Concrete SortedList is favored over interface to avoid enumerator object allocation.


    Compare<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>)

    Compares two collection, element by elements.

    public static bool Compare<TKey, TValue>(IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second)
    Parameters
    Type Name Description
    System.Collections.Generic.IDictionary<TKey, TValue> first

    The collection to compare from.

    System.Collections.Generic.IDictionary<TKey, TValue> second

    The colllection to compare to.

    Returns
    Type Description
    System.Boolean

    True if lists are identical (but no necessarely of the same time). False otherwise.

    Type Parameters
    Name Description
    TKey
    TValue

    CompareMemory(IntPtr, IntPtr, Int32)

    Compares two block of memory.

    public static bool CompareMemory(IntPtr from, IntPtr against, int sizeToCompare)
    Parameters
    Type Name Description
    System.IntPtr from

    The pointer to compare from.

    System.IntPtr against

    The pointer to compare against.

    System.Int32 sizeToCompare

    The size in bytes to compare.

    Returns
    Type Description
    System.Boolean

    True if the buffers are equivalent, false otherwise.


    ConvertRawToTimestamp(Int64)

    Converts a System.Diagnostics.Stopwatch raw time to a System.TimeSpan.

    public static TimeSpan ConvertRawToTimestamp(long delta)
    Parameters
    Type Name Description
    System.Int64 delta

    The delta.

    Returns
    Type Description
    System.TimeSpan

    The System.TimeSpan.


    CopyMemory(IntPtr, IntPtr, Int32)

    Copy memory.

    public static void CopyMemory(IntPtr dest, IntPtr src, int sizeInBytesToCopy)
    Parameters
    Type Name Description
    System.IntPtr dest

    The destination memory location

    System.IntPtr src

    The source memory location.

    System.Int32 sizeInBytesToCopy

    The count.


    Dispose<T>(ref T)

    If non-null, disposes the specified object and set it to null, otherwise do nothing.

    public static void Dispose<T>(ref T disposable)
        where T : class, IDisposable
    Parameters
    Type Name Description
    T disposable

    The disposable.

    Type Parameters
    Name Description
    T

    FreeMemory(IntPtr)

    Allocate an aligned memory buffer.

    public static void FreeMemory(IntPtr alignedBuffer)
    Parameters
    Type Name Description
    System.IntPtr alignedBuffer
    Remarks

    The buffer must have been allocated with AllocateMemory(Int32, Int32)


    GetHashCode(IDictionary)

    Computes a hashcode for a dictionary.

    public static int GetHashCode(IDictionary dict)
    Parameters
    Type Name Description
    System.Collections.IDictionary dict
    Returns
    Type Description
    System.Int32

    Hashcode for the list.


    GetHashCode(IEnumerable)

    Computes a hashcode for an enumeration

    public static int GetHashCode(IEnumerable it)
    Parameters
    Type Name Description
    System.Collections.IEnumerable it

    An enumerator.

    Returns
    Type Description
    System.Int32

    Hashcode for the list.


    GetHashCode(IEnumerator)

    Computes a hashcode for an enumeration

    public static int GetHashCode(IEnumerator it)
    Parameters
    Type Name Description
    System.Collections.IEnumerator it

    An enumerator.

    Returns
    Type Description
    System.Int32

    Hashcode for the list.


    IsMemoryAligned(IntPtr, Int32)

    Determines whether the specified memory pointer is aligned in memory.

    public static bool IsMemoryAligned(IntPtr memoryPtr, int align = 16)
    Parameters
    Type Name Description
    System.IntPtr memoryPtr

    The memory pointer.

    System.Int32 align

    The align.

    Returns
    Type Description
    System.Boolean

    true if the specified memory pointer is aligned in memory; otherwise, false.


    IterateTree<T>(T, Func<T, IEnumerable<T>>)

    Linq assisted full tree iteration and collection in a single line. Warning, could be slow.

    public static IEnumerable<T> IterateTree<T>(T root, Func<T, IEnumerable<T>> childrenF)
    Parameters
    Type Name Description
    T root

    The root item

    System.Func<T, System.Collections.Generic.IEnumerable<T>> childrenF

    The function to retrieve a child

    Returns
    Type Description
    System.Collections.Generic.IEnumerable<T>
    Type Parameters
    Name Description
    T

    The type to iterate.


    Join(String, IEnumerable)

    String helper join method to display an enumrable of object as a single string.

    public static string Join(string separator, IEnumerable elements)
    Parameters
    Type Name Description
    System.String separator

    The separator.

    System.Collections.IEnumerable elements

    The enumerable.

    Returns
    Type Description
    System.String

    a string with array elements serparated by the seperator


    Join(String, IEnumerator)

    String helper join method to display an enumrable of object as a single string.

    public static string Join(string separator, IEnumerator elements)
    Parameters
    Type Name Description
    System.String separator

    The separator.

    System.Collections.IEnumerator elements

    The enumerable.

    Returns
    Type Description
    System.String

    a string with array elements serparated by the seperator


    Join<T>(String, T[])

    String helper join method to display an array of object as a single string.

    public static string Join<T>(string separator, T[] array)
    Parameters
    Type Name Description
    System.String separator

    The separator.

    T[] array

    The array.

    Returns
    Type Description
    System.String

    a string with array elements serparated by the seperator

    Type Parameters
    Name Description
    T

    Pin<T>(ref T, Action<IntPtr>)

    Pins the specified source and call an action with the pinned pointer.

    public static void Pin<T>(ref T source, Action<IntPtr> pinAction)
        where T : struct
    Parameters
    Type Name Description
    T source

    The source.

    System.Action<System.IntPtr> pinAction

    The pin action to perform on the pinned pointer.

    Type Parameters
    Name Description
    T

    The type of the structure to pin


    Pin<T>(T[], Action<IntPtr>)

    Pins the specified source and call an action with the pinned pointer.

    public static void Pin<T>(T[] source, Action<IntPtr> pinAction)
        where T : struct
    Parameters
    Type Name Description
    T[] source

    The source array.

    System.Action<System.IntPtr> pinAction

    The pin action to perform on the pinned pointer.

    Type Parameters
    Name Description
    T

    The type of the structure to pin


    Read<T>(IntPtr)

    Reads the specified T data from a memory location.

    public static T Read<T>(IntPtr source)
        where T : struct
    Parameters
    Type Name Description
    System.IntPtr source

    Memory location to read from.

    Returns
    Type Description
    T

    The data read from the memory location

    Type Parameters
    Name Description
    T

    Type of a data to read


    Read<T>(IntPtr, ref T)

    Reads the specified T data from a memory location.

    public static void Read<T>(IntPtr source, ref T data)
        where T : struct
    Parameters
    Type Name Description
    System.IntPtr source

    Memory location to read from.

    T data

    The data write to.

    Type Parameters
    Name Description
    T

    Type of a data to read


    Read<T>(IntPtr, T[], Int32, Int32)

    Reads the specified array T[] data from a memory location.

    public static IntPtr Read<T>(IntPtr source, T[] data, int offset, int count)
        where T : struct
    Parameters
    Type Name Description
    System.IntPtr source

    Memory location to read from.

    T[] data

    The data write to.

    System.Int32 offset

    The offset in the array to write to.

    System.Int32 count

    The number of T element to read from the memory location

    Returns
    Type Description
    System.IntPtr

    source pointer + sizeof(T) * count

    Type Parameters
    Name Description
    T

    Type of a data to read


    ReadAndPosition<T>(IntPtr, ref T)

    Reads the specified T data from a memory location.

    public static IntPtr ReadAndPosition<T>(IntPtr source, ref T data)
        where T : struct
    Parameters
    Type Name Description
    System.IntPtr source

    Memory location to read from.

    T data

    The data write to.

    Returns
    Type Description
    System.IntPtr

    source pointer + sizeof(T)

    Type Parameters
    Name Description
    T

    Type of a data to read


    ReadOut<T>(IntPtr, out T)

    Reads the specified T data from a memory location.

    public static void ReadOut<T>(IntPtr source, out T data)
        where T : struct
    Parameters
    Type Name Description
    System.IntPtr source

    Memory location to read from.

    T data

    The data write to.

    Type Parameters
    Name Description
    T

    Type of a data to read


    ReadStream(Stream)

    Read stream to a byte[] buffer

    public static byte[] ReadStream(Stream stream)
    Parameters
    Type Name Description
    System.IO.Stream stream

    input stream

    Returns
    Type Description
    System.Byte[]

    a byte[] buffer


    ReadStream(Stream, ref Int32)

    Read stream to a byte[] buffer

    public static byte[] ReadStream(Stream stream, ref int readLength)
    Parameters
    Type Name Description
    System.IO.Stream stream

    input stream

    System.Int32 readLength

    length to read

    Returns
    Type Description
    System.Byte[]

    a byte[] buffer


    SizeOf<T>()

    Return the sizeof a struct from a CLR. Equivalent to sizeof operator but works on generics too.

    public static int SizeOf<T>()
        where T : struct
    Returns
    Type Description
    System.Int32

    sizeof this struct

    Type Parameters
    Name Description
    T

    a struct to evaluate


    SizeOf<T>(T[])

    Return the sizeof an array of struct. Equivalent to sizeof operator but works on generics too.

    public static int SizeOf<T>(T[] array)
        where T : struct
    Parameters
    Type Name Description
    T[] array

    The array of struct to evaluate.

    Returns
    Type Description
    System.Int32

    sizeof in bytes of this array of struct

    Type Parameters
    Name Description
    T

    a struct


    Sleep(Int32)

    Suspends current thread for a .

    public static void Sleep(int sleepTimeInMillis)
    Parameters
    Type Name Description
    System.Int32 sleepTimeInMillis

    The duration of sleep in milliseconds.


    Sleep(TimeSpan)

    Suspends current thread for a .

    public static void Sleep(TimeSpan sleepTime)
    Parameters
    Type Name Description
    System.TimeSpan sleepTime

    The duration of sleep.


    Swap<T>(ref T, ref T)

    Swaps the value between two references.

    public static void Swap<T>(ref T left, ref T right)
    Parameters
    Type Name Description
    T left

    The left value.

    T right

    The right value.

    Type Parameters
    Name Description
    T

    Type of a data to swap.


    ToByteArray<T>(T[])

    Covnerts a structured array to an equivalent byte array.

    public static byte[] ToByteArray<T>(T[] source)
        where T : struct
    Parameters
    Type Name Description
    T[] source

    The source.

    Returns
    Type Description
    System.Byte[]

    The byte array.

    Type Parameters
    Name Description
    T

    Write<T>(Byte[], T[], Int32, Int32)

    Writes the specified array T[] data to a memory location.

    public static void Write<T>(byte[] destination, T[] data, int offset, int count)
        where T : struct
    Parameters
    Type Name Description
    System.Byte[] destination

    Memory location to write to.

    T[] data

    The array of T data to write.

    System.Int32 offset

    The offset in the array to read from.

    System.Int32 count

    The number of T element to write to the memory location

    Type Parameters
    Name Description
    T

    Type of a data to write


    Write<T>(IntPtr, ref T)

    Writes the specified T data to a memory location.

    public static void Write<T>(IntPtr destination, ref T data)
        where T : struct
    Parameters
    Type Name Description
    System.IntPtr destination

    Memory location to write to.

    T data

    The data to write.

    Type Parameters
    Name Description
    T

    Type of a data to write


    Write<T>(IntPtr, T[], Int32, Int32)

    Writes the specified array T[] data to a memory location.

    public static IntPtr Write<T>(IntPtr destination, T[] data, int offset, int count)
        where T : struct
    Parameters
    Type Name Description
    System.IntPtr destination

    Memory location to write to.

    T[] data

    The array of T data to write.

    System.Int32 offset

    The offset in the array to read from.

    System.Int32 count

    The number of T element to write to the memory location

    Returns
    Type Description
    System.IntPtr

    destination pointer + sizeof(T) * count

    Type Parameters
    Name Description
    T

    Type of a data to write


    WriteAndPosition<T>(IntPtr, ref T)

    Writes the specified T data to a memory location.

    public static IntPtr WriteAndPosition<T>(IntPtr destination, ref T data)
        where T : struct
    Parameters
    Type Name Description
    System.IntPtr destination

    Memory location to write to.

    T data

    The data to write.

    Returns
    Type Description
    System.IntPtr

    destination pointer + sizeof(T)

    Type Parameters
    Name Description
    T

    Type of a data to write


    • Improve this Doc
    • View Source
    In This Article

    Back to top

    Copyright © 2019-2021 .NET Foundation and Contributors
    Supported by the .NET Foundation