Ordered Set
LAST = ~0
module-attribute
The last index.
ordered_set = OrderedSet
module-attribute
An alias of OrderedSet
.
ordered_set_unchecked = ordered_set.create_unchecked
module-attribute
An alias of ordered_set.create_unchecked
.
OrderedSet
Bases: MutableSet[Q]
, Sequence[Q]
Represents ordered sets, i.e. mutable hash sets that preserve insertion order.
The implementation is rather simple: it uses an array to store the items and a hash map to store the indices of the items in the array along with ensuring uniqueness.
The complexity of the operations assumes that hash maps
have O(1)
insertion, lookup and deletion as well
as that arrays have O(1)
by-index lookup and length-checking.
It is assumed that clearing is O(n)
, where n
is the number of elements.
Source code in iters/ordered_set.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
|
append = add
class-attribute
instance-attribute
An alias of add
.
extend = update
class-attribute
instance-attribute
An alias of update
.
get_index = wrap_option(index)
class-attribute
instance-attribute
An alias of index
wrapped to return
Option[int]
instead of erroring.
get_pop = wrap_option(pop)
class-attribute
instance-attribute
union = mixed_method(create_union, apply_union)
class-attribute
instance-attribute
Mixes create_union
and apply_union
.
intersection = mixed_method(create_intersection, apply_intersection)
class-attribute
instance-attribute
Mixes create_intersection
and apply_intersection
.
difference = mixed_method(create_difference, apply_difference)
class-attribute
instance-attribute
Mixes create_difference
and apply_difference
.
symmetric_difference = mixed_method(create_symmetric_difference, apply_symmetric_difference)
class-attribute
instance-attribute
create(iterable: Iterable[R] = ()) -> OrderedSet[R]
classmethod
Creates an ordered set from an iterable.
Complexity
O(n)
, where n
is the length of the iterable.
Example
>>> array = [0, 1, 1, 0, 1, 1, 1, 0]
>>> order_set = ordered_set.create(array)
>>> order_set
OrderedSet([0, 1])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable |
Iterable[R]
|
The iterable to create the ordered set from. |
()
|
Returns:
Type | Description |
---|---|
OrderedSet[R]
|
The created ordered set. |
Source code in iters/ordered_set.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
create_unchecked(iterable: Iterable[R] = ()) -> OrderedSet[R]
classmethod
Creates an ordered set from an iterable without checking if the items are unique.
This method is useful when constructing an ordered set from an iterable that is known to contain unique items only.
Complexity
O(n)
, where n
is the length of the iterable.
Example
>>> array = [1, 2, 3] # we know that the items are unique
>>> order_set = ordered_set.create_unchecked(array)
>>> order_set
OrderedSet([1, 2, 3])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable |
Iterable[R]
|
The iterable to create the ordered set from. |
()
|
Returns:
Type | Description |
---|---|
OrderedSet[R]
|
The created ordered set. |
Source code in iters/ordered_set.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
create_union(*iterables: Iterable[R]) -> OrderedSet[R]
classmethod
Creates an ordered set that is the union of given iterables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[R]
|
The iterables to create the ordered set union from. |
()
|
Returns:
Type | Description |
---|---|
OrderedSet[R]
|
The ordered set union. |
Source code in iters/ordered_set.py
122 123 124 125 126 127 128 129 130 131 132 |
|
create_intersection(*iterables: Iterable[R]) -> OrderedSet[R]
classmethod
Creates an ordered set that is the intersection of given iterables.
The order is determined by the first iterable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[R]
|
The iterables to create the ordered set intersection from. |
()
|
Returns:
Type | Description |
---|---|
OrderedSet[R]
|
The ordered set intersection. |
Source code in iters/ordered_set.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
create_difference(*iterables: Iterable[R]) -> OrderedSet[R]
classmethod
Creates an ordered set that is the difference of given iterables.
The order is determined by the first iterable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[R]
|
The iterables to create the orderd set difference from. |
()
|
Returns:
Type | Description |
---|---|
OrderedSet[R]
|
The ordered set difference. |
Source code in iters/ordered_set.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
create_symmetric_difference(*iterables: Iterable[R]) -> OrderedSet[R]
classmethod
Creates an ordered set that is the symmetric difference of given iterables.
The order is determined by the first iterable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[R]
|
The iterables to create the ordered set symmetric difference from. |
()
|
Returns:
Type | Description |
---|---|
OrderedSet[R]
|
The ordered set symmetric difference. |
Source code in iters/ordered_set.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
copy() -> OrderedSet[Q]
Copies the ordered set.
This is equivalent to:
order_set.create_unchecked(order_set)
Complexity
O(n)
, where n
is the length of the ordered set.
Example
>>> order_set = ordered_set([1, 2, 3])
>>> order_set
OrderedSet([1, 2, 3])
>>> order_set.copy()
OrderedSet([1, 2, 3])
Returns:
Type | Description |
---|---|
OrderedSet[Q]
|
The copied ordered set. |
Source code in iters/ordered_set.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
add(item: Q) -> None
Adds an item to the ordered set.
Complexity
O(1)
.
Example
>>> order_set = ordered_set()
>>> order_set
OrderedSet()
>>> order_set.add(0)
>>> order_set.add(1)
>>> order_set.add(0)
>>> order_set
OrderedSet([0, 1])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
Q
|
The item to add. |
required |
Source code in iters/ordered_set.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
update(iterable: Iterable[Q]) -> None
Updates the ordered set with the items from an iterable.
This is equivalent to:
for item in iterable:
ordered_set.add(item)
Complexity
O(n)
, where n
is the length of the iterable.
Example
>>> order_set = ordered_set()
>>> order_set.update([0, 1])
>>> order_set.update([1, 2, 3])
>>> order_set
OrderedSet([0, 1, 2, 3])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable |
Iterable[Q]
|
The iterable to update the ordered set with. |
required |
Source code in iters/ordered_set.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
index(item: Q, start: Optional[int] = None, stop: Optional[int] = None) -> int
Gets the index of an item in the ordered set.
Complexity
O(1)
.
Example
>>> order_set = ordered_set([1, 2, 3])
>>> order_set.index(1)
0
>>> order_set.index(5)
Traceback (most recent call last):
...
ValueError: 5 is not in the ordered set
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
Q
|
The item to get the index of. |
required |
start |
Optional[int]
|
The index to start searching from. |
None
|
stop |
Optional[int]
|
The index to stop searching at. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
The item is not in the ordered set. |
Returns:
Type | Description |
---|---|
int
|
The index of the item. |
Source code in iters/ordered_set.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
count(item: Q) -> int
Returns 1
if an item is in the ordered set, 0
otherwise.
Complexity
O(1)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
Q
|
The item to count. |
required |
Returns:
Type | Description |
---|---|
int
|
|
Source code in iters/ordered_set.py
353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
pop(index: int = LAST) -> Q
Pops an item from the ordered set at index
.
Complexity
O(n)
, see discard
.
Example
>>> order_set = ordered_set([0, 1])
>>> order_set.pop()
1
>>> order_set.pop(0)
0
>>> order_set.pop()
Traceback (most recent call last):
...
IndexError: list index out of range
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int
|
The index to pop the item from. |
LAST
|
Raises:
Type | Description |
---|---|
IndexError
|
The index is out of range. |
Returns:
Type | Description |
---|---|
Q
|
The popped item. |
Source code in iters/ordered_set.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
discard(item: Q) -> None
Discards an item from the ordered set.
Complexity
O(n)
, where n
is the length of the ordered set.
This is because all indices after the removed index must be decremented.
Example
>>> order_set = ordered_set([0, 1])
>>> order_set.discard(1)
>>> order_set
OrderedSet([0])
>>> order_set.discard(1)
>>> order_set.discard(0)
>>> order_set
OrderedSet()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
Q
|
The item to discard. |
required |
Source code in iters/ordered_set.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
|
remove(item: Q) -> None
A checked version of discard
.
Complexity: O(n)
, see discard
.
Example
>>> order_set = ordered_set([0, 1])
>>> order_set.remove(1)
>>> order_set
OrderedSet([0])
>>> order_set.remove(1)
Traceback (most recent call last):
...
ValueError: 1 is not in the ordered set
>>> order_set.remove(0)
>>> order_set
OrderedSet()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
Q
|
The item to remove. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
The item is not in the ordered set. |
Source code in iters/ordered_set.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
|
insert(index: int, item: Q) -> None
Inserts an item into the ordered set at index
.
Complexity
O(n)
, where n
is the length of the ordered set.
This is because all indices after the inserted index must be incremented.
Example
>>> order_set = ordered_set([1, 3])
>>> order_set.insert(1, 2)
>>> order_set
OrderedSet([1, 2, 3])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int
|
The index to insert the item at. |
required |
item |
Q
|
The item to insert. |
required |
Source code in iters/ordered_set.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
|
clear() -> None
Clears the ordered set.
Complexity
O(n)
.
Source code in iters/ordered_set.py
511 512 513 514 515 516 517 518 |
|
apply_union(*iterables: Iterable[Q]) -> OrderedSet[Q]
Returns the union of the ordered set and iterables
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[Q]
|
The iterables to find the union with. |
()
|
Returns:
Type | Description |
---|---|
OrderedSet[Q]
|
The union of the ordered set and |
Source code in iters/ordered_set.py
545 546 547 548 549 550 551 552 553 554 555 556 557 |
|
apply_intersection(*iterables: Iterable[Q]) -> OrderedSet[Q]
Returns the intersection of the ordered set and iterables
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[Q]
|
The iterables to find the intersection with. |
()
|
Returns:
Type | Description |
---|---|
OrderedSet[Q]
|
The intersection of the ordered set and |
Source code in iters/ordered_set.py
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
|
intersection_update(*iterables: Iterable[Q]) -> None
Updates the ordered set to be the intersection of itself and iterables
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[Q]
|
The iterables to find the intersection with. |
()
|
Source code in iters/ordered_set.py
587 588 589 590 591 592 593 594 595 596 597 598 |
|
apply_difference(*iterables: Iterable[Q]) -> OrderedSet[Q]
Returns the difference of the ordered set and iterables
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[Q]
|
The iterables to find the difference with. |
()
|
Returns:
Type | Description |
---|---|
OrderedSet[Q]
|
The difference of the ordered set and |
Source code in iters/ordered_set.py
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
|
difference_update(*iterables: Iterable[Q]) -> None
Updates the ordered set to be the difference of itself and iterables
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[Q]
|
The iterables to find the difference with. |
()
|
Source code in iters/ordered_set.py
622 623 624 625 626 627 628 629 630 631 632 633 |
|
apply_symmetric_difference(*iterables: Iterable[Q]) -> OrderedSet[Q]
Returns the symmetric difference of the ordered set and iterables
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[Q]
|
The iterables to find the symmetric difference with. |
()
|
Returns:
Type | Description |
---|---|
OrderedSet[Q]
|
The symmetric difference of the ordered set and |
Source code in iters/ordered_set.py
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
|
symmetric_difference_update(*iterables: Iterable[Q]) -> None
Updates the ordered set to be the symmetric difference of itself and iterables
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*iterables |
Iterable[Q]
|
The iterables to find the symmetric difference with. |
()
|
Source code in iters/ordered_set.py
665 666 667 668 669 670 671 672 673 674 675 676 |
|
is_subset(other: Iterable[Q]) -> bool
Checks if the ordered set is a subset of other
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Iterable[Q]
|
The iterable to check if the ordered set is a subset of. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the ordered set is a subset of |
Source code in iters/ordered_set.py
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
|
is_strict_subset(other: Iterable[Q]) -> bool
Checks if the ordered set is a strict subset of other
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Iterable[Q]
|
The iterable to check if the ordered set is a strict subset of. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the ordered set is a strict subset of |
Source code in iters/ordered_set.py
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
|
is_superset(other: Iterable[Q]) -> bool
Checks if the ordered set is a superset of other
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Iterable[Q]
|
The iterable to check if the ordered set is a superset of. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the ordered set is a superset of |
Source code in iters/ordered_set.py
718 719 720 721 722 723 724 725 726 727 728 729 730 |
|
is_strict_superset(other: Iterable[Q]) -> bool
Checks if the ordered set is a strict superset of other
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Iterable[Q]
|
The iterable to check if the ordered set is a strict superset of. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the ordered set is a strict superset of |
Source code in iters/ordered_set.py
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 |
|
is_disjoint(other: Iterable[Q]) -> bool
Checks if the ordered set is disjoint with other
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Iterable[Q]
|
The iterable to check if the ordered set is disjoint with. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the ordered set is disjoint with |
Source code in iters/ordered_set.py
748 749 750 751 752 753 754 755 756 757 |
|