Skip to content

Commit

Permalink
- Implemented the constructor to initialize the IntSet.
Browse files Browse the repository at this point in the history
SVN Rev: 505
  • Loading branch information
derickr committed Jan 12, 2010
1 parent 63287a6 commit 2d18ced
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
31 changes: 30 additions & 1 deletion qh_intset.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ void qh_register_class_intset(TSRMLS_D)
ce_intset.create_object = qh_object_new_intset;
qh_ce_intset = zend_register_internal_class_ex(&ce_intset, NULL, NULL TSRMLS_CC);
memcpy(&qh_object_handlers_intset, zend_get_std_object_handlers(), sizeof(zend_object_handlers));

zend_declare_class_constant_long(qh_ce_intset, "CHECK_FOR_DUPES", sizeof("CHECK_FOR_DUPES") - 1, QH_NO_DUPLICATES TSRMLS_CC);
}

static inline zend_object_value qh_object_new_intset_ex(zend_class_entry *class_type, php_qh_intset_obj **ptr TSRMLS_DC)
Expand Down Expand Up @@ -95,15 +97,42 @@ static void qh_object_free_storage_intset(void *object TSRMLS_DC)
{
php_qh_intset_obj *intern = (php_qh_intset_obj *) object;

/* internal cleanup */
if (intern->hash) {
efree(intern->hash->options);
qhi_free(intern->hash);
}

zend_object_std_dtor(&intern->std TSRMLS_CC);
efree(object);
}

static int qh_intset_initialize(php_qh_intset_obj *obj, long size, long flags TSRMLS_DC)
{
qho *options = emalloc(sizeof(qho));

options->size = size;
options->check_for_dupes = (flags & QH_NO_DUPLICATES);

obj->hash = qhi_create(options);
if (obj->hash == NULL) {
efree(options);
return 0;
}
return 1;
}

PHP_METHOD(QuickHashIntSet, __construct)
{
long size;
long options = 0;

php_set_error_handling(EH_THROW, NULL TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &size, &options) == SUCCESS) {
if (!qh_intset_initialize(zend_object_store_get_object(getThis() TSRMLS_CC), size, options TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize set.");
}
}
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
}

PHP_METHOD(QuickHashIntSet, add)
Expand Down
3 changes: 3 additions & 0 deletions qh_intset.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ struct _php_qh_intset_obj {
qhi *hash;
};

/* Bitfield for hash options */
#define QH_NO_DUPLICATES 1

PHP_METHOD(QuickHashIntSet, __construct);
PHP_METHOD(QuickHashIntSet, add);
PHP_METHOD(QuickHashIntSet, exists);
Expand Down
48 changes: 48 additions & 0 deletions tests/create-int-hash.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
Basic tests for creating an IntHash
--INI--
extension=quickhash.so
xdebug.default_enable=0
--FILE--
<?php
var_dump( new QuickHashIntSet( 1024 ) );
var_dump( new QuickHashIntSet( 1024, 1 ) );
var_dump( new QuickHashIntSet( 1024, QuickHashIntSet::CHECK_FOR_DUPES ) );

try
{
new QuickHashIntSet();
}
catch ( Exception $e )
{
echo $e->getMessage(), "\n";
}

try
{
new QuickHashIntSet( 1024, QuickHashIntSet::CHECK_FOR_DUPES, 4 );
}
catch ( Exception $e )
{
echo $e->getMessage(), "\n";
}

try
{
new QuickHashIntSet( 2 );
}
catch ( Exception $e )
{
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
object(QuickHashIntSet)#1 (0) {
}
object(QuickHashIntSet)#1 (0) {
}
object(QuickHashIntSet)#1 (0) {
}
QuickHashIntSet::__construct() expects at least 1 parameter, 0 given
QuickHashIntSet::__construct() expects at most 2 parameters, 3 given
QuickHashIntSet::__construct(): Could not initialize set.

0 comments on commit 2d18ced

Please sign in to comment.