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.
@@ -0,0 +1,5 @@
1
+ """Heap-based data structure implementations."""
2
+
3
+ from ._priority_queue_impl import _PriorityQueueImpl
4
+
5
+ __all__ = ['_PriorityQueueImpl']
@@ -0,0 +1,118 @@
1
+ """
2
+ Private implementation of Priority Queue data structure.
3
+
4
+ This module contains the internal implementation of a priority queue
5
+ following C++ STL semantics. Users should not access this directly.
6
+ """
7
+
8
+ from typing import TypeVar, List
9
+ import heapq
10
+ from pythonstl.core.exceptions import EmptyContainerError
11
+
12
+ T = TypeVar('T')
13
+
14
+
15
+ class _PriorityQueueImpl:
16
+ """
17
+ Internal implementation of a priority queue using heapq.
18
+
19
+ This class should not be accessed directly by users.
20
+ Use the facade class `priority_queue` instead.
21
+
22
+ Note:
23
+ Python's heapq implements a min-heap. We support both min and max heaps
24
+ through the comparator parameter.
25
+ """
26
+
27
+ def __init__(self, comparator: str = "max") -> None:
28
+ """
29
+ Initialize an empty priority queue.
30
+
31
+ Args:
32
+ comparator: Either "max" for max-heap (default, matches C++ STL)
33
+ or "min" for min-heap.
34
+
35
+ Time Complexity:
36
+ O(1)
37
+ """
38
+ self._data: List[T] = []
39
+ self._comparator = comparator
40
+ if comparator not in ["max", "min"]:
41
+ raise ValueError("comparator must be 'max' or 'min'")
42
+
43
+ def push(self, value: T) -> None:
44
+ """
45
+ Insert an element into the priority queue.
46
+
47
+ Args:
48
+ value: The element to insert.
49
+
50
+ Time Complexity:
51
+ O(log n) where n is the number of elements
52
+ """
53
+ if self._comparator == "max":
54
+ # Negate for max-heap behavior
55
+ heapq.heappush(self._data, -value if isinstance(value, (int, float)) else value)
56
+ else:
57
+ heapq.heappush(self._data, value)
58
+
59
+ def pop(self) -> None:
60
+ """
61
+ Remove the top element from the priority queue.
62
+
63
+ Raises:
64
+ EmptyContainerError: If the priority queue is empty.
65
+
66
+ Time Complexity:
67
+ O(log n) where n is the number of elements
68
+ """
69
+ if self.empty():
70
+ raise EmptyContainerError("priority_queue")
71
+ heapq.heappop(self._data)
72
+
73
+ def top(self) -> T:
74
+ """
75
+ Get the top element of the priority queue without removing it.
76
+
77
+ Returns:
78
+ The top element (highest priority).
79
+
80
+ Raises:
81
+ EmptyContainerError: If the priority queue is empty.
82
+
83
+ Time Complexity:
84
+ O(1)
85
+ """
86
+ if self.empty():
87
+ raise EmptyContainerError("priority_queue")
88
+ top_value = self._data[0]
89
+ if self._comparator == "max" and isinstance(top_value, (int, float)):
90
+ return -top_value
91
+ return top_value
92
+
93
+ def empty(self) -> bool:
94
+ """
95
+ Check if the priority queue is empty.
96
+
97
+ Returns:
98
+ True if the priority queue is empty, False otherwise.
99
+
100
+ Time Complexity:
101
+ O(1)
102
+ """
103
+ return len(self._data) == 0
104
+
105
+ def size(self) -> int:
106
+ """
107
+ Get the number of elements in the priority queue.
108
+
109
+ Returns:
110
+ The number of elements in the priority queue.
111
+
112
+ Time Complexity:
113
+ O(1)
114
+ """
115
+ return len(self._data)
116
+
117
+
118
+ __all__ = ['_PriorityQueueImpl']
@@ -0,0 +1,7 @@
1
+ """Linear data structure implementations."""
2
+
3
+ from ._stack_impl import _StackImpl
4
+ from ._queue_impl import _QueueImpl
5
+ from ._vector_impl import _VectorImpl
6
+
7
+ __all__ = ['_StackImpl', '_QueueImpl', '_VectorImpl']
@@ -0,0 +1,117 @@
1
+ """
2
+ Private implementation of Queue data structure.
3
+
4
+ This module contains the internal implementation of a queue
5
+ following C++ STL semantics. Users should not access this directly.
6
+ """
7
+
8
+ from typing import TypeVar
9
+ from collections import deque
10
+ from pythonstl.core.exceptions import EmptyContainerError
11
+
12
+ T = TypeVar('T')
13
+
14
+
15
+ class _QueueImpl:
16
+ """
17
+ Internal implementation of a queue using collections.deque.
18
+
19
+ This class should not be accessed directly by users.
20
+ Use the facade class `queue` instead.
21
+ """
22
+
23
+ def __init__(self) -> None:
24
+ """
25
+ Initialize an empty queue.
26
+
27
+ Time Complexity:
28
+ O(1)
29
+ """
30
+ self._data: deque[T] = deque()
31
+
32
+ def push(self, value: T) -> None:
33
+ """
34
+ Add an element to the back of the queue.
35
+
36
+ Args:
37
+ value: The element to add to the queue.
38
+
39
+ Time Complexity:
40
+ O(1)
41
+ """
42
+ self._data.append(value)
43
+
44
+ def pop(self) -> None:
45
+ """
46
+ Remove the front element from the queue.
47
+
48
+ Raises:
49
+ EmptyContainerError: If the queue is empty.
50
+
51
+ Time Complexity:
52
+ O(1)
53
+ """
54
+ if self.empty():
55
+ raise EmptyContainerError("queue")
56
+ self._data.popleft()
57
+
58
+ def front(self) -> T:
59
+ """
60
+ Get the front element of the queue without removing it.
61
+
62
+ Returns:
63
+ The front element of the queue.
64
+
65
+ Raises:
66
+ EmptyContainerError: If the queue is empty.
67
+
68
+ Time Complexity:
69
+ O(1)
70
+ """
71
+ if self.empty():
72
+ raise EmptyContainerError("queue")
73
+ return self._data[0]
74
+
75
+ def back(self) -> T:
76
+ """
77
+ Get the back element of the queue without removing it.
78
+
79
+ Returns:
80
+ The back element of the queue.
81
+
82
+ Raises:
83
+ EmptyContainerError: If the queue is empty.
84
+
85
+ Time Complexity:
86
+ O(1)
87
+ """
88
+ if self.empty():
89
+ raise EmptyContainerError("queue")
90
+ return self._data[-1]
91
+
92
+ def empty(self) -> bool:
93
+ """
94
+ Check if the queue is empty.
95
+
96
+ Returns:
97
+ True if the queue is empty, False otherwise.
98
+
99
+ Time Complexity:
100
+ O(1)
101
+ """
102
+ return len(self._data) == 0
103
+
104
+ def size(self) -> int:
105
+ """
106
+ Get the number of elements in the queue.
107
+
108
+ Returns:
109
+ The number of elements in the queue.
110
+
111
+ Time Complexity:
112
+ O(1)
113
+ """
114
+ return len(self._data)
115
+
116
+
117
+ __all__ = ['_QueueImpl']
@@ -0,0 +1,99 @@
1
+ """
2
+ Private implementation of Stack data structure.
3
+
4
+ This module contains the internal implementation of a stack
5
+ following C++ STL semantics. Users should not access this directly.
6
+ """
7
+
8
+ from typing import TypeVar, List
9
+ from pythonstl.core.exceptions import EmptyContainerError
10
+
11
+ T = TypeVar('T')
12
+
13
+
14
+ class _StackImpl:
15
+ """
16
+ Internal implementation of a stack using Python list.
17
+
18
+ This class should not be accessed directly by users.
19
+ Use the facade class `stack` instead.
20
+ """
21
+
22
+ def __init__(self) -> None:
23
+ """
24
+ Initialize an empty stack.
25
+
26
+ Time Complexity:
27
+ O(1)
28
+ """
29
+ self._data: List[T] = []
30
+
31
+ def push(self, value: T) -> None:
32
+ """
33
+ Push an element onto the stack.
34
+
35
+ Args:
36
+ value: The element to push onto the stack.
37
+
38
+ Time Complexity:
39
+ O(1) amortized
40
+ """
41
+ self._data.append(value)
42
+
43
+ def pop(self) -> None:
44
+ """
45
+ Remove the top element from the stack.
46
+
47
+ Raises:
48
+ EmptyContainerError: If the stack is empty.
49
+
50
+ Time Complexity:
51
+ O(1)
52
+ """
53
+ if self.empty():
54
+ raise EmptyContainerError("stack")
55
+ self._data.pop()
56
+
57
+ def top(self) -> T:
58
+ """
59
+ Get the top element of the stack without removing it.
60
+
61
+ Returns:
62
+ The top element of the stack.
63
+
64
+ Raises:
65
+ EmptyContainerError: If the stack is empty.
66
+
67
+ Time Complexity:
68
+ O(1)
69
+ """
70
+ if self.empty():
71
+ raise EmptyContainerError("stack")
72
+ return self._data[-1]
73
+
74
+ def empty(self) -> bool:
75
+ """
76
+ Check if the stack is empty.
77
+
78
+ Returns:
79
+ True if the stack is empty, False otherwise.
80
+
81
+ Time Complexity:
82
+ O(1)
83
+ """
84
+ return len(self._data) == 0
85
+
86
+ def size(self) -> int:
87
+ """
88
+ Get the number of elements in the stack.
89
+
90
+ Returns:
91
+ The number of elements in the stack.
92
+
93
+ Time Complexity:
94
+ O(1)
95
+ """
96
+ return len(self._data)
97
+
98
+
99
+ __all__ = ['_StackImpl']
@@ -0,0 +1,251 @@
1
+ """
2
+ Private implementation of Vector data structure.
3
+
4
+ This module contains the internal implementation of a dynamic array (vector)
5
+ following C++ STL semantics. Users should not access this directly.
6
+ """
7
+
8
+ from typing import TypeVar, List
9
+ from pythonstl.core.exceptions import EmptyContainerError, OutOfRangeError
10
+ from pythonstl.core.iterator import VectorIterator, VectorReverseIterator
11
+
12
+ T = TypeVar('T')
13
+
14
+
15
+ class _VectorImpl:
16
+ """
17
+ Internal implementation of a vector using Python list with capacity management.
18
+
19
+ This class should not be accessed directly by users.
20
+ Use the facade class `vector` instead.
21
+ """
22
+
23
+ def __init__(self) -> None:
24
+ """
25
+ Initialize an empty vector.
26
+
27
+ Time Complexity:
28
+ O(1)
29
+ """
30
+ self._data: List[T] = []
31
+ self._capacity: int = 0
32
+
33
+ def push_back(self, value: T) -> None:
34
+ """
35
+ Add an element to the end of the vector.
36
+
37
+ Args:
38
+ value: The element to add to the vector.
39
+
40
+ Time Complexity:
41
+ O(1) amortized
42
+ """
43
+ self._data.append(value)
44
+ if len(self._data) > self._capacity:
45
+ self._capacity = max(1, self._capacity * 2)
46
+
47
+ def pop_back(self) -> None:
48
+ """
49
+ Remove the last element from the vector.
50
+
51
+ Raises:
52
+ EmptyContainerError: If the vector is empty.
53
+
54
+ Time Complexity:
55
+ O(1)
56
+ """
57
+ if self.empty():
58
+ raise EmptyContainerError("vector")
59
+ self._data.pop()
60
+
61
+ def at(self, index: int) -> T:
62
+ """
63
+ Access element at specified index with bounds checking.
64
+
65
+ Args:
66
+ index: The index of the element to access.
67
+
68
+ Returns:
69
+ The element at the specified index.
70
+
71
+ Raises:
72
+ OutOfRangeError: If index is out of bounds.
73
+
74
+ Time Complexity:
75
+ O(1)
76
+ """
77
+ if index < 0 or index >= len(self._data):
78
+ raise OutOfRangeError(index, len(self._data))
79
+ return self._data[index]
80
+
81
+ def insert(self, position: int, value: T) -> None:
82
+ """
83
+ Insert an element at the specified position.
84
+
85
+ Args:
86
+ position: The position to insert the element.
87
+ value: The element to insert.
88
+
89
+ Raises:
90
+ OutOfRangeError: If position is out of bounds.
91
+
92
+ Time Complexity:
93
+ O(n) where n is the number of elements after position
94
+ """
95
+ if position < 0 or position > len(self._data):
96
+ raise OutOfRangeError(position, len(self._data))
97
+ self._data.insert(position, value)
98
+ if len(self._data) > self._capacity:
99
+ self._capacity = max(1, self._capacity * 2)
100
+
101
+ def erase(self, position: int) -> None:
102
+ """
103
+ Remove the element at the specified position.
104
+
105
+ Args:
106
+ position: The position of the element to remove.
107
+
108
+ Raises:
109
+ OutOfRangeError: If position is out of bounds.
110
+
111
+ Time Complexity:
112
+ O(n) where n is the number of elements after position
113
+ """
114
+ if position < 0 or position >= len(self._data):
115
+ raise OutOfRangeError(position, len(self._data))
116
+ self._data.pop(position)
117
+
118
+ def clear(self) -> None:
119
+ """
120
+ Remove all elements from the vector.
121
+
122
+ Time Complexity:
123
+ O(n) where n is the number of elements
124
+ """
125
+ self._data.clear()
126
+
127
+ def reserve(self, new_capacity: int) -> None:
128
+ """
129
+ Reserve capacity for the vector.
130
+
131
+ Pre-allocates memory to avoid reallocation during growth.
132
+ Does not change the size of the vector.
133
+
134
+ Args:
135
+ new_capacity: The new capacity to reserve.
136
+
137
+ Time Complexity:
138
+ O(1) - only updates capacity tracking
139
+ """
140
+ if new_capacity > self._capacity:
141
+ self._capacity = new_capacity
142
+
143
+ def shrink_to_fit(self) -> None:
144
+ """
145
+ Reduce capacity to match the current size.
146
+
147
+ Frees unused capacity to save memory.
148
+
149
+ Time Complexity:
150
+ O(1) - only updates capacity tracking
151
+ """
152
+ self._capacity = len(self._data)
153
+
154
+ def begin(self) -> VectorIterator:
155
+ """
156
+ Get iterator to the beginning of the vector.
157
+
158
+ Returns:
159
+ Iterator pointing to the first element.
160
+
161
+ Time Complexity:
162
+ O(1)
163
+ """
164
+ return VectorIterator(self._data, 0)
165
+
166
+ def end(self) -> VectorIterator:
167
+ """
168
+ Get iterator to the end of the vector.
169
+
170
+ Returns:
171
+ Iterator pointing past the last element.
172
+
173
+ Time Complexity:
174
+ O(1)
175
+ """
176
+ return VectorIterator(self._data, len(self._data))
177
+
178
+ def rbegin(self) -> VectorReverseIterator:
179
+ """
180
+ Get reverse iterator to the end of the vector.
181
+
182
+ Returns:
183
+ Reverse iterator pointing to the last element.
184
+
185
+ Time Complexity:
186
+ O(1)
187
+ """
188
+ return VectorReverseIterator(self._data)
189
+
190
+ def rend(self) -> VectorReverseIterator:
191
+ """
192
+ Get reverse iterator to the beginning of the vector.
193
+
194
+ Returns:
195
+ Reverse iterator pointing before the first element.
196
+
197
+ Time Complexity:
198
+ O(1)
199
+ """
200
+ return VectorReverseIterator(self._data, -1)
201
+
202
+ def get_data(self) -> List[T]:
203
+ """
204
+ Get a copy of the internal data for iteration.
205
+
206
+ Returns:
207
+ Copy of the internal data list.
208
+
209
+ Time Complexity:
210
+ O(n) where n is the number of elements
211
+ """
212
+ return self._data.copy()
213
+
214
+ def size(self) -> int:
215
+ """
216
+ Get the number of elements in the vector.
217
+
218
+ Returns:
219
+ The number of elements in the vector.
220
+
221
+ Time Complexity:
222
+ O(1)
223
+ """
224
+ return len(self._data)
225
+
226
+ def capacity(self) -> int:
227
+ """
228
+ Get the current capacity of the vector.
229
+
230
+ Returns:
231
+ The current capacity of the vector.
232
+
233
+ Time Complexity:
234
+ O(1)
235
+ """
236
+ return self._capacity
237
+
238
+ def empty(self) -> bool:
239
+ """
240
+ Check if the vector is empty.
241
+
242
+ Returns:
243
+ True if the vector is empty, False otherwise.
244
+
245
+ Time Complexity:
246
+ O(1)
247
+ """
248
+ return len(self._data) == 0
249
+
250
+
251
+ __all__ = ['_VectorImpl']