Book/quickhash-Phpdoc专题

Quickhash

目录

简介

This class wraps around a set containing integer numbers.

Sets can also be iterated over with foreach as the Iterator interface is implemented as well. The order of which elements are returned in is not guaranteed.

类摘要

QuickHashIntSet

class QuickHashIntSet {

/* Constants */

const int QuickHashIntSet::CHECK_FOR_DUPES = 1 ;

const int QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC = 2 ;

const int QuickHashIntSet::HASHER_NO_HASH = 256 ;

const int QuickHashIntSet::HASHER_JENKINS1 = 512 ;

const int QuickHashIntSet::HASHER_JENKINS2 = 1024 ;

/* Methods */

public bool add ( <span class="type">int $key )

public <span class="methodname">__construct ( <span class="methodparam">int $size [, int $options ] )

public bool delete ( <span class="type">int $key )

public bool exists ( <span class="type">int $key )

publicint <span class="methodname">getSize ( <span class="methodparam">void )

public <span class="modifier">static QuickHashIntSet loadFromFile ( <span class="methodparam">string $filename [, int $size [, <span class="type">int $options ]] )

public <span class="modifier">static QuickHashIntSet loadFromString ( <span class="methodparam">string $contents [, int $size [, <span class="type">int $options ]] )

public void saveToFile ( <span class="methodparam">string $filename )

public string saveToString ( <span class="methodparam">void )

}

预定义常量

QuickHashIntSet::CHECK_FOR_DUPES
If enabled, adding duplicate elements to a set (through either add() or loadFromFile()) will result in those elements to be dropped from the set. This will take up extra time, so only used when it is required.

QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC
Disables the use of PHP's internal memory manager for internal set structures. With this option enabled, internal allocations will not count towards the memory_limit settings.

QuickHashIntSet::HASHER_NO_HASH
Selects to not use a hashing function, but merely use a modulo to find the bucket list index. This is not faster than normal hashing, and gives more collisions.

QuickHashIntSet::HASHER_JENKINS1
This is the default hashing function to turn the integer hashes into bucket list indexes.

QuickHashIntSet::HASHER_JENKINS2
Selects a variant hashing algorithm.

QuickHashIntSet::add

This method adds a new entry to the set

说明

public bool QuickHashIntSet::add ( <span class="methodparam">int $key )

This method adds a new entry to the set, and returns whether the entry was added. Entries are by default always added unless QuickHashIntSet::CHECK_FOR_DUPES has been passed when the set was created.

参数

key
The key of the entry to add.

返回值

true when the entry was added, and false if the entry was not added.

范例

示例 #1 QuickHashIntSet::add example

<?php
echo "without dupe checking\n";
$set = new QuickHashIntSet( 1024 );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );

echo "\nwith dupe checking\n";
$set = new QuickHashIntSet( 1024, QuickHashIntSet::CHECK_FOR_DUPES );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
?>

以上例程的输出类似于:

without dupe checking
bool(false)
bool(true)
bool(true)
bool(true)

with dupe checking
bool(false)
bool(true)
bool(true)
bool(false)

QuickHashIntSet::__construct

Creates a new QuickHashIntSet object

说明

public <span class="methodname">QuickHashIntSet::__construct ( <span class="methodparam">int $size [, int $options ] )

This constructor creates a new QuickHashIntSet. The size is the amount of bucket lists to create. The more lists there are, the less collisions you will have. Options are also supported.

参数

size
The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options
The options that you can pass in are: QuickHashIntSet::CHECK_FOR_DUPES, which makes sure no duplicate entries are added to the set; QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC to not use PHP's internal memory manager as well as one of QuickHashIntSet::HASHER_NO_HASH, QuickHashIntSet::HASHER_JENKINS1 or QuickHashIntSet::HASHER_JENKINS2. These last three configure which hashing algorithm to use. All options can be combined using bitmasks.

返回值

Returns a new QuickHashIntSet object.

范例

示例 #1 QuickHashIntSet::__construct example

<?php
var_dump( new QuickHashIntSet( 1024 ) );
var_dump( new QuickHashIntSet( 1024, QuickHashIntSet::CHECK_FOR_DUPES ) );
var_dump(
    new QuickHashIntSet(
        1024,
        QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC | QuickHashIntSet::HASHER_JENKINS2 
    )
);
?>

QuickHashIntSet::delete

This method deletes an entry from the set

说明

public bool QuickHashIntSet::delete ( <span class="methodparam">int $key )

This method deletes an entry from the set, and returns whether the entry was deleted or not. Associated memory structures will not be freed immediately, but rather when the set itself is freed.

参数

key
The key of the entry to delete.

返回值

true when the entry was deleted, and false if the entry was not deleted.

范例

示例 #1 QuickHashIntSet::delete example

<?php
$set = new QuickHashIntSet( 1024 );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
var_dump( $set->delete( 4 ) );
var_dump( $set->exists( 4 ) );
var_dump( $set->delete( 4 ) );
?>

以上例程的输出类似于:

bool(false)
bool(true)
bool(true)
bool(false)
bool(false)

QuickHashIntSet::exists

This method checks whether a key is part of the set

说明

public bool QuickHashIntSet::exists ( <span class="methodparam">int $key )

This method checks whether an entry with the provided key exists in the set.

参数

key
The key of the entry to check for whether it exists in the set.

返回值

Returns true when the entry was found, or false when the entry is not found.

范例

示例 #1 QuickHashIntSet::exists example

<?php
//generate 200000 elements
$array = range( 0, 199999 );
$existingEntries = array_rand( array_flip( $array ), 180000 );
$testForEntries = array_rand( array_flip( $array ), 1000 );
$foundCount = 0;

echo "Creating set: ", microtime( true ), "\n";
$set = new QuickHashIntSet( 100000 );
echo "Adding elements: ", microtime( true ), "\n";
foreach( $existingEntries as $key )
{
     $set->add( $key );
}

echo "Doing 1000 tests: ", microtime( true ), "\n";
foreach( $testForEntries as $key )
{
     $foundCount += $set->exists( $key );
}
echo "Done, $foundCount found: ", microtime( true ), "\n";
?>

以上例程的输出类似于:

Creating set: 1263588703.0748
Adding elements: 1263588703.0757
Doing 1000 tests: 1263588703.7851
Done, 898 found: 1263588703.7897

QuickHashIntSet::getSize

Returns the number of elements in the set

说明

publicint <span class="methodname">QuickHashIntSet::getSize ( <span class="methodparam">void )

Returns the number of elements in the set.

参数

key
The key of the entry to add.

返回值

The number of elements in the set.

范例

示例 #1 QuickHashIntSet::getSize example

<?php
$set = new QuickHashIntSet( 8 );
var_dump( $set->add( 2 ) );
var_dump( $set->add( 3 ) );
var_dump( $set->getSize() );
?>

以上例程的输出类似于:

bool(true)
bool(true)
int(2)

