API reference¶
- class linkedset.DoublyLinkedSet(values=None)[source]¶
Bases:
Sequence[_T],MutableSet[_T],Generic[_T]A doubly linked ordered set of nodes.
The container is both a
collections.abc.Sequence(ordered, indexable) and acollections.abc.MutableSet. It does not allow duplicate values and maintains insertion order. One can treat it as a doubly linked list with list-like methods, or as a mutable set supporting&,|,-,^and their in-place variants.Adding and removing elements from the set during iteration is safe. Moving elements from one set to another is also safe.
During the iteration:
If new elements are inserted after the current node, the iterator will iterate over them as well.
If new elements are inserted before the current node, they will not be iterated over in this iteration.
If the current node is lifted and inserted in a different location, iteration will start from the “next” node at the _original_ location.
- Time complexity:
Inserting and removing nodes from the set is O(1). Accessing nodes by index is O(n), although accessing nodes at either end of the set is O(1). I.e.
linked_set[0]andlinked_set[-1]are O(1).
Membership, uniqueness and set operations are based on value equality (
==), not object identity. Two objects that compare equal are treated as the same element, and elements must be hashable (they are used as keys in an internaldictthat givesO(1)membership and mutation). Because it is a mutable set, instances ofDoublyLinkedSetare themselves not hashable. Since the set is ordered,==is order-sensitive: two sets are equal only when they hold the same elements (by value) in the same order.Noneis not a valid value in the set.- Parameters:
values (Iterable[_T] | None)
- __iter__()[source]¶
Iterate over the elements in the list.
If new elements are inserted after the current node, the iterator will iterate over them as well.
If new elements are inserted before the current node, they will not be iterated over in this iteration.
If the current node is lifted and inserted in a different location, iteration will start from the “next” node at the _original_ location.
- Return type:
Iterator[_T]
- __reversed__()[source]¶
Iterate over the elements in the list in reverse order.
- Return type:
Iterator[_T]
- __eq__(other)[source]¶
Return whether
otheris an equal, equally-ordered set.Because
DoublyLinkedSetis ordered, equality is order-sensitive (unlike a plainset). Two sets are equal only when they contain the same elements by value equality, in the same order. Only anotherDoublyLinkedSetcan compare equal.
- count(value)[source]¶
Return the number of occurrences of
value(0 or 1) using value equality (O(1)).
- index(value, start=0, stop=None)[source]¶
Return the index of
valuein the set using value equality.- Raises:
ValueError – If
valueis not present inself[start:stop].- Parameters:
- Return type:
- __getitem__(index: int) _T[source]¶
- __getitem__(index: slice) Sequence[_T]
Get the node at the given index.
Complexity is O(n).
- add(value)[source]¶
Add
valueto the end of the set if it is not already present.Unlike
append(), this is idempotent: ifvalue(by equality) is already in the set, its position is left unchanged. This implementscollections.abc.MutableSet.add().- Parameters:
value (_T)
- Return type:
None
- discard(value)[source]¶
Remove
valuefrom the set if it is present, without raising otherwise.This implements
collections.abc.MutableSet.discard().- Parameters:
value (_T)
- Return type:
None
- appendleft(value)[source]¶
Add
valueto the front of the set (deque-style).If
valueis already present (by equality), it is moved to the front.- Parameters:
value (_T)
- Return type:
None
- extendleft(values)[source]¶
Prepend each value in
valuesto the front of the set (deque-style).As with
collections.deque.extendleft(), the prepended items end up in reverse order relative tovalues.- Parameters:
values (Iterable[_T])
- Return type:
None
- insert(index, value)[source]¶
Insert
valuebefore positionindex(list/deque-style).indexis clamped likelist.insert(). Ifvalueis already present it is removed first, soindexrefers to a position in the resulting sequence.- Parameters:
index (int)
value (_T)
- Return type:
None
- pop(index=-1)[source]¶
Remove and return the value at
index(default: the last one, list-style).- Raises:
IndexError – If the set is empty or
indexis out of range.- Parameters:
index (int)
- Return type:
_T
- popleft()[source]¶
Remove and return the first value (deque-style).
- Raises:
IndexError – If the set is empty.
- Return type:
_T
- clear()[source]¶
Remove all elements from the set.
Safe to call during iteration: existing nodes are marked erased (so a live iterator stops yielding them) before the set is reset.
- Return type:
None
- reverse()[source]¶
Reverse the elements of the set in place.
Note
Unlike per-element mutation, this is a global reorder and is not safe to call while iterating over the set: an in-progress iterator may then skip or repeat elements.
- Return type:
None
- rotate(n=1)[source]¶
Rotate the set
nsteps to the right (deque-style).Negative
nrotates to the left.rotate(1)moves the last element to the front.Note
Like
reverse(), this is a global reorder and is not safe to call while iterating over the set.- Parameters:
n (int)
- Return type:
None
- copy()[source]¶
Return a shallow copy of the set, preserving order.
- Return type:
DoublyLinkedSet[_T]
- isdisjoint(other)[source]¶
Return
Trueif the set has no elements (by equality) in common withother.