本文整理汇总了PHP中Set::add方法的典型用法代码示例。如果您正苦于以下问题:PHP Set::add方法的具体用法?PHP Set::add怎么用?PHP Set::add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Set
的用法示例。
在下文中一共展示了Set::add方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: whenIteratingOverSet_ThenReturnAllUniqueItems
/**
* @test
*/
public function whenIteratingOverSet_ThenReturnAllUniqueItems()
{
$item1 = 'A';
$item2 = 'B';
$set = new Set();
$set->add($item1);
$set->add($item2);
$values = array();
foreach ($set as $key => $value) {
$values[$key] = $value;
}
$this->assertEquals(array(0 => 'A', 1 => 'B'), $values);
}
示例2: add
/**
* Unsupported method
* @throws Exception every time
*/
public function add($const = '')
{
if ($this->inC) {
return parent::add($const);
}
throw new Exception("You are not allowed to modify this set");
}
示例3: testMember
public function testMember()
{
$set = new Set(array(1, 2, 3));
self::assertFalse($set->member(4));
self::assertTrue($set->member(1));
$set->add(4);
self::assertTrue($set->member(4));
}
示例4: testRemove
public function testRemove()
{
$set1 = new Set();
$set2 = new Set([100, 200]);
$set1->add(200);
$set1->add(300);
$set2->retainAll($set1);
$this->assertEquals($set1->toArray(), [200, 300]);
$this->assertEquals($set2->toArray(), [200]);
$this->assertFalse($set1->equals($set2));
$this->assertFalse($set2->equals($set1));
$set1->remove(300);
$this->assertTrue($set1->equals($set2));
$this->assertTrue($set2->equals($set1));
$set1->removeAll($set2);
$this->assertTrue($set1->isEmpty());
$this->assertFalse($set2->isEmpty());
}
示例5: difference
public function difference(Set $other)
{
$difference_set = new Set();
foreach ($this as $item) {
if (!$other->contains($item)) {
$difference_set->add($item);
}
}
return $difference_set;
}
示例6: testResumesIterationAfterConvertingToArray
/**
* @dataProvider sampleNumbers
*/
public function testResumesIterationAfterConvertingToArray($numbers)
{
$set = new Set(\Cassandra::TYPE_INT);
foreach ($numbers as $number) {
$set->add($number);
}
$this->assertEquals(1, $set->current());
$set->next();
$this->assertEquals(2, $set->current());
$set->next();
$this->assertEquals(3, $set->current());
$set->values();
$set->next();
$this->assertEquals(4, $set->current());
$set->next();
$this->assertEquals(5, $set->current());
$set->next();
$this->assertEquals(6, $set->current());
}
示例7: testPartialUserType
/**
* User type using a partial user type value.
*
* This test will ensure that the PHP driver supports the user types. This
* test uses a partial user type where some values will not be assigned.
*
* @test
* @ticket PHP-57
*/
public function testPartialUserType()
{
// Alias missing from a single number
$phone = $this->getPhoneUserType();
$phone->set("number", "000-000-0000");
$numbers = new Set($phone->type());
$numbers->add($phone);
$address = UserTypeIntegrationTest::getAddressUserType();
$address->set("street", "123 Missing Alias Street");
$address->set("zip", 00);
$address->set("phone_numbers", $numbers);
$key = $this->insertAddress($address);
$this->assertAddressValue($this->selectAddress($key), $address);
// Missing/NULL values during insert
$address = UserTypeIntegrationTest::getAddressUserType();
$address->set("street", "1 Furzeground Way");
$key = $this->insertAddress($address);
$address->set("zip", null);
// Add null 'zip' to assert properly (Server will default null values)
$address->set("phone_numbers", null);
// Add null 'phone_numbers' (same as above)
$this->assertAddressValue($this->selectAddress($key), $address);
}
示例8: testSupportsRetrievingValues
public function testSupportsRetrievingValues()
{
$values = array(new Varint('1'), new Varint('2'), new Varint('3'), new Varint('4'), new Varint('5'), new Varint('6'), new Varint('7'), new Varint('8'));
$set = new Set(\Cassandra::TYPE_VARINT);
for ($i = 0; $i < count($values); $i++) {
$set->add($values[$i]);
}
$this->assertEquals($values, $set->values());
}
示例9: addIf
private function addIf(Set $to, Set $from, callable $f)
{
foreach ($from as $value) {
if ($f($value)) {
$to->add($value);
}
}
}
示例10: powerSet
/**
* Power set P(S)
* The set of all subsets of S, including the empty set and S itself.
*
* Example:
* S = {x, y, z}
* P(S) = {Ø, {x}, {y}, {z}, {x,y}, {x,z}, {y,z}, {x,y,z}}
*
* Algorithm:
* Setup:
* - n: size of the original set
* - 2ⁿ: size of the power set
* - A: original set as an array with numbered indices 0 to n - 1
* - P(S): power set to be created
*
* Iterative loop algorithm:
* - Loop i from 0 to < 2ⁿ
* - Create empty temporary Set
* - Loop j from 0 to < n
* - If the jᵗʰ bit of the i counter is set, add A[j] to temporary Set
* - Add temporary set to power set
*
* Time complexity: O(n2ⁿ)
* Reference: http://www.geeksforgeeks.org/power-set/
*
* @return Set
*/
public function powerSet() : Set
{
// Setup
$n = count($this->A);
// Size of the original set
$2ⁿ = pow(2, $n);
// Size of the power set
$A = array_values($this->A);
// Original set as an array with numbered indices
$P⟮S⟯ = new Set();
// Power set to be created
// Populate power set
for ($i = 0; $i < $2ⁿ; $i++) {
$member_set = new Set();
for ($j = 0; $j < $n; $j++) {
if ($i & 1 << $j) {
$member_set->add($A[$j]);
}
}
$P⟮S⟯->add($member_set);
}
return $P⟮S⟯;
}
示例11: testFirst
public function testFirst()
{
$element = $this->items['alpha'];
$this->object->add($element);
$this->assertEquals($element, $this->object->first());
}
示例12: addAppender
/**
* @param ILogAppender $appender
* @return ILoggerCategory
*/
public function addAppender(ILogAppender $appender)
{
$this->appenders->add($appender);
return $this;
}
示例13: difference
/**
* Returns the set of values that are in this set, excluding the values that
* are also in the other set.
*
* @param Api\Set|Iterator|array $set
*
* @return Api\Set
*/
public function difference($set)
{
$differenceSet = new Set();
foreach ($this as $value) {
if ($set->has($value)) {
$differenceSet->add($value);
}
}
return $differenceSet;
}