Skip to content

Commit

Permalink
Long array syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnc committed Oct 16, 2015
1 parent 6b1d619 commit e01f40f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
10 changes: 5 additions & 5 deletions src/Flexihash.php
Expand Up @@ -31,13 +31,13 @@ class Flexihash
* Internal map of positions (hash outputs) to targets.
* @var array { position => target, ... }
*/
private $positionToTarget = [];
private $positionToTarget = array();

/**
* Internal map of targets to lists of positions that target is hashed to.
* @var array { target => [ position, position, ... ], ... }
*/
private $targetToPositions = [];
private $targetToPositions = array();

/**
* Whether the internal map of positions to targets is already sorted.
Expand Down Expand Up @@ -70,7 +70,7 @@ public function addTarget($target, $weight = 1)
throw new Flexihash_Exception("Target '$target' already exists.");
}

$this->targetToPositions[$target] = [];
$this->targetToPositions[$target] = array();

// hash the target into multiple positions
for ($i = 0; $i < round($this->replicas * $weight); ++$i) {
Expand Down Expand Up @@ -162,7 +162,7 @@ public function lookupList($resource, $requestedCount)

// handle no targets
if (empty($this->positionToTarget)) {
return [];
return array();
}

// optimize single target
Expand All @@ -173,7 +173,7 @@ public function lookupList($resource, $requestedCount)
// hash resource to a position
$resourcePosition = $this->hasher->hash($resource);

$results = [];
$results = array();
$collect = false;

$this->sortPositionTargets();
Expand Down
28 changes: 14 additions & 14 deletions tests/Flexihash/BenchmarkTest.php
Expand Up @@ -19,20 +19,20 @@ public function dump($message)

public function testAddTargetWithNonConsistentHash()
{
$results1 = [];
$results1 = array();
foreach (range(1, $this->lookups) as $i) {
$results1[$i] = $this->basicHash("t$i", 10);
}

$results2 = [];
$results2 = array();
foreach (range(1, $this->lookups) as $i) {
$results2[$i] = $this->basicHash("t$i", 11);
}

$differences = 0;
foreach (range(1, $this->lookups) as $i) {
if ($results1[$i] !== $results2[$i]) {
$differences++;
++$differences;
}
}

Expand All @@ -44,20 +44,20 @@ public function testAddTargetWithNonConsistentHash()

public function testRemoveTargetWithNonConsistentHash()
{
$results1 = [];
$results1 = array();
foreach (range(1, $this->lookups) as $i) {
$results1[$i] = $this->basicHash("t$i", 10);
}

$results2 = [];
$results2 = array();
foreach (range(1, $this->lookups) as $i) {
$results2[$i] = $this->basicHash("t$i", 9);
}

$differences = 0;
foreach (range(1, $this->lookups) as $i) {
if ($results1[$i] !== $results2[$i]) {
$differences++;
++$differences;
}
}

Expand All @@ -76,22 +76,22 @@ public function testHopeAddingTargetDoesNotChangeMuchWithCrc32Hasher()
$hashSpace->addTarget("target$i");
}

$results1 = [];
$results1 = array();
foreach (range(1, $this->lookups) as $i) {
$results1[$i] = $hashSpace->lookup("t$i");
}

$hashSpace->addTarget('target-new');

$results2 = [];
$results2 = array();
foreach (range(1, $this->lookups) as $i) {
$results2[$i] = $hashSpace->lookup("t$i");
}

$differences = 0;
foreach (range(1, $this->lookups) as $i) {
if ($results1[$i] !== $results2[$i]) {
$differences++;
++$differences;
}
}

Expand All @@ -110,22 +110,22 @@ public function testHopeRemovingTargetDoesNotChangeMuchWithCrc32Hasher()
$hashSpace->addTarget("target$i");
}

$results1 = [];
$results1 = array();
foreach (range(1, $this->lookups) as $i) {
$results1[$i] = $hashSpace->lookup("t$i");
}

$hashSpace->removeTarget('target1');

$results2 = [];
$results2 = array();
foreach (range(1, $this->lookups) as $i) {
$results2[$i] = $hashSpace->lookup("t$i");
}

$differences = 0;
foreach (range(1, $this->lookups) as $i) {
if ($results1[$i] !== $results2[$i]) {
$differences++;
++$differences;
}
}

Expand All @@ -145,12 +145,12 @@ public function testHashDistributionWithCrc32Hasher()
$hashSpace->addTarget("target$i");
}

$results = [];
$results = array();
foreach (range(1, $this->lookups) as $i) {
$results[$i] = $hashSpace->lookup("t$i");
}

$distribution = [];
$distribution = array();
foreach ($hashSpace->getAllTargets() as $target) {
$distribution[$target] = count(array_keys($results, $target));
}
Expand Down
30 changes: 15 additions & 15 deletions tests/Flexihash/FlexihashTest.php
Expand Up @@ -9,7 +9,7 @@ class Flexihash_FlexihashTest extends PHPUnit_Framework_TestCase
public function testGetAllTargetsEmpty()
{
$hashSpace = new Flexihash();
$this->assertEquals($hashSpace->getAllTargets(), []);
$this->assertEquals($hashSpace->getAllTargets(), array());
}

public function testAddTargetThrowsExceptionOnDuplicateTarget()
Expand All @@ -29,12 +29,12 @@ public function testAddTargetAndGetAllTargets()
->addTarget('t-c')
;

$this->assertEquals($hashSpace->getAllTargets(), ['t-a', 't-b', 't-c']);
$this->assertEquals($hashSpace->getAllTargets(), array('t-a', 't-b', 't-c'));
}

public function testAddTargetsAndGetAllTargets()
{
$targets = ['t-a', 't-b', 't-c'];
$targets = array('t-a', 't-b', 't-c');

$hashSpace = new Flexihash();
$hashSpace->addTargets($targets);
Expand All @@ -50,7 +50,7 @@ public function testRemoveTarget()
->addTarget('t-c')
->removeTarget('t-b')
;
$this->assertEquals($hashSpace->getAllTargets(), ['t-a', 't-c']);
$this->assertEquals($hashSpace->getAllTargets(), array('t-a', 't-c'));
}

public function testRemoveTargetFailsOnMissingTarget()
Expand All @@ -73,7 +73,7 @@ public function testHashSpaceRepeatableLookups()

public function testHashSpaceLookupsAreValidTargets()
{
$targets = [];
$targets = array();
foreach (range(1, 10) as $i) {
$targets [] = "target$i";
}
Expand All @@ -96,7 +96,7 @@ public function testHashSpaceConsistentLookupsAfterAddingAndRemoving()
$hashSpace->addTarget("target$i");
}

$results1 = [];
$results1 = array();
foreach (range(1, 100) as $i) {
$results1 [] = $hashSpace->lookup("t$i");
}
Expand All @@ -108,7 +108,7 @@ public function testHashSpaceConsistentLookupsAfterAddingAndRemoving()
->removeTarget('new-target')
;

$results2 = [];
$results2 = array();
foreach (range(1, 100) as $i) {
$results2 [] = $hashSpace->lookup("t$i");
}
Expand All @@ -124,7 +124,7 @@ public function testHashSpaceConsistentLookupsWithNewInstance()
foreach (range(1, 10) as $i) {
$hashSpace1->addTarget("target$i");
}
$results1 = [];
$results1 = array();
foreach (range(1, 100) as $i) {
$results1 [] = $hashSpace1->lookup("t$i");
}
Expand All @@ -133,7 +133,7 @@ public function testHashSpaceConsistentLookupsWithNewInstance()
foreach (range(1, 10) as $i) {
$hashSpace2->addTarget("target$i");
}
$results2 = [];
$results2 = array();
foreach (range(1, 100) as $i) {
$results2 [] = $hashSpace2->lookup("t$i");
}
Expand Down Expand Up @@ -203,7 +203,7 @@ public function testGetMultipleTargetsNeedingToLoopToStart()
$mockHasher->setHashValue(35);
$targets = $hashSpace->lookupList('resource', 4);

$this->assertEquals($targets, ['t4', 't5', 't1', 't2']);
$this->assertEquals($targets, array('t4', 't5', 't1', 't2'));
}

public function testGetMultipleTargetsWithoutGettingAnyBeforeLoopToStart()
Expand All @@ -223,7 +223,7 @@ public function testGetMultipleTargetsWithoutGettingAnyBeforeLoopToStart()
$mockHasher->setHashValue(100);
$targets = $hashSpace->lookupList('resource', 2);

$this->assertEquals($targets, ['t1', 't2']);
$this->assertEquals($targets, array('t1', 't2'));
}

public function testGetMultipleTargetsWithoutNeedingToLoopToStart()
Expand All @@ -243,7 +243,7 @@ public function testGetMultipleTargetsWithoutNeedingToLoopToStart()
$mockHasher->setHashValue(15);
$targets = $hashSpace->lookupList('resource', 2);

$this->assertEquals($targets, ['t2', 't3']);
$this->assertEquals($targets, array('t2', 't3'));
}

public function testFallbackPrecedenceWhenServerRemoved()
Expand All @@ -265,23 +265,23 @@ public function testFallbackPrecedenceWhenServerRemoved()
$this->assertEquals($hashSpace->lookup('resource'), 't2');
$this->assertEquals(
$hashSpace->lookupList('resource', 3),
['t2', 't3', 't1']
array('t2', 't3', 't1')
);

$hashSpace->removeTarget('t2');

$this->assertEquals($hashSpace->lookup('resource'), 't3');
$this->assertEquals(
$hashSpace->lookupList('resource', 3),
['t3', 't1']
array('t3', 't1')
);

$hashSpace->removeTarget('t3');

$this->assertEquals($hashSpace->lookup('resource'), 't1');
$this->assertEquals(
$hashSpace->lookupList('resource', 3),
['t1']
array('t1')
);
}
}
Expand Down

0 comments on commit e01f40f

Please sign in to comment.