pythonstl 0.1.4__cp311-cp311-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pythonstl/__init__.py +64 -0
- pythonstl/_rust.cp311-win_amd64.pyd +0 -0
- pythonstl/core/__init__.py +32 -0
- pythonstl/core/base.py +51 -0
- pythonstl/core/exceptions.py +57 -0
- pythonstl/core/iterator.py +187 -0
- pythonstl/facade/__init__.py +10 -0
- pythonstl/facade/algorithms.py +331 -0
- pythonstl/facade/map.py +298 -0
- pythonstl/facade/priority_queue.py +228 -0
- pythonstl/facade/queue.py +234 -0
- pythonstl/facade/set.py +271 -0
- pythonstl/facade/stack.py +217 -0
- pythonstl/facade/vector.py +406 -0
- pythonstl/implementations/__init__.py +3 -0
- pythonstl/implementations/associative/__init__.py +6 -0
- pythonstl/implementations/associative/_map_impl.py +164 -0
- pythonstl/implementations/associative/_set_impl.py +138 -0
- pythonstl/implementations/heaps/__init__.py +5 -0
- pythonstl/implementations/heaps/_priority_queue_impl.py +118 -0
- pythonstl/implementations/linear/__init__.py +7 -0
- pythonstl/implementations/linear/_queue_impl.py +117 -0
- pythonstl/implementations/linear/_stack_impl.py +99 -0
- pythonstl/implementations/linear/_vector_impl.py +251 -0
- pythonstl-0.1.4.dist-info/METADATA +449 -0
- pythonstl-0.1.4.dist-info/RECORD +29 -0
- pythonstl-0.1.4.dist-info/WHEEL +4 -0
- pythonstl-0.1.4.dist-info/licenses/LICENSE +21 -0
- pythonstl-0.1.4.dist-info/sboms/pythonstl.cyclonedx.json +1051 -0
pythonstl/__init__.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""
|
|
2
|
+
PythonSTL - Python Standard Template Library
|
|
3
|
+
|
|
4
|
+
A Python package that replicates C++ STL-style data structures
|
|
5
|
+
using the Facade Design Pattern.
|
|
6
|
+
|
|
7
|
+
This package provides clean, STL-compliant interfaces for common
|
|
8
|
+
data structures while hiding implementation details from users.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
__version__ = "0.1.4"
|
|
12
|
+
__author__ = "PySTL Contributors"
|
|
13
|
+
|
|
14
|
+
from pythonstl.facade.stack import stack
|
|
15
|
+
from pythonstl.facade.queue import queue
|
|
16
|
+
from pythonstl.facade.vector import vector
|
|
17
|
+
from pythonstl.facade.set import stl_set
|
|
18
|
+
from pythonstl.facade.map import stl_map
|
|
19
|
+
from pythonstl.facade.priority_queue import priority_queue
|
|
20
|
+
from pythonstl.facade.algorithms import (
|
|
21
|
+
next_permutation,
|
|
22
|
+
prev_permutation,
|
|
23
|
+
nth_element,
|
|
24
|
+
partition,
|
|
25
|
+
lower_bound,
|
|
26
|
+
upper_bound,
|
|
27
|
+
binary_search,
|
|
28
|
+
equal_range
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
# Also export exceptions for user error handling
|
|
32
|
+
from pythonstl.core.exceptions import (
|
|
33
|
+
PySTLException,
|
|
34
|
+
EmptyContainerError,
|
|
35
|
+
OutOfRangeError,
|
|
36
|
+
KeyNotFoundError
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
__all__ = [
|
|
40
|
+
# Data structures
|
|
41
|
+
'stack',
|
|
42
|
+
'queue',
|
|
43
|
+
'vector',
|
|
44
|
+
'stl_set',
|
|
45
|
+
'stl_map',
|
|
46
|
+
'priority_queue',
|
|
47
|
+
# Algorithms
|
|
48
|
+
'next_permutation',
|
|
49
|
+
'prev_permutation',
|
|
50
|
+
'nth_element',
|
|
51
|
+
'partition',
|
|
52
|
+
'lower_bound',
|
|
53
|
+
'upper_bound',
|
|
54
|
+
'binary_search',
|
|
55
|
+
'equal_range',
|
|
56
|
+
# Exceptions
|
|
57
|
+
'PySTLException',
|
|
58
|
+
'EmptyContainerError',
|
|
59
|
+
'OutOfRangeError',
|
|
60
|
+
'KeyNotFoundError',
|
|
61
|
+
# Metadata
|
|
62
|
+
'__version__',
|
|
63
|
+
'__author__'
|
|
64
|
+
]
|
|
Binary file
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Core module for pystl package."""
|
|
2
|
+
|
|
3
|
+
from .base import ContainerBase, T, K, V
|
|
4
|
+
from .exceptions import (
|
|
5
|
+
PySTLException,
|
|
6
|
+
EmptyContainerError,
|
|
7
|
+
OutOfRangeError,
|
|
8
|
+
KeyNotFoundError
|
|
9
|
+
)
|
|
10
|
+
from .iterator import (
|
|
11
|
+
Iterator,
|
|
12
|
+
VectorIterator,
|
|
13
|
+
VectorReverseIterator,
|
|
14
|
+
SetIterator,
|
|
15
|
+
MapIterator
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
'ContainerBase',
|
|
20
|
+
'T',
|
|
21
|
+
'K',
|
|
22
|
+
'V',
|
|
23
|
+
'PySTLException',
|
|
24
|
+
'EmptyContainerError',
|
|
25
|
+
'OutOfRangeError',
|
|
26
|
+
'KeyNotFoundError',
|
|
27
|
+
'Iterator',
|
|
28
|
+
'VectorIterator',
|
|
29
|
+
'VectorReverseIterator',
|
|
30
|
+
'SetIterator',
|
|
31
|
+
'MapIterator'
|
|
32
|
+
]
|
pythonstl/core/base.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Base classes and type definitions for pystl data structures.
|
|
3
|
+
|
|
4
|
+
This module provides abstract base classes and common interfaces
|
|
5
|
+
for all STL-style data structures in the package.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from abc import ABC, abstractmethod
|
|
9
|
+
from typing import TypeVar, Generic
|
|
10
|
+
|
|
11
|
+
# Type variable for generic container elements
|
|
12
|
+
T = TypeVar('T')
|
|
13
|
+
K = TypeVar('K') # For map keys
|
|
14
|
+
V = TypeVar('V') # For map values
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ContainerBase(ABC, Generic[T]):
|
|
18
|
+
"""
|
|
19
|
+
Abstract base class for all STL-style containers.
|
|
20
|
+
|
|
21
|
+
Provides common interface methods that all containers must implement.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
@abstractmethod
|
|
25
|
+
def empty(self) -> bool:
|
|
26
|
+
"""
|
|
27
|
+
Check if the container is empty.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
bool: True if container is empty, False otherwise.
|
|
31
|
+
|
|
32
|
+
Time Complexity:
|
|
33
|
+
O(1)
|
|
34
|
+
"""
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
@abstractmethod
|
|
38
|
+
def size(self) -> int:
|
|
39
|
+
"""
|
|
40
|
+
Get the number of elements in the container.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
int: Number of elements in the container.
|
|
44
|
+
|
|
45
|
+
Time Complexity:
|
|
46
|
+
O(1)
|
|
47
|
+
"""
|
|
48
|
+
pass
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
__all__ = ['ContainerBase', 'T', 'K', 'V']
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Custom exceptions for pystl data structures.
|
|
3
|
+
|
|
4
|
+
This module defines all custom exceptions used throughout the package
|
|
5
|
+
to provide clear error messages matching C++ STL behavior.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class PySTLException(Exception):
|
|
10
|
+
"""Base exception class for all pystl exceptions."""
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class EmptyContainerError(PySTLException):
|
|
15
|
+
"""
|
|
16
|
+
Exception raised when attempting to access elements from an empty container.
|
|
17
|
+
|
|
18
|
+
This matches the behavior of C++ STL when accessing empty containers
|
|
19
|
+
(e.g., calling top() on an empty stack).
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, container_type: str):
|
|
23
|
+
self.container_type = container_type
|
|
24
|
+
super().__init__(f"Cannot access element from empty {container_type}")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class OutOfRangeError(PySTLException):
|
|
28
|
+
"""
|
|
29
|
+
Exception raised when accessing an invalid index or position.
|
|
30
|
+
|
|
31
|
+
This matches C++ STL's std::out_of_range exception.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, index: int, size: int):
|
|
35
|
+
self.index = index
|
|
36
|
+
self.size = size
|
|
37
|
+
super().__init__(f"Index {index} is out of range for container of size {size}")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class KeyNotFoundError(PySTLException):
|
|
41
|
+
"""
|
|
42
|
+
Exception raised when a key is not found in an associative container.
|
|
43
|
+
|
|
44
|
+
This matches C++ STL behavior when accessing non-existent keys in maps.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
def __init__(self, key):
|
|
48
|
+
self.key = key
|
|
49
|
+
super().__init__(f"Key '{key}' not found in container")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
__all__ = [
|
|
53
|
+
'PySTLException',
|
|
54
|
+
'EmptyContainerError',
|
|
55
|
+
'OutOfRangeError',
|
|
56
|
+
'KeyNotFoundError'
|
|
57
|
+
]
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Iterator classes for pystl containers.
|
|
3
|
+
|
|
4
|
+
This module provides iterator support for STL-style containers,
|
|
5
|
+
enabling both C++ STL-style iteration and Python iteration protocols.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from typing import TypeVar, Generic, List
|
|
9
|
+
from abc import ABC, abstractmethod
|
|
10
|
+
|
|
11
|
+
T = TypeVar('T')
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Iterator(ABC, Generic[T]):
|
|
15
|
+
"""
|
|
16
|
+
Abstract base class for iterators.
|
|
17
|
+
|
|
18
|
+
Provides the foundation for both forward and reverse iterators.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def __next__(self) -> T:
|
|
23
|
+
"""Get the next element."""
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
@abstractmethod
|
|
27
|
+
def __iter__(self):
|
|
28
|
+
"""Return the iterator itself."""
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class VectorIterator(Iterator[T]):
|
|
33
|
+
"""
|
|
34
|
+
Forward iterator for vector.
|
|
35
|
+
|
|
36
|
+
This iterator traverses the vector from beginning to end.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def __init__(self, data: List[T], index: int = 0) -> None:
|
|
40
|
+
"""
|
|
41
|
+
Initialize vector iterator.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
data: Reference to the vector's internal data
|
|
45
|
+
index: Starting position
|
|
46
|
+
"""
|
|
47
|
+
self._data = data
|
|
48
|
+
self._index = index
|
|
49
|
+
|
|
50
|
+
def __next__(self) -> T:
|
|
51
|
+
"""
|
|
52
|
+
Get the next element.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
The next element in the vector.
|
|
56
|
+
|
|
57
|
+
Raises:
|
|
58
|
+
StopIteration: When iteration is complete.
|
|
59
|
+
"""
|
|
60
|
+
if self._index >= len(self._data):
|
|
61
|
+
raise StopIteration
|
|
62
|
+
value = self._data[self._index]
|
|
63
|
+
self._index += 1
|
|
64
|
+
return value
|
|
65
|
+
|
|
66
|
+
def __iter__(self):
|
|
67
|
+
"""Return the iterator itself."""
|
|
68
|
+
return self
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class VectorReverseIterator(Iterator[T]):
|
|
72
|
+
"""
|
|
73
|
+
Reverse iterator for vector.
|
|
74
|
+
|
|
75
|
+
This iterator traverses the vector from end to beginning.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
def __init__(self, data: List[T], index: int = -1) -> None:
|
|
79
|
+
"""
|
|
80
|
+
Initialize reverse vector iterator.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
data: Reference to the vector's internal data
|
|
84
|
+
index: Starting position (default: last element)
|
|
85
|
+
"""
|
|
86
|
+
self._data = data
|
|
87
|
+
if index == -1:
|
|
88
|
+
self._index = len(data) - 1
|
|
89
|
+
else:
|
|
90
|
+
self._index = index
|
|
91
|
+
|
|
92
|
+
def __next__(self) -> T:
|
|
93
|
+
"""
|
|
94
|
+
Get the next element (in reverse order).
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
The next element in reverse order.
|
|
98
|
+
|
|
99
|
+
Raises:
|
|
100
|
+
StopIteration: When iteration is complete.
|
|
101
|
+
"""
|
|
102
|
+
if self._index < 0:
|
|
103
|
+
raise StopIteration
|
|
104
|
+
value = self._data[self._index]
|
|
105
|
+
self._index -= 1
|
|
106
|
+
return value
|
|
107
|
+
|
|
108
|
+
def __iter__(self):
|
|
109
|
+
"""Return the iterator itself."""
|
|
110
|
+
return self
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class SetIterator(Iterator[T]):
|
|
114
|
+
"""
|
|
115
|
+
Iterator for set.
|
|
116
|
+
|
|
117
|
+
This iterator traverses the set elements.
|
|
118
|
+
Note: Order is not guaranteed (matches Python set behavior).
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
def __init__(self, data: set) -> None:
|
|
122
|
+
"""
|
|
123
|
+
Initialize set iterator.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
data: Reference to the set's internal data
|
|
127
|
+
"""
|
|
128
|
+
self._iterator = iter(data)
|
|
129
|
+
|
|
130
|
+
def __next__(self) -> T:
|
|
131
|
+
"""
|
|
132
|
+
Get the next element.
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
The next element in the set.
|
|
136
|
+
|
|
137
|
+
Raises:
|
|
138
|
+
StopIteration: When iteration is complete.
|
|
139
|
+
"""
|
|
140
|
+
return next(self._iterator)
|
|
141
|
+
|
|
142
|
+
def __iter__(self):
|
|
143
|
+
"""Return the iterator itself."""
|
|
144
|
+
return self
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class MapIterator(Iterator):
|
|
148
|
+
"""
|
|
149
|
+
Iterator for map.
|
|
150
|
+
|
|
151
|
+
This iterator traverses the map's key-value pairs.
|
|
152
|
+
Returns tuples of (key, value).
|
|
153
|
+
"""
|
|
154
|
+
|
|
155
|
+
def __init__(self, data: dict) -> None:
|
|
156
|
+
"""
|
|
157
|
+
Initialize map iterator.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
data: Reference to the map's internal data
|
|
161
|
+
"""
|
|
162
|
+
self._iterator = iter(data.items())
|
|
163
|
+
|
|
164
|
+
def __next__(self):
|
|
165
|
+
"""
|
|
166
|
+
Get the next key-value pair.
|
|
167
|
+
|
|
168
|
+
Returns:
|
|
169
|
+
Tuple of (key, value).
|
|
170
|
+
|
|
171
|
+
Raises:
|
|
172
|
+
StopIteration: When iteration is complete.
|
|
173
|
+
"""
|
|
174
|
+
return next(self._iterator)
|
|
175
|
+
|
|
176
|
+
def __iter__(self):
|
|
177
|
+
"""Return the iterator itself."""
|
|
178
|
+
return self
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
__all__ = [
|
|
182
|
+
'Iterator',
|
|
183
|
+
'VectorIterator',
|
|
184
|
+
'VectorReverseIterator',
|
|
185
|
+
'SetIterator',
|
|
186
|
+
'MapIterator'
|
|
187
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Facade layer for pystl data structures."""
|
|
2
|
+
|
|
3
|
+
from .stack import stack
|
|
4
|
+
from .queue import queue
|
|
5
|
+
from .vector import vector
|
|
6
|
+
from .set import stl_set
|
|
7
|
+
from .map import stl_map
|
|
8
|
+
from .priority_queue import priority_queue
|
|
9
|
+
|
|
10
|
+
__all__ = ['stack', 'queue', 'vector', 'stl_set', 'stl_map', 'priority_queue']
|