pythonstl 0.1.0__py3-none-any.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 +45 -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/map.py +270 -0
- pythonstl/facade/priority_queue.py +200 -0
- pythonstl/facade/queue.py +203 -0
- pythonstl/facade/set.py +247 -0
- pythonstl/facade/stack.py +188 -0
- pythonstl/facade/vector.py +369 -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 +139 -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.0.dist-info/METADATA +437 -0
- pythonstl-0.1.0.dist-info/RECORD +27 -0
- pythonstl-0.1.0.dist-info/WHEEL +5 -0
- pythonstl-0.1.0.dist-info/licenses/LICENSE +21 -0
- pythonstl-0.1.0.dist-info/top_level.txt +1 -0
pythonstl/__init__.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
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.0"
|
|
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
|
+
|
|
21
|
+
# Also export exceptions for user error handling
|
|
22
|
+
from pythonstl.core.exceptions import (
|
|
23
|
+
PySTLException,
|
|
24
|
+
EmptyContainerError,
|
|
25
|
+
OutOfRangeError,
|
|
26
|
+
KeyNotFoundError
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
# Data structures
|
|
31
|
+
'stack',
|
|
32
|
+
'queue',
|
|
33
|
+
'vector',
|
|
34
|
+
'stl_set',
|
|
35
|
+
'stl_map',
|
|
36
|
+
'priority_queue',
|
|
37
|
+
# Exceptions
|
|
38
|
+
'PySTLException',
|
|
39
|
+
'EmptyContainerError',
|
|
40
|
+
'OutOfRangeError',
|
|
41
|
+
'KeyNotFoundError',
|
|
42
|
+
# Metadata
|
|
43
|
+
'__version__',
|
|
44
|
+
'__author__'
|
|
45
|
+
]
|
|
@@ -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, Any
|
|
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']
|
pythonstl/facade/map.py
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Map facade class.
|
|
3
|
+
|
|
4
|
+
This module provides the public-facing map class that users interact with.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import TypeVar, Iterator as TypingIterator, Tuple
|
|
8
|
+
from copy import deepcopy
|
|
9
|
+
from pythonstl.implementations.associative._map_impl import _MapImpl
|
|
10
|
+
from pythonstl.core.iterator import MapIterator
|
|
11
|
+
|
|
12
|
+
K = TypeVar('K')
|
|
13
|
+
V = TypeVar('V')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class stl_map:
|
|
17
|
+
"""
|
|
18
|
+
A map data structure following C++ STL semantics.
|
|
19
|
+
|
|
20
|
+
This is an associative container that stores key-value pairs.
|
|
21
|
+
Named 'stl_map' to avoid potential conflicts.
|
|
22
|
+
|
|
23
|
+
Example:
|
|
24
|
+
>>> from pythonstl import stl_map
|
|
25
|
+
>>> m = stl_map()
|
|
26
|
+
>>> m.insert("key1", 100)
|
|
27
|
+
>>> m.insert("key2", 200)
|
|
28
|
+
>>> m.at("key1")
|
|
29
|
+
100
|
|
30
|
+
>>> "key1" in m
|
|
31
|
+
True
|
|
32
|
+
>>> len(m)
|
|
33
|
+
2
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self) -> None:
|
|
37
|
+
"""
|
|
38
|
+
Initialize an empty map.
|
|
39
|
+
|
|
40
|
+
Time Complexity:
|
|
41
|
+
O(1)
|
|
42
|
+
"""
|
|
43
|
+
self._impl = _MapImpl()
|
|
44
|
+
|
|
45
|
+
def insert(self, key: K, value: V) -> None:
|
|
46
|
+
"""
|
|
47
|
+
Insert a key-value pair into the map.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
key: The key to insert.
|
|
51
|
+
value: The value associated with the key.
|
|
52
|
+
|
|
53
|
+
Note:
|
|
54
|
+
If the key already exists, the value is updated.
|
|
55
|
+
|
|
56
|
+
Time Complexity:
|
|
57
|
+
O(1) average case
|
|
58
|
+
"""
|
|
59
|
+
self._impl.insert(key, value)
|
|
60
|
+
|
|
61
|
+
def erase(self, key: K) -> None:
|
|
62
|
+
"""
|
|
63
|
+
Remove a key-value pair from the map.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
key: The key to remove.
|
|
67
|
+
|
|
68
|
+
Note:
|
|
69
|
+
Does nothing if the key is not present (matches C++ STL behavior).
|
|
70
|
+
|
|
71
|
+
Time Complexity:
|
|
72
|
+
O(1) average case
|
|
73
|
+
"""
|
|
74
|
+
self._impl.erase(key)
|
|
75
|
+
|
|
76
|
+
def find(self, key: K) -> bool:
|
|
77
|
+
"""
|
|
78
|
+
Check if a key exists in the map.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
key: The key to search for.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
True if the key exists, False otherwise.
|
|
85
|
+
|
|
86
|
+
Time Complexity:
|
|
87
|
+
O(1) average case
|
|
88
|
+
"""
|
|
89
|
+
return self._impl.find(key)
|
|
90
|
+
|
|
91
|
+
def at(self, key: K) -> V:
|
|
92
|
+
"""
|
|
93
|
+
Access the value associated with a key.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
key: The key to access.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
The value associated with the key.
|
|
100
|
+
|
|
101
|
+
Raises:
|
|
102
|
+
KeyNotFoundError: If the key does not exist.
|
|
103
|
+
|
|
104
|
+
Time Complexity:
|
|
105
|
+
O(1) average case
|
|
106
|
+
"""
|
|
107
|
+
return self._impl.at(key)
|
|
108
|
+
|
|
109
|
+
def empty(self) -> bool:
|
|
110
|
+
"""
|
|
111
|
+
Check if the map is empty.
|
|
112
|
+
|
|
113
|
+
Returns:
|
|
114
|
+
True if the map is empty, False otherwise.
|
|
115
|
+
|
|
116
|
+
Time Complexity:
|
|
117
|
+
O(1)
|
|
118
|
+
"""
|
|
119
|
+
return self._impl.empty()
|
|
120
|
+
|
|
121
|
+
def size(self) -> int:
|
|
122
|
+
"""
|
|
123
|
+
Get the number of key-value pairs in the map.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
The number of key-value pairs in the map.
|
|
127
|
+
|
|
128
|
+
Time Complexity:
|
|
129
|
+
O(1)
|
|
130
|
+
"""
|
|
131
|
+
return self._impl.size()
|
|
132
|
+
|
|
133
|
+
def begin(self) -> MapIterator:
|
|
134
|
+
"""
|
|
135
|
+
Get iterator to the beginning of the map.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
Iterator pointing to the first key-value pair.
|
|
139
|
+
|
|
140
|
+
Time Complexity:
|
|
141
|
+
O(1)
|
|
142
|
+
"""
|
|
143
|
+
return self._impl.begin()
|
|
144
|
+
|
|
145
|
+
def end(self) -> MapIterator:
|
|
146
|
+
"""
|
|
147
|
+
Get iterator to the end of the map.
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
Iterator pointing past the last key-value pair.
|
|
151
|
+
|
|
152
|
+
Time Complexity:
|
|
153
|
+
O(1)
|
|
154
|
+
"""
|
|
155
|
+
return self._impl.end()
|
|
156
|
+
|
|
157
|
+
def copy(self) -> 'stl_map':
|
|
158
|
+
"""
|
|
159
|
+
Create a deep copy of the map.
|
|
160
|
+
|
|
161
|
+
Returns:
|
|
162
|
+
A new map with copied key-value pairs.
|
|
163
|
+
|
|
164
|
+
Time Complexity:
|
|
165
|
+
O(n) where n is the number of key-value pairs
|
|
166
|
+
"""
|
|
167
|
+
new_map = stl_map()
|
|
168
|
+
for key, value in self:
|
|
169
|
+
new_map.insert(key, value)
|
|
170
|
+
return new_map
|
|
171
|
+
|
|
172
|
+
# Python magic methods
|
|
173
|
+
|
|
174
|
+
def __len__(self) -> int:
|
|
175
|
+
"""
|
|
176
|
+
Get the number of key-value pairs (Python len() support).
|
|
177
|
+
|
|
178
|
+
Returns:
|
|
179
|
+
The number of key-value pairs in the map.
|
|
180
|
+
"""
|
|
181
|
+
return self.size()
|
|
182
|
+
|
|
183
|
+
def __bool__(self) -> bool:
|
|
184
|
+
"""
|
|
185
|
+
Check if map is non-empty (Python bool() support).
|
|
186
|
+
|
|
187
|
+
Returns:
|
|
188
|
+
True if map is non-empty, False otherwise.
|
|
189
|
+
"""
|
|
190
|
+
return not self.empty()
|
|
191
|
+
|
|
192
|
+
def __contains__(self, key: K) -> bool:
|
|
193
|
+
"""
|
|
194
|
+
Check if key exists in map (Python 'in' operator support).
|
|
195
|
+
|
|
196
|
+
Args:
|
|
197
|
+
key: The key to search for.
|
|
198
|
+
|
|
199
|
+
Returns:
|
|
200
|
+
True if key exists, False otherwise.
|
|
201
|
+
|
|
202
|
+
Time Complexity:
|
|
203
|
+
O(1) average case
|
|
204
|
+
"""
|
|
205
|
+
return self.find(key)
|
|
206
|
+
|
|
207
|
+
def __repr__(self) -> str:
|
|
208
|
+
"""
|
|
209
|
+
Get string representation of the map.
|
|
210
|
+
|
|
211
|
+
Returns:
|
|
212
|
+
String representation showing all key-value pairs.
|
|
213
|
+
"""
|
|
214
|
+
pairs = [f"{k}: {v}" for k, v in self]
|
|
215
|
+
return f"stl_map({{{', '.join(pairs)}}})"
|
|
216
|
+
|
|
217
|
+
def __eq__(self, other: object) -> bool:
|
|
218
|
+
"""
|
|
219
|
+
Check equality with another map.
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
other: Another map to compare with.
|
|
223
|
+
|
|
224
|
+
Returns:
|
|
225
|
+
True if maps are equal, False otherwise.
|
|
226
|
+
"""
|
|
227
|
+
if not isinstance(other, stl_map):
|
|
228
|
+
return False
|
|
229
|
+
if self.size() != other.size():
|
|
230
|
+
return False
|
|
231
|
+
for key, value in self:
|
|
232
|
+
if not other.find(key) or other.at(key) != value:
|
|
233
|
+
return False
|
|
234
|
+
return True
|
|
235
|
+
|
|
236
|
+
def __iter__(self) -> TypingIterator[Tuple[K, V]]:
|
|
237
|
+
"""
|
|
238
|
+
Get Python iterator for the map.
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
Iterator over key-value pairs as tuples.
|
|
242
|
+
"""
|
|
243
|
+
return iter(self._impl.get_data().items())
|
|
244
|
+
|
|
245
|
+
def __copy__(self) -> 'stl_map':
|
|
246
|
+
"""
|
|
247
|
+
Support for copy.copy().
|
|
248
|
+
|
|
249
|
+
Returns:
|
|
250
|
+
A shallow copy of the map.
|
|
251
|
+
"""
|
|
252
|
+
return self.copy()
|
|
253
|
+
|
|
254
|
+
def __deepcopy__(self, memo) -> 'stl_map':
|
|
255
|
+
"""
|
|
256
|
+
Support for copy.deepcopy().
|
|
257
|
+
|
|
258
|
+
Args:
|
|
259
|
+
memo: Memoization dictionary for deepcopy.
|
|
260
|
+
|
|
261
|
+
Returns:
|
|
262
|
+
A deep copy of the map.
|
|
263
|
+
"""
|
|
264
|
+
new_map = stl_map()
|
|
265
|
+
for key, value in self:
|
|
266
|
+
new_map.insert(deepcopy(key, memo), deepcopy(value, memo))
|
|
267
|
+
return new_map
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
__all__ = ['stl_map']
|