mardi 3 février 2015

proper using php-references in my case

I want to realize algorithm of Aho-Corasick. I've made trie and it works, i've done deleting from trie, insert and search. But when i've tried to insert my test data (~56k words), php has thrown an error: php reached memory limit on one script (128 mb). So, i think the problem is in my incorrect using references. Here is part of my code, where i get an error:



public function insert($key, $value) {
$current_node = &$this->root; // setting current node as root node

for ($i = 0; $i < mb_strlen($key, 'UTF-8'); $i++) {
$char = mb_substr($key, $i, 1, 'UTF-8');

$parent = &$current_node; // setting parent node

if (isset($current_node['children'][(string)$char])) {
$current_node = &$current_node['children'][(string)$char];
if (isset($current_node['isLeaf']))
unset($current_node['isLeaf']);
} else {
$current_node['children'][(string)$char] = [];
$current_node = &$current_node['children'][(string)$char];
}

$current_node['parent'] = &$parent;

if ($i == (mb_strlen($key, 'UTF-8') - 1)) {
$current_node['value'] = $value;
if (!isset($current_node['children'])) {
$current_node['isLeaf'] = true;
}
}
}
}


I think the problem is that i try to store all massive by reference, not just address in C/C++ style. So, can i solve this problem in C/C++ style? Has php instrument for it? What if i will write php-extension in C and then just add it to my php-interpretator? Sorry for my bad english.





Aucun commentaire:

Enregistrer un commentaire