Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » PHP
  • » [PATCH] SplObjectStorage::removeCommon and removeUncommon [RSS Feed]

#1 Dec. 19, 2010 05:35:43

Matthew T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PATCH] SplObjectStorage::removeCommon and removeUncommon


Attached are source/test and manual patches for the addition of two
methods to the SplObjectStorage class. These methods, removeCommon and
removeUncommon, add support for the difference and intersection set
operations, respectively.

php-src-5.3-patch.diff is against php/php-src/branches/PHP_5_3
phpdoc-en-trunk-patch.diff is against phpdoc/en/trunk

Any feedback and/or approval/merging would be appreciated.

Regards,

Matthew TurlandIndex: ext/spl/tests/SplObjectStorage_removeUncommon_basic.phpt
===================================================================
--- ext/spl/tests/SplObjectStorage_removeUncommon_basic.phpt (revision 0)
+++ ext/spl/tests/SplObjectStorage_removeUncommon_basic.phpt (revision 0)
@@ -0,0 +1,27 @@
+--TEST--
+Check that SplObjectStorage::removeUncommon functions when receiving proper input
+--CREDITS--
+Matthew Turland (m***@*atthewturland.com)
+--FILE--
+<?php
+
+ $a = (object) 'a';
+ $b = (object) 'b';
+ $c = (object) 'c';
+
+ $foo = new SplObjectStorage;
+ $foo->attach($a);
+ $foo->attach($b);
+
+ $bar = new SplObjectStorage;
+ $bar->attach($b);
+ $bar->attach($c);
+
+ $foo->removeUncommon($bar);
+ var_dump($foo->contains($a));
+ var_dump($foo->contains($b));
+
+?>
+--EXPECT--
+bool(false)
+bool(true)
Index: ext/spl/tests/SplObjectStorage_removeCommon_basic.phpt
===================================================================
--- ext/spl/tests/SplObjectStorage_removeCommon_basic.phpt (revision 0)
+++ ext/spl/tests/SplObjectStorage_removeCommon_basic.phpt (revision 0)
@@ -0,0 +1,28 @@
+--TEST--
+Check that SplObjectStorage::removeCommon functions when receiving proper input
+--CREDITS--
+Matthew Turland (m***@*atthewturland.com)
+--FILE--
+<?php
+
+ $a = (object) 'a';
+ $b = (object) 'b';
+ $c = (object) 'c';
+
+ $foo = new SplObjectStorage;
+ $foo->attach($a);
+ $foo->attach($b);
+
+ $bar = new SplObjectStorage;
+ $bar->attach($b);
+ $bar->attach($c);
+
+ $foo->removeCommon($bar);
+ var_dump($foo->contains($a));
+ var_dump($foo->contains($b));
+
+?>
+--EXPECT--
+bool(true)
+bool(false)
+
Index: ext/spl/tests/SplObjectStorage_removeCommon_invalid_parameter.phpt
===================================================================
--- ext/spl/tests/SplObjectStorage_removeCommon_invalid_parameter.phpt (revision 0)
+++ ext/spl/tests/SplObjectStorage_removeCommon_invalid_parameter.phpt (revision 0)
@@ -0,0 +1,44 @@
+--TEST--
+Check that SplObjectStorage::removeCommon generate a warning and returns NULL when passed non-object param
+--CREDITS--
+Matthew Turland (m***@*atthewturland.com)
+Based on work done at PHPNW Testfest 2009 by Simon Westcott (swestc***@*mail.com)
+--FILE--
+<?php
+
+$data_provider = array(
+ array(),
+ true,
+ "string",
+ 12345,
+ 1.2345,
+ NULL
+);
+
+foreach($data_provider as $input) {
+
+ $s = new SplObjectStorage();
+
+ var_dump($s->removeCommon($input));
+}
+
+?>
+--EXPECTF--
+Warning: SplObjectStorage::removeCommon() expects parameter 1 to be SplObjectStorage, array given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeCommon() expects parameter 1 to be SplObjectStorage, boolean given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeCommon() expects parameter 1 to be SplObjectStorage, %unicode_string_optional% given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeCommon() expects parameter 1 to be SplObjectStorage, integer given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeCommon() expects parameter 1 to be SplObjectStorage, double given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeCommon() expects parameter 1 to be SplObjectStorage, null given in %s on line %d
+NULL
+
Index: ext/spl/tests/SplObjectStorage_removeUncommon_invalid_parameter.phpt
===================================================================
--- ext/spl/tests/SplObjectStorage_removeUncommon_invalid_parameter.phpt (revision 0)
+++ ext/spl/tests/SplObjectStorage_removeUncommon_invalid_parameter.phpt (revision 0)
@@ -0,0 +1,44 @@
+--TEST--
+Check that SplObjectStorage::removeUncommon generate a warning and returns NULL when passed non-object param
+--CREDITS--
+Matthew Turland (m***@*atthewturland.com)
+Based on work done at PHPNW Testfest 2009 by Simon Westcott (swestc***@*mail.com)
+--FILE--
+<?php
+
+$data_provider = array(
+ array(),
+ true,
+ "string",
+ 12345,
+ 1.2345,
+ NULL
+);
+
+foreach($data_provider as $input) {
+
+ $s = new SplObjectStorage();
+
+ var_dump($s->removeUncommon($input));
+}
+
+?>
+--EXPECTF--
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, array given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, boolean given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, %unicode_string_optional% given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, integer given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, double given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, null given in %s on line %d
+NULL
+
Index: ext/spl/spl_observer.c
===================================================================
--- ext/spl/spl_observer.c (revision 306446)
+++ ext/spl/spl_observer.c (working copy)
@@ -485,6 +485,64 @@
RETURN_LONG(zend_hash_num_elements(&intern->storage));
} /* }}} */

