From: Shreyas Jejurkar Date: Mon, 11 Jan 2021 13:10:38 +0000 (+0530) Subject: Minor refactoring to collections (#46506) X-Git-Tag: submit/tizen/20210909.063632~3857 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dfc70d174ff13894d9ca6added89601394c0ea2a;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Minor refactoring to collections (#46506) * Minor refactoring to collections 1. Removed unwanted casting. 2. Added condition for Clear() method for SortedDictionary. * Addressed PR feedback. Removed unwated Clear condition. * Addressed PR feedback. 1. Fixed spacing. --- diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/Queue.cs b/src/libraries/System.Collections/src/System/Collections/Generic/Queue.cs index c468d16..1581a87 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/Queue.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/Queue.cs @@ -113,8 +113,7 @@ namespace System.Collections.Generic throw new ArgumentOutOfRangeException(nameof(arrayIndex), arrayIndex, SR.ArgumentOutOfRange_Index); } - int arrayLen = array.Length; - if (arrayLen - arrayIndex < _size) + if (array.Length - arrayIndex < _size) { throw new ArgumentException(SR.Argument_InvalidOffLen); } @@ -184,7 +183,7 @@ namespace System.Collections.Generic { if (_size == _array.Length) { - int newcapacity = (int)((long)_array.Length * (long)GrowFactor / 100); + int newcapacity = (int)(_array.Length * (long)GrowFactor / 100); if (newcapacity < _array.Length + MinimumGrow) { newcapacity = _array.Length + MinimumGrow; @@ -379,7 +378,7 @@ namespace System.Collections.Generic public void TrimExcess() { - int threshold = (int)(((double)_array.Length) * 0.9); + int threshold = (int)(_array.Length * 0.9); if (_size < threshold) { SetCapacity(_size); diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs index f9c3f1f..35da349 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs @@ -323,12 +323,12 @@ namespace System.Collections.Generic ICollection IDictionary.Keys { - get { return (ICollection)Keys; } + get { return Keys; } } ICollection IDictionary.Values { - get { return (ICollection)Values; } + get { return Values; } } object? IDictionary.this[object key] @@ -353,7 +353,7 @@ namespace System.Collections.Generic throw new ArgumentNullException(nameof(key)); } - if (value == null && !(default(TValue) == null)) + if (value == null && default(TValue) != null) throw new ArgumentNullException(nameof(value)); try @@ -382,7 +382,7 @@ namespace System.Collections.Generic throw new ArgumentNullException(nameof(key)); } - if (value == null && !(default(TValue) == null)) + if (value == null && default(TValue) != null) throw new ArgumentNullException(nameof(value)); try @@ -639,8 +639,7 @@ namespace System.Collections.Generic throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall); } - TKey[]? keys = array as TKey[]; - if (keys != null) + if (array is TKey[] keys) { CopyTo(keys, index); } @@ -822,8 +821,7 @@ namespace System.Collections.Generic throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall); } - TValue[]? values = array as TValue[]; - if (values != null) + if (array is TValue[] values) { CopyTo(values, index); } @@ -967,7 +965,6 @@ namespace System.Collections.Generic public sealed class TreeSet : SortedSet { public TreeSet() - : base() { } public TreeSet(IComparer? comparer) : base(comparer) { } diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs index 220de76..8a07c41 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs @@ -270,7 +270,7 @@ namespace System.Collections.Generic if (key == null) throw new ArgumentNullException(nameof(key)); - if (value == null && !(default(TValue) == null)) // null is an invalid value for Value types + if (value == null && default(TValue) != null) // null is an invalid value for Value types throw new ArgumentNullException(nameof(value)); if (!(key is TKey)) @@ -493,8 +493,7 @@ namespace System.Collections.Generic throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall); } - KeyValuePair[]? keyValuePairArray = array as KeyValuePair[]; - if (keyValuePairArray != null) + if (array is KeyValuePair[] keyValuePairArray) { for (int i = 0; i < Count; i++) { @@ -588,7 +587,7 @@ namespace System.Collections.Generic } set { - if (((object)key) == null) throw new ArgumentNullException(nameof(key)); + if (key == null) throw new ArgumentNullException(nameof(key)); int i = Array.BinarySearch(keys, 0, _size, key, comparer); if (i >= 0) { @@ -622,7 +621,7 @@ namespace System.Collections.Generic throw new ArgumentNullException(nameof(key)); } - if (value == null && !(default(TValue) == null)) + if (value == null && default(TValue) != null) throw new ArgumentNullException(nameof(value)); TKey tempKey = (TKey)key; @@ -741,7 +740,7 @@ namespace System.Collections.Generic // SortedList.TrimExcess(); public void TrimExcess() { - int threshold = (int)(((double)keys.Length) * 0.9); + int threshold = (int)(keys.Length * 0.9); if (_size < threshold) { Capacity = _size; @@ -1105,7 +1104,7 @@ namespace System.Collections.Generic public int IndexOf(TKey key) { - if (((object)key) == null) + if (key == null) throw new ArgumentNullException(nameof(key)); int i = Array.BinarySearch(_dict.keys, 0, diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs index 91745e2..2786b1d 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using System.Runtime.Serialization; namespace System.Collections.Generic @@ -101,7 +100,7 @@ namespace System.Collections.Generic BreadthFirstTreeWalk(n => { toRemove.Add(n.Item); return true; }); while (toRemove.Count != 0) { - _underlying.Remove(toRemove[toRemove.Count - 1]); + _underlying.Remove(toRemove[^1]); toRemove.RemoveAt(toRemove.Count - 1); } diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/Stack.cs b/src/libraries/System.Collections/src/System/Collections/Generic/Stack.cs index 8f191ee..03e4282 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/Stack.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/Stack.cs @@ -177,7 +177,7 @@ namespace System.Collections.Generic public void TrimExcess() { - int threshold = (int)(((double)_array.Length) * 0.9); + int threshold = (int)(_array.Length * 0.9); if (_size < threshold) { Array.Resize(ref _array, _size);