QuickHashIntSet::loadFromFile

This factory method creates a set from a file

说明

public <span class="modifier">static QuickHashIntSet QuickHashIntSet::loadFromFile ( <span class="methodparam">string $filename [, int $size [, <span class="type">int $options ]] )

This factory method creates a new set from a definition file on disk. The file format consists of 32 bit signed integers packed together in the Endianness that the system that the code runs on uses.

参数

filename
The filename of the file to read the set from.

size
The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options
The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the set, rounded up to the nearest power of two with a maximum limit of 4194304.

返回值

Returns a new QuickHashIntSet.

范例

示例 #1 QuickHashIntSet::loadFromFile example

<?php
$file = dirname( __FILE__ ) . "/simple.set";
$set = QuickHashIntSet::loadFromFile(
    $file,
    QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key, 
        $set->exists( $key ) ? 'set' : 'unset'
    );
}
?>

以上例程的输出类似于:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

QuickHashIntSet::loadFromString

This factory method creates a set from a string

说明

public <span class="modifier">static QuickHashIntSet QuickHashIntSet::loadFromString ( <span class="methodparam">string $contents [, int $size [, <span class="type">int $options ]] )

This factory method creates a new set from a definition in a string. The file format consists of 32 bit signed integers packed together in the Endianness that the system that the code runs on uses.

参数

contents
The string containing a serialized format of the set.

size
The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options
The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the set, rounded up to the nearest power of two automatically limited from 64 to 4194304.

返回值

Returns a new QuickHashIntSet.

范例

示例 #1 QuickHashIntSet::loadFromString example

<?php
$contents = file_get_contents( dirname( __FILE__ ) . "/simple.set" );
$set = QuickHashIntSet::loadFromString(
    $contents,
    QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key, 
        $set->exists( $key ) ? 'set' : 'unset'
    );
}
?>

以上例程的输出类似于:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

QuickHashIntSet::saveToFile

This method stores an in-memory set to disk

说明

public void QuickHashIntSet::saveToFile ( <span class="methodparam">string $filename )

This method stores an existing set to a file on disk, in the same format that loadFromFile() can read.

参数

filename
The filename of the file to store the hash in.

返回值

没有返回值。

范例

示例 #1 QuickHashIntSet::saveToFile example

<?php
$set = new QuickHashIntSet( 1024 );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );

$set->saveToFile( '/tmp/test.set' );
?>

QuickHashIntSet::saveToString

This method returns a serialized version of the set

说明

public string QuickHashIntSet::saveToString ( <span class="methodparam">void )

This method returns a serialized version of the set in the same format that loadFromString() can read.

返回值

This method returns a string containing a serialized format of the set. Each element is stored as a four byte value in the Endianness that the current system uses.

范例

示例 #1 QuickHashIntSet::saveToString example

<?php
$set = new QuickHashIntSet( 1024 );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );

var_dump( $set->saveToString() );
?>

简介

This class wraps around a hash containing integer numbers, where the values are also integer numbers. Hashes are also available as implementation of the ArrayAccess interface.

Hashes can also be iterated over with foreach as the Iterator interface is implemented as well. The order of which elements are returned in is not guaranteed.

类摘要

QuickHashIntHash

class QuickHashIntHash {

/* Constants */

const int QuickHashIntHash::CHECK_FOR_DUPES = 1 ;

const int QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC = 2 ;

const int QuickHashIntHash::HASHER_NO_HASH = 256 ;

const int QuickHashIntHash::HASHER_JENKINS1 = 512 ;

const int QuickHashIntHash::HASHER_JENKINS2 = 1024 ;

/* Methods */

public bool add ( <span class="type">int $key [, <span class="methodparam">int $value ] )

public <span class="methodname">__construct ( <span class="methodparam">int $size [, int $options ] )

public bool delete ( <span class="type">int $key )

public bool exists ( <span class="type">int $key )

public int <span class="methodname">get ( <span class="type">int $key )

public int <span class="methodname">getSize ( <span class="methodparam">void )

public <span class="modifier">static <span class="type">QuickHashIntHash <span class="methodname">loadFromFile ( <span class="type">string $filename [, <span class="methodparam">int $options ] )

public <span class="modifier">static <span class="type">QuickHashIntHash <span class="methodname">loadFromString ( <span class="methodparam">string $contents [, int $options ] )

public void saveToFile ( <span class="methodparam">string $filename )

public string saveToString ( <span class="methodparam">void )

public bool set ( <span class="type">int $key , <span class="type">int $value )

public bool update ( <span class="type">int $key , <span class="type">int $value )

}

预定义常量

QuickHashIntHash::CHECK_FOR_DUPES
If enabled, adding duplicate elements to a set (through either add() or loadFromFile()) will result in those elements to be dropped from the set. This will take up extra time, so only used when it is required.

QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC
Disables the use of PHP's internal memory manager for internal set structures. With this option enabled, internal allocations will not count towards the memory_limit settings.

QuickHashIntHash::HASHER_NO_HASH
Selects to not use a hashing function, but merely use a modulo to find the bucket list index. This is not faster than normal hashing, and gives more collisions.

QuickHashIntHash::HASHER_JENKINS1
This is the default hashing function to turn the integer hashes into bucket list indexes.

QuickHashIntHash::HASHER_JENKINS2
Selects a variant hashing algorithm.

QuickHashIntHash::add

This method adds a new entry to the hash

说明

public bool QuickHashIntHash::add ( <span class="methodparam">int $key [, int $value ] )

This method adds a new entry to the hash, and returns whether the entry was added. Entries are by default always added unless QuickHashIntHash::CHECK_FOR_DUPES has been passed when the hash was created.

参数

key
The key of the entry to add.

value
The optional value of the entry to add. If no value is specified, 1 will be used.

返回值

true when the entry was added, and false if the entry was not added.

范例

示例 #1 QuickHashIntHash::add example

<?php
echo "without dupe checking\n";
$hash = new QuickHashIntHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, 22 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, 12 ) );

echo "\nwith dupe checking\n";
$hash = new QuickHashIntHash( 1024, QuickHashIntHash::CHECK_FOR_DUPES );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, 78 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, 9 ) );

echo "\ndefault value\n";
var_dump( $hash->add( 5 ) );
var_dump( $hash->get( 5 ) );
?>

以上例程的输出类似于:

without dupe checking
bool(false)
bool(false)
bool(true)
bool(true)
int(22)
bool(true)

with dupe checking
bool(false)
bool(false)
bool(true)
bool(true)
int(78)
bool(false)

default value
bool(true)
int(1)

QuickHashIntHash::__construct

Creates a new QuickHashIntHash object

说明

public <span class="methodname">QuickHashIntHash::__construct ( <span class="methodparam">int $size [, int $options ] )

This constructor creates a new QuickHashIntHash. The size is the amount of bucket lists to create. The more lists there are, the less collisions you will have. Options are also supported.

参数

size
The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 64 to 4194304.

