LeveledSet#
- class LeveledSet(items=None, to_string=None, from_string=None)#
Bases:
Generic
[T
,I
]A leveled set structure with optional compact internal representation.
Items can be stored internally as strings (or any type
I
) to reduce memory usage or enable hashing on otherwise unhashable public types. You can provide a pair of conversion functions:to_string(item: T) -> I
stores items internally (often asstr
).from_string(stored: I) -> T
reconstructs external/public items.
If either function is omitted (
None
), items are stored directly.- Levels:
The structure maintains a list of levels. New items are added into the current level (the last one). Calling
new_level()
creates a new level only if the last level is non-empty (so you don’t accumulate trailing empties). All seen items are tracked in a global set to avoid duplicates across levels.
Notes
All set operations (union, intersection, difference) act on the global content, not per-level content.
Iteration yields public/external items (
T
), applying conversion back if needed.
- Parameters:
items (Iterable[T] | None) – Optional initial items to insert at level 0.
to_string (Callable[[T], I] | None) – Function to convert an external item
T
to internalI
.from_string (Callable[[I], T] | None) – Function to convert an internal item
I
back toT
.
- __init__(items=None, to_string=None, from_string=None)#
- Parameters:
items (Iterable[T] | None)
to_string (Callable[[T], I] | None)
from_string (Callable[[I], T] | None)
- Return type:
None
Methods
__init__
([items, to_string, from_string])add
(item)Add a single item to the current level if not seen before.
contains
(item)Return True if the item appears in any level.
difference
(other)Return the difference of contents (as external items) with another LeveledSet.
extend
(items)Add multiple items to the current level.
intersection
(other)Return the intersection of contents (as external items) with another LeveledSet.
is_level_empty
(level)Return whether a given level is empty.
isdisjoint
(other)Return True if the two LeveledSets share no common items.
iter_level
(level)Iterate items of a specific level (converted to external items).
Return a tuple with the size of each level.
new_level
([items])Create a new current level (only if last level is non-empty), then add optional items.
Return the total number of unique items across all levels.
Return the number of levels currently stored.
Remove trailing empty levels.
union
(other)Return the union of contents (as external items) with another LeveledSet.
- number_of_levels()#
Return the number of levels currently stored.
- Return type:
int
- level_sizes()#
Return a tuple with the size of each level.
- Return type:
tuple[int, …]
- number_of_items()#
Return the total number of unique items across all levels.
- Return type:
int
- is_level_empty(level)#
Return whether a given level is empty.
- Parameters:
level (int) – Level index (supports negative indexing like Python lists).
- Raises:
IndexError – If level is out of range.
- Return type:
bool
- new_level(items=None)#
Create a new current level (only if last level is non-empty), then add optional items.
- Parameters:
items (Iterable[T] | T | None) – Optional single item or iterable of items to add to the new current level.
- Return type:
None
- remove_empty_levels()#
Remove trailing empty levels.
- Return type:
None
- add(item)#
Add a single item to the current level if not seen before.
- Parameters:
item (T)
- Return type:
None
- extend(items)#
Add multiple items to the current level.
- Parameters:
items (Iterable[T])
- Return type:
None
- contains(item)#
Return True if the item appears in any level.
- Parameters:
item (T)
- Return type:
bool
- union(other)#
Return the union of contents (as external items) with another LeveledSet.
- Parameters:
other (LeveledSet[T, I])
- Return type:
set[T]
- intersection(other)#
Return the intersection of contents (as external items) with another LeveledSet.
- Parameters:
other (LeveledSet[T, I])
- Return type:
set[T]
- difference(other)#
Return the difference of contents (as external items) with another LeveledSet.
- Parameters:
other (LeveledSet[T, I])
- Return type:
set[T]
- isdisjoint(other)#
Return True if the two LeveledSets share no common items.
- Parameters:
other (LeveledSet[T, I])
- Return type:
bool
- iter_level(level)#
Iterate items of a specific level (converted to external items).
- Parameters:
level (int) – Level index (supports negative indexing).
- Yields:
Items at the requested level.
- Raises:
IndexError – If level is out of range.
- Return type:
Iterator[T]