+/* {{{ proto bool SplObjectStorage::removeUncommon(SplObjectStorage $os)
+ Remove elements not common to both this SplObjectStorage instance and $os */
+SPL_METHOD(SplObjectStorage, removeUncommon)
+{
+ zval *obj;
+ spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC);
+ spl_SplObjectStorage *other;
+ spl_SplObjectStorageElement *element;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
+ return;
+ }
+
+ other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC);
+
+ zend_hash_internal_pointer_reset(&intern->storage);
+ while (zend_hash_get_current_data(&intern->storage, (void **)&element) == SUCCESS) {
+ if (!spl_object_storage_contains(other, element->obj TSRMLS_CC)) {
+ spl_object_storage_detach(intern, element->obj TSRMLS_CC);
+ }
+ zend_hash_move_forward(&intern->storage);
+ }
+
+ zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
+ intern->index = 0;
+
+ RETURN_LONG(zend_hash_num_elements(&intern->storage));
+}
+
+/* {{{ proto bool SplObjectStorage::removeCommon(SplObjectStorage $os)
+ Remove elements common to both this SplObjectStorage instance and $os */
+SPL_METHOD(SplObjectStorage, removeCommon)
+{
+ zval *obj;
+ spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC);
+ spl_SplObjectStorage *other;
+ spl_SplObjectStorageElement *element;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
+ return;
+ }
+
+ other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC);
+
+ zend_hash_internal_pointer_reset(&other->storage);
+ while (zend_hash_get_current_data(&other->storage, (void **)&element) == SUCCESS) {
+ if (spl_object_storage_contains(intern, element->obj TSRMLS_CC)) {
+ spl_object_storage_detach(intern, element->obj TSRMLS_CC);
+ }
+ zend_hash_move_forward(&other->storage);
+ }
+
+ zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
+ intern->index = 0;
+
+ RETURN_LONG(zend_hash_num_elements(&intern->storage));
+}
+
/* {{{ proto bool SplObjectStorage::contains($obj)
Determine whethe an object is contained in the storage */
SPL_METHOD(SplObjectStorage, contains)
@@ -827,6 +885,8 @@
SPL_ME(SplObjectStorage, contains, arginfo_Object, 0)
SPL_ME(SplObjectStorage, addAll, arginfo_Object, 0)
SPL_ME(SplObjectStorage, removeAll, arginfo_Object, 0)
+ SPL_ME(SplObjectStorage, removeCommon, arginfo_Object, 0)
+ SPL_ME(SplObjectStorage, removeUncommon, arginfo_Object, 0)
SPL_ME(SplObjectStorage, getInfo, arginfo_splobject_void,0)
SPL_ME(SplObjectStorage, setInfo, arginfo_setInfo, 0)
/* Countable */Index: reference/spl/splobjectstorage/removecommon.xml
===================================================================
--- reference/spl/splobjectstorage/removecommon.xml (revision 0)
+++ reference/spl/splobjectstorage/removecommon.xml (revision 0)
@@ -0,0 +1,111 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- $Revision: 288721 $ -->
+
+<refentry xml:id="splobjectstorage.removecommon" xmlns="http://docbook.org/ns/docbook"; xmlns:xlink="http://www.w3.org/1999/xlink";>
+ <refnamediv>
+ <refname>SplObjectStorage::removeCommon</refname>
+ <refpurpose>Removes objects common to both the current storage and another storage from the current storage</refpurpose>
+ </refnamediv>
+
+ <refsect1 role="description">
+ &reftitle.description;
+ <methodsynopsis>
+ <modifier>public</modifier> <type>void</type><methodname>SplObjectStorage::removeCommon</methodname>
+ <methodparam><type>SplObjectStorage</type><parameter>storage</parameter></methodparam>
+ </methodsynopsis>
+ <para>
+ Removes objects common to both the current storage and another storage from the current storage.
+ </para>
+ </refsect1>
+
+ <refsect1 role="parameters">
+ &reftitle.parameters;
+ <para>
+ <variablelist>
+ <varlistentry>
+ <term><parameter>storage</parameter></term>
+ <listitem>
+ <para>
+ The storage containing the common elements to remove.
+ </para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </para>
+ </refsect1>
+
+ <refsect1 role="returnvalues">
+ &reftitle.returnvalues;
+ <para>
+ &return.void;
+ </para>
+ </refsect1>
+
+ <refsect1 role="examples">
+ &reftitle.examples;
+ <para>
+ <example>
+ <title><function>SplObjectStorage::removeCommon</function> example</title>
+ <programlisting role="php">
+<![CDATA]>
+ </programlisting>
+ &example.outputs.similar;
+ <screen>
+<![CDATA]>
+ </screen>
+ </example>
+ </para>
+ </refsect1>
+
+ <refsect1 role="seealso">
+ &reftitle.seealso;
+ <para>
+ <simplelist>
+ <member><methodname>SplObjectStorage::removeUncommon</methodname></member>
+ </simplelist>
+ </para>
+ </refsect1>
+
+</refentry>
+
+<!-- Keep this comment at the end of the file
+Local variables:
+mode: sgml
+sgml-omittag:t
+sgml-shorttag:t
+sgml-minimize-attributes:nil
+sgml-always-quote-attributes:t
+sgml-indent-step:1
+sgml-indent-data:t
+indent-tabs-mode:nil
+sgml-parent-document:nil
+sgml-default-dtd-file:"~/.phpdoc/manual.ced"
+sgml-exposed-tags:nil
+sgml-local-catalogs:nil
+sgml-local-ecat-files:nil
+End:
+vim600: syn=xml fen fdm=syntax fdl=2 si
+vim: et tw=78 syn=sgml
+vi: ts=1 sw=1
+-->
Index: reference/spl/splobjectstorage/removeuncommon.xml
===================================================================
--- reference/spl/splobjectstorage/removeuncommon.xml (revision 0)
+++ reference/spl/splobjectstorage/removeuncommon.xml (revision 0)
@@ -0,0 +1,111 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- $Revision: 288721 $ -->
+
+<refentry xml:id="splobjectstorage.removeuncommon" xmlns="http://docbook.org/ns/docbook"; xmlns:xlink="http://www.w3.org/1999/xlink";>
+ <refnamediv>
+ <refname>SplObjectStorage::removeUncommon</refname>
+ <refpurpose>Removes objects not common to both the current storage and another storage from the current storage</refpurpose>
+ </refnamediv>
+
+ <refsect1 role="description">
+ &reftitle.description;
+ <methodsynopsis>
+ <modifier>public</modifier> <type>void</type><methodname>SplObjectStorage::removeUncommon</methodname>
+ <methodparam><type>SplObjectStorage</type><parameter>storage</parameter></methodparam>
+ </methodsynopsis>
+ <para>
+ Removes objects not common to both the current storage and another storage from the current storage.
+ </para>
+ </refsect1>
+
+ <refsect1 role="parameters">
+ &reftitle.parameters;
+ <para>
+ <variablelist>
+ <varlistentry>
+ <term><parameter>storage</parameter></term>
+ <listitem>
+ <para>
+ The storage containing the uncommon elements to remove.
+ </para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </para>
+ </refsect1>
+
+ <refsect1 role="returnvalues">
+ &reftitle.returnvalues;
+ <para>
+ &return.void;
+ </para>
+ </refsect1>
+
+ <refsect1 role="examples">
+ &reftitle.examples;
+ <para>
+ <example>
+ <title><function>SplObjectStorage::removeUncommon</function> example</title>
+ <programlisting role="php">
+<![CDATA]>
+ </programlisting>
+ &example.outputs.similar;
+ <screen>
+<![CDATA]>
+ </screen>
+ </example>
+ </para>
+ </refsect1>
+
+ <refsect1 role="seealso">
+ &reftitle.seealso;
+ <para>
+ <simplelist>
+ <member><methodname>SplObjectStorage::removeCommon</methodname></member>
+ </simplelist>
+ </para>
+ </refsect1>
+
+</refentry>
+
+<!-- Keep this comment at the end of the file
+Local variables:
+mode: sgml
+sgml-omittag:t
+sgml-shorttag:t
+sgml-minimize-attributes:nil
+sgml-always-quote-attributes:t
+sgml-indent-step:1
+sgml-indent-data:t
+indent-tabs-mode:nil
+sgml-parent-document:nil
+sgml-default-dtd-file:"~/.phpdoc/manual.ced"
+sgml-exposed-tags:nil
+sgml-local-catalogs:nil
+sgml-local-ecat-files:nil
+End:
+vim600: syn=xml fen fdm=syntax fdl=2 si
+vim: et tw=78 syn=sgml
+vi: ts=1 sw=1
+-->--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#2 Dec. 20, 2010 19:42:43

