justmd5 5年前

修改理由:

排版

此投稿已在 5年前 合并。

内容修改:

红色背景 为原始内容

绿色背景 为新增或者修改的内容

OldNewDifferences
1 ```<?php
2 //权重随机算法调度
3 class WeightedRoundRobin
4 {
5    private static $_weightArray = array();
 1```php
 2<?php
 3   class WeightedRoundRobin{
64
7    private static $_i = -1;//代表上一次选择的服务器
8    private static $_gcd;//表示集合S中所有服务器权值的最大公约数
9    private static $_cw = 0;//当前调度的权值
10    private static $_max;
11    private static $_n;//agent个数
 5       private static $_weightArray = array();
126
13    public function init()
14    {
 7       private static $_i = -1;//代表上一次选择的服务器
 8       private static $_gcd;//表示集合S中所有服务器权值的最大公约数
 9       private static $_cw = 0;//当前调度的权值
 10       private static $_max;
 11       private static $_n;//agent个数
1512
16    }
 13       public function init()
 14       {
1715
18    public function initParam(array $weightArray)
19    {
20        self::$_weightArray = $weightArray;
21        self::$_gcd = self::getGcd(self::$_weightArray);
22        self::$_max = self::getMaxWeight(self::$_weightArray);
23        self::$_n = count($weightArray);
24    }
 16       }
2517
26    private static function getGcd(array $weightArray)
27    {
28        $temp = array_shift($weightArray);
29        $min = $temp['weight'];
30        $status = false;
31        foreach ($weightArray as $val) {
32            $min = min($val['weight'],$min);
33        }
 18       public function initParam(array $weightArray)
 19       {
 20           self::$_weightArray = $weightArray;
 21           self::$_gcd = self::getGcd(self::$_weightArray);
 22           self::$_max = self::getMaxWeight(self::$_weightArray);
 23           self::$_n = count($weightArray);
 24       }
3425
35        if($min == 1){
36            return 1;
37        }else{
 26       private static function getGcd(array $weightArray)
 27       {
 28           $temp = array_shift($weightArray);
 29           $min = $temp['weight'];
 30           $status = false;
 31           foreach ($weightArray as $val) {
 32               $min = min($val['weight'],$min);
 33           }
3834
39            for ($i = $min; $i>1; $i--) {
 35           if($min == 1){
 36               return 1;
 37           }else{
4038
41                foreach ($weightArray as $val) {
42                    if (is_int($val['weight']/$i)) {
43                        $status = true;
44                    }else{
45                        $status = false;
46                        break;
47                    }
48                }
49                if ($status) {
50                    return $i;
51                }else {
52                    return 1;
53                }
 39               for ($i = $min; $i>1; $i--) {
5440
55            }
56        }
 41                   foreach ($weightArray as $val) {
 42                       if (is_int($val['weight']/$i)) {
 43                           $status = true;
 44                       }else{
 45                           $status = false;
 46                           break;
 47                       }
 48                   }
 49                   if ($status) {
 50                       return $i;
 51                   }else {
 52                       return 1;
 53                   }
5754
58    }
 55               }
 56           }
5957
60    private static function getMaxWeight(array $weightArray)
61    {
62        if(empty($weightArray)){
63            return false;
64        }
65        $temp = array_shift($weightArray);
66        $max = $temp['weight'];
67        foreach ($weightArray as $val) {
68            $max = max($val['weight'],$max);
69        }
70        return $max;
71    }
 58       }
7259
73    public function getWeight()
74    {
75        while (true){
 60       private static function getMaxWeight(array $weightArray)
 61       {
 62           if(empty($weightArray)){
 63               return false;
 64           }
 65           $temp = array_shift($weightArray);
 66           $max = $temp['weight'];
 67           foreach ($weightArray as $val) {
 68               $max = max($val['weight'],$max);
 69           }
 70           return $max;
 71       }
7672
77            self::$_i = ((int)self::$_i+1) % (int)self::$_n;
 73       public function getWeight()
 74       {
 75           while (true){
7876
79            if (self::$_i == 0) {
 77               self::$_i = ((int)self::$_i+1) % (int)self::$_n;
8078
81                self::$_cw = (int)self::$_cw - (int)self::$_gcd;
82                if (self::$_cw <= 0) {
83                    self::$_cw = (int)self::$_max;
 79               if (self::$_i == 0) {
8480
85                    if (self::$_cw == 0) {
86                        return null;
87                    }
88                }
89            }
 81                   self::$_cw = (int)self::$_cw - (int)self::$_gcd;
 82                   if (self::$_cw <= 0) {
 83                       self::$_cw = (int)self::$_max;
9084
91            if ((int)(self::$_weightArray[self::$_i]['weight']) >= self::$_cw) {
92                return self::$_weightArray[self::$_i]['id'];
93            }
94        }
95    }
96 }
 85                       if (self::$_cw == 0) {
 86                           return null;
 87                       }
 88                   }
 89               }
 90
 91               if ((int)(self::$_weightArray[self::$_i]['weight']) >= self::$_cw) {
 92                   return self::$_weightArray[self::$_i]['id'];
 93               }
 94           }
 95       }
 96   }
 97```