22// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33// See the LICENSE file in the project root for more information.
44
5+ #nullable enable
6+
57using System ;
68using System . Collections ;
7- using System . Collections . Generic ;
8- using System . Text ;
9- using Microsoft . Scripting . Runtime ;
9+
1010using IronPython . Runtime ;
11- using IronPython . Runtime . Binding ;
1211using IronPython . Runtime . Operations ;
1312using IronPython . Runtime . Types ;
1413
14+ using Microsoft . Scripting . Runtime ;
15+
1516[ assembly: PythonModule ( "_heapq" , typeof ( IronPython . Modules . PythonHeapq ) ) ]
1617namespace IronPython . Modules {
1718 public static class PythonHeapq {
@@ -21,14 +22,14 @@ public static class PythonHeapq {
2122 #region public API
2223
2324 [ Documentation ( "Transform list into a heap, in-place, in O(len(heap)) time." ) ]
24- public static void heapify ( CodeContext /*!*/ context , PythonList list ) {
25+ public static void heapify ( CodeContext /*!*/ context , [ NotNone ] PythonList list ) {
2526 lock ( list ) {
2627 DoHeapify ( context , list ) ;
2728 }
2829 }
2930
3031 [ Documentation ( "Pop the smallest item off the heap, maintaining the heap invariant." ) ]
31- public static object heappop ( CodeContext /*!*/ context , PythonList list ) {
32+ public static object ? heappop ( CodeContext /*!*/ context , [ NotNone ] PythonList list ) {
3233 lock ( list ) {
3334 int last = list . _size - 1 ;
3435 if ( last < 0 ) {
@@ -42,7 +43,7 @@ public static object heappop(CodeContext/*!*/ context, PythonList list) {
4243 }
4344
4445 [ Documentation ( "Push item onto heap, maintaining the heap invariant." ) ]
45- public static void heappush ( CodeContext /*!*/ context , PythonList list , object item ) {
46+ public static void heappush ( CodeContext /*!*/ context , [ NotNone ] PythonList list , object ? item ) {
4647 lock ( list ) {
4748 list . AddNoLock ( item ) ;
4849 SiftUp ( context , list , list . _size - 1 ) ;
@@ -53,7 +54,7 @@ public static void heappush(CodeContext/*!*/ context, PythonList list, object it
5354 + "from the heap. The combined action runs more efficiently than\n "
5455 + "heappush() followed by a separate call to heappop()."
5556 ) ]
56- public static object heappushpop ( CodeContext /*!*/ context , PythonList list , object item ) {
57+ public static object ? heappushpop ( CodeContext /*!*/ context , [ NotNone ] PythonList list , object ? item ) {
5758 lock ( list ) {
5859 return DoPushPop ( context , list , item ) ;
5960 }
@@ -67,9 +68,9 @@ public static object heappushpop(CodeContext/*!*/ context, PythonList list, obje
6768 + " if item > heap[0]:\n "
6869 + " item = heapreplace(heap, item)\n "
6970 ) ]
70- public static object heapreplace ( CodeContext /*!*/ context , PythonList list , object item ) {
71+ public static object ? heapreplace ( CodeContext /*!*/ context , [ NotNone ] PythonList list , object ? item ) {
7172 lock ( list ) {
72- object ret = list . _data [ 0 ] ;
73+ object ? ret = list . _data [ 0 ] ;
7374 list . _data [ 0 ] = item ;
7475 SiftDown ( context , list , 0 , list . _size - 1 ) ;
7576 return ret ;
@@ -80,7 +81,7 @@ public static object heapreplace(CodeContext/*!*/ context, PythonList list, obje
8081 [ Documentation ( "Find the n largest elements in a dataset.\n \n "
8182 + "Equivalent to: sorted(iterable, reverse=True)[:n]\n "
8283 ) ]
83- public static PythonList nlargest ( CodeContext /*!*/ context , int n , object iterable ) {
84+ public static PythonList nlargest ( CodeContext /*!*/ context , int n , object ? iterable ) {
8485 if ( n <= 0 ) {
8586 return new PythonList ( ) ;
8687 }
@@ -113,7 +114,7 @@ public static PythonList nlargest(CodeContext/*!*/ context, int n, object iterab
113114 [ Documentation ( "Find the n smallest elements in a dataset.\n \n "
114115 + "Equivalent to: sorted(iterable)[:n]\n "
115116 ) ]
116- public static PythonList nsmallest ( CodeContext /*!*/ context , int n , object iterable ) {
117+ public static PythonList nsmallest ( CodeContext /*!*/ context , int n , object ? iterable ) {
117118 if ( n <= 0 ) {
118119 return new PythonList ( ) ;
119120 }
@@ -146,7 +147,7 @@ public static PythonList nsmallest(CodeContext/*!*/ context, int n, object itera
146147
147148 #region private implementation details (NOTE: thread-unsafe)
148149
149- private static bool IsLessThan ( CodeContext /*!*/ context , object x , object y ) {
150+ private static bool IsLessThan ( CodeContext /*!*/ context , object ? x , object ? y ) {
150151 object ret ;
151152 if ( PythonTypeOps . TryInvokeBinaryOperator ( context , x , y , "__lt__" , out ret ) &&
152153 ! Object . ReferenceEquals ( ret , NotImplementedType . Value ) ) {
@@ -206,8 +207,8 @@ private static void DoHeapifyMax(CodeContext/*!*/ context, PythonList list) {
206207 }
207208 }
208209
209- private static object DoPushPop ( CodeContext /*!*/ context , PythonList heap , object item ) {
210- object first ;
210+ private static object ? DoPushPop ( CodeContext /*!*/ context , PythonList heap , object ? item ) {
211+ object ? first ;
211212 if ( heap . _size == 0 || ! IsLessThan ( context , first = heap . _data [ 0 ] , item ) ) {
212213 return item ;
213214 }
@@ -216,8 +217,8 @@ private static object DoPushPop(CodeContext/*!*/ context, PythonList heap, objec
216217 return first ;
217218 }
218219
219- private static object DoPushPopMax ( CodeContext /*!*/ context , PythonList heap , object item ) {
220- object first ;
220+ private static object ? DoPushPopMax ( CodeContext /*!*/ context , PythonList heap , object ? item ) {
221+ object ? first ;
221222 if ( heap . _size == 0 || ! IsLessThan ( context , item , first = heap . _data [ 0 ] ) ) {
222223 return item ;
223224 }
0 commit comments