fix: Adds ISpanFormattable to Geometry structs (#1231)

* Adds ISpanFormattable to geometry structs and makes ToString() near-zero-allocation.
* Adds IEquatable, and Parsable to Rectangle3D to get it in-line with the other structs.

Closes #1067
This commit is contained in:
Kamron Batman 2022-11-06 17:37:30 -08:00 committed by GitHub
parent c75bde55da
commit d7d914df6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 721 additions and 195 deletions

View file

@ -14,52 +14,53 @@
*************************************************************************/
using System;
using System.Buffers;
using System.Collections.Generic;
namespace Server;
[Parsable]
public struct WorldLocation
: IPoint3D, IComparable<WorldLocation>, IEquatable<object>, IEquatable<WorldLocation>, IEquatable<IEntity>
public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<WorldLocation>, IEquatable<IEntity>,
ISpanFormattable
{
internal Point3D m_Loc;
internal Map m_Map;
internal Point3D _loc;
internal Map _map;
public static readonly WorldLocation Zero = new(0, 0, 0, Map.Internal);
[CommandProperty(AccessLevel.Counselor)]
public Point3D Location
{
get => m_Loc;
set => m_Loc = value;
get => _loc;
set => _loc = value;
}
[CommandProperty(AccessLevel.Counselor)]
public int X
{
get => m_Loc.m_X;
set => m_Loc.m_X = value;
get => _loc.m_X;
set => _loc.m_X = value;
}
[CommandProperty(AccessLevel.Counselor)]
public int Y
{
get => m_Loc.m_Y;
set => m_Loc.m_Y = value;
get => _loc.m_Y;
set => _loc.m_Y = value;
}
[CommandProperty(AccessLevel.Counselor)]
public int Z
{
get => m_Loc.m_Z;
set => m_Loc.m_Z = value;
get => _loc.m_Z;
set => _loc.m_Z = value;
}
[CommandProperty(AccessLevel.Counselor)]
public Map Map
{
get => m_Map;
set => m_Map = value;
get => _map;
set => _map = value;
}
public WorldLocation(IEntity e) : this(e.Location.X, e.Location.Y, e.Location.Z, e.Map)
@ -80,65 +81,62 @@ public struct WorldLocation
public WorldLocation(int x, int y, int z, Map map)
{
m_Loc.m_X = x;
m_Loc.m_Y = y;
m_Loc.m_Z = z;
m_Map = map;
_loc.m_X = x;
_loc.m_Y = y;
_loc.m_Z = z;
_map = map;
}
public override string ToString() =>
$"({m_Loc.m_X}, {m_Loc.m_Y}, {m_Loc.m_Z}, {m_Map?.ToString() ?? "(-null-)"})";
public bool Equals(WorldLocation other) =>
m_Loc.Equals(other.m_Loc) && m_Map.MapID == other.m_Map.MapID;
_loc.Equals(other._loc) && _map.MapID == other._map.MapID;
public bool Equals(IEntity other) =>
!ReferenceEquals(other, null) && m_Loc == other.Location &&
m_Map.MapID == other.Map.MapID;
!ReferenceEquals(other, null) && _loc == other.Location &&
_map.MapID == other.Map.MapID;
public override bool Equals(object obj) =>
obj is WorldLocation other && Equals(other);
public override int GetHashCode() => HashCode.Combine(m_Loc, m_Map);
public override int GetHashCode() => HashCode.Combine(_loc, _map);
public int CompareTo(WorldLocation other)
{
var locComparison = m_Loc.CompareTo(other.m_Loc);
return locComparison != 0 ? locComparison : Comparer<Map>.Default.Compare(m_Map, other.m_Map);
var locComparison = _loc.CompareTo(other._loc);
return locComparison != 0 ? locComparison : Comparer<Map>.Default.Compare(_map, other._map);
}
public static implicit operator Point3D(WorldLocation worldLocation) => worldLocation.Location;
public static bool operator ==(WorldLocation l, WorldLocation r) =>
l.m_Loc == r.m_Loc && l.m_Map == r.m_Map;
l._loc == r._loc && l._map == r._map;
public static bool operator ==(WorldLocation l, IEntity r) =>
!ReferenceEquals(r, null) && l.m_Loc == r.Location && l.m_Map == r.Map;
!ReferenceEquals(r, null) && l._loc == r.Location && l._map == r.Map;
public static bool operator !=(WorldLocation l, WorldLocation r) => l.m_Loc != r.m_Loc && l.m_Map != r.m_Map;
public static bool operator !=(WorldLocation l, WorldLocation r) => l._loc != r._loc && l._map != r._map;
public static bool operator !=(WorldLocation l, IEntity r) =>
!ReferenceEquals(r, null) && l.m_Loc != r.Location && l.m_Map != r.Map;
!ReferenceEquals(r, null) && l._loc != r.Location && l._map != r.Map;
public static bool operator >(WorldLocation l, WorldLocation r) => l.m_Loc > r.m_Loc && l.m_Map == r.m_Map;
public static bool operator >(WorldLocation l, WorldLocation r) => l._loc > r._loc && l._map == r._map;
public static bool operator >(WorldLocation l, IEntity r) =>
!ReferenceEquals(r, null) && l.m_Loc > r.Location && l.m_Map == r.Map;
!ReferenceEquals(r, null) && l._loc > r.Location && l._map == r.Map;
public static bool operator <(WorldLocation l, WorldLocation r) => l.m_Loc < r.m_Loc && l.m_Map == r.m_Map;
public static bool operator <(WorldLocation l, WorldLocation r) => l._loc < r._loc && l._map == r._map;
public static bool operator <(WorldLocation l, IEntity r) =>
!ReferenceEquals(r, null) && l.m_Loc < r.Location && l.m_Map == r.Map;
!ReferenceEquals(r, null) && l._loc < r.Location && l._map == r.Map;
public static bool operator >=(WorldLocation l, WorldLocation r) => l.m_Loc >= r.m_Loc && l.m_Map == r.m_Map;
public static bool operator >=(WorldLocation l, WorldLocation r) => l._loc >= r._loc && l._map == r._map;
public static bool operator >=(WorldLocation l, IEntity r) =>
!ReferenceEquals(r, null) && l.m_Loc >= r.Location && l.m_Map == r.Map;
!ReferenceEquals(r, null) && l._loc >= r.Location && l._map == r.Map;
public static bool operator <=(WorldLocation l, WorldLocation r) => l.m_Loc <= r.m_Loc && l.m_Map == r.m_Map;
public static bool operator <=(WorldLocation l, WorldLocation r) => l._loc <= r._loc && l._map == r._map;
public static bool operator <=(WorldLocation l, IEntity r) =>
!ReferenceEquals(r, null) && l.m_Loc <= r.Location && l.m_Map == r.Map;
!ReferenceEquals(r, null) && l._loc <= r.Location && l._map == r.Map;
public static WorldLocation Parse(string value)
{
@ -164,4 +162,50 @@ public struct WorldLocation
return new WorldLocation(x, y, z, map);
}
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
if (_map == null)
{
return destination.TryWrite(provider, $"({_loc.m_X}, {_loc.m_Y}, {_loc.m_Z}) [(-null-)]", out charsWritten);
}
return destination.TryWrite(provider, $"({_loc.m_X}, {_loc.m_Y}, {_loc.m_Z}) [{_map}]", out charsWritten);
}
public override string ToString()
{
if (_map == null)
{
// Maximum number of characters that are needed to represent this:
// 9 characters for (, , ) [(-null-)]
const int staticLength = 17;
// Up to 11 characters to represent each integer
const int maxLength = staticLength + 11 * 3;
Span<char> span = stackalloc char[maxLength];
TryFormat(span, out var charsWritten, null, null);
return span[..charsWritten].ToString();
}
else
{
int charsWritten;
char[] array = ArrayPool<char>.Shared.Rent(128);
Span<char> span = array.AsSpan();
while (!TryFormat(span, out charsWritten, null, null))
{
array = ArrayPool<char>.Shared.Rent(array.Length * 2);
span = array.AsSpan();
}
return span[..charsWritten].ToString();
}
}
public string ToString(string format, IFormatProvider formatProvider)
{
// format and formatProvider are not doing anything right now, so use the
// default ToString implementation.
return ToString();
}
}