options
The options that you can pass in are: QuickHashIntHash::CHECK_FOR_DUPES, which makes sure no duplicate entries are added to the hash; QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC to not use PHP's internal memory manager as well as one of QuickHashIntHash::HASHER_NO_HASH, QuickHashIntHash::HASHER_JENKINS1 or QuickHashIntHash::HASHER_JENKINS2. These last three configure which hashing algorithm to use. All options can be combined using bitmasks.

返回值

Returns a new QuickHashIntHash object.

范例

示例 #1 QuickHashIntHash::__construct example

<?php
var_dump( new QuickHashIntHash( 1024 ) );
var_dump( new QuickHashIntHash( 1024, QuickHashIntHash::CHECK_FOR_DUPES ) );
var_dump(
    new QuickHashIntHash(
        1024,
        QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC | QuickHashIntHash::HASHER_JENKINS2 
    )
);
?>

QuickHashIntHash::delete

This method deletes am entry from the hash

说明

public bool QuickHashIntHash::delete ( <span class="methodparam">int $key )

This method deletes an entry from the hash, and returns whether the entry was deleted or not. Associated memory structures will not be freed immediately, but rather when the hash itself is freed.

Elements can not be deleted when the hash is used in an iterator. The method will not throw an exception, but simply return false like would happen with any other deletion failure.

参数

key
The key of the entry to delete.

返回值

true when the entry was deleted, and false if the entry was not deleted.

范例

示例 #1 QuickHashIntHash::delete example

<?php
$hash = new QuickHashIntHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, 5 ) );
var_dump( $hash->delete( 4 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->delete( 4 ) );
?>

以上例程的输出类似于:

bool(false)
bool(true)
bool(true)
bool(false)
bool(false)

QuickHashIntHash::exists

This method checks whether a key is part of the hash

说明

public bool QuickHashIntHash::exists ( <span class="methodparam">int $key )

This method checks whether an entry with the provided key exists in the hash.

参数

key
The key of the entry to check for whether it exists in the hash.

返回值

Returns true when the entry was found, or false when the entry is not found.

范例

示例 #1 QuickHashIntHash::exists example

<?php
//generate 200000 elements
$array = range( 0, 199999 );
$existingEntries = array_rand( array_flip( $array ), 180000 );
$testForEntries = array_rand( array_flip( $array ), 1000 );
$foundCount = 0;

echo "Creating hash: ", microtime( true ), "\n";
$hash = new QuickHashIntHash( 100000 );
echo "Adding elements: ", microtime( true ), "\n";
foreach( $existingEntries as $key )
{
     $hash->add( $key, 56 );
}

echo "Doing 1000 tests: ", microtime( true ), "\n";
foreach( $testForEntries as $key )
{
     $foundCount += $hash->exists( $key );
}
echo "Done, $foundCount found: ", microtime( true ), "\n";
?>

以上例程的输出类似于:

Creating hash: 1263588703.0748
Adding elements: 1263588703.0757
Doing 1000 tests: 1263588703.7851
Done, 898 found: 1263588703.7897

QuickHashIntHash::get

This method retrieves a value from the hash by its key

说明

public int <span class="methodname">QuickHashIntHash::get ( <span class="methodparam">int $key )

This method retrieves a value from the hash by its key.

参数

key
The key of the entry to add.

返回值

The value if the key exists, or null if the key wasn't part of the hash.

范例

示例 #1 QuickHashIntHash::get example

<?php
$hash = new QuickHashIntHash( 8 );
var_dump( $hash->get( 1 ) );

var_dump( $hash->add( 2 ) );
var_dump( $hash->get( 2 ) );

var_dump( $hash->add( 3, 5 ) );
var_dump( $hash->get( 3 ) );
?>

以上例程的输出类似于:

bool(false)
bool(true)
int(1)
bool(true)
int(5)

QuickHashIntHash::getSize

Returns the number of elements in the hash

说明

public int <span class="methodname">QuickHashIntHash::getSize ( <span class="methodparam">void )

Returns the number of elements in the hash.

参数

key
The key of the entry to add.

返回值

The number of elements in the hash.

范例

示例 #1 QuickHashIntHash::getSize example

<?php
$hash = new QuickHashIntHash( 8 );
var_dump( $hash->add( 2 ) );
var_dump( $hash->add( 3, 5 ) );
var_dump( $hash->getSize() );
?>

以上例程的输出类似于:

bool(true)
bool(true)
int(2)

QuickHashIntHash::loadFromFile

This factory method creates a hash from a file

说明

public <span class="modifier">static <span class="type">QuickHashIntHash <span class="methodname">QuickHashIntHash::loadFromFile ( <span class="methodparam">string $filename [, int $options ] )

This factory method creates a new hash from a definition file on disk. The file format consists of a signature 'QH\0x11\0', the number of elements as a 32 bit signed integer in system Endianness, followed by 32 bit signed integers packed together in the Endianness that the system that the code runs on uses. For each hash element there are two 32 bit signed integers stored. The first of each element is the key, and the second is the value belonging to the key. An example could be:

示例 #1 QuickHash IntHash file format

00000000  51 48 11 00 02 00 00 00  01 00 00 00 01 00 00 00  |QH..............|
00000010  03 00 00 00 09 00 00 00                           |........|
00000018

示例 #2 QuickHash IntHash file format

header signature ('QH'; key type: 1; value type: 1; filler: \0x00)
00000000  51 48 11 00

number of elements:
00000004  02 00 00 00

data string:
00000000  01 00 00 00 01 00 00 00  03 00 00 00 09 00 00 00

key/value 1 (key = 1, value = 1)
01 00 00 00  01 00 00 00

key/value 2 (key = 3, value = 9)
03 00 00 00  09 00 00 00

参数

filename
The filename of the file to read the hash from.

options
The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the hash, rounded up to the nearest power of two with a maximum limit of 4194304.

返回值

Returns a new QuickHashIntHash.

范例

示例 #3 QuickHashIntHash::loadFromFile example

<?php
$file = dirname( __FILE__ ) . "/simple.hash";
$hash = QuickHashIntHash::loadFromFile(
    $file,
    QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key, 
        $hash->exists( $key ) ? 'set' : 'unset'
    );
}
?>

以上例程的输出类似于:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

QuickHashIntHash::loadFromString

This factory method creates a hash from a string

说明

public <span class="modifier">static <span class="type">QuickHashIntHash <span class="methodname">QuickHashIntHash::loadFromString ( <span class="methodparam">string $contents [, int $options ] )

This factory method creates a new hash from a definition in a string. The file format consists of 32 bit signed integers packed together in the Endianness that the system that the code runs on uses. For each element there are two 32 bit signed integers stored. The first of each element is the key, and the second is the value belonging to the key.

参数

contents
The string containing a serialized format of the hash.

options
The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the hash, rounded up to the nearest power of two with a maximum limit of 4194304.

返回值

