diff --git a/music_kraken/objects/collection.py b/music_kraken/objects/collection.py index e4905dc..1fba27d 100644 --- a/music_kraken/objects/collection.py +++ b/music_kraken/objects/collection.py @@ -37,39 +37,33 @@ class Collection(Generic[T]): self.contain_given_in_attribute: Dict[str, Collection] = contain_given_in_attribute or {} self.append_object_to_attribute: Dict[str, T] = append_object_to_attribute or {} - self._indexed_values = defaultdict(set) - self._indexed_to_objects = defaultdict(list) + self._id_to_index_values: Dict[int, set] = defaultdict(set) + self._indexed_values = defaultdict(lambda: None) + self._indexed_to_objects = defaultdict(lambda: None) self.extend(data) def _map_element(self, __object: T, from_map: bool = False): - __object._inner._mapped_in_collection.add(self) self._contains_ids.add(__object.id) for name, value in __object.indexing_values: if value is None or value == __object._inner._default_values.get(name): continue - self._indexed_values[name].add(value) - self._indexed_to_objects[value].append(__object) + self._indexed_values[name] = value + self._indexed_to_objects[value] = __object + + self._id_to_index_values[__object.id].add((name, value)) def _unmap_element(self, __object: T): if __object.id in self._contains_ids: self._contains_ids.remove(__object.id) - for name, value in __object.indexing_values: - if value is None: - continue - if value not in self._indexed_values[name]: - continue + for name, value in self._id_to_index_values[__object.id]: + del self._indexed_values[name] + del self._indexed_to_objects[value] - try: - self._indexed_to_objects[value].remove(__object) - except ValueError: - continue - - if not len(self._indexed_to_objects[value]): - self._indexed_values[name].remove(value) + del self._id_to_index_values[__object.id] def _contained_in_self(self, __object: T) -> bool: if __object.id in self._contains_ids: @@ -78,7 +72,7 @@ class Collection(Generic[T]): for name, value in __object.indexing_values: if value is None: continue - if value in self._indexed_values[name]: + if value == self._indexed_values[name]: return True return False @@ -150,8 +144,8 @@ class Collection(Generic[T]): if value is None: continue - if value in self._indexed_values[name]: - existing_object = self._indexed_to_objects[value][0] + if value == self._indexed_values[name]: + existing_object = self._indexed_to_objects[value] if existing_object.id == __object.id: return None @@ -173,8 +167,8 @@ class Collection(Generic[T]): def _find_object_in_self(self, __object: T) -> Optional[T]: for name, value in __object.indexing_values: - if value in self._indexed_values[name]: - return self._indexed_to_objects[value][0] + if value == self._indexed_values[name]: + return self._indexed_to_objects[value] def _find_object(self, __object: T, no_sibling: bool = False) -> Tuple[Collection[T], Optional[T]]: other_object = self._find_object_in_self(__object) @@ -237,6 +231,8 @@ class Collection(Generic[T]): return append_to._unmap_element(existing_object) + append_to._unmap_element(__object) + existing_object.merge(__object) append_to._map_element(existing_object)