当前位置:网站首页>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" ...
边栏推荐
- The concept of TP FP TN FN in machine learning
- 13 `bs_ duixiang. Tag tag ` get a tag object
- Local cache selection (guava/caffeine/ohc) and performance comparison
- Selenium crawls stocks in practice
- ctfhub---SSRF
- Echo framework: implementing distributed log tracing
- How to build a "preemptive" remote control system (- - memory chapter)
- ShardingSphere-proxy-5.0.0容量范围分片的实现(五)
- [new secsha!] Have you got the 22 year new seckill activity of Tencent cloud?
- Code practice | ground extraction with logo-loam
猜你喜欢

LMS Virtual. Derivation method of lab acoustic simulation results

13 `bs_ duixiang. Tag tag ` get a tag object

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
Talk to Wu Jiesheng, head of Alibaba cloud storage: my 20 years of data storage (unlimited growth)
Shengdun technology joined dragon lizard community to build a new open source ecosystem

ICML'22 | ProGCL: 重新思考图对比学习中的难样本挖掘

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!

Handwritten digit recognition using SVM, Bayesian classification, binary tree and CNN

JS input / output statements, variables

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!
随机推荐
How to choose a website construction company self-study website or website construction company
Arm learning (7) symbol table and debugging
Attack and defense world PyC trade
Textplus - reverse engineering of textplus
什么是养老理财?养老理财产品有哪些?
Architecture solutions
[flutter] comment utiliser les paquets et plug - ins flutter
What is hypervisor? What is the difference between type 1 and type 2?
How to self-study website construction is website construction company reliable
2021-11-21: map[i][j] = = 0, which means that (I, J) is an ocean. If you cross it, the cost will be
Cvpr2022 𞓜 thin domain adaptation
Force buckle deletes duplicates in the sort array
Gin framework: automatically add requestid
Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)
How to open Tencent enterprise mailbox and Tencent enterprise mailbox login portal
Everything I see is the category of my precise positioning! Open source of a new method for saliency map visualization
Longest substring without duplicate characters
Why traifik ingress?
Installation and use of winscp and putty
Tencent cloud recruitment order sincerely invites ISV partners for customized development!