Esempio n. 1
0
 /**
  * {@link DefaultComparator::getInstance} のテストをします.
  * 以下を確認します.
  * 
  * - 返り値が DefaultComparator のインスタンスである
  * - どの返り値も, 同一のインスタンスを返す
  * 
  * @covers Peach\Util\DefaultComparator::getInstance
  */
 public function testGetInstance()
 {
     $c1 = DefaultComparator::getInstance();
     $c2 = DefaultComparator::getInstance();
     $this->assertInstanceOf("Peach\\Util\\DefaultComparator", $c1);
     $this->assertSame($c1, $c2);
 }
Esempio n. 2
0
 /**
  * 指定された配列から, 重複した値を取り除いた結果を返します.
  * 重複かどうかの判定は, 引数に指定されたコンパレータを使って行われます.
  * コンパレータが指定されなかった場合は {@link DefaultComparator} が適用されます.
  * 
  * @param  array      $arr
  * @param  Comparator $c   コンパレータ
  * @return array
  */
 public static function unique(array $arr, Comparator $c = null)
 {
     if (!isset($c)) {
         $c = DefaultComparator::getInstance();
     }
     $sorted = self::asort($arr, $c);
     $delKey = array();
     list($lastKey, $lastValue) = each($sorted);
     while (list($key, $value) = each($sorted)) {
         if ($c->compare($value, $lastValue) === 0 && $value == $lastValue) {
             $delKey[] = $key;
             continue;
         } else {
             $lastKey = $key;
             $lastValue = $value;
         }
     }
     foreach ($delKey as $key) {
         unset($arr[$key]);
     }
     return $arr;
 }