Returns a new QuickHashIntHash.

范例

示例 #1 <span class="function">QuickHashIntHash::loadFromString example

<?php
$contents = file_get_contents( dirname( __FILE__ ) . "/simple.hash" );
$hash = QuickHashIntHash::loadFromString(
    $contents,
    QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key, 
        $hash->exists( $key ) ? 'set' : 'unset'
    );
}
?>

以上例程的输出类似于:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

QuickHashIntHash::saveToFile

This method stores an in-memory hash to disk

说明

public void QuickHashIntHash::saveToFile ( <span class="methodparam">string $filename )

This method stores an existing hash to a file on disk, in the same format that loadFromFile() can read.

参数

filename
The filename of the file to store the hash in.

返回值

没有返回值。

范例

示例 #1 QuickHashIntHash::saveToFile example

<?php
$hash = new QuickHashIntHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, 43 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, 52 ) );

$hash->saveToFile( '/tmp/test.hash' );
?>

QuickHashIntHash::saveToString

This method returns a serialized version of the hash

说明

public string QuickHashIntHash::saveToString ( <span class="methodparam">void )

This method returns a serialized version of the hash in the same format that loadFromString() can read.

返回值

This method returns a string containing a serialized format of the hash. Each element is stored as a four byte value in the Endianness that the current system uses.

范例

示例 #1 QuickHashIntHash::saveToString example

<?php
$hash = new QuickHashIntHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, 34 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, 55 ) );

var_dump( $hash->saveToString() );
?>

QuickHashIntHash::set

This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

说明

public bool QuickHashIntHash::set ( <span class="methodparam">int $key , <span class="methodparam">int $value )

This method tries to update an entry with a new value. In case the entry did not yet exist, it will instead add a new entry. It returns whether the entry was added or update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashIntHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

参数

key
The key of the entry to add or update.

value
The new value to set the entry with.

返回值

2 if the entry was found and updated, 1 if the entry was newly added or 0 if there was an error.

范例

示例 #1 QuickHashIntHash::set example

<?php
$hash = new QuickHashIntHash( 1024 );

echo "Set->Add\n";
var_dump( $hash->get( 46692 ) );
var_dump( $hash->set( 46692, 16091 ) );
var_dump( $hash->get( 46692 ) );

echo "Set->Update\n";
var_dump( $hash->set( 46692, 29906 ) );
var_dump( $hash->get( 46692 ) );
?>

以上例程的输出类似于:

bool(false)
int(2)
int(16091)
Set->Update
int(1)
int(29906)

QuickHashIntHash::update

This method updates an entry in the hash with a new value

说明

public bool QuickHashIntHash::update ( <span class="methodparam">int $key , <span class="methodparam">int $value )

This method updates an entry with a new value, and returns whether the entry was update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashIntHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

参数

key
The key of the entry to add.

value
The new value to update the entry with.

返回值

true when the entry was found and updated, and false if the entry was not part of the hash already.

范例

示例 #1 QuickHashIntHash::update example

<?php
$hash = new QuickHashIntHash( 1024 );

var_dump( $hash->add( 141421, 173205 ) );
var_dump( $hash->update( 141421, 223606 ) );
var_dump( $hash->get( 141421 ) );
?>

以上例程的输出类似于:

bool(true)
bool(true)
int(223606)

简介

This class wraps around a hash containing strings, where the values are integer numbers. Hashes are also available as implementation of the ArrayAccess interface.

Hashes can also be iterated over with foreach as the Iterator interface is implemented as well. The order of which elements are returned in is not guaranteed.

类摘要

QuickHashStringIntHash

class QuickHashStringIntHash {

/* Constants */

const int QuickHashStringIntHash::CHECK_FOR_DUPES = 1 ;

const int QuickHashStringIntHash::DO_NOT_USE_ZEND_ALLOC <span class="initializer"> = 2 ;

/* Methods */

public bool add ( <span class="type">string $key , <span class="methodparam">int $value )

public <span class="methodname">__construct ( <span class="methodparam">int $size [, int $options<span class="initializer"> = 0 ] )

public bool delete ( <span class="type">string $key )

public bool exists ( <span class="type">string $key )

public mixed get ( <span class="type">string $key )

public int <span class="methodname">getSize ( <span class="methodparam">void )

public <span class="modifier">static <span class="type">QuickHashStringIntHash <span class="methodname">loadFromFile ( <span class="type">string $filename [, <span class="methodparam">int $size<span class="initializer"> = 0 [, <span class="methodparam">int $options<span class="initializer"> = 0 ]] )

public <span class="modifier">static <span class="type">QuickHashStringIntHash <span class="methodname">loadFromString ( <span class="methodparam">string $contents [, int $size<span class="initializer"> = 0 [, <span class="methodparam">int $options<span class="initializer"> = 0 ]] )

public void saveToFile ( <span class="methodparam">string $filename )

public string saveToString ( <span class="methodparam">void )

public int <span class="methodname">set ( <span class="type">string $key , <span class="methodparam">int $value )

public bool update ( <span class="type">string $key , <span class="methodparam">int $value )

}

预定义常量

QuickHashStringIntHash::CHECK_FOR_DUPES
If enabled, adding duplicate elements to a set (through either add() or loadFromFile()) will result in those elements to be dropped from the set. This will take up extra time, so only used when it is required.

QuickHashStringIntHash::DO_NOT_USE_ZEND_ALLOC
Disables the use of PHP's internal memory manager for internal set structures. With this option enabled, internal allocations will not count towards the memory_limit settings.

QuickHashStringIntHash::add

This method adds a new entry to the hash

说明

public bool QuickHashStringIntHash::add ( <span class="methodparam">string $key , int $value )

This method adds a new entry to the hash, and returns whether the entry was added. Entries are by default always added unless QuickHashStringIntHash::CHECK_FOR_DUPES has been passed when the hash was created.

参数

key
The key of the entry to add.

value
The value of the entry to add.

返回值

true when the entry was added, and false if the entry was not added.

范例

示例 #1 QuickHashStringIntHash::add example

<?php
echo "without dupe checking\n";
$hash = new QuickHashStringIntHash( 1024 );
var_dump( $hash );
var_dump( $hash->exists( "four" ) );
var_dump( $hash->get( "four" ) );
var_dump( $hash->add( "four", 22 ) );
var_dump( $hash->exists( "four" ) );
var_dump( $hash->get( "four" ) );
var_dump( $hash->add( "four", 12 ) );

echo "\nwith dupe checking\n";
$hash = new QuickHashStringIntHash( 1024, QuickHashStringIntHash::CHECK_FOR_DUPES );
var_dump( $hash );
var_dump( $hash->exists( "four" ) );
var_dump( $hash->get( "four" ) );
var_dump( $hash->add( "four", 78 ) );
var_dump( $hash->exists( "four" ) );
var_dump( $hash->get( "four" ) );
var_dump( $hash->add( "four", 9 ) );
?>

