|
|
|
|
|
|
|
|
|
|
namespace Unity.MLAgents |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// An array-like object that stores up to four elements.
|
|
|
|
/// This is a value type that does not allocate any additional memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This does not implement any interfaces such as IList, in order to avoid any accidental boxing allocations.
|
|
|
|
/// </remarks>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
private T m_elem0; |
|
|
|
private T m_elem1; |
|
|
|
private T m_elem2; |
|
|
|
private T m_elem3; |
|
|
|
private T m_Elem0; |
|
|
|
private T m_Elem1; |
|
|
|
private T m_Elem2; |
|
|
|
private T m_Elem3; |
|
|
|
/// <summary>
|
|
|
|
/// Create a length-1 array.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="elem0"></param>
|
|
|
|
m_elem0 = elem0; |
|
|
|
m_elem1 = new T {}; |
|
|
|
m_elem2 = new T {}; |
|
|
|
m_elem3 = new T {}; |
|
|
|
m_Elem0 = elem0; |
|
|
|
m_Elem1 = new T {}; |
|
|
|
m_Elem2 = new T {}; |
|
|
|
m_Elem3 = new T {}; |
|
|
|
/// <summary>
|
|
|
|
/// Create a length-2 array.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="elem0"></param>
|
|
|
|
/// <param name="elem1"></param>
|
|
|
|
m_elem0 = elem0; |
|
|
|
m_elem1 = elem1; |
|
|
|
m_elem2 = new T {}; |
|
|
|
m_elem3 = new T {}; |
|
|
|
m_Elem0 = elem0; |
|
|
|
m_Elem1 = elem1; |
|
|
|
m_Elem2 = new T {}; |
|
|
|
m_Elem3 = new T {}; |
|
|
|
/// <summary>
|
|
|
|
/// Create a length-3 array.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="elem0"></param>
|
|
|
|
/// <param name="elem1"></param>
|
|
|
|
/// <param name="elem2"></param>
|
|
|
|
m_elem0 = elem0; |
|
|
|
m_elem1 = elem1; |
|
|
|
m_elem2 = elem2; |
|
|
|
m_elem3 = new T {}; |
|
|
|
m_Elem0 = elem0; |
|
|
|
m_Elem1 = elem1; |
|
|
|
m_Elem2 = elem2; |
|
|
|
m_Elem3 = new T {}; |
|
|
|
/// <summary>
|
|
|
|
/// Create a length-3 array.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="elem0"></param>
|
|
|
|
/// <param name="elem1"></param>
|
|
|
|
/// <param name="elem2"></param>
|
|
|
|
/// <param name="elem3"></param>
|
|
|
|
m_elem0 = elem0; |
|
|
|
m_elem1 = elem1; |
|
|
|
m_elem2 = elem2; |
|
|
|
m_elem3 = elem3; |
|
|
|
m_Elem0 = elem0; |
|
|
|
m_Elem1 = elem1; |
|
|
|
m_Elem2 = elem2; |
|
|
|
m_Elem3 = elem3; |
|
|
|
/// <summary>
|
|
|
|
/// Construct an InplaceArray from an IList (e.g. Array or List).
|
|
|
|
/// The source must be non-empty and have at most 4 elements.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="elems"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException"></exception>
|
|
|
|
public static InplaceArray<T> FromList(IList<T> elems) |
|
|
|
{ |
|
|
|
switch (elems.Count) |
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Per-element access.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index"></param>
|
|
|
|
/// <exception cref="IndexOutOfRangeException"></exception>
|
|
|
|
public T this[int index] |
|
|
|
{ |
|
|
|
get |
|
|
|
|
|
|
switch (index) |
|
|
|
{ |
|
|
|
case 0: |
|
|
|
return m_elem0; |
|
|
|
return m_Elem0; |
|
|
|
return m_elem1; |
|
|
|
return m_Elem1; |
|
|
|
return m_elem2; |
|
|
|
return m_Elem2; |
|
|
|
return m_elem3; |
|
|
|
return m_Elem3; |
|
|
|
default: |
|
|
|
throw new IndexOutOfRangeException(); |
|
|
|
} |
|
|
|
|
|
|
switch (index) |
|
|
|
{ |
|
|
|
case 0: |
|
|
|
m_elem0 = value; |
|
|
|
m_Elem0 = value; |
|
|
|
m_elem1 = value; |
|
|
|
m_Elem1 = value; |
|
|
|
m_elem2 = value; |
|
|
|
m_Elem2 = value; |
|
|
|
m_elem3 = value; |
|
|
|
m_Elem3 = value; |
|
|
|
break; |
|
|
|
default: |
|
|
|
throw new IndexOutOfRangeException(); |
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The length of the array.
|
|
|
|
/// </summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a string representation of the array's elements.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="IndexOutOfRangeException"></exception>
|
|
|
|
return $"[{m_elem0}]"; |
|
|
|
return $"[{m_Elem0}]"; |
|
|
|
return $"[{m_elem0}, {m_elem1}]"; |
|
|
|
return $"[{m_Elem0}, {m_Elem1}]"; |
|
|
|
return $"[{m_elem0}, {m_elem1}, {m_elem2}]"; |
|
|
|
return $"[{m_Elem0}, {m_Elem1}, {m_Elem2}]"; |
|
|
|
return $"[{m_elem0}, {m_elem1}, {m_elem2}, {m_elem3}]"; |
|
|
|
return $"[{m_Elem0}, {m_Elem1}, {m_Elem2}, {m_Elem3}]"; |
|
|
|
/// <summary>
|
|
|
|
/// Check that the arrays have the same length and have all equal values.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="lhs"></param>
|
|
|
|
/// <param name="rhs"></param>
|
|
|
|
/// <returns>Whether the arrays are equivalent.</returns>
|
|
|
|
public static bool operator==(InplaceArray<T> lhs, InplaceArray<T> rhs) |
|
|
|
{ |
|
|
|
if (lhs.Length != rhs.Length) |
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Check that the arrays are not equivalent.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="lhs"></param>
|
|
|
|
/// <param name="rhs"></param>
|
|
|
|
/// <returns>Whether the arrays are not equivalent</returns>
|
|
|
|
/// <summary>
|
|
|
|
/// Check that the arrays are equivalent.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="other"></param>
|
|
|
|
/// <returns>Whether the arrays are not equivalent</returns>
|
|
|
|
/// <summary>
|
|
|
|
/// Check that the arrays are equivalent.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="other"></param>
|
|
|
|
/// <returns>Whether the arrays are not equivalent</returns>
|
|
|
|
/// <summary>
|
|
|
|
/// Get a hashcode for the array.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
return Tuple.Create(m_elem0, m_elem1, m_elem2, m_elem3, Length).GetHashCode(); |
|
|
|
return Tuple.Create(m_Elem0, m_Elem1, m_Elem2, m_Elem3, Length).GetHashCode(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |