Ver Mensaje Individual
  #3 (permalink)  
Antiguo 07/08/2013, 19:51
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años, 1 mes
Puntos: 292
Respuesta: Duda diseño : entre re-uso / eficiencia / evitar divergencias

Hola amigo Triby ... que pena no haber colocado el codigo..... no hay base de datos en el asunto... mira coloco algo de codigo ahora :)

A/no-A seria con / sin GAPs y ...
B/no-B seria con deep_search en TRUE/FALSE

Código PHP:
Ver original
  1. public function findSubNoGaps(TagSequence $sub, $deep_search=null)
  2.     {      
  3.  
  4.         // obtener la primera ocurrencia con Find() y a partir de ahi recorrer CONTINUO $sub comparando uno contra uno cada tag, si falla..... chequear cuanto queda por recorrer de $this y si es > long($sub) entonces volver a buscar la primera ocurrencia con Find() y repetir el proceso
  5.  
  6.         // rebobinar las secuencias aguja y pajar es responsabilidad del programador !!! no se hara aqui!    
  7.        
  8.         if ($this->length()==0 || $sub->length()==0)
  9.             Throw new \LengthException('Aguja o pajar no pueden estar vacios');
  10.        
  11.        
  12.         if ($this->length(true) < $sub->length(true))
  13.             Throw new \LengthException("Aguja no puede ser mayor que pajar");
  14.        
  15.                
  16.             $count = 0;
  17.             $tag = $this->current();
  18.             $tagSub = $sub->current();    
  19.          
  20.  
  21.             // while ext.
  22.             while ($this->valid() && $sub->valid() && ($this->length()- $this->key() >= $sub->length())  )
  23.             {
  24.                 #echo "Entrando WHILE EXTERIOR<p/>";
  25.                $count = 0;
  26.                 $sub->rewind();
  27.  
  28.                 $tagSub = $sub->current();
  29.                        
  30.                 $found = ($this->find($tagSub)>=0);
  31.                 if ($found){
  32.                     $tag = $tagSub;
  33.                     $count = 1;
  34.                 }
  35.                
  36.                 if (is_null($this->key()))
  37.                 {
  38.                     #echo "Cursor de secuencia pajar fuera de rango sin coincidencias!";
  39.                    return false;              
  40.                 }
  41.  
  42.                 #echo 'Sub buscado : '.$sub->key().' ] -- '; echo $sub->current()->getElementName(); echo '<p/> En Pajar: '.$this->key().' ] -- '; echo $tag->getElementName(); var_dump($found); echo '---------------<p/>';
  43.  
  44.                 // while int.
  45.                 while ($found && $this->valid() && $sub->valid())
  46.                 {                                
  47.                  
  48.                     $tagSub = $sub->next();  
  49.                     $tag = $this->next();                          
  50.                
  51.                     $found = ($tag->compareTo($tagSub)>=0);
  52.  
  53.                     if ($found)
  54.                         $count++;          
  55.          
  56.                     // solo para debug:  
  57.                     if ($this->valid() && $sub->valid())
  58.                     {
  59.                         #echo 'Sub buscado : '.$sub->key().' ] -- '; echo  $tagSub->getElementName(); echo '<p/> En Pajar: '.$this->key().' ] -- '; echo $tag->getElementName(); var_dump($found); echo '---------------<p/>';
  60.                    }
  61.  
  62.                
  63.  
  64.                 } // endWhile int.
  65.  
  66.             } // endWhile ext.
  67.  
  68.             #echo "<p/><i>Count = $count </i>";
  69.  
  70.             return ($count == $sub->length());
  71.            
  72.     }


Código PHP:
Ver original
  1. public function findSubWithGaps(TagSequence $sub, $deep_search=null)
  2.     {
  3.             // Asumo puede haber "gaps"
  4.             // por eso busco con find() en vez de avanzar e ir comparando
  5.  
  6.             // Deberia tener en cuenta que tags no-pueden aparecer en los Gaps, longitud minima y maxima permitida, etc.  
  7.            
  8.         if ($this->length()==0 || $sub->length()==0)
  9.             Throw new \LengthException('Aguja o pajar no pueden estar vacios');
  10.        
  11.        
  12.         if ($this->length(true) < $sub->length(true))
  13.             Throw new \LengthException("Aguja no puede ser mayor que pajar");
  14.  
  15.             $tag = $this->current();
  16.             $tagSub = $sub->current();
  17.  
  18.             #echo "<h1>[ {$this->key()} ] $tag</h1><p/>";
  19.            #echo "<h1>[ {$sub->key()} ] $tagSub</h1><p/>";
  20.            
  21.             $count = 0;
  22.        
  23.             $found = ($this->find($tagSub)>=0);
  24.             if ($found)
  25.                 $tag = $tagSub;
  26.  
  27.             echo 'Sub buscado : '.$sub->key().' ] -- '; echo $sub->current()->getElementName(); echo '<p/> En Pajar: '.$this->key().' ] -- '; echo $tag->getElementName(); var_dump($found); echo '---------------<p/>';
  28.  
  29.             while ($found && $this->valid() && $sub->valid())
  30.             {
  31.                 $count++;              
  32.                  
  33.                 $tagSub = $sub->next();                            
  34.                
  35.                 $found = ($this->find($tagSub)>=0);
  36.                 if ($found)
  37.                         $tag = $tagSub;
  38.  
  39.                 $tag = $this->current();
  40.  
  41.  
  42.                 // solo para debug:  
  43.                 if ($this->valid() && $sub->valid())
  44.                 {
  45.                     echo 'Sub buscado : '.$sub->key().' ] -- '; echo $tagSub->getElementName(); echo '<p/> En Pajar: '.$this->key().' ] -- '; echo $tag->getElementName(); var_dump($found); echo '---------------<p/>';
  46.                 }
  47.  
  48.                
  49.  
  50.             } // endWhile
  51.  
  52.             echo "<p/><i>Count = $count </i>";        
  53.            
  54.  
  55.             return ($count == $sub->length());
  56.  
  57.     }


Como veras..... esas dos funciones son bastante diferentes excepto por algunos chequeos de tipo Runtime ... pero en ambas debo ahora agregar la parte de DEEP_SEARCH que seria si quiero que las busquedas se hagan lineales o como un DomNodeList donde hay una "profundidad" involucrada y... eso se podria hacer metiendo algunos IF aqui y alla en ambas dos funciones......

Entonces.... hago de las dos funciones solo una ? hago de esas dos, cuatro ? las coloco como clase aparte especializada en busquedas ? etc ... :P

Mil gracias!
__________________
Salu2!