12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- __all__ = ['bubble' , 'insert' , 'shell' , 'heap' , 'quick' , 'merge' ]
16
-
17
- from numba import jit
18
15
from ._utils import core , generate
19
- from ._typing import Iterable , Callable , Optional
20
-
21
- @jit (nopython = True )
22
- def bubble (array : Iterable , key : Optional [Callable ]= None , reverse : bool = False ) -> None :
16
+ from ._typing import Iterable , Callable , Optional , _T , SupportsRichComparison
17
+ # TODO 加速六种方法
18
+ def bubble (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
23
19
'''
24
- :param array: iterable data, support numeric data, such as mixing integer and floating point data; support all data of string type; mixing string and numeric types is not supported on the same column .
20
+ :param array: iterable data.
25
21
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
26
22
:param reverse: whether to use descending order. The default is ascending order.
27
23
'''
@@ -37,10 +33,9 @@ def bubble(array: Iterable, key: Optional[Callable]=None, reverse: bool=False) -
37
33
if not flag :
38
34
break
39
35
40
- @jit (nopython = True )
41
- def insert (array : Iterable , key : Optional [Callable ]= None , reverse : bool = False ) -> None :
36
+ def insert (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
42
37
'''
43
- :param array: iterable data, support numeric data, such as mixing integer and floating point data; support all data of string type; mixing string and numeric types is not supported on the same column .
38
+ :param array: iterable data.
44
39
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
45
40
:param reverse: whether to use descending order. The default is ascending order.
46
41
'''
@@ -62,9 +57,9 @@ def insert(array: Iterable, key: Optional[Callable]=None, reverse: bool=False) -
62
57
if key != None :
63
58
compare [low ] = keyc
64
59
65
- def shell (array : Iterable , key : Optional [Callable ]= None , reverse : bool = False ) -> None :
60
+ def shell (array : Iterable [ _T ] , key : Optional [Callable [[ _T ], SupportsRichComparison ] ]= None , reverse : bool = False ) -> None :
66
61
'''
67
- :param array: iterable data, support numeric data, such as mixing integer and floating point data; support all data of string type; mixing string and numeric types is not supported on the same column .
62
+ :param array: iterable data.
68
63
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
69
64
:param reverse: whether to use descending order. The default is ascending order.
70
65
'''
@@ -83,17 +78,17 @@ def shell(array: Iterable, key: Optional[Callable]=None, reverse: bool=False) ->
83
78
next -= gap
84
79
gap = int (gap / 3 )
85
80
86
- @jit (nopython = True )
87
- def heap (array : Iterable , key : Optional [Callable ]= None , reverse : bool = False ) -> None :
81
+ def heap (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
88
82
'''
89
- :param array: iterable data, support numeric data, such as mixing integer and floating point data; support all data of string type; mixing string and numeric types is not supported on the same column .
83
+ :param array: iterable data.
90
84
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
91
85
:param reverse: whether to use descending order. The default is ascending order.
92
86
'''
93
87
compare = generate (array , key )
94
88
def build (root : int , end : int ) -> None :
95
89
'''
96
- Root: cursor indicating the root node (integer), end: cursor indicating the end of the array (integer)
90
+ :param root: cursor indicating the root node (int).
91
+ :param end: cursor indicating the end of the array (int).
97
92
'''
98
93
piv = root
99
94
left = 2 * root + 1
@@ -117,10 +112,9 @@ def build(root: int, end: int) -> None:
117
112
compare [0 ], compare [end ] = compare [end ], compare [0 ]
118
113
build (0 , end )
119
114
120
- @jit (nopython = True )
121
- def quick (array : Iterable , key : Optional [Callable ]= None , reverse : bool = False ) -> None :
115
+ def quick (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
122
116
'''
123
- :param array: iterable data, support numeric data, such as mixing integer and floating point data; support all data of string type; mixing string and numeric types is not supported on the same column .
117
+ :param array: iterable data.
124
118
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
125
119
:param reverse: whether to use descending order. The default is ascending order.
126
120
'''
@@ -136,7 +130,8 @@ def solve(l: int, r: int) -> None:
136
130
137
131
def partition (l : int , r : int ) -> int :
138
132
'''
139
- l: The left cursor of array (integer), r: The right cursor of array (integer)
133
+ :param l: The left cursor of array (int).
134
+ :param r: The right cursor of array (int).
140
135
'''
141
136
val = compare [r ]
142
137
index = l - 1
@@ -152,17 +147,18 @@ def partition(l: int, r: int) -> int:
152
147
return index + 1
153
148
solve (0 , len (array )- 1 )
154
149
155
- @jit (nopython = True )
156
- def merge (array : Iterable , key : Callable = None , reverse : bool = False ) -> None :
150
+ def merge (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
157
151
'''
158
- :param array: iterable data, support numeric data, such as mixing integer and floating point data; support all data of string type; mixing string and numeric types is not supported on the same column .
152
+ :param array: iterable data.
159
153
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
160
154
:param reverse: whether to use descending order. The default is ascending order.
161
155
'''
162
156
compare = generate (array , key )
163
157
def merge (low : int , mid : int , high : int ) -> None :
164
158
'''
165
- low: The low-side cursor of array (integer), mid: The middle-side cursor of array (integer), high: The high-side cursor of array (integer)
159
+ :param low: The low-side cursor of array (int).
160
+ :param mid: The middle-side cursor of array (int).
161
+ :param high: The high-side cursor of array (int).
166
162
'''
167
163
left , lc = array [low : mid ], compare [low : mid ]
168
164
right , rc = array [mid : high ], compare [mid : high ]
@@ -199,4 +195,6 @@ def solve() -> None:
199
195
merge (low , mid , high )
200
196
low += 2 * i
201
197
i *= 2
202
- solve ()
198
+ solve ()
199
+
200
+ __all__ = [bubble , insert , shell , heap , quick , merge ]
0 commit comments