以上例程的输出类似于:

without dupe checking
object(QuickHashStringIntHash)#1 (0) {
}
bool(false)
bool(false)
bool(true)
bool(true)
int(22)
bool(true)

with dupe checking
object(QuickHashStringIntHash)#2 (0) {
}
bool(false)
bool(false)
bool(true)
bool(true)
int(78)
bool(false)

QuickHashStringIntHash::__construct

Creates a new QuickHashStringIntHash object

说明

public <span class="methodname">QuickHashStringIntHash::__construct ( <span class="methodparam">int $size [, int $options<span class="initializer"> = 0 ] )

This constructor creates a new QuickHashStringIntHash. The size is the amount of bucket lists to create. The more lists there are, the less collisions you will have. Options are also supported.

参数

size
The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 64 to 4194304.

options
The options that you can pass in are: QuickHashStringIntHash::CHECK_FOR_DUPES, which makes sure no duplicate entries are added to the hash and QuickHashStringIntHash::DO_NOT_USE_ZEND_ALLOC to not use PHP's internal memory manager.

返回值

Returns a new QuickHashStringIntHash object.

范例

示例 #1 <span class="function">QuickHashStringIntHash::__construct example

<?php
var_dump( new QuickHashStringIntHash( 1024 ) );
var_dump( new QuickHashStringIntHash( 1024, QuickHashStringIntHash::CHECK_FOR_DUPES ) );
?>

QuickHashStringIntHash::delete

This method deletes am entry from the hash

说明

public bool QuickHashStringIntHash::delete ( <span class="methodparam">string $key )

This method deletes an entry from the hash, and returns whether the entry was deleted or not. Associated memory structures will not be freed immediately, but rather when the hash itself is freed.

Elements can not be deleted when the hash is used in an iterator. The method will not throw an exception, but simply return false like would happen with any other deletion failure.

参数

key
The key of the entry to delete.

返回值

true when the entry was deleted, and false if the entry was not deleted.

范例

示例 #1 QuickHashStringIntHash::delete example

<?php
$hash = new QuickHashStringIntHash( 1024 );
var_dump( $hash->exists( 'four' ) );
var_dump( $hash->add( 'four', 5 ) );
var_dump( $hash->get( 'four' ) );
var_dump( $hash->delete( 'four' ) );
var_dump( $hash->exists( 'four' ) );
var_dump( $hash->get( 'four' ) );
var_dump( $hash->delete( 'four' ) );
?>

以上例程的输出类似于:

bool(false)
bool(true)
int(5)
bool(true)
bool(false)
bool(false)
bool(false)

QuickHashStringIntHash::exists

This method checks whether a key is part of the hash

说明

public bool QuickHashStringIntHash::exists ( <span class="methodparam">string $key )

This method checks whether an entry with the provided key exists in the hash.

参数

key
The key of the entry to check for whether it exists in the hash.

返回值

Returns true when the entry was found, or false when the entry is not found.

QuickHashStringIntHash::get

This method retrieves a value from the hash by its key

说明

public mixed QuickHashStringIntHash::get ( <span class="methodparam">string $key )

This method retrieves a value from the hash by its key.

参数

key
The key of the entry to add.

返回值

The value if the key exists, or null if the key wasn't part of the hash.

范例

示例 #1 QuickHashStringIntHash::get example

<?php
$hash = new QuickHashStringIntHash( 8 );
var_dump( $hash->get( "one" ) );

var_dump( $hash->add( "two", 2 ) );
var_dump( $hash->get( "two" ) );
?>

以上例程的输出类似于:

bool(false)
bool(true)
int(2)

QuickHashStringIntHash::getSize

Returns the number of elements in the hash

说明

public int <span class="methodname">QuickHashStringIntHash::getSize ( <span class="methodparam">void )

Returns the number of elements in the hash.

参数

key
The key of the entry to add.

返回值

The number of elements in the hash.

范例

示例 #1 QuickHashStringIntHash::getSize example

<?php
$hash = new QuickHashStringIntHash( 8 );
var_dump( $hash->add( "two", 2 ) );
var_dump( $hash->add( "three", 5 ) );
var_dump( $hash->getSize() );
?>

以上例程的输出类似于:

bool(true)
bool(true)
int(2)

QuickHashStringIntHash::loadFromFile

This factory method creates a hash from a file

说明

public <span class="modifier">static <span class="type">QuickHashStringIntHash <span class="methodname">QuickHashStringIntHash::loadFromFile ( <span class="methodparam">string $filename [, int $size<span class="initializer"> = 0 [, <span class="methodparam">int $options<span class="initializer"> = 0 ]] )

This factory method creates a new hash from a definition file on disk. The file format consists of a signature 'QH\0x21\0', the number of elements as a 32 bit signed integer in system Endianness, an unsigned 32 bit integer containing the number of element data to follow in characters. This element data contains all the strings. The follows another signed 32 bit integer containing the number of bucket lists. After the header and the strings, the elements follow. They are ordered by bucket list so that the keys don't have to be hashed in order to restore the hash. For each bucket list, the following information is stored (all as 32 bit integers): the bucket list index, the number of elements in that list, and then in pairs of two unsigned 32 bit integers the elements, where the first one is the index into the string list containing the keys, and the second one the value. An example could be:

示例 #1 QuickHash StringIntHash file format

00000000  51 48 21 00 02 00 00 00  09 00 00 00 40 00 00 00  |QH!.........@...|
00000010  4f 4e 45 00 4e 49 4e 45  00 07 00 00 00 01 00 00  |ONE.NINE........|
00000020  00 00 00 00 00 01 00 00  00 2f 00 00 00 01 00 00  |........./......|
00000030  00 04 00 00 00 03 00 00  00                       |.........|
00000039

示例 #2 QuickHash IntHash file format

header signature ('QH'; key type: 2; value type: 1; filler: \0x00)
00000000  51 48 21 00

number of elements:
00000004  02 00 00 00

length of string values (9 characters):
00000008  09 00 00 00

number of hash bucket lists (this is configured for hashes as argument to the
constructor normally, 64 in this case):
0000000C  40 00 00 00

string values:
00000010  4f 4e 45 00 4e 49 4e 45  00

bucket lists:
  bucket list 1 (with key 7, and 1 element):
    header:
    07 00 00 00 01 00 00 00
    elements (key index: 0 ('ONE'), value = 0):
    00 00 00 00 01 00 00 00
  bucket list 2 (with key 0x2f, and 1 element):
    header:
    2f 00 00 00 01 00 00 00
    elements (key index: 4 ('NINE'), value = 3):
    04 00 00 00 03 00 00 00

参数

filename
The filename of the file to read the hash from.

size
The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options
The same options that the class' constructor takes; except that the size option is ignored. It is read from the file format (unlike the QuickHashIntHash and QuickHashIntStringHash classes, where it is automatically calculated from the number of entries in the hash.)

返回值

Returns a new QuickHashStringIntHash.

