Skip to content

Commit 016670c

Browse files
committed
renew project
1 parent 809417e commit 016670c

File tree

6 files changed

+85
-54
lines changed

6 files changed

+85
-54
lines changed

Diff for: README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ As you can see, I create a core function to drive keyword sorting just by openin
8383
!pip install sortingx # in jupyter
8484
pip install sortingx # in cmd
8585
```
86-
sortingx can do whatever `list.sort()` do, and support more methods.
86+
sortingx can do whatever `list.sort()` do, and support more methods and more data types.
87+
88+
explain:
89+
- sortingx-1.1.0 is the first version aligned with the `list.sort()` usage method.
90+
- sortingx-1.1.1 is the first stable version accelerated with typing_extensions.
91+
- sortingx-1.1.2 is the first stable version that has a return value and extends the iterable data types.
8792

8893
## LICENSE
8994

Diff for: README_release.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
|--|--|--|--|
77
|v1.1.0|`pip install sortingx==1.1.0`|Write in Python with Typing||
88
|v1.1.1|`pip install sortingx==1.1.1`|Complete Typing and Accelerate||
9+
|v1.1.2|`pip install sortingx==1.1.2`|Support More Iterative Data Types||
910
|v2.0.0|`pip install sortingx==2.0.0`|Rely on Cython||
1011
|v3.0.0|`pip install sortingx==3.0.0`|Restuct with C++||
1112
|v4.0.0|`pip install sortingx==4.0.0`|Rust with PyO3||

Diff for: sortingx/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616

1717
from .sorting import bubble, insert, shell, heap, quick, merge
1818

19-
__version__ = '1.1.1'
19+
__version__ = '1.1.2'
2020

2121
assert sys.version_info >= (3, 7, 0)

Diff for: sortingx/_utils.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
from ._typing import Iterable, Callable, Optional, _T, SupportsRichComparison
1616

17-
def generate(array: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None) -> list:
18-
compare = list(map(key, array)) if key != None else array
19-
compare = ([[value] for value in compare] if compare and compare[0] is not list else compare) if key != None else array
17+
def generate(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None) -> list:
18+
compare = list(map(key, __iterable)) if key != None else __iterable
19+
compare = ([[value] for value in compare] if (compare and not isinstance(compare[0], Iterable)) else compare) if key != None else __iterable
2020
return compare
2121

2222
def core(left: Iterable[_T], right: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> bool:
@@ -31,4 +31,9 @@ def core(left: Iterable[_T], right: Iterable[_T], key: Optional[Callable[[_T], S
3131
return True
3232
elif left[index] < right[index] and not reverse:
3333
return False
34-
return False
34+
return False
35+
36+
def convert(__iterable: Iterable[_T]) -> list:
37+
if isinstance(__iterable, (tuple, str, set, dict)):
38+
return list(__iterable)
39+
return __iterable

Diff for: sortingx/sorting.py

+61-48
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,39 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from ._utils import core, generate
15+
from ._utils import core, generate, convert
1616
from ._typing import Iterable, Callable, Optional, _T, SupportsRichComparison
1717

18-
def bubble(array: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> None:
18+
def bubble(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> list:
1919
'''
20-
:param array: iterable data.
20+
:param __iterable: iterable data.
2121
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
2222
:param reverse: whether to use descending order. The default is ascending order.
2323
'''
24-
compare = generate(array, key)
25-
for i in range(len(array) - 1):
24+
__iterable = convert(__iterable)
25+
compare = generate(__iterable, key)
26+
for i in range(len(__iterable) - 1):
2627
flag = False # early stop by adding a bool value named flag
27-
for j in range(len(array) - i - 1):
28+
for j in range(len(__iterable) - i - 1):
2829
if core(compare[j], compare[j + 1], key, reverse):
29-
array[j], array[j + 1] = array[j + 1], array[j]
30+
__iterable[j], __iterable[j + 1] = __iterable[j + 1], __iterable[j]
3031
flag = True
3132
if key != None:
3233
compare[j], compare[j + 1] = compare[j + 1], compare[j]
3334
if not flag:
3435
break
36+
return __iterable
3537

36-
def insert(array: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> None:
38+
def insert(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> list:
3739
'''
38-
:param array: iterable data.
40+
:param __iterable: iterable data.
3941
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
4042
:param reverse: whether to use descending order. The default is ascending order.
4143
'''
42-
compare = generate(array, key)
43-
for index in range(1, len(array)):
44-
keyc, keya = compare[index], array[index]
44+
__iterable = convert(__iterable)
45+
compare = generate(__iterable, key)
46+
for index in range(1, len(__iterable)):
47+
keyc, keya = compare[index], __iterable[index]
4548
low, high = 0, index - 1
4649
while low <= high: # sequence conforming to monotonicity
4750
mid = (low + high) // 2
@@ -50,45 +53,50 @@ def insert(array: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCompari
5053
else:
5154
high = mid - 1
5255
for pre in range(index, low, -1): # from back to front
53-
array[pre] = array[pre - 1]
56+
__iterable[pre] = __iterable[pre - 1]
5457
if key != None:
5558
compare[pre] = compare[pre - 1]
56-
array[low] = keya
59+
__iterable[low] = keya
5760
if key != None:
5861
compare[low] = keyc
62+
return __iterable
5963

60-
def shell(array: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> None:
64+
def shell(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> list:
6165
'''
62-
:param array: iterable data.
66+
:param __iterable: iterable data.
6367
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
6468
:param reverse: whether to use descending order. The default is ascending order.
6569
'''
66-
compare = generate(array, key)
67-
length = len(array)
70+
__iterable = convert(__iterable)
71+
compare = generate(__iterable, key)
72+
length = len(__iterable)
6873
gap = 1
6974
while gap < length / 3:
7075
gap = int(3 * gap + 1)
7176
while gap >= 1:
7277
for index in range(gap, length):
7378
next = index
7479
while next >= gap and core(compare[next - gap], compare[next], key, reverse):
75-
array[next], array[next - gap] = array[next - gap], array[next]
80+
__iterable[next], __iterable[next - gap] = __iterable[next - gap], __iterable[next]
7681
if key != None:
7782
compare[next], compare[next - gap] = compare[next - gap], compare[next]
7883
next -= gap
7984
gap = int(gap / 3)
85+
return __iterable
86+
8087

81-
def heap(array: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> None:
88+
def heap(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> list:
8289
'''
83-
:param array: iterable data.
90+
:param __iterable: iterable data.
8491
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
8592
:param reverse: whether to use descending order. The default is ascending order.
8693
'''
87-
compare = generate(array, key)
94+
__iterable = convert(__iterable)
95+
compare = generate(__iterable, key)
8896
def build(root: int, end: int) -> None:
8997
'''
9098
:param root: cursor indicating the root node (int).
91-
:param end: cursor indicating the end of the array (int).
99+
:param end: cursor indicating the end of the __iterable (int).
92100
'''
93101
piv = root
94102
left = 2 * root + 1
@@ -98,27 +106,29 @@ def build(root: int, end: int) -> None:
98106
if right < end and core(compare[right], compare[piv], key, reverse):
99107
piv = right
100108
if piv != root:
101-
array[root], array[piv] = array[piv], array[root]
109+
__iterable[root], __iterable[piv] = __iterable[piv], __iterable[root]
102110
if key != None:
103111
compare[root], compare[piv] = compare[piv], compare[root]
104112
build(piv, end)
105113

106-
length = len(array)
114+
length = len(__iterable)
107115
for root in range(length // 2 - 1 , -1, -1):
108116
build(root, length)
109117
for end in range(length - 1, 0, -1):
110-
array[0], array[end] = array[end], array[0]
118+
__iterable[0], __iterable[end] = __iterable[end], __iterable[0]
111119
if key != None:
112120
compare[0], compare[end] = compare[end], compare[0]
113121
build(0, end)
122+
return __iterable
114123

115-
def quick(array: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> None:
124+
def quick(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> list:
116125
'''
117-
:param array: iterable data.
126+
:param __iterable: iterable data.
118127
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
119128
:param reverse: whether to use descending order. The default is ascending order.
120129
'''
121-
compare = generate(array, key)
130+
__iterable = convert(__iterable)
131+
compare = generate(__iterable, key)
122132
def solve(l: int, r: int) -> None:
123133
'''
124134
main
@@ -130,38 +140,40 @@ def solve(l: int, r: int) -> None:
130140

131141
def partition(l: int, r: int) -> int:
132142
'''
133-
:param l: The left cursor of array (int).
134-
:param r: The right cursor of array (int).
143+
:param l: The left cursor of __iterable (int).
144+
:param r: The right cursor of __iterable (int).
135145
'''
136146
val = compare[r]
137147
index = l - 1
138148
for ind in range(l, r):
139149
if core(val, compare[ind], key, reverse):
140150
index += 1
141-
array[index], array[ind] = array[ind], array[index]
151+
__iterable[index], __iterable[ind] = __iterable[ind], __iterable[index]
142152
if key != None:
143153
compare[index], compare[ind] = compare[ind], compare[index]
144-
array[index + 1], array[r] = array[r], array[index + 1]
154+
__iterable[index + 1], __iterable[r] = __iterable[r], __iterable[index + 1]
145155
if key != None:
146156
compare[index + 1], compare[r] = compare[r], compare[index + 1]
147157
return index + 1
148-
solve(0, len(array)-1)
158+
solve(0, len(__iterable)-1)
159+
return __iterable
149160

150-
def merge(array: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> None:
161+
def merge(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichComparison]]=None, reverse: bool=False) -> list:
151162
'''
152-
:param array: iterable data.
163+
:param __iterable: iterable data.
153164
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
154165
:param reverse: whether to use descending order. The default is ascending order.
155166
'''
156-
compare = generate(array, key)
157-
def merge(low: int, mid: int, high: int) -> None:
167+
__iterable = convert(__iterable)
168+
compare = generate(__iterable, key)
169+
def merg(low: int, mid: int, high: int) -> None:
158170
'''
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).
171+
:param low: The low-side cursor of __iterable (int).
172+
:param mid: The middle-side cursor of __iterable (int).
173+
:param high: The high-side cursor of __iterable (int).
162174
'''
163-
left, lc = array[low: mid], compare[low: mid]
164-
right, rc = array[mid: high], compare[mid: high]
175+
left, lc = __iterable[low: mid], compare[low: mid]
176+
right, rc = __iterable[mid: high], compare[mid: high]
165177
i = 0
166178
j = 0
167179
result, store = [], []
@@ -178,23 +190,24 @@ def merge(low: int, mid: int, high: int) -> None:
178190
store += lc[i:]
179191
result += right[j:]
180192
store += rc[j:]
181-
array[low: high] = result
193+
__iterable[low: high] = result
182194
compare[low: high] = store
183195

184196
def solve() -> None:
185197
'''
186198
main
187199
'''
188200
i = 1
189-
while i < len(array):
201+
while i < len(__iterable):
190202
low = 0
191-
while low < len(array):
203+
while low < len(__iterable):
192204
mid = low + i
193-
high = min(low + 2 * i, len(array))
205+
high = min(low + 2 * i, len(__iterable))
194206
if mid < high:
195-
merge(low, mid, high)
207+
merg(low, mid, high)
196208
low += 2 * i
197209
i *= 2
198210
solve()
211+
return __iterable
199212

200213
__all__ = [bubble, insert, shell, heap, quick, merge]

Diff for: test_typing.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import sortingx as six
2+
3+
data = {'Alex': 100, 'Jack': 97, 'Peter': 88, 'Li': 98}
4+
print(type(data))
5+
test = sorted(data, reverse=True)
6+
output = six.bubble(data, reverse=True)
7+
print(output, '\n', output == test)

0 commit comments

Comments
 (0)