public function intersect(TrieCollection $collection) { $keys = $collection->getKeys(); $this->entries = array_values(array_filter($this->entries, function ($value) use($keys) { return in_array($value->key, $keys); })); return $this->sortKeys(); }
/** * Fetch all child nodes with a value below a specified node * * @param TrieNode $trieNode Node that is our start point for the retrieval * @param string $searchPrefix The prefix that we're searching for * @param string $prefix Full Key for the requested start point * @return TrieCollection[] Collection of TrieEntry key/value pairs for all child nodes with a value */ protected function getAllChildren(TrieNode $trieNode, $searchPrefix, $prefix) { $collection = new TrieCollection(); if ($trieNode->value !== null) { if (strpos($prefix, $searchPrefix) === 0) { foreach ($trieNode->value as $value) { if ($value instanceof TrieEntry) { $collection->add(clone $value); } else { $collection->add(new TrieEntry($value, $prefix)); } } } } if (isset($trieNode->children)) { foreach ($trieNode->children as $characters => $trie) { $collection->merge($this->getAllChildren($trie, $searchPrefix, $prefix . $characters)); } } return $collection; }