"""Thread-safe hash set. Ported from net.i2p.util.ConcurrentHashSet. """ from __future__ import annotations import threading from typing import Iterator class ConcurrentHashSet: """Thread-safe set wrapper.""" def __init__(self) -> None: self._lock = threading.Lock() self._data: set = set() def add(self, item) -> None: with self._lock: self._data.add(item) def remove(self, item) -> None: with self._lock: self._data.remove(item) def discard(self, item) -> None: with self._lock: self._data.discard(item) def __contains__(self, item) -> bool: with self._lock: return item in self._data def __len__(self) -> int: with self._lock: return len(self._data) def __iter__(self) -> Iterator: with self._lock: return iter(list(self._data))