Matthew T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PATCH] SplObjectStorage::removeCommon and removeUncommon


Thanks to comments from Gustavo Lopes, I've removed the removeCommon
method from my patch. I honestly wish I could say why I didn't realize
his point before I submitted the patch in the first place, but I
appreciate the feedback. I've attached the amended patch files, which
include only the removeUncommon method, which I definitely know does
not already exist in the class.

php-src-5.3-patch.diff is against php/php-src/branches/PHP_5_3
phpdoc-en-trunk-patch.diff is against phpdoc/en/trunk

As for the comments regarding the naming conventions, I do agree to a
certain extent. However, I would like to remain consistent with the
format of names of existing methods. I suggest a separate patch be
submitted with method aliases to deal with that situation. If anyone
has a better name for the removeUncommon method, I'm open to
suggestions.

Any further feedback and/or approval/merging would be appreciated.

Regards,

Matthew TurlandIndex: reference/spl/splobjectstorage/removeuncommon.xml
===================================================================
--- reference/spl/splobjectstorage/removeuncommon.xml (revision 0)
+++ reference/spl/splobjectstorage/removeuncommon.xml (revision 0)
@@ -0,0 +1,111 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- $Revision: 288721 $ -->
+
+<refentry xml:id="splobjectstorage.removeuncommon" xmlns="http://docbook.org/ns/docbook"; xmlns:xlink="http://www.w3.org/1999/xlink";>
+ <refnamediv>
+ <refname>SplObjectStorage::removeUncommon</refname>
+ <refpurpose>Removes objects not common to both the current storage and another storage from the current storage</refpurpose>
+ </refnamediv>
+
+ <refsect1 role="description">
+ &reftitle.description;
+ <methodsynopsis>
+ <modifier>public</modifier> <type>void</type><methodname>SplObjectStorage::removeUncommon</methodname>
+ <methodparam><type>SplObjectStorage</type><parameter>storage</parameter></methodparam>
+ </methodsynopsis>
+ <para>
+ Removes objects not common to both the current storage and another storage from the current storage.
+ </para>
+ </refsect1>
+
+ <refsect1 role="parameters">
+ &reftitle.parameters;
+ <para>
+ <variablelist>
+ <varlistentry>
+ <term><parameter>storage</parameter></term>
+ <listitem>
+ <para>
+ The storage containing the uncommon elements to remove.
+ </para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </para>
+ </refsect1>
+
+ <refsect1 role="returnvalues">
+ &reftitle.returnvalues;
+ <para>
+ &return.void;
+ </para>
+ </refsect1>
+
+ <refsect1 role="examples">
+ &reftitle.examples;
+ <para>
+ <example>
+ <title><function>SplObjectStorage::removeUncommon</function> example</title>
+ <programlisting role="php">
+<![CDATA]>
+ </programlisting>
+ &example.outputs.similar;
+ <screen>
+<![CDATA]>
+ </screen>
+ </example>
+ </para>
+ </refsect1>
+
+ <refsect1 role="seealso">
+ &reftitle.seealso;
+ <para>
+ <simplelist>
+ <member><methodname>SplObjectStorage::removeCommon</methodname></member>
+ </simplelist>
+ </para>
+ </refsect1>
+
+</refentry>
+
+<!-- Keep this comment at the end of the file
+Local variables:
+mode: sgml
+sgml-omittag:t
+sgml-shorttag:t
+sgml-minimize-attributes:nil
+sgml-always-quote-attributes:t
+sgml-indent-step:1
+sgml-indent-data:t
+indent-tabs-mode:nil
+sgml-parent-document:nil
+sgml-default-dtd-file:"~/.phpdoc/manual.ced"
+sgml-exposed-tags:nil
+sgml-local-catalogs:nil
+sgml-local-ecat-files:nil
+End:
+vim600: syn=xml fen fdm=syntax fdl=2 si
+vim: et tw=78 syn=sgml
+vi: ts=1 sw=1
+-->Index: ext/spl/tests/SplObjectStorage_removeUncommon_basic.phpt
===================================================================
--- ext/spl/tests/SplObjectStorage_removeUncommon_basic.phpt (revision 0)
+++ ext/spl/tests/SplObjectStorage_removeUncommon_basic.phpt (revision 0)
@@ -0,0 +1,27 @@
+--TEST--
+Check that SplObjectStorage::removeUncommon functions when receiving proper input
+--CREDITS--
+Matthew Turland (m***@*atthewturland.com)
+--FILE--
+<?php
+
+ $a = (object) 'a';
+ $b = (object) 'b';
+ $c = (object) 'c';
+
+ $foo = new SplObjectStorage;
+ $foo->attach($a);
+ $foo->attach($b);
+
+ $bar = new SplObjectStorage;
+ $bar->attach($b);
+ $bar->attach($c);
+
+ $foo->removeUncommon($bar);
+ var_dump($foo->contains($a));
+ var_dump($foo->contains($b));
+
+?>
+--EXPECT--
+bool(false)
+bool(true)
Index: ext/spl/tests/SplObjectStorage_removeUncommon_invalid_parameter.phpt
===================================================================
--- ext/spl/tests/SplObjectStorage_removeUncommon_invalid_parameter.phpt (revision 0)
+++ ext/spl/tests/SplObjectStorage_removeUncommon_invalid_parameter.phpt (revision 0)
@@ -0,0 +1,44 @@
+--TEST--
+Check that SplObjectStorage::removeUncommon generate a warning and returns NULL when passed non-object param
+--CREDITS--
+Matthew Turland (m***@*atthewturland.com)
+Based on work done at PHPNW Testfest 2009 by Simon Westcott (swestc***@*mail.com)
+--FILE--
+<?php
+
+$data_provider = array(
+ array(),
+ true,
+ "string",
+ 12345,
+ 1.2345,
+ NULL
+);
+
+foreach($data_provider as $input) {
+
+ $s = new SplObjectStorage();
+
+ var_dump($s->removeUncommon($input));
+}
+
+?>
+--EXPECTF--
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, array given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, boolean given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, %unicode_string_optional% given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, integer given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, double given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, null given in %s on line %d
+NULL
+
Index: ext/spl/spl_observer.c
===================================================================
--- ext/spl/spl_observer.c (revision 306446)
+++ ext/spl/spl_observer.c (working copy)
@@ -485,6 +485,35 @@
RETURN_LONG(zend_hash_num_elements(&intern->storage));
} /* }}} */

