File: src/Bitap/matched_indices.php

Recommend this page to a friend!
  Classes of AccountKiller  >  Fuse  >  src/Bitap/matched_indices.php  >  Download  
File: src/Bitap/matched_indices.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Fuse
Fuzzy search of arrays using the Bitap algorithm
Author: By
Last change: Add check for existing indices to avoid PHP notices
Date: 2 years ago
Size: 788 bytes
 

Contents

Class file image Download
<?php namespace Fuse\Bitap;

function
matched_indices($matchmask = [], $minMatchCharLength = 1)
{
   
$matchedIndices = [];
   
$start = -1;
   
$end = -1;
   
$i = 0;

    for (
$len = sizeof($matchmask); $i < $len; $i++) {
       
$match = $matchmask[$i];
        if (
$match && $start === -1) {
           
$start = $i;
        } elseif (!
$match && $start !== -1) {
           
$end = $i - 1;
            if ((
$end - $start) + 1 >= $minMatchCharLength) {
               
$matchedIndices[] = [ $start, $end ];
            }
           
$start = -1;
        }
    }

   
// (i-1 - start) + 1 => i - start
   
if (isset($matchmask[$i - 1]) && $matchmask[$i - 1] && ($i - $start) >= $minMatchCharLength) {
       
$matchedIndices[] = [ $start, $i - 1 ];
    }

    return
$matchedIndices;
}


For more information send a message to info at phpclasses dot org.