|
| 1 | +""" |
| 2 | +PySTL v2 Example Usage |
| 3 | +
|
| 4 | +This script demonstrates all features of PySTL v2 including: |
| 5 | +- STL-style operations |
| 6 | +- Python magic methods |
| 7 | +- Iterator support |
| 8 | +- Copy operations |
| 9 | +- New vector methods |
| 10 | +- Priority queue comparators |
| 11 | +""" |
| 12 | + |
| 13 | +from pythonstl import stack, queue, vector, stl_set, stl_map, priority_queue |
| 14 | +from pythonstl import EmptyContainerError, OutOfRangeError, KeyNotFoundError |
| 15 | +from copy import copy, deepcopy |
| 16 | + |
| 17 | + |
| 18 | +def demonstrate_stack(): |
| 19 | + """Demonstrate stack operations with v2 features.""" |
| 20 | + print("=" * 60) |
| 21 | + print("STACK DEMONSTRATION") |
| 22 | + print("=" * 60) |
| 23 | + |
| 24 | + s = stack() |
| 25 | + s.push(10) |
| 26 | + s.push(20) |
| 27 | + s.push(30) |
| 28 | + |
| 29 | + print(f"Stack after pushes: {s}") |
| 30 | + print(f"Top element: {s.top()}") |
| 31 | + print(f"Size (using len()): {len(s)}") |
| 32 | + print(f"Is non-empty (using bool()): {bool(s)}") |
| 33 | + |
| 34 | + # Copy operations |
| 35 | + s_copy = s.copy() |
| 36 | + s_deepcopy = deepcopy(s) |
| 37 | + print(f"Original == Copy: {s == s_copy}") |
| 38 | + |
| 39 | + s.pop() |
| 40 | + print(f"After pop: {s}") |
| 41 | + print() |
| 42 | + |
| 43 | + |
| 44 | +def demonstrate_queue(): |
| 45 | + """Demonstrate queue operations with v2 features.""" |
| 46 | + print("=" * 60) |
| 47 | + print("QUEUE DEMONSTRATION") |
| 48 | + print("=" * 60) |
| 49 | + |
| 50 | + q = queue() |
| 51 | + q.push(1) |
| 52 | + q.push(2) |
| 53 | + q.push(3) |
| 54 | + |
| 55 | + print(f"Queue after pushes: {q}") |
| 56 | + print(f"Front element: {q.front()}") |
| 57 | + print(f"Back element: {q.back()}") |
| 58 | + print(f"Size (using len()): {len(q)}") |
| 59 | + |
| 60 | + # Copy operations |
| 61 | + q_copy = copy(q) |
| 62 | + print(f"Original == Copy: {q == q_copy}") |
| 63 | + |
| 64 | + q.pop() |
| 65 | + print(f"After pop: {q}") |
| 66 | + print() |
| 67 | + |
| 68 | + |
| 69 | +def demonstrate_vector(): |
| 70 | + """Demonstrate vector operations with v2 features.""" |
| 71 | + print("=" * 60) |
| 72 | + print("VECTOR DEMONSTRATION") |
| 73 | + print("=" * 60) |
| 74 | + |
| 75 | + v = vector() |
| 76 | + v.push_back(100) |
| 77 | + v.push_back(200) |
| 78 | + v.push_back(300) |
| 79 | + v.push_back(400) |
| 80 | + |
| 81 | + print(f"Vector: {v}") |
| 82 | + print(f"Size: {len(v)}") |
| 83 | + print(f"Capacity: {v.capacity()}") |
| 84 | + |
| 85 | + # New v2 methods |
| 86 | + v.reserve(1000) |
| 87 | + print(f"After reserve(1000), capacity: {v.capacity()}") |
| 88 | + |
| 89 | + v.shrink_to_fit() |
| 90 | + print(f"After shrink_to_fit(), capacity: {v.capacity()}") |
| 91 | + |
| 92 | + # Python integration |
| 93 | + print(f"200 in vector: {200 in v}") |
| 94 | + print(f"500 in vector: {500 in v}") |
| 95 | + |
| 96 | + # Iterator support |
| 97 | + print("\nForward iteration:") |
| 98 | + for elem in v: |
| 99 | + print(f" {elem}") |
| 100 | + |
| 101 | + print("\nReverse iteration:") |
| 102 | + for elem in v.rbegin(): |
| 103 | + print(f" {elem}") |
| 104 | + |
| 105 | + # Comparison |
| 106 | + v2 = vector() |
| 107 | + v2.push_back(100) |
| 108 | + v2.push_back(200) |
| 109 | + v2.push_back(300) |
| 110 | + v2.push_back(400) |
| 111 | + print(f"\nv == v2: {v == v2}") |
| 112 | + print(f"v < v2: {v < v2}") |
| 113 | + |
| 114 | + # Copy operations |
| 115 | + v_copy = v.copy() |
| 116 | + print(f"Copy successful: {v == v_copy}") |
| 117 | + print() |
| 118 | + |
| 119 | + |
| 120 | +def demonstrate_set(): |
| 121 | + """Demonstrate set operations with v2 features.""" |
| 122 | + print("=" * 60) |
| 123 | + print("SET DEMONSTRATION") |
| 124 | + print("=" * 60) |
| 125 | + |
| 126 | + s = stl_set() |
| 127 | + s.insert(5) |
| 128 | + s.insert(10) |
| 129 | + s.insert(15) |
| 130 | + s.insert(10) # Duplicate, won't be added |
| 131 | + |
| 132 | + print(f"Set: {s}") |
| 133 | + print(f"Size: {len(s)}") |
| 134 | + |
| 135 | + # Python integration |
| 136 | + print(f"10 in set: {10 in s}") |
| 137 | + print(f"20 in set: {20 in s}") |
| 138 | + |
| 139 | + # Iteration |
| 140 | + print("\nIterating over set:") |
| 141 | + for elem in s: |
| 142 | + print(f" {elem}") |
| 143 | + |
| 144 | + # STL-style iteration |
| 145 | + print("\nSTL-style iteration:") |
| 146 | + it = s.begin() |
| 147 | + for elem in it: |
| 148 | + print(f" {elem}") |
| 149 | + |
| 150 | + # Copy operations |
| 151 | + s_copy = deepcopy(s) |
| 152 | + print(f"\nCopy successful: {s == s_copy}") |
| 153 | + |
| 154 | + s.erase(10) |
| 155 | + print(f"After erase(10): {s}") |
| 156 | + print() |
| 157 | + |
| 158 | + |
| 159 | +def demonstrate_map(): |
| 160 | + """Demonstrate map operations with v2 features.""" |
| 161 | + print("=" * 60) |
| 162 | + print("MAP DEMONSTRATION") |
| 163 | + print("=" * 60) |
| 164 | + |
| 165 | + m = stl_map() |
| 166 | + m.insert("apple", 100) |
| 167 | + m.insert("banana", 200) |
| 168 | + m.insert("cherry", 300) |
| 169 | + |
| 170 | + print(f"Map: {m}") |
| 171 | + print(f"Size: {len(m)}") |
| 172 | + |
| 173 | + # Python integration |
| 174 | + print(f"'apple' in map: {'apple' in m}") |
| 175 | + print(f"'orange' in map: {'orange' in m}") |
| 176 | + |
| 177 | + # Access |
| 178 | + print(f"Value for 'banana': {m.at('banana')}") |
| 179 | + |
| 180 | + # Iteration |
| 181 | + print("\nIterating over map:") |
| 182 | + for key, value in m: |
| 183 | + print(f" {key}: {value}") |
| 184 | + |
| 185 | + # Copy operations |
| 186 | + m_copy = m.copy() |
| 187 | + print(f"\nCopy successful: {m == m_copy}") |
| 188 | + |
| 189 | + m.erase("banana") |
| 190 | + print(f"After erase('banana'): {m}") |
| 191 | + print() |
| 192 | + |
| 193 | + |
| 194 | +def demonstrate_priority_queue(): |
| 195 | + """Demonstrate priority queue operations with v2 features.""" |
| 196 | + print("=" * 60) |
| 197 | + print("PRIORITY QUEUE DEMONSTRATION") |
| 198 | + print("=" * 60) |
| 199 | + |
| 200 | + # Max-heap (default) |
| 201 | + pq_max = priority_queue(comparator="max") |
| 202 | + pq_max.push(30) |
| 203 | + pq_max.push(10) |
| 204 | + pq_max.push(50) |
| 205 | + pq_max.push(20) |
| 206 | + |
| 207 | + print("Max-Heap Priority Queue:") |
| 208 | + print(f" {pq_max}") |
| 209 | + print(f" Top element: {pq_max.top()}") |
| 210 | + print(f" Size: {len(pq_max)}") |
| 211 | + |
| 212 | + # Min-heap |
| 213 | + pq_min = priority_queue(comparator="min") |
| 214 | + pq_min.push(30) |
| 215 | + pq_min.push(10) |
| 216 | + pq_min.push(50) |
| 217 | + pq_min.push(20) |
| 218 | + |
| 219 | + print("\nMin-Heap Priority Queue:") |
| 220 | + print(f" {pq_min}") |
| 221 | + print(f" Top element: {pq_min.top()}") |
| 222 | + print(f" Size: {len(pq_min)}") |
| 223 | + |
| 224 | + # Copy operations |
| 225 | + pq_copy = pq_max.copy() |
| 226 | + print(f"\nCopy successful: {pq_max == pq_copy}") |
| 227 | + |
| 228 | + print("\nExtracting from max-heap:") |
| 229 | + while not pq_max.empty(): |
| 230 | + print(f" {pq_max.top()}") |
| 231 | + pq_max.pop() |
| 232 | + print() |
| 233 | + |
| 234 | + |
| 235 | +def demonstrate_error_handling(): |
| 236 | + """Demonstrate error handling.""" |
| 237 | + print("=" * 60) |
| 238 | + print("ERROR HANDLING DEMONSTRATION") |
| 239 | + print("=" * 60) |
| 240 | + |
| 241 | + # Empty container error |
| 242 | + try: |
| 243 | + s = stack() |
| 244 | + s.top() # Error: empty stack |
| 245 | + except EmptyContainerError as e: |
| 246 | + print(f"EmptyContainerError: {e}") |
| 247 | + |
| 248 | + # Out of range error |
| 249 | + try: |
| 250 | + v = vector() |
| 251 | + v.push_back(10) |
| 252 | + v.at(5) # Error: index out of range |
| 253 | + except OutOfRangeError as e: |
| 254 | + print(f"OutOfRangeError: {e}") |
| 255 | + |
| 256 | + # Key not found error |
| 257 | + try: |
| 258 | + m = stl_map() |
| 259 | + m.insert("key1", 100) |
| 260 | + m.at("key2") # Error: key not found |
| 261 | + except KeyNotFoundError as e: |
| 262 | + print(f"KeyNotFoundError: {e}") |
| 263 | + |
| 264 | + print() |
| 265 | + |
| 266 | + |
| 267 | +def demonstrate_copy_semantics(): |
| 268 | + """Demonstrate deep copy vs shallow copy.""" |
| 269 | + print("=" * 60) |
| 270 | + print("COPY SEMANTICS DEMONSTRATION") |
| 271 | + print("=" * 60) |
| 272 | + |
| 273 | + # Create a vector with mutable objects |
| 274 | + v1 = vector() |
| 275 | + v1.push_back([1, 2, 3]) |
| 276 | + v1.push_back([4, 5, 6]) |
| 277 | + |
| 278 | + # Shallow copy (copy.copy) |
| 279 | + v2 = copy(v1) |
| 280 | + |
| 281 | + # Deep copy (copy.deepcopy) |
| 282 | + v3 = deepcopy(v1) |
| 283 | + |
| 284 | + print(f"Original: {v1}") |
| 285 | + print(f"Shallow copy: {v2}") |
| 286 | + print(f"Deep copy: {v3}") |
| 287 | + |
| 288 | + # All are equal |
| 289 | + print(f"\nv1 == v2: {v1 == v2}") |
| 290 | + print(f"v1 == v3: {v1 == v3}") |
| 291 | + |
| 292 | + print("\nNote: All PySTL copy operations are deep copies by default") |
| 293 | + print("to ensure data independence.") |
| 294 | + print() |
| 295 | + |
| 296 | + |
| 297 | +def main(): |
| 298 | + """Run all demonstrations.""" |
| 299 | + print("\n") |
| 300 | + print("╔" + "=" * 58 + "╗") |
| 301 | + print("║" + " " * 12 + "PySTL v2 - Feature Demonstration" + " " * 13 + "║") |
| 302 | + print("╚" + "=" * 58 + "╝") |
| 303 | + print() |
| 304 | + |
| 305 | + demonstrate_stack() |
| 306 | + demonstrate_queue() |
| 307 | + demonstrate_vector() |
| 308 | + demonstrate_set() |
| 309 | + demonstrate_map() |
| 310 | + demonstrate_priority_queue() |
| 311 | + demonstrate_error_handling() |
| 312 | + demonstrate_copy_semantics() |
| 313 | + |
| 314 | + print("=" * 60) |
| 315 | + print("All demonstrations completed successfully!") |
| 316 | + print("=" * 60) |
| 317 | + |
| 318 | + |
| 319 | +if __name__ == "__main__": |
| 320 | + main() |
0 commit comments