+/* {{{ proto bool SplObjectStorage::removeUncommon(SplObjectStorage $os)
+ Remove elements not common to both this SplObjectStorage instance and $os */
+SPL_METHOD(SplObjectStorage, removeUncommon)
+{
+ zval *obj;
+ spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC);
+ spl_SplObjectStorage *other;
+ spl_SplObjectStorageElement *element;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
+ return;
+ }
+
+ other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC);
+
+ zend_hash_internal_pointer_reset(&intern->storage);
+ while (zend_hash_get_current_data(&intern->storage, (void **)&element) == SUCCESS) {
+ if (!spl_object_storage_contains(other, element->obj TSRMLS_CC)) {
+ spl_object_storage_detach(intern, element->obj TSRMLS_CC);
+ }
+ zend_hash_move_forward(&intern->storage);
+ }
+
+ zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
+ intern->index = 0;
+
+ RETURN_LONG(zend_hash_num_elements(&intern->storage));
+}
+
/* {{{ proto bool SplObjectStorage::contains($obj)
Determine whethe an object is contained in the storage */
SPL_METHOD(SplObjectStorage, contains)
@@ -827,6 +856,8 @@
SPL_ME(SplObjectStorage, contains, arginfo_Object, 0)
SPL_ME(SplObjectStorage, addAll, arginfo_Object, 0)
SPL_ME(SplObjectStorage, removeAll, arginfo_Object, 0)
+ SPL_ME(SplObjectStorage, removeCommon, arginfo_Object, 0)
+ SPL_ME(SplObjectStorage, removeUncommon, arginfo_Object, 0)
SPL_ME(SplObjectStorage, getInfo, arginfo_splobject_void,0)
SPL_ME(SplObjectStorage, setInfo, arginfo_setInfo, 0)
/* Countable */--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#3 Dec. 20, 2010 19:51:19

