当前位置:网站首页>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" ...
边栏推荐
- 7 tips for preventing DDoS Attacks
- Is the domain name available for trademark registration? How long is the trademark registration cycle?
- [technical grass planting] use webhook to automatically deploy my blogs on multiple sites in Tencent cloud
- Relationship between continuous testing and quality assurance
- Echo framework: implementing timeout Middleware
- Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
- How to use IOT gateway to realize networking communication of smart road lamp posts
- What is memory out of order access?
- How to open Tencent enterprise mailbox and Tencent enterprise mailbox login portal
- How to improve program performance
猜你喜欢
Shengdun technology joined dragon lizard community to build a new open source ecosystem

Error reported using worker: uncaught domexception: failed to construct 'worker': script at***
![[flutter] comment utiliser les paquets et plug - ins flutter](/img/a6/e494dcdb2d3830b6d6c24d0ee05af2.png)
[flutter] comment utiliser les paquets et plug - ins flutter

Use recursion to form a multi-level directory tree structure, with possibly the most detailed notes of the whole network.

对抗训练理论分析:自适应步长快速对抗训练

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

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

跨域和JSONP

GNN upper edge distributor! Instead of trying to refine pills, you might as well give your GNN some tricks
Talk to Wu Jiesheng, head of Alibaba cloud storage: my 20 years of data storage (unlimited growth)
随机推荐
How to realize court face recognition / flow statistics based on easycvr technology?
ctfhub---SSRF
Spatial4j introduction practice
4 most common automated test challenges and Countermeasures
Solve the problem that Base64 compressed files are extracted with spaces after post request
If you want to open an account for stock trading, is it safe to open an account online-
Cross domain and jsonp
WinSCP和PuTTY的安装和使用
Relationship between continuous testing and quality assurance
Selenium crawls stocks in practice
How to build high quality and high brand websites what should be paid attention to in the construction of enterprise websites
Tencent cloud recruitment order sincerely invites ISV partners for customized development!
What is the website construction process? What details need to be valued?
[technical grass planting] deploy a super large capacity and unlimited speed network disk in Tencent cloud
2021-11-19:[0,4,7]:0 means that the stone here has no color. If it turns red
Batch generation of 2D codes from txt files
Video stream playback address redirection optimization after easycvr replaces the new kernel
How to build a pagoda panel web site on Tencent ECS?
LSF opens job idle information to view the CPU time/elapse time usage of the job
Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks