MutableIntervalSet

class part.MutableIntervalSet(iterable=None)

Mutable Interval Set class.

The MutableIntervalSet class (which inherits from the IntervalSet class) is designed to hold mutable disjoint sorted intervals.

Note

Les \(n\) (or \(n_0\)) the number of intervals of the self variable and \(m\) the number of intervals in the other variable. Let \(n_1, ... n_k\) the number of intervals for methods with multiple arguments.

The complexity in time of methods is:

Methods

Average case

__getitem__()

\(O(\log(n))\) \(O(n)\)

__iand__()

\(O(\log^2(n)+\log^2(m)))\)

__ior__()

\(O(\log^2(n)+\log^2(m)))\)

__isub__()

\(O(\log^2(n)+\log^2(m)))\)

__ixor__()

\(O(\log^2(n)+\log^2(m)))\)

update()

\(O(\sum_{i=0}^k\log^2(n_i))\)

intersection_update()

\(O(\sum_{i=0}^k\log^2(n_i))\)

difference_update()

\(O(\sum_{i=0}^k\log^2(n_i))\)

symmetric_difference_update()

\(O(\sum_{i=0}^k\log^2(n_i))\)

add()

\(O(\log(n))\)

remove()

\(O(\log(n))\)

discard()

\(O(\log(n))\)

pop()

\(O(\log(n))\)

clear()

\(O(1)\)

Methods

Worst Case (when different of average case)

__iand__()

\(O(m\log(m)+n\log(n))\)

__ior__()

\(O(m\log(m)+n\log(n))\)

__isub__()

\(O(m\log(m)+n\log(n))\)

__ixor__()

\(O(m\log(m)+n\log(n))\)

update()

\(O(\sum_{i=0}^k n_i\log(n_i))\)

intersection_update()

\(O(\sum_{i=0}^k n_i\log(n_i))\)

difference_update()

\(O(\sum_{i=0}^k n_i\log(n_i))\)

symmetric_difference_update()

\(O(\sum_{i=0}^k n_i\log(n_i))\)

__init__(iterable=None)

Initialize a MutableIntervalSet instance.

Parameters

iterable (Iterable) –

An optional iterable of either

Examples

>>> from part import MutableIntervalSet
>>> a = MutableIntervalSet([2, (6, 7), (8, 10, None), (11, 13, True, True)])
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;13]
__ior__(other)

Update self with the union other.

Parameters

other (IntervalSet) – Another interval set.

Returns

The updated MutableIntervalSet.

Return type

MutableIntervalSet

Examples

>>> from part import MutableIntervalSet
>>> a = MutableIntervalSet([2, (6, 7), (8, 10, None), (11, 13, True, True)])
>>> b = MutableIntervalSet([(0, 7), (8, 12)])
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;13]
>>> print(b)
[0;7) | [8;12)
>>> a |= b
>>> print(a)
[0;7) | [8;13]
__iand__(other)

Update self with the intersection with other.

Parameters

other (IntervalSet) – Another interval set.

Returns

The updated MutableIntervalSet.

Return type

MutableIntervalSet

Examples

>>> from part import MutableIntervalSet
>>> a = MutableIntervalSet([2, (6, 7), (8, 10, None), (11, 13, True, True)])
>>> b = MutableIntervalSet([(0, 7), (8, 12)])
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;13]
>>> print(b)
[0;7) | [8;12)
>>> a &= b
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;12)
__isub__(other)

Update self with the difference with other.

Parameters

other (IntervalSet) – Another interval set.

Returns

The updated MutableIntervalSet.

Return type

MutableIntervalSet

Examples

>>> from part import MutableIntervalSet
>>> a = MutableIntervalSet([2, (6, 7), (8, 10, None), (11, 13, True, True)])
>>> b = MutableIntervalSet([(0, 7), (8, 12)])
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;13]
>>> print(b)
[0;7) | [8;12)
>>> a -= b
>>> print(a)
[12;13]
__ixor__(other)

Update self with the symmetric difference with other.

Parameters

other (IntervalSet) – Another interval set.

Returns

The updated MutableIntervalSet.

Return type

MutableIntervalSet

Examples

>>> from part import MutableIntervalSet
>>> a = MutableIntervalSet([2, (6, 7), (8, 10, None), (11, 13, True, True)])
>>> b = MutableIntervalSet([(0, 7), (8, 12)])
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;13]
>>> print(b)
[0;7) | [8;12)
>>> a ^= b
>>> print(a)
[0;2) | (2;6) | [8;8] | [10;11) | [12;13]
update(*args)

Update the set, keeping only elements found in it and all others.

Parameters

*args (Iterable[IntervalValue]) – An iterable of Atomic or valid tuple for an interval creation.

Raises

TypeError – if an argument is not iterable.

intersection_update(*args)

Update the set, keeping only elements found in it and all others.

Parameters

*args (Iterable[IntervalValue]) – An iterable of Atomic or valid tuple for an interval creation.

Raises

TypeError – if an argument is not iterable.

difference_update(*args)

Update the set, removing elements found in others.

Parameters

*args (Iterable[IntervalValue]) – An iterable of Atomic or valid tuple for an interval creation.

Raises

TypeError – if an argument is not iterable.

symmetric_difference_update(other)

Update the set, keeping only elements found in either set, but not in both.

Parameters

other (Iterable[IntervalValue]) – An iterable of Atomic or valid tuple for an interval creation.

Raises

TypeError – if other is not iterable.

add(value)

Add element value to the set.

Parameters

value (IntervalValue) –

The value to add:

Examples

>>> from part import MutableIntervalSet
>>> a = MutableIntervalSet([2, (6, 7), (8, 10, None), (11, 13, True, True)])
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;13]
>>> a.add((2, 6))
>>> print(a)
[2;7) | (8;10) | [11;13]
remove(value)

Remove element value from the set.

Parameters

value (IntervalValue) –

The value to remove:

Raises

KeyError – if value is not contained in the set.

Examples

>>> from part import MutableIntervalSet
>>> a = MutableIntervalSet([2, (6, 7), (8, 10, None), (11, 13, True, True)])
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;13]
>>> try:
...     a.remove((2, 8))
... except KeyError as e:
...     print(e)
'(2, 8)'
discard(value)

Discard element value from the set.

Parameters

value (IntervalValue) –

The value to discard:

Examples

>>> from part import MutableIntervalSet
>>> a = MutableIntervalSet([2, (6, 7), (8, 10, None), (11, 13, True, True)])
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;13]
>>> a.discard((2, 8))
>>> print(a)
(8;10) | [11;13]
pop()

Get the first interval.

Returns

The first interval.

Return type

part.Interval

Raises

KeyError – if the set is empty.

Examples

>>> from part import MutableIntervalSet
>>> a = MutableIntervalSet([2, (6, 7), (8, 10, None), (11, 13, True, True)])
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;13]
>>> print(a.pop())
[2;2]
>>> print(a)
[6;7) | (8;10) | [11;13]
clear()

Remove all elements from the set.

Examples

>>> from part import MutableIntervalSet
>>> a = MutableIntervalSet([2, (6, 7), (8, 10, None), (11, 13, True, True)])
>>> print(a)
[2;2] | [6;7) | (8;10) | [11;13]
>>> a.clear()
>>> print(a)