Matthew T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PATCH] SplObjectStorage::removeCommon and removeUncommon


Sorry to flood the list, but I noticed that I left a stray reference
to removeCommon in my amended patch. Attached the fixed version. My
profound apologies.

Regards,

Matthew TurlandIndex: ext/spl/tests/SplObjectStorage_removeUncommon_basic.phpt
===================================================================
--- ext/spl/tests/SplObjectStorage_removeUncommon_basic.phpt (revision 0)
+++ ext/spl/tests/SplObjectStorage_removeUncommon_basic.phpt (revision 0)
@@ -0,0 +1,27 @@
+--TEST--
+Check that SplObjectStorage::removeUncommon functions when receiving proper input
+--CREDITS--
+Matthew Turland (m***@*atthewturland.com)
+--FILE--
+<?php
+
+ $a = (object) 'a';
+ $b = (object) 'b';
+ $c = (object) 'c';
+
+ $foo = new SplObjectStorage;
+ $foo->attach($a);
+ $foo->attach($b);
+
+ $bar = new SplObjectStorage;
+ $bar->attach($b);
+ $bar->attach($c);
+
+ $foo->removeUncommon($bar);
+ var_dump($foo->contains($a));
+ var_dump($foo->contains($b));
+
+?>
+--EXPECT--
+bool(false)
+bool(true)
Index: ext/spl/tests/SplObjectStorage_removeUncommon_invalid_parameter.phpt
===================================================================
--- ext/spl/tests/SplObjectStorage_removeUncommon_invalid_parameter.phpt (revision 0)
+++ ext/spl/tests/SplObjectStorage_removeUncommon_invalid_parameter.phpt (revision 0)
@@ -0,0 +1,44 @@
+--TEST--
+Check that SplObjectStorage::removeUncommon generate a warning and returns NULL when passed non-object param
+--CREDITS--
+Matthew Turland (m***@*atthewturland.com)
+Based on work done at PHPNW Testfest 2009 by Simon Westcott (swestc***@*mail.com)
+--FILE--
+<?php
+
+$data_provider = array(
+ array(),
+ true,
+ "string",
+ 12345,
+ 1.2345,
+ NULL
+);
+
+foreach($data_provider as $input) {
+
+ $s = new SplObjectStorage();
+
+ var_dump($s->removeUncommon($input));
+}
+
+?>
+--EXPECTF--
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, array given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, boolean given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, %unicode_string_optional% given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, integer given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, double given in %s on line %d
+NULL
+
+Warning: SplObjectStorage::removeUncommon() expects parameter 1 to be SplObjectStorage, null given in %s on line %d
+NULL
+
Index: ext/spl/spl_observer.c
===================================================================
--- ext/spl/spl_observer.c (revision 306446)
+++ ext/spl/spl_observer.c (working copy)
@@ -485,6 +485,35 @@
RETURN_LONG(zend_hash_num_elements(&intern->storage));
} /* }}} */