范例

示例 #3 <span class="function">QuickHashStringIntHash::loadFromFile example

<?php
$file = dirname( __FILE__ ) . "/simple.hash.string";
$hash = QuickHashStringIntHash::loadFromFile(
    $file,
    QuickHashStringIntHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    $i = 48712 + $key * 1631;
    $k = base_convert( $i, 10, 36 );
    echo $k, ' => ', $hash->get( $k ), "\n";
}
?>

以上例程的输出类似于:

11l4 => 48712
12uf => 50343
143q => 51974
15d1 => 53605
16mc => 55236
17vn => 56867
194y => 58498
1ae9 => 60129
1bnk => 61760
1cwv => 63391
1e66 => 65022
1ffh => 66653
1gos => 68284
1hy3 => 69915
1j7e => 71546
1kgp => 73177

QuickHashStringIntHash::loadFromString

This factory method creates a hash from a string

说明

public <span class="modifier">static <span class="type">QuickHashStringIntHash <span class="methodname">QuickHashStringIntHash::loadFromString ( <span class="methodparam">string $contents [, int $size<span class="initializer"> = 0 [, <span class="methodparam">int $options<span class="initializer"> = 0 ]] )

This factory method creates a new hash from a definition in a string. The format is the same as the one used in "loadFromFile".

参数

contents
The string containing a serialized format of the hash.

size
The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options
The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the hash, rounded up to the nearest power of two with a maximum limit of 4194304.

返回值

Returns a new QuickHashStringIntHash.

范例

示例 #1 <span class="function">QuickHashStringIntHash::loadFromString example

<?php
$contents = file_get_contents( dirname( __FILE__ ) . "/simple.hash.string" );
$hash = QuickHashStringIntHash::loadFromString(
    $contents,
    QuickHashStringIntHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    $i = 48712 + $key * 1631;
    $k = base_convert( $i, 10, 36 );
    echo $k, ' => ', $hash->get( $k ), "\n";
}
?>

以上例程的输出类似于:

11l4 => 48712
12uf => 50343
143q => 51974
15d1 => 53605
16mc => 55236
17vn => 56867
194y => 58498
1ae9 => 60129
1bnk => 61760
1cwv => 63391
1e66 => 65022
1ffh => 66653
1gos => 68284
1hy3 => 69915
1j7e => 71546
1kgp => 73177

QuickHashStringIntHash::saveToFile

This method stores an in-memory hash to disk

说明

public void QuickHashStringIntHash::saveToFile ( string $filename )

This method stores an existing hash to a file on disk, in the same format that loadFromFile() can read.

参数

filename
The filename of the file to store the hash in.

返回值

没有返回值。

范例

示例 #1 <span class="function">QuickHashStringIntHash::saveToFile example

<?php
$hash = new QuickHashStringIntHash( 1024 );
var_dump( $hash->add( "fourty three", 42 ) );
var_dump( $hash->add( "fifty two", 52 ) );

$hash->saveToFile( '/tmp/test.hash.string' );
?>

QuickHashStringIntHash::saveToString

This method returns a serialized version of the hash

说明

public string QuickHashStringIntHash::saveToString ( void )

This method returns a serialized version of the hash in the same format that loadFromString() can read.

返回值

This method returns a serialized format of an existing hash, in the same format that loadFromString() can read.

范例

示例 #1 <span class="function">QuickHashStringIntHash::saveToString example

<?php
$hash = new QuickHashStringIntHash( 1024 );
var_dump( $hash->add( "fourty three", 42 ) );
var_dump( $hash->add( "fifty two", 52 ) );

var_dump( $hash->saveToString() );
?>

QuickHashStringIntHash::set

This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

说明

public int <span class="methodname">QuickHashStringIntHash::set ( <span class="methodparam">string $key , int $value )

This method tries to update an entry with a new value. In case the entry did not yet exist, it will instead add a new entry. It returns whether the entry was added or update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashStringIntHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

参数

key
The key of the entry to add or update.

value
The value of the entry to add. If a non-string is passed, it will be converted to a string automatically if possible.

返回值

2 if the entry was found and updated, 1 if the entry was newly added or 0 if there was an error.

范例

示例 #1 QuickHashStringIntHash::set example

<?php
$hash = new QuickHashStringIntHash( 1024 );

echo "Set->Add\n";
var_dump( $hash->get( "fourty six thousand six hundred ninety two" ) );
var_dump( $hash->set( "fourty six thousand six hundred ninety two", 16091 ) );
var_dump( $hash->get( "fourty six thousand six hundred ninety two" ) );

echo "Set->Update\n";
var_dump( $hash->set( "fourty six thousand six hundred ninety two", 29906 ) );
var_dump( $hash->get( "fourty six thousand six hundred ninety two" ) );
?>

以上例程的输出类似于:

Set->Add
bool(false)
int(2)
int(16091)
Set->Update
int(1)
int(29906)

QuickHashStringIntHash::update

This method updates an entry in the hash with a new value

说明

public bool QuickHashStringIntHash::update ( <span class="methodparam">string $key , int $value )

This method updates an entry with a new value, and returns whether the entry was update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashStringIntHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

参数

key
The key of the entry to add.

value
The new value for the entry. If a non-string is passed, it will be converted to a string automatically if possible.

返回值

true when the entry was found and updated, and false if the entry was not part of the hash already.

范例

示例 #1 QuickHashStringIntHash::update example

<?php
$hash = new QuickHashStringIntHash( 1024 );

$hash->add( 'six', 314159265 );
$hash->add( "a lot", 314159265 );

echo $hash->get( 'six' ), "\n";
echo $hash->get( 'a lot' ), "\n";

var_dump( $hash->update( 'a lot', 314159266 ) ); 
var_dump( $hash->update( "a lot plus one", 314159999 ) );

echo $hash->get( 'six' ), "\n";
echo $hash->get( 'a lot' ), "\n";
?>

以上例程的输出类似于:

314159265
314159265
bool(true)
bool(false)
314159265
314159266

简介

This class wraps around a hash containing integer numbers, where the values are strings. Hashes are also available as implementation of the ArrayAccess interface.

Hashes can also be iterated over with foreach as the Iterator interface is implemented as well. The order of which elements are returned in is not guaranteed.

类摘要

QuickHashIntStringHash

