PHP Classes

File: src/Search/Bitap/convertMaskToIndices.php

Recommend this page to a friend!
  Classes of AccountKiller   Fuse   src/Search/Bitap/convertMaskToIndices.php   Download  
File: src/Search/Bitap/convertMaskToIndices.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:
Date: 1 year ago
Size: 891 bytes
 

Contents

Class file image Download
<?php

namespace Fuse\Search\Bitap;

use function
Fuse\Core\config;

function
convertMaskToIndices(array $matchmask = [], ?int $minMatchCharLength = null): array
{
   
$minMatchCharLength = $minMatchCharLength ?? config('minMatchCharLength');

   
$indices = [];
   
$start = -1;
   
$end = -1;
   
$i = 0;

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

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

    return
$indices;
}