+/* {{{ proto bool SplObjectStorage::removeUncommon(SplObjectStorage $os)
+ Remove elements not common to both this SplObjectStorage instance and $os */
+SPL_METHOD(SplObjectStorage, removeUncommon)
+{
+ zval *obj;
+ spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC);
+ spl_SplObjectStorage *other;
+ spl_SplObjectStorageElement *element;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
+ return;
+ }
+
+ other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC);
+
+ zend_hash_internal_pointer_reset(&intern->storage);
+ while (zend_hash_get_current_data(&intern->storage, (void **)&element) == SUCCESS) {
+ if (!spl_object_storage_contains(other, element->obj TSRMLS_CC)) {
+ spl_object_storage_detach(intern, element->obj TSRMLS_CC);
+ }
+ zend_hash_move_forward(&intern->storage);
+ }
+
+ zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
+ intern->index = 0;
+
+ RETURN_LONG(zend_hash_num_elements(&intern->storage));
+}
+
/* {{{ proto bool SplObjectStorage::contains($obj)
Determine whethe an object is contained in the storage */
SPL_METHOD(SplObjectStorage, contains)
@@ -827,6 +856,7 @@
SPL_ME(SplObjectStorage, contains, arginfo_Object, 0)
SPL_ME(SplObjectStorage, addAll, arginfo_Object, 0)
SPL_ME(SplObjectStorage, removeAll, arginfo_Object, 0)
+ SPL_ME(SplObjectStorage, removeUncommon, arginfo_Object, 0)
SPL_ME(SplObjectStorage, getInfo, arginfo_splobject_void,0)
SPL_ME(SplObjectStorage, setInfo, arginfo_setInfo, 0)
/* Countable */--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

  • Root
  • » PHP
  • » [PATCH] SplObjectStorage::removeCommon and removeUncommon [RSS Feed]

Board footer

Moderator control

Enjoy the 12th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support