class QuickHashIntStringHash {

/* Constants */

const int QuickHashIntStringHash::CHECK_FOR_DUPES = 1 ;

const int QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC <span class="initializer"> = 2 ;

const int QuickHashIntStringHash::HASHER_NO_HASH = 256 ;

const int QuickHashIntStringHash::HASHER_JENKINS1 = 512 ;

const int QuickHashIntStringHash::HASHER_JENKINS2 = 1024 ;

/* Methods */

public bool add ( <span class="type">int $key , <span class="type">string $value )

public <span class="methodname">__construct ( <span class="methodparam">int $size [, int $options<span class="initializer"> = 0 ] )

public bool delete ( <span class="type">int $key )

public bool exists ( <span class="type">int $key )

public mixed get ( <span class="type">int $key )

public int <span class="methodname">getSize ( <span class="methodparam">void )

public <span class="modifier">static <span class="type">QuickHashIntStringHash <span class="methodname">loadFromFile ( <span class="type">string $filename [, <span class="methodparam">int $size<span class="initializer"> = 0 [, <span class="methodparam">int $options<span class="initializer"> = 0 ]] )

public <span class="modifier">static <span class="type">QuickHashIntStringHash <span class="methodname">loadFromString ( <span class="methodparam">string $contents [, int $size<span class="initializer"> = 0 [, <span class="methodparam">int $options<span class="initializer"> = 0 ]] )

public void saveToFile ( <span class="methodparam">string $filename )

public string saveToString ( <span class="methodparam">void )

public int <span class="methodname">set ( <span class="type">int $key , <span class="type">string $value )

public bool update ( <span class="type">int $key , <span class="type">string $value )

}

预定义常量

QuickHashIntStringHash::CHECK_FOR_DUPES
If enabled, adding duplicate elements to a set (through either add() or loadFromFile()) will result in those elements to be dropped from the set. This will take up extra time, so only used when it is required.

QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC
Disables the use of PHP's internal memory manager for internal set structures. With this option enabled, internal allocations will not count towards the memory_limit settings.

QuickHashIntStringHash::HASHER_NO_HASH
Selects to not use a hashing function, but merely use a modulo to find the bucket list index. This is not faster than normal hashing, and gives more collisions.

QuickHashIntStringHash::HASHER_JENKINS1
This is the default hashing function to turn the integer hashes into bucket list indexes.

QuickHashIntStringHash::HASHER_JENKINS2
Selects a variant hashing algorithm.

QuickHashIntStringHash::add

This method adds a new entry to the hash

说明

public bool QuickHashIntStringHash::add ( <span class="methodparam">int $key , <span class="methodparam">string $value )

This method adds a new entry to the hash, and returns whether the entry was added. Entries are by default always added unless QuickHashIntStringHash::CHECK_FOR_DUPES has been passed when the hash was created.

参数

key
The key of the entry to add.

value
The value of the entry to add. If a non-string is passed, it will be converted to a string automatically if possible.

返回值

true when the entry was added, and false if the entry was not added.

范例

示例 #1 QuickHashIntStringHash::add example

<?php
echo "without dupe checking\n";
$hash = new QuickHashIntStringHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, "twenty two" ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, "twelve" ) );

echo "\nwith dupe checking\n";
$hash = new QuickHashIntStringHash( 1024, QuickHashIntStringHash::CHECK_FOR_DUPES );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, "seventy eight" ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, "nine" ) );
?>

以上例程的输出类似于:

without dupe checking
bool(false)
bool(false)
bool(true)
bool(true)
string(10) "twenty two"
bool(true)

with dupe checking
bool(false)
bool(false)
bool(true)
bool(true)
string(13) "seventy eight"
bool(false)

QuickHashIntStringHash::__construct

Creates a new QuickHashIntStringHash object

说明

public <span class="methodname">QuickHashIntStringHash::__construct ( <span class="methodparam">int $size [, int $options<span class="initializer"> = 0 ] )

This constructor creates a new QuickHashIntStringHash. The size is the amount of bucket lists to create. The more lists there are, the less collisions you will have. Options are also supported.

参数

size
The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 64 to 4194304.

options
The options that you can pass in are: QuickHashIntStringHash::CHECK_FOR_DUPES, which makes sure no duplicate entries are added to the hash; QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC to not use PHP's internal memory manager as well as one of QuickHashIntStringHash::HASHER_NO_HASH, QuickHashIntStringHash::HASHER_JENKINS1 or QuickHashIntStringHash::HASHER_JENKINS2. These last three configure which hashing algorithm to use. All options can be combined using bitmasks.

返回值

Returns a new QuickHashIntStringHash object.

范例

示例 #1 <span class="function">QuickHashIntStringHash::__construct example

<?php
var_dump( new QuickHashIntStringHash( 1024 ) );
var_dump( new QuickHashIntStringHash( 1024, QuickHashIntStringHash::CHECK_FOR_DUPES ) );
var_dump(
    new QuickHashIntStringHash(
        1024,
        QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC | QuickHashIntStringHash::HASHER_JENKINS2 
    )
);
?>

QuickHashIntStringHash::delete

This method deletes am entry from the hash

说明

public bool QuickHashIntStringHash::delete ( <span class="methodparam">int $key )

This method deletes an entry from the hash, and returns whether the entry was deleted or not. Associated memory structures will not be freed immediately, but rather when the hash itself is freed.

Elements can not be deleted when the hash is used in an iterator. The method will not throw an exception, but simply return false like would happen with any other deletion failure.

参数

key
The key of the entry to delete.

返回值

true when the entry was deleted, and false if the entry was not deleted.

范例

示例 #1 QuickHashIntStringHash::delete example

