Minor refactoring to collections (#46506)
authorShreyas Jejurkar <shreyasjejurkar123@live.com>
Mon, 11 Jan 2021 13:10:38 +0000 (18:40 +0530)
committerGitHub <noreply@github.com>
Mon, 11 Jan 2021 13:10:38 +0000 (13:10 +0000)
* 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.

src/libraries/System.Collections/src/System/Collections/Generic/Queue.cs
src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs
src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs
src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs
src/libraries/System.Collections/src/System/Collections/Generic/Stack.cs

index c468d16..1581a87 100644 (file)
@@ -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);
index f9c3f1f..35da349 100644 (file)
@@ -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<T> : SortedSet<T>
     {
         public TreeSet()
-            : base()
         { }
 
         public TreeSet(IComparer<T>? comparer) : base(comparer) { }
index 220de76..8a07c41 100644 (file)
@@ -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<TKey, TValue>[]? keyValuePairArray = array as KeyValuePair<TKey, TValue>[];
-            if (keyValuePairArray != null)
+            if (array is KeyValuePair<TKey, TValue>[] 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<TKey>(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<TKey>(_dict.keys, 0,
index 91745e2..2786b1d 100644 (file)
@@ -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);
                 }
 
index 8f191ee..03e4282 100644 (file)
@@ -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);