1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
codon/stdlib/internal/sort.codon
A. R. Shajii c128e59132
Annotation updates (#41)
* Fix gitignore, versions

* Add caching (WIP)

* Fix decorator

* Refactor

* Support tuple conversions

* Fix void; allow pyobj default conversion

* Improve setup

* Rename

* Fix JIT output capturing

* Support conversion of "complex"

* Allow class conversions

* Use Python number API

* Use Python API for (get/set/del)item

* Add decorator docs

* Cleanup

* Support slice and optional conversions

* Add comparison magics

* Remove Optional.__invert__() method

* Fix tests

* Fix test

* Add release notes

* New pybridge tests

* Update decorator tests

* Fix optional tuple handling

* Fix optional str() and repr()

* Add more decorator tests

* Fix optional.__bool__

* Update releases.md

* Add op tests

* Organize release notes [skip ci]

* clang-format [skip ci]

* Add r-magics to pyobj; more tests [skip ci]

* More pybridge tests

* Add plugin library paths to build command

* Remove walrus operator [skip ci]

* Fix optional operator handling; Fix right-magic handling

Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
Co-authored-by: Ibrahim Numanagić <inumanag@users.noreply.github.com>
2022-08-02 14:53:17 -04:00

60 lines
1.6 KiB
Python

# (c) 2022 Exaloop Inc. All rights reserved.
from algorithms.pdqsort import pdq_sort_inplace
from algorithms.insertionsort import insertion_sort_inplace
from algorithms.heapsort import heap_sort_inplace
from algorithms.qsort import qsort_inplace
from algorithms.timsort import tim_sort_inplace
def sorted(
v: Generator[T],
key=Optional[int](),
algorithm: Optional[str] = None,
reverse: bool = False,
T: type,
) -> List[T]:
"""
Return a sorted list of the elements in v
"""
newlist = [a for a in v]
if not isinstance(key, Optional):
newlist.sort(key, algorithm, reverse)
else:
newlist.sort(algorithm=algorithm, reverse=reverse)
return newlist
def _sort_list(
self: List[T], key: Callable[[T], S], algorithm: str, T: type, S: type
):
if algorithm == "tim":
tim_sort_inplace(self, key)
elif algorithm == "pdq":
pdq_sort_inplace(self, key)
elif algorithm == "insertion":
insertion_sort_inplace(self, key)
elif algorithm == "heap":
heap_sort_inplace(self, key)
elif algorithm == "quick":
qsort_inplace(self, key)
else:
raise ValueError(f"Algorithm '{algorithm}' does not exist")
@extend
class List:
def sort(
self,
key=Optional[int](),
algorithm: Optional[str] = None,
reverse: bool = False,
):
algorithm: str = algorithm if algorithm is not None else "tim"
if isinstance(key, Optional):
_sort_list(self, lambda x: x, algorithm)
else:
_sort_list(self, key, algorithm)
if reverse:
self.reverse()