//----------------------------------------------------------------------- // // Copyright (c) 2018 Sirenix IVS // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // //----------------------------------------------------------------------- namespace VRC.Udon.Serialization.OdinSerializer.Utilities { using System; using System.Collections.Generic; /// /// Garbage free enumerator methods. /// public static class GarbageFreeIterators { /// /// Garbage free enumerator for lists. /// public static ListIterator GFIterator(this List list) { return new ListIterator(list); } /// /// Garbage free enumerator for dictionaries. /// public static DictionaryIterator GFIterator(this Dictionary dictionary) { return new DictionaryIterator(dictionary); } /// /// Garbage free enumator for dictionary values. /// public static DictionaryValueIterator GFValueIterator(this Dictionary dictionary) { return new DictionaryValueIterator(dictionary); } /// /// Garbage free enumerator for hashsets. /// public static HashsetIterator GFIterator(this HashSet hashset) { return new HashsetIterator(hashset); } /// /// List iterator. /// public struct ListIterator : IDisposable { private bool isNull; private List list; private List.Enumerator enumerator; /// /// Creates a list iterator. /// public ListIterator(List list) { this.isNull = list == null; if (this.isNull) { this.list = null; this.enumerator = new List.Enumerator(); } else { this.list = list; this.enumerator = this.list.GetEnumerator(); } } /// /// Gets the enumerator. /// public ListIterator GetEnumerator() { return this; } /// /// Gets the current value. /// public T Current { get { return this.enumerator.Current; } } /// /// Moves to the next value. /// public bool MoveNext() { if (this.isNull) { return false; } return this.enumerator.MoveNext(); } /// /// Disposes the iterator. /// public void Dispose() { this.enumerator.Dispose(); } } /// /// Hashset iterator. /// public struct HashsetIterator : IDisposable { private bool isNull; private HashSet hashset; private HashSet.Enumerator enumerator; /// /// Creates a hashset iterator. /// public HashsetIterator(HashSet hashset) { this.isNull = hashset == null; if (this.isNull) { this.hashset = null; this.enumerator = new HashSet.Enumerator(); } else { this.hashset = hashset; this.enumerator = this.hashset.GetEnumerator(); } } /// /// Gets the enumerator. /// public HashsetIterator GetEnumerator() { return this; } /// /// Gets the current value. /// public T Current { get { return this.enumerator.Current; } } /// /// Moves to the next value. /// public bool MoveNext() { if (this.isNull) { return false; } return this.enumerator.MoveNext(); } /// /// Disposes the iterator. /// public void Dispose() { this.enumerator.Dispose(); } } /// /// Dictionary iterator. /// public struct DictionaryIterator : IDisposable { private Dictionary dictionary; private Dictionary.Enumerator enumerator; private bool isNull; /// /// Creates a dictionary iterator. /// public DictionaryIterator(Dictionary dictionary) { this.isNull = dictionary == null; if (this.isNull) { this.dictionary = null; this.enumerator = new Dictionary.Enumerator(); } else { this.dictionary = dictionary; this.enumerator = this.dictionary.GetEnumerator(); } } /// /// Gets the enumerator. /// public DictionaryIterator GetEnumerator() { return this; } /// /// Gets the current value. /// public KeyValuePair Current { get { return this.enumerator.Current; } } /// /// Moves to the next value. /// public bool MoveNext() { if (this.isNull) { return false; } return this.enumerator.MoveNext(); } /// /// Disposes the iterator. /// public void Dispose() { this.enumerator.Dispose(); } } /// /// Dictionary value iterator. /// public struct DictionaryValueIterator : IDisposable { private Dictionary dictionary; private Dictionary.Enumerator enumerator; private bool isNull; /// /// Creates a dictionary value iterator. /// public DictionaryValueIterator(Dictionary dictionary) { this.isNull = dictionary == null; if (this.isNull) { this.dictionary = null; this.enumerator = new Dictionary.Enumerator(); } else { this.dictionary = dictionary; this.enumerator = this.dictionary.GetEnumerator(); } } /// /// Gets the enumerator. /// public DictionaryValueIterator GetEnumerator() { return this; } /// /// Gets the current value. /// public T2 Current { get { return this.enumerator.Current.Value; } } /// /// Moves to the next value. /// public bool MoveNext() { if (this.isNull) { return false; } return this.enumerator.MoveNext(); } /// /// Disposes the iterator. /// public void Dispose() { this.enumerator.Dispose(); } } } }