MediaWiki  master
ArrayUtils.php
Go to the documentation of this file.
1 <?php
28 class ArrayUtils {
49  public static function consistentHashSort( &$array, $key, $separator = "\000" ) {
50  $hashes = [];
51  foreach ( $array as $elt ) {
52  $hashes[$elt] = md5( $elt . $separator . $key );
53  }
54  uasort( $array, function ( $a, $b ) use ( $hashes ) {
55  return strcmp( $hashes[$a], $hashes[$b] );
56  } );
57  }
58 
66  public static function pickRandom( $weights ) {
67  if ( !is_array( $weights ) || count( $weights ) == 0 ) {
68  return false;
69  }
70 
71  $sum = array_sum( $weights );
72  if ( $sum == 0 ) {
73  # No loads on any of them
74  # In previous versions, this triggered an unweighted random selection,
75  # but this feature has been removed as of April 2006 to allow for strict
76  # separation of query groups.
77  return false;
78  }
79  $max = mt_getrandmax();
80  $rand = mt_rand( 0, $max ) / $max * $sum;
81 
82  $sum = 0;
83  foreach ( $weights as $i => $w ) {
84  $sum += $w;
85  # Do not return keys if they have 0 weight.
86  # Note that the "all 0 weight" case is handed above
87  if ( $w > 0 && $sum >= $rand ) {
88  break;
89  }
90  }
91 
92  return $i;
93  }
94 
112  public static function findLowerBound( $valueCallback, $valueCount,
113  $comparisonCallback, $target
114  ) {
115  if ( $valueCount === 0 ) {
116  return false;
117  }
118 
119  $min = 0;
120  $max = $valueCount;
121  do {
122  $mid = $min + ( ( $max - $min ) >> 1 );
123  $item = $valueCallback( $mid );
124  $comparison = $comparisonCallback( $target, $item );
125  if ( $comparison > 0 ) {
126  $min = $mid;
127  } elseif ( $comparison == 0 ) {
128  $min = $mid;
129  break;
130  } else {
131  $max = $mid;
132  }
133  } while ( $min < $max - 1 );
134 
135  if ( $min == 0 ) {
136  $item = $valueCallback( $min );
137  $comparison = $comparisonCallback( $target, $item );
138  if ( $comparison < 0 ) {
139  // Before the first item
140  return false;
141  }
142  }
143  return $min;
144  }
145 
158  public static function arrayDiffAssocRecursive( $array1, ...$arrays ) {
159  $ret = [];
160 
161  foreach ( $array1 as $key => $value ) {
162  if ( is_array( $value ) ) {
163  $args = [ $value ];
164  foreach ( $arrays as $array ) {
165  if ( isset( $array[$key] ) ) {
166  $args[] = $array[$key];
167  }
168  }
169  $valueret = self::arrayDiffAssocRecursive( ...$args );
170  if ( count( $valueret ) ) {
171  $ret[$key] = $valueret;
172  }
173  } else {
174  foreach ( $arrays as $array ) {
175  if ( isset( $array[$key] ) && $array[$key] === $value ) {
176  continue 2;
177  }
178  }
179  $ret[$key] = $value;
180  }
181  }
182 
183  return $ret;
184  }
185 }
static arrayDiffAssocRecursive( $array1,... $arrays)
Do array_diff_assoc() on multi-dimensional arrays.
Definition: ArrayUtils.php:158
static findLowerBound( $valueCallback, $valueCount, $comparisonCallback, $target)
Do a binary search, and return the index of the largest item that sorts less than or equal to the tar...
Definition: ArrayUtils.php:112
if( $line===false) $args
Definition: cdb.php:64
static consistentHashSort(&$array, $key, $separator="\0")
Sort the given array in a pseudo-random order which depends only on the given key and each element va...
Definition: ArrayUtils.php:49
static pickRandom( $weights)
Given an array of non-normalised probabilities, this function will select an element and return the a...
Definition: ArrayUtils.php:66