fix(core): Fixes type caching & cleanup (#422)

- [X] Fixes some issues with the type caching
- [X] Changes type check default to ignore case
- [X] Splits out type caching of insensitive and sensitive lists. This means less stuff to iterate through when checking a type against the string.
- [X] Adds an ArrayEnumerator, mostly for reference purposes (copy/paste as needed)
This commit is contained in:
Kamron Batman 2021-01-20 00:10:33 -08:00 • committed by GitHub
parent b921c38879
commit e869c105d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 253 additions and 96 deletions

View file

@ -0,0 +1,79 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: ArrayEnumerator.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
namespace Server.Collections
{
/// <summary>
/// Non-thread safe, non-guarded enumerator for classes that have internal arrays.
/// Recommended to copy this and use it as a nested struct.
/// Recommend adding version checking to properly guard against modification during enumeration.
/// </summary>
/// <typeparam name="T"></typeparam>
public struct ArrayEnumerator<T> : IEnumerator<T>
{
private readonly T[] _array;
private int _index;
private T? _current;
public ArrayEnumerator(T[] array)
{
_array = array;
_index = 0;
_current = default;
}
public void Dispose()
{
}
public bool MoveNext()
{
T[] localList = _array;
if ((uint)_index < (uint)localList.Length)
{
_current = _array[_index++];
return true;
}
return false;
}
public T? Current => _current!;
object IEnumerator.Current
{
get
{
if (_index == 0 || _index == _array.Length + 1)
{
throw new InvalidOperationException(nameof(_index));
}
return _current;
}
}
void IEnumerator.Reset()
{
_index = 0;
_current = default;
}
}
}

View file

@ -609,7 +609,7 @@ namespace Server.Collections
public struct Enumerator : IEnumerator<TValue>
{
private readonly OrderedHashSet<TValue> _OrderedHashSet;
private readonly OrderedHashSet<TValue> _orderedHashSet;
private readonly int _version;
private int _index;
private TValue _current;
@ -618,10 +618,10 @@ namespace Server.Collections
object IEnumerator.Current => _current;
internal Enumerator(OrderedHashSet<TValue> OrderedHashSet)
internal Enumerator(OrderedHashSet<TValue> orderedHashSet)
{
_OrderedHashSet = OrderedHashSet;
_version = OrderedHashSet._version;
_orderedHashSet = orderedHashSet;
_version = orderedHashSet._version;
_index = 0;
_current = default;
}
@ -632,14 +632,14 @@ namespace Server.Collections
public bool MoveNext()
{
if (_version != _OrderedHashSet._version)
if (_version != _orderedHashSet._version)
{
throw new InvalidOperationException(InvalidOperation_EnumFailedVersion);
}
if (_index < _OrderedHashSet.Count)
if (_index < _orderedHashSet.Count)
{
Entry entry = _OrderedHashSet._entries[_index];
Entry entry = _orderedHashSet._entries[_index];
_current = entry.Value;
++_index;
return true;
@ -650,7 +650,7 @@ namespace Server.Collections
void IEnumerator.Reset()
{
if (_version != _OrderedHashSet._version)
if (_version != _orderedHashSet._version)
{
throw new InvalidOperationException(InvalidOperation_EnumFailedVersion);
}