当前位置:网站首页>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" ...
边栏推荐
- 一次 MySQL 误操作导致的事故,「高可用」都顶不住了!
- Radware load balancer common maintenance query commands
- Millions of routers are at risk of attack, and hackers supported by North Korea are invading the United States and Britain | November 19 global network security hotspot
- If the program exits abnormally, how to catch the fatal error through the go language?
- NJS triggers system command operation
- Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
- 跨域和JSONP
- ctfhub---SSRF
- Coordinate system "slang" in GIS world
- 985 Android programmers won the oral offer of Alibaba P6 in 40 days. After the successful interview, they sorted out these interview ideas
猜你喜欢

GNN upper edge distributor! Instead of trying to refine pills, you might as well give your GNN some tricks

Perhaps the greatest romance of programmers is to commemorate their dead mother with a software

Installation and use of winscp and putty

Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)

Zhongshanshan: engineers after being blasted will take off | ONEFLOW u

Cvpr2022 𞓜 thin domain adaptation

js输入输出语句,变量
![[flutter] comment utiliser les paquets et plug - ins flutter](/img/a6/e494dcdb2d3830b6d6c24d0ee05af2.png)
[flutter] comment utiliser les paquets et plug - ins flutter

985 Android programmers won the oral offer of Alibaba P6 in 40 days. After the successful interview, they sorted out these interview ideas

13 `bs_ duixiang. Tag tag ` get a tag object
随机推荐
What is the website construction process? What details need to be valued?
Devops culture: Amazon leadership principles
一次 MySQL 误操作导致的事故,「高可用」都顶不住了!
How to build a practical website and how to operate after the website goes online
Millions of routers are at risk of attack, and hackers supported by North Korea are invading the United States and Britain | November 19 global network security hotspot
One article introduces you to the world of kubernetes
SAP executes PGI on the delivery order of STO and reports an error -fld selectn for Mvmt type 643 acct 400020 differences
How do small businesses do a good job in website construction? Is there any guarantee for network companies to build websites
Sockfwd a data forwarding gadget
How to build a pagoda panel web site on Tencent ECS?
Architecture solutions
An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
Remove the cloud disk service display "continued" logo
Implementation of automatic triggering of inward delivery order after outward delivery order PGI in SAP inter company sto process
Leetcode lecture on algorithm interview for large factories 2 Time space complexity
ICML'22 | ProGCL: 重新思考图对比学习中的难样本挖掘
所见之处都是我精准定位的范畴!显著图可视化新方法开源
Real time preview of RTSP video based on webrtc
2021-11-18: given a length len, it indicates how many bits there are in total. All characters
Interviewer: why does the new generation memory need two survivor zones?