docplex.util.collections module

class docplex.util.collections.IndexedSet(initial: AbstractSet[T] | Sequence[T] | Iterable[T] | None = None)[source]

Bases: MutableSet[T], Sequence[T]

An IndexedSet is a custom MutableSet that remembers its order, so that every entry has an index that can be looked up.

Example

>>> IndexedSet([1, 1, 2, 3, 2])
IndexedSet([1, 2, 3])
add(key: T) int[source]

Add key as an item to this IndexedSet, then return its index.

If key is already in the IndexedSet, return the index it already had.

Example

>>> oset = IndexedSet()
>>> oset.append(3)
0
>>> print(oset)
IndexedSet([3])
append(key: T) int

Add key as an item to this IndexedSet, then return its index.

If key is already in the IndexedSet, return the index it already had.

Example

>>> oset = IndexedSet()
>>> oset.append(3)
0
>>> print(oset)
IndexedSet([3])
clear() None[source]

Remove all items from this IndexedSet.

copy() IndexedSet[T][source]

Return a shallow copy of this object.

Example

>>> this = IndexedSet([1, 2, 3])
>>> other = this.copy()
>>> this == other
True
>>> this is other
False
difference(*sets: AbstractSet[T] | Sequence[T]) IndexedSet[T][source]

Returns all elements that are in this set but not the others.

Example

>>> IndexedSet([1, 2, 3]).difference(IndexedSet([2]))
IndexedSet([1, 3])
>>> IndexedSet([1, 2, 3]).difference(IndexedSet([2]), IndexedSet([3]))
IndexedSet([1])
>>> IndexedSet([1, 2, 3]) - IndexedSet([2])
IndexedSet([1, 3])
>>> IndexedSet([1, 2, 3]).difference()
IndexedSet([1, 2, 3])
difference_update(*sets: AbstractSet[T] | Sequence[T]) None[source]

Update this IndexedSet to remove items from one or more other sets.

Example

>>> this = IndexedSet([1, 2, 3])
>>> this.difference_update(IndexedSet([2, 4]))
>>> print(this)
IndexedSet([1, 3])
>>> this = IndexedSet([1, 2, 3, 4, 5])
>>> this.difference_update(IndexedSet([2, 4]), IndexedSet([1, 4, 6]))
>>> print(this)
IndexedSet([3, 5])
discard(key: T) None[source]

Remove an element. Do not raise an exception if absent.

The MutableSet mixin uses this to implement the .remove() method, which does raise an error when asked to remove a non-existent item.

Example

>>> oset = IndexedSet([1, 2, 3])
>>> oset.discard(2)
>>> print(oset)
IndexedSet([1, 3])
>>> oset.discard(2)
>>> print(oset)
IndexedSet([1, 3])
get_indexer(key)

Get the index of a given entry, raising an IndexError if it’s not present.

key can be an iterable of entries that is not a string, in which case this returns a list of indices.

Example

>>> oset = IndexedSet([1, 2, 3])
>>> oset.index(2)
1
get_loc(key)

Get the index of a given entry, raising an IndexError if it’s not present.

key can be an iterable of entries that is not a string, in which case this returns a list of indices.

Example

>>> oset = IndexedSet([1, 2, 3])
>>> oset.index(2)
1
index(key: Sequence[T]) List[int][source]
index(key: T) int

Get the index of a given entry, raising an IndexError if it’s not present.

key can be an iterable of entries that is not a string, in which case this returns a list of indices.

Example

>>> oset = IndexedSet([1, 2, 3])
>>> oset.index(2)
1
intersection(*sets: AbstractSet[T] | Sequence[T]) IndexedSet[T][source]

Returns elements in common between all sets. Order is defined only by the first set.

Example

>>> oset = IndexedSet.intersection(IndexedSet([0, 1, 2, 3]), [1, 2, 3])
>>> print(oset)
IndexedSet([1, 2, 3])
>>> oset.intersection([2, 4, 5], [1, 2, 3, 4])
IndexedSet([2])
>>> oset.intersection()
IndexedSet([1, 2, 3])
intersection_update(other: AbstractSet[T] | Sequence[T]) None[source]

Update this IndexedSet to keep only items in another set, preserving their order in this set.

Example

>>> this = IndexedSet([1, 4, 3, 5, 7])
>>> other = IndexedSet([9, 7, 1, 3, 2])
>>> this.intersection_update(other)
>>> print(this)
IndexedSet([1, 3, 7])
pop(index=-1) T[source]

Remove and return item at index (default last).

Raises KeyError if the set is empty. Raises IndexError if index is out of range.

Example

>>> oset = IndexedSet([1, 2, 3])
>>> oset.pop()
3
symmetric_difference(other: AbstractSet[T] | Sequence[T]) IndexedSet[T][source]

Return the symmetric difference of two IndexedSet as a new set. That is, the new set will contain all elements that are in exactly one of the sets.

Their order will be preserved, with elements from self preceding elements from other.

Example

>>> this = IndexedSet([1, 4, 3, 5, 7])
>>> other = IndexedSet([9, 7, 1, 3, 2])
>>> this.symmetric_difference(other)
IndexedSet([4, 5, 9, 2])
symmetric_difference_update(other: AbstractSet[T] | Sequence[T]) None[source]

Update this IndexedSet to remove items from another set, then add items from the other set that were not present in this set.

Example

>>> this = IndexedSet([1, 4, 3, 5, 7])
>>> other = IndexedSet([9, 7, 1, 3, 2])
>>> this.symmetric_difference_update(other)
>>> print(this)
IndexedSet([4, 5, 9, 2])
union(*sets: AbstractSet[T] | Sequence[T]) IndexedSet[T][source]

Combines all unique items. Each items order is defined by its first appearance.

Example

>>> oset = IndexedSet.union(IndexedSet([3, 1, 4, 1, 5]), [1, 3], [2, 0])
>>> print(oset)
IndexedSet([3, 1, 4, 5, 2, 0])
>>> oset.union([8, 9])
IndexedSet([3, 1, 4, 5, 2, 0, 8, 9])
>>> oset | {10}
IndexedSet([3, 1, 4, 5, 2, 0, 10])
update(sequence: AbstractSet[T] | Sequence[T]) int[source]

Update the set with the given iterable sequence, then return the index of the last element inserted.

Example

>>> oset = IndexedSet([1, 2, 3])
>>> oset.update([3, 1, 5, 1, 4])
4
>>> print(oset)
IndexedSet([1, 2, 3, 5, 4])