<?php
$hash = new QuickHashIntStringHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, "five" ) );
var_dump( $hash->delete( 4 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->delete( 4 ) );
?>

以上例程的输出类似于:

bool(false)
bool(true)
bool(true)
bool(false)
bool(false)

QuickHashIntStringHash::exists

This method checks whether a key is part of the hash

说明

public bool QuickHashIntStringHash::exists ( <span class="methodparam">int $key )

This method checks whether an entry with the provided key exists in the hash.

参数

key
The key of the entry to check for whether it exists in the hash.

返回值

Returns true when the entry was found, or false when the entry is not found.

QuickHashIntStringHash::get

This method retrieves a value from the hash by its key

说明

public mixed QuickHashIntStringHash::get ( <span class="methodparam">int $key )

This method retrieves a value from the hash by its key.

参数

key
The key of the entry to add.

返回值

The value if the key exists, or null if the key wasn't part of the hash.

范例

示例 #1 QuickHashIntStringHash::get example

<?php
$hash = new QuickHashIntStringHash( 8 );
var_dump( $hash->get( 1 ) );

var_dump( $hash->add( 2, "two" ) );
var_dump( $hash->get( 2 ) );

var_dump( $hash->add( 3, 5 ) );
var_dump( $hash->get( 3 ) );
?>

以上例程的输出类似于:

bool(false)
bool(true)
string(3) "two"
bool(true)
string(1) "5"

QuickHashIntStringHash::getSize

Returns the number of elements in the hash

说明

public int <span class="methodname">QuickHashIntStringHash::getSize ( <span class="methodparam">void )

Returns the number of elements in the hash.

返回值

The number of elements in the hash.

范例

示例 #1 QuickHashIntStringHash::getSize example

<?php
$hash = new QuickHashIntStringHash( 8 );
var_dump( $hash->add( 2, "two" ) );
var_dump( $hash->add( 3, 5 ) );
var_dump( $hash->getSize() );
?>

以上例程的输出类似于:

bool(true)
bool(true)
int(2)

QuickHashIntStringHash::loadFromFile

This factory method creates a hash from a file

说明

public <span class="modifier">static <span class="type">QuickHashIntStringHash <span class="methodname">QuickHashIntStringHash::loadFromFile ( <span class="methodparam">string $filename [, int $size<span class="initializer"> = 0 [, <span class="methodparam">int $options<span class="initializer"> = 0 ]] )

This factory method creates a new hash from a definition file on disk. The file format consists of a signature 'QH\0x12\0', the number of elements as a 32 bit signed integer in system Endianness, an unsigned 32 bit integer containing the number of element data to follow in characters. This element data contains all the strings. After the header and the strings, the elements follow in pairs of two unsigned 32 bit integers where the first one is the key, and the second one the index in the element data string. An example could be:

示例 #1 QuickHash IntString file format

00000000  51 48 12 00 02 00 00 00  09 00 00 00 4f 4e 45 00  |QH..........ONE.|
00000010  4e 49 4e 45 00 01 00 00  00 00 00 00 00 03 00 00  |NINE............|
00000020  00 04 00 00 00                                    |.....|
00000025

示例 #2 QuickHash IntString file format

header signature ('QH'; key type: 1; value type: 2; filler: \0x00)
00000000  51 48 12 00

number of elements:
00000004  02 00 00 00

length of string values (9 characters):
00000008  09 00 00 00

string values:
0000000C  4f 4e 45 00 4e 49 4e 45  00

data string:
00000015  01 00 00 00 00 00 00 00  03 00 00 00 04 00 00 00

key/value 1 (key = 1, string index = 0 ("ONE")):
01 00 00 00  00 00 00 00

key/value 2 (key = 3, string index = 4 ("NINE")):
03 00 00 00  04 00 00 00

参数

filename
The filename of the file to read the hash from.

size
The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options
The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the hash, rounded up to the nearest power of two with a maximum limit of 4194304.

返回值

Returns a new QuickHashIntStringHash.

范例

示例 #3 <span class="function">QuickHashIntStringHash::loadFromFile example

<?php
$file = dirname( __FILE__ ) . "/simple.string.hash";
$hash = QuickHashIntStringHash::loadFromFile(
    $file,
    QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key, 
        $hash->exists( $key ) ? 'set' : 'unset'
    );
}
?>

以上例程的输出类似于:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

QuickHashIntStringHash::loadFromString

This factory method creates a hash from a string

说明

public <span class="modifier">static <span class="type">QuickHashIntStringHash <span class="methodname">QuickHashIntStringHash::loadFromString ( <span class="methodparam">string $contents [, int $size<span class="initializer"> = 0 [, <span class="methodparam">int $options<span class="initializer"> = 0 ]] )

This factory method creates a new hash from a definition in a string. The format is the same as the one used in "loadFromFile".

参数

contents
The string containing a serialized format of the hash.

size
The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options
The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the hash, rounded up to the nearest power of two with a maximum limit of 4194304.

返回值

Returns a new QuickHashIntStringHash.

范例

示例 #1 <span class="function">QuickHashIntStringHash::loadFromString example

<?php
$contents = file_get_contents( dirname( __FILE__ ) . "/simple.hash" );
$hash = QuickHashIntStringHash::loadFromString(
    $contents,
    QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key, 
        $hash->exists( $key ) ? 'set' : 'unset'
    );
}
?>

以上例程的输出类似于:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

QuickHashIntStringHash::saveToFile

This method stores an in-memory hash to disk

说明

public void QuickHashIntStringHash::saveToFile ( string $filename )

This method stores an existing hash to a file on disk, in the same format that loadFromFile() can read.

参数

filename
The filename of the file to store the hash in.

返回值

没有返回值。

范例

示例 #1 <span class="function">QuickHashIntStringHash::saveToFile example

<?php
$hash = new QuickHashIntStringHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, "fourty three" ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, "fifty two" ) );

$hash->saveToFile( '/tmp/test.string.hash' );
?>

QuickHashIntStringHash::saveToString

This method returns a serialized version of the hash

说明

public string QuickHashIntStringHash::saveToString ( void )

This method returns a serialized version of the hash in the same format that loadFromString() can read.

返回值

This method returns a string containing a serialized format of the hash. Each element is stored as a four byte value in the Endianness that the current system uses.

范例

示例 #1 <span class="function">QuickHashIntStringHash::saveToString example

<?php
$hash = new QuickHashIntStringHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, "thirty four" ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 5, "fifty five" ) );

var_dump( $hash->saveToString() );
?>

QuickHashIntStringHash::set

This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

说明

public int <span class="methodname">QuickHashIntStringHash::set ( <span class="methodparam">int $key , <span class="methodparam">string $value )

This method tries to update an entry with a new value. In case the entry did not yet exist, it will instead add a new entry. It returns whether the entry was added or update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashIntStringHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

参数

key
The key of the entry to add or update.

value
The value of the entry to add. If a non-string is passed, it will be converted to a string automatically if possible.

返回值

2 if the entry was found and updated, 1 if the entry was newly added or 0 if there was an error.

范例

示例 #1 QuickHashIntStringHash::set example

<?php
$hash = new QuickHashIntStringHash( 1024 );

echo "Set->Add\n";
var_dump( $hash->get( 46692 ) );
var_dump( $hash->set( 46692, "sixteen thousand ninety one" ) );
var_dump( $hash->get( 46692 ) );

echo "Set->Update\n";
var_dump( $hash->set( 46692, "twenty nine thousand nine hundred six" ) );
var_dump( $hash->get( 46692 ) );
?>

以上例程的输出类似于:

Set->Add
bool(false)
int(2)
string(27) "sixteen thousand ninety one"
Set->Update
int(1)
string(37) "twenty nine thousand nine hundred six"

QuickHashIntStringHash::update

This method updates an entry in the hash with a new value

说明

public bool QuickHashIntStringHash::update ( <span class="methodparam">int $key , <span class="methodparam">string $value )

This method updates an entry with a new value, and returns whether the entry was update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashIntStringHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

参数

key
The key of the entry to add.

value
The new value for the entry. If a non-string is passed, it will be converted to a string automatically if possible.

返回值

true when the entry was found and updated, and false if the entry was not part of the hash already.

范例

示例 #1 QuickHashIntStringHash::update example

<?php
$hash->add( 161803398, "--" );
$hash->add( 314159265, "a lot" );

echo $hash->get( 161803398 ), "\n";
echo $hash->get( 314159265 ), "\n";

var_dump( $hash->update( 314159265, "a lot plus one" ) );
var_dump( $hash->update( 314159999, "a lot plus one" ) );

echo $hash->get( 161803398 ), "\n";
echo $hash->get( 314159265 ), "\n";
?>

以上例程的输出类似于:

--
a lot
bool(true)
bool(false)
--
a lot plus one

本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除