MutableIntervalDict

class part.MutableIntervalDict(iterable=None, default=None, operator=None, strict=True)

Mutable Interval Dictionary class.

The MutableIntervalDict class (which inherits from the IntervalDict class) is designed to hold mutable dict of 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

__setitem__()

\(O(n)\)

__delitem__()

\(O(n)\)

__ior__()

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

update()

\(O((\sum_{i=1}^kn_i)\log(\sum_{i=0}^kn_i))\)

clear()

\(O(1)\)

__init__(iterable=None, default=None, operator=None, strict=True)

Initialize a MutableIntervalDict instance.

Parameters

iterable (Iterable) – An optional iterable that can be converted to a dictionary of ( interval, value).

Keyword Arguments
  • default (Callable[[], V], optional) – The default factory.

  • operator (Callable[[V, V], V]) – The operator function.

  • strict (bool) – False if operator is a commutative and associative law on V.

Note

If operator is a commutative and associative law on V, the complexity in time is much faster if strict is set to False.

Examples

>>> from part import MutableIntervalDict
>>> a = MutableIntervalDict[int, set](
...     operator=lambda x, y: x | y,
...     strict=False
... )
>>> a.update({(1, 10): {1}})
>>> print(a)
{'[1;10)': {1}}
>>> a.update({(5, 20): {2}})
>>> print(a)
{'[1;5)': {1}, '[5;10)': {1, 2}, '[10;20)': {2}}
>>> a.update({(10, 30): {1}})
>>> print(a)
{'[1;5)': {1}, '[5;10)': {1, 2}, '[10;20)': {1, 2}, '[20;30)': {1}}
>>> print(a.compress())
{'[1;5)': {1}, '[5;20)': {1, 2}, '[20;30)': {1}}
__getitem__(key)

Return a value using either a slice or an interval value.

Parameters

key (Union[IntervalValue, slice]) – The interval requested.

Returns

Return type

The found value

Raises

KeyError – If the key is out of range.

__setitem__(key, value)

Set a value using either a slice or an interval value.

Parameters

key (Union[IntervalValue, slice]) – The interval requested.

Raises

KeyError – If the key is out of range.

Examples

>>> from part import MutableIntervalDict
>>> a = MutableIntervalDict[int, int](
...     {(10, 15): 1, (20, 25): 2, (30, 35): 3}
... )
>>> print(a)
{'[10;15)': 1, '[20;25)': 2, '[30;35)': 3}
>>> a[12] = 4
>>> print(a)
{'[10;12)': 1, '[12;12]': 4, '(12;15)': 1, '[20;25)': 2, '[30;35)': 3}
>>> a[13:31] = 5
>>> print(a)
{'[10;12)': 1, '[12;12]': 4, '(12;13)': 1, '[13;31)': 5, '[31;35)': 3}
>>> a[:] = 0
>>> print(a)
{'(-inf;+inf)': 0}
__delitem__(key)

Delete a value using either a slice or an interval value.

Parameters

key (Union[IntervalValue, slice]) – The interval requested.

Raises

KeyError – If the key is out of range.

Examples

>>> from part import MutableIntervalDict
>>> a = MutableIntervalDict[int, int](
...     {(10, 15): 1, (20, 25): 2, (30, 35): 3}
... )
>>> print(a)
{'[10;15)': 1, '[20;25)': 2, '[30;35)': 3}
>>> del a[12]
>>> print(a)
{'[10;12)': 1, '(12;15)': 1, '[20;25)': 2, '[30;35)': 3}
>>> del a[13:31]
>>> print(a)
{'[10;12)': 1, '(12;13)': 1, '[31;35)': 3}
>>> del a[:]
>>> print(a)
{}
__or__(other)

Construct a new dictionary using self and the other.

Parameters

other (IntervalDict) – Another interval dict.

Returns

The new IntervalDict.

Return type

MutableIntervalDict

Examples

>>> from part import MutableIntervalDict
>>> a = MutableIntervalDict[int, int](
...     {(10, 15): 1, (20, 25): 2, (30, 35): 3},
...     operator=lambda x, y: x + y,
...     strict=False
... )
>>> print(a | FrozenIntervalDict[int, int]({(15, 22): 4}))
{'[10;15)': 1, '[15;20)': 4, '[20;22)': 6, '[22;25)': 2, '[30;35)': 3}
__ior__(other)

Update self with the other.

Parameters

other (IntervalDict) – Another interval dict.

Returns

The updated MutableIntervalDict.

Return type

MutableIntervalDict

Examples

>>> from part import MutableIntervalDict
>>> a = MutableIntervalDict[int, int](
...     operator=lambda x, y: x + y,
...     strict=False
... )
>>> a |= MutableIntervalDict[int, int]({(1, 10): 1})
>>> print(a)
{'[1;10)': 1}
>>> a |= MutableIntervalDict[int, int]({(5, 20): 2})
>>> print(a)
{'[1;5)': 1, '[5;10)': 3, '[10;20)': 2}
>>> a |= MutableIntervalDict[int, int]({(10, 30): 3})
>>> print(a)
{'[1;5)': 1, '[5;10)': 3, '[10;20)': 5, '[20;30)': 3}
update(*args)

Update the dict.

Parameters

*args (Iterable) – An iterable of IntervalDict or valid iterable for an interval dictionary creation.

Raises

TypeError – if an argument is not iterable.

Note

If the parameter strict used in the constructor is False, the complexity is in \(O(n\,log(n)\,k\,\lambda)\) where:

  • \(n\) is the length of *args;

  • \(k\) is the number of output intervals;

  • \(\lambda\) is the the cost of the operator parameter used in the constructor.

Examples

>>> from part import MutableIntervalDict
>>> from operator import add
>>> a = MutableIntervalDict[int, int](
...     operator=add,
...     default=lambda: 0,
... )
>>> a.update({(1, 10): 1})
>>> print(a)
{'[1;10)': 1}
>>> a.update(
...     FrozenIntervalDict[int, int]({(5, 20): 2}),
...     FrozenIntervalDict[int, int]({(10, 30): 3})
... )
>>> print(a)
{'[1;5)': 1, '[5;10)': 3, '[10;20)': 5, '[20;30)': 3}
>>> a = MutableIntervalDict[int, set](
...     operator=lambda x, y: x | y,
...     strict=False
... )
>>> a.update({(1, 10): {1}})
>>> print(a)
{'[1;10)': {1}}
>>> a.update(
...     FrozenIntervalDict[int, set]({(5, 20): {2}}),
...     FrozenIntervalDict[int, set]({(10, 30): {3}})
... )
>>> print(a)
{'[1;5)': {1}, '[5;10)': {1, 2}, '[10;20)': {2, 3}, '[20;30)': {3}}
clear()

Remove all items from self (same as del self[:]).