@@ -76,70 +76,6 @@ public static object heapreplace(CodeContext/*!*/ context, PythonList list, obje
7676 }
7777 }
7878
79- [ Documentation ( "Find the n largest elements in a dataset.\n \n "
80- + "Equivalent to: sorted(iterable, reverse=True)[:n]\n "
81- ) ]
82- public static PythonList nlargest ( CodeContext /*!*/ context , int n , object iterable ) {
83- if ( n <= 0 ) {
84- return new PythonList ( ) ;
85- }
86-
87- PythonList ret = new PythonList ( Math . Min ( n , 4000 ) ) ; // don't allocate anything too huge
88- IEnumerator en = PythonOps . GetEnumerator ( iterable ) ;
89-
90- // populate list with first n items
91- for ( int i = 0 ; i < n ; i ++ ) {
92- if ( ! en . MoveNext ( ) ) {
93- // fewer than n items; finish up here
94- HeapSort ( context , ret , true ) ;
95- return ret ;
96- }
97- ret . append ( en . Current ) ;
98- }
99-
100- // go through the remainder of the iterator, maintaining a min-heap of the n largest values
101- DoHeapify ( context , ret ) ;
102- while ( en . MoveNext ( ) ) {
103- DoPushPop ( context , ret , en . Current ) ;
104- }
105-
106- // return the largest items, in descending order
107- HeapSort ( context , ret , true ) ;
108- return ret ;
109- }
110-
111- [ Documentation ( "Find the n smallest elements in a dataset.\n \n "
112- + "Equivalent to: sorted(iterable)[:n]\n "
113- ) ]
114- public static PythonList nsmallest ( CodeContext /*!*/ context , int n , object iterable ) {
115- if ( n <= 0 ) {
116- return new PythonList ( ) ;
117- }
118-
119- PythonList ret = new PythonList ( Math . Min ( n , 4000 ) ) ; // don't allocate anything too huge
120- IEnumerator en = PythonOps . GetEnumerator ( iterable ) ;
121-
122- // populate list with first n items
123- for ( int i = 0 ; i < n ; i ++ ) {
124- if ( ! en . MoveNext ( ) ) {
125- // fewer than n items; finish up here
126- HeapSort ( context , ret ) ;
127- return ret ;
128- }
129- ret . append ( en . Current ) ;
130- }
131-
132- // go through the remainder of the iterator, maintaining a max-heap of the n smallest values
133- DoHeapifyMax ( context , ret ) ;
134- while ( en . MoveNext ( ) ) {
135- DoPushPopMax ( context , ret , en . Current ) ;
136- }
137-
138- // return the smallest items, in ascending order
139- HeapSort ( context , ret ) ;
140- return ret ;
141- }
142-
14379 #endregion
14480
14581 #region private implementation details (NOTE: thread-unsafe)
0 commit comments