当前位置:网站首页>PHP implementation of interval sorting of classified data

PHP implementation of interval sorting of classified data

2022-06-24 01:26:00 lascyb

Data scenarios :

There are several videos , Each video has its own category , Data item The format is as follows

[
    "id"=>1,
    "cate_id"=>1    
]

The existing videos have been sorted according to the specified rules

Sorting requirements :

In sequence , Successive 10 In video , No videos belonging to the same category

Generate false data :

// Suppose there is 100 A classification ,ID by  1-100  //$cates=[1,...,100];
// Generate 5000 Video data 
$video=[];
for ($i=1;$i<=5000;$i++){
    $video[]=[
        "id"=>$i,
        "cate_id"=>mt_rand(1,100), // Classification ID
        "other"=>$i
    ];
}

Code implementation :

function buildQueue($list=[],$step=10){
    $data=[]; // Receive the generated data 
    $steps=[]; /** receive   because   Before and after 10 Data items that cannot be inserted because the step size range has the same classification ,
         The storage format is :
        $steps = [
            "cate_id"=>[
                "wait"=>0-10 // Wait for step size 
                "queue"=>[] // Store data items 
            ],
            ....
        ]
        */
        
    // Traverse $list Insert $data Array  , If before and after the current item 10 The step size has the same classification element , Then pursue and add $steps Array 
    foreach ($list as $item){ 
        if (isset($steps[$item["cate_id"]]["wait"])&&$steps[$item["cate_id"]]["wait"]>0){
             // Wait for the step length to run out 
            //dump(" Join the wait queue ");
            if (isset($steps[$item["cate_id"]]["queue"])){ // Queue already exists 
                array_unshift($steps[$item["cate_id"]]["queue"],$item); // Chasing data from the head , fifo 
            }else{ // Queue does not exist 
                $steps[$item["cate_id"]]["queue"]=[$item]; // Initialize queue 
            }
        }else{ // No waiting steps 
            $steps[$item["cate_id"]]["wait"]=$step; // The record is inserted in $steps Wait step length for the record in 10
            $data[]=$item; // Add directly into $data Array 
            reduceSteps($steps,$data,$step); // operation $steps Array - Subtract the waiting step for the data waiting 1
        }
    }
    //debug start  Print $data Data saved in 
    //$i=1;
    //foreach ($data as $datum){
    //    dump($i++.": ".$datum["id"].'-'.$datum["cate_id"]);
    //}
    
    //dump($steps); //$steps The array may not be empty , Because the step spacing is insufficient 10
    //debug end
    
    // Step spacing from 10 Reduce to 0, Make sure $steps Empty ,$list Data is not lost 
    for ($step-=1;$step>=0;$step--){
        //dump("step:----------".$step);
        //$num1=count($data)-1;
        reduceSteps($steps,$data,$step);
        //$num2=count($data)-1;
        //for ($i=$num1;$i<=$num2;$i++){ // Print $data Data added this time in 
        //    dump(($i+1).": ".$data[$i]["rate"].'-'.$data[$i]["id"].'-'.$data[$i]["cate_id"]);
        //}
    }
    //dump($steps);  Is it empty 
    return $data;
}
//$steps And $data Use reference types , Reduce memory usage , Reduce code complexity 
function reduceSteps(&$steps,&$data,$step=10){
        // Subtract the waiting step for all waiting data 1
        foreach ($steps as $key=>$datum){ //
            if ($datum["wait"]>0){
                $steps[$key]["wait"]--;
            }
        }
        // Processing waiting data 
        foreach ($steps as $key=>$datum){
            if ($datum["wait"] == 0){ // Data without waiting 
                if (!empty($steps[$key]["queue"])){ // The waiting queue is not empty 
                    $data[]=array_pop($steps[$key]["queue"]); // Bomb stack , The data waiting first pops up first 
                    $steps[$key]["wait"] = $step; // this cate_id The classification continues to wait for the step size 
                    reduceSteps($steps,$data,$step); //// Subtract the waiting step for all waiting data 1
                }else{ // The wait queue is empty 
                    unset($steps[$key]); // Delete cate_id Index related data 
                } 
                // No waiting step is 0 The data of , Is out of 
            }
        }

    }

Code testing :

dump(microtime(true));
$data=buildQueue($video);
dump(microtime(true));
foreach ($data as $datum) {
    dump($datum["id"]."-".$datum["cate_id"]."-".$datum["other"]);
}

test result :( because cate_id Random generation , So the test results may be different )

1637206150.3998
1637206150.4236
"1-77-1"
"2-85-2"
"3-25-3"
"4-94-4"
"5-22-5"
"6-61-6"
"7-66-7"        // -- id:7   -   Classification 66
"8-56-8"        // -- id:8    -     Classification 56
"10-51-10"
"11-27-11"
"12-8-12"
"13-100-13"
"14-65-14"
"16-52-16"
"17-11-17"
"18-91-18"
"9-66-9"       // -- id:9    -     Classification 66   -  Distance same as classification  ID 7  step 10
"15-56-15"     // -- id:15    -     Classification 56  -  Distance same as classification  ID 8  step 10
"19-55-19" 
"20-43-20"
"21-46-21"
"22-50-22"
...
原网站

版权声明
本文为[lascyb]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211118130340083w.html