Conversion¶
Overview¶
This module converts between a Python list and any linear structure of
sds.linear. Its main use is to sort the content of a structure with the
list-based functions of sds.algorithms.sorting and get a structure
back.
Both functions are generic: they rely only on __iter__ and add(),
which every LinearCollection implements with its
own semantics. No class-specific code is involved.
graph LR
A["Linear structure"] -->|"to_list()"| B["list"]
B -->|"sorted() / quick_sort()"| C["sorted list"]
C -->|"from_list(cls, ...)"| D["Linear structure"]
style A fill:#3498db,color:#fff
style D fill:#2ecc71,color:#fff
Round-trip behaviour¶
to_list(from_list(cls, items)) returns items unchanged only for the
structures that iterate in insertion order.
Structure |
Round trip |
Why |
|---|---|---|
|
Identity |
|
|
Identity |
|
|
Identity |
|
|
Reversed |
|
|
Sorted by priority |
|
Example¶
from sds.linear import Queue, Stack, from_list, to_list
queue = from_list(Queue, [3, 1, 2])
ordered = from_list(Queue, sorted(to_list(queue)))
print(to_list(ordered)) # [1, 2, 3]
print(to_list(from_list(Stack, [1, 2, 3]))) # [3, 2, 1] - LIFO
# Constructor arguments are forwarded
from sds.linear import PriorityQueue
pq = from_list(PriorityQueue, ["pear", "fig", "banana"], key=len)
print(to_list(pq)) # ['fig', 'pear', 'banana']
Complexity¶
Function |
Time |
Notes |
|---|---|---|
|
O(n) |
One pass over the structure |
|
n × cost of |
O(n) for |
Detailed Documentation¶
- sds.linear.convert.to_list(structure)[source]¶
Return the items of a linear structure as a new
list.Items come out in the structure’s own iteration order, so the result of a
Stackstarts with its top element and the result of aPriorityQueuestarts with its highest-priority element.- Parameters:
structure (LinearCollection) – Any linear structure. It is left unchanged.
- Returns:
A new list; mutating it does not affect
structure.- Return type:
- Raises:
StructureTypeError – If
structureis not aLinearCollection.
Examples
>>> from sds.linear import Deque, to_list >>> d = Deque() >>> d.add_rear(1) >>> d.add_front(0) >>> to_list(d) [0, 1]
Notes
Time complexity: O(n). Space complexity: O(n).
- sds.linear.convert.from_list(cls, items, **kwargs)[source]¶
Build a linear structure of type
clsfromitems.A fresh
cls(**kwargs)is created, then each item is passed, in order, to itsadd()method. What “adding” means is up to the structure: appended to a linked list, pushed on a stack, inserted at its priority in a priority queue.- Parameters:
cls (type) – A concrete
LinearCollectionsubclass.items (iterable) – Items to add, in order. Any iterable is accepted, including another linear structure.
**kwargs (Any) – Forwarded to the constructor, e.g.
keyandreverseforPriorityQueue.
- Returns:
A new instance of
cls.- Return type:
- Raises:
StructureTypeError – If
clsis not aLinearCollectionsubclass, or ifkwargsdoes not match its constructor.
Examples
>>> from sds.linear import PriorityQueue, from_list, to_list >>> pq = from_list(PriorityQueue, ["ccc", "a", "bb"], key=len, reverse=True) >>> to_list(pq) ['ccc', 'bb', 'a']
Notes
Time complexity: n times the cost of
cls.add(). That is O(n) forDoublyLinkedList,CircularLinkedList,StackandDeque, but O(n²) forLinkedListandQueue(appending walks the chain, no tail pointer) and forPriorityQueue(sorted insertion).